blob: f4e94bf2f7e98d4c5fa0d26730614a3a049ce27b [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
Janos Follath999f6482019-06-11 12:04:10 +01002125#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002126static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2127{
2128 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2129 memset( hmac, 0, sizeof( *hmac ) );
2130}
Janos Follath999f6482019-06-11 12:04:10 +01002131#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002132#endif /* MBEDTLS_MD_C */
2133
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2135{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002136 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002138 /* The object has (apparently) been initialized but it is not
2139 * in use. It's ok to call abort on such an object, and there's
2140 * nothing to do. */
2141 return( PSA_SUCCESS );
2142 }
2143 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002145 if( operation->alg == PSA_ALG_CMAC )
2146 {
2147 mbedtls_cipher_free( &operation->ctx.cmac );
2148 }
2149 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002151#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002152 if( PSA_ALG_IS_HMAC( operation->alg ) )
2153 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002154 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002155 }
2156 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002157#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002158 {
2159 /* Sanity check (shouldn't happen: operation->alg should
2160 * always have been initialized to a valid value). */
2161 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002162 }
Moran Peker41deec42018-04-04 15:43:05 +03002163
Gilles Peskine8c9def32018-02-08 10:02:12 +01002164 operation->alg = 0;
2165 operation->key_set = 0;
2166 operation->iv_set = 0;
2167 operation->iv_required = 0;
2168 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002169 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002170
Gilles Peskine8c9def32018-02-08 10:02:12 +01002171 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002172
2173bad_state:
2174 /* If abort is called on an uninitialized object, we can't trust
2175 * anything. Wipe the object in case it contains confidential data.
2176 * This may result in a memory leak if a pointer gets overwritten,
2177 * but it's too late to do anything about this. */
2178 memset( operation, 0, sizeof( *operation ) );
2179 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002180}
2181
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002182#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002183static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002184 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002185 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002186 const mbedtls_cipher_info_t *cipher_info )
2187{
2188 int ret;
2189
2190 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002191
2192 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2193 if( ret != 0 )
2194 return( ret );
2195
2196 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2197 slot->data.raw.data,
2198 key_bits );
2199 return( ret );
2200}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002201#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002202
Gilles Peskine248051a2018-06-20 16:09:38 +02002203#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002204static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2205 const uint8_t *key,
2206 size_t key_length,
2207 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002208{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002209 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002210 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002211 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002212 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002213 psa_status_t status;
2214
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002215 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2216 * overflow below. This should never trigger if the hash algorithm
2217 * is implemented correctly. */
2218 /* The size checks against the ipad and opad buffers cannot be written
2219 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2220 * because that triggers -Wlogical-op on GCC 7.3. */
2221 if( block_size > sizeof( ipad ) )
2222 return( PSA_ERROR_NOT_SUPPORTED );
2223 if( block_size > sizeof( hmac->opad ) )
2224 return( PSA_ERROR_NOT_SUPPORTED );
2225 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002226 return( PSA_ERROR_NOT_SUPPORTED );
2227
Gilles Peskined223b522018-06-11 18:12:58 +02002228 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002229 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002230 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002231 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002232 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002233 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002234 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002235 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002236 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002237 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002238 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002239 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002240 }
Gilles Peskine96889972018-07-12 17:07:03 +02002241 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2242 * but it is permitted. It is common when HMAC is used in HKDF, for
2243 * example. Don't call `memcpy` in the 0-length because `key` could be
2244 * an invalid pointer which would make the behavior undefined. */
2245 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002246 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002247
Gilles Peskined223b522018-06-11 18:12:58 +02002248 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2249 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002250 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002251 ipad[i] ^= 0x36;
2252 memset( ipad + key_length, 0x36, block_size - key_length );
2253
2254 /* Copy the key material from ipad to opad, flipping the requisite bits,
2255 * and filling the rest of opad with the requisite constant. */
2256 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002257 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2258 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002259
Gilles Peskine01126fa2018-07-12 17:04:55 +02002260 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002261 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002262 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002263
Gilles Peskine01126fa2018-07-12 17:04:55 +02002264 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002265
2266cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002267 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002268
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002269 return( status );
2270}
Gilles Peskine248051a2018-06-20 16:09:38 +02002271#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002272
Gilles Peskine89167cb2018-07-08 20:12:23 +02002273static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002274 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002275 psa_algorithm_t alg,
2276 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002279 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002281 psa_key_usage_t usage =
2282 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002283 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002284 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002285
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002286 /* A context must be freshly initialized before it can be set up. */
2287 if( operation->alg != 0 )
2288 {
2289 return( PSA_ERROR_BAD_STATE );
2290 }
2291
Gilles Peskined911eb72018-08-14 15:18:45 +02002292 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002293 if( status != PSA_SUCCESS )
2294 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002295 if( is_sign )
2296 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002297
Gilles Peskinec5487a82018-12-03 18:08:14 +01002298 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002299 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002300 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002301 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002302
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002304 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002305 {
2306 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002307 mbedtls_cipher_info_from_psa( full_length_alg,
2308 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002309 int ret;
2310 if( cipher_info == NULL )
2311 {
2312 status = PSA_ERROR_NOT_SUPPORTED;
2313 goto exit;
2314 }
2315 operation->mac_size = cipher_info->block_size;
2316 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2317 status = mbedtls_to_psa_error( ret );
2318 }
2319 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002321#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002322 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002323 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002324 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002325 if( hash_alg == 0 )
2326 {
2327 status = PSA_ERROR_NOT_SUPPORTED;
2328 goto exit;
2329 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002330
2331 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2332 /* Sanity check. This shouldn't fail on a valid configuration. */
2333 if( operation->mac_size == 0 ||
2334 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2335 {
2336 status = PSA_ERROR_NOT_SUPPORTED;
2337 goto exit;
2338 }
2339
Gilles Peskine01126fa2018-07-12 17:04:55 +02002340 if( slot->type != PSA_KEY_TYPE_HMAC )
2341 {
2342 status = PSA_ERROR_INVALID_ARGUMENT;
2343 goto exit;
2344 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002345
Gilles Peskine01126fa2018-07-12 17:04:55 +02002346 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2347 slot->data.raw.data,
2348 slot->data.raw.bytes,
2349 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002350 }
2351 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002353 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002354 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002355 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002357
Gilles Peskined911eb72018-08-14 15:18:45 +02002358 if( truncated == 0 )
2359 {
2360 /* The "normal" case: untruncated algorithm. Nothing to do. */
2361 }
2362 else if( truncated < 4 )
2363 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002364 /* A very short MAC is too short for security since it can be
2365 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2366 * so we make this our minimum, even though 32 bits is still
2367 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002368 status = PSA_ERROR_NOT_SUPPORTED;
2369 }
2370 else if( truncated > operation->mac_size )
2371 {
2372 /* It's impossible to "truncate" to a larger length. */
2373 status = PSA_ERROR_INVALID_ARGUMENT;
2374 }
2375 else
2376 operation->mac_size = truncated;
2377
Gilles Peskinefbfac682018-07-08 20:51:54 +02002378exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002379 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002381 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002383 else
2384 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002385 operation->key_set = 1;
2386 }
2387 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388}
2389
Gilles Peskine89167cb2018-07-08 20:12:23 +02002390psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002391 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002392 psa_algorithm_t alg )
2393{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002394 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002395}
2396
2397psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002398 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002399 psa_algorithm_t alg )
2400{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002401 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002402}
2403
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2405 const uint8_t *input,
2406 size_t input_length )
2407{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002408 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002410 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002412 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413 operation->has_input = 1;
2414
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002417 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002418 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2419 input, input_length );
2420 status = mbedtls_to_psa_error( ret );
2421 }
2422 else
2423#endif /* MBEDTLS_CMAC_C */
2424#if defined(MBEDTLS_MD_C)
2425 if( PSA_ALG_IS_HMAC( operation->alg ) )
2426 {
2427 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2428 input_length );
2429 }
2430 else
2431#endif /* MBEDTLS_MD_C */
2432 {
2433 /* This shouldn't happen if `operation` was initialized by
2434 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002435 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002436 }
2437
Gilles Peskinefbfac682018-07-08 20:51:54 +02002438 if( status != PSA_SUCCESS )
2439 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002440 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441}
2442
Gilles Peskine01126fa2018-07-12 17:04:55 +02002443#if defined(MBEDTLS_MD_C)
2444static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2445 uint8_t *mac,
2446 size_t mac_size )
2447{
2448 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2449 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2450 size_t hash_size = 0;
2451 size_t block_size = psa_get_hash_block_size( hash_alg );
2452 psa_status_t status;
2453
Gilles Peskine01126fa2018-07-12 17:04:55 +02002454 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2455 if( status != PSA_SUCCESS )
2456 return( status );
2457 /* From here on, tmp needs to be wiped. */
2458
2459 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2460 if( status != PSA_SUCCESS )
2461 goto exit;
2462
2463 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2464 if( status != PSA_SUCCESS )
2465 goto exit;
2466
2467 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2468 if( status != PSA_SUCCESS )
2469 goto exit;
2470
Gilles Peskined911eb72018-08-14 15:18:45 +02002471 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2472 if( status != PSA_SUCCESS )
2473 goto exit;
2474
2475 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002476
2477exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002478 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002479 return( status );
2480}
2481#endif /* MBEDTLS_MD_C */
2482
mohammad16036df908f2018-04-02 08:34:15 -07002483static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002484 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002485 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002487 if( ! operation->key_set )
2488 return( PSA_ERROR_BAD_STATE );
2489 if( operation->iv_required && ! operation->iv_set )
2490 return( PSA_ERROR_BAD_STATE );
2491
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492 if( mac_size < operation->mac_size )
2493 return( PSA_ERROR_BUFFER_TOO_SMALL );
2494
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002496 if( operation->alg == PSA_ALG_CMAC )
2497 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002498 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2499 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2500 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002501 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002502 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002503 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002505 else
2506#endif /* MBEDTLS_CMAC_C */
2507#if defined(MBEDTLS_MD_C)
2508 if( PSA_ALG_IS_HMAC( operation->alg ) )
2509 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002510 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002511 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002512 }
2513 else
2514#endif /* MBEDTLS_MD_C */
2515 {
2516 /* This shouldn't happen if `operation` was initialized by
2517 * a setup function. */
2518 return( PSA_ERROR_BAD_STATE );
2519 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520}
2521
Gilles Peskineacd4be32018-07-08 19:56:25 +02002522psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2523 uint8_t *mac,
2524 size_t mac_size,
2525 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002526{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002527 psa_status_t status;
2528
Jaeden Amero252ef282019-02-15 14:05:35 +00002529 if( operation->alg == 0 )
2530 {
2531 return( PSA_ERROR_BAD_STATE );
2532 }
2533
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002534 /* Fill the output buffer with something that isn't a valid mac
2535 * (barring an attack on the mac and deliberately-crafted input),
2536 * in case the caller doesn't check the return status properly. */
2537 *mac_length = mac_size;
2538 /* If mac_size is 0 then mac may be NULL and then the
2539 * call to memset would have undefined behavior. */
2540 if( mac_size != 0 )
2541 memset( mac, '!', mac_size );
2542
Gilles Peskine89167cb2018-07-08 20:12:23 +02002543 if( ! operation->is_sign )
2544 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002545 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002546 }
mohammad16036df908f2018-04-02 08:34:15 -07002547
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002548 status = psa_mac_finish_internal( operation, mac, mac_size );
2549
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002550 if( status == PSA_SUCCESS )
2551 {
2552 status = psa_mac_abort( operation );
2553 if( status == PSA_SUCCESS )
2554 *mac_length = operation->mac_size;
2555 else
2556 memset( mac, '!', mac_size );
2557 }
2558 else
2559 psa_mac_abort( operation );
2560 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002561}
2562
Gilles Peskineacd4be32018-07-08 19:56:25 +02002563psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2564 const uint8_t *mac,
2565 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566{
Gilles Peskine828ed142018-06-18 23:25:51 +02002567 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002568 psa_status_t status;
2569
Jaeden Amero252ef282019-02-15 14:05:35 +00002570 if( operation->alg == 0 )
2571 {
2572 return( PSA_ERROR_BAD_STATE );
2573 }
2574
Gilles Peskine89167cb2018-07-08 20:12:23 +02002575 if( operation->is_sign )
2576 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002577 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002578 }
2579 if( operation->mac_size != mac_length )
2580 {
2581 status = PSA_ERROR_INVALID_SIGNATURE;
2582 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002583 }
mohammad16036df908f2018-04-02 08:34:15 -07002584
2585 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002586 actual_mac, sizeof( actual_mac ) );
2587
2588 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2589 status = PSA_ERROR_INVALID_SIGNATURE;
2590
2591cleanup:
2592 if( status == PSA_SUCCESS )
2593 status = psa_mac_abort( operation );
2594 else
2595 psa_mac_abort( operation );
2596
Gilles Peskine3f108122018-12-07 18:14:53 +01002597 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002598
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002599 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600}
2601
2602
Gilles Peskine20035e32018-02-03 22:44:14 +01002603
Gilles Peskine20035e32018-02-03 22:44:14 +01002604/****************************************************************/
2605/* Asymmetric cryptography */
2606/****************************************************************/
2607
Gilles Peskine2b450e32018-06-27 15:42:46 +02002608#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002609/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002610 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002611static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2612 size_t hash_length,
2613 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002614{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002615 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002616 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002617 *md_alg = mbedtls_md_get_type( md_info );
2618
2619 /* The Mbed TLS RSA module uses an unsigned int for hash length
2620 * parameters. Validate that it fits so that we don't risk an
2621 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002622#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002623 if( hash_length > UINT_MAX )
2624 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002625#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002626
2627#if defined(MBEDTLS_PKCS1_V15)
2628 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2629 * must be correct. */
2630 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2631 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002632 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002633 if( md_info == NULL )
2634 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002635 if( mbedtls_md_get_size( md_info ) != hash_length )
2636 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002637 }
2638#endif /* MBEDTLS_PKCS1_V15 */
2639
2640#if defined(MBEDTLS_PKCS1_V21)
2641 /* PSS requires a hash internally. */
2642 if( PSA_ALG_IS_RSA_PSS( alg ) )
2643 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002644 if( md_info == NULL )
2645 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002646 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002647#endif /* MBEDTLS_PKCS1_V21 */
2648
Gilles Peskine61b91d42018-06-08 16:09:36 +02002649 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002650}
2651
Gilles Peskine2b450e32018-06-27 15:42:46 +02002652static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2653 psa_algorithm_t alg,
2654 const uint8_t *hash,
2655 size_t hash_length,
2656 uint8_t *signature,
2657 size_t signature_size,
2658 size_t *signature_length )
2659{
2660 psa_status_t status;
2661 int ret;
2662 mbedtls_md_type_t md_alg;
2663
2664 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2665 if( status != PSA_SUCCESS )
2666 return( status );
2667
Gilles Peskine630a18a2018-06-29 17:49:35 +02002668 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002669 return( PSA_ERROR_BUFFER_TOO_SMALL );
2670
2671#if defined(MBEDTLS_PKCS1_V15)
2672 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2673 {
2674 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2675 MBEDTLS_MD_NONE );
2676 ret = mbedtls_rsa_pkcs1_sign( rsa,
2677 mbedtls_ctr_drbg_random,
2678 &global_data.ctr_drbg,
2679 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002680 md_alg,
2681 (unsigned int) hash_length,
2682 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002683 signature );
2684 }
2685 else
2686#endif /* MBEDTLS_PKCS1_V15 */
2687#if defined(MBEDTLS_PKCS1_V21)
2688 if( PSA_ALG_IS_RSA_PSS( alg ) )
2689 {
2690 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2691 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2692 mbedtls_ctr_drbg_random,
2693 &global_data.ctr_drbg,
2694 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002695 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002696 (unsigned int) hash_length,
2697 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002698 signature );
2699 }
2700 else
2701#endif /* MBEDTLS_PKCS1_V21 */
2702 {
2703 return( PSA_ERROR_INVALID_ARGUMENT );
2704 }
2705
2706 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002707 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002708 return( mbedtls_to_psa_error( ret ) );
2709}
2710
2711static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2712 psa_algorithm_t alg,
2713 const uint8_t *hash,
2714 size_t hash_length,
2715 const uint8_t *signature,
2716 size_t signature_length )
2717{
2718 psa_status_t status;
2719 int ret;
2720 mbedtls_md_type_t md_alg;
2721
2722 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2723 if( status != PSA_SUCCESS )
2724 return( status );
2725
Gilles Peskine630a18a2018-06-29 17:49:35 +02002726 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002727 return( PSA_ERROR_BUFFER_TOO_SMALL );
2728
2729#if defined(MBEDTLS_PKCS1_V15)
2730 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2731 {
2732 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2733 MBEDTLS_MD_NONE );
2734 ret = mbedtls_rsa_pkcs1_verify( rsa,
2735 mbedtls_ctr_drbg_random,
2736 &global_data.ctr_drbg,
2737 MBEDTLS_RSA_PUBLIC,
2738 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002739 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002740 hash,
2741 signature );
2742 }
2743 else
2744#endif /* MBEDTLS_PKCS1_V15 */
2745#if defined(MBEDTLS_PKCS1_V21)
2746 if( PSA_ALG_IS_RSA_PSS( alg ) )
2747 {
2748 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2749 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2750 mbedtls_ctr_drbg_random,
2751 &global_data.ctr_drbg,
2752 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002753 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002754 (unsigned int) hash_length,
2755 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002756 signature );
2757 }
2758 else
2759#endif /* MBEDTLS_PKCS1_V21 */
2760 {
2761 return( PSA_ERROR_INVALID_ARGUMENT );
2762 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002763
2764 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2765 * the rest of the signature is invalid". This has little use in
2766 * practice and PSA doesn't report this distinction. */
2767 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2768 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002769 return( mbedtls_to_psa_error( ret ) );
2770}
2771#endif /* MBEDTLS_RSA_C */
2772
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002773#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002774/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2775 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2776 * (even though these functions don't modify it). */
2777static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2778 psa_algorithm_t alg,
2779 const uint8_t *hash,
2780 size_t hash_length,
2781 uint8_t *signature,
2782 size_t signature_size,
2783 size_t *signature_length )
2784{
2785 int ret;
2786 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002787 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002788 mbedtls_mpi_init( &r );
2789 mbedtls_mpi_init( &s );
2790
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002791 if( signature_size < 2 * curve_bytes )
2792 {
2793 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2794 goto cleanup;
2795 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002796
Gilles Peskinea05219c2018-11-16 16:02:56 +01002797#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002798 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2799 {
2800 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2801 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2802 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2803 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2804 hash, hash_length,
2805 md_alg ) );
2806 }
2807 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002808#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002809 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002810 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002811 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2812 hash, hash_length,
2813 mbedtls_ctr_drbg_random,
2814 &global_data.ctr_drbg ) );
2815 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002816
2817 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2818 signature,
2819 curve_bytes ) );
2820 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2821 signature + curve_bytes,
2822 curve_bytes ) );
2823
2824cleanup:
2825 mbedtls_mpi_free( &r );
2826 mbedtls_mpi_free( &s );
2827 if( ret == 0 )
2828 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002829 return( mbedtls_to_psa_error( ret ) );
2830}
2831
2832static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2833 const uint8_t *hash,
2834 size_t hash_length,
2835 const uint8_t *signature,
2836 size_t signature_length )
2837{
2838 int ret;
2839 mbedtls_mpi r, s;
2840 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2841 mbedtls_mpi_init( &r );
2842 mbedtls_mpi_init( &s );
2843
2844 if( signature_length != 2 * curve_bytes )
2845 return( PSA_ERROR_INVALID_SIGNATURE );
2846
2847 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2848 signature,
2849 curve_bytes ) );
2850 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2851 signature + curve_bytes,
2852 curve_bytes ) );
2853
2854 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2855 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002856
2857cleanup:
2858 mbedtls_mpi_free( &r );
2859 mbedtls_mpi_free( &s );
2860 return( mbedtls_to_psa_error( ret ) );
2861}
2862#endif /* MBEDTLS_ECDSA_C */
2863
Gilles Peskinec5487a82018-12-03 18:08:14 +01002864psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002865 psa_algorithm_t alg,
2866 const uint8_t *hash,
2867 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002868 uint8_t *signature,
2869 size_t signature_size,
2870 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002871{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002872 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002873 psa_status_t status;
2874
2875 *signature_length = signature_size;
2876
Gilles Peskinec5487a82018-12-03 18:08:14 +01002877 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002878 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002879 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002880 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002881 {
2882 status = PSA_ERROR_INVALID_ARGUMENT;
2883 goto exit;
2884 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002885
Gilles Peskine20035e32018-02-03 22:44:14 +01002886#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002887 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002888 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002889 status = psa_rsa_sign( slot->data.rsa,
2890 alg,
2891 hash, hash_length,
2892 signature, signature_size,
2893 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002894 }
2895 else
2896#endif /* defined(MBEDTLS_RSA_C) */
2897#if defined(MBEDTLS_ECP_C)
2898 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2899 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002900#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002901 if(
2902#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2903 PSA_ALG_IS_ECDSA( alg )
2904#else
2905 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2906#endif
2907 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002908 status = psa_ecdsa_sign( slot->data.ecp,
2909 alg,
2910 hash, hash_length,
2911 signature, signature_size,
2912 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002913 else
2914#endif /* defined(MBEDTLS_ECDSA_C) */
2915 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002916 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002917 }
itayzafrir5c753392018-05-08 11:18:38 +03002918 }
2919 else
2920#endif /* defined(MBEDTLS_ECP_C) */
2921 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002922 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002923 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002924
2925exit:
2926 /* Fill the unused part of the output buffer (the whole buffer on error,
2927 * the trailing part on success) with something that isn't a valid mac
2928 * (barring an attack on the mac and deliberately-crafted input),
2929 * in case the caller doesn't check the return status properly. */
2930 if( status == PSA_SUCCESS )
2931 memset( signature + *signature_length, '!',
2932 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002933 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002934 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002935 /* If signature_size is 0 then we have nothing to do. We must not call
2936 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002937 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002938}
2939
Gilles Peskinec5487a82018-12-03 18:08:14 +01002940psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002941 psa_algorithm_t alg,
2942 const uint8_t *hash,
2943 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002944 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002945 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002946{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002947 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002948 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002949
Gilles Peskinec5487a82018-12-03 18:08:14 +01002950 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002951 if( status != PSA_SUCCESS )
2952 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002953
Gilles Peskine61b91d42018-06-08 16:09:36 +02002954#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002955 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002957 return( psa_rsa_verify( slot->data.rsa,
2958 alg,
2959 hash, hash_length,
2960 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 }
2962 else
2963#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002964#if defined(MBEDTLS_ECP_C)
2965 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2966 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002967#if defined(MBEDTLS_ECDSA_C)
2968 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002969 return( psa_ecdsa_verify( slot->data.ecp,
2970 hash, hash_length,
2971 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002972 else
2973#endif /* defined(MBEDTLS_ECDSA_C) */
2974 {
2975 return( PSA_ERROR_INVALID_ARGUMENT );
2976 }
itayzafrir5c753392018-05-08 11:18:38 +03002977 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002978 else
2979#endif /* defined(MBEDTLS_ECP_C) */
2980 {
2981 return( PSA_ERROR_NOT_SUPPORTED );
2982 }
2983}
2984
Gilles Peskine072ac562018-06-30 00:21:29 +02002985#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2986static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2987 mbedtls_rsa_context *rsa )
2988{
2989 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2990 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2991 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2992 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2993}
2994#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2995
Gilles Peskinec5487a82018-12-03 18:08:14 +01002996psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002997 psa_algorithm_t alg,
2998 const uint8_t *input,
2999 size_t input_length,
3000 const uint8_t *salt,
3001 size_t salt_length,
3002 uint8_t *output,
3003 size_t output_size,
3004 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003005{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003006 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003007 psa_status_t status;
3008
Darryl Green5cc689a2018-07-24 15:34:10 +01003009 (void) input;
3010 (void) input_length;
3011 (void) salt;
3012 (void) output;
3013 (void) output_size;
3014
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003015 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003016
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003017 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3018 return( PSA_ERROR_INVALID_ARGUMENT );
3019
Gilles Peskinec5487a82018-12-03 18:08:14 +01003020 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003021 if( status != PSA_SUCCESS )
3022 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003023 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003024 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003026
3027#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003028 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029 {
3030 mbedtls_rsa_context *rsa = slot->data.rsa;
3031 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003032 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003033 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003035 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003036 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003037 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3038 mbedtls_ctr_drbg_random,
3039 &global_data.ctr_drbg,
3040 MBEDTLS_RSA_PUBLIC,
3041 input_length,
3042 input,
3043 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044 }
3045 else
3046#endif /* MBEDTLS_PKCS1_V15 */
3047#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003048 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003049 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003050 psa_rsa_oaep_set_padding_mode( alg, rsa );
3051 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3052 mbedtls_ctr_drbg_random,
3053 &global_data.ctr_drbg,
3054 MBEDTLS_RSA_PUBLIC,
3055 salt, salt_length,
3056 input_length,
3057 input,
3058 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059 }
3060 else
3061#endif /* MBEDTLS_PKCS1_V21 */
3062 {
3063 return( PSA_ERROR_INVALID_ARGUMENT );
3064 }
3065 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003066 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067 return( mbedtls_to_psa_error( ret ) );
3068 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003070#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071 {
3072 return( PSA_ERROR_NOT_SUPPORTED );
3073 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003074}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003075
Gilles Peskinec5487a82018-12-03 18:08:14 +01003076psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003077 psa_algorithm_t alg,
3078 const uint8_t *input,
3079 size_t input_length,
3080 const uint8_t *salt,
3081 size_t salt_length,
3082 uint8_t *output,
3083 size_t output_size,
3084 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003086 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087 psa_status_t status;
3088
Darryl Green5cc689a2018-07-24 15:34:10 +01003089 (void) input;
3090 (void) input_length;
3091 (void) salt;
3092 (void) output;
3093 (void) output_size;
3094
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003095 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003097 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3098 return( PSA_ERROR_INVALID_ARGUMENT );
3099
Gilles Peskinec5487a82018-12-03 18:08:14 +01003100 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003101 if( status != PSA_SUCCESS )
3102 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003103 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003104 return( PSA_ERROR_INVALID_ARGUMENT );
3105
3106#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003107 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003108 {
3109 mbedtls_rsa_context *rsa = slot->data.rsa;
3110 int ret;
3111
Gilles Peskine630a18a2018-06-29 17:49:35 +02003112 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113 return( PSA_ERROR_INVALID_ARGUMENT );
3114
3115#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003116 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003118 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3119 mbedtls_ctr_drbg_random,
3120 &global_data.ctr_drbg,
3121 MBEDTLS_RSA_PRIVATE,
3122 output_length,
3123 input,
3124 output,
3125 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126 }
3127 else
3128#endif /* MBEDTLS_PKCS1_V15 */
3129#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003130 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003132 psa_rsa_oaep_set_padding_mode( alg, rsa );
3133 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3134 mbedtls_ctr_drbg_random,
3135 &global_data.ctr_drbg,
3136 MBEDTLS_RSA_PRIVATE,
3137 salt, salt_length,
3138 output_length,
3139 input,
3140 output,
3141 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142 }
3143 else
3144#endif /* MBEDTLS_PKCS1_V21 */
3145 {
3146 return( PSA_ERROR_INVALID_ARGUMENT );
3147 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003148
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149 return( mbedtls_to_psa_error( ret ) );
3150 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003152#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 {
3154 return( PSA_ERROR_NOT_SUPPORTED );
3155 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003156}
Gilles Peskine20035e32018-02-03 22:44:14 +01003157
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003158
3159
mohammad1603503973b2018-03-12 15:59:30 +02003160/****************************************************************/
3161/* Symmetric cryptography */
3162/****************************************************************/
3163
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164/* Initialize the cipher operation structure. Once this function has been
3165 * called, psa_cipher_abort can run and will do the right thing. */
3166static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3167 psa_algorithm_t alg )
3168{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003169 if( ! PSA_ALG_IS_CIPHER( alg ) )
3170 {
3171 memset( operation, 0, sizeof( *operation ) );
3172 return( PSA_ERROR_INVALID_ARGUMENT );
3173 }
3174
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175 operation->alg = alg;
3176 operation->key_set = 0;
3177 operation->iv_set = 0;
3178 operation->iv_required = 1;
3179 operation->iv_size = 0;
3180 operation->block_size = 0;
3181 mbedtls_cipher_init( &operation->ctx.cipher );
3182 return( PSA_SUCCESS );
3183}
3184
Gilles Peskinee553c652018-06-04 16:22:46 +02003185static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003186 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003187 psa_algorithm_t alg,
3188 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003189{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003190 int ret = 0;
3191 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003192 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003193 size_t key_bits;
3194 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003195 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3196 PSA_KEY_USAGE_ENCRYPT :
3197 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003198
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003199 /* A context must be freshly initialized before it can be set up. */
3200 if( operation->alg != 0 )
3201 {
3202 return( PSA_ERROR_BAD_STATE );
3203 }
3204
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003205 status = psa_cipher_init( operation, alg );
3206 if( status != PSA_SUCCESS )
3207 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003208
Gilles Peskinec5487a82018-12-03 18:08:14 +01003209 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003210 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003211 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003212 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003213
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003214 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003215 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003216 {
3217 status = PSA_ERROR_NOT_SUPPORTED;
3218 goto exit;
3219 }
mohammad1603503973b2018-03-12 15:59:30 +02003220
mohammad1603503973b2018-03-12 15:59:30 +02003221 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003222 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003223 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003224
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003225#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003226 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003227 {
3228 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3229 unsigned char keys[24];
3230 memcpy( keys, slot->data.raw.data, 16 );
3231 memcpy( keys + 16, slot->data.raw.data, 8 );
3232 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3233 keys,
3234 192, cipher_operation );
3235 }
3236 else
3237#endif
3238 {
3239 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3240 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003241 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003242 }
Moran Peker41deec42018-04-04 15:43:05 +03003243 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003244 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003245
mohammad16038481e742018-03-18 13:57:31 +02003246#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003247 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003248 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003249 case PSA_ALG_CBC_NO_PADDING:
3250 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3251 MBEDTLS_PADDING_NONE );
3252 break;
3253 case PSA_ALG_CBC_PKCS7:
3254 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3255 MBEDTLS_PADDING_PKCS7 );
3256 break;
3257 default:
3258 /* The algorithm doesn't involve padding. */
3259 ret = 0;
3260 break;
3261 }
3262 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003263 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003264#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3265
mohammad1603503973b2018-03-12 15:59:30 +02003266 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003267 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3268 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3269 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003270 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003271 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003272 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003273#if defined(MBEDTLS_CHACHA20_C)
3274 else
3275 if( alg == PSA_ALG_CHACHA20 )
3276 operation->iv_size = 12;
3277#endif
mohammad1603503973b2018-03-12 15:59:30 +02003278
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003279exit:
3280 if( status == 0 )
3281 status = mbedtls_to_psa_error( ret );
3282 if( status != 0 )
3283 psa_cipher_abort( operation );
3284 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003285}
3286
Gilles Peskinefe119512018-07-08 21:39:34 +02003287psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003288 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003289 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003290{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003291 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003292}
3293
Gilles Peskinefe119512018-07-08 21:39:34 +02003294psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003295 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003296 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003297{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003298 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003299}
3300
Gilles Peskinefe119512018-07-08 21:39:34 +02003301psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3302 unsigned char *iv,
3303 size_t iv_size,
3304 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003305{
itayzafrir534bd7c2018-08-02 13:56:32 +03003306 psa_status_t status;
3307 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003308 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003309 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003310 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003311 }
Moran Peker41deec42018-04-04 15:43:05 +03003312 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003313 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003314 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003315 goto exit;
3316 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003317 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3318 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003319 if( ret != 0 )
3320 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003321 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003322 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003323 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003324
mohammad16038481e742018-03-18 13:57:31 +02003325 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003326 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003327
Moran Peker395db872018-05-31 14:07:14 +03003328exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003329 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003330 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003331 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003332}
3333
Gilles Peskinefe119512018-07-08 21:39:34 +02003334psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3335 const unsigned char *iv,
3336 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003337{
itayzafrir534bd7c2018-08-02 13:56:32 +03003338 psa_status_t status;
3339 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003340 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003341 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003342 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003343 }
Moran Pekera28258c2018-05-29 16:25:04 +03003344 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003345 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003346 status = PSA_ERROR_INVALID_ARGUMENT;
3347 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003348 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003349 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3350 status = mbedtls_to_psa_error( ret );
3351exit:
3352 if( status == PSA_SUCCESS )
3353 operation->iv_set = 1;
3354 else
mohammad1603503973b2018-03-12 15:59:30 +02003355 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003356 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003357}
3358
Gilles Peskinee553c652018-06-04 16:22:46 +02003359psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3360 const uint8_t *input,
3361 size_t input_length,
3362 unsigned char *output,
3363 size_t output_size,
3364 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003365{
itayzafrir534bd7c2018-08-02 13:56:32 +03003366 psa_status_t status;
3367 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003368 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003369
3370 if( operation->alg == 0 )
3371 {
3372 return( PSA_ERROR_BAD_STATE );
3373 }
3374
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003375 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003376 {
3377 /* Take the unprocessed partial block left over from previous
3378 * update calls, if any, plus the input to this call. Remove
3379 * the last partial block, if any. You get the data that will be
3380 * output in this call. */
3381 expected_output_size =
3382 ( operation->ctx.cipher.unprocessed_len + input_length )
3383 / operation->block_size * operation->block_size;
3384 }
3385 else
3386 {
3387 expected_output_size = input_length;
3388 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003389
Gilles Peskine89d789c2018-06-04 17:17:16 +02003390 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003391 {
3392 status = PSA_ERROR_BUFFER_TOO_SMALL;
3393 goto exit;
3394 }
mohammad160382759612018-03-12 18:16:40 +02003395
mohammad1603503973b2018-03-12 15:59:30 +02003396 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003397 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003398 status = mbedtls_to_psa_error( ret );
3399exit:
3400 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003401 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003402 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003403}
3404
Gilles Peskinee553c652018-06-04 16:22:46 +02003405psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3406 uint8_t *output,
3407 size_t output_size,
3408 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003409{
David Saadab4ecc272019-02-14 13:48:10 +02003410 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003411 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003412 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003413
mohammad1603503973b2018-03-12 15:59:30 +02003414 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003415 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003416 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003417 }
3418 if( operation->iv_required && ! operation->iv_set )
3419 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003420 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003421 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003422
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003423 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003424 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3425 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003426 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003427 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003428 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003429 }
3430
Janos Follath315b51c2018-07-09 16:04:51 +01003431 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3432 temp_output_buffer,
3433 output_length );
3434 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003435 {
Janos Follath315b51c2018-07-09 16:04:51 +01003436 status = mbedtls_to_psa_error( cipher_ret );
3437 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003438 }
Janos Follath315b51c2018-07-09 16:04:51 +01003439
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003440 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003441 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003442 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003443 memcpy( output, temp_output_buffer, *output_length );
3444 else
3445 {
Janos Follath315b51c2018-07-09 16:04:51 +01003446 status = PSA_ERROR_BUFFER_TOO_SMALL;
3447 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003448 }
mohammad1603503973b2018-03-12 15:59:30 +02003449
Gilles Peskine3f108122018-12-07 18:14:53 +01003450 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003451 status = psa_cipher_abort( operation );
3452
3453 return( status );
3454
3455error:
3456
3457 *output_length = 0;
3458
Gilles Peskine3f108122018-12-07 18:14:53 +01003459 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003460 (void) psa_cipher_abort( operation );
3461
3462 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003463}
3464
Gilles Peskinee553c652018-06-04 16:22:46 +02003465psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3466{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003467 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003468 {
3469 /* The object has (apparently) been initialized but it is not
3470 * in use. It's ok to call abort on such an object, and there's
3471 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003472 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003473 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003474
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003475 /* Sanity check (shouldn't happen: operation->alg should
3476 * always have been initialized to a valid value). */
3477 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3478 return( PSA_ERROR_BAD_STATE );
3479
mohammad1603503973b2018-03-12 15:59:30 +02003480 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003481
Moran Peker41deec42018-04-04 15:43:05 +03003482 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003483 operation->key_set = 0;
3484 operation->iv_set = 0;
3485 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003486 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003487 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003488
Moran Peker395db872018-05-31 14:07:14 +03003489 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003490}
3491
Gilles Peskinea0655c32018-04-30 17:06:50 +02003492
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003493
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003494
mohammad16035955c982018-04-26 00:53:03 +03003495/****************************************************************/
3496/* AEAD */
3497/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003498
Gilles Peskineedf9a652018-08-17 18:11:56 +02003499typedef struct
3500{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003501 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003502 const mbedtls_cipher_info_t *cipher_info;
3503 union
3504 {
3505#if defined(MBEDTLS_CCM_C)
3506 mbedtls_ccm_context ccm;
3507#endif /* MBEDTLS_CCM_C */
3508#if defined(MBEDTLS_GCM_C)
3509 mbedtls_gcm_context gcm;
3510#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003511#if defined(MBEDTLS_CHACHAPOLY_C)
3512 mbedtls_chachapoly_context chachapoly;
3513#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003514 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003515 psa_algorithm_t core_alg;
3516 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003517 uint8_t tag_length;
3518} aead_operation_t;
3519
Gilles Peskine30a9e412019-01-14 18:36:12 +01003520static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003521{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003522 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003523 {
3524#if defined(MBEDTLS_CCM_C)
3525 case PSA_ALG_CCM:
3526 mbedtls_ccm_free( &operation->ctx.ccm );
3527 break;
3528#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003529#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003530 case PSA_ALG_GCM:
3531 mbedtls_gcm_free( &operation->ctx.gcm );
3532 break;
3533#endif /* MBEDTLS_GCM_C */
3534 }
3535}
3536
3537static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003538 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003539 psa_key_usage_t usage,
3540 psa_algorithm_t alg )
3541{
3542 psa_status_t status;
3543 size_t key_bits;
3544 mbedtls_cipher_id_t cipher_id;
3545
Gilles Peskinec5487a82018-12-03 18:08:14 +01003546 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003547 if( status != PSA_SUCCESS )
3548 return( status );
3549
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003550 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003551
3552 operation->cipher_info =
3553 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3554 &cipher_id );
3555 if( operation->cipher_info == NULL )
3556 return( PSA_ERROR_NOT_SUPPORTED );
3557
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003558 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003559 {
3560#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003561 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3562 operation->core_alg = PSA_ALG_CCM;
3563 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003564 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3565 * The call to mbedtls_ccm_encrypt_and_tag or
3566 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003567 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3568 return( PSA_ERROR_INVALID_ARGUMENT );
3569 mbedtls_ccm_init( &operation->ctx.ccm );
3570 status = mbedtls_to_psa_error(
3571 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3572 operation->slot->data.raw.data,
3573 (unsigned int) key_bits ) );
3574 if( status != 0 )
3575 goto cleanup;
3576 break;
3577#endif /* MBEDTLS_CCM_C */
3578
3579#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003580 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3581 operation->core_alg = PSA_ALG_GCM;
3582 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003583 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3584 * The call to mbedtls_gcm_crypt_and_tag or
3585 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003586 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3587 return( PSA_ERROR_INVALID_ARGUMENT );
3588 mbedtls_gcm_init( &operation->ctx.gcm );
3589 status = mbedtls_to_psa_error(
3590 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3591 operation->slot->data.raw.data,
3592 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003593 if( status != 0 )
3594 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003595 break;
3596#endif /* MBEDTLS_GCM_C */
3597
Gilles Peskine26869f22019-05-06 15:25:00 +02003598#if defined(MBEDTLS_CHACHAPOLY_C)
3599 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3600 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3601 operation->full_tag_length = 16;
3602 /* We only support the default tag length. */
3603 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3604 return( PSA_ERROR_NOT_SUPPORTED );
3605 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3606 status = mbedtls_to_psa_error(
3607 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3608 operation->slot->data.raw.data ) );
3609 if( status != 0 )
3610 goto cleanup;
3611 break;
3612#endif /* MBEDTLS_CHACHAPOLY_C */
3613
Gilles Peskineedf9a652018-08-17 18:11:56 +02003614 default:
3615 return( PSA_ERROR_NOT_SUPPORTED );
3616 }
3617
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003618 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3619 {
3620 status = PSA_ERROR_INVALID_ARGUMENT;
3621 goto cleanup;
3622 }
3623 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003624
Gilles Peskineedf9a652018-08-17 18:11:56 +02003625 return( PSA_SUCCESS );
3626
3627cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003628 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003629 return( status );
3630}
3631
Gilles Peskinec5487a82018-12-03 18:08:14 +01003632psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003633 psa_algorithm_t alg,
3634 const uint8_t *nonce,
3635 size_t nonce_length,
3636 const uint8_t *additional_data,
3637 size_t additional_data_length,
3638 const uint8_t *plaintext,
3639 size_t plaintext_length,
3640 uint8_t *ciphertext,
3641 size_t ciphertext_size,
3642 size_t *ciphertext_length )
3643{
mohammad16035955c982018-04-26 00:53:03 +03003644 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003645 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003646 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003647
mohammad1603f08a5502018-06-03 15:05:47 +03003648 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003649
Gilles Peskinec5487a82018-12-03 18:08:14 +01003650 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003651 if( status != PSA_SUCCESS )
3652 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003653
Gilles Peskineedf9a652018-08-17 18:11:56 +02003654 /* For all currently supported modes, the tag is at the end of the
3655 * ciphertext. */
3656 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3657 {
3658 status = PSA_ERROR_BUFFER_TOO_SMALL;
3659 goto exit;
3660 }
3661 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003662
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003663#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003664 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003665 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003666 status = mbedtls_to_psa_error(
3667 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3668 MBEDTLS_GCM_ENCRYPT,
3669 plaintext_length,
3670 nonce, nonce_length,
3671 additional_data, additional_data_length,
3672 plaintext, ciphertext,
3673 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003674 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003675 else
3676#endif /* MBEDTLS_GCM_C */
3677#if defined(MBEDTLS_CCM_C)
3678 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003679 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003680 status = mbedtls_to_psa_error(
3681 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3682 plaintext_length,
3683 nonce, nonce_length,
3684 additional_data,
3685 additional_data_length,
3686 plaintext, ciphertext,
3687 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003688 }
mohammad16035c8845f2018-05-09 05:40:09 -07003689 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003690#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003691#if defined(MBEDTLS_CHACHAPOLY_C)
3692 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3693 {
3694 if( nonce_length != 12 || operation.tag_length != 16 )
3695 {
3696 status = PSA_ERROR_NOT_SUPPORTED;
3697 goto exit;
3698 }
3699 status = mbedtls_to_psa_error(
3700 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3701 plaintext_length,
3702 nonce,
3703 additional_data,
3704 additional_data_length,
3705 plaintext,
3706 ciphertext,
3707 tag ) );
3708 }
3709 else
3710#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003711 {
mohammad1603554faad2018-06-03 15:07:38 +03003712 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003713 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003714
Gilles Peskineedf9a652018-08-17 18:11:56 +02003715 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3716 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003717
Gilles Peskineedf9a652018-08-17 18:11:56 +02003718exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003719 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003720 if( status == PSA_SUCCESS )
3721 *ciphertext_length = plaintext_length + operation.tag_length;
3722 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003723}
3724
Gilles Peskineee652a32018-06-01 19:23:52 +02003725/* Locate the tag in a ciphertext buffer containing the encrypted data
3726 * followed by the tag. Return the length of the part preceding the tag in
3727 * *plaintext_length. This is the size of the plaintext in modes where
3728 * the encrypted data has the same size as the plaintext, such as
3729 * CCM and GCM. */
3730static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3731 const uint8_t *ciphertext,
3732 size_t ciphertext_length,
3733 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003734 const uint8_t **p_tag )
3735{
3736 size_t payload_length;
3737 if( tag_length > ciphertext_length )
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 payload_length = ciphertext_length - tag_length;
3740 if( payload_length > plaintext_size )
3741 return( PSA_ERROR_BUFFER_TOO_SMALL );
3742 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003743 return( PSA_SUCCESS );
3744}
3745
Gilles Peskinec5487a82018-12-03 18:08:14 +01003746psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003747 psa_algorithm_t alg,
3748 const uint8_t *nonce,
3749 size_t nonce_length,
3750 const uint8_t *additional_data,
3751 size_t additional_data_length,
3752 const uint8_t *ciphertext,
3753 size_t ciphertext_length,
3754 uint8_t *plaintext,
3755 size_t plaintext_size,
3756 size_t *plaintext_length )
3757{
mohammad16035955c982018-04-26 00:53:03 +03003758 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003759 aead_operation_t operation;
3760 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003761
Gilles Peskineee652a32018-06-01 19:23:52 +02003762 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003763
Gilles Peskinec5487a82018-12-03 18:08:14 +01003764 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003765 if( status != PSA_SUCCESS )
3766 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003767
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003768 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3769 ciphertext, ciphertext_length,
3770 plaintext_size, &tag );
3771 if( status != PSA_SUCCESS )
3772 goto exit;
3773
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003774#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003775 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003776 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003777 status = mbedtls_to_psa_error(
3778 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3779 ciphertext_length - operation.tag_length,
3780 nonce, nonce_length,
3781 additional_data,
3782 additional_data_length,
3783 tag, operation.tag_length,
3784 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003785 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003786 else
3787#endif /* MBEDTLS_GCM_C */
3788#if defined(MBEDTLS_CCM_C)
3789 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003790 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003791 status = mbedtls_to_psa_error(
3792 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3793 ciphertext_length - operation.tag_length,
3794 nonce, nonce_length,
3795 additional_data,
3796 additional_data_length,
3797 ciphertext, plaintext,
3798 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003799 }
mohammad160339574652018-06-01 04:39:53 -07003800 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003801#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003802#if defined(MBEDTLS_CHACHAPOLY_C)
3803 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3804 {
3805 if( nonce_length != 12 || operation.tag_length != 16 )
3806 {
3807 status = PSA_ERROR_NOT_SUPPORTED;
3808 goto exit;
3809 }
3810 status = mbedtls_to_psa_error(
3811 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3812 ciphertext_length - operation.tag_length,
3813 nonce,
3814 additional_data,
3815 additional_data_length,
3816 tag,
3817 ciphertext,
3818 plaintext ) );
3819 }
3820 else
3821#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003822 {
mohammad1603554faad2018-06-03 15:07:38 +03003823 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003824 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003825
Gilles Peskineedf9a652018-08-17 18:11:56 +02003826 if( status != PSA_SUCCESS && plaintext_size != 0 )
3827 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003828
Gilles Peskineedf9a652018-08-17 18:11:56 +02003829exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003830 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003831 if( status == PSA_SUCCESS )
3832 *plaintext_length = ciphertext_length - operation.tag_length;
3833 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003834}
3835
Gilles Peskinea0655c32018-04-30 17:06:50 +02003836
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003837
Gilles Peskine20035e32018-02-03 22:44:14 +01003838/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003839/* Generators */
3840/****************************************************************/
3841
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003842#define HKDF_STATE_INIT 0 /* no input yet */
3843#define HKDF_STATE_STARTED 1 /* got salt */
3844#define HKDF_STATE_KEYED 2 /* got key */
3845#define HKDF_STATE_OUTPUT 3 /* output started */
3846
Gilles Peskinecbe66502019-05-16 16:59:18 +02003847static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003848 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003849{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003850 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3851 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003852 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003853 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003854}
3855
3856
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003857psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003858{
3859 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003860 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003861 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003862 {
3863 /* The object has (apparently) been initialized but it is not
3864 * in use. It's ok to call abort on such an object, and there's
3865 * nothing to do. */
3866 }
3867 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003868 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003869 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003870 if( operation->ctx.buffer.data != NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02003871 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003872 mbedtls_platform_zeroize( operation->ctx.buffer.data,
3873 operation->ctx.buffer.size );
3874 mbedtls_free( operation->ctx.buffer.data );
Gilles Peskine751d9652018-09-18 12:05:44 +02003875 }
3876 }
3877 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003878#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003879 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003880 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003881 mbedtls_free( operation->ctx.hkdf.info );
3882 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003883 }
Janos Follath999f6482019-06-11 12:04:10 +01003884#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003885 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003886 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003887 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003888 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003889 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003890 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003891 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3892 operation->ctx.tls12_prf.key_len );
3893 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003894 }
Hanno Becker580fba12018-11-13 20:50:45 +00003895
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003896 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003897 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003898 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3899 operation->ctx.tls12_prf.Ai_with_seed_len );
3900 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003901 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003902 }
Janos Follath999f6482019-06-11 12:04:10 +01003903#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003904 else
3905#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003906 {
3907 status = PSA_ERROR_BAD_STATE;
3908 }
Janos Follath083036a2019-06-11 10:22:26 +01003909 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003910 return( status );
3911}
3912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003913psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003914 size_t *capacity)
3915{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003916 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003917 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003918 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003919 return PSA_ERROR_BAD_STATE;
3920 }
3921
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003922 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003923 return( PSA_SUCCESS );
3924}
3925
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003926psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003927 size_t capacity )
3928{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003929 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003930 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003931 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003932 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003933 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003934 return( PSA_SUCCESS );
3935}
3936
Gilles Peskinebef7f142018-07-12 17:22:21 +02003937#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003938/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003939 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003940static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941 psa_algorithm_t hash_alg,
3942 uint8_t *output,
3943 size_t output_length )
3944{
3945 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3946 psa_status_t status;
3947
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003948 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3949 return( PSA_ERROR_BAD_STATE );
3950 hkdf->state = HKDF_STATE_OUTPUT;
3951
Gilles Peskinebef7f142018-07-12 17:22:21 +02003952 while( output_length != 0 )
3953 {
3954 /* Copy what remains of the current block */
3955 uint8_t n = hash_length - hkdf->offset_in_block;
3956 if( n > output_length )
3957 n = (uint8_t) output_length;
3958 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3959 output += n;
3960 output_length -= n;
3961 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003962 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003963 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003964 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003965 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003966 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003967 * object was corrupted or if this function is called directly
3968 * inside the library. */
3969 if( hkdf->block_number == 0xff )
3970 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003971
3972 /* We need a new block */
3973 ++hkdf->block_number;
3974 hkdf->offset_in_block = 0;
3975 status = psa_hmac_setup_internal( &hkdf->hmac,
3976 hkdf->prk, hash_length,
3977 hash_alg );
3978 if( status != PSA_SUCCESS )
3979 return( status );
3980 if( hkdf->block_number != 1 )
3981 {
3982 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3983 hkdf->output_block,
3984 hash_length );
3985 if( status != PSA_SUCCESS )
3986 return( status );
3987 }
3988 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3989 hkdf->info,
3990 hkdf->info_length );
3991 if( status != PSA_SUCCESS )
3992 return( status );
3993 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3994 &hkdf->block_number, 1 );
3995 if( status != PSA_SUCCESS )
3996 return( status );
3997 status = psa_hmac_finish_internal( &hkdf->hmac,
3998 hkdf->output_block,
3999 sizeof( hkdf->output_block ) );
4000 if( status != PSA_SUCCESS )
4001 return( status );
4002 }
4003
4004 return( PSA_SUCCESS );
4005}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004006
Janos Follath999f6482019-06-11 12:04:10 +01004007#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004008static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4009 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004010 psa_algorithm_t alg )
4011{
4012 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4013 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4014 psa_hmac_internal_data hmac;
4015 psa_status_t status, cleanup_status;
4016
Hanno Becker3b339e22018-11-13 20:56:14 +00004017 unsigned char *Ai;
4018 size_t Ai_len;
4019
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004020 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004021 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004023 * object was corrupted or if this function is called directly
4024 * inside the library. */
4025 if( tls12_prf->block_number == 0xff )
4026 return( PSA_ERROR_BAD_STATE );
4027
4028 /* We need a new block */
4029 ++tls12_prf->block_number;
4030 tls12_prf->offset_in_block = 0;
4031
4032 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4033 *
4034 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4035 *
4036 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4037 * HMAC_hash(secret, A(2) + seed) +
4038 * HMAC_hash(secret, A(3) + seed) + ...
4039 *
4040 * A(0) = seed
4041 * A(i) = HMAC_hash( secret, A(i-1) )
4042 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004043 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004044 * `HMAC_hash(secret, A(i) + seed)` from which the output
4045 * is currently extracted as `output_block`, while
4046 * `A(i) + seed` is stored in `Ai_with_seed`.
4047 *
4048 * Generating a new block means recalculating `Ai_with_seed`
4049 * from the A(i)-part of it, and afterwards recalculating
4050 * `output_block`.
4051 *
4052 * A(0) is computed at setup time.
4053 *
4054 */
4055
4056 psa_hmac_init_internal( &hmac );
4057
4058 /* We must distinguish the calculation of A(1) from those
4059 * of A(2) and higher, because A(0)=seed has a different
4060 * length than the other A(i). */
4061 if( tls12_prf->block_number == 1 )
4062 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004063 Ai = tls12_prf->Ai_with_seed + hash_length;
4064 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004065 }
4066 else
4067 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004068 Ai = tls12_prf->Ai_with_seed;
4069 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004070 }
4071
Hanno Becker3b339e22018-11-13 20:56:14 +00004072 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4073 status = psa_hmac_setup_internal( &hmac,
4074 tls12_prf->key,
4075 tls12_prf->key_len,
4076 hash_alg );
4077 if( status != PSA_SUCCESS )
4078 goto cleanup;
4079
4080 status = psa_hash_update( &hmac.hash_ctx,
4081 Ai, Ai_len );
4082 if( status != PSA_SUCCESS )
4083 goto cleanup;
4084
4085 status = psa_hmac_finish_internal( &hmac,
4086 tls12_prf->Ai_with_seed,
4087 hash_length );
4088 if( status != PSA_SUCCESS )
4089 goto cleanup;
4090
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004091 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4092 status = psa_hmac_setup_internal( &hmac,
4093 tls12_prf->key,
4094 tls12_prf->key_len,
4095 hash_alg );
4096 if( status != PSA_SUCCESS )
4097 goto cleanup;
4098
4099 status = psa_hash_update( &hmac.hash_ctx,
4100 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004101 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004102 if( status != PSA_SUCCESS )
4103 goto cleanup;
4104
4105 status = psa_hmac_finish_internal( &hmac,
4106 tls12_prf->output_block,
4107 hash_length );
4108 if( status != PSA_SUCCESS )
4109 goto cleanup;
4110
4111cleanup:
4112
4113 cleanup_status = psa_hmac_abort_internal( &hmac );
4114 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4115 status = cleanup_status;
4116
4117 return( status );
4118}
Janos Follath999f6482019-06-11 12:04:10 +01004119#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004120
Janos Follath999f6482019-06-11 12:04:10 +01004121#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004122/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004123 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004124static psa_status_t psa_key_derivation_tls12_prf_read(
4125 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004126 psa_algorithm_t alg,
4127 uint8_t *output,
4128 size_t output_length )
4129{
4130 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4131 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4132 psa_status_t status;
4133
4134 while( output_length != 0 )
4135 {
4136 /* Copy what remains of the current block */
4137 uint8_t n = hash_length - tls12_prf->offset_in_block;
4138
4139 /* Check if we have fully processed the current block. */
4140 if( n == 0 )
4141 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004142 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004143 alg );
4144 if( status != PSA_SUCCESS )
4145 return( status );
4146
4147 continue;
4148 }
4149
4150 if( n > output_length )
4151 n = (uint8_t) output_length;
4152 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4153 n );
4154 output += n;
4155 output_length -= n;
4156 tls12_prf->offset_in_block += n;
4157 }
4158
4159 return( PSA_SUCCESS );
4160}
Janos Follath999f6482019-06-11 12:04:10 +01004161#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004162#endif /* MBEDTLS_MD_C */
4163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164psa_status_t psa_key_derivation_output_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004165 uint8_t *output,
4166 size_t output_length )
4167{
4168 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004169 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004170
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004172 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004173 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004174 return PSA_ERROR_BAD_STATE;
4175 }
4176
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004178 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004180 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004181 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004182 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004183 goto exit;
4184 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004186 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004188 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004189 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4190 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004192 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004193 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004196
Gilles Peskine969c5d62019-01-16 15:53:06 +01004197 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004198 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 /* Initially, the capacity of a selection operation is always
4200 * the size of the buffer, i.e. `operation->ctx.buffer.size`,
Gilles Peskine211a4362018-10-25 22:22:31 +02004201 * abbreviated in this comment as `size`. When the remaining
4202 * capacity is `c`, the next bytes to serve start `c` bytes
4203 * from the end of the buffer, i.e. `size - c` from the
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004204 * beginning of the buffer. Since `operation->capacity` was just
Gilles Peskine211a4362018-10-25 22:22:31 +02004205 * decremented above, we need to serve the bytes from
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004206 * `size - operation->capacity - output_length` to
4207 * `size - operation->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004208 size_t offset =
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004209 operation->ctx.buffer.size - operation->capacity - output_length;
4210 memcpy( output, operation->ctx.buffer.data + offset, output_length );
Gilles Peskine751d9652018-09-18 12:05:44 +02004211 status = PSA_SUCCESS;
4212 }
4213 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004214#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004215 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004216 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004217 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004219 output, output_length );
4220 }
Janos Follath999f6482019-06-11 12:04:10 +01004221#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004222 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4223 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004224 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004225 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004226 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004227 output_length );
4228 }
Janos Follath999f6482019-06-11 12:04:10 +01004229#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004230 else
4231#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004232 {
4233 return( PSA_ERROR_BAD_STATE );
4234 }
4235
4236exit:
4237 if( status != PSA_SUCCESS )
4238 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004239 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004240 * This allows us to differentiate between exhausted operations and
4241 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4242 * operations. */
4243 psa_algorithm_t alg = operation->alg;
4244 psa_key_derivation_abort( operation );
4245 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004246 memset( output, '!', output_length );
4247 }
4248 return( status );
4249}
4250
Gilles Peskine08542d82018-07-19 17:05:42 +02004251#if defined(MBEDTLS_DES_C)
4252static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4253{
4254 if( data_size >= 8 )
4255 mbedtls_des_key_set_parity( data );
4256 if( data_size >= 16 )
4257 mbedtls_des_key_set_parity( data + 8 );
4258 if( data_size >= 24 )
4259 mbedtls_des_key_set_parity( data + 16 );
4260}
4261#endif /* MBEDTLS_DES_C */
4262
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004263static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004264 psa_key_slot_t *slot,
4265 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004266 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004267{
4268 uint8_t *data = NULL;
4269 size_t bytes = PSA_BITS_TO_BYTES( bits );
4270 psa_status_t status;
4271
4272 if( ! key_type_is_raw_bytes( slot->type ) )
4273 return( PSA_ERROR_INVALID_ARGUMENT );
4274 if( bits % 8 != 0 )
4275 return( PSA_ERROR_INVALID_ARGUMENT );
4276 data = mbedtls_calloc( 1, bytes );
4277 if( data == NULL )
4278 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004280 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004281 if( status != PSA_SUCCESS )
4282 goto exit;
4283#if defined(MBEDTLS_DES_C)
4284 if( slot->type == PSA_KEY_TYPE_DES )
4285 psa_des_set_key_parity( data, bytes );
4286#endif /* MBEDTLS_DES_C */
4287 status = psa_import_key_into_slot( slot, data, bytes );
4288
4289exit:
4290 mbedtls_free( data );
4291 return( status );
4292}
4293
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004294psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004295 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004296 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004297{
4298 psa_status_t status;
4299 psa_key_slot_t *slot = NULL;
4300 status = psa_start_key_creation( attributes, handle, &slot );
4301 if( status == PSA_SUCCESS )
4302 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004303 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004304 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004305 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004306 }
4307 if( status == PSA_SUCCESS )
4308 status = psa_finish_key_creation( slot );
4309 if( status != PSA_SUCCESS )
4310 {
4311 psa_fail_key_creation( slot );
4312 *handle = 0;
4313 }
4314 return( status );
4315}
4316
Gilles Peskineeab56e42018-07-12 17:12:33 +02004317
4318
4319/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004320/* Key derivation */
4321/****************************************************************/
4322
Gilles Peskinea05219c2018-11-16 16:02:56 +01004323#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004324#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004325/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004326 * of the HKDF algorithm.
4327 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004328 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004329 * to potentially free embedded data structures and wipe confidential data.
4330 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004331static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004332 const uint8_t *secret,
4333 size_t secret_length,
4334 psa_algorithm_t hash_alg,
4335 const uint8_t *salt,
4336 size_t salt_length,
4337 const uint8_t *label,
4338 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004339{
4340 psa_status_t status;
4341 status = psa_hmac_setup_internal( &hkdf->hmac,
4342 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004343 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004344 if( status != PSA_SUCCESS )
4345 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004346 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004347 if( status != PSA_SUCCESS )
4348 return( status );
4349 status = psa_hmac_finish_internal( &hkdf->hmac,
4350 hkdf->prk,
4351 sizeof( hkdf->prk ) );
4352 if( status != PSA_SUCCESS )
4353 return( status );
4354 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4355 hkdf->block_number = 0;
4356 hkdf->info_length = label_length;
4357 if( label_length != 0 )
4358 {
4359 hkdf->info = mbedtls_calloc( 1, label_length );
4360 if( hkdf->info == NULL )
4361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4362 memcpy( hkdf->info, label, label_length );
4363 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004364 hkdf->state = HKDF_STATE_KEYED;
4365 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004366 return( PSA_SUCCESS );
4367}
Janos Follath71a4c912019-06-11 09:14:47 +01004368#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004369#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004370
Gilles Peskinea05219c2018-11-16 16:02:56 +01004371#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004372#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004374 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004375 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004376 * to potentially free embedded data structures and wipe confidential data.
4377 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004378static psa_status_t psa_key_derivation_tls12_prf_setup(
4379 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004380 const unsigned char *key,
4381 size_t key_len,
4382 psa_algorithm_t hash_alg,
4383 const uint8_t *salt,
4384 size_t salt_length,
4385 const uint8_t *label,
4386 size_t label_length )
4387{
4388 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004389 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4390 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004391
4392 tls12_prf->key = mbedtls_calloc( 1, key_len );
4393 if( tls12_prf->key == NULL )
4394 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4395 tls12_prf->key_len = key_len;
4396 memcpy( tls12_prf->key, key, key_len );
4397
Hanno Becker580fba12018-11-13 20:50:45 +00004398 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004399 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004400 if( overflow )
4401 return( PSA_ERROR_INVALID_ARGUMENT );
4402
4403 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4404 if( tls12_prf->Ai_with_seed == NULL )
4405 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4406 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4407
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004408 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4409 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004410 if( label_length != 0 )
4411 {
4412 memcpy( tls12_prf->Ai_with_seed + hash_length,
4413 label, label_length );
4414 }
4415
4416 if( salt_length != 0 )
4417 {
4418 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4419 salt, salt_length );
4420 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004421
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004422 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004423 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004424 tls12_prf->block_number = 0;
4425 tls12_prf->offset_in_block = hash_length;
4426
4427 return( PSA_SUCCESS );
4428}
Janos Follath71a4c912019-06-11 09:14:47 +01004429#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004430
Janos Follath71a4c912019-06-11 09:14:47 +01004431#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004433static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4434 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004435 const unsigned char *psk,
4436 size_t psk_len,
4437 psa_algorithm_t hash_alg,
4438 const uint8_t *salt,
4439 size_t salt_length,
4440 const uint8_t *label,
4441 size_t label_length )
4442{
4443 psa_status_t status;
4444 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4445
4446 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4447 return( PSA_ERROR_INVALID_ARGUMENT );
4448
4449 /* Quoting RFC 4279, Section 2:
4450 *
4451 * The premaster secret is formed as follows: if the PSK is N octets
4452 * long, concatenate a uint16 with the value N, N zero octets, a second
4453 * uint16 with the value N, and the PSK itself.
4454 */
4455
4456 pms[0] = ( psk_len >> 8 ) & 0xff;
4457 pms[1] = ( psk_len >> 0 ) & 0xff;
4458 memset( pms + 2, 0, psk_len );
4459 pms[2 + psk_len + 0] = pms[0];
4460 pms[2 + psk_len + 1] = pms[1];
4461 memcpy( pms + 4 + psk_len, psk, psk_len );
4462
Gilles Peskinecbe66502019-05-16 16:59:18 +02004463 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004464 pms, 4 + 2 * psk_len,
4465 hash_alg,
4466 salt, salt_length,
4467 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004468
Gilles Peskine3f108122018-12-07 18:14:53 +01004469 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004470 return( status );
4471}
Janos Follath71a4c912019-06-11 09:14:47 +01004472#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004473#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004474
Janos Follath71a4c912019-06-11 09:14:47 +01004475#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004476/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004477 * to potentially free embedded data structures and wipe confidential data.
4478 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004479static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004481 const uint8_t *secret, size_t secret_length,
4482 psa_algorithm_t alg,
4483 const uint8_t *salt, size_t salt_length,
4484 const uint8_t *label, size_t label_length,
4485 size_t capacity )
4486{
4487 psa_status_t status;
4488 size_t max_capacity;
4489
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 /* Set operation->alg even on failure so that abort knows what to do. */
4491 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004492
Gilles Peskine751d9652018-09-18 12:05:44 +02004493 if( alg == PSA_ALG_SELECT_RAW )
4494 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004495 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004496 if( salt_length != 0 )
4497 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004498 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004499 if( label_length != 0 )
4500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 operation->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4502 if( operation->ctx.buffer.data == NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 memcpy( operation->ctx.buffer.data, secret, secret_length );
4505 operation->ctx.buffer.size = secret_length;
Gilles Peskine751d9652018-09-18 12:05:44 +02004506 max_capacity = secret_length;
4507 status = PSA_SUCCESS;
4508 }
4509 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004510#if defined(MBEDTLS_MD_C)
4511 if( PSA_ALG_IS_HKDF( alg ) )
4512 {
4513 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4514 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4515 if( hash_size == 0 )
4516 return( PSA_ERROR_NOT_SUPPORTED );
4517 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004518 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004519 secret, secret_length,
4520 hash_alg,
4521 salt, salt_length,
4522 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004523 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004524 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4525 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4526 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004527 {
4528 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4529 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4530
4531 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4532 if( hash_alg != PSA_ALG_SHA_256 &&
4533 hash_alg != PSA_ALG_SHA_384 )
4534 {
4535 return( PSA_ERROR_NOT_SUPPORTED );
4536 }
4537
4538 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004539
4540 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4541 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004543 secret, secret_length,
4544 hash_alg, salt, salt_length,
4545 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004546 }
4547 else
4548 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004549 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004551 secret, secret_length,
4552 hash_alg, salt, salt_length,
4553 label, label_length );
4554 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004555 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004556 else
4557#endif
4558 {
4559 return( PSA_ERROR_NOT_SUPPORTED );
4560 }
4561
4562 if( status != PSA_SUCCESS )
4563 return( status );
4564
4565 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004567 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004568 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004569 else
4570 return( PSA_ERROR_INVALID_ARGUMENT );
4571
4572 return( PSA_SUCCESS );
4573}
Janos Follath71a4c912019-06-11 09:14:47 +01004574#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004575
Janos Follath71a4c912019-06-11 09:14:47 +01004576#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004578 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004579 psa_algorithm_t alg,
4580 const uint8_t *salt,
4581 size_t salt_length,
4582 const uint8_t *label,
4583 size_t label_length,
4584 size_t capacity )
4585{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004586 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004587 psa_status_t status;
4588
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004589 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004590 return( PSA_ERROR_BAD_STATE );
4591
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004592 /* Make sure that alg is a key derivation algorithm. This prevents
4593 * key selection algorithms, which psa_key_derivation_internal
4594 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004595 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4596 return( PSA_ERROR_INVALID_ARGUMENT );
4597
Gilles Peskinec5487a82018-12-03 18:08:14 +01004598 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004599 if( status != PSA_SUCCESS )
4600 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004601
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004602 if( slot->type != PSA_KEY_TYPE_DERIVE )
4603 return( PSA_ERROR_INVALID_ARGUMENT );
4604
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004606 slot->data.raw.data,
4607 slot->data.raw.bytes,
4608 alg,
4609 salt, salt_length,
4610 label, label_length,
4611 capacity );
4612 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004613 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004614 return( status );
4615}
Janos Follath71a4c912019-06-11 09:14:47 +01004616#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004617
Gilles Peskine969c5d62019-01-16 15:53:06 +01004618static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004620 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004621{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004622 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004623#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004624 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4625 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4626 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004627 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004628 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004629 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4630 if( hash_size == 0 )
4631 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004632 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4633 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004634 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004635 {
4636 return( PSA_ERROR_NOT_SUPPORTED );
4637 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004639 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004640 }
4641#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004642 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004643 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004644}
4645
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004647 psa_algorithm_t alg )
4648{
4649 psa_status_t status;
4650
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004652 return( PSA_ERROR_BAD_STATE );
4653
Gilles Peskine6843c292019-01-18 16:44:49 +01004654 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4655 return( PSA_ERROR_INVALID_ARGUMENT );
4656 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004657 {
4658 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004660 }
4661 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4662 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004664 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004665 else
4666 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004667
4668 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004670 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004671}
4672
4673#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004674static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004675 psa_algorithm_t hash_alg,
4676 psa_key_derivation_step_t step,
4677 const uint8_t *data,
4678 size_t data_length )
4679{
4680 psa_status_t status;
4681 switch( step )
4682 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004683 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004684 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004685 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004686 status = psa_hmac_setup_internal( &hkdf->hmac,
4687 data, data_length,
4688 hash_alg );
4689 if( status != PSA_SUCCESS )
4690 return( status );
4691 hkdf->state = HKDF_STATE_STARTED;
4692 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004693 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004694 /* If no salt was provided, use an empty salt. */
4695 if( hkdf->state == HKDF_STATE_INIT )
4696 {
4697 status = psa_hmac_setup_internal( &hkdf->hmac,
4698 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004699 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004700 if( status != PSA_SUCCESS )
4701 return( status );
4702 hkdf->state = HKDF_STATE_STARTED;
4703 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004704 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004705 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004706 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4707 data, data_length );
4708 if( status != PSA_SUCCESS )
4709 return( status );
4710 status = psa_hmac_finish_internal( &hkdf->hmac,
4711 hkdf->prk,
4712 sizeof( hkdf->prk ) );
4713 if( status != PSA_SUCCESS )
4714 return( status );
4715 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4716 hkdf->block_number = 0;
4717 hkdf->state = HKDF_STATE_KEYED;
4718 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004719 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004720 if( hkdf->state == HKDF_STATE_OUTPUT )
4721 return( PSA_ERROR_BAD_STATE );
4722 if( hkdf->info_set )
4723 return( PSA_ERROR_BAD_STATE );
4724 hkdf->info_length = data_length;
4725 if( data_length != 0 )
4726 {
4727 hkdf->info = mbedtls_calloc( 1, data_length );
4728 if( hkdf->info == NULL )
4729 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4730 memcpy( hkdf->info, data, data_length );
4731 }
4732 hkdf->info_set = 1;
4733 return( PSA_SUCCESS );
4734 default:
4735 return( PSA_ERROR_INVALID_ARGUMENT );
4736 }
4737}
4738#endif /* MBEDTLS_MD_C */
4739
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004740static psa_status_t psa_key_derivation_input_raw(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004742 psa_key_derivation_step_t step,
4743 const uint8_t *data,
4744 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004745{
4746 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004748
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 if( kdf_alg == PSA_ALG_SELECT_RAW )
4750 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 if( operation->capacity != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004752 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 operation->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4754 if( operation->ctx.buffer.data == NULL )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004755 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 memcpy( operation->ctx.buffer.data, data, data_length );
4757 operation->ctx.buffer.size = data_length;
4758 operation->capacity = data_length;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004759 status = PSA_SUCCESS;
4760 }
4761 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004762#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004764 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004766 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767 step, data, data_length );
4768 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004769 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004770#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771#if defined(MBEDTLS_MD_C)
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004772 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004773 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004774 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004775 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004776 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004777 status = PSA_ERROR_NOT_SUPPORTED;
4778 }
4779 else
4780#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004781 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004783 return( PSA_ERROR_BAD_STATE );
4784 }
4785
4786 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004788 return( status );
4789}
4790
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791psa_status_t psa_key_derivation_input_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004792 psa_key_derivation_step_t step,
4793 const uint8_t *data,
4794 size_t data_length )
4795{
4796 switch( step )
4797 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004798 case PSA_KEY_DERIVATION_INPUT_LABEL:
4799 case PSA_KEY_DERIVATION_INPUT_SALT:
4800 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine8f2a6dc2019-05-29 17:32:21 +02004801 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 return( psa_key_derivation_input_raw( operation, step,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004803 data, data_length ) );
4804 default:
4805 return( PSA_ERROR_INVALID_ARGUMENT );
4806 }
4807}
4808
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809psa_status_t psa_key_derivation_input_key( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810 psa_key_derivation_step_t step,
4811 psa_key_handle_t handle )
4812{
4813 psa_key_slot_t *slot;
4814 psa_status_t status;
4815 status = psa_get_key_from_slot( handle, &slot,
4816 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004817 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004818 if( status != PSA_SUCCESS )
4819 return( status );
4820 if( slot->type != PSA_KEY_TYPE_DERIVE )
4821 return( PSA_ERROR_INVALID_ARGUMENT );
4822 /* Don't allow a key to be used as an input that is usually public.
4823 * This is debatable. It's ok from a cryptographic perspective to
4824 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004825 * the material should be dedicated to a particular input step,
4826 * otherwise this may allow the key to be used in an unintended way
4827 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02004828 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 return( psa_key_derivation_input_raw( operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004831 step,
4832 slot->data.raw.data,
4833 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004834}
4835
Gilles Peskineea0fb492018-07-12 17:17:20 +02004836
4837
4838/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004839/* Key agreement */
4840/****************************************************************/
4841
Gilles Peskinea05219c2018-11-16 16:02:56 +01004842#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004843static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4844 size_t peer_key_length,
4845 const mbedtls_ecp_keypair *our_key,
4846 uint8_t *shared_secret,
4847 size_t shared_secret_size,
4848 size_t *shared_secret_length )
4849{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004850 mbedtls_ecp_keypair *their_key = NULL;
4851 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004852 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004853 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004854
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004855 status = psa_import_ec_public_key(
4856 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4857 peer_key, peer_key_length,
4858 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004859 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004860 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004861
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004862 status = mbedtls_to_psa_error(
4863 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4864 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004865 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004866 status = mbedtls_to_psa_error(
4867 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4868 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004869 goto exit;
4870
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004871 status = mbedtls_to_psa_error(
4872 mbedtls_ecdh_calc_secret( &ecdh,
4873 shared_secret_length,
4874 shared_secret, shared_secret_size,
4875 mbedtls_ctr_drbg_random,
4876 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004877
4878exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004879 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004880 mbedtls_ecp_keypair_free( their_key );
4881 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004882 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004883}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004884#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004885
Gilles Peskine01d718c2018-09-18 12:01:02 +02004886#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4887
Gilles Peskine0216fe12019-04-11 21:23:21 +02004888static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4889 psa_key_slot_t *private_key,
4890 const uint8_t *peer_key,
4891 size_t peer_key_length,
4892 uint8_t *shared_secret,
4893 size_t shared_secret_size,
4894 size_t *shared_secret_length )
4895{
4896 switch( alg )
4897 {
4898#if defined(MBEDTLS_ECDH_C)
4899 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004900 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004901 return( PSA_ERROR_INVALID_ARGUMENT );
4902 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4903 private_key->data.ecp,
4904 shared_secret, shared_secret_size,
4905 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004906#endif /* MBEDTLS_ECDH_C */
4907 default:
4908 (void) private_key;
4909 (void) peer_key;
4910 (void) peer_key_length;
4911 (void) shared_secret;
4912 (void) shared_secret_size;
4913 (void) shared_secret_length;
4914 return( PSA_ERROR_NOT_SUPPORTED );
4915 }
4916}
4917
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004918/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004919 * to potentially free embedded data structures and wipe confidential data.
4920 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004923 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004924 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004926{
4927 psa_status_t status;
4928 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4929 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004931
4932 /* Step 1: run the secret agreement algorithm to generate the shared
4933 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004934 status = psa_key_agreement_raw_internal( ka_alg,
4935 private_key,
4936 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004937 shared_secret,
4938 sizeof( shared_secret ),
4939 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004940 if( status != PSA_SUCCESS )
4941 goto exit;
4942
4943 /* Step 2: set up the key derivation to generate key material from
4944 * the shared secret. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 status = psa_key_derivation_input_raw( operation, step,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004946 shared_secret, shared_secret_length );
4947
Gilles Peskine01d718c2018-09-18 12:01:02 +02004948exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004949 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004950 return( status );
4951}
4952
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004954 psa_key_derivation_step_t step,
4955 psa_key_handle_t private_key,
4956 const uint8_t *peer_key,
4957 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004958{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004959 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004960 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004962 return( PSA_ERROR_INVALID_ARGUMENT );
4963 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004965 if( status != PSA_SUCCESS )
4966 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004968 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004969 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004970 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01004972 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004973}
4974
Gilles Peskinebe697d82019-05-16 18:00:41 +02004975psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
4976 psa_key_handle_t private_key,
4977 const uint8_t *peer_key,
4978 size_t peer_key_length,
4979 uint8_t *output,
4980 size_t output_size,
4981 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004982{
4983 psa_key_slot_t *slot;
4984 psa_status_t status;
4985
4986 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4987 {
4988 status = PSA_ERROR_INVALID_ARGUMENT;
4989 goto exit;
4990 }
4991 status = psa_get_key_from_slot( private_key, &slot,
4992 PSA_KEY_USAGE_DERIVE, alg );
4993 if( status != PSA_SUCCESS )
4994 goto exit;
4995
4996 status = psa_key_agreement_raw_internal( alg, slot,
4997 peer_key, peer_key_length,
4998 output, output_size,
4999 output_length );
5000
5001exit:
5002 if( status != PSA_SUCCESS )
5003 {
5004 /* If an error happens and is not handled properly, the output
5005 * may be used as a key to protect sensitive data. Arrange for such
5006 * a key to be random, which is likely to result in decryption or
5007 * verification errors. This is better than filling the buffer with
5008 * some constant data such as zeros, which would result in the data
5009 * being protected with a reproducible, easily knowable key.
5010 */
5011 psa_generate_random( output, output_size );
5012 *output_length = output_size;
5013 }
5014 return( status );
5015}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005016
5017
5018/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005019/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005020/****************************************************************/
5021
5022psa_status_t psa_generate_random( uint8_t *output,
5023 size_t output_size )
5024{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005025 int ret;
5026 GUARD_MODULE_INITIALIZED;
5027
5028 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005029 return( mbedtls_to_psa_error( ret ) );
5030}
5031
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005032#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5033#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005034
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005035psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5036 size_t seed_size )
5037{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005038 if( global_data.initialized )
5039 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005040
5041 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5042 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5043 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5044 return( PSA_ERROR_INVALID_ARGUMENT );
5045
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005046 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005047}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005048#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005049
Gilles Peskinee56e8782019-04-26 17:34:02 +02005050#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5051static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5052 size_t domain_parameters_size,
5053 int *exponent )
5054{
5055 size_t i;
5056 uint32_t acc = 0;
5057
5058 if( domain_parameters_size == 0 )
5059 {
5060 *exponent = 65537;
5061 return( PSA_SUCCESS );
5062 }
5063
5064 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5065 * support values that fit in a 32-bit integer, which is larger than
5066 * int on just about every platform anyway. */
5067 if( domain_parameters_size > sizeof( acc ) )
5068 return( PSA_ERROR_NOT_SUPPORTED );
5069 for( i = 0; i < domain_parameters_size; i++ )
5070 acc = ( acc << 8 ) | domain_parameters[i];
5071 if( acc > INT_MAX )
5072 return( PSA_ERROR_NOT_SUPPORTED );
5073 *exponent = acc;
5074 return( PSA_SUCCESS );
5075}
5076#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5077
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005078static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005079 psa_key_slot_t *slot, size_t bits,
5080 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005081{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005082 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005083
Gilles Peskinee56e8782019-04-26 17:34:02 +02005084 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005085 return( PSA_ERROR_INVALID_ARGUMENT );
5086
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005087 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005088 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005089 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005090 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005091 if( status != PSA_SUCCESS )
5092 return( status );
5093 status = psa_generate_random( slot->data.raw.data,
5094 slot->data.raw.bytes );
5095 if( status != PSA_SUCCESS )
5096 {
5097 mbedtls_free( slot->data.raw.data );
5098 return( status );
5099 }
5100#if defined(MBEDTLS_DES_C)
5101 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005102 psa_des_set_key_parity( slot->data.raw.data,
5103 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005104#endif /* MBEDTLS_DES_C */
5105 }
5106 else
5107
5108#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005109 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005110 {
5111 mbedtls_rsa_context *rsa;
5112 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005113 int exponent;
5114 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005115 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5116 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005117 /* Accept only byte-aligned keys, for the same reasons as
5118 * in psa_import_rsa_key(). */
5119 if( bits % 8 != 0 )
5120 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005121 status = psa_read_rsa_exponent( domain_parameters,
5122 domain_parameters_size,
5123 &exponent );
5124 if( status != PSA_SUCCESS )
5125 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005126 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5127 if( rsa == NULL )
5128 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5129 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5130 ret = mbedtls_rsa_gen_key( rsa,
5131 mbedtls_ctr_drbg_random,
5132 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005133 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005134 exponent );
5135 if( ret != 0 )
5136 {
5137 mbedtls_rsa_free( rsa );
5138 mbedtls_free( rsa );
5139 return( mbedtls_to_psa_error( ret ) );
5140 }
5141 slot->data.rsa = rsa;
5142 }
5143 else
5144#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5145
5146#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005147 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005148 {
5149 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5150 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5151 const mbedtls_ecp_curve_info *curve_info =
5152 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5153 mbedtls_ecp_keypair *ecp;
5154 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005155 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005156 return( PSA_ERROR_NOT_SUPPORTED );
5157 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5158 return( PSA_ERROR_NOT_SUPPORTED );
5159 if( curve_info->bit_size != bits )
5160 return( PSA_ERROR_INVALID_ARGUMENT );
5161 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5162 if( ecp == NULL )
5163 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5164 mbedtls_ecp_keypair_init( ecp );
5165 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5166 mbedtls_ctr_drbg_random,
5167 &global_data.ctr_drbg );
5168 if( ret != 0 )
5169 {
5170 mbedtls_ecp_keypair_free( ecp );
5171 mbedtls_free( ecp );
5172 return( mbedtls_to_psa_error( ret ) );
5173 }
5174 slot->data.ecp = ecp;
5175 }
5176 else
5177#endif /* MBEDTLS_ECP_C */
5178
5179 return( PSA_ERROR_NOT_SUPPORTED );
5180
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005181 return( PSA_SUCCESS );
5182}
5183
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005184psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005185 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005186{
5187 psa_status_t status;
5188 psa_key_slot_t *slot = NULL;
5189 status = psa_start_key_creation( attributes, handle, &slot );
5190 if( status == PSA_SUCCESS )
5191 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005192 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005193 slot, attributes->bits,
5194 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005195 }
5196 if( status == PSA_SUCCESS )
5197 status = psa_finish_key_creation( slot );
5198 if( status != PSA_SUCCESS )
5199 {
5200 psa_fail_key_creation( slot );
5201 *handle = 0;
5202 }
5203 return( status );
5204}
5205
5206
Gilles Peskine05d69892018-06-19 22:00:52 +02005207
5208/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005209/* Module setup */
5210/****************************************************************/
5211
Gilles Peskine5e769522018-11-20 21:59:56 +01005212psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5213 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5214 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5215{
5216 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5217 return( PSA_ERROR_BAD_STATE );
5218 global_data.entropy_init = entropy_init;
5219 global_data.entropy_free = entropy_free;
5220 return( PSA_SUCCESS );
5221}
5222
Gilles Peskinee59236f2018-01-27 23:32:46 +01005223void mbedtls_psa_crypto_free( void )
5224{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005225 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005226 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5227 {
5228 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005229 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005230 }
5231 /* Wipe all remaining data, including configuration.
5232 * In particular, this sets all state indicator to the value
5233 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005234 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005235}
5236
5237psa_status_t psa_crypto_init( void )
5238{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005239 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005240 const unsigned char drbg_seed[] = "PSA";
5241
Gilles Peskinec6b69072018-11-20 21:42:52 +01005242 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005243 if( global_data.initialized != 0 )
5244 return( PSA_SUCCESS );
5245
Gilles Peskine5e769522018-11-20 21:59:56 +01005246 /* Set default configuration if
5247 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5248 if( global_data.entropy_init == NULL )
5249 global_data.entropy_init = mbedtls_entropy_init;
5250 if( global_data.entropy_free == NULL )
5251 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005252
Gilles Peskinec6b69072018-11-20 21:42:52 +01005253 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005254 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005255 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005256 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005257 status = mbedtls_to_psa_error(
5258 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5259 mbedtls_entropy_func,
5260 &global_data.entropy,
5261 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5262 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005263 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005264 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005265
Gilles Peskine66fb1262018-12-10 16:29:04 +01005266 status = psa_initialize_key_slots( );
5267 if( status != PSA_SUCCESS )
5268 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005269
5270 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005271 global_data.initialized = 1;
5272
Gilles Peskinee59236f2018-01-27 23:32:46 +01005273exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005274 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005275 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005276 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005277}
5278
5279#endif /* MBEDTLS_PSA_CRYPTO_C */