blob: 924b291f43f7f4787952af85adc1f16f53483b84 [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 Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Gilles Peskine996deb12018-08-01 15:45:45 +020081#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
82
Gilles Peskine9ef733f2018-02-07 21:05:37 +010083/* constant-time buffer comparison */
84static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
85{
86 size_t i;
87 unsigned char diff = 0;
88
89 for( i = 0; i < n; i++ )
90 diff |= a[i] ^ b[i];
91
92 return( diff );
93}
94
95
96
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101static int key_type_is_raw_bytes( psa_key_type_t type )
102{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200103 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine2d277862018-06-18 15:41:12 +0200111typedef struct
112{
Gilles Peskine5e769522018-11-20 21:59:56 +0100113 void (* entropy_init )( mbedtls_entropy_context *ctx );
114 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100115 mbedtls_entropy_context entropy;
116 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100118 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119} psa_global_data_t;
120
121static psa_global_data_t global_data;
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
124 if( global_data.initialized == 0 ) \
125 return( PSA_ERROR_BAD_STATE );
126
Gilles Peskinee59236f2018-01-27 23:32:46 +0100127static psa_status_t mbedtls_to_psa_error( int ret )
128{
Gilles Peskinea5905292018-02-07 20:59:33 +0100129 /* If there's both a high-level code and low-level code, dispatch on
130 * the high-level code. */
131 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 {
133 case 0:
134 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100135
136 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
137 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
138 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
139 return( PSA_ERROR_NOT_SUPPORTED );
140 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
141 return( PSA_ERROR_HARDWARE_FAILURE );
142
143 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
147 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
148 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
149 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
150 case MBEDTLS_ERR_ASN1_INVALID_DATA:
151 return( PSA_ERROR_INVALID_ARGUMENT );
152 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
153 return( PSA_ERROR_INSUFFICIENT_MEMORY );
154 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
155 return( PSA_ERROR_BUFFER_TOO_SMALL );
156
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000158 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100160 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000168 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
173 return( PSA_ERROR_NOT_SUPPORTED );
174 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
175 return( PSA_ERROR_HARDWARE_FAILURE );
176
177 case MBEDTLS_ERR_CCM_BAD_INPUT:
178 return( PSA_ERROR_INVALID_ARGUMENT );
179 case MBEDTLS_ERR_CCM_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
181 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
182 return( PSA_ERROR_HARDWARE_FAILURE );
183
Gilles Peskine26869f22019-05-06 15:25:00 +0200184 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
185 return( PSA_ERROR_INVALID_ARGUMENT );
186
187 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
188 return( PSA_ERROR_BAD_STATE );
189 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
195 return( PSA_ERROR_INVALID_ARGUMENT );
196 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
198 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
199 return( PSA_ERROR_INVALID_PADDING );
200 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200205 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
213 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
214 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
215 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
216 return( PSA_ERROR_NOT_SUPPORTED );
217 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219
220 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
231 return( PSA_ERROR_INVALID_SIGNATURE );
232 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200233 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
237 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
238 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MD_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
249 return( PSA_ERROR_STORAGE_FAILURE );
250 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
260 return( PSA_ERROR_BUFFER_TOO_SMALL );
261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
284 return( PSA_ERROR_NOT_PERMITTED );
285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
294 return( PSA_ERROR_HARDWARE_FAILURE );
295
Gilles Peskineff2d2002019-05-06 15:26:23 +0200296 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
299 return( PSA_ERROR_NOT_SUPPORTED );
300
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303
304 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_RSA_INVALID_PADDING:
307 return( PSA_ERROR_INVALID_PADDING );
308 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
309 return( PSA_ERROR_HARDWARE_FAILURE );
310 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
313 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200314 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
316 return( PSA_ERROR_INVALID_SIGNATURE );
317 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
318 return( PSA_ERROR_BUFFER_TOO_SMALL );
319 case MBEDTLS_ERR_RSA_RNG_FAILED:
320 return( PSA_ERROR_INSUFFICIENT_MEMORY );
321 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
327 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
itayzafrir5c753392018-05-08 11:18:38 +0300336 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300337 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300338 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
340 return( PSA_ERROR_BUFFER_TOO_SMALL );
341 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
342 return( PSA_ERROR_NOT_SUPPORTED );
343 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
344 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
348 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300350
Gilles Peskinee59236f2018-01-27 23:32:46 +0100351 default:
David Saadab4ecc272019-02-14 13:48:10 +0200352 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100353 }
354}
355
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200356
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200357
358
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359/****************************************************************/
360/* Key management */
361/****************************************************************/
362
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100363#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200364static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
365{
366 switch( grpid )
367 {
368 case MBEDTLS_ECP_DP_SECP192R1:
369 return( PSA_ECC_CURVE_SECP192R1 );
370 case MBEDTLS_ECP_DP_SECP224R1:
371 return( PSA_ECC_CURVE_SECP224R1 );
372 case MBEDTLS_ECP_DP_SECP256R1:
373 return( PSA_ECC_CURVE_SECP256R1 );
374 case MBEDTLS_ECP_DP_SECP384R1:
375 return( PSA_ECC_CURVE_SECP384R1 );
376 case MBEDTLS_ECP_DP_SECP521R1:
377 return( PSA_ECC_CURVE_SECP521R1 );
378 case MBEDTLS_ECP_DP_BP256R1:
379 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
380 case MBEDTLS_ECP_DP_BP384R1:
381 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
382 case MBEDTLS_ECP_DP_BP512R1:
383 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
384 case MBEDTLS_ECP_DP_CURVE25519:
385 return( PSA_ECC_CURVE_CURVE25519 );
386 case MBEDTLS_ECP_DP_SECP192K1:
387 return( PSA_ECC_CURVE_SECP192K1 );
388 case MBEDTLS_ECP_DP_SECP224K1:
389 return( PSA_ECC_CURVE_SECP224K1 );
390 case MBEDTLS_ECP_DP_SECP256K1:
391 return( PSA_ECC_CURVE_SECP256K1 );
392 case MBEDTLS_ECP_DP_CURVE448:
393 return( PSA_ECC_CURVE_CURVE448 );
394 default:
395 return( 0 );
396 }
397}
398
Gilles Peskine12313cd2018-06-20 00:20:32 +0200399static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
400{
401 switch( curve )
402 {
403 case PSA_ECC_CURVE_SECP192R1:
404 return( MBEDTLS_ECP_DP_SECP192R1 );
405 case PSA_ECC_CURVE_SECP224R1:
406 return( MBEDTLS_ECP_DP_SECP224R1 );
407 case PSA_ECC_CURVE_SECP256R1:
408 return( MBEDTLS_ECP_DP_SECP256R1 );
409 case PSA_ECC_CURVE_SECP384R1:
410 return( MBEDTLS_ECP_DP_SECP384R1 );
411 case PSA_ECC_CURVE_SECP521R1:
412 return( MBEDTLS_ECP_DP_SECP521R1 );
413 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
414 return( MBEDTLS_ECP_DP_BP256R1 );
415 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
416 return( MBEDTLS_ECP_DP_BP384R1 );
417 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
418 return( MBEDTLS_ECP_DP_BP512R1 );
419 case PSA_ECC_CURVE_CURVE25519:
420 return( MBEDTLS_ECP_DP_CURVE25519 );
421 case PSA_ECC_CURVE_SECP192K1:
422 return( MBEDTLS_ECP_DP_SECP192K1 );
423 case PSA_ECC_CURVE_SECP224K1:
424 return( MBEDTLS_ECP_DP_SECP224K1 );
425 case PSA_ECC_CURVE_SECP256K1:
426 return( MBEDTLS_ECP_DP_SECP256K1 );
427 case PSA_ECC_CURVE_CURVE448:
428 return( MBEDTLS_ECP_DP_CURVE448 );
429 default:
430 return( MBEDTLS_ECP_DP_NONE );
431 }
432}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100433#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200434
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200435static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
436 size_t bits,
437 struct raw_data *raw )
438{
439 /* Check that the bit size is acceptable for the key type */
440 switch( type )
441 {
442 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200443 if( bits == 0 )
444 {
445 raw->bytes = 0;
446 raw->data = NULL;
447 return( PSA_SUCCESS );
448 }
449 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
491 /* Allocate memory for the key */
492 raw->bytes = PSA_BITS_TO_BYTES( bits );
493 raw->data = mbedtls_calloc( 1, raw->bytes );
494 if( raw->data == NULL )
495 {
496 raw->bytes = 0;
497 return( PSA_ERROR_INSUFFICIENT_MEMORY );
498 }
499 return( PSA_SUCCESS );
500}
501
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200502#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100503/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
504 * that are not a multiple of 8) well. For example, there is only
505 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
506 * way to return the exact bit size of a key.
507 * To keep things simple, reject non-byte-aligned key sizes. */
508static psa_status_t psa_check_rsa_key_byte_aligned(
509 const mbedtls_rsa_context *rsa )
510{
511 mbedtls_mpi n;
512 psa_status_t status;
513 mbedtls_mpi_init( &n );
514 status = mbedtls_to_psa_error(
515 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
516 if( status == PSA_SUCCESS )
517 {
518 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
519 status = PSA_ERROR_NOT_SUPPORTED;
520 }
521 mbedtls_mpi_free( &n );
522 return( status );
523}
524
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000525static psa_status_t psa_import_rsa_key( psa_key_type_t type,
526 const uint8_t *data,
527 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200528 mbedtls_rsa_context **p_rsa )
529{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000530 psa_status_t status;
531 mbedtls_pk_context pk;
532 mbedtls_rsa_context *rsa;
533 size_t bits;
534
535 mbedtls_pk_init( &pk );
536
537 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200538 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000539 status = mbedtls_to_psa_error(
540 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000542 status = mbedtls_to_psa_error(
543 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
544 if( status != PSA_SUCCESS )
545 goto exit;
546
547 /* We have something that the pkparse module recognizes. If it is a
548 * valid RSA key, store it. */
549 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000551 status = PSA_ERROR_INVALID_ARGUMENT;
552 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200553 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000554
555 rsa = mbedtls_pk_rsa( pk );
556 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
557 * supports non-byte-aligned key sizes, but not well. For example,
558 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
559 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 {
562 status = PSA_ERROR_NOT_SUPPORTED;
563 goto exit;
564 }
565 status = psa_check_rsa_key_byte_aligned( rsa );
566
567exit:
568 /* Free the content of the pk object only on error. */
569 if( status != PSA_SUCCESS )
570 {
571 mbedtls_pk_free( &pk );
572 return( status );
573 }
574
575 /* On success, store the content of the object in the RSA context. */
576 *p_rsa = rsa;
577
578 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200579}
580#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
581
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000582#if defined(MBEDTLS_ECP_C)
583
584/* Import a public key given as the uncompressed representation defined by SEC1
585 * 2.3.3 as the content of an ECPoint. */
586static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
587 const uint8_t *data,
588 size_t data_length,
589 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592 mbedtls_ecp_keypair *ecp = NULL;
593 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
594
595 *p_ecp = NULL;
596 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
597 if( ecp == NULL )
598 return( PSA_ERROR_INSUFFICIENT_MEMORY );
599 mbedtls_ecp_keypair_init( ecp );
600
601 /* Load the group. */
602 status = mbedtls_to_psa_error(
603 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
604 if( status != PSA_SUCCESS )
605 goto exit;
606 /* Load the public value. */
607 status = mbedtls_to_psa_error(
608 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
609 data, data_length ) );
610 if( status != PSA_SUCCESS )
611 goto exit;
612
613 /* Check that the point is on the curve. */
614 status = mbedtls_to_psa_error(
615 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
616 if( status != PSA_SUCCESS )
617 goto exit;
618
619 *p_ecp = ecp;
620 return( PSA_SUCCESS );
621
622exit:
623 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200624 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000625 mbedtls_ecp_keypair_free( ecp );
626 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200627 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000628 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200629}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000630#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631
Gilles Peskinef76aa772018-10-29 19:24:33 +0100632#if defined(MBEDTLS_ECP_C)
633/* Import a private key given as a byte string which is the private value
634 * in big-endian order. */
635static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
636 const uint8_t *data,
637 size_t data_length,
638 mbedtls_ecp_keypair **p_ecp )
639{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100641 mbedtls_ecp_keypair *ecp = NULL;
642 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
643
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200644 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
645 return( PSA_ERROR_INVALID_ARGUMENT );
646
Gilles Peskinef76aa772018-10-29 19:24:33 +0100647 *p_ecp = NULL;
648 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
649 if( ecp == NULL )
650 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000651 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652
653 /* Load the group. */
654 status = mbedtls_to_psa_error(
655 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
656 if( status != PSA_SUCCESS )
657 goto exit;
658 /* Load the secret value. */
659 status = mbedtls_to_psa_error(
660 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
661 if( status != PSA_SUCCESS )
662 goto exit;
663 /* Validate the private key. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Calculate the public key from the private key. */
669 status = mbedtls_to_psa_error(
670 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
671 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 *p_ecp = ecp;
676 return( PSA_SUCCESS );
677
678exit:
679 if( ecp != NULL )
680 {
681 mbedtls_ecp_keypair_free( ecp );
682 mbedtls_free( ecp );
683 }
684 return( status );
685}
686#endif /* defined(MBEDTLS_ECP_C) */
687
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100688/** Import key data into a slot. `slot->type` must have been set
689 * previously. This function assumes that the slot does not contain
690 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100691psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
692 const uint8_t *data,
693 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200695 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696
Darryl Green940d72c2018-07-13 13:18:51 +0100697 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100699 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 if( data_length > SIZE_MAX / 8 )
701 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100702 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200703 PSA_BYTES_TO_BITS( data_length ),
704 &slot->data.raw );
705 if( status != PSA_SUCCESS )
706 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200707 if( data_length != 0 )
708 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 }
710 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100711#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100713 {
Darryl Green940d72c2018-07-13 13:18:51 +0100714 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100715 data, data_length,
716 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000718 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
719 {
720 status = psa_import_ec_public_key(
721 PSA_KEY_TYPE_GET_CURVE( slot->type ),
722 data, data_length,
723 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000724 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 else
726#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000727#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
728 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000730 status = psa_import_rsa_key( slot->type,
731 data, data_length,
732 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733 }
734 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000735#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 {
737 return( PSA_ERROR_NOT_SUPPORTED );
738 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000739 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100740}
741
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100742/** Calculate the intersection of two algorithm usage policies.
743 *
744 * Return 0 (which allows no operation) on incompatibility.
745 */
746static psa_algorithm_t psa_key_policy_algorithm_intersection(
747 psa_algorithm_t alg1,
748 psa_algorithm_t alg2 )
749{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200750 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100751 if( alg1 == alg2 )
752 return( alg1 );
753 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100754 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100755 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
756 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
757 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
758 {
759 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
760 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100761 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100762 return( alg1 );
763 }
764 /* If the policies are incompatible, allow nothing. */
765 return( 0 );
766}
767
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200768static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
769 psa_algorithm_t requested_alg )
770{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200771 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200772 if( requested_alg == policy_alg )
773 return( 1 );
774 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200775 * and requested_alg is the same hash-and-sign family with any hash,
776 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200777 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
778 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
779 {
780 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
781 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
782 }
783 /* If it isn't permitted, it's forbidden. */
784 return( 0 );
785}
786
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100787/** Test whether a policy permits an algorithm.
788 *
789 * The caller must test usage flags separately.
790 */
791static int psa_key_policy_permits( const psa_key_policy_t *policy,
792 psa_algorithm_t alg )
793{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200794 return( psa_key_algorithm_permits( policy->alg, alg ) ||
795 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100796}
797
Gilles Peskinef603c712019-01-19 13:40:11 +0100798/** Restrict a key policy based on a constraint.
799 *
800 * \param[in,out] policy The policy to restrict.
801 * \param[in] constraint The policy constraint to apply.
802 *
803 * \retval #PSA_SUCCESS
804 * \c *policy contains the intersection of the original value of
805 * \c *policy and \c *constraint.
806 * \retval #PSA_ERROR_INVALID_ARGUMENT
807 * \c *policy and \c *constraint are incompatible.
808 * \c *policy is unchanged.
809 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100810static psa_status_t psa_restrict_key_policy(
811 psa_key_policy_t *policy,
812 const psa_key_policy_t *constraint )
813{
814 psa_algorithm_t intersection_alg =
815 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200816 psa_algorithm_t intersection_alg2 =
817 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100818 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
819 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200820 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
821 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100822 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100823 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200824 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100825 return( PSA_SUCCESS );
826}
827
Darryl Green06fd18d2018-07-16 11:21:11 +0100828/** Retrieve a slot which must contain a key. The key must have allow all the
829 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
830 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100831static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100832 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100833 psa_key_usage_t usage,
834 psa_algorithm_t alg )
835{
836 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100837 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100838
839 *p_slot = NULL;
840
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100842 if( status != PSA_SUCCESS )
843 return( status );
844 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200845 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100846
847 /* Enforce that usage policy for the key slot contains all the flags
848 * required by the usage parameter. There is one exception: public
849 * keys can always be exported, so we treat public key objects as
850 * if they had the export flag. */
851 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
852 usage &= ~PSA_KEY_USAGE_EXPORT;
853 if( ( slot->policy.usage & usage ) != usage )
854 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100855
856 /* Enforce that the usage policy permits the requested algortihm. */
857 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100858 return( PSA_ERROR_NOT_PERMITTED );
859
860 *p_slot = slot;
861 return( PSA_SUCCESS );
862}
Darryl Green940d72c2018-07-13 13:18:51 +0100863
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100864/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100865static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000866{
867 if( slot->type == PSA_KEY_TYPE_NONE )
868 {
869 /* No key material to clean. */
870 }
871 else if( key_type_is_raw_bytes( slot->type ) )
872 {
873 mbedtls_free( slot->data.raw.data );
874 }
875 else
876#if defined(MBEDTLS_RSA_C)
877 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
878 {
879 mbedtls_rsa_free( slot->data.rsa );
880 mbedtls_free( slot->data.rsa );
881 }
882 else
883#endif /* defined(MBEDTLS_RSA_C) */
884#if defined(MBEDTLS_ECP_C)
885 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
886 {
887 mbedtls_ecp_keypair_free( slot->data.ecp );
888 mbedtls_free( slot->data.ecp );
889 }
890 else
891#endif /* defined(MBEDTLS_ECP_C) */
892 {
893 /* Shouldn't happen: the key type is not any type that we
894 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200895 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000896 }
897
898 return( PSA_SUCCESS );
899}
900
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100901static void psa_abort_operations_using_key( psa_key_slot_t *slot )
902{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200903 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100904 (void) slot;
905}
906
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100907/** Completely wipe a slot in memory, including its policy.
908 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100909psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100910{
911 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100912 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100913 /* At this point, key material and other type-specific content has
914 * been wiped. Clear remaining metadata. We can call memset and not
915 * zeroize because the metadata is not particularly sensitive. */
916 memset( slot, 0, sizeof( *slot ) );
917 return( status );
918}
919
Gilles Peskinec5487a82018-12-03 18:08:14 +0100920psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100922 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100923 psa_status_t status = PSA_SUCCESS;
924 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100925
Gilles Peskinec5487a82018-12-03 18:08:14 +0100926 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200927 if( status != PSA_SUCCESS )
928 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100929#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
930 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
931 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100932 storage_status =
933 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100934 }
935#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100936 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100937 if( status != PSA_SUCCESS )
938 return( status );
939 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940}
941
Gilles Peskineb870b182018-07-06 16:02:09 +0200942/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200943static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200944{
945 if( key_type_is_raw_bytes( slot->type ) )
946 return( slot->data.raw.bytes * 8 );
947#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200948 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100949 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200950#endif /* defined(MBEDTLS_RSA_C) */
951#if defined(MBEDTLS_ECP_C)
952 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
953 return( slot->data.ecp->grp.pbits );
954#endif /* defined(MBEDTLS_ECP_C) */
955 /* Shouldn't happen except on an empty slot. */
956 return( 0 );
957}
958
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200959void psa_reset_key_attributes( psa_key_attributes_t *attributes )
960{
Gilles Peskineb699f072019-04-26 16:06:02 +0200961 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200962 memset( attributes, 0, sizeof( *attributes ) );
963}
964
Gilles Peskineb699f072019-04-26 16:06:02 +0200965psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
966 psa_key_type_t type,
967 const uint8_t *data,
968 size_t data_length )
969{
970 uint8_t *copy = NULL;
971
972 if( data_length != 0 )
973 {
974 copy = mbedtls_calloc( 1, data_length );
975 if( copy == NULL )
976 return( PSA_ERROR_INSUFFICIENT_MEMORY );
977 memcpy( copy, data, data_length );
978 }
979 /* After this point, this function is guaranteed to succeed, so it
980 * can start modifying `*attributes`. */
981
982 if( attributes->domain_parameters != NULL )
983 {
984 mbedtls_free( attributes->domain_parameters );
985 attributes->domain_parameters = NULL;
986 attributes->domain_parameters_size = 0;
987 }
988
989 attributes->domain_parameters = copy;
990 attributes->domain_parameters_size = data_length;
991 attributes->type = type;
992 return( PSA_SUCCESS );
993}
994
995psa_status_t psa_get_key_domain_parameters(
996 const psa_key_attributes_t *attributes,
997 uint8_t *data, size_t data_size, size_t *data_length )
998{
999 if( attributes->domain_parameters_size > data_size )
1000 return( PSA_ERROR_BUFFER_TOO_SMALL );
1001 *data_length = attributes->domain_parameters_size;
1002 if( attributes->domain_parameters_size != 0 )
1003 memcpy( data, attributes->domain_parameters,
1004 attributes->domain_parameters_size );
1005 return( PSA_SUCCESS );
1006}
1007
1008#if defined(MBEDTLS_RSA_C)
1009static psa_status_t psa_get_rsa_public_exponent(
1010 const mbedtls_rsa_context *rsa,
1011 psa_key_attributes_t *attributes )
1012{
1013 mbedtls_mpi mpi;
1014 int ret;
1015 uint8_t *buffer = NULL;
1016 size_t buflen;
1017 mbedtls_mpi_init( &mpi );
1018
1019 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1020 if( ret != 0 )
1021 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001022 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1023 {
1024 /* It's the default value, which is reported as an empty string,
1025 * so there's nothing to do. */
1026 goto exit;
1027 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001028
1029 buflen = mbedtls_mpi_size( &mpi );
1030 buffer = mbedtls_calloc( 1, buflen );
1031 if( buffer == NULL )
1032 {
1033 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1034 goto exit;
1035 }
1036 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1037 if( ret != 0 )
1038 goto exit;
1039 attributes->domain_parameters = buffer;
1040 attributes->domain_parameters_size = buflen;
1041
1042exit:
1043 mbedtls_mpi_free( &mpi );
1044 if( ret != 0 )
1045 mbedtls_free( buffer );
1046 return( mbedtls_to_psa_error( ret ) );
1047}
1048#endif /* MBEDTLS_RSA_C */
1049
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001050psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1051 psa_key_attributes_t *attributes )
1052{
1053 psa_key_slot_t *slot;
1054 psa_status_t status;
1055
1056 psa_reset_key_attributes( attributes );
1057
1058 status = psa_get_key_slot( handle, &slot );
1059 if( status != PSA_SUCCESS )
1060 return( status );
1061
1062 attributes->id = slot->persistent_storage_id;
1063 attributes->lifetime = slot->lifetime;
1064 attributes->policy = slot->policy;
1065 attributes->type = slot->type;
1066 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001067
1068 switch( slot->type )
1069 {
1070#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001071 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001072 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1073 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1074 break;
1075#endif
1076 default:
1077 /* Nothing else to do. */
1078 break;
1079 }
1080
1081 if( status != PSA_SUCCESS )
1082 psa_reset_key_attributes( attributes );
1083 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001084}
1085
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001086#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001087static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1088 unsigned char *buf, size_t size )
1089{
1090 int ret;
1091 unsigned char *c;
1092 size_t len = 0;
1093
1094 c = buf + size;
1095
1096 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1097
1098 return( (int) len );
1099}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001100#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001101
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001102static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1103 uint8_t *data,
1104 size_t data_size,
1105 size_t *data_length,
1106 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001107{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001108 *data_length = 0;
1109
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001110 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001111 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001112
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001113 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114 {
1115 if( slot->data.raw.bytes > data_size )
1116 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001117 if( data_size != 0 )
1118 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001119 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001120 memset( data + slot->data.raw.bytes, 0,
1121 data_size - slot->data.raw.bytes );
1122 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 *data_length = slot->data.raw.bytes;
1124 return( PSA_SUCCESS );
1125 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001126#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001127 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001128 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001129 psa_status_t status;
1130
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001131 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001132 if( bytes > data_size )
1133 return( PSA_ERROR_BUFFER_TOO_SMALL );
1134 status = mbedtls_to_psa_error(
1135 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1136 if( status != PSA_SUCCESS )
1137 return( status );
1138 memset( data + bytes, 0, data_size - bytes );
1139 *data_length = bytes;
1140 return( PSA_SUCCESS );
1141 }
1142#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143 else
Moran Peker17e36e12018-05-02 12:55:20 +03001144 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001145#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001146 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001147 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001148 {
Moran Pekera998bc62018-04-16 18:16:20 +03001149 mbedtls_pk_context pk;
1150 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001151 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001152 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001153#if defined(MBEDTLS_RSA_C)
1154 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001155 pk.pk_info = &mbedtls_rsa_info;
1156 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001157#else
1158 return( PSA_ERROR_NOT_SUPPORTED );
1159#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001160 }
1161 else
1162 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001163#if defined(MBEDTLS_ECP_C)
1164 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001165 pk.pk_info = &mbedtls_eckey_info;
1166 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001167#else
1168 return( PSA_ERROR_NOT_SUPPORTED );
1169#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001170 }
Moran Pekerd7326592018-05-29 16:56:39 +03001171 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001172 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001173 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001174 }
Moran Peker17e36e12018-05-02 12:55:20 +03001175 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001176 {
Moran Peker17e36e12018-05-02 12:55:20 +03001177 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001178 }
Moran Peker60364322018-04-29 11:34:58 +03001179 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001180 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001181 /* If data_size is 0 then data may be NULL and then the
1182 * call to memset would have undefined behavior. */
1183 if( data_size != 0 )
1184 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001185 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001186 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001187 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1188 * Move the data to the beginning and erase remaining data
1189 * at the original location. */
1190 if( 2 * (size_t) ret <= data_size )
1191 {
1192 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001193 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001194 }
1195 else if( (size_t) ret < data_size )
1196 {
1197 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001198 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001199 }
Moran Pekera998bc62018-04-16 18:16:20 +03001200 *data_length = ret;
1201 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001202 }
1203 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001204#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001205 {
1206 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001207 it is valid for a special-purpose implementation to omit
1208 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001209 return( PSA_ERROR_NOT_SUPPORTED );
1210 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211 }
1212}
1213
Gilles Peskinec5487a82018-12-03 18:08:14 +01001214psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001215 uint8_t *data,
1216 size_t data_size,
1217 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001218{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001219 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001220 psa_status_t status;
1221
1222 /* Set the key to empty now, so that even when there are errors, we always
1223 * set data_length to a value between 0 and data_size. On error, setting
1224 * the key to empty is a good choice because an empty key representation is
1225 * unlikely to be accepted anywhere. */
1226 *data_length = 0;
1227
1228 /* Export requires the EXPORT flag. There is an exception for public keys,
1229 * which don't require any flag, but psa_get_key_from_slot takes
1230 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001231 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001232 if( status != PSA_SUCCESS )
1233 return( status );
1234 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001235 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001237
Gilles Peskinec5487a82018-12-03 18:08:14 +01001238psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001239 uint8_t *data,
1240 size_t data_size,
1241 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001242{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001243 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001244 psa_status_t status;
1245
1246 /* Set the key to empty now, so that even when there are errors, we always
1247 * set data_length to a value between 0 and data_size. On error, setting
1248 * the key to empty is a good choice because an empty key representation is
1249 * unlikely to be accepted anywhere. */
1250 *data_length = 0;
1251
1252 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001253 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001254 if( status != PSA_SUCCESS )
1255 return( status );
1256 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001257 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001258}
1259
Gilles Peskine4747d192019-04-17 15:05:45 +02001260static psa_status_t psa_set_key_policy_internal(
1261 psa_key_slot_t *slot,
1262 const psa_key_policy_t *policy )
1263{
1264 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001265 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001266 PSA_KEY_USAGE_ENCRYPT |
1267 PSA_KEY_USAGE_DECRYPT |
1268 PSA_KEY_USAGE_SIGN |
1269 PSA_KEY_USAGE_VERIFY |
1270 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1271 return( PSA_ERROR_INVALID_ARGUMENT );
1272
1273 slot->policy = *policy;
1274 return( PSA_SUCCESS );
1275}
1276
1277/** Prepare a key slot to receive key material.
1278 *
1279 * This function allocates a key slot and sets its metadata.
1280 *
1281 * If this function fails, call psa_fail_key_creation().
1282 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001283 * This function is intended to be used as follows:
1284 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1285 * it with the specified attributes, and assign it a handle.
1286 * -# Populate the slot with the key material.
1287 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1288 * In case of failure at any step, stop the sequence and call
1289 * psa_fail_key_creation().
1290 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001291 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001292 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001293 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001294 *
1295 * \retval #PSA_SUCCESS
1296 * The key slot is ready to receive key material.
1297 * \return If this function fails, the key slot is an invalid state.
1298 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001299 */
1300static psa_status_t psa_start_key_creation(
1301 const psa_key_attributes_t *attributes,
1302 psa_key_handle_t *handle,
1303 psa_key_slot_t **p_slot )
1304{
1305 psa_status_t status;
1306 psa_key_slot_t *slot;
1307
Gilles Peskine267c6562019-05-27 19:01:54 +02001308 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001309 if( status != PSA_SUCCESS )
1310 return( status );
1311 slot = *p_slot;
1312
1313 status = psa_set_key_policy_internal( slot, &attributes->policy );
1314 if( status != PSA_SUCCESS )
1315 return( status );
1316 slot->lifetime = attributes->lifetime;
1317 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001318 {
1319 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +02001320 attributes->id, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001321 if( status != PSA_SUCCESS )
1322 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001323 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001324 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001325 slot->type = attributes->type;
1326
1327 return( status );
1328}
1329
1330/** Finalize the creation of a key once its key material has been set.
1331 *
1332 * This entails writing the key to persistent storage.
1333 *
1334 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001335 * See the documentation of psa_start_key_creation() for the intended use
1336 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001337 *
1338 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001339 *
1340 * \retval #PSA_SUCCESS
1341 * The key was successfully created. The handle is now valid.
1342 * \return If this function fails, the key slot is an invalid state.
1343 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001344 */
1345static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1346{
1347 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001348 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001349
1350#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1351 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1352 {
1353 uint8_t *buffer = NULL;
1354 size_t buffer_size = 0;
1355 size_t length;
1356
1357 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001358 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001359 buffer = mbedtls_calloc( 1, buffer_size );
1360 if( buffer == NULL && buffer_size != 0 )
1361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1362 status = psa_internal_export_key( slot,
1363 buffer, buffer_size, &length,
1364 0 );
1365
1366 if( status == PSA_SUCCESS )
1367 {
1368 status = psa_save_persistent_key( slot->persistent_storage_id,
1369 slot->type, &slot->policy,
1370 buffer, length );
1371 }
1372
1373 if( buffer_size != 0 )
1374 mbedtls_platform_zeroize( buffer, buffer_size );
1375 mbedtls_free( buffer );
1376 }
1377#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1378
1379 return( status );
1380}
1381
1382/** Abort the creation of a key.
1383 *
1384 * You may call this function after calling psa_start_key_creation(),
1385 * or after psa_finish_key_creation() fails. In other circumstances, this
1386 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001387 * See the documentation of psa_start_key_creation() for the intended use
1388 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001389 *
1390 * \param slot Pointer to the slot with key material.
1391 */
1392static void psa_fail_key_creation( psa_key_slot_t *slot )
1393{
1394 if( slot == NULL )
1395 return;
1396 psa_wipe_key_slot( slot );
1397}
1398
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001399static psa_status_t psa_check_key_slot_attributes(
1400 const psa_key_slot_t *slot,
1401 const psa_key_attributes_t *attributes )
1402{
1403 if( attributes->type != 0 )
1404 {
1405 if( attributes->type != slot->type )
1406 return( PSA_ERROR_INVALID_ARGUMENT );
1407 }
1408
1409 if( attributes->domain_parameters_size != 0 )
1410 {
1411#if defined(MBEDTLS_RSA_C)
1412 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1413 {
1414 mbedtls_mpi actual, required;
1415 int ret;
1416 mbedtls_mpi_init( &actual );
1417 mbedtls_mpi_init( &required );
1418 ret = mbedtls_rsa_export( slot->data.rsa,
1419 NULL, NULL, NULL, NULL, &actual );
1420 if( ret != 0 )
1421 goto rsa_exit;
1422 ret = mbedtls_mpi_read_binary( &required,
1423 attributes->domain_parameters,
1424 attributes->domain_parameters_size );
1425 if( ret != 0 )
1426 goto rsa_exit;
1427 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1428 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1429 rsa_exit:
1430 mbedtls_mpi_free( &actual );
1431 mbedtls_mpi_free( &required );
1432 if( ret != 0)
1433 return( mbedtls_to_psa_error( ret ) );
1434 }
1435 else
1436#endif
1437 {
1438 return( PSA_ERROR_INVALID_ARGUMENT );
1439 }
1440 }
1441
1442 if( attributes->bits != 0 )
1443 {
1444 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1445 return( PSA_ERROR_INVALID_ARGUMENT );
1446 }
1447
1448 return( PSA_SUCCESS );
1449}
1450
Gilles Peskine4747d192019-04-17 15:05:45 +02001451psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001453 size_t data_length,
1454 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001455{
1456 psa_status_t status;
1457 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001458
Gilles Peskine4747d192019-04-17 15:05:45 +02001459 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001460 if( status != PSA_SUCCESS )
1461 goto exit;
1462
1463 status = psa_import_key_into_slot( slot, data, data_length );
1464 if( status != PSA_SUCCESS )
1465 goto exit;
1466 status = psa_check_key_slot_attributes( slot, attributes );
1467 if( status != PSA_SUCCESS )
1468 goto exit;
1469
1470 status = psa_finish_key_creation( slot );
1471exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001472 if( status != PSA_SUCCESS )
1473 {
1474 psa_fail_key_creation( slot );
1475 *handle = 0;
1476 }
1477 return( status );
1478}
1479
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001480static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001481 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001482{
1483 psa_status_t status;
1484 uint8_t *buffer = NULL;
1485 size_t buffer_size = 0;
1486 size_t length;
1487
1488 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001489 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001490 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001491 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001492 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001493 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1494 if( status != PSA_SUCCESS )
1495 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496 target->type = source->type;
1497 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001498
1499exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001500 if( buffer_size != 0 )
1501 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001502 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001503 return( status );
1504}
1505
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001506psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1507 const psa_key_attributes_t *specified_attributes,
1508 psa_key_handle_t *target_handle )
1509{
1510 psa_status_t status;
1511 psa_key_slot_t *source_slot = NULL;
1512 psa_key_slot_t *target_slot = NULL;
1513 psa_key_attributes_t actual_attributes = *specified_attributes;
1514
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001515 status = psa_get_key_from_slot( source_handle, &source_slot,
1516 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001517 if( status != PSA_SUCCESS )
1518 goto exit;
1519
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001520 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1521 if( status != PSA_SUCCESS )
1522 goto exit;
1523
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 status = psa_restrict_key_policy( &actual_attributes.policy,
1525 &source_slot->policy );
1526 if( status != PSA_SUCCESS )
1527 goto exit;
1528
1529 status = psa_start_key_creation( &actual_attributes,
1530 target_handle, &target_slot );
1531 if( status != PSA_SUCCESS )
1532 goto exit;
1533
1534 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001535 if( status != PSA_SUCCESS )
1536 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001537
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001538 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001540 if( status != PSA_SUCCESS )
1541 {
1542 psa_fail_key_creation( target_slot );
1543 *target_handle = 0;
1544 }
1545 return( status );
1546}
1547
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001548
1549
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001551/* Message digests */
1552/****************************************************************/
1553
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001554static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001555{
1556 switch( alg )
1557 {
1558#if defined(MBEDTLS_MD2_C)
1559 case PSA_ALG_MD2:
1560 return( &mbedtls_md2_info );
1561#endif
1562#if defined(MBEDTLS_MD4_C)
1563 case PSA_ALG_MD4:
1564 return( &mbedtls_md4_info );
1565#endif
1566#if defined(MBEDTLS_MD5_C)
1567 case PSA_ALG_MD5:
1568 return( &mbedtls_md5_info );
1569#endif
1570#if defined(MBEDTLS_RIPEMD160_C)
1571 case PSA_ALG_RIPEMD160:
1572 return( &mbedtls_ripemd160_info );
1573#endif
1574#if defined(MBEDTLS_SHA1_C)
1575 case PSA_ALG_SHA_1:
1576 return( &mbedtls_sha1_info );
1577#endif
1578#if defined(MBEDTLS_SHA256_C)
1579 case PSA_ALG_SHA_224:
1580 return( &mbedtls_sha224_info );
1581 case PSA_ALG_SHA_256:
1582 return( &mbedtls_sha256_info );
1583#endif
1584#if defined(MBEDTLS_SHA512_C)
1585 case PSA_ALG_SHA_384:
1586 return( &mbedtls_sha384_info );
1587 case PSA_ALG_SHA_512:
1588 return( &mbedtls_sha512_info );
1589#endif
1590 default:
1591 return( NULL );
1592 }
1593}
1594
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001595psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1596{
1597 switch( operation->alg )
1598 {
Gilles Peskine81736312018-06-26 15:04:31 +02001599 case 0:
1600 /* The object has (apparently) been initialized but it is not
1601 * in use. It's ok to call abort on such an object, and there's
1602 * nothing to do. */
1603 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001604#if defined(MBEDTLS_MD2_C)
1605 case PSA_ALG_MD2:
1606 mbedtls_md2_free( &operation->ctx.md2 );
1607 break;
1608#endif
1609#if defined(MBEDTLS_MD4_C)
1610 case PSA_ALG_MD4:
1611 mbedtls_md4_free( &operation->ctx.md4 );
1612 break;
1613#endif
1614#if defined(MBEDTLS_MD5_C)
1615 case PSA_ALG_MD5:
1616 mbedtls_md5_free( &operation->ctx.md5 );
1617 break;
1618#endif
1619#if defined(MBEDTLS_RIPEMD160_C)
1620 case PSA_ALG_RIPEMD160:
1621 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1622 break;
1623#endif
1624#if defined(MBEDTLS_SHA1_C)
1625 case PSA_ALG_SHA_1:
1626 mbedtls_sha1_free( &operation->ctx.sha1 );
1627 break;
1628#endif
1629#if defined(MBEDTLS_SHA256_C)
1630 case PSA_ALG_SHA_224:
1631 case PSA_ALG_SHA_256:
1632 mbedtls_sha256_free( &operation->ctx.sha256 );
1633 break;
1634#endif
1635#if defined(MBEDTLS_SHA512_C)
1636 case PSA_ALG_SHA_384:
1637 case PSA_ALG_SHA_512:
1638 mbedtls_sha512_free( &operation->ctx.sha512 );
1639 break;
1640#endif
1641 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001642 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001643 }
1644 operation->alg = 0;
1645 return( PSA_SUCCESS );
1646}
1647
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001648psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001649 psa_algorithm_t alg )
1650{
1651 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001652
1653 /* A context must be freshly initialized before it can be set up. */
1654 if( operation->alg != 0 )
1655 {
1656 return( PSA_ERROR_BAD_STATE );
1657 }
1658
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001659 switch( alg )
1660 {
1661#if defined(MBEDTLS_MD2_C)
1662 case PSA_ALG_MD2:
1663 mbedtls_md2_init( &operation->ctx.md2 );
1664 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1665 break;
1666#endif
1667#if defined(MBEDTLS_MD4_C)
1668 case PSA_ALG_MD4:
1669 mbedtls_md4_init( &operation->ctx.md4 );
1670 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1671 break;
1672#endif
1673#if defined(MBEDTLS_MD5_C)
1674 case PSA_ALG_MD5:
1675 mbedtls_md5_init( &operation->ctx.md5 );
1676 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1677 break;
1678#endif
1679#if defined(MBEDTLS_RIPEMD160_C)
1680 case PSA_ALG_RIPEMD160:
1681 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1682 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1683 break;
1684#endif
1685#if defined(MBEDTLS_SHA1_C)
1686 case PSA_ALG_SHA_1:
1687 mbedtls_sha1_init( &operation->ctx.sha1 );
1688 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1689 break;
1690#endif
1691#if defined(MBEDTLS_SHA256_C)
1692 case PSA_ALG_SHA_224:
1693 mbedtls_sha256_init( &operation->ctx.sha256 );
1694 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1695 break;
1696 case PSA_ALG_SHA_256:
1697 mbedtls_sha256_init( &operation->ctx.sha256 );
1698 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1699 break;
1700#endif
1701#if defined(MBEDTLS_SHA512_C)
1702 case PSA_ALG_SHA_384:
1703 mbedtls_sha512_init( &operation->ctx.sha512 );
1704 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1705 break;
1706 case PSA_ALG_SHA_512:
1707 mbedtls_sha512_init( &operation->ctx.sha512 );
1708 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1709 break;
1710#endif
1711 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001712 return( PSA_ALG_IS_HASH( alg ) ?
1713 PSA_ERROR_NOT_SUPPORTED :
1714 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001715 }
1716 if( ret == 0 )
1717 operation->alg = alg;
1718 else
1719 psa_hash_abort( operation );
1720 return( mbedtls_to_psa_error( ret ) );
1721}
1722
1723psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1724 const uint8_t *input,
1725 size_t input_length )
1726{
1727 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001728
1729 /* Don't require hash implementations to behave correctly on a
1730 * zero-length input, which may have an invalid pointer. */
1731 if( input_length == 0 )
1732 return( PSA_SUCCESS );
1733
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001734 switch( operation->alg )
1735 {
1736#if defined(MBEDTLS_MD2_C)
1737 case PSA_ALG_MD2:
1738 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1739 input, input_length );
1740 break;
1741#endif
1742#if defined(MBEDTLS_MD4_C)
1743 case PSA_ALG_MD4:
1744 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1745 input, input_length );
1746 break;
1747#endif
1748#if defined(MBEDTLS_MD5_C)
1749 case PSA_ALG_MD5:
1750 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1751 input, input_length );
1752 break;
1753#endif
1754#if defined(MBEDTLS_RIPEMD160_C)
1755 case PSA_ALG_RIPEMD160:
1756 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1757 input, input_length );
1758 break;
1759#endif
1760#if defined(MBEDTLS_SHA1_C)
1761 case PSA_ALG_SHA_1:
1762 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1763 input, input_length );
1764 break;
1765#endif
1766#if defined(MBEDTLS_SHA256_C)
1767 case PSA_ALG_SHA_224:
1768 case PSA_ALG_SHA_256:
1769 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1770 input, input_length );
1771 break;
1772#endif
1773#if defined(MBEDTLS_SHA512_C)
1774 case PSA_ALG_SHA_384:
1775 case PSA_ALG_SHA_512:
1776 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1777 input, input_length );
1778 break;
1779#endif
1780 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001781 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001782 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001783
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001784 if( ret != 0 )
1785 psa_hash_abort( operation );
1786 return( mbedtls_to_psa_error( ret ) );
1787}
1788
1789psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1790 uint8_t *hash,
1791 size_t hash_size,
1792 size_t *hash_length )
1793{
itayzafrir40835d42018-08-02 13:14:17 +03001794 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001795 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001796 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001797
1798 /* Fill the output buffer with something that isn't a valid hash
1799 * (barring an attack on the hash and deliberately-crafted input),
1800 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001801 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001802 /* If hash_size is 0 then hash may be NULL and then the
1803 * call to memset would have undefined behavior. */
1804 if( hash_size != 0 )
1805 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001806
1807 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001808 {
1809 status = PSA_ERROR_BUFFER_TOO_SMALL;
1810 goto exit;
1811 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001812
1813 switch( operation->alg )
1814 {
1815#if defined(MBEDTLS_MD2_C)
1816 case PSA_ALG_MD2:
1817 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1818 break;
1819#endif
1820#if defined(MBEDTLS_MD4_C)
1821 case PSA_ALG_MD4:
1822 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1823 break;
1824#endif
1825#if defined(MBEDTLS_MD5_C)
1826 case PSA_ALG_MD5:
1827 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1828 break;
1829#endif
1830#if defined(MBEDTLS_RIPEMD160_C)
1831 case PSA_ALG_RIPEMD160:
1832 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1833 break;
1834#endif
1835#if defined(MBEDTLS_SHA1_C)
1836 case PSA_ALG_SHA_1:
1837 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1838 break;
1839#endif
1840#if defined(MBEDTLS_SHA256_C)
1841 case PSA_ALG_SHA_224:
1842 case PSA_ALG_SHA_256:
1843 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1844 break;
1845#endif
1846#if defined(MBEDTLS_SHA512_C)
1847 case PSA_ALG_SHA_384:
1848 case PSA_ALG_SHA_512:
1849 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1850 break;
1851#endif
1852 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001853 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001854 }
itayzafrir40835d42018-08-02 13:14:17 +03001855 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001856
itayzafrir40835d42018-08-02 13:14:17 +03001857exit:
1858 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001859 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001860 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001861 return( psa_hash_abort( operation ) );
1862 }
1863 else
1864 {
1865 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001866 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001867 }
1868}
1869
Gilles Peskine2d277862018-06-18 15:41:12 +02001870psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1871 const uint8_t *hash,
1872 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001873{
1874 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1875 size_t actual_hash_length;
1876 psa_status_t status = psa_hash_finish( operation,
1877 actual_hash, sizeof( actual_hash ),
1878 &actual_hash_length );
1879 if( status != PSA_SUCCESS )
1880 return( status );
1881 if( actual_hash_length != hash_length )
1882 return( PSA_ERROR_INVALID_SIGNATURE );
1883 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1884 return( PSA_ERROR_INVALID_SIGNATURE );
1885 return( PSA_SUCCESS );
1886}
1887
Gilles Peskineeb35d782019-01-22 17:56:16 +01001888psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1889 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001890{
1891 if( target_operation->alg != 0 )
1892 return( PSA_ERROR_BAD_STATE );
1893
1894 switch( source_operation->alg )
1895 {
1896 case 0:
1897 return( PSA_ERROR_BAD_STATE );
1898#if defined(MBEDTLS_MD2_C)
1899 case PSA_ALG_MD2:
1900 mbedtls_md2_clone( &target_operation->ctx.md2,
1901 &source_operation->ctx.md2 );
1902 break;
1903#endif
1904#if defined(MBEDTLS_MD4_C)
1905 case PSA_ALG_MD4:
1906 mbedtls_md4_clone( &target_operation->ctx.md4,
1907 &source_operation->ctx.md4 );
1908 break;
1909#endif
1910#if defined(MBEDTLS_MD5_C)
1911 case PSA_ALG_MD5:
1912 mbedtls_md5_clone( &target_operation->ctx.md5,
1913 &source_operation->ctx.md5 );
1914 break;
1915#endif
1916#if defined(MBEDTLS_RIPEMD160_C)
1917 case PSA_ALG_RIPEMD160:
1918 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1919 &source_operation->ctx.ripemd160 );
1920 break;
1921#endif
1922#if defined(MBEDTLS_SHA1_C)
1923 case PSA_ALG_SHA_1:
1924 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1925 &source_operation->ctx.sha1 );
1926 break;
1927#endif
1928#if defined(MBEDTLS_SHA256_C)
1929 case PSA_ALG_SHA_224:
1930 case PSA_ALG_SHA_256:
1931 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1932 &source_operation->ctx.sha256 );
1933 break;
1934#endif
1935#if defined(MBEDTLS_SHA512_C)
1936 case PSA_ALG_SHA_384:
1937 case PSA_ALG_SHA_512:
1938 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1939 &source_operation->ctx.sha512 );
1940 break;
1941#endif
1942 default:
1943 return( PSA_ERROR_NOT_SUPPORTED );
1944 }
1945
1946 target_operation->alg = source_operation->alg;
1947 return( PSA_SUCCESS );
1948}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001949
1950
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001951/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001952/* MAC */
1953/****************************************************************/
1954
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001955static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001956 psa_algorithm_t alg,
1957 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001958 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001959 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001960{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001961 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001962 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001963
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001964 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001965 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001966
Gilles Peskine8c9def32018-02-08 10:02:12 +01001967 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1968 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001969 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001970 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001971 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02001972 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001973 mode = MBEDTLS_MODE_STREAM;
1974 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001975 case PSA_ALG_CTR:
1976 mode = MBEDTLS_MODE_CTR;
1977 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001978 case PSA_ALG_CFB:
1979 mode = MBEDTLS_MODE_CFB;
1980 break;
1981 case PSA_ALG_OFB:
1982 mode = MBEDTLS_MODE_OFB;
1983 break;
1984 case PSA_ALG_CBC_NO_PADDING:
1985 mode = MBEDTLS_MODE_CBC;
1986 break;
1987 case PSA_ALG_CBC_PKCS7:
1988 mode = MBEDTLS_MODE_CBC;
1989 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001990 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991 mode = MBEDTLS_MODE_CCM;
1992 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001993 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994 mode = MBEDTLS_MODE_GCM;
1995 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02001996 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
1997 mode = MBEDTLS_MODE_CHACHAPOLY;
1998 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001999 default:
2000 return( NULL );
2001 }
2002 }
2003 else if( alg == PSA_ALG_CMAC )
2004 mode = MBEDTLS_MODE_ECB;
2005 else if( alg == PSA_ALG_GMAC )
2006 mode = MBEDTLS_MODE_GCM;
2007 else
2008 return( NULL );
2009
2010 switch( key_type )
2011 {
2012 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002013 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014 break;
2015 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002016 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2017 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002019 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002021 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002022 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2023 * but two-key Triple-DES is functionally three-key Triple-DES
2024 * with K1=K3, so that's how we present it to mbedtls. */
2025 if( key_bits == 128 )
2026 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 break;
2028 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002029 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030 break;
2031 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002032 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002033 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002034 case PSA_KEY_TYPE_CHACHA20:
2035 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2036 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002037 default:
2038 return( NULL );
2039 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002040 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002041 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042
Jaeden Amero23bbb752018-06-26 14:16:54 +01002043 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2044 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045}
2046
Gilles Peskinea05219c2018-11-16 16:02:56 +01002047#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002048static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002049{
Gilles Peskine2d277862018-06-18 15:41:12 +02002050 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002051 {
2052 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002053 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002054 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002055 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002056 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002057 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002058 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002059 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002060 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002061 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002062 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002063 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002064 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002065 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002066 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002067 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002068 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002069 return( 128 );
2070 default:
2071 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002072 }
2073}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002074#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002075
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002076/* Initialize the MAC operation structure. Once this function has been
2077 * called, psa_mac_abort can run and will do the right thing. */
2078static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2079 psa_algorithm_t alg )
2080{
2081 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2082
2083 operation->alg = alg;
2084 operation->key_set = 0;
2085 operation->iv_set = 0;
2086 operation->iv_required = 0;
2087 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002088 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002089
2090#if defined(MBEDTLS_CMAC_C)
2091 if( alg == PSA_ALG_CMAC )
2092 {
2093 operation->iv_required = 0;
2094 mbedtls_cipher_init( &operation->ctx.cmac );
2095 status = PSA_SUCCESS;
2096 }
2097 else
2098#endif /* MBEDTLS_CMAC_C */
2099#if defined(MBEDTLS_MD_C)
2100 if( PSA_ALG_IS_HMAC( operation->alg ) )
2101 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002102 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2103 operation->ctx.hmac.hash_ctx.alg = 0;
2104 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105 }
2106 else
2107#endif /* MBEDTLS_MD_C */
2108 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002109 if( ! PSA_ALG_IS_MAC( alg ) )
2110 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002111 }
2112
2113 if( status != PSA_SUCCESS )
2114 memset( operation, 0, sizeof( *operation ) );
2115 return( status );
2116}
2117
Gilles Peskine01126fa2018-07-12 17:04:55 +02002118#if defined(MBEDTLS_MD_C)
2119static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2120{
Gilles Peskine3f108122018-12-07 18:14:53 +01002121 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002122 return( psa_hash_abort( &hmac->hash_ctx ) );
2123}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002124
2125static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2126{
2127 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2128 memset( hmac, 0, sizeof( *hmac ) );
2129}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002130#endif /* MBEDTLS_MD_C */
2131
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2133{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002134 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002135 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002136 /* The object has (apparently) been initialized but it is not
2137 * in use. It's ok to call abort on such an object, and there's
2138 * nothing to do. */
2139 return( PSA_SUCCESS );
2140 }
2141 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002142#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002143 if( operation->alg == PSA_ALG_CMAC )
2144 {
2145 mbedtls_cipher_free( &operation->ctx.cmac );
2146 }
2147 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002148#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002149#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002150 if( PSA_ALG_IS_HMAC( operation->alg ) )
2151 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002152 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002153 }
2154 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002155#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002156 {
2157 /* Sanity check (shouldn't happen: operation->alg should
2158 * always have been initialized to a valid value). */
2159 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160 }
Moran Peker41deec42018-04-04 15:43:05 +03002161
Gilles Peskine8c9def32018-02-08 10:02:12 +01002162 operation->alg = 0;
2163 operation->key_set = 0;
2164 operation->iv_set = 0;
2165 operation->iv_required = 0;
2166 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002167 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002168
Gilles Peskine8c9def32018-02-08 10:02:12 +01002169 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002170
2171bad_state:
2172 /* If abort is called on an uninitialized object, we can't trust
2173 * anything. Wipe the object in case it contains confidential data.
2174 * This may result in a memory leak if a pointer gets overwritten,
2175 * but it's too late to do anything about this. */
2176 memset( operation, 0, sizeof( *operation ) );
2177 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002178}
2179
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002180#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002181static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002182 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002183 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002184 const mbedtls_cipher_info_t *cipher_info )
2185{
2186 int ret;
2187
2188 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002189
2190 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2191 if( ret != 0 )
2192 return( ret );
2193
2194 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2195 slot->data.raw.data,
2196 key_bits );
2197 return( ret );
2198}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002199#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002200
Gilles Peskine248051a2018-06-20 16:09:38 +02002201#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002202static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2203 const uint8_t *key,
2204 size_t key_length,
2205 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002206{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002207 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002208 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002209 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002210 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002211 psa_status_t status;
2212
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002213 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2214 * overflow below. This should never trigger if the hash algorithm
2215 * is implemented correctly. */
2216 /* The size checks against the ipad and opad buffers cannot be written
2217 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2218 * because that triggers -Wlogical-op on GCC 7.3. */
2219 if( block_size > sizeof( ipad ) )
2220 return( PSA_ERROR_NOT_SUPPORTED );
2221 if( block_size > sizeof( hmac->opad ) )
2222 return( PSA_ERROR_NOT_SUPPORTED );
2223 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002224 return( PSA_ERROR_NOT_SUPPORTED );
2225
Gilles Peskined223b522018-06-11 18:12:58 +02002226 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002227 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002228 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002229 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002230 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002231 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002232 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002233 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002234 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002235 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002236 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002237 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002238 }
Gilles Peskine96889972018-07-12 17:07:03 +02002239 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2240 * but it is permitted. It is common when HMAC is used in HKDF, for
2241 * example. Don't call `memcpy` in the 0-length because `key` could be
2242 * an invalid pointer which would make the behavior undefined. */
2243 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002244 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002245
Gilles Peskined223b522018-06-11 18:12:58 +02002246 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2247 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002248 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002249 ipad[i] ^= 0x36;
2250 memset( ipad + key_length, 0x36, block_size - key_length );
2251
2252 /* Copy the key material from ipad to opad, flipping the requisite bits,
2253 * and filling the rest of opad with the requisite constant. */
2254 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002255 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2256 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002257
Gilles Peskine01126fa2018-07-12 17:04:55 +02002258 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002259 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002260 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002261
Gilles Peskine01126fa2018-07-12 17:04:55 +02002262 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002263
2264cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002265 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002266
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002267 return( status );
2268}
Gilles Peskine248051a2018-06-20 16:09:38 +02002269#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002270
Gilles Peskine89167cb2018-07-08 20:12:23 +02002271static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002272 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002273 psa_algorithm_t alg,
2274 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002275{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002277 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002279 psa_key_usage_t usage =
2280 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002281 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002282 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002283
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284 /* A context must be freshly initialized before it can be set up. */
2285 if( operation->alg != 0 )
2286 {
2287 return( PSA_ERROR_BAD_STATE );
2288 }
2289
Gilles Peskined911eb72018-08-14 15:18:45 +02002290 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002291 if( status != PSA_SUCCESS )
2292 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002293 if( is_sign )
2294 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002295
Gilles Peskinec5487a82018-12-03 18:08:14 +01002296 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002297 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002298 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002299 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002300
Gilles Peskine8c9def32018-02-08 10:02:12 +01002301#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002302 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002303 {
2304 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002305 mbedtls_cipher_info_from_psa( full_length_alg,
2306 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002307 int ret;
2308 if( cipher_info == NULL )
2309 {
2310 status = PSA_ERROR_NOT_SUPPORTED;
2311 goto exit;
2312 }
2313 operation->mac_size = cipher_info->block_size;
2314 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2315 status = mbedtls_to_psa_error( ret );
2316 }
2317 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002318#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002320 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002321 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002322 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002323 if( hash_alg == 0 )
2324 {
2325 status = PSA_ERROR_NOT_SUPPORTED;
2326 goto exit;
2327 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002328
2329 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2330 /* Sanity check. This shouldn't fail on a valid configuration. */
2331 if( operation->mac_size == 0 ||
2332 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2333 {
2334 status = PSA_ERROR_NOT_SUPPORTED;
2335 goto exit;
2336 }
2337
Gilles Peskine01126fa2018-07-12 17:04:55 +02002338 if( slot->type != PSA_KEY_TYPE_HMAC )
2339 {
2340 status = PSA_ERROR_INVALID_ARGUMENT;
2341 goto exit;
2342 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002343
Gilles Peskine01126fa2018-07-12 17:04:55 +02002344 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2345 slot->data.raw.data,
2346 slot->data.raw.bytes,
2347 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002348 }
2349 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002351 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002352 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002353 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002355
Gilles Peskined911eb72018-08-14 15:18:45 +02002356 if( truncated == 0 )
2357 {
2358 /* The "normal" case: untruncated algorithm. Nothing to do. */
2359 }
2360 else if( truncated < 4 )
2361 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002362 /* A very short MAC is too short for security since it can be
2363 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2364 * so we make this our minimum, even though 32 bits is still
2365 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002366 status = PSA_ERROR_NOT_SUPPORTED;
2367 }
2368 else if( truncated > operation->mac_size )
2369 {
2370 /* It's impossible to "truncate" to a larger length. */
2371 status = PSA_ERROR_INVALID_ARGUMENT;
2372 }
2373 else
2374 operation->mac_size = truncated;
2375
Gilles Peskinefbfac682018-07-08 20:51:54 +02002376exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002377 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002379 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002381 else
2382 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002383 operation->key_set = 1;
2384 }
2385 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386}
2387
Gilles Peskine89167cb2018-07-08 20:12:23 +02002388psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002389 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002390 psa_algorithm_t alg )
2391{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002392 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002393}
2394
2395psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002396 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002397 psa_algorithm_t alg )
2398{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002399 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002400}
2401
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2403 const uint8_t *input,
2404 size_t input_length )
2405{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002406 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002408 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002410 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 operation->has_input = 1;
2412
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002414 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002415 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2417 input, input_length );
2418 status = mbedtls_to_psa_error( ret );
2419 }
2420 else
2421#endif /* MBEDTLS_CMAC_C */
2422#if defined(MBEDTLS_MD_C)
2423 if( PSA_ALG_IS_HMAC( operation->alg ) )
2424 {
2425 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2426 input_length );
2427 }
2428 else
2429#endif /* MBEDTLS_MD_C */
2430 {
2431 /* This shouldn't happen if `operation` was initialized by
2432 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002433 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002434 }
2435
Gilles Peskinefbfac682018-07-08 20:51:54 +02002436 if( status != PSA_SUCCESS )
2437 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002438 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439}
2440
Gilles Peskine01126fa2018-07-12 17:04:55 +02002441#if defined(MBEDTLS_MD_C)
2442static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2443 uint8_t *mac,
2444 size_t mac_size )
2445{
2446 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2447 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2448 size_t hash_size = 0;
2449 size_t block_size = psa_get_hash_block_size( hash_alg );
2450 psa_status_t status;
2451
Gilles Peskine01126fa2018-07-12 17:04:55 +02002452 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2453 if( status != PSA_SUCCESS )
2454 return( status );
2455 /* From here on, tmp needs to be wiped. */
2456
2457 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2458 if( status != PSA_SUCCESS )
2459 goto exit;
2460
2461 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2462 if( status != PSA_SUCCESS )
2463 goto exit;
2464
2465 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2466 if( status != PSA_SUCCESS )
2467 goto exit;
2468
Gilles Peskined911eb72018-08-14 15:18:45 +02002469 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2470 if( status != PSA_SUCCESS )
2471 goto exit;
2472
2473 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002474
2475exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002476 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002477 return( status );
2478}
2479#endif /* MBEDTLS_MD_C */
2480
mohammad16036df908f2018-04-02 08:34:15 -07002481static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002482 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002483 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002485 if( ! operation->key_set )
2486 return( PSA_ERROR_BAD_STATE );
2487 if( operation->iv_required && ! operation->iv_set )
2488 return( PSA_ERROR_BAD_STATE );
2489
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 if( mac_size < operation->mac_size )
2491 return( PSA_ERROR_BUFFER_TOO_SMALL );
2492
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002494 if( operation->alg == PSA_ALG_CMAC )
2495 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002496 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2497 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2498 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002499 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002500 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002501 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002503 else
2504#endif /* MBEDTLS_CMAC_C */
2505#if defined(MBEDTLS_MD_C)
2506 if( PSA_ALG_IS_HMAC( operation->alg ) )
2507 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002508 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002509 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002510 }
2511 else
2512#endif /* MBEDTLS_MD_C */
2513 {
2514 /* This shouldn't happen if `operation` was initialized by
2515 * a setup function. */
2516 return( PSA_ERROR_BAD_STATE );
2517 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518}
2519
Gilles Peskineacd4be32018-07-08 19:56:25 +02002520psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2521 uint8_t *mac,
2522 size_t mac_size,
2523 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002524{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002525 psa_status_t status;
2526
Jaeden Amero252ef282019-02-15 14:05:35 +00002527 if( operation->alg == 0 )
2528 {
2529 return( PSA_ERROR_BAD_STATE );
2530 }
2531
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002532 /* Fill the output buffer with something that isn't a valid mac
2533 * (barring an attack on the mac and deliberately-crafted input),
2534 * in case the caller doesn't check the return status properly. */
2535 *mac_length = mac_size;
2536 /* If mac_size is 0 then mac may be NULL and then the
2537 * call to memset would have undefined behavior. */
2538 if( mac_size != 0 )
2539 memset( mac, '!', mac_size );
2540
Gilles Peskine89167cb2018-07-08 20:12:23 +02002541 if( ! operation->is_sign )
2542 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002543 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002544 }
mohammad16036df908f2018-04-02 08:34:15 -07002545
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002546 status = psa_mac_finish_internal( operation, mac, mac_size );
2547
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002548 if( status == PSA_SUCCESS )
2549 {
2550 status = psa_mac_abort( operation );
2551 if( status == PSA_SUCCESS )
2552 *mac_length = operation->mac_size;
2553 else
2554 memset( mac, '!', mac_size );
2555 }
2556 else
2557 psa_mac_abort( operation );
2558 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002559}
2560
Gilles Peskineacd4be32018-07-08 19:56:25 +02002561psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2562 const uint8_t *mac,
2563 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564{
Gilles Peskine828ed142018-06-18 23:25:51 +02002565 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002566 psa_status_t status;
2567
Jaeden Amero252ef282019-02-15 14:05:35 +00002568 if( operation->alg == 0 )
2569 {
2570 return( PSA_ERROR_BAD_STATE );
2571 }
2572
Gilles Peskine89167cb2018-07-08 20:12:23 +02002573 if( operation->is_sign )
2574 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002575 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002576 }
2577 if( operation->mac_size != mac_length )
2578 {
2579 status = PSA_ERROR_INVALID_SIGNATURE;
2580 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002581 }
mohammad16036df908f2018-04-02 08:34:15 -07002582
2583 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002584 actual_mac, sizeof( actual_mac ) );
2585
2586 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2587 status = PSA_ERROR_INVALID_SIGNATURE;
2588
2589cleanup:
2590 if( status == PSA_SUCCESS )
2591 status = psa_mac_abort( operation );
2592 else
2593 psa_mac_abort( operation );
2594
Gilles Peskine3f108122018-12-07 18:14:53 +01002595 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002596
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002597 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598}
2599
2600
Gilles Peskine20035e32018-02-03 22:44:14 +01002601
Gilles Peskine20035e32018-02-03 22:44:14 +01002602/****************************************************************/
2603/* Asymmetric cryptography */
2604/****************************************************************/
2605
Gilles Peskine2b450e32018-06-27 15:42:46 +02002606#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002607/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002608 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002609static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2610 size_t hash_length,
2611 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002612{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002613 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002614 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002615 *md_alg = mbedtls_md_get_type( md_info );
2616
2617 /* The Mbed TLS RSA module uses an unsigned int for hash length
2618 * parameters. Validate that it fits so that we don't risk an
2619 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002620#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002621 if( hash_length > UINT_MAX )
2622 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002623#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002624
2625#if defined(MBEDTLS_PKCS1_V15)
2626 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2627 * must be correct. */
2628 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2629 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002630 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002631 if( md_info == NULL )
2632 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002633 if( mbedtls_md_get_size( md_info ) != hash_length )
2634 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002635 }
2636#endif /* MBEDTLS_PKCS1_V15 */
2637
2638#if defined(MBEDTLS_PKCS1_V21)
2639 /* PSS requires a hash internally. */
2640 if( PSA_ALG_IS_RSA_PSS( alg ) )
2641 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002642 if( md_info == NULL )
2643 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002644 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002645#endif /* MBEDTLS_PKCS1_V21 */
2646
Gilles Peskine61b91d42018-06-08 16:09:36 +02002647 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002648}
2649
Gilles Peskine2b450e32018-06-27 15:42:46 +02002650static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2651 psa_algorithm_t alg,
2652 const uint8_t *hash,
2653 size_t hash_length,
2654 uint8_t *signature,
2655 size_t signature_size,
2656 size_t *signature_length )
2657{
2658 psa_status_t status;
2659 int ret;
2660 mbedtls_md_type_t md_alg;
2661
2662 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2663 if( status != PSA_SUCCESS )
2664 return( status );
2665
Gilles Peskine630a18a2018-06-29 17:49:35 +02002666 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002667 return( PSA_ERROR_BUFFER_TOO_SMALL );
2668
2669#if defined(MBEDTLS_PKCS1_V15)
2670 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2671 {
2672 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2673 MBEDTLS_MD_NONE );
2674 ret = mbedtls_rsa_pkcs1_sign( rsa,
2675 mbedtls_ctr_drbg_random,
2676 &global_data.ctr_drbg,
2677 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002678 md_alg,
2679 (unsigned int) hash_length,
2680 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002681 signature );
2682 }
2683 else
2684#endif /* MBEDTLS_PKCS1_V15 */
2685#if defined(MBEDTLS_PKCS1_V21)
2686 if( PSA_ALG_IS_RSA_PSS( alg ) )
2687 {
2688 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2689 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2690 mbedtls_ctr_drbg_random,
2691 &global_data.ctr_drbg,
2692 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002693 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002694 (unsigned int) hash_length,
2695 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002696 signature );
2697 }
2698 else
2699#endif /* MBEDTLS_PKCS1_V21 */
2700 {
2701 return( PSA_ERROR_INVALID_ARGUMENT );
2702 }
2703
2704 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002705 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002706 return( mbedtls_to_psa_error( ret ) );
2707}
2708
2709static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2710 psa_algorithm_t alg,
2711 const uint8_t *hash,
2712 size_t hash_length,
2713 const uint8_t *signature,
2714 size_t signature_length )
2715{
2716 psa_status_t status;
2717 int ret;
2718 mbedtls_md_type_t md_alg;
2719
2720 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2721 if( status != PSA_SUCCESS )
2722 return( status );
2723
Gilles Peskine630a18a2018-06-29 17:49:35 +02002724 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002725 return( PSA_ERROR_BUFFER_TOO_SMALL );
2726
2727#if defined(MBEDTLS_PKCS1_V15)
2728 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2729 {
2730 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2731 MBEDTLS_MD_NONE );
2732 ret = mbedtls_rsa_pkcs1_verify( rsa,
2733 mbedtls_ctr_drbg_random,
2734 &global_data.ctr_drbg,
2735 MBEDTLS_RSA_PUBLIC,
2736 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002737 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002738 hash,
2739 signature );
2740 }
2741 else
2742#endif /* MBEDTLS_PKCS1_V15 */
2743#if defined(MBEDTLS_PKCS1_V21)
2744 if( PSA_ALG_IS_RSA_PSS( alg ) )
2745 {
2746 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2747 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2748 mbedtls_ctr_drbg_random,
2749 &global_data.ctr_drbg,
2750 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002751 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002752 (unsigned int) hash_length,
2753 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002754 signature );
2755 }
2756 else
2757#endif /* MBEDTLS_PKCS1_V21 */
2758 {
2759 return( PSA_ERROR_INVALID_ARGUMENT );
2760 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002761
2762 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2763 * the rest of the signature is invalid". This has little use in
2764 * practice and PSA doesn't report this distinction. */
2765 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2766 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002767 return( mbedtls_to_psa_error( ret ) );
2768}
2769#endif /* MBEDTLS_RSA_C */
2770
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002771#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002772/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2773 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2774 * (even though these functions don't modify it). */
2775static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2776 psa_algorithm_t alg,
2777 const uint8_t *hash,
2778 size_t hash_length,
2779 uint8_t *signature,
2780 size_t signature_size,
2781 size_t *signature_length )
2782{
2783 int ret;
2784 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002785 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002786 mbedtls_mpi_init( &r );
2787 mbedtls_mpi_init( &s );
2788
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002789 if( signature_size < 2 * curve_bytes )
2790 {
2791 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2792 goto cleanup;
2793 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002794
Gilles Peskinea05219c2018-11-16 16:02:56 +01002795#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002796 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2797 {
2798 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2799 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2800 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2801 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2802 hash, hash_length,
2803 md_alg ) );
2804 }
2805 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002806#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002807 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002808 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002809 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2810 hash, hash_length,
2811 mbedtls_ctr_drbg_random,
2812 &global_data.ctr_drbg ) );
2813 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002814
2815 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2816 signature,
2817 curve_bytes ) );
2818 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2819 signature + curve_bytes,
2820 curve_bytes ) );
2821
2822cleanup:
2823 mbedtls_mpi_free( &r );
2824 mbedtls_mpi_free( &s );
2825 if( ret == 0 )
2826 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002827 return( mbedtls_to_psa_error( ret ) );
2828}
2829
2830static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2831 const uint8_t *hash,
2832 size_t hash_length,
2833 const uint8_t *signature,
2834 size_t signature_length )
2835{
2836 int ret;
2837 mbedtls_mpi r, s;
2838 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2839 mbedtls_mpi_init( &r );
2840 mbedtls_mpi_init( &s );
2841
2842 if( signature_length != 2 * curve_bytes )
2843 return( PSA_ERROR_INVALID_SIGNATURE );
2844
2845 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2846 signature,
2847 curve_bytes ) );
2848 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2849 signature + curve_bytes,
2850 curve_bytes ) );
2851
2852 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2853 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002854
2855cleanup:
2856 mbedtls_mpi_free( &r );
2857 mbedtls_mpi_free( &s );
2858 return( mbedtls_to_psa_error( ret ) );
2859}
2860#endif /* MBEDTLS_ECDSA_C */
2861
Gilles Peskinec5487a82018-12-03 18:08:14 +01002862psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002863 psa_algorithm_t alg,
2864 const uint8_t *hash,
2865 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002866 uint8_t *signature,
2867 size_t signature_size,
2868 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002869{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002870 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002871 psa_status_t status;
2872
2873 *signature_length = signature_size;
2874
Gilles Peskinec5487a82018-12-03 18:08:14 +01002875 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002876 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002877 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002878 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002879 {
2880 status = PSA_ERROR_INVALID_ARGUMENT;
2881 goto exit;
2882 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002883
Gilles Peskine20035e32018-02-03 22:44:14 +01002884#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002885 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002886 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002887 status = psa_rsa_sign( slot->data.rsa,
2888 alg,
2889 hash, hash_length,
2890 signature, signature_size,
2891 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002892 }
2893 else
2894#endif /* defined(MBEDTLS_RSA_C) */
2895#if defined(MBEDTLS_ECP_C)
2896 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2897 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002898#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002899 if(
2900#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2901 PSA_ALG_IS_ECDSA( alg )
2902#else
2903 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2904#endif
2905 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002906 status = psa_ecdsa_sign( slot->data.ecp,
2907 alg,
2908 hash, hash_length,
2909 signature, signature_size,
2910 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002911 else
2912#endif /* defined(MBEDTLS_ECDSA_C) */
2913 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002914 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002915 }
itayzafrir5c753392018-05-08 11:18:38 +03002916 }
2917 else
2918#endif /* defined(MBEDTLS_ECP_C) */
2919 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002920 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002921 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002922
2923exit:
2924 /* Fill the unused part of the output buffer (the whole buffer on error,
2925 * the trailing part on success) with something that isn't a valid mac
2926 * (barring an attack on the mac and deliberately-crafted input),
2927 * in case the caller doesn't check the return status properly. */
2928 if( status == PSA_SUCCESS )
2929 memset( signature + *signature_length, '!',
2930 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002931 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002932 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002933 /* If signature_size is 0 then we have nothing to do. We must not call
2934 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002935 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002936}
2937
Gilles Peskinec5487a82018-12-03 18:08:14 +01002938psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002939 psa_algorithm_t alg,
2940 const uint8_t *hash,
2941 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002942 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002943 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002944{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002945 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002946 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002947
Gilles Peskinec5487a82018-12-03 18:08:14 +01002948 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002949 if( status != PSA_SUCCESS )
2950 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002951
Gilles Peskine61b91d42018-06-08 16:09:36 +02002952#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002953 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002955 return( psa_rsa_verify( slot->data.rsa,
2956 alg,
2957 hash, hash_length,
2958 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 }
2960 else
2961#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002962#if defined(MBEDTLS_ECP_C)
2963 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2964 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002965#if defined(MBEDTLS_ECDSA_C)
2966 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002967 return( psa_ecdsa_verify( slot->data.ecp,
2968 hash, hash_length,
2969 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002970 else
2971#endif /* defined(MBEDTLS_ECDSA_C) */
2972 {
2973 return( PSA_ERROR_INVALID_ARGUMENT );
2974 }
itayzafrir5c753392018-05-08 11:18:38 +03002975 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002976 else
2977#endif /* defined(MBEDTLS_ECP_C) */
2978 {
2979 return( PSA_ERROR_NOT_SUPPORTED );
2980 }
2981}
2982
Gilles Peskine072ac562018-06-30 00:21:29 +02002983#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2984static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2985 mbedtls_rsa_context *rsa )
2986{
2987 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2988 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2989 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2990 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2991}
2992#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2993
Gilles Peskinec5487a82018-12-03 18:08:14 +01002994psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002995 psa_algorithm_t alg,
2996 const uint8_t *input,
2997 size_t input_length,
2998 const uint8_t *salt,
2999 size_t salt_length,
3000 uint8_t *output,
3001 size_t output_size,
3002 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003003{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003004 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003005 psa_status_t status;
3006
Darryl Green5cc689a2018-07-24 15:34:10 +01003007 (void) input;
3008 (void) input_length;
3009 (void) salt;
3010 (void) output;
3011 (void) output_size;
3012
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003013 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003015 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3016 return( PSA_ERROR_INVALID_ARGUMENT );
3017
Gilles Peskinec5487a82018-12-03 18:08:14 +01003018 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003019 if( status != PSA_SUCCESS )
3020 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003021 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003022 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003023 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024
3025#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003026 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003027 {
3028 mbedtls_rsa_context *rsa = slot->data.rsa;
3029 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003030 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003031 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003033 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003035 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3036 mbedtls_ctr_drbg_random,
3037 &global_data.ctr_drbg,
3038 MBEDTLS_RSA_PUBLIC,
3039 input_length,
3040 input,
3041 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042 }
3043 else
3044#endif /* MBEDTLS_PKCS1_V15 */
3045#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003046 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003048 psa_rsa_oaep_set_padding_mode( alg, rsa );
3049 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3050 mbedtls_ctr_drbg_random,
3051 &global_data.ctr_drbg,
3052 MBEDTLS_RSA_PUBLIC,
3053 salt, salt_length,
3054 input_length,
3055 input,
3056 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057 }
3058 else
3059#endif /* MBEDTLS_PKCS1_V21 */
3060 {
3061 return( PSA_ERROR_INVALID_ARGUMENT );
3062 }
3063 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003064 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065 return( mbedtls_to_psa_error( ret ) );
3066 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003068#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 {
3070 return( PSA_ERROR_NOT_SUPPORTED );
3071 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003072}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073
Gilles Peskinec5487a82018-12-03 18:08:14 +01003074psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003075 psa_algorithm_t alg,
3076 const uint8_t *input,
3077 size_t input_length,
3078 const uint8_t *salt,
3079 size_t salt_length,
3080 uint8_t *output,
3081 size_t output_size,
3082 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003084 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003085 psa_status_t status;
3086
Darryl Green5cc689a2018-07-24 15:34:10 +01003087 (void) input;
3088 (void) input_length;
3089 (void) salt;
3090 (void) output;
3091 (void) output_size;
3092
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003093 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003094
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003095 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3096 return( PSA_ERROR_INVALID_ARGUMENT );
3097
Gilles Peskinec5487a82018-12-03 18:08:14 +01003098 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003099 if( status != PSA_SUCCESS )
3100 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003101 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102 return( PSA_ERROR_INVALID_ARGUMENT );
3103
3104#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003105 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106 {
3107 mbedtls_rsa_context *rsa = slot->data.rsa;
3108 int ret;
3109
Gilles Peskine630a18a2018-06-29 17:49:35 +02003110 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111 return( PSA_ERROR_INVALID_ARGUMENT );
3112
3113#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003114 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003116 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3117 mbedtls_ctr_drbg_random,
3118 &global_data.ctr_drbg,
3119 MBEDTLS_RSA_PRIVATE,
3120 output_length,
3121 input,
3122 output,
3123 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124 }
3125 else
3126#endif /* MBEDTLS_PKCS1_V15 */
3127#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003128 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003130 psa_rsa_oaep_set_padding_mode( alg, rsa );
3131 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3132 mbedtls_ctr_drbg_random,
3133 &global_data.ctr_drbg,
3134 MBEDTLS_RSA_PRIVATE,
3135 salt, salt_length,
3136 output_length,
3137 input,
3138 output,
3139 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140 }
3141 else
3142#endif /* MBEDTLS_PKCS1_V21 */
3143 {
3144 return( PSA_ERROR_INVALID_ARGUMENT );
3145 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003146
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 return( mbedtls_to_psa_error( ret ) );
3148 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003150#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 {
3152 return( PSA_ERROR_NOT_SUPPORTED );
3153 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003154}
Gilles Peskine20035e32018-02-03 22:44:14 +01003155
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003156
3157
mohammad1603503973b2018-03-12 15:59:30 +02003158/****************************************************************/
3159/* Symmetric cryptography */
3160/****************************************************************/
3161
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003162/* Initialize the cipher operation structure. Once this function has been
3163 * called, psa_cipher_abort can run and will do the right thing. */
3164static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3165 psa_algorithm_t alg )
3166{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003167 if( ! PSA_ALG_IS_CIPHER( alg ) )
3168 {
3169 memset( operation, 0, sizeof( *operation ) );
3170 return( PSA_ERROR_INVALID_ARGUMENT );
3171 }
3172
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173 operation->alg = alg;
3174 operation->key_set = 0;
3175 operation->iv_set = 0;
3176 operation->iv_required = 1;
3177 operation->iv_size = 0;
3178 operation->block_size = 0;
3179 mbedtls_cipher_init( &operation->ctx.cipher );
3180 return( PSA_SUCCESS );
3181}
3182
Gilles Peskinee553c652018-06-04 16:22:46 +02003183static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003184 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003185 psa_algorithm_t alg,
3186 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003187{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003188 int ret = 0;
3189 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003190 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003191 size_t key_bits;
3192 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003193 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3194 PSA_KEY_USAGE_ENCRYPT :
3195 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003196
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003197 /* A context must be freshly initialized before it can be set up. */
3198 if( operation->alg != 0 )
3199 {
3200 return( PSA_ERROR_BAD_STATE );
3201 }
3202
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003203 status = psa_cipher_init( operation, alg );
3204 if( status != PSA_SUCCESS )
3205 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003206
Gilles Peskinec5487a82018-12-03 18:08:14 +01003207 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003208 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003209 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003210 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003211
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003212 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003213 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003214 {
3215 status = PSA_ERROR_NOT_SUPPORTED;
3216 goto exit;
3217 }
mohammad1603503973b2018-03-12 15:59:30 +02003218
mohammad1603503973b2018-03-12 15:59:30 +02003219 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003220 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003221 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003222
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003223#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003224 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003225 {
3226 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3227 unsigned char keys[24];
3228 memcpy( keys, slot->data.raw.data, 16 );
3229 memcpy( keys + 16, slot->data.raw.data, 8 );
3230 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3231 keys,
3232 192, cipher_operation );
3233 }
3234 else
3235#endif
3236 {
3237 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3238 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003239 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003240 }
Moran Peker41deec42018-04-04 15:43:05 +03003241 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003242 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003243
mohammad16038481e742018-03-18 13:57:31 +02003244#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003245 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003246 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003247 case PSA_ALG_CBC_NO_PADDING:
3248 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3249 MBEDTLS_PADDING_NONE );
3250 break;
3251 case PSA_ALG_CBC_PKCS7:
3252 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3253 MBEDTLS_PADDING_PKCS7 );
3254 break;
3255 default:
3256 /* The algorithm doesn't involve padding. */
3257 ret = 0;
3258 break;
3259 }
3260 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003261 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003262#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3263
mohammad1603503973b2018-03-12 15:59:30 +02003264 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003265 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3266 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3267 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003268 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003269 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003270 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003271#if defined(MBEDTLS_CHACHA20_C)
3272 else
3273 if( alg == PSA_ALG_CHACHA20 )
3274 operation->iv_size = 12;
3275#endif
mohammad1603503973b2018-03-12 15:59:30 +02003276
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003277exit:
3278 if( status == 0 )
3279 status = mbedtls_to_psa_error( ret );
3280 if( status != 0 )
3281 psa_cipher_abort( operation );
3282 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003283}
3284
Gilles Peskinefe119512018-07-08 21:39:34 +02003285psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003286 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003287 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003288{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003289 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003290}
3291
Gilles Peskinefe119512018-07-08 21:39:34 +02003292psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003293 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003294 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003295{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003296 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003297}
3298
Gilles Peskinefe119512018-07-08 21:39:34 +02003299psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3300 unsigned char *iv,
3301 size_t iv_size,
3302 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003303{
itayzafrir534bd7c2018-08-02 13:56:32 +03003304 psa_status_t status;
3305 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003306 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003307 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003308 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003309 }
Moran Peker41deec42018-04-04 15:43:05 +03003310 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003311 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003312 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003313 goto exit;
3314 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003315 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3316 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003317 if( ret != 0 )
3318 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003319 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003320 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003321 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003322
mohammad16038481e742018-03-18 13:57:31 +02003323 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003324 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003325
Moran Peker395db872018-05-31 14:07:14 +03003326exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003327 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003328 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003329 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003330}
3331
Gilles Peskinefe119512018-07-08 21:39:34 +02003332psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3333 const unsigned char *iv,
3334 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003335{
itayzafrir534bd7c2018-08-02 13:56:32 +03003336 psa_status_t status;
3337 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003338 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003339 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003340 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003341 }
Moran Pekera28258c2018-05-29 16:25:04 +03003342 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003343 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003344 status = PSA_ERROR_INVALID_ARGUMENT;
3345 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003346 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003347 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3348 status = mbedtls_to_psa_error( ret );
3349exit:
3350 if( status == PSA_SUCCESS )
3351 operation->iv_set = 1;
3352 else
mohammad1603503973b2018-03-12 15:59:30 +02003353 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003354 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003355}
3356
Gilles Peskinee553c652018-06-04 16:22:46 +02003357psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3358 const uint8_t *input,
3359 size_t input_length,
3360 unsigned char *output,
3361 size_t output_size,
3362 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003363{
itayzafrir534bd7c2018-08-02 13:56:32 +03003364 psa_status_t status;
3365 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003366 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003367
3368 if( operation->alg == 0 )
3369 {
3370 return( PSA_ERROR_BAD_STATE );
3371 }
3372
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003373 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003374 {
3375 /* Take the unprocessed partial block left over from previous
3376 * update calls, if any, plus the input to this call. Remove
3377 * the last partial block, if any. You get the data that will be
3378 * output in this call. */
3379 expected_output_size =
3380 ( operation->ctx.cipher.unprocessed_len + input_length )
3381 / operation->block_size * operation->block_size;
3382 }
3383 else
3384 {
3385 expected_output_size = input_length;
3386 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003387
Gilles Peskine89d789c2018-06-04 17:17:16 +02003388 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003389 {
3390 status = PSA_ERROR_BUFFER_TOO_SMALL;
3391 goto exit;
3392 }
mohammad160382759612018-03-12 18:16:40 +02003393
mohammad1603503973b2018-03-12 15:59:30 +02003394 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003395 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003396 status = mbedtls_to_psa_error( ret );
3397exit:
3398 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003399 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003400 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003401}
3402
Gilles Peskinee553c652018-06-04 16:22:46 +02003403psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3404 uint8_t *output,
3405 size_t output_size,
3406 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003407{
David Saadab4ecc272019-02-14 13:48:10 +02003408 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003409 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003410 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003411
mohammad1603503973b2018-03-12 15:59:30 +02003412 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003413 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003414 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003415 }
3416 if( operation->iv_required && ! operation->iv_set )
3417 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003418 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003419 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003420
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003421 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003422 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3423 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003424 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003425 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003426 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003427 }
3428
Janos Follath315b51c2018-07-09 16:04:51 +01003429 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3430 temp_output_buffer,
3431 output_length );
3432 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003433 {
Janos Follath315b51c2018-07-09 16:04:51 +01003434 status = mbedtls_to_psa_error( cipher_ret );
3435 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003436 }
Janos Follath315b51c2018-07-09 16:04:51 +01003437
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003438 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003439 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003440 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003441 memcpy( output, temp_output_buffer, *output_length );
3442 else
3443 {
Janos Follath315b51c2018-07-09 16:04:51 +01003444 status = PSA_ERROR_BUFFER_TOO_SMALL;
3445 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003446 }
mohammad1603503973b2018-03-12 15:59:30 +02003447
Gilles Peskine3f108122018-12-07 18:14:53 +01003448 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003449 status = psa_cipher_abort( operation );
3450
3451 return( status );
3452
3453error:
3454
3455 *output_length = 0;
3456
Gilles Peskine3f108122018-12-07 18:14:53 +01003457 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003458 (void) psa_cipher_abort( operation );
3459
3460 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003461}
3462
Gilles Peskinee553c652018-06-04 16:22:46 +02003463psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3464{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003465 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003466 {
3467 /* The object has (apparently) been initialized but it is not
3468 * in use. It's ok to call abort on such an object, and there's
3469 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003470 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003471 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003472
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003473 /* Sanity check (shouldn't happen: operation->alg should
3474 * always have been initialized to a valid value). */
3475 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3476 return( PSA_ERROR_BAD_STATE );
3477
mohammad1603503973b2018-03-12 15:59:30 +02003478 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003479
Moran Peker41deec42018-04-04 15:43:05 +03003480 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003481 operation->key_set = 0;
3482 operation->iv_set = 0;
3483 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003484 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003485 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003486
Moran Peker395db872018-05-31 14:07:14 +03003487 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003488}
3489
Gilles Peskinea0655c32018-04-30 17:06:50 +02003490
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003491
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003492
mohammad16035955c982018-04-26 00:53:03 +03003493/****************************************************************/
3494/* AEAD */
3495/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003496
Gilles Peskineedf9a652018-08-17 18:11:56 +02003497typedef struct
3498{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003499 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003500 const mbedtls_cipher_info_t *cipher_info;
3501 union
3502 {
3503#if defined(MBEDTLS_CCM_C)
3504 mbedtls_ccm_context ccm;
3505#endif /* MBEDTLS_CCM_C */
3506#if defined(MBEDTLS_GCM_C)
3507 mbedtls_gcm_context gcm;
3508#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003509#if defined(MBEDTLS_CHACHAPOLY_C)
3510 mbedtls_chachapoly_context chachapoly;
3511#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003512 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003513 psa_algorithm_t core_alg;
3514 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003515 uint8_t tag_length;
3516} aead_operation_t;
3517
Gilles Peskine30a9e412019-01-14 18:36:12 +01003518static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003519{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003520 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003521 {
3522#if defined(MBEDTLS_CCM_C)
3523 case PSA_ALG_CCM:
3524 mbedtls_ccm_free( &operation->ctx.ccm );
3525 break;
3526#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003527#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003528 case PSA_ALG_GCM:
3529 mbedtls_gcm_free( &operation->ctx.gcm );
3530 break;
3531#endif /* MBEDTLS_GCM_C */
3532 }
3533}
3534
3535static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003536 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003537 psa_key_usage_t usage,
3538 psa_algorithm_t alg )
3539{
3540 psa_status_t status;
3541 size_t key_bits;
3542 mbedtls_cipher_id_t cipher_id;
3543
Gilles Peskinec5487a82018-12-03 18:08:14 +01003544 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003545 if( status != PSA_SUCCESS )
3546 return( status );
3547
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003548 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003549
3550 operation->cipher_info =
3551 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3552 &cipher_id );
3553 if( operation->cipher_info == NULL )
3554 return( PSA_ERROR_NOT_SUPPORTED );
3555
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003556 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003557 {
3558#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003559 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3560 operation->core_alg = PSA_ALG_CCM;
3561 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003562 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3563 * The call to mbedtls_ccm_encrypt_and_tag or
3564 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003565 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3566 return( PSA_ERROR_INVALID_ARGUMENT );
3567 mbedtls_ccm_init( &operation->ctx.ccm );
3568 status = mbedtls_to_psa_error(
3569 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3570 operation->slot->data.raw.data,
3571 (unsigned int) key_bits ) );
3572 if( status != 0 )
3573 goto cleanup;
3574 break;
3575#endif /* MBEDTLS_CCM_C */
3576
3577#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003578 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3579 operation->core_alg = PSA_ALG_GCM;
3580 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003581 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3582 * The call to mbedtls_gcm_crypt_and_tag or
3583 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003584 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3585 return( PSA_ERROR_INVALID_ARGUMENT );
3586 mbedtls_gcm_init( &operation->ctx.gcm );
3587 status = mbedtls_to_psa_error(
3588 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3589 operation->slot->data.raw.data,
3590 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003591 if( status != 0 )
3592 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003593 break;
3594#endif /* MBEDTLS_GCM_C */
3595
Gilles Peskine26869f22019-05-06 15:25:00 +02003596#if defined(MBEDTLS_CHACHAPOLY_C)
3597 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3598 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3599 operation->full_tag_length = 16;
3600 /* We only support the default tag length. */
3601 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3602 return( PSA_ERROR_NOT_SUPPORTED );
3603 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3604 status = mbedtls_to_psa_error(
3605 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3606 operation->slot->data.raw.data ) );
3607 if( status != 0 )
3608 goto cleanup;
3609 break;
3610#endif /* MBEDTLS_CHACHAPOLY_C */
3611
Gilles Peskineedf9a652018-08-17 18:11:56 +02003612 default:
3613 return( PSA_ERROR_NOT_SUPPORTED );
3614 }
3615
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003616 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3617 {
3618 status = PSA_ERROR_INVALID_ARGUMENT;
3619 goto cleanup;
3620 }
3621 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003622
Gilles Peskineedf9a652018-08-17 18:11:56 +02003623 return( PSA_SUCCESS );
3624
3625cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003626 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003627 return( status );
3628}
3629
Gilles Peskinec5487a82018-12-03 18:08:14 +01003630psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003631 psa_algorithm_t alg,
3632 const uint8_t *nonce,
3633 size_t nonce_length,
3634 const uint8_t *additional_data,
3635 size_t additional_data_length,
3636 const uint8_t *plaintext,
3637 size_t plaintext_length,
3638 uint8_t *ciphertext,
3639 size_t ciphertext_size,
3640 size_t *ciphertext_length )
3641{
mohammad16035955c982018-04-26 00:53:03 +03003642 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003643 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003644 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003645
mohammad1603f08a5502018-06-03 15:05:47 +03003646 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003647
Gilles Peskinec5487a82018-12-03 18:08:14 +01003648 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003649 if( status != PSA_SUCCESS )
3650 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003651
Gilles Peskineedf9a652018-08-17 18:11:56 +02003652 /* For all currently supported modes, the tag is at the end of the
3653 * ciphertext. */
3654 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3655 {
3656 status = PSA_ERROR_BUFFER_TOO_SMALL;
3657 goto exit;
3658 }
3659 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003660
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003661#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003662 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003663 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003664 status = mbedtls_to_psa_error(
3665 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3666 MBEDTLS_GCM_ENCRYPT,
3667 plaintext_length,
3668 nonce, nonce_length,
3669 additional_data, additional_data_length,
3670 plaintext, ciphertext,
3671 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003672 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003673 else
3674#endif /* MBEDTLS_GCM_C */
3675#if defined(MBEDTLS_CCM_C)
3676 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003677 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003678 status = mbedtls_to_psa_error(
3679 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3680 plaintext_length,
3681 nonce, nonce_length,
3682 additional_data,
3683 additional_data_length,
3684 plaintext, ciphertext,
3685 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003686 }
mohammad16035c8845f2018-05-09 05:40:09 -07003687 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003688#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003689#if defined(MBEDTLS_CHACHAPOLY_C)
3690 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3691 {
3692 if( nonce_length != 12 || operation.tag_length != 16 )
3693 {
3694 status = PSA_ERROR_NOT_SUPPORTED;
3695 goto exit;
3696 }
3697 status = mbedtls_to_psa_error(
3698 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3699 plaintext_length,
3700 nonce,
3701 additional_data,
3702 additional_data_length,
3703 plaintext,
3704 ciphertext,
3705 tag ) );
3706 }
3707 else
3708#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003709 {
mohammad1603554faad2018-06-03 15:07:38 +03003710 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003711 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003712
Gilles Peskineedf9a652018-08-17 18:11:56 +02003713 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3714 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003715
Gilles Peskineedf9a652018-08-17 18:11:56 +02003716exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003717 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003718 if( status == PSA_SUCCESS )
3719 *ciphertext_length = plaintext_length + operation.tag_length;
3720 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003721}
3722
Gilles Peskineee652a32018-06-01 19:23:52 +02003723/* Locate the tag in a ciphertext buffer containing the encrypted data
3724 * followed by the tag. Return the length of the part preceding the tag in
3725 * *plaintext_length. This is the size of the plaintext in modes where
3726 * the encrypted data has the same size as the plaintext, such as
3727 * CCM and GCM. */
3728static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3729 const uint8_t *ciphertext,
3730 size_t ciphertext_length,
3731 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003732 const uint8_t **p_tag )
3733{
3734 size_t payload_length;
3735 if( tag_length > ciphertext_length )
3736 return( PSA_ERROR_INVALID_ARGUMENT );
3737 payload_length = ciphertext_length - tag_length;
3738 if( payload_length > plaintext_size )
3739 return( PSA_ERROR_BUFFER_TOO_SMALL );
3740 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003741 return( PSA_SUCCESS );
3742}
3743
Gilles Peskinec5487a82018-12-03 18:08:14 +01003744psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003745 psa_algorithm_t alg,
3746 const uint8_t *nonce,
3747 size_t nonce_length,
3748 const uint8_t *additional_data,
3749 size_t additional_data_length,
3750 const uint8_t *ciphertext,
3751 size_t ciphertext_length,
3752 uint8_t *plaintext,
3753 size_t plaintext_size,
3754 size_t *plaintext_length )
3755{
mohammad16035955c982018-04-26 00:53:03 +03003756 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757 aead_operation_t operation;
3758 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003759
Gilles Peskineee652a32018-06-01 19:23:52 +02003760 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003761
Gilles Peskinec5487a82018-12-03 18:08:14 +01003762 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003763 if( status != PSA_SUCCESS )
3764 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003765
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003766 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3767 ciphertext, ciphertext_length,
3768 plaintext_size, &tag );
3769 if( status != PSA_SUCCESS )
3770 goto exit;
3771
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003772#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003773 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003774 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003775 status = mbedtls_to_psa_error(
3776 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3777 ciphertext_length - operation.tag_length,
3778 nonce, nonce_length,
3779 additional_data,
3780 additional_data_length,
3781 tag, operation.tag_length,
3782 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003783 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003784 else
3785#endif /* MBEDTLS_GCM_C */
3786#if defined(MBEDTLS_CCM_C)
3787 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003788 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003789 status = mbedtls_to_psa_error(
3790 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3791 ciphertext_length - operation.tag_length,
3792 nonce, nonce_length,
3793 additional_data,
3794 additional_data_length,
3795 ciphertext, plaintext,
3796 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003797 }
mohammad160339574652018-06-01 04:39:53 -07003798 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003799#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003800#if defined(MBEDTLS_CHACHAPOLY_C)
3801 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3802 {
3803 if( nonce_length != 12 || operation.tag_length != 16 )
3804 {
3805 status = PSA_ERROR_NOT_SUPPORTED;
3806 goto exit;
3807 }
3808 status = mbedtls_to_psa_error(
3809 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3810 ciphertext_length - operation.tag_length,
3811 nonce,
3812 additional_data,
3813 additional_data_length,
3814 tag,
3815 ciphertext,
3816 plaintext ) );
3817 }
3818 else
3819#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003820 {
mohammad1603554faad2018-06-03 15:07:38 +03003821 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003822 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003823
Gilles Peskineedf9a652018-08-17 18:11:56 +02003824 if( status != PSA_SUCCESS && plaintext_size != 0 )
3825 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003826
Gilles Peskineedf9a652018-08-17 18:11:56 +02003827exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003828 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003829 if( status == PSA_SUCCESS )
3830 *plaintext_length = ciphertext_length - operation.tag_length;
3831 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003832}
3833
Gilles Peskinea0655c32018-04-30 17:06:50 +02003834
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003835
Gilles Peskine20035e32018-02-03 22:44:14 +01003836/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003837/* Generators */
3838/****************************************************************/
3839
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003840#define HKDF_STATE_INIT 0 /* no input yet */
3841#define HKDF_STATE_STARTED 1 /* got salt */
3842#define HKDF_STATE_KEYED 2 /* got key */
3843#define HKDF_STATE_OUTPUT 3 /* output started */
3844
Gilles Peskinecbe66502019-05-16 16:59:18 +02003845static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003846 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003847{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003848 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3849 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003850 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003851 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003852}
3853
3854
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003855psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003856{
3857 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003858 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003859 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003860 {
3861 /* The object has (apparently) been initialized but it is not
3862 * in use. It's ok to call abort on such an object, and there's
3863 * nothing to do. */
3864 }
3865 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003866 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003867 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003868 if( operation->ctx.buffer.data != NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02003869 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003870 mbedtls_platform_zeroize( operation->ctx.buffer.data,
3871 operation->ctx.buffer.size );
3872 mbedtls_free( operation->ctx.buffer.data );
Gilles Peskine751d9652018-09-18 12:05:44 +02003873 }
3874 }
3875 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003876#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003877 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003878 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003879 mbedtls_free( operation->ctx.hkdf.info );
3880 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003881 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003882 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003883 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003884 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003885 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003886 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003887 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003888 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3889 operation->ctx.tls12_prf.key_len );
3890 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003891 }
Hanno Becker580fba12018-11-13 20:50:45 +00003892
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003893 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003894 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003895 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3896 operation->ctx.tls12_prf.Ai_with_seed_len );
3897 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003898 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003899 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003900 else
3901#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003902 {
3903 status = PSA_ERROR_BAD_STATE;
3904 }
Janos Follath083036a2019-06-11 10:22:26 +01003905 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003906 return( status );
3907}
3908
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003909psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003910 size_t *capacity)
3911{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003912 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003913 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003914 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003915 return PSA_ERROR_BAD_STATE;
3916 }
3917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003918 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003919 return( PSA_SUCCESS );
3920}
3921
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003922psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003923 size_t capacity )
3924{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003925 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003926 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003927 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003928 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003929 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003930 return( PSA_SUCCESS );
3931}
3932
Gilles Peskinebef7f142018-07-12 17:22:21 +02003933#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003934/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003935 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003936static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003937 psa_algorithm_t hash_alg,
3938 uint8_t *output,
3939 size_t output_length )
3940{
3941 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3942 psa_status_t status;
3943
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003944 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3945 return( PSA_ERROR_BAD_STATE );
3946 hkdf->state = HKDF_STATE_OUTPUT;
3947
Gilles Peskinebef7f142018-07-12 17:22:21 +02003948 while( output_length != 0 )
3949 {
3950 /* Copy what remains of the current block */
3951 uint8_t n = hash_length - hkdf->offset_in_block;
3952 if( n > output_length )
3953 n = (uint8_t) output_length;
3954 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3955 output += n;
3956 output_length -= n;
3957 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003958 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003959 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003960 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003961 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003962 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003963 * object was corrupted or if this function is called directly
3964 * inside the library. */
3965 if( hkdf->block_number == 0xff )
3966 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003967
3968 /* We need a new block */
3969 ++hkdf->block_number;
3970 hkdf->offset_in_block = 0;
3971 status = psa_hmac_setup_internal( &hkdf->hmac,
3972 hkdf->prk, hash_length,
3973 hash_alg );
3974 if( status != PSA_SUCCESS )
3975 return( status );
3976 if( hkdf->block_number != 1 )
3977 {
3978 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3979 hkdf->output_block,
3980 hash_length );
3981 if( status != PSA_SUCCESS )
3982 return( status );
3983 }
3984 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3985 hkdf->info,
3986 hkdf->info_length );
3987 if( status != PSA_SUCCESS )
3988 return( status );
3989 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3990 &hkdf->block_number, 1 );
3991 if( status != PSA_SUCCESS )
3992 return( status );
3993 status = psa_hmac_finish_internal( &hkdf->hmac,
3994 hkdf->output_block,
3995 sizeof( hkdf->output_block ) );
3996 if( status != PSA_SUCCESS )
3997 return( status );
3998 }
3999
4000 return( PSA_SUCCESS );
4001}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004002
Gilles Peskinecbe66502019-05-16 16:59:18 +02004003static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4004 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004005 psa_algorithm_t alg )
4006{
4007 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4008 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4009 psa_hmac_internal_data hmac;
4010 psa_status_t status, cleanup_status;
4011
Hanno Becker3b339e22018-11-13 20:56:14 +00004012 unsigned char *Ai;
4013 size_t Ai_len;
4014
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004015 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004016 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004017 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004018 * object was corrupted or if this function is called directly
4019 * inside the library. */
4020 if( tls12_prf->block_number == 0xff )
4021 return( PSA_ERROR_BAD_STATE );
4022
4023 /* We need a new block */
4024 ++tls12_prf->block_number;
4025 tls12_prf->offset_in_block = 0;
4026
4027 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4028 *
4029 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4030 *
4031 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4032 * HMAC_hash(secret, A(2) + seed) +
4033 * HMAC_hash(secret, A(3) + seed) + ...
4034 *
4035 * A(0) = seed
4036 * A(i) = HMAC_hash( secret, A(i-1) )
4037 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004038 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004039 * `HMAC_hash(secret, A(i) + seed)` from which the output
4040 * is currently extracted as `output_block`, while
4041 * `A(i) + seed` is stored in `Ai_with_seed`.
4042 *
4043 * Generating a new block means recalculating `Ai_with_seed`
4044 * from the A(i)-part of it, and afterwards recalculating
4045 * `output_block`.
4046 *
4047 * A(0) is computed at setup time.
4048 *
4049 */
4050
4051 psa_hmac_init_internal( &hmac );
4052
4053 /* We must distinguish the calculation of A(1) from those
4054 * of A(2) and higher, because A(0)=seed has a different
4055 * length than the other A(i). */
4056 if( tls12_prf->block_number == 1 )
4057 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004058 Ai = tls12_prf->Ai_with_seed + hash_length;
4059 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004060 }
4061 else
4062 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004063 Ai = tls12_prf->Ai_with_seed;
4064 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004065 }
4066
Hanno Becker3b339e22018-11-13 20:56:14 +00004067 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4068 status = psa_hmac_setup_internal( &hmac,
4069 tls12_prf->key,
4070 tls12_prf->key_len,
4071 hash_alg );
4072 if( status != PSA_SUCCESS )
4073 goto cleanup;
4074
4075 status = psa_hash_update( &hmac.hash_ctx,
4076 Ai, Ai_len );
4077 if( status != PSA_SUCCESS )
4078 goto cleanup;
4079
4080 status = psa_hmac_finish_internal( &hmac,
4081 tls12_prf->Ai_with_seed,
4082 hash_length );
4083 if( status != PSA_SUCCESS )
4084 goto cleanup;
4085
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004086 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4087 status = psa_hmac_setup_internal( &hmac,
4088 tls12_prf->key,
4089 tls12_prf->key_len,
4090 hash_alg );
4091 if( status != PSA_SUCCESS )
4092 goto cleanup;
4093
4094 status = psa_hash_update( &hmac.hash_ctx,
4095 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004096 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004097 if( status != PSA_SUCCESS )
4098 goto cleanup;
4099
4100 status = psa_hmac_finish_internal( &hmac,
4101 tls12_prf->output_block,
4102 hash_length );
4103 if( status != PSA_SUCCESS )
4104 goto cleanup;
4105
4106cleanup:
4107
4108 cleanup_status = psa_hmac_abort_internal( &hmac );
4109 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4110 status = cleanup_status;
4111
4112 return( status );
4113}
4114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004115/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004116 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004117static psa_status_t psa_key_derivation_tls12_prf_read(
4118 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004119 psa_algorithm_t alg,
4120 uint8_t *output,
4121 size_t output_length )
4122{
4123 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4124 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4125 psa_status_t status;
4126
4127 while( output_length != 0 )
4128 {
4129 /* Copy what remains of the current block */
4130 uint8_t n = hash_length - tls12_prf->offset_in_block;
4131
4132 /* Check if we have fully processed the current block. */
4133 if( n == 0 )
4134 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004135 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004136 alg );
4137 if( status != PSA_SUCCESS )
4138 return( status );
4139
4140 continue;
4141 }
4142
4143 if( n > output_length )
4144 n = (uint8_t) output_length;
4145 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4146 n );
4147 output += n;
4148 output_length -= n;
4149 tls12_prf->offset_in_block += n;
4150 }
4151
4152 return( PSA_SUCCESS );
4153}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004154#endif /* MBEDTLS_MD_C */
4155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156psa_status_t psa_key_derivation_output_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004157 uint8_t *output,
4158 size_t output_length )
4159{
4160 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004161 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004162
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004163 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004164 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004165 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004166 return PSA_ERROR_BAD_STATE;
4167 }
4168
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004169 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004170 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004172 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004173 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004174 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175 goto exit;
4176 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004178 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004180 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004181 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4182 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004183 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004184 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004185 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004186 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004188
Gilles Peskine969c5d62019-01-16 15:53:06 +01004189 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004190 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 /* Initially, the capacity of a selection operation is always
4192 * the size of the buffer, i.e. `operation->ctx.buffer.size`,
Gilles Peskine211a4362018-10-25 22:22:31 +02004193 * abbreviated in this comment as `size`. When the remaining
4194 * capacity is `c`, the next bytes to serve start `c` bytes
4195 * from the end of the buffer, i.e. `size - c` from the
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 * beginning of the buffer. Since `operation->capacity` was just
Gilles Peskine211a4362018-10-25 22:22:31 +02004197 * decremented above, we need to serve the bytes from
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198 * `size - operation->capacity - output_length` to
4199 * `size - operation->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004200 size_t offset =
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 operation->ctx.buffer.size - operation->capacity - output_length;
4202 memcpy( output, operation->ctx.buffer.data + offset, output_length );
Gilles Peskine751d9652018-09-18 12:05:44 +02004203 status = PSA_SUCCESS;
4204 }
4205 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004206#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004207 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004208 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004209 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004210 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004211 output, output_length );
4212 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004213 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4214 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004215 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004216 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004217 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004218 output_length );
4219 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004220 else
4221#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004222 {
4223 return( PSA_ERROR_BAD_STATE );
4224 }
4225
4226exit:
4227 if( status != PSA_SUCCESS )
4228 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004229 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004230 * This allows us to differentiate between exhausted operations and
4231 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4232 * operations. */
4233 psa_algorithm_t alg = operation->alg;
4234 psa_key_derivation_abort( operation );
4235 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004236 memset( output, '!', output_length );
4237 }
4238 return( status );
4239}
4240
Gilles Peskine08542d82018-07-19 17:05:42 +02004241#if defined(MBEDTLS_DES_C)
4242static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4243{
4244 if( data_size >= 8 )
4245 mbedtls_des_key_set_parity( data );
4246 if( data_size >= 16 )
4247 mbedtls_des_key_set_parity( data + 8 );
4248 if( data_size >= 24 )
4249 mbedtls_des_key_set_parity( data + 16 );
4250}
4251#endif /* MBEDTLS_DES_C */
4252
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004253static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004254 psa_key_slot_t *slot,
4255 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004256 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004257{
4258 uint8_t *data = NULL;
4259 size_t bytes = PSA_BITS_TO_BYTES( bits );
4260 psa_status_t status;
4261
4262 if( ! key_type_is_raw_bytes( slot->type ) )
4263 return( PSA_ERROR_INVALID_ARGUMENT );
4264 if( bits % 8 != 0 )
4265 return( PSA_ERROR_INVALID_ARGUMENT );
4266 data = mbedtls_calloc( 1, bytes );
4267 if( data == NULL )
4268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004270 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004271 if( status != PSA_SUCCESS )
4272 goto exit;
4273#if defined(MBEDTLS_DES_C)
4274 if( slot->type == PSA_KEY_TYPE_DES )
4275 psa_des_set_key_parity( data, bytes );
4276#endif /* MBEDTLS_DES_C */
4277 status = psa_import_key_into_slot( slot, data, bytes );
4278
4279exit:
4280 mbedtls_free( data );
4281 return( status );
4282}
4283
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004284psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004285 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004286 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004287{
4288 psa_status_t status;
4289 psa_key_slot_t *slot = NULL;
4290 status = psa_start_key_creation( attributes, handle, &slot );
4291 if( status == PSA_SUCCESS )
4292 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004293 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004294 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004295 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004296 }
4297 if( status == PSA_SUCCESS )
4298 status = psa_finish_key_creation( slot );
4299 if( status != PSA_SUCCESS )
4300 {
4301 psa_fail_key_creation( slot );
4302 *handle = 0;
4303 }
4304 return( status );
4305}
4306
Gilles Peskineeab56e42018-07-12 17:12:33 +02004307
4308
4309/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004310/* Key derivation */
4311/****************************************************************/
4312
Gilles Peskinea05219c2018-11-16 16:02:56 +01004313#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004314#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004315/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004316 * of the HKDF algorithm.
4317 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004318 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004319 * to potentially free embedded data structures and wipe confidential data.
4320 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004321static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004322 const uint8_t *secret,
4323 size_t secret_length,
4324 psa_algorithm_t hash_alg,
4325 const uint8_t *salt,
4326 size_t salt_length,
4327 const uint8_t *label,
4328 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004329{
4330 psa_status_t status;
4331 status = psa_hmac_setup_internal( &hkdf->hmac,
4332 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004333 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004334 if( status != PSA_SUCCESS )
4335 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004336 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004337 if( status != PSA_SUCCESS )
4338 return( status );
4339 status = psa_hmac_finish_internal( &hkdf->hmac,
4340 hkdf->prk,
4341 sizeof( hkdf->prk ) );
4342 if( status != PSA_SUCCESS )
4343 return( status );
4344 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4345 hkdf->block_number = 0;
4346 hkdf->info_length = label_length;
4347 if( label_length != 0 )
4348 {
4349 hkdf->info = mbedtls_calloc( 1, label_length );
4350 if( hkdf->info == NULL )
4351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4352 memcpy( hkdf->info, label, label_length );
4353 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004354 hkdf->state = HKDF_STATE_KEYED;
4355 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004356 return( PSA_SUCCESS );
4357}
Janos Follath71a4c912019-06-11 09:14:47 +01004358#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004359#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004360
Gilles Peskinea05219c2018-11-16 16:02:56 +01004361#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004362#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004364 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004365 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004366 * to potentially free embedded data structures and wipe confidential data.
4367 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004368static psa_status_t psa_key_derivation_tls12_prf_setup(
4369 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370 const unsigned char *key,
4371 size_t key_len,
4372 psa_algorithm_t hash_alg,
4373 const uint8_t *salt,
4374 size_t salt_length,
4375 const uint8_t *label,
4376 size_t label_length )
4377{
4378 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004379 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4380 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004381
4382 tls12_prf->key = mbedtls_calloc( 1, key_len );
4383 if( tls12_prf->key == NULL )
4384 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4385 tls12_prf->key_len = key_len;
4386 memcpy( tls12_prf->key, key, key_len );
4387
Hanno Becker580fba12018-11-13 20:50:45 +00004388 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004389 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004390 if( overflow )
4391 return( PSA_ERROR_INVALID_ARGUMENT );
4392
4393 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4394 if( tls12_prf->Ai_with_seed == NULL )
4395 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4396 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4397
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004398 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4399 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004400 if( label_length != 0 )
4401 {
4402 memcpy( tls12_prf->Ai_with_seed + hash_length,
4403 label, label_length );
4404 }
4405
4406 if( salt_length != 0 )
4407 {
4408 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4409 salt, salt_length );
4410 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004411
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004412 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004413 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004414 tls12_prf->block_number = 0;
4415 tls12_prf->offset_in_block = hash_length;
4416
4417 return( PSA_SUCCESS );
4418}
Janos Follath71a4c912019-06-11 09:14:47 +01004419#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004420
Janos Follath71a4c912019-06-11 09:14:47 +01004421#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004423static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4424 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004425 const unsigned char *psk,
4426 size_t psk_len,
4427 psa_algorithm_t hash_alg,
4428 const uint8_t *salt,
4429 size_t salt_length,
4430 const uint8_t *label,
4431 size_t label_length )
4432{
4433 psa_status_t status;
4434 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4435
4436 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4437 return( PSA_ERROR_INVALID_ARGUMENT );
4438
4439 /* Quoting RFC 4279, Section 2:
4440 *
4441 * The premaster secret is formed as follows: if the PSK is N octets
4442 * long, concatenate a uint16 with the value N, N zero octets, a second
4443 * uint16 with the value N, and the PSK itself.
4444 */
4445
4446 pms[0] = ( psk_len >> 8 ) & 0xff;
4447 pms[1] = ( psk_len >> 0 ) & 0xff;
4448 memset( pms + 2, 0, psk_len );
4449 pms[2 + psk_len + 0] = pms[0];
4450 pms[2 + psk_len + 1] = pms[1];
4451 memcpy( pms + 4 + psk_len, psk, psk_len );
4452
Gilles Peskinecbe66502019-05-16 16:59:18 +02004453 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004454 pms, 4 + 2 * psk_len,
4455 hash_alg,
4456 salt, salt_length,
4457 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004458
Gilles Peskine3f108122018-12-07 18:14:53 +01004459 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004460 return( status );
4461}
Janos Follath71a4c912019-06-11 09:14:47 +01004462#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004463#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004464
Janos Follath71a4c912019-06-11 09:14:47 +01004465#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004466/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004467 * to potentially free embedded data structures and wipe confidential data.
4468 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004469static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004470 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004471 const uint8_t *secret, size_t secret_length,
4472 psa_algorithm_t alg,
4473 const uint8_t *salt, size_t salt_length,
4474 const uint8_t *label, size_t label_length,
4475 size_t capacity )
4476{
4477 psa_status_t status;
4478 size_t max_capacity;
4479
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 /* Set operation->alg even on failure so that abort knows what to do. */
4481 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004482
Gilles Peskine751d9652018-09-18 12:05:44 +02004483 if( alg == PSA_ALG_SELECT_RAW )
4484 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004485 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004486 if( salt_length != 0 )
4487 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004488 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004489 if( label_length != 0 )
4490 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 operation->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4492 if( operation->ctx.buffer.data == NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004493 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 memcpy( operation->ctx.buffer.data, secret, secret_length );
4495 operation->ctx.buffer.size = secret_length;
Gilles Peskine751d9652018-09-18 12:05:44 +02004496 max_capacity = secret_length;
4497 status = PSA_SUCCESS;
4498 }
4499 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004500#if defined(MBEDTLS_MD_C)
4501 if( PSA_ALG_IS_HKDF( alg ) )
4502 {
4503 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4504 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4505 if( hash_size == 0 )
4506 return( PSA_ERROR_NOT_SUPPORTED );
4507 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004509 secret, secret_length,
4510 hash_alg,
4511 salt, salt_length,
4512 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004513 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004514 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4515 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4516 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004517 {
4518 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4519 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4520
4521 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4522 if( hash_alg != PSA_ALG_SHA_256 &&
4523 hash_alg != PSA_ALG_SHA_384 )
4524 {
4525 return( PSA_ERROR_NOT_SUPPORTED );
4526 }
4527
4528 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004529
4530 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4531 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004533 secret, secret_length,
4534 hash_alg, salt, salt_length,
4535 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004536 }
4537 else
4538 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004539 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004541 secret, secret_length,
4542 hash_alg, salt, salt_length,
4543 label, label_length );
4544 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004545 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004546 else
4547#endif
4548 {
4549 return( PSA_ERROR_NOT_SUPPORTED );
4550 }
4551
4552 if( status != PSA_SUCCESS )
4553 return( status );
4554
4555 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004556 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004557 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004558 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004559 else
4560 return( PSA_ERROR_INVALID_ARGUMENT );
4561
4562 return( PSA_SUCCESS );
4563}
Janos Follath71a4c912019-06-11 09:14:47 +01004564#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004565
Janos Follath71a4c912019-06-11 09:14:47 +01004566#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004568 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004569 psa_algorithm_t alg,
4570 const uint8_t *salt,
4571 size_t salt_length,
4572 const uint8_t *label,
4573 size_t label_length,
4574 size_t capacity )
4575{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004576 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004577 psa_status_t status;
4578
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004580 return( PSA_ERROR_BAD_STATE );
4581
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004582 /* Make sure that alg is a key derivation algorithm. This prevents
4583 * key selection algorithms, which psa_key_derivation_internal
4584 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004585 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4586 return( PSA_ERROR_INVALID_ARGUMENT );
4587
Gilles Peskinec5487a82018-12-03 18:08:14 +01004588 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004589 if( status != PSA_SUCCESS )
4590 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004591
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004592 if( slot->type != PSA_KEY_TYPE_DERIVE )
4593 return( PSA_ERROR_INVALID_ARGUMENT );
4594
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004596 slot->data.raw.data,
4597 slot->data.raw.bytes,
4598 alg,
4599 salt, salt_length,
4600 label, label_length,
4601 capacity );
4602 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004604 return( status );
4605}
Janos Follath71a4c912019-06-11 09:14:47 +01004606#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004607
Gilles Peskine969c5d62019-01-16 15:53:06 +01004608static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004610 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004611{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004612 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004613#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004614 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4615 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4616 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004617 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004618 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004619 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4620 if( hash_size == 0 )
4621 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004622 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4623 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004624 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004625 {
4626 return( PSA_ERROR_NOT_SUPPORTED );
4627 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004629 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004630 }
4631#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004632 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004633 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004634}
4635
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004636psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004637 psa_algorithm_t alg )
4638{
4639 psa_status_t status;
4640
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004642 return( PSA_ERROR_BAD_STATE );
4643
Gilles Peskine6843c292019-01-18 16:44:49 +01004644 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4645 return( PSA_ERROR_INVALID_ARGUMENT );
4646 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004647 {
4648 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004650 }
4651 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4652 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004654 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004655 else
4656 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004657
4658 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004660 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004661}
4662
4663#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004664static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004665 psa_algorithm_t hash_alg,
4666 psa_key_derivation_step_t step,
4667 const uint8_t *data,
4668 size_t data_length )
4669{
4670 psa_status_t status;
4671 switch( step )
4672 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004673 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004674 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004675 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004676 status = psa_hmac_setup_internal( &hkdf->hmac,
4677 data, data_length,
4678 hash_alg );
4679 if( status != PSA_SUCCESS )
4680 return( status );
4681 hkdf->state = HKDF_STATE_STARTED;
4682 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004683 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004684 /* If no salt was provided, use an empty salt. */
4685 if( hkdf->state == HKDF_STATE_INIT )
4686 {
4687 status = psa_hmac_setup_internal( &hkdf->hmac,
4688 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004689 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004690 if( status != PSA_SUCCESS )
4691 return( status );
4692 hkdf->state = HKDF_STATE_STARTED;
4693 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004694 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004695 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004696 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4697 data, data_length );
4698 if( status != PSA_SUCCESS )
4699 return( status );
4700 status = psa_hmac_finish_internal( &hkdf->hmac,
4701 hkdf->prk,
4702 sizeof( hkdf->prk ) );
4703 if( status != PSA_SUCCESS )
4704 return( status );
4705 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4706 hkdf->block_number = 0;
4707 hkdf->state = HKDF_STATE_KEYED;
4708 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004709 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004710 if( hkdf->state == HKDF_STATE_OUTPUT )
4711 return( PSA_ERROR_BAD_STATE );
4712 if( hkdf->info_set )
4713 return( PSA_ERROR_BAD_STATE );
4714 hkdf->info_length = data_length;
4715 if( data_length != 0 )
4716 {
4717 hkdf->info = mbedtls_calloc( 1, data_length );
4718 if( hkdf->info == NULL )
4719 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4720 memcpy( hkdf->info, data, data_length );
4721 }
4722 hkdf->info_set = 1;
4723 return( PSA_SUCCESS );
4724 default:
4725 return( PSA_ERROR_INVALID_ARGUMENT );
4726 }
4727}
4728#endif /* MBEDTLS_MD_C */
4729
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004730static psa_status_t psa_key_derivation_input_raw(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004732 psa_key_derivation_step_t step,
4733 const uint8_t *data,
4734 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004735{
4736 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004738
Gilles Peskine969c5d62019-01-16 15:53:06 +01004739 if( kdf_alg == PSA_ALG_SELECT_RAW )
4740 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 if( operation->capacity != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004742 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 operation->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4744 if( operation->ctx.buffer.data == NULL )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004745 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 memcpy( operation->ctx.buffer.data, data, data_length );
4747 operation->ctx.buffer.size = data_length;
4748 operation->capacity = data_length;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 status = PSA_SUCCESS;
4750 }
4751 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004752#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004753 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004754 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004756 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004757 step, data, data_length );
4758 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004759 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004760#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004761#if defined(MBEDTLS_MD_C)
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004762 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004764 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004765 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004766 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767 status = PSA_ERROR_NOT_SUPPORTED;
4768 }
4769 else
4770#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004773 return( PSA_ERROR_BAD_STATE );
4774 }
4775
4776 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778 return( status );
4779}
4780
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781psa_status_t psa_key_derivation_input_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004782 psa_key_derivation_step_t step,
4783 const uint8_t *data,
4784 size_t data_length )
4785{
4786 switch( step )
4787 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004788 case PSA_KEY_DERIVATION_INPUT_LABEL:
4789 case PSA_KEY_DERIVATION_INPUT_SALT:
4790 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine8f2a6dc2019-05-29 17:32:21 +02004791 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 return( psa_key_derivation_input_raw( operation, step,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004793 data, data_length ) );
4794 default:
4795 return( PSA_ERROR_INVALID_ARGUMENT );
4796 }
4797}
4798
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799psa_status_t psa_key_derivation_input_key( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004800 psa_key_derivation_step_t step,
4801 psa_key_handle_t handle )
4802{
4803 psa_key_slot_t *slot;
4804 psa_status_t status;
4805 status = psa_get_key_from_slot( handle, &slot,
4806 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004808 if( status != PSA_SUCCESS )
4809 return( status );
4810 if( slot->type != PSA_KEY_TYPE_DERIVE )
4811 return( PSA_ERROR_INVALID_ARGUMENT );
4812 /* Don't allow a key to be used as an input that is usually public.
4813 * This is debatable. It's ok from a cryptographic perspective to
4814 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004815 * the material should be dedicated to a particular input step,
4816 * otherwise this may allow the key to be used in an unintended way
4817 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02004818 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 return( psa_key_derivation_input_raw( operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004821 step,
4822 slot->data.raw.data,
4823 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824}
4825
Gilles Peskineea0fb492018-07-12 17:17:20 +02004826
4827
4828/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004829/* Key agreement */
4830/****************************************************************/
4831
Gilles Peskinea05219c2018-11-16 16:02:56 +01004832#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004833static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4834 size_t peer_key_length,
4835 const mbedtls_ecp_keypair *our_key,
4836 uint8_t *shared_secret,
4837 size_t shared_secret_size,
4838 size_t *shared_secret_length )
4839{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004840 mbedtls_ecp_keypair *their_key = NULL;
4841 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004842 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004843 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004844
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004845 status = psa_import_ec_public_key(
4846 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4847 peer_key, peer_key_length,
4848 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004849 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004850 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004851
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004852 status = mbedtls_to_psa_error(
4853 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4854 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004855 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004856 status = mbedtls_to_psa_error(
4857 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4858 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004859 goto exit;
4860
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004861 status = mbedtls_to_psa_error(
4862 mbedtls_ecdh_calc_secret( &ecdh,
4863 shared_secret_length,
4864 shared_secret, shared_secret_size,
4865 mbedtls_ctr_drbg_random,
4866 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004867
4868exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004869 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004870 mbedtls_ecp_keypair_free( their_key );
4871 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004872 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004873}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004874#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004875
Gilles Peskine01d718c2018-09-18 12:01:02 +02004876#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4877
Gilles Peskine0216fe12019-04-11 21:23:21 +02004878static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4879 psa_key_slot_t *private_key,
4880 const uint8_t *peer_key,
4881 size_t peer_key_length,
4882 uint8_t *shared_secret,
4883 size_t shared_secret_size,
4884 size_t *shared_secret_length )
4885{
4886 switch( alg )
4887 {
4888#if defined(MBEDTLS_ECDH_C)
4889 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004890 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004891 return( PSA_ERROR_INVALID_ARGUMENT );
4892 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4893 private_key->data.ecp,
4894 shared_secret, shared_secret_size,
4895 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004896#endif /* MBEDTLS_ECDH_C */
4897 default:
4898 (void) private_key;
4899 (void) peer_key;
4900 (void) peer_key_length;
4901 (void) shared_secret;
4902 (void) shared_secret_size;
4903 (void) shared_secret_length;
4904 return( PSA_ERROR_NOT_SUPPORTED );
4905 }
4906}
4907
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004908/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004909 * to potentially free embedded data structures and wipe confidential data.
4910 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004913 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004914 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004916{
4917 psa_status_t status;
4918 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4919 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004921
4922 /* Step 1: run the secret agreement algorithm to generate the shared
4923 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004924 status = psa_key_agreement_raw_internal( ka_alg,
4925 private_key,
4926 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004927 shared_secret,
4928 sizeof( shared_secret ),
4929 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 if( status != PSA_SUCCESS )
4931 goto exit;
4932
4933 /* Step 2: set up the key derivation to generate key material from
4934 * the shared secret. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 status = psa_key_derivation_input_raw( operation, step,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 shared_secret, shared_secret_length );
4937
Gilles Peskine01d718c2018-09-18 12:01:02 +02004938exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004939 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004940 return( status );
4941}
4942
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004944 psa_key_derivation_step_t step,
4945 psa_key_handle_t private_key,
4946 const uint8_t *peer_key,
4947 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004948{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004949 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004950 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004952 return( PSA_ERROR_INVALID_ARGUMENT );
4953 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004955 if( status != PSA_SUCCESS )
4956 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004958 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004960 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01004962 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004963}
4964
Gilles Peskinebe697d82019-05-16 18:00:41 +02004965psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
4966 psa_key_handle_t private_key,
4967 const uint8_t *peer_key,
4968 size_t peer_key_length,
4969 uint8_t *output,
4970 size_t output_size,
4971 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004972{
4973 psa_key_slot_t *slot;
4974 psa_status_t status;
4975
4976 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4977 {
4978 status = PSA_ERROR_INVALID_ARGUMENT;
4979 goto exit;
4980 }
4981 status = psa_get_key_from_slot( private_key, &slot,
4982 PSA_KEY_USAGE_DERIVE, alg );
4983 if( status != PSA_SUCCESS )
4984 goto exit;
4985
4986 status = psa_key_agreement_raw_internal( alg, slot,
4987 peer_key, peer_key_length,
4988 output, output_size,
4989 output_length );
4990
4991exit:
4992 if( status != PSA_SUCCESS )
4993 {
4994 /* If an error happens and is not handled properly, the output
4995 * may be used as a key to protect sensitive data. Arrange for such
4996 * a key to be random, which is likely to result in decryption or
4997 * verification errors. This is better than filling the buffer with
4998 * some constant data such as zeros, which would result in the data
4999 * being protected with a reproducible, easily knowable key.
5000 */
5001 psa_generate_random( output, output_size );
5002 *output_length = output_size;
5003 }
5004 return( status );
5005}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005006
5007
5008/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005009/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005010/****************************************************************/
5011
5012psa_status_t psa_generate_random( uint8_t *output,
5013 size_t output_size )
5014{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005015 int ret;
5016 GUARD_MODULE_INITIALIZED;
5017
5018 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005019 return( mbedtls_to_psa_error( ret ) );
5020}
5021
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005022#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5023#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005024
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005025psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5026 size_t seed_size )
5027{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005028 if( global_data.initialized )
5029 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005030
5031 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5032 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5033 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5034 return( PSA_ERROR_INVALID_ARGUMENT );
5035
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005036 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005037}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005038#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005039
Gilles Peskinee56e8782019-04-26 17:34:02 +02005040#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5041static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5042 size_t domain_parameters_size,
5043 int *exponent )
5044{
5045 size_t i;
5046 uint32_t acc = 0;
5047
5048 if( domain_parameters_size == 0 )
5049 {
5050 *exponent = 65537;
5051 return( PSA_SUCCESS );
5052 }
5053
5054 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5055 * support values that fit in a 32-bit integer, which is larger than
5056 * int on just about every platform anyway. */
5057 if( domain_parameters_size > sizeof( acc ) )
5058 return( PSA_ERROR_NOT_SUPPORTED );
5059 for( i = 0; i < domain_parameters_size; i++ )
5060 acc = ( acc << 8 ) | domain_parameters[i];
5061 if( acc > INT_MAX )
5062 return( PSA_ERROR_NOT_SUPPORTED );
5063 *exponent = acc;
5064 return( PSA_SUCCESS );
5065}
5066#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5067
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005068static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005069 psa_key_slot_t *slot, size_t bits,
5070 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005071{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005072 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005073
Gilles Peskinee56e8782019-04-26 17:34:02 +02005074 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005075 return( PSA_ERROR_INVALID_ARGUMENT );
5076
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005077 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005078 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005080 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005081 if( status != PSA_SUCCESS )
5082 return( status );
5083 status = psa_generate_random( slot->data.raw.data,
5084 slot->data.raw.bytes );
5085 if( status != PSA_SUCCESS )
5086 {
5087 mbedtls_free( slot->data.raw.data );
5088 return( status );
5089 }
5090#if defined(MBEDTLS_DES_C)
5091 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005092 psa_des_set_key_parity( slot->data.raw.data,
5093 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005094#endif /* MBEDTLS_DES_C */
5095 }
5096 else
5097
5098#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005099 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005100 {
5101 mbedtls_rsa_context *rsa;
5102 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005103 int exponent;
5104 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005105 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5106 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005107 /* Accept only byte-aligned keys, for the same reasons as
5108 * in psa_import_rsa_key(). */
5109 if( bits % 8 != 0 )
5110 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005111 status = psa_read_rsa_exponent( domain_parameters,
5112 domain_parameters_size,
5113 &exponent );
5114 if( status != PSA_SUCCESS )
5115 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005116 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5117 if( rsa == NULL )
5118 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5119 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5120 ret = mbedtls_rsa_gen_key( rsa,
5121 mbedtls_ctr_drbg_random,
5122 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005123 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005124 exponent );
5125 if( ret != 0 )
5126 {
5127 mbedtls_rsa_free( rsa );
5128 mbedtls_free( rsa );
5129 return( mbedtls_to_psa_error( ret ) );
5130 }
5131 slot->data.rsa = rsa;
5132 }
5133 else
5134#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5135
5136#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005137 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005138 {
5139 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5140 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5141 const mbedtls_ecp_curve_info *curve_info =
5142 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5143 mbedtls_ecp_keypair *ecp;
5144 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005145 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005146 return( PSA_ERROR_NOT_SUPPORTED );
5147 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5148 return( PSA_ERROR_NOT_SUPPORTED );
5149 if( curve_info->bit_size != bits )
5150 return( PSA_ERROR_INVALID_ARGUMENT );
5151 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5152 if( ecp == NULL )
5153 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5154 mbedtls_ecp_keypair_init( ecp );
5155 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5156 mbedtls_ctr_drbg_random,
5157 &global_data.ctr_drbg );
5158 if( ret != 0 )
5159 {
5160 mbedtls_ecp_keypair_free( ecp );
5161 mbedtls_free( ecp );
5162 return( mbedtls_to_psa_error( ret ) );
5163 }
5164 slot->data.ecp = ecp;
5165 }
5166 else
5167#endif /* MBEDTLS_ECP_C */
5168
5169 return( PSA_ERROR_NOT_SUPPORTED );
5170
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005171 return( PSA_SUCCESS );
5172}
5173
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005174psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005175 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005176{
5177 psa_status_t status;
5178 psa_key_slot_t *slot = NULL;
5179 status = psa_start_key_creation( attributes, handle, &slot );
5180 if( status == PSA_SUCCESS )
5181 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005182 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005183 slot, attributes->bits,
5184 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005185 }
5186 if( status == PSA_SUCCESS )
5187 status = psa_finish_key_creation( slot );
5188 if( status != PSA_SUCCESS )
5189 {
5190 psa_fail_key_creation( slot );
5191 *handle = 0;
5192 }
5193 return( status );
5194}
5195
5196
Gilles Peskine05d69892018-06-19 22:00:52 +02005197
5198/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005199/* Module setup */
5200/****************************************************************/
5201
Gilles Peskine5e769522018-11-20 21:59:56 +01005202psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5203 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5204 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5205{
5206 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5207 return( PSA_ERROR_BAD_STATE );
5208 global_data.entropy_init = entropy_init;
5209 global_data.entropy_free = entropy_free;
5210 return( PSA_SUCCESS );
5211}
5212
Gilles Peskinee59236f2018-01-27 23:32:46 +01005213void mbedtls_psa_crypto_free( void )
5214{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005215 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005216 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5217 {
5218 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005219 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005220 }
5221 /* Wipe all remaining data, including configuration.
5222 * In particular, this sets all state indicator to the value
5223 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005224 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005225}
5226
5227psa_status_t psa_crypto_init( void )
5228{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005229 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005230 const unsigned char drbg_seed[] = "PSA";
5231
Gilles Peskinec6b69072018-11-20 21:42:52 +01005232 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005233 if( global_data.initialized != 0 )
5234 return( PSA_SUCCESS );
5235
Gilles Peskine5e769522018-11-20 21:59:56 +01005236 /* Set default configuration if
5237 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5238 if( global_data.entropy_init == NULL )
5239 global_data.entropy_init = mbedtls_entropy_init;
5240 if( global_data.entropy_free == NULL )
5241 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005242
Gilles Peskinec6b69072018-11-20 21:42:52 +01005243 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005244 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005245 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005246 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005247 status = mbedtls_to_psa_error(
5248 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5249 mbedtls_entropy_func,
5250 &global_data.entropy,
5251 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5252 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005253 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005254 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005255
Gilles Peskine66fb1262018-12-10 16:29:04 +01005256 status = psa_initialize_key_slots( );
5257 if( status != PSA_SUCCESS )
5258 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005259
5260 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005261 global_data.initialized = 1;
5262
Gilles Peskinee59236f2018-01-27 23:32:46 +01005263exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005264 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005265 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005266 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005267}
5268
5269#endif /* MBEDTLS_PSA_CRYPTO_C */