blob: 7794e7a4986d353f14999cb2582530776e157846 [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:
205 return( PSA_ERROR_TAMPERING_DETECTED );
206 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:
314 return( PSA_ERROR_TAMPERING_DETECTED );
315 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. */
538 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
539 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{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000591 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
592 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{
640 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
641 mbedtls_ecp_keypair *ecp = NULL;
642 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
643
644 *p_ecp = NULL;
645 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
646 if( ecp == NULL )
647 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000648 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100649
650 /* Load the group. */
651 status = mbedtls_to_psa_error(
652 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
653 if( status != PSA_SUCCESS )
654 goto exit;
655 /* Load the secret value. */
656 status = mbedtls_to_psa_error(
657 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
658 if( status != PSA_SUCCESS )
659 goto exit;
660 /* Validate the private key. */
661 status = mbedtls_to_psa_error(
662 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
663 if( status != PSA_SUCCESS )
664 goto exit;
665 /* Calculate the public key from the private key. */
666 status = mbedtls_to_psa_error(
667 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
668 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
669 if( status != PSA_SUCCESS )
670 goto exit;
671
672 *p_ecp = ecp;
673 return( PSA_SUCCESS );
674
675exit:
676 if( ecp != NULL )
677 {
678 mbedtls_ecp_keypair_free( ecp );
679 mbedtls_free( ecp );
680 }
681 return( status );
682}
683#endif /* defined(MBEDTLS_ECP_C) */
684
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100685/** Import key data into a slot. `slot->type` must have been set
686 * previously. This function assumes that the slot does not contain
687 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100688psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
689 const uint8_t *data,
690 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200692 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
Darryl Green940d72c2018-07-13 13:18:51 +0100694 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100696 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697 if( data_length > SIZE_MAX / 8 )
698 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100699 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200700 PSA_BYTES_TO_BITS( data_length ),
701 &slot->data.raw );
702 if( status != PSA_SUCCESS )
703 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200704 if( data_length != 0 )
705 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 }
707 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100708#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100709 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100710 {
Darryl Green940d72c2018-07-13 13:18:51 +0100711 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100712 data, data_length,
713 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100714 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000715 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
716 {
717 status = psa_import_ec_public_key(
718 PSA_KEY_TYPE_GET_CURVE( slot->type ),
719 data, data_length,
720 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000721 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100722 else
723#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000724#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
725 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000727 status = psa_import_rsa_key( slot->type,
728 data, data_length,
729 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730 }
731 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000732#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733 {
734 return( PSA_ERROR_NOT_SUPPORTED );
735 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000736 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100737}
738
Darryl Green06fd18d2018-07-16 11:21:11 +0100739/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000740 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100741static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100742 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100743{
744 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100745 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100746
747 *p_slot = NULL;
748
Gilles Peskinec5487a82018-12-03 18:08:14 +0100749 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100750 if( status != PSA_SUCCESS )
751 return( status );
752
753 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200754 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100755
756 *p_slot = slot;
757 return( status );
758}
759
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760/** Calculate the intersection of two algorithm usage policies.
761 *
762 * Return 0 (which allows no operation) on incompatibility.
763 */
764static psa_algorithm_t psa_key_policy_algorithm_intersection(
765 psa_algorithm_t alg1,
766 psa_algorithm_t alg2 )
767{
768 /* Common case: the policy only allows alg. */
769 if( alg1 == alg2 )
770 return( alg1 );
771 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100772 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100773 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
774 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
775 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
776 {
777 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
778 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100779 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100780 return( alg1 );
781 }
782 /* If the policies are incompatible, allow nothing. */
783 return( 0 );
784}
785
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100786/** Test whether a policy permits an algorithm.
787 *
788 * The caller must test usage flags separately.
789 */
790static int psa_key_policy_permits( const psa_key_policy_t *policy,
791 psa_algorithm_t alg )
792{
793 /* Common case: the policy only allows alg. */
794 if( alg == policy->alg )
795 return( 1 );
796 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
797 * and alg is the same hash-and-sign family with any hash,
798 * then alg is compliant with policy->alg. */
799 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
800 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
801 {
802 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
803 ( alg & ~PSA_ALG_HASH_MASK ) );
804 }
805 /* If it isn't permitted, it's forbidden. */
806 return( 0 );
807}
808
Gilles Peskinef603c712019-01-19 13:40:11 +0100809/** Restrict a key policy based on a constraint.
810 *
811 * \param[in,out] policy The policy to restrict.
812 * \param[in] constraint The policy constraint to apply.
813 *
814 * \retval #PSA_SUCCESS
815 * \c *policy contains the intersection of the original value of
816 * \c *policy and \c *constraint.
817 * \retval #PSA_ERROR_INVALID_ARGUMENT
818 * \c *policy and \c *constraint are incompatible.
819 * \c *policy is unchanged.
820 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100821static psa_status_t psa_restrict_key_policy(
822 psa_key_policy_t *policy,
823 const psa_key_policy_t *constraint )
824{
825 psa_algorithm_t intersection_alg =
826 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
827 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
828 return( PSA_ERROR_INVALID_ARGUMENT );
829 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100830 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100831 return( PSA_SUCCESS );
832}
833
Darryl Green06fd18d2018-07-16 11:21:11 +0100834/** Retrieve a slot which must contain a key. The key must have allow all the
835 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
836 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100837static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100838 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100839 psa_key_usage_t usage,
840 psa_algorithm_t alg )
841{
842 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100843 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100844
845 *p_slot = NULL;
846
Gilles Peskinec5487a82018-12-03 18:08:14 +0100847 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100848 if( status != PSA_SUCCESS )
849 return( status );
850 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200851 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852
853 /* Enforce that usage policy for the key slot contains all the flags
854 * required by the usage parameter. There is one exception: public
855 * keys can always be exported, so we treat public key objects as
856 * if they had the export flag. */
857 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
858 usage &= ~PSA_KEY_USAGE_EXPORT;
859 if( ( slot->policy.usage & usage ) != usage )
860 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861
862 /* Enforce that the usage policy permits the requested algortihm. */
863 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100864 return( PSA_ERROR_NOT_PERMITTED );
865
866 *p_slot = slot;
867 return( PSA_SUCCESS );
868}
Darryl Green940d72c2018-07-13 13:18:51 +0100869
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100870/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100871static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000872{
873 if( slot->type == PSA_KEY_TYPE_NONE )
874 {
875 /* No key material to clean. */
876 }
877 else if( key_type_is_raw_bytes( slot->type ) )
878 {
879 mbedtls_free( slot->data.raw.data );
880 }
881 else
882#if defined(MBEDTLS_RSA_C)
883 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
884 {
885 mbedtls_rsa_free( slot->data.rsa );
886 mbedtls_free( slot->data.rsa );
887 }
888 else
889#endif /* defined(MBEDTLS_RSA_C) */
890#if defined(MBEDTLS_ECP_C)
891 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
892 {
893 mbedtls_ecp_keypair_free( slot->data.ecp );
894 mbedtls_free( slot->data.ecp );
895 }
896 else
897#endif /* defined(MBEDTLS_ECP_C) */
898 {
899 /* Shouldn't happen: the key type is not any type that we
900 * put in. */
901 return( PSA_ERROR_TAMPERING_DETECTED );
902 }
903
904 return( PSA_SUCCESS );
905}
906
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100907static void psa_abort_operations_using_key( psa_key_slot_t *slot )
908{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200909 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100910 (void) slot;
911}
912
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100913/** Completely wipe a slot in memory, including its policy.
914 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100915psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100916{
917 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100918 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100919 /* At this point, key material and other type-specific content has
920 * been wiped. Clear remaining metadata. We can call memset and not
921 * zeroize because the metadata is not particularly sensitive. */
922 memset( slot, 0, sizeof( *slot ) );
923 return( status );
924}
925
Gilles Peskine87a5e562019-04-17 12:28:25 +0200926psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100927 psa_key_type_t type,
928 const uint8_t *data,
929 size_t data_length )
930{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100931 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100932 psa_status_t status;
933
Gilles Peskinec5487a82018-12-03 18:08:14 +0100934 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100935 if( status != PSA_SUCCESS )
936 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937
938 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100939
940 status = psa_import_key_into_slot( slot, data, data_length );
941 if( status != PSA_SUCCESS )
942 {
943 slot->type = PSA_KEY_TYPE_NONE;
944 return( status );
945 }
946
Darryl Greend49a4992018-06-18 17:27:26 +0100947#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
948 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
949 {
950 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100951 status = psa_save_persistent_key( slot->persistent_storage_id,
952 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100953 data_length );
954 if( status != PSA_SUCCESS )
955 {
956 (void) psa_remove_key_data_from_memory( slot );
957 slot->type = PSA_KEY_TYPE_NONE;
958 }
959 }
960#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
961
962 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100963}
964
Gilles Peskinec5487a82018-12-03 18:08:14 +0100965psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100967 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100968 psa_status_t status = PSA_SUCCESS;
969 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100970
Gilles Peskinec5487a82018-12-03 18:08:14 +0100971 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200972 if( status != PSA_SUCCESS )
973 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100974#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
975 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
976 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100977 storage_status =
978 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100979 }
980#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100981 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100982 if( status != PSA_SUCCESS )
983 return( status );
984 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985}
986
Gilles Peskineb870b182018-07-06 16:02:09 +0200987/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200988static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200989{
990 if( key_type_is_raw_bytes( slot->type ) )
991 return( slot->data.raw.bytes * 8 );
992#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200993 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100994 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200995#endif /* defined(MBEDTLS_RSA_C) */
996#if defined(MBEDTLS_ECP_C)
997 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
998 return( slot->data.ecp->grp.pbits );
999#endif /* defined(MBEDTLS_ECP_C) */
1000 /* Shouldn't happen except on an empty slot. */
1001 return( 0 );
1002}
1003
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001004void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1005{
Gilles Peskineb699f072019-04-26 16:06:02 +02001006 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001007 memset( attributes, 0, sizeof( *attributes ) );
1008}
1009
Gilles Peskineb699f072019-04-26 16:06:02 +02001010psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1011 psa_key_type_t type,
1012 const uint8_t *data,
1013 size_t data_length )
1014{
1015 uint8_t *copy = NULL;
1016
1017 if( data_length != 0 )
1018 {
1019 copy = mbedtls_calloc( 1, data_length );
1020 if( copy == NULL )
1021 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1022 memcpy( copy, data, data_length );
1023 }
1024 /* After this point, this function is guaranteed to succeed, so it
1025 * can start modifying `*attributes`. */
1026
1027 if( attributes->domain_parameters != NULL )
1028 {
1029 mbedtls_free( attributes->domain_parameters );
1030 attributes->domain_parameters = NULL;
1031 attributes->domain_parameters_size = 0;
1032 }
1033
1034 attributes->domain_parameters = copy;
1035 attributes->domain_parameters_size = data_length;
1036 attributes->type = type;
1037 return( PSA_SUCCESS );
1038}
1039
1040psa_status_t psa_get_key_domain_parameters(
1041 const psa_key_attributes_t *attributes,
1042 uint8_t *data, size_t data_size, size_t *data_length )
1043{
1044 if( attributes->domain_parameters_size > data_size )
1045 return( PSA_ERROR_BUFFER_TOO_SMALL );
1046 *data_length = attributes->domain_parameters_size;
1047 if( attributes->domain_parameters_size != 0 )
1048 memcpy( data, attributes->domain_parameters,
1049 attributes->domain_parameters_size );
1050 return( PSA_SUCCESS );
1051}
1052
1053#if defined(MBEDTLS_RSA_C)
1054static psa_status_t psa_get_rsa_public_exponent(
1055 const mbedtls_rsa_context *rsa,
1056 psa_key_attributes_t *attributes )
1057{
1058 mbedtls_mpi mpi;
1059 int ret;
1060 uint8_t *buffer = NULL;
1061 size_t buflen;
1062 mbedtls_mpi_init( &mpi );
1063
1064 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1065 if( ret != 0 )
1066 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001067 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1068 {
1069 /* It's the default value, which is reported as an empty string,
1070 * so there's nothing to do. */
1071 goto exit;
1072 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001073
1074 buflen = mbedtls_mpi_size( &mpi );
1075 buffer = mbedtls_calloc( 1, buflen );
1076 if( buffer == NULL )
1077 {
1078 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1079 goto exit;
1080 }
1081 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1082 if( ret != 0 )
1083 goto exit;
1084 attributes->domain_parameters = buffer;
1085 attributes->domain_parameters_size = buflen;
1086
1087exit:
1088 mbedtls_mpi_free( &mpi );
1089 if( ret != 0 )
1090 mbedtls_free( buffer );
1091 return( mbedtls_to_psa_error( ret ) );
1092}
1093#endif /* MBEDTLS_RSA_C */
1094
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001095psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1096 psa_key_attributes_t *attributes )
1097{
1098 psa_key_slot_t *slot;
1099 psa_status_t status;
1100
1101 psa_reset_key_attributes( attributes );
1102
1103 status = psa_get_key_slot( handle, &slot );
1104 if( status != PSA_SUCCESS )
1105 return( status );
1106
1107 attributes->id = slot->persistent_storage_id;
1108 attributes->lifetime = slot->lifetime;
1109 attributes->policy = slot->policy;
1110 attributes->type = slot->type;
1111 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001112
1113 switch( slot->type )
1114 {
1115#if defined(MBEDTLS_RSA_C)
1116 case PSA_KEY_TYPE_RSA_KEYPAIR:
1117 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1118 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1119 break;
1120#endif
1121 default:
1122 /* Nothing else to do. */
1123 break;
1124 }
1125
1126 if( status != PSA_SUCCESS )
1127 psa_reset_key_attributes( attributes );
1128 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001129}
1130
Gilles Peskinec5487a82018-12-03 18:08:14 +01001131psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001132 psa_key_type_t *type,
1133 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001134{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001135 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001136 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001139 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140 if( bits != NULL )
1141 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001142 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001143 if( status != PSA_SUCCESS )
1144 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001145
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001146 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001147 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001148 if( type != NULL )
1149 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001150 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001151 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152 return( PSA_SUCCESS );
1153}
1154
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001155#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001156static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1157 unsigned char *buf, size_t size )
1158{
1159 int ret;
1160 unsigned char *c;
1161 size_t len = 0;
1162
1163 c = buf + size;
1164
1165 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1166
1167 return( (int) len );
1168}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001169#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001170
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001171static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1172 uint8_t *data,
1173 size_t data_size,
1174 size_t *data_length,
1175 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001176{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001177 *data_length = 0;
1178
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001179 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001180 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001181
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001182 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001183 {
1184 if( slot->data.raw.bytes > data_size )
1185 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001186 if( data_size != 0 )
1187 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001188 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001189 memset( data + slot->data.raw.bytes, 0,
1190 data_size - slot->data.raw.bytes );
1191 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192 *data_length = slot->data.raw.bytes;
1193 return( PSA_SUCCESS );
1194 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001195#if defined(MBEDTLS_ECP_C)
1196 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1197 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001198 psa_status_t status;
1199
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001200 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001201 if( bytes > data_size )
1202 return( PSA_ERROR_BUFFER_TOO_SMALL );
1203 status = mbedtls_to_psa_error(
1204 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1205 if( status != PSA_SUCCESS )
1206 return( status );
1207 memset( data + bytes, 0, data_size - bytes );
1208 *data_length = bytes;
1209 return( PSA_SUCCESS );
1210 }
1211#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212 else
Moran Peker17e36e12018-05-02 12:55:20 +03001213 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001214#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001215 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001216 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001217 {
Moran Pekera998bc62018-04-16 18:16:20 +03001218 mbedtls_pk_context pk;
1219 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001220 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001221 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001222#if defined(MBEDTLS_RSA_C)
1223 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001224 pk.pk_info = &mbedtls_rsa_info;
1225 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001226#else
1227 return( PSA_ERROR_NOT_SUPPORTED );
1228#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001229 }
1230 else
1231 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001232#if defined(MBEDTLS_ECP_C)
1233 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001234 pk.pk_info = &mbedtls_eckey_info;
1235 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001236#else
1237 return( PSA_ERROR_NOT_SUPPORTED );
1238#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001239 }
Moran Pekerd7326592018-05-29 16:56:39 +03001240 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001241 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001242 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001243 }
Moran Peker17e36e12018-05-02 12:55:20 +03001244 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001245 {
Moran Peker17e36e12018-05-02 12:55:20 +03001246 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001247 }
Moran Peker60364322018-04-29 11:34:58 +03001248 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001249 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001250 /* If data_size is 0 then data may be NULL and then the
1251 * call to memset would have undefined behavior. */
1252 if( data_size != 0 )
1253 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001254 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001255 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001256 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1257 * Move the data to the beginning and erase remaining data
1258 * at the original location. */
1259 if( 2 * (size_t) ret <= data_size )
1260 {
1261 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001262 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001263 }
1264 else if( (size_t) ret < data_size )
1265 {
1266 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001267 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001268 }
Moran Pekera998bc62018-04-16 18:16:20 +03001269 *data_length = ret;
1270 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001271 }
1272 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001273#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001274 {
1275 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001276 it is valid for a special-purpose implementation to omit
1277 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001278 return( PSA_ERROR_NOT_SUPPORTED );
1279 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280 }
1281}
1282
Gilles Peskinec5487a82018-12-03 18:08:14 +01001283psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001284 uint8_t *data,
1285 size_t data_size,
1286 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001287{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001288 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001289 psa_status_t status;
1290
1291 /* Set the key to empty now, so that even when there are errors, we always
1292 * set data_length to a value between 0 and data_size. On error, setting
1293 * the key to empty is a good choice because an empty key representation is
1294 * unlikely to be accepted anywhere. */
1295 *data_length = 0;
1296
1297 /* Export requires the EXPORT flag. There is an exception for public keys,
1298 * which don't require any flag, but psa_get_key_from_slot takes
1299 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001300 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001301 if( status != PSA_SUCCESS )
1302 return( status );
1303 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001304 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskinec5487a82018-12-03 18:08:14 +01001307psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001308 uint8_t *data,
1309 size_t data_size,
1310 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001311{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001312 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001313 psa_status_t status;
1314
1315 /* Set the key to empty now, so that even when there are errors, we always
1316 * set data_length to a value between 0 and data_size. On error, setting
1317 * the key to empty is a good choice because an empty key representation is
1318 * unlikely to be accepted anywhere. */
1319 *data_length = 0;
1320
1321 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001322 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001323 if( status != PSA_SUCCESS )
1324 return( status );
1325 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001326 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001327}
1328
Darryl Green0c6575a2018-11-07 16:05:30 +00001329#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001330static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001331 size_t bits )
1332{
1333 psa_status_t status;
1334 uint8_t *data;
1335 size_t key_length;
1336 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1337 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001338 if( data == NULL )
1339 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001340 /* Get key data in export format */
1341 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1342 if( status != PSA_SUCCESS )
1343 {
1344 slot->type = PSA_KEY_TYPE_NONE;
1345 goto exit;
1346 }
1347 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001348 status = psa_save_persistent_key( slot->persistent_storage_id,
1349 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001350 data, key_length );
1351 if( status != PSA_SUCCESS )
1352 {
1353 slot->type = PSA_KEY_TYPE_NONE;
1354 }
1355exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001356 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001357 mbedtls_free( data );
1358 return( status );
1359}
1360#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1361
Gilles Peskine4747d192019-04-17 15:05:45 +02001362static psa_status_t psa_set_key_policy_internal(
1363 psa_key_slot_t *slot,
1364 const psa_key_policy_t *policy )
1365{
1366 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1367 PSA_KEY_USAGE_ENCRYPT |
1368 PSA_KEY_USAGE_DECRYPT |
1369 PSA_KEY_USAGE_SIGN |
1370 PSA_KEY_USAGE_VERIFY |
1371 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1372 return( PSA_ERROR_INVALID_ARGUMENT );
1373
1374 slot->policy = *policy;
1375 return( PSA_SUCCESS );
1376}
1377
1378/** Prepare a key slot to receive key material.
1379 *
1380 * This function allocates a key slot and sets its metadata.
1381 *
1382 * If this function fails, call psa_fail_key_creation().
1383 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001384 * This function is intended to be used as follows:
1385 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1386 * it with the specified attributes, and assign it a handle.
1387 * -# Populate the slot with the key material.
1388 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1389 * In case of failure at any step, stop the sequence and call
1390 * psa_fail_key_creation().
1391 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001392 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001393 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001394 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001395 *
1396 * \retval #PSA_SUCCESS
1397 * The key slot is ready to receive key material.
1398 * \return If this function fails, the key slot is an invalid state.
1399 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001400 */
1401static psa_status_t psa_start_key_creation(
1402 const psa_key_attributes_t *attributes,
1403 psa_key_handle_t *handle,
1404 psa_key_slot_t **p_slot )
1405{
1406 psa_status_t status;
1407 psa_key_slot_t *slot;
1408
1409 status = psa_allocate_key( handle );
1410 if( status != PSA_SUCCESS )
1411 return( status );
1412 status = psa_get_key_slot( *handle, p_slot );
1413 if( status != PSA_SUCCESS )
1414 return( status );
1415 slot = *p_slot;
1416
1417 status = psa_set_key_policy_internal( slot, &attributes->policy );
1418 if( status != PSA_SUCCESS )
1419 return( status );
1420 slot->lifetime = attributes->lifetime;
1421 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001422 {
1423 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1424 attributes->id );
1425 if( status != PSA_SUCCESS )
1426 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001427 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001428 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001429 slot->type = attributes->type;
1430
1431 return( status );
1432}
1433
1434/** Finalize the creation of a key once its key material has been set.
1435 *
1436 * This entails writing the key to persistent storage.
1437 *
1438 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001439 * See the documentation of psa_start_key_creation() for the intended use
1440 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001441 *
1442 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001443 *
1444 * \retval #PSA_SUCCESS
1445 * The key was successfully created. The handle is now valid.
1446 * \return If this function fails, the key slot is an invalid state.
1447 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001448 */
1449static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1450{
1451 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001452 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001453
1454#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1455 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1456 {
1457 uint8_t *buffer = NULL;
1458 size_t buffer_size = 0;
1459 size_t length;
1460
1461 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001462 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 buffer = mbedtls_calloc( 1, buffer_size );
1464 if( buffer == NULL && buffer_size != 0 )
1465 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1466 status = psa_internal_export_key( slot,
1467 buffer, buffer_size, &length,
1468 0 );
1469
1470 if( status == PSA_SUCCESS )
1471 {
1472 status = psa_save_persistent_key( slot->persistent_storage_id,
1473 slot->type, &slot->policy,
1474 buffer, length );
1475 }
1476
1477 if( buffer_size != 0 )
1478 mbedtls_platform_zeroize( buffer, buffer_size );
1479 mbedtls_free( buffer );
1480 }
1481#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1482
1483 return( status );
1484}
1485
1486/** Abort the creation of a key.
1487 *
1488 * You may call this function after calling psa_start_key_creation(),
1489 * or after psa_finish_key_creation() fails. In other circumstances, this
1490 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001491 * See the documentation of psa_start_key_creation() for the intended use
1492 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001493 *
1494 * \param slot Pointer to the slot with key material.
1495 */
1496static void psa_fail_key_creation( psa_key_slot_t *slot )
1497{
1498 if( slot == NULL )
1499 return;
1500 psa_wipe_key_slot( slot );
1501}
1502
1503psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1504 psa_key_handle_t *handle,
1505 const uint8_t *data,
1506 size_t data_length )
1507{
1508 psa_status_t status;
1509 psa_key_slot_t *slot = NULL;
1510 status = psa_start_key_creation( attributes, handle, &slot );
1511 if( status == PSA_SUCCESS )
1512 {
1513 status = psa_import_key_into_slot( slot, data, data_length );
1514 }
1515 if( status == PSA_SUCCESS )
1516 status = psa_finish_key_creation( slot );
1517 if( status != PSA_SUCCESS )
1518 {
1519 psa_fail_key_creation( slot );
1520 *handle = 0;
1521 }
1522 return( status );
1523}
1524
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001525static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001526 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001527{
1528 psa_status_t status;
1529 uint8_t *buffer = NULL;
1530 size_t buffer_size = 0;
1531 size_t length;
1532
1533 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001534 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001535 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001536 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001537 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001538 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1539 if( status != PSA_SUCCESS )
1540 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001541 target->type = source->type;
1542 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001543
1544exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001545 if( buffer_size != 0 )
1546 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001547 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001548 return( status );
1549}
1550
Gilles Peskine87a5e562019-04-17 12:28:25 +02001551psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001552 psa_key_handle_t target_handle,
1553 const psa_key_policy_t *constraint)
1554{
1555 psa_key_slot_t *source_slot = NULL;
1556 psa_key_slot_t *target_slot = NULL;
1557 psa_key_policy_t new_policy;
1558 psa_status_t status;
1559 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1560 if( status != PSA_SUCCESS )
1561 return( status );
1562 status = psa_get_empty_key_slot( target_handle, &target_slot );
1563 if( status != PSA_SUCCESS )
1564 return( status );
1565
1566 new_policy = target_slot->policy;
1567 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1568 if( status != PSA_SUCCESS )
1569 return( status );
1570 if( constraint != NULL )
1571 {
1572 status = psa_restrict_key_policy( &new_policy, constraint );
1573 if( status != PSA_SUCCESS )
1574 return( status );
1575 }
1576
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001577 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001578 if( status != PSA_SUCCESS )
1579 return( status );
1580
1581 target_slot->policy = new_policy;
1582 return( PSA_SUCCESS );
1583}
1584
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001585psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1586 const psa_key_attributes_t *specified_attributes,
1587 psa_key_handle_t *target_handle )
1588{
1589 psa_status_t status;
1590 psa_key_slot_t *source_slot = NULL;
1591 psa_key_slot_t *target_slot = NULL;
1592 psa_key_attributes_t actual_attributes = *specified_attributes;
1593
1594 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1595 if( status != PSA_SUCCESS )
1596 goto exit;
1597
1598 status = psa_restrict_key_policy( &actual_attributes.policy,
1599 &source_slot->policy );
1600 if( status != PSA_SUCCESS )
1601 goto exit;
1602
1603 status = psa_start_key_creation( &actual_attributes,
1604 target_handle, &target_slot );
1605 if( status != PSA_SUCCESS )
1606 goto exit;
1607
1608 status = psa_copy_key_material( source_slot, target_slot );
1609
1610exit:
1611 if( status == PSA_SUCCESS )
1612 status = psa_finish_key_creation( target_slot );
1613 if( status != PSA_SUCCESS )
1614 {
1615 psa_fail_key_creation( target_slot );
1616 *target_handle = 0;
1617 }
1618 return( status );
1619}
1620
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001621
1622
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001624/* Message digests */
1625/****************************************************************/
1626
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001627static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001628{
1629 switch( alg )
1630 {
1631#if defined(MBEDTLS_MD2_C)
1632 case PSA_ALG_MD2:
1633 return( &mbedtls_md2_info );
1634#endif
1635#if defined(MBEDTLS_MD4_C)
1636 case PSA_ALG_MD4:
1637 return( &mbedtls_md4_info );
1638#endif
1639#if defined(MBEDTLS_MD5_C)
1640 case PSA_ALG_MD5:
1641 return( &mbedtls_md5_info );
1642#endif
1643#if defined(MBEDTLS_RIPEMD160_C)
1644 case PSA_ALG_RIPEMD160:
1645 return( &mbedtls_ripemd160_info );
1646#endif
1647#if defined(MBEDTLS_SHA1_C)
1648 case PSA_ALG_SHA_1:
1649 return( &mbedtls_sha1_info );
1650#endif
1651#if defined(MBEDTLS_SHA256_C)
1652 case PSA_ALG_SHA_224:
1653 return( &mbedtls_sha224_info );
1654 case PSA_ALG_SHA_256:
1655 return( &mbedtls_sha256_info );
1656#endif
1657#if defined(MBEDTLS_SHA512_C)
1658 case PSA_ALG_SHA_384:
1659 return( &mbedtls_sha384_info );
1660 case PSA_ALG_SHA_512:
1661 return( &mbedtls_sha512_info );
1662#endif
1663 default:
1664 return( NULL );
1665 }
1666}
1667
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001668psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1669{
1670 switch( operation->alg )
1671 {
Gilles Peskine81736312018-06-26 15:04:31 +02001672 case 0:
1673 /* The object has (apparently) been initialized but it is not
1674 * in use. It's ok to call abort on such an object, and there's
1675 * nothing to do. */
1676 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001677#if defined(MBEDTLS_MD2_C)
1678 case PSA_ALG_MD2:
1679 mbedtls_md2_free( &operation->ctx.md2 );
1680 break;
1681#endif
1682#if defined(MBEDTLS_MD4_C)
1683 case PSA_ALG_MD4:
1684 mbedtls_md4_free( &operation->ctx.md4 );
1685 break;
1686#endif
1687#if defined(MBEDTLS_MD5_C)
1688 case PSA_ALG_MD5:
1689 mbedtls_md5_free( &operation->ctx.md5 );
1690 break;
1691#endif
1692#if defined(MBEDTLS_RIPEMD160_C)
1693 case PSA_ALG_RIPEMD160:
1694 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1695 break;
1696#endif
1697#if defined(MBEDTLS_SHA1_C)
1698 case PSA_ALG_SHA_1:
1699 mbedtls_sha1_free( &operation->ctx.sha1 );
1700 break;
1701#endif
1702#if defined(MBEDTLS_SHA256_C)
1703 case PSA_ALG_SHA_224:
1704 case PSA_ALG_SHA_256:
1705 mbedtls_sha256_free( &operation->ctx.sha256 );
1706 break;
1707#endif
1708#if defined(MBEDTLS_SHA512_C)
1709 case PSA_ALG_SHA_384:
1710 case PSA_ALG_SHA_512:
1711 mbedtls_sha512_free( &operation->ctx.sha512 );
1712 break;
1713#endif
1714 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001715 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001716 }
1717 operation->alg = 0;
1718 return( PSA_SUCCESS );
1719}
1720
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001721psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001722 psa_algorithm_t alg )
1723{
1724 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001725
1726 /* A context must be freshly initialized before it can be set up. */
1727 if( operation->alg != 0 )
1728 {
1729 return( PSA_ERROR_BAD_STATE );
1730 }
1731
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001732 switch( alg )
1733 {
1734#if defined(MBEDTLS_MD2_C)
1735 case PSA_ALG_MD2:
1736 mbedtls_md2_init( &operation->ctx.md2 );
1737 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1738 break;
1739#endif
1740#if defined(MBEDTLS_MD4_C)
1741 case PSA_ALG_MD4:
1742 mbedtls_md4_init( &operation->ctx.md4 );
1743 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1744 break;
1745#endif
1746#if defined(MBEDTLS_MD5_C)
1747 case PSA_ALG_MD5:
1748 mbedtls_md5_init( &operation->ctx.md5 );
1749 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1750 break;
1751#endif
1752#if defined(MBEDTLS_RIPEMD160_C)
1753 case PSA_ALG_RIPEMD160:
1754 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1755 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1756 break;
1757#endif
1758#if defined(MBEDTLS_SHA1_C)
1759 case PSA_ALG_SHA_1:
1760 mbedtls_sha1_init( &operation->ctx.sha1 );
1761 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1762 break;
1763#endif
1764#if defined(MBEDTLS_SHA256_C)
1765 case PSA_ALG_SHA_224:
1766 mbedtls_sha256_init( &operation->ctx.sha256 );
1767 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1768 break;
1769 case PSA_ALG_SHA_256:
1770 mbedtls_sha256_init( &operation->ctx.sha256 );
1771 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1772 break;
1773#endif
1774#if defined(MBEDTLS_SHA512_C)
1775 case PSA_ALG_SHA_384:
1776 mbedtls_sha512_init( &operation->ctx.sha512 );
1777 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1778 break;
1779 case PSA_ALG_SHA_512:
1780 mbedtls_sha512_init( &operation->ctx.sha512 );
1781 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1782 break;
1783#endif
1784 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001785 return( PSA_ALG_IS_HASH( alg ) ?
1786 PSA_ERROR_NOT_SUPPORTED :
1787 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001788 }
1789 if( ret == 0 )
1790 operation->alg = alg;
1791 else
1792 psa_hash_abort( operation );
1793 return( mbedtls_to_psa_error( ret ) );
1794}
1795
1796psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1797 const uint8_t *input,
1798 size_t input_length )
1799{
1800 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001801
1802 /* Don't require hash implementations to behave correctly on a
1803 * zero-length input, which may have an invalid pointer. */
1804 if( input_length == 0 )
1805 return( PSA_SUCCESS );
1806
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001807 switch( operation->alg )
1808 {
1809#if defined(MBEDTLS_MD2_C)
1810 case PSA_ALG_MD2:
1811 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1812 input, input_length );
1813 break;
1814#endif
1815#if defined(MBEDTLS_MD4_C)
1816 case PSA_ALG_MD4:
1817 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1818 input, input_length );
1819 break;
1820#endif
1821#if defined(MBEDTLS_MD5_C)
1822 case PSA_ALG_MD5:
1823 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1824 input, input_length );
1825 break;
1826#endif
1827#if defined(MBEDTLS_RIPEMD160_C)
1828 case PSA_ALG_RIPEMD160:
1829 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1830 input, input_length );
1831 break;
1832#endif
1833#if defined(MBEDTLS_SHA1_C)
1834 case PSA_ALG_SHA_1:
1835 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1836 input, input_length );
1837 break;
1838#endif
1839#if defined(MBEDTLS_SHA256_C)
1840 case PSA_ALG_SHA_224:
1841 case PSA_ALG_SHA_256:
1842 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1843 input, input_length );
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_update_ret( &operation->ctx.sha512,
1850 input, input_length );
1851 break;
1852#endif
1853 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001854 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001855 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001856
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001857 if( ret != 0 )
1858 psa_hash_abort( operation );
1859 return( mbedtls_to_psa_error( ret ) );
1860}
1861
1862psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1863 uint8_t *hash,
1864 size_t hash_size,
1865 size_t *hash_length )
1866{
itayzafrir40835d42018-08-02 13:14:17 +03001867 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001868 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001869 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001870
1871 /* Fill the output buffer with something that isn't a valid hash
1872 * (barring an attack on the hash and deliberately-crafted input),
1873 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001874 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001875 /* If hash_size is 0 then hash may be NULL and then the
1876 * call to memset would have undefined behavior. */
1877 if( hash_size != 0 )
1878 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001879
1880 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001881 {
1882 status = PSA_ERROR_BUFFER_TOO_SMALL;
1883 goto exit;
1884 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001885
1886 switch( operation->alg )
1887 {
1888#if defined(MBEDTLS_MD2_C)
1889 case PSA_ALG_MD2:
1890 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1891 break;
1892#endif
1893#if defined(MBEDTLS_MD4_C)
1894 case PSA_ALG_MD4:
1895 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1896 break;
1897#endif
1898#if defined(MBEDTLS_MD5_C)
1899 case PSA_ALG_MD5:
1900 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1901 break;
1902#endif
1903#if defined(MBEDTLS_RIPEMD160_C)
1904 case PSA_ALG_RIPEMD160:
1905 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1906 break;
1907#endif
1908#if defined(MBEDTLS_SHA1_C)
1909 case PSA_ALG_SHA_1:
1910 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1911 break;
1912#endif
1913#if defined(MBEDTLS_SHA256_C)
1914 case PSA_ALG_SHA_224:
1915 case PSA_ALG_SHA_256:
1916 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1917 break;
1918#endif
1919#if defined(MBEDTLS_SHA512_C)
1920 case PSA_ALG_SHA_384:
1921 case PSA_ALG_SHA_512:
1922 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1923 break;
1924#endif
1925 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001926 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001927 }
itayzafrir40835d42018-08-02 13:14:17 +03001928 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001929
itayzafrir40835d42018-08-02 13:14:17 +03001930exit:
1931 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001932 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001933 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001934 return( psa_hash_abort( operation ) );
1935 }
1936 else
1937 {
1938 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001939 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001940 }
1941}
1942
Gilles Peskine2d277862018-06-18 15:41:12 +02001943psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1944 const uint8_t *hash,
1945 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001946{
1947 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1948 size_t actual_hash_length;
1949 psa_status_t status = psa_hash_finish( operation,
1950 actual_hash, sizeof( actual_hash ),
1951 &actual_hash_length );
1952 if( status != PSA_SUCCESS )
1953 return( status );
1954 if( actual_hash_length != hash_length )
1955 return( PSA_ERROR_INVALID_SIGNATURE );
1956 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1957 return( PSA_ERROR_INVALID_SIGNATURE );
1958 return( PSA_SUCCESS );
1959}
1960
Gilles Peskineeb35d782019-01-22 17:56:16 +01001961psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1962 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001963{
1964 if( target_operation->alg != 0 )
1965 return( PSA_ERROR_BAD_STATE );
1966
1967 switch( source_operation->alg )
1968 {
1969 case 0:
1970 return( PSA_ERROR_BAD_STATE );
1971#if defined(MBEDTLS_MD2_C)
1972 case PSA_ALG_MD2:
1973 mbedtls_md2_clone( &target_operation->ctx.md2,
1974 &source_operation->ctx.md2 );
1975 break;
1976#endif
1977#if defined(MBEDTLS_MD4_C)
1978 case PSA_ALG_MD4:
1979 mbedtls_md4_clone( &target_operation->ctx.md4,
1980 &source_operation->ctx.md4 );
1981 break;
1982#endif
1983#if defined(MBEDTLS_MD5_C)
1984 case PSA_ALG_MD5:
1985 mbedtls_md5_clone( &target_operation->ctx.md5,
1986 &source_operation->ctx.md5 );
1987 break;
1988#endif
1989#if defined(MBEDTLS_RIPEMD160_C)
1990 case PSA_ALG_RIPEMD160:
1991 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1992 &source_operation->ctx.ripemd160 );
1993 break;
1994#endif
1995#if defined(MBEDTLS_SHA1_C)
1996 case PSA_ALG_SHA_1:
1997 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1998 &source_operation->ctx.sha1 );
1999 break;
2000#endif
2001#if defined(MBEDTLS_SHA256_C)
2002 case PSA_ALG_SHA_224:
2003 case PSA_ALG_SHA_256:
2004 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2005 &source_operation->ctx.sha256 );
2006 break;
2007#endif
2008#if defined(MBEDTLS_SHA512_C)
2009 case PSA_ALG_SHA_384:
2010 case PSA_ALG_SHA_512:
2011 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2012 &source_operation->ctx.sha512 );
2013 break;
2014#endif
2015 default:
2016 return( PSA_ERROR_NOT_SUPPORTED );
2017 }
2018
2019 target_operation->alg = source_operation->alg;
2020 return( PSA_SUCCESS );
2021}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002022
2023
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002024/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025/* MAC */
2026/****************************************************************/
2027
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002028static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002029 psa_algorithm_t alg,
2030 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002031 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002032 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002033{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002034 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002035 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002036
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002037 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002038 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002039
Gilles Peskine8c9def32018-02-08 10:02:12 +01002040 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2041 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002042 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002044 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002045 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046 mode = MBEDTLS_MODE_STREAM;
2047 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048 case PSA_ALG_CTR:
2049 mode = MBEDTLS_MODE_CTR;
2050 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002051 case PSA_ALG_CFB:
2052 mode = MBEDTLS_MODE_CFB;
2053 break;
2054 case PSA_ALG_OFB:
2055 mode = MBEDTLS_MODE_OFB;
2056 break;
2057 case PSA_ALG_CBC_NO_PADDING:
2058 mode = MBEDTLS_MODE_CBC;
2059 break;
2060 case PSA_ALG_CBC_PKCS7:
2061 mode = MBEDTLS_MODE_CBC;
2062 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002063 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002064 mode = MBEDTLS_MODE_CCM;
2065 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002066 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067 mode = MBEDTLS_MODE_GCM;
2068 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002069 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2070 mode = MBEDTLS_MODE_CHACHAPOLY;
2071 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072 default:
2073 return( NULL );
2074 }
2075 }
2076 else if( alg == PSA_ALG_CMAC )
2077 mode = MBEDTLS_MODE_ECB;
2078 else if( alg == PSA_ALG_GMAC )
2079 mode = MBEDTLS_MODE_GCM;
2080 else
2081 return( NULL );
2082
2083 switch( key_type )
2084 {
2085 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002086 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087 break;
2088 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002089 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2090 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002092 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002093 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002094 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002095 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2096 * but two-key Triple-DES is functionally three-key Triple-DES
2097 * with K1=K3, so that's how we present it to mbedtls. */
2098 if( key_bits == 128 )
2099 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 break;
2101 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002102 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103 break;
2104 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002105 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002106 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002107 case PSA_KEY_TYPE_CHACHA20:
2108 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2109 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 default:
2111 return( NULL );
2112 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002113 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002114 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115
Jaeden Amero23bbb752018-06-26 14:16:54 +01002116 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2117 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002118}
2119
Gilles Peskinea05219c2018-11-16 16:02:56 +01002120#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002121static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002122{
Gilles Peskine2d277862018-06-18 15:41:12 +02002123 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002124 {
2125 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002126 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002127 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002128 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002129 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002130 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002131 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002132 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002133 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002134 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002135 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002136 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002137 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002138 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002139 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002140 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002141 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002142 return( 128 );
2143 default:
2144 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002145 }
2146}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002147#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002148
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002149/* Initialize the MAC operation structure. Once this function has been
2150 * called, psa_mac_abort can run and will do the right thing. */
2151static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2152 psa_algorithm_t alg )
2153{
2154 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2155
2156 operation->alg = alg;
2157 operation->key_set = 0;
2158 operation->iv_set = 0;
2159 operation->iv_required = 0;
2160 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002161 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002162
2163#if defined(MBEDTLS_CMAC_C)
2164 if( alg == PSA_ALG_CMAC )
2165 {
2166 operation->iv_required = 0;
2167 mbedtls_cipher_init( &operation->ctx.cmac );
2168 status = PSA_SUCCESS;
2169 }
2170 else
2171#endif /* MBEDTLS_CMAC_C */
2172#if defined(MBEDTLS_MD_C)
2173 if( PSA_ALG_IS_HMAC( operation->alg ) )
2174 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002175 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2176 operation->ctx.hmac.hash_ctx.alg = 0;
2177 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002178 }
2179 else
2180#endif /* MBEDTLS_MD_C */
2181 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002182 if( ! PSA_ALG_IS_MAC( alg ) )
2183 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002184 }
2185
2186 if( status != PSA_SUCCESS )
2187 memset( operation, 0, sizeof( *operation ) );
2188 return( status );
2189}
2190
Gilles Peskine01126fa2018-07-12 17:04:55 +02002191#if defined(MBEDTLS_MD_C)
2192static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2193{
Gilles Peskine3f108122018-12-07 18:14:53 +01002194 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002195 return( psa_hash_abort( &hmac->hash_ctx ) );
2196}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002197
2198static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2199{
2200 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2201 memset( hmac, 0, sizeof( *hmac ) );
2202}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002203#endif /* MBEDTLS_MD_C */
2204
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2206{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002207 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002209 /* The object has (apparently) been initialized but it is not
2210 * in use. It's ok to call abort on such an object, and there's
2211 * nothing to do. */
2212 return( PSA_SUCCESS );
2213 }
2214 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002216 if( operation->alg == PSA_ALG_CMAC )
2217 {
2218 mbedtls_cipher_free( &operation->ctx.cmac );
2219 }
2220 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002223 if( PSA_ALG_IS_HMAC( operation->alg ) )
2224 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002225 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002226 }
2227 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002229 {
2230 /* Sanity check (shouldn't happen: operation->alg should
2231 * always have been initialized to a valid value). */
2232 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233 }
Moran Peker41deec42018-04-04 15:43:05 +03002234
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235 operation->alg = 0;
2236 operation->key_set = 0;
2237 operation->iv_set = 0;
2238 operation->iv_required = 0;
2239 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002240 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002241
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002243
2244bad_state:
2245 /* If abort is called on an uninitialized object, we can't trust
2246 * anything. Wipe the object in case it contains confidential data.
2247 * This may result in a memory leak if a pointer gets overwritten,
2248 * but it's too late to do anything about this. */
2249 memset( operation, 0, sizeof( *operation ) );
2250 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002251}
2252
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002253#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002254static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002255 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002256 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002257 const mbedtls_cipher_info_t *cipher_info )
2258{
2259 int ret;
2260
2261 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002262
2263 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2264 if( ret != 0 )
2265 return( ret );
2266
2267 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2268 slot->data.raw.data,
2269 key_bits );
2270 return( ret );
2271}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002272#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002273
Gilles Peskine248051a2018-06-20 16:09:38 +02002274#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002275static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2276 const uint8_t *key,
2277 size_t key_length,
2278 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002279{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002280 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002281 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002282 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002283 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002284 psa_status_t status;
2285
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002286 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2287 * overflow below. This should never trigger if the hash algorithm
2288 * is implemented correctly. */
2289 /* The size checks against the ipad and opad buffers cannot be written
2290 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2291 * because that triggers -Wlogical-op on GCC 7.3. */
2292 if( block_size > sizeof( ipad ) )
2293 return( PSA_ERROR_NOT_SUPPORTED );
2294 if( block_size > sizeof( hmac->opad ) )
2295 return( PSA_ERROR_NOT_SUPPORTED );
2296 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002297 return( PSA_ERROR_NOT_SUPPORTED );
2298
Gilles Peskined223b522018-06-11 18:12:58 +02002299 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002300 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002301 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002302 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002303 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002304 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002305 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002306 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002307 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002308 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002309 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002310 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002311 }
Gilles Peskine96889972018-07-12 17:07:03 +02002312 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2313 * but it is permitted. It is common when HMAC is used in HKDF, for
2314 * example. Don't call `memcpy` in the 0-length because `key` could be
2315 * an invalid pointer which would make the behavior undefined. */
2316 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002317 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002318
Gilles Peskined223b522018-06-11 18:12:58 +02002319 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2320 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002321 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002322 ipad[i] ^= 0x36;
2323 memset( ipad + key_length, 0x36, block_size - key_length );
2324
2325 /* Copy the key material from ipad to opad, flipping the requisite bits,
2326 * and filling the rest of opad with the requisite constant. */
2327 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002328 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2329 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002330
Gilles Peskine01126fa2018-07-12 17:04:55 +02002331 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002332 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002333 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002334
Gilles Peskine01126fa2018-07-12 17:04:55 +02002335 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002336
2337cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002338 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002339
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002340 return( status );
2341}
Gilles Peskine248051a2018-06-20 16:09:38 +02002342#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002343
Gilles Peskine89167cb2018-07-08 20:12:23 +02002344static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002345 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002346 psa_algorithm_t alg,
2347 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002348{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002350 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002352 psa_key_usage_t usage =
2353 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002354 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002355 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002357 /* A context must be freshly initialized before it can be set up. */
2358 if( operation->alg != 0 )
2359 {
2360 return( PSA_ERROR_BAD_STATE );
2361 }
2362
Gilles Peskined911eb72018-08-14 15:18:45 +02002363 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002364 if( status != PSA_SUCCESS )
2365 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002366 if( is_sign )
2367 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002368
Gilles Peskinec5487a82018-12-03 18:08:14 +01002369 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002370 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002371 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002372 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002373
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002375 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002376 {
2377 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002378 mbedtls_cipher_info_from_psa( full_length_alg,
2379 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002380 int ret;
2381 if( cipher_info == NULL )
2382 {
2383 status = PSA_ERROR_NOT_SUPPORTED;
2384 goto exit;
2385 }
2386 operation->mac_size = cipher_info->block_size;
2387 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2388 status = mbedtls_to_psa_error( ret );
2389 }
2390 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002392#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002393 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002394 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002395 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002396 if( hash_alg == 0 )
2397 {
2398 status = PSA_ERROR_NOT_SUPPORTED;
2399 goto exit;
2400 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002401
2402 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2403 /* Sanity check. This shouldn't fail on a valid configuration. */
2404 if( operation->mac_size == 0 ||
2405 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2406 {
2407 status = PSA_ERROR_NOT_SUPPORTED;
2408 goto exit;
2409 }
2410
Gilles Peskine01126fa2018-07-12 17:04:55 +02002411 if( slot->type != PSA_KEY_TYPE_HMAC )
2412 {
2413 status = PSA_ERROR_INVALID_ARGUMENT;
2414 goto exit;
2415 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002416
Gilles Peskine01126fa2018-07-12 17:04:55 +02002417 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2418 slot->data.raw.data,
2419 slot->data.raw.bytes,
2420 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002421 }
2422 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002425 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002426 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002428
Gilles Peskined911eb72018-08-14 15:18:45 +02002429 if( truncated == 0 )
2430 {
2431 /* The "normal" case: untruncated algorithm. Nothing to do. */
2432 }
2433 else if( truncated < 4 )
2434 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002435 /* A very short MAC is too short for security since it can be
2436 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2437 * so we make this our minimum, even though 32 bits is still
2438 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002439 status = PSA_ERROR_NOT_SUPPORTED;
2440 }
2441 else if( truncated > operation->mac_size )
2442 {
2443 /* It's impossible to "truncate" to a larger length. */
2444 status = PSA_ERROR_INVALID_ARGUMENT;
2445 }
2446 else
2447 operation->mac_size = truncated;
2448
Gilles Peskinefbfac682018-07-08 20:51:54 +02002449exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002450 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002452 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002454 else
2455 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002456 operation->key_set = 1;
2457 }
2458 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459}
2460
Gilles Peskine89167cb2018-07-08 20:12:23 +02002461psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002462 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002463 psa_algorithm_t alg )
2464{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002465 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466}
2467
2468psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002469 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002470 psa_algorithm_t alg )
2471{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002472 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002473}
2474
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2476 const uint8_t *input,
2477 size_t input_length )
2478{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002479 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002481 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002483 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484 operation->has_input = 1;
2485
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002487 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002488 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002489 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2490 input, input_length );
2491 status = mbedtls_to_psa_error( ret );
2492 }
2493 else
2494#endif /* MBEDTLS_CMAC_C */
2495#if defined(MBEDTLS_MD_C)
2496 if( PSA_ALG_IS_HMAC( operation->alg ) )
2497 {
2498 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2499 input_length );
2500 }
2501 else
2502#endif /* MBEDTLS_MD_C */
2503 {
2504 /* This shouldn't happen if `operation` was initialized by
2505 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002506 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002507 }
2508
Gilles Peskinefbfac682018-07-08 20:51:54 +02002509 if( status != PSA_SUCCESS )
2510 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002511 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512}
2513
Gilles Peskine01126fa2018-07-12 17:04:55 +02002514#if defined(MBEDTLS_MD_C)
2515static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2516 uint8_t *mac,
2517 size_t mac_size )
2518{
2519 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2520 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2521 size_t hash_size = 0;
2522 size_t block_size = psa_get_hash_block_size( hash_alg );
2523 psa_status_t status;
2524
Gilles Peskine01126fa2018-07-12 17:04:55 +02002525 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2526 if( status != PSA_SUCCESS )
2527 return( status );
2528 /* From here on, tmp needs to be wiped. */
2529
2530 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2531 if( status != PSA_SUCCESS )
2532 goto exit;
2533
2534 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2535 if( status != PSA_SUCCESS )
2536 goto exit;
2537
2538 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2539 if( status != PSA_SUCCESS )
2540 goto exit;
2541
Gilles Peskined911eb72018-08-14 15:18:45 +02002542 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2543 if( status != PSA_SUCCESS )
2544 goto exit;
2545
2546 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002547
2548exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002549 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002550 return( status );
2551}
2552#endif /* MBEDTLS_MD_C */
2553
mohammad16036df908f2018-04-02 08:34:15 -07002554static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002555 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002556 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002558 if( ! operation->key_set )
2559 return( PSA_ERROR_BAD_STATE );
2560 if( operation->iv_required && ! operation->iv_set )
2561 return( PSA_ERROR_BAD_STATE );
2562
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 if( mac_size < operation->mac_size )
2564 return( PSA_ERROR_BUFFER_TOO_SMALL );
2565
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002567 if( operation->alg == PSA_ALG_CMAC )
2568 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002569 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2570 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2571 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002572 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002573 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002574 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002576 else
2577#endif /* MBEDTLS_CMAC_C */
2578#if defined(MBEDTLS_MD_C)
2579 if( PSA_ALG_IS_HMAC( operation->alg ) )
2580 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002581 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002582 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002583 }
2584 else
2585#endif /* MBEDTLS_MD_C */
2586 {
2587 /* This shouldn't happen if `operation` was initialized by
2588 * a setup function. */
2589 return( PSA_ERROR_BAD_STATE );
2590 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002591}
2592
Gilles Peskineacd4be32018-07-08 19:56:25 +02002593psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2594 uint8_t *mac,
2595 size_t mac_size,
2596 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002597{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002598 psa_status_t status;
2599
Jaeden Amero252ef282019-02-15 14:05:35 +00002600 if( operation->alg == 0 )
2601 {
2602 return( PSA_ERROR_BAD_STATE );
2603 }
2604
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002605 /* Fill the output buffer with something that isn't a valid mac
2606 * (barring an attack on the mac and deliberately-crafted input),
2607 * in case the caller doesn't check the return status properly. */
2608 *mac_length = mac_size;
2609 /* If mac_size is 0 then mac may be NULL and then the
2610 * call to memset would have undefined behavior. */
2611 if( mac_size != 0 )
2612 memset( mac, '!', mac_size );
2613
Gilles Peskine89167cb2018-07-08 20:12:23 +02002614 if( ! operation->is_sign )
2615 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002616 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002617 }
mohammad16036df908f2018-04-02 08:34:15 -07002618
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002619 status = psa_mac_finish_internal( operation, mac, mac_size );
2620
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002621 if( status == PSA_SUCCESS )
2622 {
2623 status = psa_mac_abort( operation );
2624 if( status == PSA_SUCCESS )
2625 *mac_length = operation->mac_size;
2626 else
2627 memset( mac, '!', mac_size );
2628 }
2629 else
2630 psa_mac_abort( operation );
2631 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002632}
2633
Gilles Peskineacd4be32018-07-08 19:56:25 +02002634psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2635 const uint8_t *mac,
2636 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637{
Gilles Peskine828ed142018-06-18 23:25:51 +02002638 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002639 psa_status_t status;
2640
Jaeden Amero252ef282019-02-15 14:05:35 +00002641 if( operation->alg == 0 )
2642 {
2643 return( PSA_ERROR_BAD_STATE );
2644 }
2645
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646 if( operation->is_sign )
2647 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002648 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002649 }
2650 if( operation->mac_size != mac_length )
2651 {
2652 status = PSA_ERROR_INVALID_SIGNATURE;
2653 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002654 }
mohammad16036df908f2018-04-02 08:34:15 -07002655
2656 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002657 actual_mac, sizeof( actual_mac ) );
2658
2659 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2660 status = PSA_ERROR_INVALID_SIGNATURE;
2661
2662cleanup:
2663 if( status == PSA_SUCCESS )
2664 status = psa_mac_abort( operation );
2665 else
2666 psa_mac_abort( operation );
2667
Gilles Peskine3f108122018-12-07 18:14:53 +01002668 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002669
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002670 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671}
2672
2673
Gilles Peskine20035e32018-02-03 22:44:14 +01002674
Gilles Peskine20035e32018-02-03 22:44:14 +01002675/****************************************************************/
2676/* Asymmetric cryptography */
2677/****************************************************************/
2678
Gilles Peskine2b450e32018-06-27 15:42:46 +02002679#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002680/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002681 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002682static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2683 size_t hash_length,
2684 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002685{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002686 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002687 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002688 *md_alg = mbedtls_md_get_type( md_info );
2689
2690 /* The Mbed TLS RSA module uses an unsigned int for hash length
2691 * parameters. Validate that it fits so that we don't risk an
2692 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002693#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002694 if( hash_length > UINT_MAX )
2695 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002696#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002697
2698#if defined(MBEDTLS_PKCS1_V15)
2699 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2700 * must be correct. */
2701 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2702 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002703 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002704 if( md_info == NULL )
2705 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002706 if( mbedtls_md_get_size( md_info ) != hash_length )
2707 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002708 }
2709#endif /* MBEDTLS_PKCS1_V15 */
2710
2711#if defined(MBEDTLS_PKCS1_V21)
2712 /* PSS requires a hash internally. */
2713 if( PSA_ALG_IS_RSA_PSS( alg ) )
2714 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002715 if( md_info == NULL )
2716 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002717 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002718#endif /* MBEDTLS_PKCS1_V21 */
2719
Gilles Peskine61b91d42018-06-08 16:09:36 +02002720 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002721}
2722
Gilles Peskine2b450e32018-06-27 15:42:46 +02002723static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2724 psa_algorithm_t alg,
2725 const uint8_t *hash,
2726 size_t hash_length,
2727 uint8_t *signature,
2728 size_t signature_size,
2729 size_t *signature_length )
2730{
2731 psa_status_t status;
2732 int ret;
2733 mbedtls_md_type_t md_alg;
2734
2735 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2736 if( status != PSA_SUCCESS )
2737 return( status );
2738
Gilles Peskine630a18a2018-06-29 17:49:35 +02002739 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002740 return( PSA_ERROR_BUFFER_TOO_SMALL );
2741
2742#if defined(MBEDTLS_PKCS1_V15)
2743 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2744 {
2745 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2746 MBEDTLS_MD_NONE );
2747 ret = mbedtls_rsa_pkcs1_sign( rsa,
2748 mbedtls_ctr_drbg_random,
2749 &global_data.ctr_drbg,
2750 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002751 md_alg,
2752 (unsigned int) hash_length,
2753 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002754 signature );
2755 }
2756 else
2757#endif /* MBEDTLS_PKCS1_V15 */
2758#if defined(MBEDTLS_PKCS1_V21)
2759 if( PSA_ALG_IS_RSA_PSS( alg ) )
2760 {
2761 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2762 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2763 mbedtls_ctr_drbg_random,
2764 &global_data.ctr_drbg,
2765 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002766 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002767 (unsigned int) hash_length,
2768 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002769 signature );
2770 }
2771 else
2772#endif /* MBEDTLS_PKCS1_V21 */
2773 {
2774 return( PSA_ERROR_INVALID_ARGUMENT );
2775 }
2776
2777 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002778 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002779 return( mbedtls_to_psa_error( ret ) );
2780}
2781
2782static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2783 psa_algorithm_t alg,
2784 const uint8_t *hash,
2785 size_t hash_length,
2786 const uint8_t *signature,
2787 size_t signature_length )
2788{
2789 psa_status_t status;
2790 int ret;
2791 mbedtls_md_type_t md_alg;
2792
2793 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2794 if( status != PSA_SUCCESS )
2795 return( status );
2796
Gilles Peskine630a18a2018-06-29 17:49:35 +02002797 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002798 return( PSA_ERROR_BUFFER_TOO_SMALL );
2799
2800#if defined(MBEDTLS_PKCS1_V15)
2801 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2802 {
2803 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2804 MBEDTLS_MD_NONE );
2805 ret = mbedtls_rsa_pkcs1_verify( rsa,
2806 mbedtls_ctr_drbg_random,
2807 &global_data.ctr_drbg,
2808 MBEDTLS_RSA_PUBLIC,
2809 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002810 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002811 hash,
2812 signature );
2813 }
2814 else
2815#endif /* MBEDTLS_PKCS1_V15 */
2816#if defined(MBEDTLS_PKCS1_V21)
2817 if( PSA_ALG_IS_RSA_PSS( alg ) )
2818 {
2819 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2820 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2821 mbedtls_ctr_drbg_random,
2822 &global_data.ctr_drbg,
2823 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002824 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002825 (unsigned int) hash_length,
2826 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002827 signature );
2828 }
2829 else
2830#endif /* MBEDTLS_PKCS1_V21 */
2831 {
2832 return( PSA_ERROR_INVALID_ARGUMENT );
2833 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002834
2835 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2836 * the rest of the signature is invalid". This has little use in
2837 * practice and PSA doesn't report this distinction. */
2838 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2839 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002840 return( mbedtls_to_psa_error( ret ) );
2841}
2842#endif /* MBEDTLS_RSA_C */
2843
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002844#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002845/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2846 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2847 * (even though these functions don't modify it). */
2848static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2849 psa_algorithm_t alg,
2850 const uint8_t *hash,
2851 size_t hash_length,
2852 uint8_t *signature,
2853 size_t signature_size,
2854 size_t *signature_length )
2855{
2856 int ret;
2857 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002858 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002859 mbedtls_mpi_init( &r );
2860 mbedtls_mpi_init( &s );
2861
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002862 if( signature_size < 2 * curve_bytes )
2863 {
2864 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2865 goto cleanup;
2866 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002867
Gilles Peskinea05219c2018-11-16 16:02:56 +01002868#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002869 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2870 {
2871 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2872 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2873 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2874 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2875 hash, hash_length,
2876 md_alg ) );
2877 }
2878 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002879#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002880 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002881 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002882 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2883 hash, hash_length,
2884 mbedtls_ctr_drbg_random,
2885 &global_data.ctr_drbg ) );
2886 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002887
2888 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2889 signature,
2890 curve_bytes ) );
2891 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2892 signature + curve_bytes,
2893 curve_bytes ) );
2894
2895cleanup:
2896 mbedtls_mpi_free( &r );
2897 mbedtls_mpi_free( &s );
2898 if( ret == 0 )
2899 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002900 return( mbedtls_to_psa_error( ret ) );
2901}
2902
2903static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2904 const uint8_t *hash,
2905 size_t hash_length,
2906 const uint8_t *signature,
2907 size_t signature_length )
2908{
2909 int ret;
2910 mbedtls_mpi r, s;
2911 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2912 mbedtls_mpi_init( &r );
2913 mbedtls_mpi_init( &s );
2914
2915 if( signature_length != 2 * curve_bytes )
2916 return( PSA_ERROR_INVALID_SIGNATURE );
2917
2918 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2919 signature,
2920 curve_bytes ) );
2921 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2922 signature + curve_bytes,
2923 curve_bytes ) );
2924
2925 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2926 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002927
2928cleanup:
2929 mbedtls_mpi_free( &r );
2930 mbedtls_mpi_free( &s );
2931 return( mbedtls_to_psa_error( ret ) );
2932}
2933#endif /* MBEDTLS_ECDSA_C */
2934
Gilles Peskinec5487a82018-12-03 18:08:14 +01002935psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002936 psa_algorithm_t alg,
2937 const uint8_t *hash,
2938 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002939 uint8_t *signature,
2940 size_t signature_size,
2941 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002942{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002943 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002944 psa_status_t status;
2945
2946 *signature_length = signature_size;
2947
Gilles Peskinec5487a82018-12-03 18:08:14 +01002948 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002949 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002950 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002951 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002952 {
2953 status = PSA_ERROR_INVALID_ARGUMENT;
2954 goto exit;
2955 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002956
Gilles Peskine20035e32018-02-03 22:44:14 +01002957#if defined(MBEDTLS_RSA_C)
2958 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2959 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002960 status = psa_rsa_sign( slot->data.rsa,
2961 alg,
2962 hash, hash_length,
2963 signature, signature_size,
2964 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002965 }
2966 else
2967#endif /* defined(MBEDTLS_RSA_C) */
2968#if defined(MBEDTLS_ECP_C)
2969 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2970 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002971#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002972 if(
2973#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2974 PSA_ALG_IS_ECDSA( alg )
2975#else
2976 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2977#endif
2978 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002979 status = psa_ecdsa_sign( slot->data.ecp,
2980 alg,
2981 hash, hash_length,
2982 signature, signature_size,
2983 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002984 else
2985#endif /* defined(MBEDTLS_ECDSA_C) */
2986 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002987 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002988 }
itayzafrir5c753392018-05-08 11:18:38 +03002989 }
2990 else
2991#endif /* defined(MBEDTLS_ECP_C) */
2992 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002993 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002994 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002995
2996exit:
2997 /* Fill the unused part of the output buffer (the whole buffer on error,
2998 * the trailing part on success) with something that isn't a valid mac
2999 * (barring an attack on the mac and deliberately-crafted input),
3000 * in case the caller doesn't check the return status properly. */
3001 if( status == PSA_SUCCESS )
3002 memset( signature + *signature_length, '!',
3003 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003004 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003005 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003006 /* If signature_size is 0 then we have nothing to do. We must not call
3007 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003008 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003009}
3010
Gilles Peskinec5487a82018-12-03 18:08:14 +01003011psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003012 psa_algorithm_t alg,
3013 const uint8_t *hash,
3014 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003015 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003016 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003017{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003018 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003019 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003020
Gilles Peskinec5487a82018-12-03 18:08:14 +01003021 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003022 if( status != PSA_SUCCESS )
3023 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003024
Gilles Peskine61b91d42018-06-08 16:09:36 +02003025#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003026 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003027 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003028 return( psa_rsa_verify( slot->data.rsa,
3029 alg,
3030 hash, hash_length,
3031 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032 }
3033 else
3034#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003035#if defined(MBEDTLS_ECP_C)
3036 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3037 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003038#if defined(MBEDTLS_ECDSA_C)
3039 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003040 return( psa_ecdsa_verify( slot->data.ecp,
3041 hash, hash_length,
3042 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003043 else
3044#endif /* defined(MBEDTLS_ECDSA_C) */
3045 {
3046 return( PSA_ERROR_INVALID_ARGUMENT );
3047 }
itayzafrir5c753392018-05-08 11:18:38 +03003048 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003049 else
3050#endif /* defined(MBEDTLS_ECP_C) */
3051 {
3052 return( PSA_ERROR_NOT_SUPPORTED );
3053 }
3054}
3055
Gilles Peskine072ac562018-06-30 00:21:29 +02003056#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3057static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3058 mbedtls_rsa_context *rsa )
3059{
3060 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3061 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3062 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3063 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3064}
3065#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3066
Gilles Peskinec5487a82018-12-03 18:08:14 +01003067psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003068 psa_algorithm_t alg,
3069 const uint8_t *input,
3070 size_t input_length,
3071 const uint8_t *salt,
3072 size_t salt_length,
3073 uint8_t *output,
3074 size_t output_size,
3075 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003076{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003077 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003078 psa_status_t status;
3079
Darryl Green5cc689a2018-07-24 15:34:10 +01003080 (void) input;
3081 (void) input_length;
3082 (void) salt;
3083 (void) output;
3084 (void) output_size;
3085
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003086 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003087
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003088 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3089 return( PSA_ERROR_INVALID_ARGUMENT );
3090
Gilles Peskinec5487a82018-12-03 18:08:14 +01003091 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003092 if( status != PSA_SUCCESS )
3093 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003094 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3095 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003097
3098#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003099 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003100 {
3101 mbedtls_rsa_context *rsa = slot->data.rsa;
3102 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003103 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003104 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003106 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003108 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3109 mbedtls_ctr_drbg_random,
3110 &global_data.ctr_drbg,
3111 MBEDTLS_RSA_PUBLIC,
3112 input_length,
3113 input,
3114 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 }
3116 else
3117#endif /* MBEDTLS_PKCS1_V15 */
3118#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003119 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003121 psa_rsa_oaep_set_padding_mode( alg, rsa );
3122 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3123 mbedtls_ctr_drbg_random,
3124 &global_data.ctr_drbg,
3125 MBEDTLS_RSA_PUBLIC,
3126 salt, salt_length,
3127 input_length,
3128 input,
3129 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130 }
3131 else
3132#endif /* MBEDTLS_PKCS1_V21 */
3133 {
3134 return( PSA_ERROR_INVALID_ARGUMENT );
3135 }
3136 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003137 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138 return( mbedtls_to_psa_error( ret ) );
3139 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003141#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142 {
3143 return( PSA_ERROR_NOT_SUPPORTED );
3144 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003145}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146
Gilles Peskinec5487a82018-12-03 18:08:14 +01003147psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003148 psa_algorithm_t alg,
3149 const uint8_t *input,
3150 size_t input_length,
3151 const uint8_t *salt,
3152 size_t salt_length,
3153 uint8_t *output,
3154 size_t output_size,
3155 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003157 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003158 psa_status_t status;
3159
Darryl Green5cc689a2018-07-24 15:34:10 +01003160 (void) input;
3161 (void) input_length;
3162 (void) salt;
3163 (void) output;
3164 (void) output_size;
3165
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003166 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003168 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3169 return( PSA_ERROR_INVALID_ARGUMENT );
3170
Gilles Peskinec5487a82018-12-03 18:08:14 +01003171 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003172 if( status != PSA_SUCCESS )
3173 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3175 return( PSA_ERROR_INVALID_ARGUMENT );
3176
3177#if defined(MBEDTLS_RSA_C)
3178 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3179 {
3180 mbedtls_rsa_context *rsa = slot->data.rsa;
3181 int ret;
3182
Gilles Peskine630a18a2018-06-29 17:49:35 +02003183 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184 return( PSA_ERROR_INVALID_ARGUMENT );
3185
3186#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003187 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003189 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3190 mbedtls_ctr_drbg_random,
3191 &global_data.ctr_drbg,
3192 MBEDTLS_RSA_PRIVATE,
3193 output_length,
3194 input,
3195 output,
3196 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 }
3198 else
3199#endif /* MBEDTLS_PKCS1_V15 */
3200#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003201 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003203 psa_rsa_oaep_set_padding_mode( alg, rsa );
3204 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3205 mbedtls_ctr_drbg_random,
3206 &global_data.ctr_drbg,
3207 MBEDTLS_RSA_PRIVATE,
3208 salt, salt_length,
3209 output_length,
3210 input,
3211 output,
3212 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003213 }
3214 else
3215#endif /* MBEDTLS_PKCS1_V21 */
3216 {
3217 return( PSA_ERROR_INVALID_ARGUMENT );
3218 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003219
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220 return( mbedtls_to_psa_error( ret ) );
3221 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003223#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224 {
3225 return( PSA_ERROR_NOT_SUPPORTED );
3226 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003227}
Gilles Peskine20035e32018-02-03 22:44:14 +01003228
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003229
3230
mohammad1603503973b2018-03-12 15:59:30 +02003231/****************************************************************/
3232/* Symmetric cryptography */
3233/****************************************************************/
3234
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235/* Initialize the cipher operation structure. Once this function has been
3236 * called, psa_cipher_abort can run and will do the right thing. */
3237static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3238 psa_algorithm_t alg )
3239{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003240 if( ! PSA_ALG_IS_CIPHER( alg ) )
3241 {
3242 memset( operation, 0, sizeof( *operation ) );
3243 return( PSA_ERROR_INVALID_ARGUMENT );
3244 }
3245
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003246 operation->alg = alg;
3247 operation->key_set = 0;
3248 operation->iv_set = 0;
3249 operation->iv_required = 1;
3250 operation->iv_size = 0;
3251 operation->block_size = 0;
3252 mbedtls_cipher_init( &operation->ctx.cipher );
3253 return( PSA_SUCCESS );
3254}
3255
Gilles Peskinee553c652018-06-04 16:22:46 +02003256static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003257 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003258 psa_algorithm_t alg,
3259 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003260{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003261 int ret = 0;
3262 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003263 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003264 size_t key_bits;
3265 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003266 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3267 PSA_KEY_USAGE_ENCRYPT :
3268 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003269
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003270 /* A context must be freshly initialized before it can be set up. */
3271 if( operation->alg != 0 )
3272 {
3273 return( PSA_ERROR_BAD_STATE );
3274 }
3275
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003276 status = psa_cipher_init( operation, alg );
3277 if( status != PSA_SUCCESS )
3278 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003279
Gilles Peskinec5487a82018-12-03 18:08:14 +01003280 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003282 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003283 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003284
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003285 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003286 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003287 {
3288 status = PSA_ERROR_NOT_SUPPORTED;
3289 goto exit;
3290 }
mohammad1603503973b2018-03-12 15:59:30 +02003291
mohammad1603503973b2018-03-12 15:59:30 +02003292 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003293 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003294 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003295
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003296#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003297 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003298 {
3299 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3300 unsigned char keys[24];
3301 memcpy( keys, slot->data.raw.data, 16 );
3302 memcpy( keys + 16, slot->data.raw.data, 8 );
3303 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3304 keys,
3305 192, cipher_operation );
3306 }
3307 else
3308#endif
3309 {
3310 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3311 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003312 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003313 }
Moran Peker41deec42018-04-04 15:43:05 +03003314 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003315 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003316
mohammad16038481e742018-03-18 13:57:31 +02003317#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003318 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003319 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003320 case PSA_ALG_CBC_NO_PADDING:
3321 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3322 MBEDTLS_PADDING_NONE );
3323 break;
3324 case PSA_ALG_CBC_PKCS7:
3325 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3326 MBEDTLS_PADDING_PKCS7 );
3327 break;
3328 default:
3329 /* The algorithm doesn't involve padding. */
3330 ret = 0;
3331 break;
3332 }
3333 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003334 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003335#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3336
mohammad1603503973b2018-03-12 15:59:30 +02003337 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003338 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3339 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3340 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003341 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003342 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003343 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003344#if defined(MBEDTLS_CHACHA20_C)
3345 else
3346 if( alg == PSA_ALG_CHACHA20 )
3347 operation->iv_size = 12;
3348#endif
mohammad1603503973b2018-03-12 15:59:30 +02003349
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003350exit:
3351 if( status == 0 )
3352 status = mbedtls_to_psa_error( ret );
3353 if( status != 0 )
3354 psa_cipher_abort( operation );
3355 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003356}
3357
Gilles Peskinefe119512018-07-08 21:39:34 +02003358psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003359 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003360 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003361{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003362 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003363}
3364
Gilles Peskinefe119512018-07-08 21:39:34 +02003365psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003366 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003367 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003368{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003369 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003370}
3371
Gilles Peskinefe119512018-07-08 21:39:34 +02003372psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3373 unsigned char *iv,
3374 size_t iv_size,
3375 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003376{
itayzafrir534bd7c2018-08-02 13:56:32 +03003377 psa_status_t status;
3378 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003379 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003380 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003381 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003382 }
Moran Peker41deec42018-04-04 15:43:05 +03003383 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003384 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003385 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003386 goto exit;
3387 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003388 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3389 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003390 if( ret != 0 )
3391 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003392 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003393 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003394 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003395
mohammad16038481e742018-03-18 13:57:31 +02003396 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003397 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003398
Moran Peker395db872018-05-31 14:07:14 +03003399exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003400 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003401 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003402 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003403}
3404
Gilles Peskinefe119512018-07-08 21:39:34 +02003405psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3406 const unsigned char *iv,
3407 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003408{
itayzafrir534bd7c2018-08-02 13:56:32 +03003409 psa_status_t status;
3410 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003411 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003412 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003413 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003414 }
Moran Pekera28258c2018-05-29 16:25:04 +03003415 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003416 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003417 status = PSA_ERROR_INVALID_ARGUMENT;
3418 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003419 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003420 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3421 status = mbedtls_to_psa_error( ret );
3422exit:
3423 if( status == PSA_SUCCESS )
3424 operation->iv_set = 1;
3425 else
mohammad1603503973b2018-03-12 15:59:30 +02003426 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003427 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003428}
3429
Gilles Peskinee553c652018-06-04 16:22:46 +02003430psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3431 const uint8_t *input,
3432 size_t input_length,
3433 unsigned char *output,
3434 size_t output_size,
3435 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003436{
itayzafrir534bd7c2018-08-02 13:56:32 +03003437 psa_status_t status;
3438 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003439 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003440
3441 if( operation->alg == 0 )
3442 {
3443 return( PSA_ERROR_BAD_STATE );
3444 }
3445
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003446 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003447 {
3448 /* Take the unprocessed partial block left over from previous
3449 * update calls, if any, plus the input to this call. Remove
3450 * the last partial block, if any. You get the data that will be
3451 * output in this call. */
3452 expected_output_size =
3453 ( operation->ctx.cipher.unprocessed_len + input_length )
3454 / operation->block_size * operation->block_size;
3455 }
3456 else
3457 {
3458 expected_output_size = input_length;
3459 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003460
Gilles Peskine89d789c2018-06-04 17:17:16 +02003461 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003462 {
3463 status = PSA_ERROR_BUFFER_TOO_SMALL;
3464 goto exit;
3465 }
mohammad160382759612018-03-12 18:16:40 +02003466
mohammad1603503973b2018-03-12 15:59:30 +02003467 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003468 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003469 status = mbedtls_to_psa_error( ret );
3470exit:
3471 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003472 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003473 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003474}
3475
Gilles Peskinee553c652018-06-04 16:22:46 +02003476psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3477 uint8_t *output,
3478 size_t output_size,
3479 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003480{
David Saadab4ecc272019-02-14 13:48:10 +02003481 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003482 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003483 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003484
mohammad1603503973b2018-03-12 15:59:30 +02003485 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003486 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003487 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003488 }
3489 if( operation->iv_required && ! operation->iv_set )
3490 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003491 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003492 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003493
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003494 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003495 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3496 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003497 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003498 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003499 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003500 }
3501
Janos Follath315b51c2018-07-09 16:04:51 +01003502 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3503 temp_output_buffer,
3504 output_length );
3505 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003506 {
Janos Follath315b51c2018-07-09 16:04:51 +01003507 status = mbedtls_to_psa_error( cipher_ret );
3508 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003509 }
Janos Follath315b51c2018-07-09 16:04:51 +01003510
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003511 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003512 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003513 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003514 memcpy( output, temp_output_buffer, *output_length );
3515 else
3516 {
Janos Follath315b51c2018-07-09 16:04:51 +01003517 status = PSA_ERROR_BUFFER_TOO_SMALL;
3518 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003519 }
mohammad1603503973b2018-03-12 15:59:30 +02003520
Gilles Peskine3f108122018-12-07 18:14:53 +01003521 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003522 status = psa_cipher_abort( operation );
3523
3524 return( status );
3525
3526error:
3527
3528 *output_length = 0;
3529
Gilles Peskine3f108122018-12-07 18:14:53 +01003530 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003531 (void) psa_cipher_abort( operation );
3532
3533 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003534}
3535
Gilles Peskinee553c652018-06-04 16:22:46 +02003536psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3537{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003538 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003539 {
3540 /* The object has (apparently) been initialized but it is not
3541 * in use. It's ok to call abort on such an object, and there's
3542 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003543 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003544 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003545
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003546 /* Sanity check (shouldn't happen: operation->alg should
3547 * always have been initialized to a valid value). */
3548 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3549 return( PSA_ERROR_BAD_STATE );
3550
mohammad1603503973b2018-03-12 15:59:30 +02003551 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003552
Moran Peker41deec42018-04-04 15:43:05 +03003553 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003554 operation->key_set = 0;
3555 operation->iv_set = 0;
3556 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003557 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003558 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003559
Moran Peker395db872018-05-31 14:07:14 +03003560 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003561}
3562
Gilles Peskinea0655c32018-04-30 17:06:50 +02003563
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003564
mohammad16038cc1cee2018-03-28 01:21:33 +03003565/****************************************************************/
3566/* Key Policy */
3567/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003568
mohammad160327010052018-07-03 13:16:15 +03003569#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003570void psa_key_policy_set_usage( psa_key_policy_t *policy,
3571 psa_key_usage_t usage,
3572 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003573{
mohammad16034eed7572018-03-28 05:14:59 -07003574 policy->usage = usage;
3575 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003576}
3577
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003578psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003579{
mohammad16036df908f2018-04-02 08:34:15 -07003580 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003581}
3582
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003583psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003584{
mohammad16036df908f2018-04-02 08:34:15 -07003585 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003586}
mohammad160327010052018-07-03 13:16:15 +03003587#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003588
Gilles Peskinec5487a82018-12-03 18:08:14 +01003589psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003590 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003591{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003592 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003593 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003594
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003595 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003596 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003597
Gilles Peskinec5487a82018-12-03 18:08:14 +01003598 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003599 if( status != PSA_SUCCESS )
3600 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003601
Gilles Peskine4747d192019-04-17 15:05:45 +02003602 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003603}
3604
Gilles Peskinec5487a82018-12-03 18:08:14 +01003605psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003606 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003607{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003608 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003609 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003610
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003611 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003612 return( PSA_ERROR_INVALID_ARGUMENT );
3613
Gilles Peskinec5487a82018-12-03 18:08:14 +01003614 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003615 if( status != PSA_SUCCESS )
3616 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003617
mohammad16036df908f2018-04-02 08:34:15 -07003618 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003619
3620 return( PSA_SUCCESS );
3621}
Gilles Peskine20035e32018-02-03 22:44:14 +01003622
Gilles Peskinea0655c32018-04-30 17:06:50 +02003623
3624
mohammad1603804cd712018-03-20 22:44:08 +02003625/****************************************************************/
3626/* Key Lifetime */
3627/****************************************************************/
3628
Gilles Peskine87a5e562019-04-17 12:28:25 +02003629psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003630 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003631{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003632 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003633 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003634
Gilles Peskinec5487a82018-12-03 18:08:14 +01003635 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003636 if( status != PSA_SUCCESS )
3637 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003638
mohammad1603804cd712018-03-20 22:44:08 +02003639 *lifetime = slot->lifetime;
3640
3641 return( PSA_SUCCESS );
3642}
3643
Gilles Peskine20035e32018-02-03 22:44:14 +01003644
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003645
mohammad16035955c982018-04-26 00:53:03 +03003646/****************************************************************/
3647/* AEAD */
3648/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003649
Gilles Peskineedf9a652018-08-17 18:11:56 +02003650typedef struct
3651{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003652 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003653 const mbedtls_cipher_info_t *cipher_info;
3654 union
3655 {
3656#if defined(MBEDTLS_CCM_C)
3657 mbedtls_ccm_context ccm;
3658#endif /* MBEDTLS_CCM_C */
3659#if defined(MBEDTLS_GCM_C)
3660 mbedtls_gcm_context gcm;
3661#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003662#if defined(MBEDTLS_CHACHAPOLY_C)
3663 mbedtls_chachapoly_context chachapoly;
3664#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003665 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003666 psa_algorithm_t core_alg;
3667 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003668 uint8_t tag_length;
3669} aead_operation_t;
3670
Gilles Peskine30a9e412019-01-14 18:36:12 +01003671static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003672{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003673 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003674 {
3675#if defined(MBEDTLS_CCM_C)
3676 case PSA_ALG_CCM:
3677 mbedtls_ccm_free( &operation->ctx.ccm );
3678 break;
3679#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003680#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003681 case PSA_ALG_GCM:
3682 mbedtls_gcm_free( &operation->ctx.gcm );
3683 break;
3684#endif /* MBEDTLS_GCM_C */
3685 }
3686}
3687
3688static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003689 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003690 psa_key_usage_t usage,
3691 psa_algorithm_t alg )
3692{
3693 psa_status_t status;
3694 size_t key_bits;
3695 mbedtls_cipher_id_t cipher_id;
3696
Gilles Peskinec5487a82018-12-03 18:08:14 +01003697 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003698 if( status != PSA_SUCCESS )
3699 return( status );
3700
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003701 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003702
3703 operation->cipher_info =
3704 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3705 &cipher_id );
3706 if( operation->cipher_info == NULL )
3707 return( PSA_ERROR_NOT_SUPPORTED );
3708
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003709 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003710 {
3711#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003712 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3713 operation->core_alg = PSA_ALG_CCM;
3714 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003715 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3716 * The call to mbedtls_ccm_encrypt_and_tag or
3717 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003718 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3719 return( PSA_ERROR_INVALID_ARGUMENT );
3720 mbedtls_ccm_init( &operation->ctx.ccm );
3721 status = mbedtls_to_psa_error(
3722 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3723 operation->slot->data.raw.data,
3724 (unsigned int) key_bits ) );
3725 if( status != 0 )
3726 goto cleanup;
3727 break;
3728#endif /* MBEDTLS_CCM_C */
3729
3730#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003731 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3732 operation->core_alg = PSA_ALG_GCM;
3733 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003734 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3735 * The call to mbedtls_gcm_crypt_and_tag or
3736 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003737 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 mbedtls_gcm_init( &operation->ctx.gcm );
3740 status = mbedtls_to_psa_error(
3741 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3742 operation->slot->data.raw.data,
3743 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003744 if( status != 0 )
3745 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003746 break;
3747#endif /* MBEDTLS_GCM_C */
3748
Gilles Peskine26869f22019-05-06 15:25:00 +02003749#if defined(MBEDTLS_CHACHAPOLY_C)
3750 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3751 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3752 operation->full_tag_length = 16;
3753 /* We only support the default tag length. */
3754 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3755 return( PSA_ERROR_NOT_SUPPORTED );
3756 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3757 status = mbedtls_to_psa_error(
3758 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3759 operation->slot->data.raw.data ) );
3760 if( status != 0 )
3761 goto cleanup;
3762 break;
3763#endif /* MBEDTLS_CHACHAPOLY_C */
3764
Gilles Peskineedf9a652018-08-17 18:11:56 +02003765 default:
3766 return( PSA_ERROR_NOT_SUPPORTED );
3767 }
3768
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003769 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3770 {
3771 status = PSA_ERROR_INVALID_ARGUMENT;
3772 goto cleanup;
3773 }
3774 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003775
Gilles Peskineedf9a652018-08-17 18:11:56 +02003776 return( PSA_SUCCESS );
3777
3778cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003779 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780 return( status );
3781}
3782
Gilles Peskinec5487a82018-12-03 18:08:14 +01003783psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003784 psa_algorithm_t alg,
3785 const uint8_t *nonce,
3786 size_t nonce_length,
3787 const uint8_t *additional_data,
3788 size_t additional_data_length,
3789 const uint8_t *plaintext,
3790 size_t plaintext_length,
3791 uint8_t *ciphertext,
3792 size_t ciphertext_size,
3793 size_t *ciphertext_length )
3794{
mohammad16035955c982018-04-26 00:53:03 +03003795 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003796 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003797 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003798
mohammad1603f08a5502018-06-03 15:05:47 +03003799 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003800
Gilles Peskinec5487a82018-12-03 18:08:14 +01003801 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003802 if( status != PSA_SUCCESS )
3803 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003804
Gilles Peskineedf9a652018-08-17 18:11:56 +02003805 /* For all currently supported modes, the tag is at the end of the
3806 * ciphertext. */
3807 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3808 {
3809 status = PSA_ERROR_BUFFER_TOO_SMALL;
3810 goto exit;
3811 }
3812 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003813
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003814#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003815 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003816 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003817 status = mbedtls_to_psa_error(
3818 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3819 MBEDTLS_GCM_ENCRYPT,
3820 plaintext_length,
3821 nonce, nonce_length,
3822 additional_data, additional_data_length,
3823 plaintext, ciphertext,
3824 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003825 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003826 else
3827#endif /* MBEDTLS_GCM_C */
3828#if defined(MBEDTLS_CCM_C)
3829 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003830 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003831 status = mbedtls_to_psa_error(
3832 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3833 plaintext_length,
3834 nonce, nonce_length,
3835 additional_data,
3836 additional_data_length,
3837 plaintext, ciphertext,
3838 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003839 }
mohammad16035c8845f2018-05-09 05:40:09 -07003840 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003841#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003842#if defined(MBEDTLS_CHACHAPOLY_C)
3843 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3844 {
3845 if( nonce_length != 12 || operation.tag_length != 16 )
3846 {
3847 status = PSA_ERROR_NOT_SUPPORTED;
3848 goto exit;
3849 }
3850 status = mbedtls_to_psa_error(
3851 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3852 plaintext_length,
3853 nonce,
3854 additional_data,
3855 additional_data_length,
3856 plaintext,
3857 ciphertext,
3858 tag ) );
3859 }
3860 else
3861#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003862 {
mohammad1603554faad2018-06-03 15:07:38 +03003863 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003864 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003865
Gilles Peskineedf9a652018-08-17 18:11:56 +02003866 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3867 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003868
Gilles Peskineedf9a652018-08-17 18:11:56 +02003869exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003870 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003871 if( status == PSA_SUCCESS )
3872 *ciphertext_length = plaintext_length + operation.tag_length;
3873 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003874}
3875
Gilles Peskineee652a32018-06-01 19:23:52 +02003876/* Locate the tag in a ciphertext buffer containing the encrypted data
3877 * followed by the tag. Return the length of the part preceding the tag in
3878 * *plaintext_length. This is the size of the plaintext in modes where
3879 * the encrypted data has the same size as the plaintext, such as
3880 * CCM and GCM. */
3881static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3882 const uint8_t *ciphertext,
3883 size_t ciphertext_length,
3884 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003885 const uint8_t **p_tag )
3886{
3887 size_t payload_length;
3888 if( tag_length > ciphertext_length )
3889 return( PSA_ERROR_INVALID_ARGUMENT );
3890 payload_length = ciphertext_length - tag_length;
3891 if( payload_length > plaintext_size )
3892 return( PSA_ERROR_BUFFER_TOO_SMALL );
3893 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003894 return( PSA_SUCCESS );
3895}
3896
Gilles Peskinec5487a82018-12-03 18:08:14 +01003897psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003898 psa_algorithm_t alg,
3899 const uint8_t *nonce,
3900 size_t nonce_length,
3901 const uint8_t *additional_data,
3902 size_t additional_data_length,
3903 const uint8_t *ciphertext,
3904 size_t ciphertext_length,
3905 uint8_t *plaintext,
3906 size_t plaintext_size,
3907 size_t *plaintext_length )
3908{
mohammad16035955c982018-04-26 00:53:03 +03003909 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003910 aead_operation_t operation;
3911 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003912
Gilles Peskineee652a32018-06-01 19:23:52 +02003913 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003914
Gilles Peskinec5487a82018-12-03 18:08:14 +01003915 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003916 if( status != PSA_SUCCESS )
3917 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003918
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003919 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3920 ciphertext, ciphertext_length,
3921 plaintext_size, &tag );
3922 if( status != PSA_SUCCESS )
3923 goto exit;
3924
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003925#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003926 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003927 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003928 status = mbedtls_to_psa_error(
3929 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3930 ciphertext_length - operation.tag_length,
3931 nonce, nonce_length,
3932 additional_data,
3933 additional_data_length,
3934 tag, operation.tag_length,
3935 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003936 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003937 else
3938#endif /* MBEDTLS_GCM_C */
3939#if defined(MBEDTLS_CCM_C)
3940 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003941 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003942 status = mbedtls_to_psa_error(
3943 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3944 ciphertext_length - operation.tag_length,
3945 nonce, nonce_length,
3946 additional_data,
3947 additional_data_length,
3948 ciphertext, plaintext,
3949 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003950 }
mohammad160339574652018-06-01 04:39:53 -07003951 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003952#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003953#if defined(MBEDTLS_CHACHAPOLY_C)
3954 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3955 {
3956 if( nonce_length != 12 || operation.tag_length != 16 )
3957 {
3958 status = PSA_ERROR_NOT_SUPPORTED;
3959 goto exit;
3960 }
3961 status = mbedtls_to_psa_error(
3962 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3963 ciphertext_length - operation.tag_length,
3964 nonce,
3965 additional_data,
3966 additional_data_length,
3967 tag,
3968 ciphertext,
3969 plaintext ) );
3970 }
3971 else
3972#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003973 {
mohammad1603554faad2018-06-03 15:07:38 +03003974 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003975 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003976
Gilles Peskineedf9a652018-08-17 18:11:56 +02003977 if( status != PSA_SUCCESS && plaintext_size != 0 )
3978 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003979
Gilles Peskineedf9a652018-08-17 18:11:56 +02003980exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003981 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003982 if( status == PSA_SUCCESS )
3983 *plaintext_length = ciphertext_length - operation.tag_length;
3984 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003985}
3986
Gilles Peskinea0655c32018-04-30 17:06:50 +02003987
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003988
Gilles Peskine20035e32018-02-03 22:44:14 +01003989/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003990/* Generators */
3991/****************************************************************/
3992
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003993#define HKDF_STATE_INIT 0 /* no input yet */
3994#define HKDF_STATE_STARTED 1 /* got salt */
3995#define HKDF_STATE_KEYED 2 /* got key */
3996#define HKDF_STATE_OUTPUT 3 /* output started */
3997
Gilles Peskine969c5d62019-01-16 15:53:06 +01003998static psa_algorithm_t psa_generator_get_kdf_alg(
3999 const psa_crypto_generator_t *generator )
4000{
4001 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
4002 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
4003 else
4004 return( generator->alg );
4005}
4006
4007
Gilles Peskineeab56e42018-07-12 17:12:33 +02004008psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
4009{
4010 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004011 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
4012 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004013 {
4014 /* The object has (apparently) been initialized but it is not
4015 * in use. It's ok to call abort on such an object, and there's
4016 * nothing to do. */
4017 }
4018 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01004019 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004020 {
4021 if( generator->ctx.buffer.data != NULL )
4022 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004023 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02004024 generator->ctx.buffer.size );
4025 mbedtls_free( generator->ctx.buffer.data );
4026 }
4027 }
4028 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004029#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004030 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004031 {
4032 mbedtls_free( generator->ctx.hkdf.info );
4033 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
4034 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004035 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00004036 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004037 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004038 {
4039 if( generator->ctx.tls12_prf.key != NULL )
4040 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004041 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004042 generator->ctx.tls12_prf.key_len );
4043 mbedtls_free( generator->ctx.tls12_prf.key );
4044 }
Hanno Becker580fba12018-11-13 20:50:45 +00004045
4046 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
4047 {
Gilles Peskine3f108122018-12-07 18:14:53 +01004048 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004049 generator->ctx.tls12_prf.Ai_with_seed_len );
4050 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
4051 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004052 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004053 else
4054#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004055 {
4056 status = PSA_ERROR_BAD_STATE;
4057 }
4058 memset( generator, 0, sizeof( *generator ) );
4059 return( status );
4060}
4061
Gilles Peskineeab56e42018-07-12 17:12:33 +02004062psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
4063 size_t *capacity)
4064{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004065 if( generator->alg == 0 )
4066 {
4067 /* This is a blank generator. */
4068 return PSA_ERROR_BAD_STATE;
4069 }
4070
Gilles Peskineeab56e42018-07-12 17:12:33 +02004071 *capacity = generator->capacity;
4072 return( PSA_SUCCESS );
4073}
4074
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004075psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
4076 size_t capacity )
4077{
4078 if( generator->alg == 0 )
4079 return( PSA_ERROR_BAD_STATE );
4080 if( capacity > generator->capacity )
4081 return( PSA_ERROR_INVALID_ARGUMENT );
4082 generator->capacity = capacity;
4083 return( PSA_SUCCESS );
4084}
4085
Gilles Peskinebef7f142018-07-12 17:22:21 +02004086#if defined(MBEDTLS_MD_C)
4087/* Read some bytes from an HKDF-based generator. This performs a chunk
4088 * of the expand phase of the HKDF algorithm. */
4089static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
4090 psa_algorithm_t hash_alg,
4091 uint8_t *output,
4092 size_t output_length )
4093{
4094 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4095 psa_status_t status;
4096
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004097 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4098 return( PSA_ERROR_BAD_STATE );
4099 hkdf->state = HKDF_STATE_OUTPUT;
4100
Gilles Peskinebef7f142018-07-12 17:22:21 +02004101 while( output_length != 0 )
4102 {
4103 /* Copy what remains of the current block */
4104 uint8_t n = hash_length - hkdf->offset_in_block;
4105 if( n > output_length )
4106 n = (uint8_t) output_length;
4107 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4108 output += n;
4109 output_length -= n;
4110 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004111 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004112 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004113 /* We can't be wanting more output after block 0xff, otherwise
4114 * the capacity check in psa_generator_read() would have
4115 * prevented this call. It could happen only if the generator
4116 * object was corrupted or if this function is called directly
4117 * inside the library. */
4118 if( hkdf->block_number == 0xff )
4119 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004120
4121 /* We need a new block */
4122 ++hkdf->block_number;
4123 hkdf->offset_in_block = 0;
4124 status = psa_hmac_setup_internal( &hkdf->hmac,
4125 hkdf->prk, hash_length,
4126 hash_alg );
4127 if( status != PSA_SUCCESS )
4128 return( status );
4129 if( hkdf->block_number != 1 )
4130 {
4131 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4132 hkdf->output_block,
4133 hash_length );
4134 if( status != PSA_SUCCESS )
4135 return( status );
4136 }
4137 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4138 hkdf->info,
4139 hkdf->info_length );
4140 if( status != PSA_SUCCESS )
4141 return( status );
4142 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4143 &hkdf->block_number, 1 );
4144 if( status != PSA_SUCCESS )
4145 return( status );
4146 status = psa_hmac_finish_internal( &hkdf->hmac,
4147 hkdf->output_block,
4148 sizeof( hkdf->output_block ) );
4149 if( status != PSA_SUCCESS )
4150 return( status );
4151 }
4152
4153 return( PSA_SUCCESS );
4154}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004155
4156static psa_status_t psa_generator_tls12_prf_generate_next_block(
4157 psa_tls12_prf_generator_t *tls12_prf,
4158 psa_algorithm_t alg )
4159{
4160 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4161 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4162 psa_hmac_internal_data hmac;
4163 psa_status_t status, cleanup_status;
4164
Hanno Becker3b339e22018-11-13 20:56:14 +00004165 unsigned char *Ai;
4166 size_t Ai_len;
4167
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004168 /* We can't be wanting more output after block 0xff, otherwise
4169 * the capacity check in psa_generator_read() would have
4170 * prevented this call. It could happen only if the generator
4171 * object was corrupted or if this function is called directly
4172 * inside the library. */
4173 if( tls12_prf->block_number == 0xff )
4174 return( PSA_ERROR_BAD_STATE );
4175
4176 /* We need a new block */
4177 ++tls12_prf->block_number;
4178 tls12_prf->offset_in_block = 0;
4179
4180 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4181 *
4182 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4183 *
4184 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4185 * HMAC_hash(secret, A(2) + seed) +
4186 * HMAC_hash(secret, A(3) + seed) + ...
4187 *
4188 * A(0) = seed
4189 * A(i) = HMAC_hash( secret, A(i-1) )
4190 *
4191 * The `psa_tls12_prf_generator` structures saves the block
4192 * `HMAC_hash(secret, A(i) + seed)` from which the output
4193 * is currently extracted as `output_block`, while
4194 * `A(i) + seed` is stored in `Ai_with_seed`.
4195 *
4196 * Generating a new block means recalculating `Ai_with_seed`
4197 * from the A(i)-part of it, and afterwards recalculating
4198 * `output_block`.
4199 *
4200 * A(0) is computed at setup time.
4201 *
4202 */
4203
4204 psa_hmac_init_internal( &hmac );
4205
4206 /* We must distinguish the calculation of A(1) from those
4207 * of A(2) and higher, because A(0)=seed has a different
4208 * length than the other A(i). */
4209 if( tls12_prf->block_number == 1 )
4210 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004211 Ai = tls12_prf->Ai_with_seed + hash_length;
4212 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004213 }
4214 else
4215 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004216 Ai = tls12_prf->Ai_with_seed;
4217 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004218 }
4219
Hanno Becker3b339e22018-11-13 20:56:14 +00004220 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4221 status = psa_hmac_setup_internal( &hmac,
4222 tls12_prf->key,
4223 tls12_prf->key_len,
4224 hash_alg );
4225 if( status != PSA_SUCCESS )
4226 goto cleanup;
4227
4228 status = psa_hash_update( &hmac.hash_ctx,
4229 Ai, Ai_len );
4230 if( status != PSA_SUCCESS )
4231 goto cleanup;
4232
4233 status = psa_hmac_finish_internal( &hmac,
4234 tls12_prf->Ai_with_seed,
4235 hash_length );
4236 if( status != PSA_SUCCESS )
4237 goto cleanup;
4238
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004239 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4240 status = psa_hmac_setup_internal( &hmac,
4241 tls12_prf->key,
4242 tls12_prf->key_len,
4243 hash_alg );
4244 if( status != PSA_SUCCESS )
4245 goto cleanup;
4246
4247 status = psa_hash_update( &hmac.hash_ctx,
4248 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004249 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004250 if( status != PSA_SUCCESS )
4251 goto cleanup;
4252
4253 status = psa_hmac_finish_internal( &hmac,
4254 tls12_prf->output_block,
4255 hash_length );
4256 if( status != PSA_SUCCESS )
4257 goto cleanup;
4258
4259cleanup:
4260
4261 cleanup_status = psa_hmac_abort_internal( &hmac );
4262 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4263 status = cleanup_status;
4264
4265 return( status );
4266}
4267
4268/* Read some bytes from an TLS-1.2-PRF-based generator.
4269 * See Section 5 of RFC 5246. */
4270static psa_status_t psa_generator_tls12_prf_read(
4271 psa_tls12_prf_generator_t *tls12_prf,
4272 psa_algorithm_t alg,
4273 uint8_t *output,
4274 size_t output_length )
4275{
4276 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4277 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4278 psa_status_t status;
4279
4280 while( output_length != 0 )
4281 {
4282 /* Copy what remains of the current block */
4283 uint8_t n = hash_length - tls12_prf->offset_in_block;
4284
4285 /* Check if we have fully processed the current block. */
4286 if( n == 0 )
4287 {
4288 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4289 alg );
4290 if( status != PSA_SUCCESS )
4291 return( status );
4292
4293 continue;
4294 }
4295
4296 if( n > output_length )
4297 n = (uint8_t) output_length;
4298 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4299 n );
4300 output += n;
4301 output_length -= n;
4302 tls12_prf->offset_in_block += n;
4303 }
4304
4305 return( PSA_SUCCESS );
4306}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004307#endif /* MBEDTLS_MD_C */
4308
Gilles Peskineeab56e42018-07-12 17:12:33 +02004309psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4310 uint8_t *output,
4311 size_t output_length )
4312{
4313 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004314 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004315
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004316 if( generator->alg == 0 )
4317 {
4318 /* This is a blank generator. */
4319 return PSA_ERROR_BAD_STATE;
4320 }
4321
Gilles Peskineeab56e42018-07-12 17:12:33 +02004322 if( output_length > generator->capacity )
4323 {
4324 generator->capacity = 0;
4325 /* Go through the error path to wipe all confidential data now
4326 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004327 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004328 goto exit;
4329 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004330 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004331 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004332 /* Edge case: this is a finished generator, and 0 bytes
4333 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004334 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4335 * INSUFFICIENT_CAPACITY, which is right for a finished
4336 * generator, for consistency with the case when
4337 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004338 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004339 }
4340 generator->capacity -= output_length;
4341
Gilles Peskine969c5d62019-01-16 15:53:06 +01004342 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004343 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004344 /* Initially, the capacity of a selection generator is always
4345 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4346 * abbreviated in this comment as `size`. When the remaining
4347 * capacity is `c`, the next bytes to serve start `c` bytes
4348 * from the end of the buffer, i.e. `size - c` from the
4349 * beginning of the buffer. Since `generator->capacity` was just
4350 * decremented above, we need to serve the bytes from
4351 * `size - generator->capacity - output_length` to
4352 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004353 size_t offset =
4354 generator->ctx.buffer.size - generator->capacity - output_length;
4355 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4356 status = PSA_SUCCESS;
4357 }
4358 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004359#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004360 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004361 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004362 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004363 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4364 output, output_length );
4365 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004366 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4367 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004368 {
4369 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004370 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004371 output_length );
4372 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004373 else
4374#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004375 {
4376 return( PSA_ERROR_BAD_STATE );
4377 }
4378
4379exit:
4380 if( status != PSA_SUCCESS )
4381 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004382 /* Preserve the algorithm upon errors, but clear all sensitive state.
4383 * This allows us to differentiate between exhausted generators and
4384 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4385 * generators. */
4386 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004387 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004388 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004389 memset( output, '!', output_length );
4390 }
4391 return( status );
4392}
4393
Gilles Peskine08542d82018-07-19 17:05:42 +02004394#if defined(MBEDTLS_DES_C)
4395static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4396{
4397 if( data_size >= 8 )
4398 mbedtls_des_key_set_parity( data );
4399 if( data_size >= 16 )
4400 mbedtls_des_key_set_parity( data + 8 );
4401 if( data_size >= 24 )
4402 mbedtls_des_key_set_parity( data + 16 );
4403}
4404#endif /* MBEDTLS_DES_C */
4405
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004406static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004407 psa_key_slot_t *slot,
4408 size_t bits,
4409 psa_crypto_generator_t *generator )
4410{
4411 uint8_t *data = NULL;
4412 size_t bytes = PSA_BITS_TO_BYTES( bits );
4413 psa_status_t status;
4414
4415 if( ! key_type_is_raw_bytes( slot->type ) )
4416 return( PSA_ERROR_INVALID_ARGUMENT );
4417 if( bits % 8 != 0 )
4418 return( PSA_ERROR_INVALID_ARGUMENT );
4419 data = mbedtls_calloc( 1, bytes );
4420 if( data == NULL )
4421 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4422
4423 status = psa_generator_read( generator, data, bytes );
4424 if( status != PSA_SUCCESS )
4425 goto exit;
4426#if defined(MBEDTLS_DES_C)
4427 if( slot->type == PSA_KEY_TYPE_DES )
4428 psa_des_set_key_parity( data, bytes );
4429#endif /* MBEDTLS_DES_C */
4430 status = psa_import_key_into_slot( slot, data, bytes );
4431
4432exit:
4433 mbedtls_free( data );
4434 return( status );
4435}
4436
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004437psa_status_t psa_generate_derived_key( const psa_key_attributes_t *attributes,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004438 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004439 psa_crypto_generator_t *generator )
4440{
4441 psa_status_t status;
4442 psa_key_slot_t *slot = NULL;
4443 status = psa_start_key_creation( attributes, handle, &slot );
4444 if( status == PSA_SUCCESS )
4445 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004446 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004447 attributes->bits,
4448 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004449 }
4450 if( status == PSA_SUCCESS )
4451 status = psa_finish_key_creation( slot );
4452 if( status != PSA_SUCCESS )
4453 {
4454 psa_fail_key_creation( slot );
4455 *handle = 0;
4456 }
4457 return( status );
4458}
4459
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004460psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004461 psa_key_type_t type,
4462 size_t bits,
4463 psa_crypto_generator_t *generator )
4464{
4465 uint8_t *data = NULL;
4466 size_t bytes = PSA_BITS_TO_BYTES( bits );
4467 psa_status_t status;
4468
4469 if( ! key_type_is_raw_bytes( type ) )
4470 return( PSA_ERROR_INVALID_ARGUMENT );
4471 if( bits % 8 != 0 )
4472 return( PSA_ERROR_INVALID_ARGUMENT );
4473 data = mbedtls_calloc( 1, bytes );
4474 if( data == NULL )
4475 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4476
4477 status = psa_generator_read( generator, data, bytes );
4478 if( status != PSA_SUCCESS )
4479 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004480#if defined(MBEDTLS_DES_C)
4481 if( type == PSA_KEY_TYPE_DES )
4482 psa_des_set_key_parity( data, bytes );
4483#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004484 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004485
4486exit:
4487 mbedtls_free( data );
4488 return( status );
4489}
4490
4491
4492
4493/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004494/* Key derivation */
4495/****************************************************************/
4496
Gilles Peskinea05219c2018-11-16 16:02:56 +01004497#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004498/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004499 * of the HKDF algorithm.
4500 *
4501 * Note that if this function fails, you must call psa_generator_abort()
4502 * to potentially free embedded data structures and wipe confidential data.
4503 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004504static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004505 const uint8_t *secret,
4506 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004507 psa_algorithm_t hash_alg,
4508 const uint8_t *salt,
4509 size_t salt_length,
4510 const uint8_t *label,
4511 size_t label_length )
4512{
4513 psa_status_t status;
4514 status = psa_hmac_setup_internal( &hkdf->hmac,
4515 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004516 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004517 if( status != PSA_SUCCESS )
4518 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004519 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004520 if( status != PSA_SUCCESS )
4521 return( status );
4522 status = psa_hmac_finish_internal( &hkdf->hmac,
4523 hkdf->prk,
4524 sizeof( hkdf->prk ) );
4525 if( status != PSA_SUCCESS )
4526 return( status );
4527 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4528 hkdf->block_number = 0;
4529 hkdf->info_length = label_length;
4530 if( label_length != 0 )
4531 {
4532 hkdf->info = mbedtls_calloc( 1, label_length );
4533 if( hkdf->info == NULL )
4534 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4535 memcpy( hkdf->info, label, label_length );
4536 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004537 hkdf->state = HKDF_STATE_KEYED;
4538 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004539 return( PSA_SUCCESS );
4540}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004541#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004542
Gilles Peskinea05219c2018-11-16 16:02:56 +01004543#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004544/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4545 *
4546 * Note that if this function fails, you must call psa_generator_abort()
4547 * to potentially free embedded data structures and wipe confidential data.
4548 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004549static psa_status_t psa_generator_tls12_prf_setup(
4550 psa_tls12_prf_generator_t *tls12_prf,
4551 const unsigned char *key,
4552 size_t key_len,
4553 psa_algorithm_t hash_alg,
4554 const uint8_t *salt,
4555 size_t salt_length,
4556 const uint8_t *label,
4557 size_t label_length )
4558{
4559 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004560 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4561 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004562
4563 tls12_prf->key = mbedtls_calloc( 1, key_len );
4564 if( tls12_prf->key == NULL )
4565 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4566 tls12_prf->key_len = key_len;
4567 memcpy( tls12_prf->key, key, key_len );
4568
Hanno Becker580fba12018-11-13 20:50:45 +00004569 overflow = ( salt_length + label_length < salt_length ) ||
4570 ( salt_length + label_length + hash_length < hash_length );
4571 if( overflow )
4572 return( PSA_ERROR_INVALID_ARGUMENT );
4573
4574 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4575 if( tls12_prf->Ai_with_seed == NULL )
4576 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4577 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4578
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004579 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4580 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004581 if( label_length != 0 )
4582 {
4583 memcpy( tls12_prf->Ai_with_seed + hash_length,
4584 label, label_length );
4585 }
4586
4587 if( salt_length != 0 )
4588 {
4589 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4590 salt, salt_length );
4591 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004592
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004593 /* The first block gets generated when
4594 * psa_generator_read() is called. */
4595 tls12_prf->block_number = 0;
4596 tls12_prf->offset_in_block = hash_length;
4597
4598 return( PSA_SUCCESS );
4599}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004600
4601/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4602static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4603 psa_tls12_prf_generator_t *tls12_prf,
4604 const unsigned char *psk,
4605 size_t psk_len,
4606 psa_algorithm_t hash_alg,
4607 const uint8_t *salt,
4608 size_t salt_length,
4609 const uint8_t *label,
4610 size_t label_length )
4611{
4612 psa_status_t status;
4613 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4614
4615 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4616 return( PSA_ERROR_INVALID_ARGUMENT );
4617
4618 /* Quoting RFC 4279, Section 2:
4619 *
4620 * The premaster secret is formed as follows: if the PSK is N octets
4621 * long, concatenate a uint16 with the value N, N zero octets, a second
4622 * uint16 with the value N, and the PSK itself.
4623 */
4624
4625 pms[0] = ( psk_len >> 8 ) & 0xff;
4626 pms[1] = ( psk_len >> 0 ) & 0xff;
4627 memset( pms + 2, 0, psk_len );
4628 pms[2 + psk_len + 0] = pms[0];
4629 pms[2 + psk_len + 1] = pms[1];
4630 memcpy( pms + 4 + psk_len, psk, psk_len );
4631
4632 status = psa_generator_tls12_prf_setup( tls12_prf,
4633 pms, 4 + 2 * psk_len,
4634 hash_alg,
4635 salt, salt_length,
4636 label, label_length );
4637
Gilles Peskine3f108122018-12-07 18:14:53 +01004638 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004639 return( status );
4640}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004641#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004642
Gilles Peskine346797d2018-11-16 16:05:06 +01004643/* Note that if this function fails, you must call psa_generator_abort()
4644 * to potentially free embedded data structures and wipe confidential data.
4645 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004646static psa_status_t psa_key_derivation_internal(
4647 psa_crypto_generator_t *generator,
4648 const uint8_t *secret, size_t secret_length,
4649 psa_algorithm_t alg,
4650 const uint8_t *salt, size_t salt_length,
4651 const uint8_t *label, size_t label_length,
4652 size_t capacity )
4653{
4654 psa_status_t status;
4655 size_t max_capacity;
4656
4657 /* Set generator->alg even on failure so that abort knows what to do. */
4658 generator->alg = alg;
4659
Gilles Peskine751d9652018-09-18 12:05:44 +02004660 if( alg == PSA_ALG_SELECT_RAW )
4661 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004662 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004663 if( salt_length != 0 )
4664 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004665 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004666 if( label_length != 0 )
4667 return( PSA_ERROR_INVALID_ARGUMENT );
4668 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4669 if( generator->ctx.buffer.data == NULL )
4670 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4671 memcpy( generator->ctx.buffer.data, secret, secret_length );
4672 generator->ctx.buffer.size = secret_length;
4673 max_capacity = secret_length;
4674 status = PSA_SUCCESS;
4675 }
4676 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004677#if defined(MBEDTLS_MD_C)
4678 if( PSA_ALG_IS_HKDF( alg ) )
4679 {
4680 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4681 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4682 if( hash_size == 0 )
4683 return( PSA_ERROR_NOT_SUPPORTED );
4684 max_capacity = 255 * hash_size;
4685 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4686 secret, secret_length,
4687 hash_alg,
4688 salt, salt_length,
4689 label, label_length );
4690 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004691 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4692 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4693 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004694 {
4695 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4696 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4697
4698 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4699 if( hash_alg != PSA_ALG_SHA_256 &&
4700 hash_alg != PSA_ALG_SHA_384 )
4701 {
4702 return( PSA_ERROR_NOT_SUPPORTED );
4703 }
4704
4705 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004706
4707 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4708 {
4709 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4710 secret, secret_length,
4711 hash_alg, salt, salt_length,
4712 label, label_length );
4713 }
4714 else
4715 {
4716 status = psa_generator_tls12_psk_to_ms_setup(
4717 &generator->ctx.tls12_prf,
4718 secret, secret_length,
4719 hash_alg, salt, salt_length,
4720 label, label_length );
4721 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004722 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004723 else
4724#endif
4725 {
4726 return( PSA_ERROR_NOT_SUPPORTED );
4727 }
4728
4729 if( status != PSA_SUCCESS )
4730 return( status );
4731
4732 if( capacity <= max_capacity )
4733 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004734 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4735 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004736 else
4737 return( PSA_ERROR_INVALID_ARGUMENT );
4738
4739 return( PSA_SUCCESS );
4740}
4741
Gilles Peskineea0fb492018-07-12 17:17:20 +02004742psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004743 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004744 psa_algorithm_t alg,
4745 const uint8_t *salt,
4746 size_t salt_length,
4747 const uint8_t *label,
4748 size_t label_length,
4749 size_t capacity )
4750{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004751 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004752 psa_status_t status;
4753
4754 if( generator->alg != 0 )
4755 return( PSA_ERROR_BAD_STATE );
4756
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004757 /* Make sure that alg is a key derivation algorithm. This prevents
4758 * key selection algorithms, which psa_key_derivation_internal
4759 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004760 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4761 return( PSA_ERROR_INVALID_ARGUMENT );
4762
Gilles Peskinec5487a82018-12-03 18:08:14 +01004763 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004764 if( status != PSA_SUCCESS )
4765 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004766
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004767 if( slot->type != PSA_KEY_TYPE_DERIVE )
4768 return( PSA_ERROR_INVALID_ARGUMENT );
4769
4770 status = psa_key_derivation_internal( generator,
4771 slot->data.raw.data,
4772 slot->data.raw.bytes,
4773 alg,
4774 salt, salt_length,
4775 label, label_length,
4776 capacity );
4777 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004778 psa_generator_abort( generator );
4779 return( status );
4780}
4781
Gilles Peskine969c5d62019-01-16 15:53:06 +01004782static psa_status_t psa_key_derivation_setup_kdf(
4783 psa_crypto_generator_t *generator,
4784 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004785{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004786 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004787#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004788 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4789 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4790 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004791 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004792 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004793 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4794 if( hash_size == 0 )
4795 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004796 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4797 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004798 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004799 {
4800 return( PSA_ERROR_NOT_SUPPORTED );
4801 }
4802 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004803 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004804 }
4805#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004806 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004807 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004808}
4809
4810psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4811 psa_algorithm_t alg )
4812{
4813 psa_status_t status;
4814
4815 if( generator->alg != 0 )
4816 return( PSA_ERROR_BAD_STATE );
4817
Gilles Peskine6843c292019-01-18 16:44:49 +01004818 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4819 return( PSA_ERROR_INVALID_ARGUMENT );
4820 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 {
4822 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004823 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004824 }
4825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4826 {
4827 status = psa_key_derivation_setup_kdf( generator, alg );
4828 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 else
4830 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831
4832 if( status == PSA_SUCCESS )
4833 generator->alg = alg;
4834 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004835}
4836
4837#if defined(MBEDTLS_MD_C)
4838static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4839 psa_algorithm_t hash_alg,
4840 psa_key_derivation_step_t step,
4841 const uint8_t *data,
4842 size_t data_length )
4843{
4844 psa_status_t status;
4845 switch( step )
4846 {
4847 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004848 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004849 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004850 status = psa_hmac_setup_internal( &hkdf->hmac,
4851 data, data_length,
4852 hash_alg );
4853 if( status != PSA_SUCCESS )
4854 return( status );
4855 hkdf->state = HKDF_STATE_STARTED;
4856 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857 case PSA_KDF_STEP_SECRET:
4858 /* If no salt was provided, use an empty salt. */
4859 if( hkdf->state == HKDF_STATE_INIT )
4860 {
4861 status = psa_hmac_setup_internal( &hkdf->hmac,
4862 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004863 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004864 if( status != PSA_SUCCESS )
4865 return( status );
4866 hkdf->state = HKDF_STATE_STARTED;
4867 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004868 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004870 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4871 data, data_length );
4872 if( status != PSA_SUCCESS )
4873 return( status );
4874 status = psa_hmac_finish_internal( &hkdf->hmac,
4875 hkdf->prk,
4876 sizeof( hkdf->prk ) );
4877 if( status != PSA_SUCCESS )
4878 return( status );
4879 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4880 hkdf->block_number = 0;
4881 hkdf->state = HKDF_STATE_KEYED;
4882 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883 case PSA_KDF_STEP_INFO:
4884 if( hkdf->state == HKDF_STATE_OUTPUT )
4885 return( PSA_ERROR_BAD_STATE );
4886 if( hkdf->info_set )
4887 return( PSA_ERROR_BAD_STATE );
4888 hkdf->info_length = data_length;
4889 if( data_length != 0 )
4890 {
4891 hkdf->info = mbedtls_calloc( 1, data_length );
4892 if( hkdf->info == NULL )
4893 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4894 memcpy( hkdf->info, data, data_length );
4895 }
4896 hkdf->info_set = 1;
4897 return( PSA_SUCCESS );
4898 default:
4899 return( PSA_ERROR_INVALID_ARGUMENT );
4900 }
4901}
4902#endif /* MBEDTLS_MD_C */
4903
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004904static psa_status_t psa_key_derivation_input_raw(
4905 psa_crypto_generator_t *generator,
4906 psa_key_derivation_step_t step,
4907 const uint8_t *data,
4908 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909{
4910 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004912
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 if( kdf_alg == PSA_ALG_SELECT_RAW )
4914 {
4915 if( generator->capacity != 0 )
4916 return( PSA_ERROR_INVALID_ARGUMENT );
4917 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4918 if( generator->ctx.buffer.data == NULL )
4919 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4920 memcpy( generator->ctx.buffer.data, data, data_length );
4921 generator->ctx.buffer.size = data_length;
4922 generator->capacity = data_length;
4923 status = PSA_SUCCESS;
4924 }
4925 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004926#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928 {
4929 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931 step, data, data_length );
4932 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004934#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004935#if defined(MBEDTLS_MD_C)
4936 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4938 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004939 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004940 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004941 status = PSA_ERROR_NOT_SUPPORTED;
4942 }
4943 else
4944#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 {
4946 /* This can't happen unless the generator object was not initialized */
4947 return( PSA_ERROR_BAD_STATE );
4948 }
4949
4950 if( status != PSA_SUCCESS )
4951 psa_generator_abort( generator );
4952 return( status );
4953}
4954
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004955psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4956 psa_key_derivation_step_t step,
4957 const uint8_t *data,
4958 size_t data_length )
4959{
4960 switch( step )
4961 {
4962 case PSA_KDF_STEP_LABEL:
4963 case PSA_KDF_STEP_SALT:
4964 case PSA_KDF_STEP_INFO:
4965 return( psa_key_derivation_input_raw( generator, step,
4966 data, data_length ) );
4967 default:
4968 return( PSA_ERROR_INVALID_ARGUMENT );
4969 }
4970}
4971
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004972psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4973 psa_key_derivation_step_t step,
4974 psa_key_handle_t handle )
4975{
4976 psa_key_slot_t *slot;
4977 psa_status_t status;
4978 status = psa_get_key_from_slot( handle, &slot,
4979 PSA_KEY_USAGE_DERIVE,
4980 generator->alg );
4981 if( status != PSA_SUCCESS )
4982 return( status );
4983 if( slot->type != PSA_KEY_TYPE_DERIVE )
4984 return( PSA_ERROR_INVALID_ARGUMENT );
4985 /* Don't allow a key to be used as an input that is usually public.
4986 * This is debatable. It's ok from a cryptographic perspective to
4987 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004988 * the material should be dedicated to a particular input step,
4989 * otherwise this may allow the key to be used in an unintended way
4990 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004991 if( step != PSA_KDF_STEP_SECRET )
4992 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004993 return( psa_key_derivation_input_raw( generator,
4994 step,
4995 slot->data.raw.data,
4996 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004997}
4998
Gilles Peskineea0fb492018-07-12 17:17:20 +02004999
5000
5001/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005002/* Key agreement */
5003/****************************************************************/
5004
Gilles Peskinea05219c2018-11-16 16:02:56 +01005005#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005006static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5007 size_t peer_key_length,
5008 const mbedtls_ecp_keypair *our_key,
5009 uint8_t *shared_secret,
5010 size_t shared_secret_size,
5011 size_t *shared_secret_length )
5012{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005013 mbedtls_ecp_keypair *their_key = NULL;
5014 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005015 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005016 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005017
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005018 status = psa_import_ec_public_key(
5019 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5020 peer_key, peer_key_length,
5021 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005022 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005023 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005024
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005025 status = mbedtls_to_psa_error(
5026 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5027 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005028 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005029 status = mbedtls_to_psa_error(
5030 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5031 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005032 goto exit;
5033
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005034 status = mbedtls_to_psa_error(
5035 mbedtls_ecdh_calc_secret( &ecdh,
5036 shared_secret_length,
5037 shared_secret, shared_secret_size,
5038 mbedtls_ctr_drbg_random,
5039 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005040
5041exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005042 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005043 mbedtls_ecp_keypair_free( their_key );
5044 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005045 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005046}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005047#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005048
Gilles Peskine01d718c2018-09-18 12:01:02 +02005049#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5050
Gilles Peskine0216fe12019-04-11 21:23:21 +02005051static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5052 psa_key_slot_t *private_key,
5053 const uint8_t *peer_key,
5054 size_t peer_key_length,
5055 uint8_t *shared_secret,
5056 size_t shared_secret_size,
5057 size_t *shared_secret_length )
5058{
5059 switch( alg )
5060 {
5061#if defined(MBEDTLS_ECDH_C)
5062 case PSA_ALG_ECDH:
5063 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
5064 return( PSA_ERROR_INVALID_ARGUMENT );
5065 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5066 private_key->data.ecp,
5067 shared_secret, shared_secret_size,
5068 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005069#endif /* MBEDTLS_ECDH_C */
5070 default:
5071 (void) private_key;
5072 (void) peer_key;
5073 (void) peer_key_length;
5074 (void) shared_secret;
5075 (void) shared_secret_size;
5076 (void) shared_secret_length;
5077 return( PSA_ERROR_NOT_SUPPORTED );
5078 }
5079}
5080
Gilles Peskine346797d2018-11-16 16:05:06 +01005081/* Note that if this function fails, you must call psa_generator_abort()
5082 * to potentially free embedded data structures and wipe confidential data.
5083 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005084static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005086 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005087 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005088 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005089{
5090 psa_status_t status;
5091 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5092 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005093 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005094
5095 /* Step 1: run the secret agreement algorithm to generate the shared
5096 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005097 status = psa_key_agreement_raw_internal( ka_alg,
5098 private_key,
5099 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005100 shared_secret,
5101 sizeof( shared_secret ),
5102 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005103 if( status != PSA_SUCCESS )
5104 goto exit;
5105
5106 /* Step 2: set up the key derivation to generate key material from
5107 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108 status = psa_key_derivation_input_raw( generator, step,
5109 shared_secret, shared_secret_length );
5110
Gilles Peskine01d718c2018-09-18 12:01:02 +02005111exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005112 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005113 return( status );
5114}
5115
5116psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005117 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005118 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005119 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005120 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005121{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005122 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005123 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005124 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005125 return( PSA_ERROR_INVALID_ARGUMENT );
5126 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005128 if( status != PSA_SUCCESS )
5129 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005130 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005131 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005132 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005133 if( status != PSA_SUCCESS )
5134 psa_generator_abort( generator );
5135 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005136}
5137
Gilles Peskine0216fe12019-04-11 21:23:21 +02005138psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5139 psa_key_handle_t private_key,
5140 const uint8_t *peer_key,
5141 size_t peer_key_length,
5142 uint8_t *output,
5143 size_t output_size,
5144 size_t *output_length )
5145{
5146 psa_key_slot_t *slot;
5147 psa_status_t status;
5148
5149 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5150 {
5151 status = PSA_ERROR_INVALID_ARGUMENT;
5152 goto exit;
5153 }
5154 status = psa_get_key_from_slot( private_key, &slot,
5155 PSA_KEY_USAGE_DERIVE, alg );
5156 if( status != PSA_SUCCESS )
5157 goto exit;
5158
5159 status = psa_key_agreement_raw_internal( alg, slot,
5160 peer_key, peer_key_length,
5161 output, output_size,
5162 output_length );
5163
5164exit:
5165 if( status != PSA_SUCCESS )
5166 {
5167 /* If an error happens and is not handled properly, the output
5168 * may be used as a key to protect sensitive data. Arrange for such
5169 * a key to be random, which is likely to result in decryption or
5170 * verification errors. This is better than filling the buffer with
5171 * some constant data such as zeros, which would result in the data
5172 * being protected with a reproducible, easily knowable key.
5173 */
5174 psa_generate_random( output, output_size );
5175 *output_length = output_size;
5176 }
5177 return( status );
5178}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005179
5180
5181/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005182/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005183/****************************************************************/
5184
5185psa_status_t psa_generate_random( uint8_t *output,
5186 size_t output_size )
5187{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005188 int ret;
5189 GUARD_MODULE_INITIALIZED;
5190
5191 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005192 return( mbedtls_to_psa_error( ret ) );
5193}
5194
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005195#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5196#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005197
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005198psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5199 size_t seed_size )
5200{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005201 if( global_data.initialized )
5202 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005203
5204 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5205 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5206 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5207 return( PSA_ERROR_INVALID_ARGUMENT );
5208
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005209 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005210}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005211#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005212
Gilles Peskinee56e8782019-04-26 17:34:02 +02005213#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5214static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5215 size_t domain_parameters_size,
5216 int *exponent )
5217{
5218 size_t i;
5219 uint32_t acc = 0;
5220
5221 if( domain_parameters_size == 0 )
5222 {
5223 *exponent = 65537;
5224 return( PSA_SUCCESS );
5225 }
5226
5227 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5228 * support values that fit in a 32-bit integer, which is larger than
5229 * int on just about every platform anyway. */
5230 if( domain_parameters_size > sizeof( acc ) )
5231 return( PSA_ERROR_NOT_SUPPORTED );
5232 for( i = 0; i < domain_parameters_size; i++ )
5233 acc = ( acc << 8 ) | domain_parameters[i];
5234 if( acc > INT_MAX )
5235 return( PSA_ERROR_NOT_SUPPORTED );
5236 *exponent = acc;
5237 return( PSA_SUCCESS );
5238}
5239#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5240
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005241static psa_status_t psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005242 psa_key_slot_t *slot, size_t bits,
5243 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005244{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005245 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005246
Gilles Peskinee56e8782019-04-26 17:34:02 +02005247 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005248 return( PSA_ERROR_INVALID_ARGUMENT );
5249
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005250 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005251 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005252 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005253 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005254 if( status != PSA_SUCCESS )
5255 return( status );
5256 status = psa_generate_random( slot->data.raw.data,
5257 slot->data.raw.bytes );
5258 if( status != PSA_SUCCESS )
5259 {
5260 mbedtls_free( slot->data.raw.data );
5261 return( status );
5262 }
5263#if defined(MBEDTLS_DES_C)
5264 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005265 psa_des_set_key_parity( slot->data.raw.data,
5266 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005267#endif /* MBEDTLS_DES_C */
5268 }
5269 else
5270
5271#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5272 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5273 {
5274 mbedtls_rsa_context *rsa;
5275 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005276 int exponent;
5277 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005278 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5279 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005280 /* Accept only byte-aligned keys, for the same reasons as
5281 * in psa_import_rsa_key(). */
5282 if( bits % 8 != 0 )
5283 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005284 status = psa_read_rsa_exponent( domain_parameters,
5285 domain_parameters_size,
5286 &exponent );
5287 if( status != PSA_SUCCESS )
5288 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005289 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5290 if( rsa == NULL )
5291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5292 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5293 ret = mbedtls_rsa_gen_key( rsa,
5294 mbedtls_ctr_drbg_random,
5295 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005296 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005297 exponent );
5298 if( ret != 0 )
5299 {
5300 mbedtls_rsa_free( rsa );
5301 mbedtls_free( rsa );
5302 return( mbedtls_to_psa_error( ret ) );
5303 }
5304 slot->data.rsa = rsa;
5305 }
5306 else
5307#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5308
5309#if defined(MBEDTLS_ECP_C)
5310 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5311 {
5312 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5313 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5314 const mbedtls_ecp_curve_info *curve_info =
5315 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5316 mbedtls_ecp_keypair *ecp;
5317 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005318 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005319 return( PSA_ERROR_NOT_SUPPORTED );
5320 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5321 return( PSA_ERROR_NOT_SUPPORTED );
5322 if( curve_info->bit_size != bits )
5323 return( PSA_ERROR_INVALID_ARGUMENT );
5324 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5325 if( ecp == NULL )
5326 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5327 mbedtls_ecp_keypair_init( ecp );
5328 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5329 mbedtls_ctr_drbg_random,
5330 &global_data.ctr_drbg );
5331 if( ret != 0 )
5332 {
5333 mbedtls_ecp_keypair_free( ecp );
5334 mbedtls_free( ecp );
5335 return( mbedtls_to_psa_error( ret ) );
5336 }
5337 slot->data.ecp = ecp;
5338 }
5339 else
5340#endif /* MBEDTLS_ECP_C */
5341
5342 return( PSA_ERROR_NOT_SUPPORTED );
5343
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005344 return( PSA_SUCCESS );
5345}
5346
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005347psa_status_t psa_generate_random_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005348 psa_key_type_t type,
5349 size_t bits,
5350 const void *extra,
5351 size_t extra_size )
5352{
5353 psa_key_slot_t *slot;
5354 psa_status_t status;
5355
Gilles Peskinee56e8782019-04-26 17:34:02 +02005356#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5357 /* The old public exponent encoding is no longer supported. */
5358 if( extra_size != 0 )
5359 return( PSA_ERROR_NOT_SUPPORTED );
5360#endif
5361
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005362 status = psa_get_empty_key_slot( handle, &slot );
5363 if( status != PSA_SUCCESS )
5364 return( status );
5365
Gilles Peskine12313cd2018-06-20 00:20:32 +02005366 slot->type = type;
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005367 status = psa_generate_random_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005368 if( status != PSA_SUCCESS )
5369 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005370
5371#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5372 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5373 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005374 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005375 }
5376#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5377
5378 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005379}
5380
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005381psa_status_t psa_generate_random_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005382 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383{
5384 psa_status_t status;
5385 psa_key_slot_t *slot = NULL;
5386 status = psa_start_key_creation( attributes, handle, &slot );
5387 if( status == PSA_SUCCESS )
5388 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005389 status = psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005390 slot, attributes->bits,
5391 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005392 }
5393 if( status == PSA_SUCCESS )
5394 status = psa_finish_key_creation( slot );
5395 if( status != PSA_SUCCESS )
5396 {
5397 psa_fail_key_creation( slot );
5398 *handle = 0;
5399 }
5400 return( status );
5401}
5402
5403
Gilles Peskine05d69892018-06-19 22:00:52 +02005404
5405/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005406/* Module setup */
5407/****************************************************************/
5408
Gilles Peskine5e769522018-11-20 21:59:56 +01005409psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5410 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5411 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5412{
5413 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5414 return( PSA_ERROR_BAD_STATE );
5415 global_data.entropy_init = entropy_init;
5416 global_data.entropy_free = entropy_free;
5417 return( PSA_SUCCESS );
5418}
5419
Gilles Peskinee59236f2018-01-27 23:32:46 +01005420void mbedtls_psa_crypto_free( void )
5421{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005422 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005423 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5424 {
5425 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005426 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005427 }
5428 /* Wipe all remaining data, including configuration.
5429 * In particular, this sets all state indicator to the value
5430 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005431 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005432}
5433
5434psa_status_t psa_crypto_init( void )
5435{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005436 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005437 const unsigned char drbg_seed[] = "PSA";
5438
Gilles Peskinec6b69072018-11-20 21:42:52 +01005439 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005440 if( global_data.initialized != 0 )
5441 return( PSA_SUCCESS );
5442
Gilles Peskine5e769522018-11-20 21:59:56 +01005443 /* Set default configuration if
5444 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5445 if( global_data.entropy_init == NULL )
5446 global_data.entropy_init = mbedtls_entropy_init;
5447 if( global_data.entropy_free == NULL )
5448 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449
Gilles Peskinec6b69072018-11-20 21:42:52 +01005450 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005451 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005453 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005454 status = mbedtls_to_psa_error(
5455 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5456 mbedtls_entropy_func,
5457 &global_data.entropy,
5458 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5459 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005460 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005461 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005462
Gilles Peskine66fb1262018-12-10 16:29:04 +01005463 status = psa_initialize_key_slots( );
5464 if( status != PSA_SUCCESS )
5465 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005466
5467 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005468 global_data.initialized = 1;
5469
Gilles Peskinee59236f2018-01-27 23:32:46 +01005470exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005471 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005472 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005473 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005474}
5475
5476#endif /* MBEDTLS_PSA_CRYPTO_C */