blob: 7e20071298f5340115a405c9c5492804f835b647 [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 Peskined0890212019-06-24 14:34:43 +020035#include "psa_crypto_se.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010036#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010037/* Include internal declarations that are useful for implementing persistently
38 * stored keys. */
39#include "psa_crypto_storage.h"
40
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041#include <stdlib.h>
42#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020044#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020055#include "mbedtls/chacha20.h"
56#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/cipher.h"
58#include "mbedtls/ccm.h"
59#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020062#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010064#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/error.h"
66#include "mbedtls/gcm.h"
67#include "mbedtls/md2.h"
68#include "mbedtls/md4.h"
69#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010070#include "mbedtls/md.h"
71#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010072#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010074#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128static psa_status_t mbedtls_to_psa_error( int ret )
129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300351
Gilles Peskinee59236f2018-01-27 23:32:46 +0100352 default:
David Saadab4ecc272019-02-14 13:48:10 +0200353 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 }
355}
356
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200357
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200358
359
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100360/****************************************************************/
361/* Key management */
362/****************************************************************/
363
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100364#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200365static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
366{
367 switch( grpid )
368 {
369 case MBEDTLS_ECP_DP_SECP192R1:
370 return( PSA_ECC_CURVE_SECP192R1 );
371 case MBEDTLS_ECP_DP_SECP224R1:
372 return( PSA_ECC_CURVE_SECP224R1 );
373 case MBEDTLS_ECP_DP_SECP256R1:
374 return( PSA_ECC_CURVE_SECP256R1 );
375 case MBEDTLS_ECP_DP_SECP384R1:
376 return( PSA_ECC_CURVE_SECP384R1 );
377 case MBEDTLS_ECP_DP_SECP521R1:
378 return( PSA_ECC_CURVE_SECP521R1 );
379 case MBEDTLS_ECP_DP_BP256R1:
380 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
381 case MBEDTLS_ECP_DP_BP384R1:
382 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
383 case MBEDTLS_ECP_DP_BP512R1:
384 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
385 case MBEDTLS_ECP_DP_CURVE25519:
386 return( PSA_ECC_CURVE_CURVE25519 );
387 case MBEDTLS_ECP_DP_SECP192K1:
388 return( PSA_ECC_CURVE_SECP192K1 );
389 case MBEDTLS_ECP_DP_SECP224K1:
390 return( PSA_ECC_CURVE_SECP224K1 );
391 case MBEDTLS_ECP_DP_SECP256K1:
392 return( PSA_ECC_CURVE_SECP256K1 );
393 case MBEDTLS_ECP_DP_CURVE448:
394 return( PSA_ECC_CURVE_CURVE448 );
395 default:
396 return( 0 );
397 }
398}
399
Gilles Peskine12313cd2018-06-20 00:20:32 +0200400static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
401{
402 switch( curve )
403 {
404 case PSA_ECC_CURVE_SECP192R1:
405 return( MBEDTLS_ECP_DP_SECP192R1 );
406 case PSA_ECC_CURVE_SECP224R1:
407 return( MBEDTLS_ECP_DP_SECP224R1 );
408 case PSA_ECC_CURVE_SECP256R1:
409 return( MBEDTLS_ECP_DP_SECP256R1 );
410 case PSA_ECC_CURVE_SECP384R1:
411 return( MBEDTLS_ECP_DP_SECP384R1 );
412 case PSA_ECC_CURVE_SECP521R1:
413 return( MBEDTLS_ECP_DP_SECP521R1 );
414 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
415 return( MBEDTLS_ECP_DP_BP256R1 );
416 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
417 return( MBEDTLS_ECP_DP_BP384R1 );
418 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
419 return( MBEDTLS_ECP_DP_BP512R1 );
420 case PSA_ECC_CURVE_CURVE25519:
421 return( MBEDTLS_ECP_DP_CURVE25519 );
422 case PSA_ECC_CURVE_SECP192K1:
423 return( MBEDTLS_ECP_DP_SECP192K1 );
424 case PSA_ECC_CURVE_SECP224K1:
425 return( MBEDTLS_ECP_DP_SECP224K1 );
426 case PSA_ECC_CURVE_SECP256K1:
427 return( MBEDTLS_ECP_DP_SECP256K1 );
428 case PSA_ECC_CURVE_CURVE448:
429 return( MBEDTLS_ECP_DP_CURVE448 );
430 default:
431 return( MBEDTLS_ECP_DP_NONE );
432 }
433}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100434#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200435
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
437 size_t bits,
438 struct raw_data *raw )
439{
440 /* Check that the bit size is acceptable for the key type */
441 switch( type )
442 {
443 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200444 if( bits == 0 )
445 {
446 raw->bytes = 0;
447 raw->data = NULL;
448 return( PSA_SUCCESS );
449 }
450 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200451#if defined(MBEDTLS_MD_C)
452 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200454 case PSA_KEY_TYPE_DERIVE:
455 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456#if defined(MBEDTLS_AES_C)
457 case PSA_KEY_TYPE_AES:
458 if( bits != 128 && bits != 192 && bits != 256 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462#if defined(MBEDTLS_CAMELLIA_C)
463 case PSA_KEY_TYPE_CAMELLIA:
464 if( bits != 128 && bits != 192 && bits != 256 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_DES_C)
469 case PSA_KEY_TYPE_DES:
470 if( bits != 64 && bits != 128 && bits != 192 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_ARC4_C)
475 case PSA_KEY_TYPE_ARC4:
476 if( bits < 8 || bits > 2048 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200480#if defined(MBEDTLS_CHACHA20_C)
481 case PSA_KEY_TYPE_CHACHA20:
482 if( bits != 256 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 default:
487 return( PSA_ERROR_NOT_SUPPORTED );
488 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200489 if( bits % 8 != 0 )
490 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491
492 /* Allocate memory for the key */
493 raw->bytes = PSA_BITS_TO_BYTES( bits );
494 raw->data = mbedtls_calloc( 1, raw->bytes );
495 if( raw->data == NULL )
496 {
497 raw->bytes = 0;
498 return( PSA_ERROR_INSUFFICIENT_MEMORY );
499 }
500 return( PSA_SUCCESS );
501}
502
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200503#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100504/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
505 * that are not a multiple of 8) well. For example, there is only
506 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
507 * way to return the exact bit size of a key.
508 * To keep things simple, reject non-byte-aligned key sizes. */
509static psa_status_t psa_check_rsa_key_byte_aligned(
510 const mbedtls_rsa_context *rsa )
511{
512 mbedtls_mpi n;
513 psa_status_t status;
514 mbedtls_mpi_init( &n );
515 status = mbedtls_to_psa_error(
516 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
517 if( status == PSA_SUCCESS )
518 {
519 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
520 status = PSA_ERROR_NOT_SUPPORTED;
521 }
522 mbedtls_mpi_free( &n );
523 return( status );
524}
525
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000526static psa_status_t psa_import_rsa_key( psa_key_type_t type,
527 const uint8_t *data,
528 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200529 mbedtls_rsa_context **p_rsa )
530{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000531 psa_status_t status;
532 mbedtls_pk_context pk;
533 mbedtls_rsa_context *rsa;
534 size_t bits;
535
536 mbedtls_pk_init( &pk );
537
538 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200539 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 status = mbedtls_to_psa_error(
541 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200542 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
545 if( status != PSA_SUCCESS )
546 goto exit;
547
548 /* We have something that the pkparse module recognizes. If it is a
549 * valid RSA key, store it. */
550 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = PSA_ERROR_INVALID_ARGUMENT;
553 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000555
556 rsa = mbedtls_pk_rsa( pk );
557 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
558 * supports non-byte-aligned key sizes, but not well. For example,
559 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
560 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
561 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
562 {
563 status = PSA_ERROR_NOT_SUPPORTED;
564 goto exit;
565 }
566 status = psa_check_rsa_key_byte_aligned( rsa );
567
568exit:
569 /* Free the content of the pk object only on error. */
570 if( status != PSA_SUCCESS )
571 {
572 mbedtls_pk_free( &pk );
573 return( status );
574 }
575
576 /* On success, store the content of the object in the RSA context. */
577 *p_rsa = rsa;
578
579 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200580}
581#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
582
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000583#if defined(MBEDTLS_ECP_C)
584
585/* Import a public key given as the uncompressed representation defined by SEC1
586 * 2.3.3 as the content of an ECPoint. */
587static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
588 const uint8_t *data,
589 size_t data_length,
590 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200591{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593 mbedtls_ecp_keypair *ecp = NULL;
594 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
595
596 *p_ecp = NULL;
597 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
598 if( ecp == NULL )
599 return( PSA_ERROR_INSUFFICIENT_MEMORY );
600 mbedtls_ecp_keypair_init( ecp );
601
602 /* Load the group. */
603 status = mbedtls_to_psa_error(
604 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
605 if( status != PSA_SUCCESS )
606 goto exit;
607 /* Load the public value. */
608 status = mbedtls_to_psa_error(
609 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
610 data, data_length ) );
611 if( status != PSA_SUCCESS )
612 goto exit;
613
614 /* Check that the point is on the curve. */
615 status = mbedtls_to_psa_error(
616 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
617 if( status != PSA_SUCCESS )
618 goto exit;
619
620 *p_ecp = ecp;
621 return( PSA_SUCCESS );
622
623exit:
624 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200625 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000626 mbedtls_ecp_keypair_free( ecp );
627 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000629 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200630}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000631#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200632
Gilles Peskinef76aa772018-10-29 19:24:33 +0100633#if defined(MBEDTLS_ECP_C)
634/* Import a private key given as a byte string which is the private value
635 * in big-endian order. */
636static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
637 const uint8_t *data,
638 size_t data_length,
639 mbedtls_ecp_keypair **p_ecp )
640{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642 mbedtls_ecp_keypair *ecp = NULL;
643 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
644
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200645 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
646 return( PSA_ERROR_INVALID_ARGUMENT );
647
Gilles Peskinef76aa772018-10-29 19:24:33 +0100648 *p_ecp = NULL;
649 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
650 if( ecp == NULL )
651 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000652 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100653
654 /* Load the group. */
655 status = mbedtls_to_psa_error(
656 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
657 if( status != PSA_SUCCESS )
658 goto exit;
659 /* Load the secret value. */
660 status = mbedtls_to_psa_error(
661 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
662 if( status != PSA_SUCCESS )
663 goto exit;
664 /* Validate the private key. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Calculate the public key from the private key. */
670 status = mbedtls_to_psa_error(
671 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
672 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
673 if( status != PSA_SUCCESS )
674 goto exit;
675
676 *p_ecp = ecp;
677 return( PSA_SUCCESS );
678
679exit:
680 if( ecp != NULL )
681 {
682 mbedtls_ecp_keypair_free( ecp );
683 mbedtls_free( ecp );
684 }
685 return( status );
686}
687#endif /* defined(MBEDTLS_ECP_C) */
688
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100689/** Import key data into a slot. `slot->type` must have been set
690 * previously. This function assumes that the slot does not contain
691 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100692psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
693 const uint8_t *data,
694 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200696 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697
Darryl Green940d72c2018-07-13 13:18:51 +0100698 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100699 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100700 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 if( data_length > SIZE_MAX / 8 )
702 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100703 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200704 PSA_BYTES_TO_BITS( data_length ),
705 &slot->data.raw );
706 if( status != PSA_SUCCESS )
707 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200708 if( data_length != 0 )
709 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100712#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200713 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100714 {
Darryl Green940d72c2018-07-13 13:18:51 +0100715 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100716 data, data_length,
717 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100718 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000719 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
720 {
721 status = psa_import_ec_public_key(
722 PSA_KEY_TYPE_GET_CURVE( slot->type ),
723 data, data_length,
724 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000725 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100726 else
727#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
729 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000731 status = psa_import_rsa_key( slot->type,
732 data, data_length,
733 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734 }
735 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000736#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737 {
738 return( PSA_ERROR_NOT_SUPPORTED );
739 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000740 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100741}
742
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100743/** Calculate the intersection of two algorithm usage policies.
744 *
745 * Return 0 (which allows no operation) on incompatibility.
746 */
747static psa_algorithm_t psa_key_policy_algorithm_intersection(
748 psa_algorithm_t alg1,
749 psa_algorithm_t alg2 )
750{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200751 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752 if( alg1 == alg2 )
753 return( alg1 );
754 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100755 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100756 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
757 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
758 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
759 {
760 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
761 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100762 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100763 return( alg1 );
764 }
765 /* If the policies are incompatible, allow nothing. */
766 return( 0 );
767}
768
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200769static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
770 psa_algorithm_t requested_alg )
771{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200772 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200773 if( requested_alg == policy_alg )
774 return( 1 );
775 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200776 * and requested_alg is the same hash-and-sign family with any hash,
777 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
779 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
780 {
781 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
782 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
783 }
784 /* If it isn't permitted, it's forbidden. */
785 return( 0 );
786}
787
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100788/** Test whether a policy permits an algorithm.
789 *
790 * The caller must test usage flags separately.
791 */
792static int psa_key_policy_permits( const psa_key_policy_t *policy,
793 psa_algorithm_t alg )
794{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200795 return( psa_key_algorithm_permits( policy->alg, alg ) ||
796 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797}
798
Gilles Peskinef603c712019-01-19 13:40:11 +0100799/** Restrict a key policy based on a constraint.
800 *
801 * \param[in,out] policy The policy to restrict.
802 * \param[in] constraint The policy constraint to apply.
803 *
804 * \retval #PSA_SUCCESS
805 * \c *policy contains the intersection of the original value of
806 * \c *policy and \c *constraint.
807 * \retval #PSA_ERROR_INVALID_ARGUMENT
808 * \c *policy and \c *constraint are incompatible.
809 * \c *policy is unchanged.
810 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811static psa_status_t psa_restrict_key_policy(
812 psa_key_policy_t *policy,
813 const psa_key_policy_t *constraint )
814{
815 psa_algorithm_t intersection_alg =
816 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200817 psa_algorithm_t intersection_alg2 =
818 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100819 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
820 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200821 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
822 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100824 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200825 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100826 return( PSA_SUCCESS );
827}
828
Darryl Green06fd18d2018-07-16 11:21:11 +0100829/** Retrieve a slot which must contain a key. The key must have allow all the
830 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
831 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100832static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100833 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100834 psa_key_usage_t usage,
835 psa_algorithm_t alg )
836{
837 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100838 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100839
840 *p_slot = NULL;
841
Gilles Peskinec5487a82018-12-03 18:08:14 +0100842 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 if( status != PSA_SUCCESS )
844 return( status );
845 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200846 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100847
848 /* Enforce that usage policy for the key slot contains all the flags
849 * required by the usage parameter. There is one exception: public
850 * keys can always be exported, so we treat public key objects as
851 * if they had the export flag. */
852 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
853 usage &= ~PSA_KEY_USAGE_EXPORT;
854 if( ( slot->policy.usage & usage ) != usage )
855 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100856
857 /* Enforce that the usage policy permits the requested algortihm. */
858 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100859 return( PSA_ERROR_NOT_PERMITTED );
860
861 *p_slot = slot;
862 return( PSA_SUCCESS );
863}
Darryl Green940d72c2018-07-13 13:18:51 +0100864
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100865/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100866static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000867{
868 if( slot->type == PSA_KEY_TYPE_NONE )
869 {
870 /* No key material to clean. */
871 }
872 else if( key_type_is_raw_bytes( slot->type ) )
873 {
874 mbedtls_free( slot->data.raw.data );
875 }
876 else
877#if defined(MBEDTLS_RSA_C)
878 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
879 {
880 mbedtls_rsa_free( slot->data.rsa );
881 mbedtls_free( slot->data.rsa );
882 }
883 else
884#endif /* defined(MBEDTLS_RSA_C) */
885#if defined(MBEDTLS_ECP_C)
886 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
887 {
888 mbedtls_ecp_keypair_free( slot->data.ecp );
889 mbedtls_free( slot->data.ecp );
890 }
891 else
892#endif /* defined(MBEDTLS_ECP_C) */
893 {
894 /* Shouldn't happen: the key type is not any type that we
895 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200896 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000897 }
898
899 return( PSA_SUCCESS );
900}
901
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100902static void psa_abort_operations_using_key( psa_key_slot_t *slot )
903{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200904 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100905 (void) slot;
906}
907
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100908/** Completely wipe a slot in memory, including its policy.
909 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100910psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100911{
912 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100913 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100914 /* At this point, key material and other type-specific content has
915 * been wiped. Clear remaining metadata. We can call memset and not
916 * zeroize because the metadata is not particularly sensitive. */
917 memset( slot, 0, sizeof( *slot ) );
918 return( status );
919}
920
Gilles Peskinec5487a82018-12-03 18:08:14 +0100921psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100923 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100924 psa_status_t status = PSA_SUCCESS;
925 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926
Gilles Peskinec5487a82018-12-03 18:08:14 +0100927 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200928 if( status != PSA_SUCCESS )
929 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100930#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
931 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
932 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100933 storage_status =
934 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100935 }
936#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100937 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100938 if( status != PSA_SUCCESS )
939 return( status );
940 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941}
942
Gilles Peskineb870b182018-07-06 16:02:09 +0200943/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200944static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200945{
946 if( key_type_is_raw_bytes( slot->type ) )
947 return( slot->data.raw.bytes * 8 );
948#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200949 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100950 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200951#endif /* defined(MBEDTLS_RSA_C) */
952#if defined(MBEDTLS_ECP_C)
953 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
954 return( slot->data.ecp->grp.pbits );
955#endif /* defined(MBEDTLS_ECP_C) */
956 /* Shouldn't happen except on an empty slot. */
957 return( 0 );
958}
959
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200960void psa_reset_key_attributes( psa_key_attributes_t *attributes )
961{
Gilles Peskineb699f072019-04-26 16:06:02 +0200962 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200963 memset( attributes, 0, sizeof( *attributes ) );
964}
965
Gilles Peskineb699f072019-04-26 16:06:02 +0200966psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
967 psa_key_type_t type,
968 const uint8_t *data,
969 size_t data_length )
970{
971 uint8_t *copy = NULL;
972
973 if( data_length != 0 )
974 {
975 copy = mbedtls_calloc( 1, data_length );
976 if( copy == NULL )
977 return( PSA_ERROR_INSUFFICIENT_MEMORY );
978 memcpy( copy, data, data_length );
979 }
980 /* After this point, this function is guaranteed to succeed, so it
981 * can start modifying `*attributes`. */
982
983 if( attributes->domain_parameters != NULL )
984 {
985 mbedtls_free( attributes->domain_parameters );
986 attributes->domain_parameters = NULL;
987 attributes->domain_parameters_size = 0;
988 }
989
990 attributes->domain_parameters = copy;
991 attributes->domain_parameters_size = data_length;
992 attributes->type = type;
993 return( PSA_SUCCESS );
994}
995
996psa_status_t psa_get_key_domain_parameters(
997 const psa_key_attributes_t *attributes,
998 uint8_t *data, size_t data_size, size_t *data_length )
999{
1000 if( attributes->domain_parameters_size > data_size )
1001 return( PSA_ERROR_BUFFER_TOO_SMALL );
1002 *data_length = attributes->domain_parameters_size;
1003 if( attributes->domain_parameters_size != 0 )
1004 memcpy( data, attributes->domain_parameters,
1005 attributes->domain_parameters_size );
1006 return( PSA_SUCCESS );
1007}
1008
1009#if defined(MBEDTLS_RSA_C)
1010static psa_status_t psa_get_rsa_public_exponent(
1011 const mbedtls_rsa_context *rsa,
1012 psa_key_attributes_t *attributes )
1013{
1014 mbedtls_mpi mpi;
1015 int ret;
1016 uint8_t *buffer = NULL;
1017 size_t buflen;
1018 mbedtls_mpi_init( &mpi );
1019
1020 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1021 if( ret != 0 )
1022 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001023 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1024 {
1025 /* It's the default value, which is reported as an empty string,
1026 * so there's nothing to do. */
1027 goto exit;
1028 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001029
1030 buflen = mbedtls_mpi_size( &mpi );
1031 buffer = mbedtls_calloc( 1, buflen );
1032 if( buffer == NULL )
1033 {
1034 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1035 goto exit;
1036 }
1037 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1038 if( ret != 0 )
1039 goto exit;
1040 attributes->domain_parameters = buffer;
1041 attributes->domain_parameters_size = buflen;
1042
1043exit:
1044 mbedtls_mpi_free( &mpi );
1045 if( ret != 0 )
1046 mbedtls_free( buffer );
1047 return( mbedtls_to_psa_error( ret ) );
1048}
1049#endif /* MBEDTLS_RSA_C */
1050
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001051psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1052 psa_key_attributes_t *attributes )
1053{
1054 psa_key_slot_t *slot;
1055 psa_status_t status;
1056
1057 psa_reset_key_attributes( attributes );
1058
1059 status = psa_get_key_slot( handle, &slot );
1060 if( status != PSA_SUCCESS )
1061 return( status );
1062
1063 attributes->id = slot->persistent_storage_id;
1064 attributes->lifetime = slot->lifetime;
1065 attributes->policy = slot->policy;
1066 attributes->type = slot->type;
1067 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001068
1069 switch( slot->type )
1070 {
1071#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001072 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001073 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1074 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1075 break;
1076#endif
1077 default:
1078 /* Nothing else to do. */
1079 break;
1080 }
1081
1082 if( status != PSA_SUCCESS )
1083 psa_reset_key_attributes( attributes );
1084 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001085}
1086
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001087#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001088static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1089 unsigned char *buf, size_t size )
1090{
1091 int ret;
1092 unsigned char *c;
1093 size_t len = 0;
1094
1095 c = buf + size;
1096
1097 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1098
1099 return( (int) len );
1100}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001101#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001102
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001103static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1104 uint8_t *data,
1105 size_t data_size,
1106 size_t *data_length,
1107 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001108{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001109 *data_length = 0;
1110
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001111 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001112 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001113
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001114 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001115 {
1116 if( slot->data.raw.bytes > data_size )
1117 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001118 if( data_size != 0 )
1119 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001120 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001121 memset( data + slot->data.raw.bytes, 0,
1122 data_size - slot->data.raw.bytes );
1123 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001124 *data_length = slot->data.raw.bytes;
1125 return( PSA_SUCCESS );
1126 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001127#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001128 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001129 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001130 psa_status_t status;
1131
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001132 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001133 if( bytes > data_size )
1134 return( PSA_ERROR_BUFFER_TOO_SMALL );
1135 status = mbedtls_to_psa_error(
1136 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1137 if( status != PSA_SUCCESS )
1138 return( status );
1139 memset( data + bytes, 0, data_size - bytes );
1140 *data_length = bytes;
1141 return( PSA_SUCCESS );
1142 }
1143#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001144 else
Moran Peker17e36e12018-05-02 12:55:20 +03001145 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001146#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001147 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001148 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001149 {
Moran Pekera998bc62018-04-16 18:16:20 +03001150 mbedtls_pk_context pk;
1151 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001152 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001153 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001154#if defined(MBEDTLS_RSA_C)
1155 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001156 pk.pk_info = &mbedtls_rsa_info;
1157 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001158#else
1159 return( PSA_ERROR_NOT_SUPPORTED );
1160#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001161 }
1162 else
1163 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001164#if defined(MBEDTLS_ECP_C)
1165 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001166 pk.pk_info = &mbedtls_eckey_info;
1167 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001168#else
1169 return( PSA_ERROR_NOT_SUPPORTED );
1170#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001171 }
Moran Pekerd7326592018-05-29 16:56:39 +03001172 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001173 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001174 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001175 }
Moran Peker17e36e12018-05-02 12:55:20 +03001176 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001177 {
Moran Peker17e36e12018-05-02 12:55:20 +03001178 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001179 }
Moran Peker60364322018-04-29 11:34:58 +03001180 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001181 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001182 /* If data_size is 0 then data may be NULL and then the
1183 * call to memset would have undefined behavior. */
1184 if( data_size != 0 )
1185 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001186 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001187 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001188 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1189 * Move the data to the beginning and erase remaining data
1190 * at the original location. */
1191 if( 2 * (size_t) ret <= data_size )
1192 {
1193 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001194 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001195 }
1196 else if( (size_t) ret < data_size )
1197 {
1198 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001199 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001200 }
Moran Pekera998bc62018-04-16 18:16:20 +03001201 *data_length = ret;
1202 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001203 }
1204 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001205#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001206 {
1207 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001208 it is valid for a special-purpose implementation to omit
1209 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001210 return( PSA_ERROR_NOT_SUPPORTED );
1211 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212 }
1213}
1214
Gilles Peskinec5487a82018-12-03 18:08:14 +01001215psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001216 uint8_t *data,
1217 size_t data_size,
1218 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001219{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001220 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001221 psa_status_t status;
1222
1223 /* Set the key to empty now, so that even when there are errors, we always
1224 * set data_length to a value between 0 and data_size. On error, setting
1225 * the key to empty is a good choice because an empty key representation is
1226 * unlikely to be accepted anywhere. */
1227 *data_length = 0;
1228
1229 /* Export requires the EXPORT flag. There is an exception for public keys,
1230 * which don't require any flag, but psa_get_key_from_slot takes
1231 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001232 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001233 if( status != PSA_SUCCESS )
1234 return( status );
1235 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001236 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001237}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238
Gilles Peskinec5487a82018-12-03 18:08:14 +01001239psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001240 uint8_t *data,
1241 size_t data_size,
1242 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001243{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001244 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001245 psa_status_t status;
1246
1247 /* Set the key to empty now, so that even when there are errors, we always
1248 * set data_length to a value between 0 and data_size. On error, setting
1249 * the key to empty is a good choice because an empty key representation is
1250 * unlikely to be accepted anywhere. */
1251 *data_length = 0;
1252
1253 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001254 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001255 if( status != PSA_SUCCESS )
1256 return( status );
1257 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001258 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001259}
1260
Gilles Peskine4747d192019-04-17 15:05:45 +02001261static psa_status_t psa_set_key_policy_internal(
1262 psa_key_slot_t *slot,
1263 const psa_key_policy_t *policy )
1264{
1265 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001266 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001267 PSA_KEY_USAGE_ENCRYPT |
1268 PSA_KEY_USAGE_DECRYPT |
1269 PSA_KEY_USAGE_SIGN |
1270 PSA_KEY_USAGE_VERIFY |
1271 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1272 return( PSA_ERROR_INVALID_ARGUMENT );
1273
1274 slot->policy = *policy;
1275 return( PSA_SUCCESS );
1276}
1277
1278/** Prepare a key slot to receive key material.
1279 *
1280 * This function allocates a key slot and sets its metadata.
1281 *
1282 * If this function fails, call psa_fail_key_creation().
1283 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001284 * This function is intended to be used as follows:
1285 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1286 * it with the specified attributes, and assign it a handle.
1287 * -# Populate the slot with the key material.
1288 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1289 * In case of failure at any step, stop the sequence and call
1290 * psa_fail_key_creation().
1291 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001292 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001293 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001294 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001295 *
1296 * \retval #PSA_SUCCESS
1297 * The key slot is ready to receive key material.
1298 * \return If this function fails, the key slot is an invalid state.
1299 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001300 */
1301static psa_status_t psa_start_key_creation(
1302 const psa_key_attributes_t *attributes,
1303 psa_key_handle_t *handle,
1304 psa_key_slot_t **p_slot )
1305{
1306 psa_status_t status;
1307 psa_key_slot_t *slot;
1308
Gilles Peskine267c6562019-05-27 19:01:54 +02001309 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001310 if( status != PSA_SUCCESS )
1311 return( status );
1312 slot = *p_slot;
1313
1314 status = psa_set_key_policy_internal( slot, &attributes->policy );
1315 if( status != PSA_SUCCESS )
1316 return( status );
1317 slot->lifetime = attributes->lifetime;
1318 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001319 {
1320 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +02001321 attributes->id, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001322 if( status != PSA_SUCCESS )
1323 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001324 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001325 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001326 slot->type = attributes->type;
1327
1328 return( status );
1329}
1330
1331/** Finalize the creation of a key once its key material has been set.
1332 *
1333 * This entails writing the key to persistent storage.
1334 *
1335 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001336 * See the documentation of psa_start_key_creation() for the intended use
1337 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001338 *
1339 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001340 *
1341 * \retval #PSA_SUCCESS
1342 * The key was successfully created. The handle is now valid.
1343 * \return If this function fails, the key slot is an invalid state.
1344 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001345 */
1346static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1347{
1348 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001349 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001350
1351#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1352 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1353 {
1354 uint8_t *buffer = NULL;
1355 size_t buffer_size = 0;
1356 size_t length;
1357
1358 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001359 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001360 buffer = mbedtls_calloc( 1, buffer_size );
1361 if( buffer == NULL && buffer_size != 0 )
1362 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1363 status = psa_internal_export_key( slot,
1364 buffer, buffer_size, &length,
1365 0 );
1366
1367 if( status == PSA_SUCCESS )
1368 {
1369 status = psa_save_persistent_key( slot->persistent_storage_id,
1370 slot->type, &slot->policy,
1371 buffer, length );
1372 }
1373
1374 if( buffer_size != 0 )
1375 mbedtls_platform_zeroize( buffer, buffer_size );
1376 mbedtls_free( buffer );
1377 }
1378#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1379
1380 return( status );
1381}
1382
1383/** Abort the creation of a key.
1384 *
1385 * You may call this function after calling psa_start_key_creation(),
1386 * or after psa_finish_key_creation() fails. In other circumstances, this
1387 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001388 * See the documentation of psa_start_key_creation() for the intended use
1389 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001390 *
1391 * \param slot Pointer to the slot with key material.
1392 */
1393static void psa_fail_key_creation( psa_key_slot_t *slot )
1394{
1395 if( slot == NULL )
1396 return;
1397 psa_wipe_key_slot( slot );
1398}
1399
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001400static psa_status_t psa_check_key_slot_attributes(
1401 const psa_key_slot_t *slot,
1402 const psa_key_attributes_t *attributes )
1403{
1404 if( attributes->type != 0 )
1405 {
1406 if( attributes->type != slot->type )
1407 return( PSA_ERROR_INVALID_ARGUMENT );
1408 }
1409
1410 if( attributes->domain_parameters_size != 0 )
1411 {
1412#if defined(MBEDTLS_RSA_C)
1413 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1414 {
1415 mbedtls_mpi actual, required;
1416 int ret;
1417 mbedtls_mpi_init( &actual );
1418 mbedtls_mpi_init( &required );
1419 ret = mbedtls_rsa_export( slot->data.rsa,
1420 NULL, NULL, NULL, NULL, &actual );
1421 if( ret != 0 )
1422 goto rsa_exit;
1423 ret = mbedtls_mpi_read_binary( &required,
1424 attributes->domain_parameters,
1425 attributes->domain_parameters_size );
1426 if( ret != 0 )
1427 goto rsa_exit;
1428 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1429 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1430 rsa_exit:
1431 mbedtls_mpi_free( &actual );
1432 mbedtls_mpi_free( &required );
1433 if( ret != 0)
1434 return( mbedtls_to_psa_error( ret ) );
1435 }
1436 else
1437#endif
1438 {
1439 return( PSA_ERROR_INVALID_ARGUMENT );
1440 }
1441 }
1442
1443 if( attributes->bits != 0 )
1444 {
1445 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1446 return( PSA_ERROR_INVALID_ARGUMENT );
1447 }
1448
1449 return( PSA_SUCCESS );
1450}
1451
Gilles Peskine4747d192019-04-17 15:05:45 +02001452psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001454 size_t data_length,
1455 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001456{
1457 psa_status_t status;
1458 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001459
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001461 if( status != PSA_SUCCESS )
1462 goto exit;
1463
1464 status = psa_import_key_into_slot( slot, data, data_length );
1465 if( status != PSA_SUCCESS )
1466 goto exit;
1467 status = psa_check_key_slot_attributes( slot, attributes );
1468 if( status != PSA_SUCCESS )
1469 goto exit;
1470
1471 status = psa_finish_key_creation( slot );
1472exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 if( status != PSA_SUCCESS )
1474 {
1475 psa_fail_key_creation( slot );
1476 *handle = 0;
1477 }
1478 return( status );
1479}
1480
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001481static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001482 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001483{
1484 psa_status_t status;
1485 uint8_t *buffer = NULL;
1486 size_t buffer_size = 0;
1487 size_t length;
1488
1489 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001490 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001491 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001492 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001493 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001494 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1495 if( status != PSA_SUCCESS )
1496 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001497 target->type = source->type;
1498 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001499
1500exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001501 if( buffer_size != 0 )
1502 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001503 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001504 return( status );
1505}
1506
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001507psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1508 const psa_key_attributes_t *specified_attributes,
1509 psa_key_handle_t *target_handle )
1510{
1511 psa_status_t status;
1512 psa_key_slot_t *source_slot = NULL;
1513 psa_key_slot_t *target_slot = NULL;
1514 psa_key_attributes_t actual_attributes = *specified_attributes;
1515
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001516 status = psa_get_key_from_slot( source_handle, &source_slot,
1517 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001518 if( status != PSA_SUCCESS )
1519 goto exit;
1520
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001521 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1522 if( status != PSA_SUCCESS )
1523 goto exit;
1524
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001525 status = psa_restrict_key_policy( &actual_attributes.policy,
1526 &source_slot->policy );
1527 if( status != PSA_SUCCESS )
1528 goto exit;
1529
1530 status = psa_start_key_creation( &actual_attributes,
1531 target_handle, &target_slot );
1532 if( status != PSA_SUCCESS )
1533 goto exit;
1534
1535 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001536 if( status != PSA_SUCCESS )
1537 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001538
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001539 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001540exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001541 if( status != PSA_SUCCESS )
1542 {
1543 psa_fail_key_creation( target_slot );
1544 *target_handle = 0;
1545 }
1546 return( status );
1547}
1548
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001549
1550
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001552/* Message digests */
1553/****************************************************************/
1554
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001555static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001556{
1557 switch( alg )
1558 {
1559#if defined(MBEDTLS_MD2_C)
1560 case PSA_ALG_MD2:
1561 return( &mbedtls_md2_info );
1562#endif
1563#if defined(MBEDTLS_MD4_C)
1564 case PSA_ALG_MD4:
1565 return( &mbedtls_md4_info );
1566#endif
1567#if defined(MBEDTLS_MD5_C)
1568 case PSA_ALG_MD5:
1569 return( &mbedtls_md5_info );
1570#endif
1571#if defined(MBEDTLS_RIPEMD160_C)
1572 case PSA_ALG_RIPEMD160:
1573 return( &mbedtls_ripemd160_info );
1574#endif
1575#if defined(MBEDTLS_SHA1_C)
1576 case PSA_ALG_SHA_1:
1577 return( &mbedtls_sha1_info );
1578#endif
1579#if defined(MBEDTLS_SHA256_C)
1580 case PSA_ALG_SHA_224:
1581 return( &mbedtls_sha224_info );
1582 case PSA_ALG_SHA_256:
1583 return( &mbedtls_sha256_info );
1584#endif
1585#if defined(MBEDTLS_SHA512_C)
1586 case PSA_ALG_SHA_384:
1587 return( &mbedtls_sha384_info );
1588 case PSA_ALG_SHA_512:
1589 return( &mbedtls_sha512_info );
1590#endif
1591 default:
1592 return( NULL );
1593 }
1594}
1595
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001596psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1597{
1598 switch( operation->alg )
1599 {
Gilles Peskine81736312018-06-26 15:04:31 +02001600 case 0:
1601 /* The object has (apparently) been initialized but it is not
1602 * in use. It's ok to call abort on such an object, and there's
1603 * nothing to do. */
1604 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001605#if defined(MBEDTLS_MD2_C)
1606 case PSA_ALG_MD2:
1607 mbedtls_md2_free( &operation->ctx.md2 );
1608 break;
1609#endif
1610#if defined(MBEDTLS_MD4_C)
1611 case PSA_ALG_MD4:
1612 mbedtls_md4_free( &operation->ctx.md4 );
1613 break;
1614#endif
1615#if defined(MBEDTLS_MD5_C)
1616 case PSA_ALG_MD5:
1617 mbedtls_md5_free( &operation->ctx.md5 );
1618 break;
1619#endif
1620#if defined(MBEDTLS_RIPEMD160_C)
1621 case PSA_ALG_RIPEMD160:
1622 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1623 break;
1624#endif
1625#if defined(MBEDTLS_SHA1_C)
1626 case PSA_ALG_SHA_1:
1627 mbedtls_sha1_free( &operation->ctx.sha1 );
1628 break;
1629#endif
1630#if defined(MBEDTLS_SHA256_C)
1631 case PSA_ALG_SHA_224:
1632 case PSA_ALG_SHA_256:
1633 mbedtls_sha256_free( &operation->ctx.sha256 );
1634 break;
1635#endif
1636#if defined(MBEDTLS_SHA512_C)
1637 case PSA_ALG_SHA_384:
1638 case PSA_ALG_SHA_512:
1639 mbedtls_sha512_free( &operation->ctx.sha512 );
1640 break;
1641#endif
1642 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001643 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001644 }
1645 operation->alg = 0;
1646 return( PSA_SUCCESS );
1647}
1648
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001649psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001650 psa_algorithm_t alg )
1651{
1652 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001653
1654 /* A context must be freshly initialized before it can be set up. */
1655 if( operation->alg != 0 )
1656 {
1657 return( PSA_ERROR_BAD_STATE );
1658 }
1659
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001660 switch( alg )
1661 {
1662#if defined(MBEDTLS_MD2_C)
1663 case PSA_ALG_MD2:
1664 mbedtls_md2_init( &operation->ctx.md2 );
1665 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1666 break;
1667#endif
1668#if defined(MBEDTLS_MD4_C)
1669 case PSA_ALG_MD4:
1670 mbedtls_md4_init( &operation->ctx.md4 );
1671 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1672 break;
1673#endif
1674#if defined(MBEDTLS_MD5_C)
1675 case PSA_ALG_MD5:
1676 mbedtls_md5_init( &operation->ctx.md5 );
1677 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1678 break;
1679#endif
1680#if defined(MBEDTLS_RIPEMD160_C)
1681 case PSA_ALG_RIPEMD160:
1682 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1683 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1684 break;
1685#endif
1686#if defined(MBEDTLS_SHA1_C)
1687 case PSA_ALG_SHA_1:
1688 mbedtls_sha1_init( &operation->ctx.sha1 );
1689 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1690 break;
1691#endif
1692#if defined(MBEDTLS_SHA256_C)
1693 case PSA_ALG_SHA_224:
1694 mbedtls_sha256_init( &operation->ctx.sha256 );
1695 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1696 break;
1697 case PSA_ALG_SHA_256:
1698 mbedtls_sha256_init( &operation->ctx.sha256 );
1699 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1700 break;
1701#endif
1702#if defined(MBEDTLS_SHA512_C)
1703 case PSA_ALG_SHA_384:
1704 mbedtls_sha512_init( &operation->ctx.sha512 );
1705 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1706 break;
1707 case PSA_ALG_SHA_512:
1708 mbedtls_sha512_init( &operation->ctx.sha512 );
1709 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1710 break;
1711#endif
1712 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001713 return( PSA_ALG_IS_HASH( alg ) ?
1714 PSA_ERROR_NOT_SUPPORTED :
1715 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001716 }
1717 if( ret == 0 )
1718 operation->alg = alg;
1719 else
1720 psa_hash_abort( operation );
1721 return( mbedtls_to_psa_error( ret ) );
1722}
1723
1724psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1725 const uint8_t *input,
1726 size_t input_length )
1727{
1728 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001729
1730 /* Don't require hash implementations to behave correctly on a
1731 * zero-length input, which may have an invalid pointer. */
1732 if( input_length == 0 )
1733 return( PSA_SUCCESS );
1734
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001735 switch( operation->alg )
1736 {
1737#if defined(MBEDTLS_MD2_C)
1738 case PSA_ALG_MD2:
1739 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1740 input, input_length );
1741 break;
1742#endif
1743#if defined(MBEDTLS_MD4_C)
1744 case PSA_ALG_MD4:
1745 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1746 input, input_length );
1747 break;
1748#endif
1749#if defined(MBEDTLS_MD5_C)
1750 case PSA_ALG_MD5:
1751 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1752 input, input_length );
1753 break;
1754#endif
1755#if defined(MBEDTLS_RIPEMD160_C)
1756 case PSA_ALG_RIPEMD160:
1757 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1758 input, input_length );
1759 break;
1760#endif
1761#if defined(MBEDTLS_SHA1_C)
1762 case PSA_ALG_SHA_1:
1763 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1764 input, input_length );
1765 break;
1766#endif
1767#if defined(MBEDTLS_SHA256_C)
1768 case PSA_ALG_SHA_224:
1769 case PSA_ALG_SHA_256:
1770 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1771 input, input_length );
1772 break;
1773#endif
1774#if defined(MBEDTLS_SHA512_C)
1775 case PSA_ALG_SHA_384:
1776 case PSA_ALG_SHA_512:
1777 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1778 input, input_length );
1779 break;
1780#endif
1781 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001782 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001783 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001784
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001785 if( ret != 0 )
1786 psa_hash_abort( operation );
1787 return( mbedtls_to_psa_error( ret ) );
1788}
1789
1790psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1791 uint8_t *hash,
1792 size_t hash_size,
1793 size_t *hash_length )
1794{
itayzafrir40835d42018-08-02 13:14:17 +03001795 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001796 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001797 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001798
1799 /* Fill the output buffer with something that isn't a valid hash
1800 * (barring an attack on the hash and deliberately-crafted input),
1801 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001802 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001803 /* If hash_size is 0 then hash may be NULL and then the
1804 * call to memset would have undefined behavior. */
1805 if( hash_size != 0 )
1806 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001807
1808 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001809 {
1810 status = PSA_ERROR_BUFFER_TOO_SMALL;
1811 goto exit;
1812 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001813
1814 switch( operation->alg )
1815 {
1816#if defined(MBEDTLS_MD2_C)
1817 case PSA_ALG_MD2:
1818 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1819 break;
1820#endif
1821#if defined(MBEDTLS_MD4_C)
1822 case PSA_ALG_MD4:
1823 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1824 break;
1825#endif
1826#if defined(MBEDTLS_MD5_C)
1827 case PSA_ALG_MD5:
1828 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1829 break;
1830#endif
1831#if defined(MBEDTLS_RIPEMD160_C)
1832 case PSA_ALG_RIPEMD160:
1833 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1834 break;
1835#endif
1836#if defined(MBEDTLS_SHA1_C)
1837 case PSA_ALG_SHA_1:
1838 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1839 break;
1840#endif
1841#if defined(MBEDTLS_SHA256_C)
1842 case PSA_ALG_SHA_224:
1843 case PSA_ALG_SHA_256:
1844 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1845 break;
1846#endif
1847#if defined(MBEDTLS_SHA512_C)
1848 case PSA_ALG_SHA_384:
1849 case PSA_ALG_SHA_512:
1850 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
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 }
itayzafrir40835d42018-08-02 13:14:17 +03001856 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001857
itayzafrir40835d42018-08-02 13:14:17 +03001858exit:
1859 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001860 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001861 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001862 return( psa_hash_abort( operation ) );
1863 }
1864 else
1865 {
1866 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001867 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001868 }
1869}
1870
Gilles Peskine2d277862018-06-18 15:41:12 +02001871psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1872 const uint8_t *hash,
1873 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001874{
1875 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1876 size_t actual_hash_length;
1877 psa_status_t status = psa_hash_finish( operation,
1878 actual_hash, sizeof( actual_hash ),
1879 &actual_hash_length );
1880 if( status != PSA_SUCCESS )
1881 return( status );
1882 if( actual_hash_length != hash_length )
1883 return( PSA_ERROR_INVALID_SIGNATURE );
1884 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1885 return( PSA_ERROR_INVALID_SIGNATURE );
1886 return( PSA_SUCCESS );
1887}
1888
Gilles Peskineeb35d782019-01-22 17:56:16 +01001889psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1890 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001891{
1892 if( target_operation->alg != 0 )
1893 return( PSA_ERROR_BAD_STATE );
1894
1895 switch( source_operation->alg )
1896 {
1897 case 0:
1898 return( PSA_ERROR_BAD_STATE );
1899#if defined(MBEDTLS_MD2_C)
1900 case PSA_ALG_MD2:
1901 mbedtls_md2_clone( &target_operation->ctx.md2,
1902 &source_operation->ctx.md2 );
1903 break;
1904#endif
1905#if defined(MBEDTLS_MD4_C)
1906 case PSA_ALG_MD4:
1907 mbedtls_md4_clone( &target_operation->ctx.md4,
1908 &source_operation->ctx.md4 );
1909 break;
1910#endif
1911#if defined(MBEDTLS_MD5_C)
1912 case PSA_ALG_MD5:
1913 mbedtls_md5_clone( &target_operation->ctx.md5,
1914 &source_operation->ctx.md5 );
1915 break;
1916#endif
1917#if defined(MBEDTLS_RIPEMD160_C)
1918 case PSA_ALG_RIPEMD160:
1919 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1920 &source_operation->ctx.ripemd160 );
1921 break;
1922#endif
1923#if defined(MBEDTLS_SHA1_C)
1924 case PSA_ALG_SHA_1:
1925 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1926 &source_operation->ctx.sha1 );
1927 break;
1928#endif
1929#if defined(MBEDTLS_SHA256_C)
1930 case PSA_ALG_SHA_224:
1931 case PSA_ALG_SHA_256:
1932 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1933 &source_operation->ctx.sha256 );
1934 break;
1935#endif
1936#if defined(MBEDTLS_SHA512_C)
1937 case PSA_ALG_SHA_384:
1938 case PSA_ALG_SHA_512:
1939 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1940 &source_operation->ctx.sha512 );
1941 break;
1942#endif
1943 default:
1944 return( PSA_ERROR_NOT_SUPPORTED );
1945 }
1946
1947 target_operation->alg = source_operation->alg;
1948 return( PSA_SUCCESS );
1949}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001950
1951
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001952/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001953/* MAC */
1954/****************************************************************/
1955
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001956static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001957 psa_algorithm_t alg,
1958 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001959 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001960 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001961{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001962 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001963 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001965 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001966 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001967
Gilles Peskine8c9def32018-02-08 10:02:12 +01001968 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1969 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001970 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001971 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001972 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02001973 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001974 mode = MBEDTLS_MODE_STREAM;
1975 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001976 case PSA_ALG_CTR:
1977 mode = MBEDTLS_MODE_CTR;
1978 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001979 case PSA_ALG_CFB:
1980 mode = MBEDTLS_MODE_CFB;
1981 break;
1982 case PSA_ALG_OFB:
1983 mode = MBEDTLS_MODE_OFB;
1984 break;
1985 case PSA_ALG_CBC_NO_PADDING:
1986 mode = MBEDTLS_MODE_CBC;
1987 break;
1988 case PSA_ALG_CBC_PKCS7:
1989 mode = MBEDTLS_MODE_CBC;
1990 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001991 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001992 mode = MBEDTLS_MODE_CCM;
1993 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001994 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 mode = MBEDTLS_MODE_GCM;
1996 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02001997 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
1998 mode = MBEDTLS_MODE_CHACHAPOLY;
1999 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000 default:
2001 return( NULL );
2002 }
2003 }
2004 else if( alg == PSA_ALG_CMAC )
2005 mode = MBEDTLS_MODE_ECB;
2006 else if( alg == PSA_ALG_GMAC )
2007 mode = MBEDTLS_MODE_GCM;
2008 else
2009 return( NULL );
2010
2011 switch( key_type )
2012 {
2013 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002014 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 break;
2016 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002017 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2018 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002020 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002022 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002023 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2024 * but two-key Triple-DES is functionally three-key Triple-DES
2025 * with K1=K3, so that's how we present it to mbedtls. */
2026 if( key_bits == 128 )
2027 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028 break;
2029 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002030 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031 break;
2032 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002033 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002034 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002035 case PSA_KEY_TYPE_CHACHA20:
2036 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2037 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038 default:
2039 return( NULL );
2040 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002041 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002042 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043
Jaeden Amero23bbb752018-06-26 14:16:54 +01002044 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2045 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046}
2047
Gilles Peskinea05219c2018-11-16 16:02:56 +01002048#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002049static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002050{
Gilles Peskine2d277862018-06-18 15:41:12 +02002051 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002052 {
2053 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002054 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002055 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002056 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002057 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002058 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002059 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002060 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002061 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002062 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002063 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002064 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002065 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002066 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002067 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002068 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002069 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002070 return( 128 );
2071 default:
2072 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002073 }
2074}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002075#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002076
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002077/* Initialize the MAC operation structure. Once this function has been
2078 * called, psa_mac_abort can run and will do the right thing. */
2079static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2080 psa_algorithm_t alg )
2081{
2082 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2083
2084 operation->alg = alg;
2085 operation->key_set = 0;
2086 operation->iv_set = 0;
2087 operation->iv_required = 0;
2088 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002089 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002090
2091#if defined(MBEDTLS_CMAC_C)
2092 if( alg == PSA_ALG_CMAC )
2093 {
2094 operation->iv_required = 0;
2095 mbedtls_cipher_init( &operation->ctx.cmac );
2096 status = PSA_SUCCESS;
2097 }
2098 else
2099#endif /* MBEDTLS_CMAC_C */
2100#if defined(MBEDTLS_MD_C)
2101 if( PSA_ALG_IS_HMAC( operation->alg ) )
2102 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002103 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2104 operation->ctx.hmac.hash_ctx.alg = 0;
2105 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002106 }
2107 else
2108#endif /* MBEDTLS_MD_C */
2109 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002110 if( ! PSA_ALG_IS_MAC( alg ) )
2111 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112 }
2113
2114 if( status != PSA_SUCCESS )
2115 memset( operation, 0, sizeof( *operation ) );
2116 return( status );
2117}
2118
Gilles Peskine01126fa2018-07-12 17:04:55 +02002119#if defined(MBEDTLS_MD_C)
2120static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2121{
Gilles Peskine3f108122018-12-07 18:14:53 +01002122 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002123 return( psa_hash_abort( &hmac->hash_ctx ) );
2124}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002125
2126static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2127{
2128 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2129 memset( hmac, 0, sizeof( *hmac ) );
2130}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002131#endif /* MBEDTLS_MD_C */
2132
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2134{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002135 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002137 /* The object has (apparently) been initialized but it is not
2138 * in use. It's ok to call abort on such an object, and there's
2139 * nothing to do. */
2140 return( PSA_SUCCESS );
2141 }
2142 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002144 if( operation->alg == PSA_ALG_CMAC )
2145 {
2146 mbedtls_cipher_free( &operation->ctx.cmac );
2147 }
2148 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002149#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002151 if( PSA_ALG_IS_HMAC( operation->alg ) )
2152 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002153 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002154 }
2155 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002157 {
2158 /* Sanity check (shouldn't happen: operation->alg should
2159 * always have been initialized to a valid value). */
2160 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002161 }
Moran Peker41deec42018-04-04 15:43:05 +03002162
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163 operation->alg = 0;
2164 operation->key_set = 0;
2165 operation->iv_set = 0;
2166 operation->iv_required = 0;
2167 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002168 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002169
Gilles Peskine8c9def32018-02-08 10:02:12 +01002170 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002171
2172bad_state:
2173 /* If abort is called on an uninitialized object, we can't trust
2174 * anything. Wipe the object in case it contains confidential data.
2175 * This may result in a memory leak if a pointer gets overwritten,
2176 * but it's too late to do anything about this. */
2177 memset( operation, 0, sizeof( *operation ) );
2178 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002179}
2180
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002181#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002182static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002183 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002184 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002185 const mbedtls_cipher_info_t *cipher_info )
2186{
2187 int ret;
2188
2189 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002190
2191 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2192 if( ret != 0 )
2193 return( ret );
2194
2195 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2196 slot->data.raw.data,
2197 key_bits );
2198 return( ret );
2199}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002200#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002201
Gilles Peskine248051a2018-06-20 16:09:38 +02002202#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002203static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2204 const uint8_t *key,
2205 size_t key_length,
2206 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002207{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002208 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002209 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002210 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002211 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002212 psa_status_t status;
2213
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002214 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2215 * overflow below. This should never trigger if the hash algorithm
2216 * is implemented correctly. */
2217 /* The size checks against the ipad and opad buffers cannot be written
2218 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2219 * because that triggers -Wlogical-op on GCC 7.3. */
2220 if( block_size > sizeof( ipad ) )
2221 return( PSA_ERROR_NOT_SUPPORTED );
2222 if( block_size > sizeof( hmac->opad ) )
2223 return( PSA_ERROR_NOT_SUPPORTED );
2224 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002225 return( PSA_ERROR_NOT_SUPPORTED );
2226
Gilles Peskined223b522018-06-11 18:12:58 +02002227 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002228 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002229 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002230 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002231 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002232 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002233 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002234 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002235 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002236 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002237 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002238 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002239 }
Gilles Peskine96889972018-07-12 17:07:03 +02002240 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2241 * but it is permitted. It is common when HMAC is used in HKDF, for
2242 * example. Don't call `memcpy` in the 0-length because `key` could be
2243 * an invalid pointer which would make the behavior undefined. */
2244 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002245 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002246
Gilles Peskined223b522018-06-11 18:12:58 +02002247 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2248 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002249 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002250 ipad[i] ^= 0x36;
2251 memset( ipad + key_length, 0x36, block_size - key_length );
2252
2253 /* Copy the key material from ipad to opad, flipping the requisite bits,
2254 * and filling the rest of opad with the requisite constant. */
2255 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002256 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2257 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002258
Gilles Peskine01126fa2018-07-12 17:04:55 +02002259 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002260 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002261 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002262
Gilles Peskine01126fa2018-07-12 17:04:55 +02002263 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002264
2265cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002266 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002267
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002268 return( status );
2269}
Gilles Peskine248051a2018-06-20 16:09:38 +02002270#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002271
Gilles Peskine89167cb2018-07-08 20:12:23 +02002272static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002273 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002274 psa_algorithm_t alg,
2275 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002278 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002279 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002280 psa_key_usage_t usage =
2281 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002282 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002283 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002284
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002285 /* A context must be freshly initialized before it can be set up. */
2286 if( operation->alg != 0 )
2287 {
2288 return( PSA_ERROR_BAD_STATE );
2289 }
2290
Gilles Peskined911eb72018-08-14 15:18:45 +02002291 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292 if( status != PSA_SUCCESS )
2293 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002294 if( is_sign )
2295 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296
Gilles Peskinec5487a82018-12-03 18:08:14 +01002297 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002298 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002299 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002300 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002301
Gilles Peskine8c9def32018-02-08 10:02:12 +01002302#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002303 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002304 {
2305 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002306 mbedtls_cipher_info_from_psa( full_length_alg,
2307 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002308 int ret;
2309 if( cipher_info == NULL )
2310 {
2311 status = PSA_ERROR_NOT_SUPPORTED;
2312 goto exit;
2313 }
2314 operation->mac_size = cipher_info->block_size;
2315 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2316 status = mbedtls_to_psa_error( ret );
2317 }
2318 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002321 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002322 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002323 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002324 if( hash_alg == 0 )
2325 {
2326 status = PSA_ERROR_NOT_SUPPORTED;
2327 goto exit;
2328 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002329
2330 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2331 /* Sanity check. This shouldn't fail on a valid configuration. */
2332 if( operation->mac_size == 0 ||
2333 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2334 {
2335 status = PSA_ERROR_NOT_SUPPORTED;
2336 goto exit;
2337 }
2338
Gilles Peskine01126fa2018-07-12 17:04:55 +02002339 if( slot->type != PSA_KEY_TYPE_HMAC )
2340 {
2341 status = PSA_ERROR_INVALID_ARGUMENT;
2342 goto exit;
2343 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002344
Gilles Peskine01126fa2018-07-12 17:04:55 +02002345 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2346 slot->data.raw.data,
2347 slot->data.raw.bytes,
2348 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002349 }
2350 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002352 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002353 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002354 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002356
Gilles Peskined911eb72018-08-14 15:18:45 +02002357 if( truncated == 0 )
2358 {
2359 /* The "normal" case: untruncated algorithm. Nothing to do. */
2360 }
2361 else if( truncated < 4 )
2362 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002363 /* A very short MAC is too short for security since it can be
2364 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2365 * so we make this our minimum, even though 32 bits is still
2366 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002367 status = PSA_ERROR_NOT_SUPPORTED;
2368 }
2369 else if( truncated > operation->mac_size )
2370 {
2371 /* It's impossible to "truncate" to a larger length. */
2372 status = PSA_ERROR_INVALID_ARGUMENT;
2373 }
2374 else
2375 operation->mac_size = truncated;
2376
Gilles Peskinefbfac682018-07-08 20:51:54 +02002377exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002378 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002379 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002380 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002381 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002382 else
2383 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002384 operation->key_set = 1;
2385 }
2386 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387}
2388
Gilles Peskine89167cb2018-07-08 20:12:23 +02002389psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002390 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002391 psa_algorithm_t alg )
2392{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002393 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002394}
2395
2396psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002397 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002398 psa_algorithm_t alg )
2399{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002400 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002401}
2402
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2404 const uint8_t *input,
2405 size_t input_length )
2406{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002407 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002409 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002411 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412 operation->has_input = 1;
2413
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002415 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002416 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002417 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2418 input, input_length );
2419 status = mbedtls_to_psa_error( ret );
2420 }
2421 else
2422#endif /* MBEDTLS_CMAC_C */
2423#if defined(MBEDTLS_MD_C)
2424 if( PSA_ALG_IS_HMAC( operation->alg ) )
2425 {
2426 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2427 input_length );
2428 }
2429 else
2430#endif /* MBEDTLS_MD_C */
2431 {
2432 /* This shouldn't happen if `operation` was initialized by
2433 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002434 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002435 }
2436
Gilles Peskinefbfac682018-07-08 20:51:54 +02002437 if( status != PSA_SUCCESS )
2438 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002439 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440}
2441
Gilles Peskine01126fa2018-07-12 17:04:55 +02002442#if defined(MBEDTLS_MD_C)
2443static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2444 uint8_t *mac,
2445 size_t mac_size )
2446{
2447 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2448 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2449 size_t hash_size = 0;
2450 size_t block_size = psa_get_hash_block_size( hash_alg );
2451 psa_status_t status;
2452
Gilles Peskine01126fa2018-07-12 17:04:55 +02002453 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2454 if( status != PSA_SUCCESS )
2455 return( status );
2456 /* From here on, tmp needs to be wiped. */
2457
2458 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2459 if( status != PSA_SUCCESS )
2460 goto exit;
2461
2462 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2463 if( status != PSA_SUCCESS )
2464 goto exit;
2465
2466 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2467 if( status != PSA_SUCCESS )
2468 goto exit;
2469
Gilles Peskined911eb72018-08-14 15:18:45 +02002470 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2471 if( status != PSA_SUCCESS )
2472 goto exit;
2473
2474 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002475
2476exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002477 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002478 return( status );
2479}
2480#endif /* MBEDTLS_MD_C */
2481
mohammad16036df908f2018-04-02 08:34:15 -07002482static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002483 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002484 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002486 if( ! operation->key_set )
2487 return( PSA_ERROR_BAD_STATE );
2488 if( operation->iv_required && ! operation->iv_set )
2489 return( PSA_ERROR_BAD_STATE );
2490
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 if( mac_size < operation->mac_size )
2492 return( PSA_ERROR_BUFFER_TOO_SMALL );
2493
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002495 if( operation->alg == PSA_ALG_CMAC )
2496 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002497 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2498 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2499 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002500 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002501 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002502 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002504 else
2505#endif /* MBEDTLS_CMAC_C */
2506#if defined(MBEDTLS_MD_C)
2507 if( PSA_ALG_IS_HMAC( operation->alg ) )
2508 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002509 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002510 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002511 }
2512 else
2513#endif /* MBEDTLS_MD_C */
2514 {
2515 /* This shouldn't happen if `operation` was initialized by
2516 * a setup function. */
2517 return( PSA_ERROR_BAD_STATE );
2518 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519}
2520
Gilles Peskineacd4be32018-07-08 19:56:25 +02002521psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2522 uint8_t *mac,
2523 size_t mac_size,
2524 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002525{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002526 psa_status_t status;
2527
Jaeden Amero252ef282019-02-15 14:05:35 +00002528 if( operation->alg == 0 )
2529 {
2530 return( PSA_ERROR_BAD_STATE );
2531 }
2532
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002533 /* Fill the output buffer with something that isn't a valid mac
2534 * (barring an attack on the mac and deliberately-crafted input),
2535 * in case the caller doesn't check the return status properly. */
2536 *mac_length = mac_size;
2537 /* If mac_size is 0 then mac may be NULL and then the
2538 * call to memset would have undefined behavior. */
2539 if( mac_size != 0 )
2540 memset( mac, '!', mac_size );
2541
Gilles Peskine89167cb2018-07-08 20:12:23 +02002542 if( ! operation->is_sign )
2543 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002544 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002545 }
mohammad16036df908f2018-04-02 08:34:15 -07002546
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002547 status = psa_mac_finish_internal( operation, mac, mac_size );
2548
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002549 if( status == PSA_SUCCESS )
2550 {
2551 status = psa_mac_abort( operation );
2552 if( status == PSA_SUCCESS )
2553 *mac_length = operation->mac_size;
2554 else
2555 memset( mac, '!', mac_size );
2556 }
2557 else
2558 psa_mac_abort( operation );
2559 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002560}
2561
Gilles Peskineacd4be32018-07-08 19:56:25 +02002562psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2563 const uint8_t *mac,
2564 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565{
Gilles Peskine828ed142018-06-18 23:25:51 +02002566 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002567 psa_status_t status;
2568
Jaeden Amero252ef282019-02-15 14:05:35 +00002569 if( operation->alg == 0 )
2570 {
2571 return( PSA_ERROR_BAD_STATE );
2572 }
2573
Gilles Peskine89167cb2018-07-08 20:12:23 +02002574 if( operation->is_sign )
2575 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002576 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002577 }
2578 if( operation->mac_size != mac_length )
2579 {
2580 status = PSA_ERROR_INVALID_SIGNATURE;
2581 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002582 }
mohammad16036df908f2018-04-02 08:34:15 -07002583
2584 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002585 actual_mac, sizeof( actual_mac ) );
2586
2587 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2588 status = PSA_ERROR_INVALID_SIGNATURE;
2589
2590cleanup:
2591 if( status == PSA_SUCCESS )
2592 status = psa_mac_abort( operation );
2593 else
2594 psa_mac_abort( operation );
2595
Gilles Peskine3f108122018-12-07 18:14:53 +01002596 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002597
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002598 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599}
2600
2601
Gilles Peskine20035e32018-02-03 22:44:14 +01002602
Gilles Peskine20035e32018-02-03 22:44:14 +01002603/****************************************************************/
2604/* Asymmetric cryptography */
2605/****************************************************************/
2606
Gilles Peskine2b450e32018-06-27 15:42:46 +02002607#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002608/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002609 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002610static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2611 size_t hash_length,
2612 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002613{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002614 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002615 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002616 *md_alg = mbedtls_md_get_type( md_info );
2617
2618 /* The Mbed TLS RSA module uses an unsigned int for hash length
2619 * parameters. Validate that it fits so that we don't risk an
2620 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002621#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002622 if( hash_length > UINT_MAX )
2623 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002624#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002625
2626#if defined(MBEDTLS_PKCS1_V15)
2627 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2628 * must be correct. */
2629 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2630 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002631 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002632 if( md_info == NULL )
2633 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002634 if( mbedtls_md_get_size( md_info ) != hash_length )
2635 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002636 }
2637#endif /* MBEDTLS_PKCS1_V15 */
2638
2639#if defined(MBEDTLS_PKCS1_V21)
2640 /* PSS requires a hash internally. */
2641 if( PSA_ALG_IS_RSA_PSS( alg ) )
2642 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002643 if( md_info == NULL )
2644 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002645 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002646#endif /* MBEDTLS_PKCS1_V21 */
2647
Gilles Peskine61b91d42018-06-08 16:09:36 +02002648 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002649}
2650
Gilles Peskine2b450e32018-06-27 15:42:46 +02002651static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2652 psa_algorithm_t alg,
2653 const uint8_t *hash,
2654 size_t hash_length,
2655 uint8_t *signature,
2656 size_t signature_size,
2657 size_t *signature_length )
2658{
2659 psa_status_t status;
2660 int ret;
2661 mbedtls_md_type_t md_alg;
2662
2663 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2664 if( status != PSA_SUCCESS )
2665 return( status );
2666
Gilles Peskine630a18a2018-06-29 17:49:35 +02002667 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002668 return( PSA_ERROR_BUFFER_TOO_SMALL );
2669
2670#if defined(MBEDTLS_PKCS1_V15)
2671 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2672 {
2673 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2674 MBEDTLS_MD_NONE );
2675 ret = mbedtls_rsa_pkcs1_sign( rsa,
2676 mbedtls_ctr_drbg_random,
2677 &global_data.ctr_drbg,
2678 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002679 md_alg,
2680 (unsigned int) hash_length,
2681 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002682 signature );
2683 }
2684 else
2685#endif /* MBEDTLS_PKCS1_V15 */
2686#if defined(MBEDTLS_PKCS1_V21)
2687 if( PSA_ALG_IS_RSA_PSS( alg ) )
2688 {
2689 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2690 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2691 mbedtls_ctr_drbg_random,
2692 &global_data.ctr_drbg,
2693 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002694 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002695 (unsigned int) hash_length,
2696 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002697 signature );
2698 }
2699 else
2700#endif /* MBEDTLS_PKCS1_V21 */
2701 {
2702 return( PSA_ERROR_INVALID_ARGUMENT );
2703 }
2704
2705 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002706 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002707 return( mbedtls_to_psa_error( ret ) );
2708}
2709
2710static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2711 psa_algorithm_t alg,
2712 const uint8_t *hash,
2713 size_t hash_length,
2714 const uint8_t *signature,
2715 size_t signature_length )
2716{
2717 psa_status_t status;
2718 int ret;
2719 mbedtls_md_type_t md_alg;
2720
2721 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2722 if( status != PSA_SUCCESS )
2723 return( status );
2724
Gilles Peskine630a18a2018-06-29 17:49:35 +02002725 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002726 return( PSA_ERROR_BUFFER_TOO_SMALL );
2727
2728#if defined(MBEDTLS_PKCS1_V15)
2729 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2730 {
2731 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2732 MBEDTLS_MD_NONE );
2733 ret = mbedtls_rsa_pkcs1_verify( rsa,
2734 mbedtls_ctr_drbg_random,
2735 &global_data.ctr_drbg,
2736 MBEDTLS_RSA_PUBLIC,
2737 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002738 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002739 hash,
2740 signature );
2741 }
2742 else
2743#endif /* MBEDTLS_PKCS1_V15 */
2744#if defined(MBEDTLS_PKCS1_V21)
2745 if( PSA_ALG_IS_RSA_PSS( alg ) )
2746 {
2747 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2748 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2749 mbedtls_ctr_drbg_random,
2750 &global_data.ctr_drbg,
2751 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002752 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002753 (unsigned int) hash_length,
2754 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002755 signature );
2756 }
2757 else
2758#endif /* MBEDTLS_PKCS1_V21 */
2759 {
2760 return( PSA_ERROR_INVALID_ARGUMENT );
2761 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002762
2763 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2764 * the rest of the signature is invalid". This has little use in
2765 * practice and PSA doesn't report this distinction. */
2766 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2767 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002768 return( mbedtls_to_psa_error( ret ) );
2769}
2770#endif /* MBEDTLS_RSA_C */
2771
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002772#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002773/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2774 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2775 * (even though these functions don't modify it). */
2776static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2777 psa_algorithm_t alg,
2778 const uint8_t *hash,
2779 size_t hash_length,
2780 uint8_t *signature,
2781 size_t signature_size,
2782 size_t *signature_length )
2783{
2784 int ret;
2785 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002786 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002787 mbedtls_mpi_init( &r );
2788 mbedtls_mpi_init( &s );
2789
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002790 if( signature_size < 2 * curve_bytes )
2791 {
2792 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2793 goto cleanup;
2794 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002795
Gilles Peskinea05219c2018-11-16 16:02:56 +01002796#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002797 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2798 {
2799 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2800 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2801 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2802 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2803 hash, hash_length,
2804 md_alg ) );
2805 }
2806 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002807#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002808 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002809 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002810 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2811 hash, hash_length,
2812 mbedtls_ctr_drbg_random,
2813 &global_data.ctr_drbg ) );
2814 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002815
2816 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2817 signature,
2818 curve_bytes ) );
2819 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2820 signature + curve_bytes,
2821 curve_bytes ) );
2822
2823cleanup:
2824 mbedtls_mpi_free( &r );
2825 mbedtls_mpi_free( &s );
2826 if( ret == 0 )
2827 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002828 return( mbedtls_to_psa_error( ret ) );
2829}
2830
2831static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2832 const uint8_t *hash,
2833 size_t hash_length,
2834 const uint8_t *signature,
2835 size_t signature_length )
2836{
2837 int ret;
2838 mbedtls_mpi r, s;
2839 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2840 mbedtls_mpi_init( &r );
2841 mbedtls_mpi_init( &s );
2842
2843 if( signature_length != 2 * curve_bytes )
2844 return( PSA_ERROR_INVALID_SIGNATURE );
2845
2846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2847 signature,
2848 curve_bytes ) );
2849 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2850 signature + curve_bytes,
2851 curve_bytes ) );
2852
2853 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2854 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002855
2856cleanup:
2857 mbedtls_mpi_free( &r );
2858 mbedtls_mpi_free( &s );
2859 return( mbedtls_to_psa_error( ret ) );
2860}
2861#endif /* MBEDTLS_ECDSA_C */
2862
Gilles Peskinec5487a82018-12-03 18:08:14 +01002863psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002864 psa_algorithm_t alg,
2865 const uint8_t *hash,
2866 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002867 uint8_t *signature,
2868 size_t signature_size,
2869 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002870{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002871 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002872 psa_status_t status;
2873
2874 *signature_length = signature_size;
2875
Gilles Peskinec5487a82018-12-03 18:08:14 +01002876 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002877 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002878 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002879 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002880 {
2881 status = PSA_ERROR_INVALID_ARGUMENT;
2882 goto exit;
2883 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002884
Gilles Peskine20035e32018-02-03 22:44:14 +01002885#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002886 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002887 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002888 status = psa_rsa_sign( slot->data.rsa,
2889 alg,
2890 hash, hash_length,
2891 signature, signature_size,
2892 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002893 }
2894 else
2895#endif /* defined(MBEDTLS_RSA_C) */
2896#if defined(MBEDTLS_ECP_C)
2897 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2898 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002899#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002900 if(
2901#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2902 PSA_ALG_IS_ECDSA( alg )
2903#else
2904 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2905#endif
2906 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002907 status = psa_ecdsa_sign( slot->data.ecp,
2908 alg,
2909 hash, hash_length,
2910 signature, signature_size,
2911 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002912 else
2913#endif /* defined(MBEDTLS_ECDSA_C) */
2914 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002915 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002916 }
itayzafrir5c753392018-05-08 11:18:38 +03002917 }
2918 else
2919#endif /* defined(MBEDTLS_ECP_C) */
2920 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002921 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002922 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002923
2924exit:
2925 /* Fill the unused part of the output buffer (the whole buffer on error,
2926 * the trailing part on success) with something that isn't a valid mac
2927 * (barring an attack on the mac and deliberately-crafted input),
2928 * in case the caller doesn't check the return status properly. */
2929 if( status == PSA_SUCCESS )
2930 memset( signature + *signature_length, '!',
2931 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002932 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002933 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002934 /* If signature_size is 0 then we have nothing to do. We must not call
2935 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002936 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002937}
2938
Gilles Peskinec5487a82018-12-03 18:08:14 +01002939psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002940 psa_algorithm_t alg,
2941 const uint8_t *hash,
2942 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002943 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002944 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002945{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002946 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002947 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002948
Gilles Peskinec5487a82018-12-03 18:08:14 +01002949 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002950 if( status != PSA_SUCCESS )
2951 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002952
Gilles Peskine61b91d42018-06-08 16:09:36 +02002953#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002954 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002956 return( psa_rsa_verify( slot->data.rsa,
2957 alg,
2958 hash, hash_length,
2959 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002960 }
2961 else
2962#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002963#if defined(MBEDTLS_ECP_C)
2964 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2965 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002966#if defined(MBEDTLS_ECDSA_C)
2967 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002968 return( psa_ecdsa_verify( slot->data.ecp,
2969 hash, hash_length,
2970 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002971 else
2972#endif /* defined(MBEDTLS_ECDSA_C) */
2973 {
2974 return( PSA_ERROR_INVALID_ARGUMENT );
2975 }
itayzafrir5c753392018-05-08 11:18:38 +03002976 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002977 else
2978#endif /* defined(MBEDTLS_ECP_C) */
2979 {
2980 return( PSA_ERROR_NOT_SUPPORTED );
2981 }
2982}
2983
Gilles Peskine072ac562018-06-30 00:21:29 +02002984#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2985static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2986 mbedtls_rsa_context *rsa )
2987{
2988 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2989 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2990 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2991 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2992}
2993#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2994
Gilles Peskinec5487a82018-12-03 18:08:14 +01002995psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002996 psa_algorithm_t alg,
2997 const uint8_t *input,
2998 size_t input_length,
2999 const uint8_t *salt,
3000 size_t salt_length,
3001 uint8_t *output,
3002 size_t output_size,
3003 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003005 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003006 psa_status_t status;
3007
Darryl Green5cc689a2018-07-24 15:34:10 +01003008 (void) input;
3009 (void) input_length;
3010 (void) salt;
3011 (void) output;
3012 (void) output_size;
3013
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003014 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003016 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3017 return( PSA_ERROR_INVALID_ARGUMENT );
3018
Gilles Peskinec5487a82018-12-03 18:08:14 +01003019 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003020 if( status != PSA_SUCCESS )
3021 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003022 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003023 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025
3026#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003027 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028 {
3029 mbedtls_rsa_context *rsa = slot->data.rsa;
3030 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003031 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003032 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003034 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003036 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3037 mbedtls_ctr_drbg_random,
3038 &global_data.ctr_drbg,
3039 MBEDTLS_RSA_PUBLIC,
3040 input_length,
3041 input,
3042 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043 }
3044 else
3045#endif /* MBEDTLS_PKCS1_V15 */
3046#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003047 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003049 psa_rsa_oaep_set_padding_mode( alg, rsa );
3050 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3051 mbedtls_ctr_drbg_random,
3052 &global_data.ctr_drbg,
3053 MBEDTLS_RSA_PUBLIC,
3054 salt, salt_length,
3055 input_length,
3056 input,
3057 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058 }
3059 else
3060#endif /* MBEDTLS_PKCS1_V21 */
3061 {
3062 return( PSA_ERROR_INVALID_ARGUMENT );
3063 }
3064 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003065 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003066 return( mbedtls_to_psa_error( ret ) );
3067 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003069#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070 {
3071 return( PSA_ERROR_NOT_SUPPORTED );
3072 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003073}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074
Gilles Peskinec5487a82018-12-03 18:08:14 +01003075psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003076 psa_algorithm_t alg,
3077 const uint8_t *input,
3078 size_t input_length,
3079 const uint8_t *salt,
3080 size_t salt_length,
3081 uint8_t *output,
3082 size_t output_size,
3083 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003084{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003085 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003086 psa_status_t status;
3087
Darryl Green5cc689a2018-07-24 15:34:10 +01003088 (void) input;
3089 (void) input_length;
3090 (void) salt;
3091 (void) output;
3092 (void) output_size;
3093
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003094 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003096 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3097 return( PSA_ERROR_INVALID_ARGUMENT );
3098
Gilles Peskinec5487a82018-12-03 18:08:14 +01003099 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003100 if( status != PSA_SUCCESS )
3101 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003102 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103 return( PSA_ERROR_INVALID_ARGUMENT );
3104
3105#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003106 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107 {
3108 mbedtls_rsa_context *rsa = slot->data.rsa;
3109 int ret;
3110
Gilles Peskine630a18a2018-06-29 17:49:35 +02003111 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003112 return( PSA_ERROR_INVALID_ARGUMENT );
3113
3114#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003115 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003117 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3118 mbedtls_ctr_drbg_random,
3119 &global_data.ctr_drbg,
3120 MBEDTLS_RSA_PRIVATE,
3121 output_length,
3122 input,
3123 output,
3124 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 }
3126 else
3127#endif /* MBEDTLS_PKCS1_V15 */
3128#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003129 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003131 psa_rsa_oaep_set_padding_mode( alg, rsa );
3132 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3133 mbedtls_ctr_drbg_random,
3134 &global_data.ctr_drbg,
3135 MBEDTLS_RSA_PRIVATE,
3136 salt, salt_length,
3137 output_length,
3138 input,
3139 output,
3140 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141 }
3142 else
3143#endif /* MBEDTLS_PKCS1_V21 */
3144 {
3145 return( PSA_ERROR_INVALID_ARGUMENT );
3146 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003147
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148 return( mbedtls_to_psa_error( ret ) );
3149 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003151#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 {
3153 return( PSA_ERROR_NOT_SUPPORTED );
3154 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003155}
Gilles Peskine20035e32018-02-03 22:44:14 +01003156
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003157
3158
mohammad1603503973b2018-03-12 15:59:30 +02003159/****************************************************************/
3160/* Symmetric cryptography */
3161/****************************************************************/
3162
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003163/* Initialize the cipher operation structure. Once this function has been
3164 * called, psa_cipher_abort can run and will do the right thing. */
3165static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3166 psa_algorithm_t alg )
3167{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003168 if( ! PSA_ALG_IS_CIPHER( alg ) )
3169 {
3170 memset( operation, 0, sizeof( *operation ) );
3171 return( PSA_ERROR_INVALID_ARGUMENT );
3172 }
3173
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003174 operation->alg = alg;
3175 operation->key_set = 0;
3176 operation->iv_set = 0;
3177 operation->iv_required = 1;
3178 operation->iv_size = 0;
3179 operation->block_size = 0;
3180 mbedtls_cipher_init( &operation->ctx.cipher );
3181 return( PSA_SUCCESS );
3182}
3183
Gilles Peskinee553c652018-06-04 16:22:46 +02003184static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003185 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003186 psa_algorithm_t alg,
3187 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003188{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003189 int ret = 0;
3190 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003191 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003192 size_t key_bits;
3193 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003194 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3195 PSA_KEY_USAGE_ENCRYPT :
3196 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003197
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003198 /* A context must be freshly initialized before it can be set up. */
3199 if( operation->alg != 0 )
3200 {
3201 return( PSA_ERROR_BAD_STATE );
3202 }
3203
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003204 status = psa_cipher_init( operation, alg );
3205 if( status != PSA_SUCCESS )
3206 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003207
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003209 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003210 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003211 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003212
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003213 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003214 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003215 {
3216 status = PSA_ERROR_NOT_SUPPORTED;
3217 goto exit;
3218 }
mohammad1603503973b2018-03-12 15:59:30 +02003219
mohammad1603503973b2018-03-12 15:59:30 +02003220 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003221 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003222 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003223
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003224#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003225 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003226 {
3227 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3228 unsigned char keys[24];
3229 memcpy( keys, slot->data.raw.data, 16 );
3230 memcpy( keys + 16, slot->data.raw.data, 8 );
3231 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3232 keys,
3233 192, cipher_operation );
3234 }
3235 else
3236#endif
3237 {
3238 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3239 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003240 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003241 }
Moran Peker41deec42018-04-04 15:43:05 +03003242 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003243 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003244
mohammad16038481e742018-03-18 13:57:31 +02003245#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003246 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003247 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003248 case PSA_ALG_CBC_NO_PADDING:
3249 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3250 MBEDTLS_PADDING_NONE );
3251 break;
3252 case PSA_ALG_CBC_PKCS7:
3253 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3254 MBEDTLS_PADDING_PKCS7 );
3255 break;
3256 default:
3257 /* The algorithm doesn't involve padding. */
3258 ret = 0;
3259 break;
3260 }
3261 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003262 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003263#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3264
mohammad1603503973b2018-03-12 15:59:30 +02003265 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003266 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3267 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3268 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003269 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003270 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003271 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003272#if defined(MBEDTLS_CHACHA20_C)
3273 else
3274 if( alg == PSA_ALG_CHACHA20 )
3275 operation->iv_size = 12;
3276#endif
mohammad1603503973b2018-03-12 15:59:30 +02003277
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003278exit:
3279 if( status == 0 )
3280 status = mbedtls_to_psa_error( ret );
3281 if( status != 0 )
3282 psa_cipher_abort( operation );
3283 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003284}
3285
Gilles Peskinefe119512018-07-08 21:39:34 +02003286psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003287 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003288 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003289{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003290 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003291}
3292
Gilles Peskinefe119512018-07-08 21:39:34 +02003293psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003294 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003295 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003296{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003297 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003298}
3299
Gilles Peskinefe119512018-07-08 21:39:34 +02003300psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3301 unsigned char *iv,
3302 size_t iv_size,
3303 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003304{
itayzafrir534bd7c2018-08-02 13:56:32 +03003305 psa_status_t status;
3306 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003307 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003308 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003309 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003310 }
Moran Peker41deec42018-04-04 15:43:05 +03003311 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003312 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003313 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003314 goto exit;
3315 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003316 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3317 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003318 if( ret != 0 )
3319 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003320 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003321 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003322 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003323
mohammad16038481e742018-03-18 13:57:31 +02003324 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003325 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003326
Moran Peker395db872018-05-31 14:07:14 +03003327exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003328 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003329 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003330 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003331}
3332
Gilles Peskinefe119512018-07-08 21:39:34 +02003333psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3334 const unsigned char *iv,
3335 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003336{
itayzafrir534bd7c2018-08-02 13:56:32 +03003337 psa_status_t status;
3338 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003339 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003340 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003341 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003342 }
Moran Pekera28258c2018-05-29 16:25:04 +03003343 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003344 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003345 status = PSA_ERROR_INVALID_ARGUMENT;
3346 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003347 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003348 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3349 status = mbedtls_to_psa_error( ret );
3350exit:
3351 if( status == PSA_SUCCESS )
3352 operation->iv_set = 1;
3353 else
mohammad1603503973b2018-03-12 15:59:30 +02003354 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003355 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003356}
3357
Gilles Peskinee553c652018-06-04 16:22:46 +02003358psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3359 const uint8_t *input,
3360 size_t input_length,
3361 unsigned char *output,
3362 size_t output_size,
3363 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003364{
itayzafrir534bd7c2018-08-02 13:56:32 +03003365 psa_status_t status;
3366 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003367 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003368
3369 if( operation->alg == 0 )
3370 {
3371 return( PSA_ERROR_BAD_STATE );
3372 }
3373
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003374 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003375 {
3376 /* Take the unprocessed partial block left over from previous
3377 * update calls, if any, plus the input to this call. Remove
3378 * the last partial block, if any. You get the data that will be
3379 * output in this call. */
3380 expected_output_size =
3381 ( operation->ctx.cipher.unprocessed_len + input_length )
3382 / operation->block_size * operation->block_size;
3383 }
3384 else
3385 {
3386 expected_output_size = input_length;
3387 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003388
Gilles Peskine89d789c2018-06-04 17:17:16 +02003389 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003390 {
3391 status = PSA_ERROR_BUFFER_TOO_SMALL;
3392 goto exit;
3393 }
mohammad160382759612018-03-12 18:16:40 +02003394
mohammad1603503973b2018-03-12 15:59:30 +02003395 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003396 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003397 status = mbedtls_to_psa_error( ret );
3398exit:
3399 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003400 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003401 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003402}
3403
Gilles Peskinee553c652018-06-04 16:22:46 +02003404psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3405 uint8_t *output,
3406 size_t output_size,
3407 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003408{
David Saadab4ecc272019-02-14 13:48:10 +02003409 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003410 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003411 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003412
mohammad1603503973b2018-03-12 15:59:30 +02003413 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003414 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003415 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003416 }
3417 if( operation->iv_required && ! operation->iv_set )
3418 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003419 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003420 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003421
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003422 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003423 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3424 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003425 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003426 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003427 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003428 }
3429
Janos Follath315b51c2018-07-09 16:04:51 +01003430 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3431 temp_output_buffer,
3432 output_length );
3433 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003434 {
Janos Follath315b51c2018-07-09 16:04:51 +01003435 status = mbedtls_to_psa_error( cipher_ret );
3436 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003437 }
Janos Follath315b51c2018-07-09 16:04:51 +01003438
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003439 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003440 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003441 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003442 memcpy( output, temp_output_buffer, *output_length );
3443 else
3444 {
Janos Follath315b51c2018-07-09 16:04:51 +01003445 status = PSA_ERROR_BUFFER_TOO_SMALL;
3446 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003447 }
mohammad1603503973b2018-03-12 15:59:30 +02003448
Gilles Peskine3f108122018-12-07 18:14:53 +01003449 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003450 status = psa_cipher_abort( operation );
3451
3452 return( status );
3453
3454error:
3455
3456 *output_length = 0;
3457
Gilles Peskine3f108122018-12-07 18:14:53 +01003458 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003459 (void) psa_cipher_abort( operation );
3460
3461 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003462}
3463
Gilles Peskinee553c652018-06-04 16:22:46 +02003464psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3465{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003466 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003467 {
3468 /* The object has (apparently) been initialized but it is not
3469 * in use. It's ok to call abort on such an object, and there's
3470 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003471 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003472 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003473
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003474 /* Sanity check (shouldn't happen: operation->alg should
3475 * always have been initialized to a valid value). */
3476 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3477 return( PSA_ERROR_BAD_STATE );
3478
mohammad1603503973b2018-03-12 15:59:30 +02003479 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003480
Moran Peker41deec42018-04-04 15:43:05 +03003481 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003482 operation->key_set = 0;
3483 operation->iv_set = 0;
3484 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003485 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003486 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003487
Moran Peker395db872018-05-31 14:07:14 +03003488 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003489}
3490
Gilles Peskinea0655c32018-04-30 17:06:50 +02003491
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003492
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003493
mohammad16035955c982018-04-26 00:53:03 +03003494/****************************************************************/
3495/* AEAD */
3496/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003497
Gilles Peskineedf9a652018-08-17 18:11:56 +02003498typedef struct
3499{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003500 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003501 const mbedtls_cipher_info_t *cipher_info;
3502 union
3503 {
3504#if defined(MBEDTLS_CCM_C)
3505 mbedtls_ccm_context ccm;
3506#endif /* MBEDTLS_CCM_C */
3507#if defined(MBEDTLS_GCM_C)
3508 mbedtls_gcm_context gcm;
3509#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003510#if defined(MBEDTLS_CHACHAPOLY_C)
3511 mbedtls_chachapoly_context chachapoly;
3512#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003513 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003514 psa_algorithm_t core_alg;
3515 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003516 uint8_t tag_length;
3517} aead_operation_t;
3518
Gilles Peskine30a9e412019-01-14 18:36:12 +01003519static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003520{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003521 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003522 {
3523#if defined(MBEDTLS_CCM_C)
3524 case PSA_ALG_CCM:
3525 mbedtls_ccm_free( &operation->ctx.ccm );
3526 break;
3527#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003528#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003529 case PSA_ALG_GCM:
3530 mbedtls_gcm_free( &operation->ctx.gcm );
3531 break;
3532#endif /* MBEDTLS_GCM_C */
3533 }
3534}
3535
3536static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003537 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003538 psa_key_usage_t usage,
3539 psa_algorithm_t alg )
3540{
3541 psa_status_t status;
3542 size_t key_bits;
3543 mbedtls_cipher_id_t cipher_id;
3544
Gilles Peskinec5487a82018-12-03 18:08:14 +01003545 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003546 if( status != PSA_SUCCESS )
3547 return( status );
3548
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003549 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003550
3551 operation->cipher_info =
3552 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3553 &cipher_id );
3554 if( operation->cipher_info == NULL )
3555 return( PSA_ERROR_NOT_SUPPORTED );
3556
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003557 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003558 {
3559#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003560 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3561 operation->core_alg = PSA_ALG_CCM;
3562 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003563 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3564 * The call to mbedtls_ccm_encrypt_and_tag or
3565 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003566 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3567 return( PSA_ERROR_INVALID_ARGUMENT );
3568 mbedtls_ccm_init( &operation->ctx.ccm );
3569 status = mbedtls_to_psa_error(
3570 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3571 operation->slot->data.raw.data,
3572 (unsigned int) key_bits ) );
3573 if( status != 0 )
3574 goto cleanup;
3575 break;
3576#endif /* MBEDTLS_CCM_C */
3577
3578#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003579 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3580 operation->core_alg = PSA_ALG_GCM;
3581 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003582 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3583 * The call to mbedtls_gcm_crypt_and_tag or
3584 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003585 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3586 return( PSA_ERROR_INVALID_ARGUMENT );
3587 mbedtls_gcm_init( &operation->ctx.gcm );
3588 status = mbedtls_to_psa_error(
3589 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3590 operation->slot->data.raw.data,
3591 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003592 if( status != 0 )
3593 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003594 break;
3595#endif /* MBEDTLS_GCM_C */
3596
Gilles Peskine26869f22019-05-06 15:25:00 +02003597#if defined(MBEDTLS_CHACHAPOLY_C)
3598 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3599 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3600 operation->full_tag_length = 16;
3601 /* We only support the default tag length. */
3602 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3603 return( PSA_ERROR_NOT_SUPPORTED );
3604 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3605 status = mbedtls_to_psa_error(
3606 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3607 operation->slot->data.raw.data ) );
3608 if( status != 0 )
3609 goto cleanup;
3610 break;
3611#endif /* MBEDTLS_CHACHAPOLY_C */
3612
Gilles Peskineedf9a652018-08-17 18:11:56 +02003613 default:
3614 return( PSA_ERROR_NOT_SUPPORTED );
3615 }
3616
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003617 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3618 {
3619 status = PSA_ERROR_INVALID_ARGUMENT;
3620 goto cleanup;
3621 }
3622 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003623
Gilles Peskineedf9a652018-08-17 18:11:56 +02003624 return( PSA_SUCCESS );
3625
3626cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003627 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003628 return( status );
3629}
3630
Gilles Peskinec5487a82018-12-03 18:08:14 +01003631psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003632 psa_algorithm_t alg,
3633 const uint8_t *nonce,
3634 size_t nonce_length,
3635 const uint8_t *additional_data,
3636 size_t additional_data_length,
3637 const uint8_t *plaintext,
3638 size_t plaintext_length,
3639 uint8_t *ciphertext,
3640 size_t ciphertext_size,
3641 size_t *ciphertext_length )
3642{
mohammad16035955c982018-04-26 00:53:03 +03003643 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003644 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003645 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003646
mohammad1603f08a5502018-06-03 15:05:47 +03003647 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003648
Gilles Peskinec5487a82018-12-03 18:08:14 +01003649 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003650 if( status != PSA_SUCCESS )
3651 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003652
Gilles Peskineedf9a652018-08-17 18:11:56 +02003653 /* For all currently supported modes, the tag is at the end of the
3654 * ciphertext. */
3655 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3656 {
3657 status = PSA_ERROR_BUFFER_TOO_SMALL;
3658 goto exit;
3659 }
3660 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003661
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003662#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003663 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003664 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003665 status = mbedtls_to_psa_error(
3666 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3667 MBEDTLS_GCM_ENCRYPT,
3668 plaintext_length,
3669 nonce, nonce_length,
3670 additional_data, additional_data_length,
3671 plaintext, ciphertext,
3672 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003673 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003674 else
3675#endif /* MBEDTLS_GCM_C */
3676#if defined(MBEDTLS_CCM_C)
3677 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003678 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003679 status = mbedtls_to_psa_error(
3680 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3681 plaintext_length,
3682 nonce, nonce_length,
3683 additional_data,
3684 additional_data_length,
3685 plaintext, ciphertext,
3686 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003687 }
mohammad16035c8845f2018-05-09 05:40:09 -07003688 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003689#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003690#if defined(MBEDTLS_CHACHAPOLY_C)
3691 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3692 {
3693 if( nonce_length != 12 || operation.tag_length != 16 )
3694 {
3695 status = PSA_ERROR_NOT_SUPPORTED;
3696 goto exit;
3697 }
3698 status = mbedtls_to_psa_error(
3699 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3700 plaintext_length,
3701 nonce,
3702 additional_data,
3703 additional_data_length,
3704 plaintext,
3705 ciphertext,
3706 tag ) );
3707 }
3708 else
3709#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003710 {
mohammad1603554faad2018-06-03 15:07:38 +03003711 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003712 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003713
Gilles Peskineedf9a652018-08-17 18:11:56 +02003714 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3715 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003716
Gilles Peskineedf9a652018-08-17 18:11:56 +02003717exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003718 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003719 if( status == PSA_SUCCESS )
3720 *ciphertext_length = plaintext_length + operation.tag_length;
3721 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003722}
3723
Gilles Peskineee652a32018-06-01 19:23:52 +02003724/* Locate the tag in a ciphertext buffer containing the encrypted data
3725 * followed by the tag. Return the length of the part preceding the tag in
3726 * *plaintext_length. This is the size of the plaintext in modes where
3727 * the encrypted data has the same size as the plaintext, such as
3728 * CCM and GCM. */
3729static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3730 const uint8_t *ciphertext,
3731 size_t ciphertext_length,
3732 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003733 const uint8_t **p_tag )
3734{
3735 size_t payload_length;
3736 if( tag_length > ciphertext_length )
3737 return( PSA_ERROR_INVALID_ARGUMENT );
3738 payload_length = ciphertext_length - tag_length;
3739 if( payload_length > plaintext_size )
3740 return( PSA_ERROR_BUFFER_TOO_SMALL );
3741 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003742 return( PSA_SUCCESS );
3743}
3744
Gilles Peskinec5487a82018-12-03 18:08:14 +01003745psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003746 psa_algorithm_t alg,
3747 const uint8_t *nonce,
3748 size_t nonce_length,
3749 const uint8_t *additional_data,
3750 size_t additional_data_length,
3751 const uint8_t *ciphertext,
3752 size_t ciphertext_length,
3753 uint8_t *plaintext,
3754 size_t plaintext_size,
3755 size_t *plaintext_length )
3756{
mohammad16035955c982018-04-26 00:53:03 +03003757 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003758 aead_operation_t operation;
3759 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003760
Gilles Peskineee652a32018-06-01 19:23:52 +02003761 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003762
Gilles Peskinec5487a82018-12-03 18:08:14 +01003763 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003764 if( status != PSA_SUCCESS )
3765 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003766
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003767 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3768 ciphertext, ciphertext_length,
3769 plaintext_size, &tag );
3770 if( status != PSA_SUCCESS )
3771 goto exit;
3772
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003773#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003774 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003775 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003776 status = mbedtls_to_psa_error(
3777 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3778 ciphertext_length - operation.tag_length,
3779 nonce, nonce_length,
3780 additional_data,
3781 additional_data_length,
3782 tag, operation.tag_length,
3783 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003784 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003785 else
3786#endif /* MBEDTLS_GCM_C */
3787#if defined(MBEDTLS_CCM_C)
3788 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003789 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790 status = mbedtls_to_psa_error(
3791 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3792 ciphertext_length - operation.tag_length,
3793 nonce, nonce_length,
3794 additional_data,
3795 additional_data_length,
3796 ciphertext, plaintext,
3797 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003798 }
mohammad160339574652018-06-01 04:39:53 -07003799 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003800#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003801#if defined(MBEDTLS_CHACHAPOLY_C)
3802 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3803 {
3804 if( nonce_length != 12 || operation.tag_length != 16 )
3805 {
3806 status = PSA_ERROR_NOT_SUPPORTED;
3807 goto exit;
3808 }
3809 status = mbedtls_to_psa_error(
3810 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3811 ciphertext_length - operation.tag_length,
3812 nonce,
3813 additional_data,
3814 additional_data_length,
3815 tag,
3816 ciphertext,
3817 plaintext ) );
3818 }
3819 else
3820#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003821 {
mohammad1603554faad2018-06-03 15:07:38 +03003822 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003823 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003824
Gilles Peskineedf9a652018-08-17 18:11:56 +02003825 if( status != PSA_SUCCESS && plaintext_size != 0 )
3826 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003827
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003829 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003830 if( status == PSA_SUCCESS )
3831 *plaintext_length = ciphertext_length - operation.tag_length;
3832 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003833}
3834
Gilles Peskinea0655c32018-04-30 17:06:50 +02003835
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003836
Gilles Peskine20035e32018-02-03 22:44:14 +01003837/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003838/* Generators */
3839/****************************************************************/
3840
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003841#define HKDF_STATE_INIT 0 /* no input yet */
3842#define HKDF_STATE_STARTED 1 /* got salt */
3843#define HKDF_STATE_KEYED 2 /* got key */
3844#define HKDF_STATE_OUTPUT 3 /* output started */
3845
Gilles Peskinecbe66502019-05-16 16:59:18 +02003846static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003847 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003848{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003849 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3850 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003851 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003852 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003853}
3854
3855
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003856psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003857{
3858 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003859 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003860 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003861 {
3862 /* The object has (apparently) been initialized but it is not
3863 * in use. It's ok to call abort on such an object, and there's
3864 * nothing to do. */
3865 }
3866 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003867 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003868 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003869 if( operation->ctx.buffer.data != NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02003870 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003871 mbedtls_platform_zeroize( operation->ctx.buffer.data,
3872 operation->ctx.buffer.size );
3873 mbedtls_free( operation->ctx.buffer.data );
Gilles Peskine751d9652018-09-18 12:05:44 +02003874 }
3875 }
3876 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003877#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003878 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003879 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003880 mbedtls_free( operation->ctx.hkdf.info );
3881 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003882 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003883 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003884 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003885 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003886 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003887 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003888 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003889 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3890 operation->ctx.tls12_prf.key_len );
3891 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003892 }
Hanno Becker580fba12018-11-13 20:50:45 +00003893
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003894 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003895 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003896 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3897 operation->ctx.tls12_prf.Ai_with_seed_len );
3898 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003899 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003900 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003901 else
3902#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003903 {
3904 status = PSA_ERROR_BAD_STATE;
3905 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003906 memset( operation, 0, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003907 return( status );
3908}
3909
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003910psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003911 size_t *capacity)
3912{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003913 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003914 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003915 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003916 return PSA_ERROR_BAD_STATE;
3917 }
3918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003919 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003920 return( PSA_SUCCESS );
3921}
3922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003923psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003924 size_t capacity )
3925{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003926 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003927 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003929 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003930 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003931 return( PSA_SUCCESS );
3932}
3933
Gilles Peskinebef7f142018-07-12 17:22:21 +02003934#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003935/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003936 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003937static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003938 psa_algorithm_t hash_alg,
3939 uint8_t *output,
3940 size_t output_length )
3941{
3942 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3943 psa_status_t status;
3944
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003945 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3946 return( PSA_ERROR_BAD_STATE );
3947 hkdf->state = HKDF_STATE_OUTPUT;
3948
Gilles Peskinebef7f142018-07-12 17:22:21 +02003949 while( output_length != 0 )
3950 {
3951 /* Copy what remains of the current block */
3952 uint8_t n = hash_length - hkdf->offset_in_block;
3953 if( n > output_length )
3954 n = (uint8_t) output_length;
3955 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3956 output += n;
3957 output_length -= n;
3958 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003959 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003960 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003961 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003962 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003963 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003964 * object was corrupted or if this function is called directly
3965 * inside the library. */
3966 if( hkdf->block_number == 0xff )
3967 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003968
3969 /* We need a new block */
3970 ++hkdf->block_number;
3971 hkdf->offset_in_block = 0;
3972 status = psa_hmac_setup_internal( &hkdf->hmac,
3973 hkdf->prk, hash_length,
3974 hash_alg );
3975 if( status != PSA_SUCCESS )
3976 return( status );
3977 if( hkdf->block_number != 1 )
3978 {
3979 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3980 hkdf->output_block,
3981 hash_length );
3982 if( status != PSA_SUCCESS )
3983 return( status );
3984 }
3985 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3986 hkdf->info,
3987 hkdf->info_length );
3988 if( status != PSA_SUCCESS )
3989 return( status );
3990 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3991 &hkdf->block_number, 1 );
3992 if( status != PSA_SUCCESS )
3993 return( status );
3994 status = psa_hmac_finish_internal( &hkdf->hmac,
3995 hkdf->output_block,
3996 sizeof( hkdf->output_block ) );
3997 if( status != PSA_SUCCESS )
3998 return( status );
3999 }
4000
4001 return( PSA_SUCCESS );
4002}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004003
Gilles Peskinecbe66502019-05-16 16:59:18 +02004004static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4005 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004006 psa_algorithm_t alg )
4007{
4008 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4009 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4010 psa_hmac_internal_data hmac;
4011 psa_status_t status, cleanup_status;
4012
Hanno Becker3b339e22018-11-13 20:56:14 +00004013 unsigned char *Ai;
4014 size_t Ai_len;
4015
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004016 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004017 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004018 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004019 * object was corrupted or if this function is called directly
4020 * inside the library. */
4021 if( tls12_prf->block_number == 0xff )
4022 return( PSA_ERROR_BAD_STATE );
4023
4024 /* We need a new block */
4025 ++tls12_prf->block_number;
4026 tls12_prf->offset_in_block = 0;
4027
4028 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4029 *
4030 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4031 *
4032 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4033 * HMAC_hash(secret, A(2) + seed) +
4034 * HMAC_hash(secret, A(3) + seed) + ...
4035 *
4036 * A(0) = seed
4037 * A(i) = HMAC_hash( secret, A(i-1) )
4038 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004039 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004040 * `HMAC_hash(secret, A(i) + seed)` from which the output
4041 * is currently extracted as `output_block`, while
4042 * `A(i) + seed` is stored in `Ai_with_seed`.
4043 *
4044 * Generating a new block means recalculating `Ai_with_seed`
4045 * from the A(i)-part of it, and afterwards recalculating
4046 * `output_block`.
4047 *
4048 * A(0) is computed at setup time.
4049 *
4050 */
4051
4052 psa_hmac_init_internal( &hmac );
4053
4054 /* We must distinguish the calculation of A(1) from those
4055 * of A(2) and higher, because A(0)=seed has a different
4056 * length than the other A(i). */
4057 if( tls12_prf->block_number == 1 )
4058 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004059 Ai = tls12_prf->Ai_with_seed + hash_length;
4060 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004061 }
4062 else
4063 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004064 Ai = tls12_prf->Ai_with_seed;
4065 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004066 }
4067
Hanno Becker3b339e22018-11-13 20:56:14 +00004068 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4069 status = psa_hmac_setup_internal( &hmac,
4070 tls12_prf->key,
4071 tls12_prf->key_len,
4072 hash_alg );
4073 if( status != PSA_SUCCESS )
4074 goto cleanup;
4075
4076 status = psa_hash_update( &hmac.hash_ctx,
4077 Ai, Ai_len );
4078 if( status != PSA_SUCCESS )
4079 goto cleanup;
4080
4081 status = psa_hmac_finish_internal( &hmac,
4082 tls12_prf->Ai_with_seed,
4083 hash_length );
4084 if( status != PSA_SUCCESS )
4085 goto cleanup;
4086
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004087 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4088 status = psa_hmac_setup_internal( &hmac,
4089 tls12_prf->key,
4090 tls12_prf->key_len,
4091 hash_alg );
4092 if( status != PSA_SUCCESS )
4093 goto cleanup;
4094
4095 status = psa_hash_update( &hmac.hash_ctx,
4096 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004097 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004098 if( status != PSA_SUCCESS )
4099 goto cleanup;
4100
4101 status = psa_hmac_finish_internal( &hmac,
4102 tls12_prf->output_block,
4103 hash_length );
4104 if( status != PSA_SUCCESS )
4105 goto cleanup;
4106
4107cleanup:
4108
4109 cleanup_status = psa_hmac_abort_internal( &hmac );
4110 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4111 status = cleanup_status;
4112
4113 return( status );
4114}
4115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004117 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004118static psa_status_t psa_key_derivation_tls12_prf_read(
4119 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004120 psa_algorithm_t alg,
4121 uint8_t *output,
4122 size_t output_length )
4123{
4124 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4125 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4126 psa_status_t status;
4127
4128 while( output_length != 0 )
4129 {
4130 /* Copy what remains of the current block */
4131 uint8_t n = hash_length - tls12_prf->offset_in_block;
4132
4133 /* Check if we have fully processed the current block. */
4134 if( n == 0 )
4135 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004136 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004137 alg );
4138 if( status != PSA_SUCCESS )
4139 return( status );
4140
4141 continue;
4142 }
4143
4144 if( n > output_length )
4145 n = (uint8_t) output_length;
4146 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4147 n );
4148 output += n;
4149 output_length -= n;
4150 tls12_prf->offset_in_block += n;
4151 }
4152
4153 return( PSA_SUCCESS );
4154}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004155#endif /* MBEDTLS_MD_C */
4156
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004157psa_status_t psa_key_derivation_output_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004158 uint8_t *output,
4159 size_t output_length )
4160{
4161 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004162 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004165 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004166 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004167 return PSA_ERROR_BAD_STATE;
4168 }
4169
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004171 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004173 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004174 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004175 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004176 goto exit;
4177 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004179 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004180 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004181 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004182 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4183 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004184 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004185 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004186 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004187 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004188 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004189
Gilles Peskine969c5d62019-01-16 15:53:06 +01004190 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004191 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 /* Initially, the capacity of a selection operation is always
4193 * the size of the buffer, i.e. `operation->ctx.buffer.size`,
Gilles Peskine211a4362018-10-25 22:22:31 +02004194 * abbreviated in this comment as `size`. When the remaining
4195 * capacity is `c`, the next bytes to serve start `c` bytes
4196 * from the end of the buffer, i.e. `size - c` from the
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 * beginning of the buffer. Since `operation->capacity` was just
Gilles Peskine211a4362018-10-25 22:22:31 +02004198 * decremented above, we need to serve the bytes from
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 * `size - operation->capacity - output_length` to
4200 * `size - operation->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004201 size_t offset =
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004202 operation->ctx.buffer.size - operation->capacity - output_length;
4203 memcpy( output, operation->ctx.buffer.data + offset, output_length );
Gilles Peskine751d9652018-09-18 12:05:44 +02004204 status = PSA_SUCCESS;
4205 }
4206 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004207#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004208 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004209 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004210 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004211 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004212 output, output_length );
4213 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004214 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4215 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004216 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004217 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004218 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004219 output_length );
4220 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004221 else
4222#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004223 {
4224 return( PSA_ERROR_BAD_STATE );
4225 }
4226
4227exit:
4228 if( status != PSA_SUCCESS )
4229 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004230 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004231 * This allows us to differentiate between exhausted operations and
4232 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4233 * operations. */
4234 psa_algorithm_t alg = operation->alg;
4235 psa_key_derivation_abort( operation );
4236 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004237 memset( output, '!', output_length );
4238 }
4239 return( status );
4240}
4241
Gilles Peskine08542d82018-07-19 17:05:42 +02004242#if defined(MBEDTLS_DES_C)
4243static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4244{
4245 if( data_size >= 8 )
4246 mbedtls_des_key_set_parity( data );
4247 if( data_size >= 16 )
4248 mbedtls_des_key_set_parity( data + 8 );
4249 if( data_size >= 24 )
4250 mbedtls_des_key_set_parity( data + 16 );
4251}
4252#endif /* MBEDTLS_DES_C */
4253
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004254static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004255 psa_key_slot_t *slot,
4256 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004257 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004258{
4259 uint8_t *data = NULL;
4260 size_t bytes = PSA_BITS_TO_BYTES( bits );
4261 psa_status_t status;
4262
4263 if( ! key_type_is_raw_bytes( slot->type ) )
4264 return( PSA_ERROR_INVALID_ARGUMENT );
4265 if( bits % 8 != 0 )
4266 return( PSA_ERROR_INVALID_ARGUMENT );
4267 data = mbedtls_calloc( 1, bytes );
4268 if( data == NULL )
4269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004272 if( status != PSA_SUCCESS )
4273 goto exit;
4274#if defined(MBEDTLS_DES_C)
4275 if( slot->type == PSA_KEY_TYPE_DES )
4276 psa_des_set_key_parity( data, bytes );
4277#endif /* MBEDTLS_DES_C */
4278 status = psa_import_key_into_slot( slot, data, bytes );
4279
4280exit:
4281 mbedtls_free( data );
4282 return( status );
4283}
4284
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004285psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004286 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004287 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004288{
4289 psa_status_t status;
4290 psa_key_slot_t *slot = NULL;
4291 status = psa_start_key_creation( attributes, handle, &slot );
4292 if( status == PSA_SUCCESS )
4293 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004294 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004295 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004296 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004297 }
4298 if( status == PSA_SUCCESS )
4299 status = psa_finish_key_creation( slot );
4300 if( status != PSA_SUCCESS )
4301 {
4302 psa_fail_key_creation( slot );
4303 *handle = 0;
4304 }
4305 return( status );
4306}
4307
Gilles Peskineeab56e42018-07-12 17:12:33 +02004308
4309
4310/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004311/* Key derivation */
4312/****************************************************************/
4313
Gilles Peskinea05219c2018-11-16 16:02:56 +01004314#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004315/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004316 * of the HKDF algorithm.
4317 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004318 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004319 * to potentially free embedded data structures and wipe confidential data.
4320 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004321static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004322 const uint8_t *secret,
4323 size_t secret_length,
4324 psa_algorithm_t hash_alg,
4325 const uint8_t *salt,
4326 size_t salt_length,
4327 const uint8_t *label,
4328 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004329{
4330 psa_status_t status;
4331 status = psa_hmac_setup_internal( &hkdf->hmac,
4332 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004333 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004334 if( status != PSA_SUCCESS )
4335 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004336 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004337 if( status != PSA_SUCCESS )
4338 return( status );
4339 status = psa_hmac_finish_internal( &hkdf->hmac,
4340 hkdf->prk,
4341 sizeof( hkdf->prk ) );
4342 if( status != PSA_SUCCESS )
4343 return( status );
4344 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4345 hkdf->block_number = 0;
4346 hkdf->info_length = label_length;
4347 if( label_length != 0 )
4348 {
4349 hkdf->info = mbedtls_calloc( 1, label_length );
4350 if( hkdf->info == NULL )
4351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4352 memcpy( hkdf->info, label, label_length );
4353 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004354 hkdf->state = HKDF_STATE_KEYED;
4355 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004356 return( PSA_SUCCESS );
4357}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004358#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004359
Gilles Peskinea05219c2018-11-16 16:02:56 +01004360#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004362 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004363 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004364 * to potentially free embedded data structures and wipe confidential data.
4365 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004366static psa_status_t psa_key_derivation_tls12_prf_setup(
4367 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004368 const unsigned char *key,
4369 size_t key_len,
4370 psa_algorithm_t hash_alg,
4371 const uint8_t *salt,
4372 size_t salt_length,
4373 const uint8_t *label,
4374 size_t label_length )
4375{
4376 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004377 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4378 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004379
4380 tls12_prf->key = mbedtls_calloc( 1, key_len );
4381 if( tls12_prf->key == NULL )
4382 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4383 tls12_prf->key_len = key_len;
4384 memcpy( tls12_prf->key, key, key_len );
4385
Hanno Becker580fba12018-11-13 20:50:45 +00004386 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004387 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004388 if( overflow )
4389 return( PSA_ERROR_INVALID_ARGUMENT );
4390
4391 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4392 if( tls12_prf->Ai_with_seed == NULL )
4393 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4394 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4395
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004396 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4397 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004398 if( label_length != 0 )
4399 {
4400 memcpy( tls12_prf->Ai_with_seed + hash_length,
4401 label, label_length );
4402 }
4403
4404 if( salt_length != 0 )
4405 {
4406 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4407 salt, salt_length );
4408 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004409
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004410 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004411 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004412 tls12_prf->block_number = 0;
4413 tls12_prf->offset_in_block = hash_length;
4414
4415 return( PSA_SUCCESS );
4416}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004417
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004419static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4420 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004421 const unsigned char *psk,
4422 size_t psk_len,
4423 psa_algorithm_t hash_alg,
4424 const uint8_t *salt,
4425 size_t salt_length,
4426 const uint8_t *label,
4427 size_t label_length )
4428{
4429 psa_status_t status;
4430 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4431
4432 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4433 return( PSA_ERROR_INVALID_ARGUMENT );
4434
4435 /* Quoting RFC 4279, Section 2:
4436 *
4437 * The premaster secret is formed as follows: if the PSK is N octets
4438 * long, concatenate a uint16 with the value N, N zero octets, a second
4439 * uint16 with the value N, and the PSK itself.
4440 */
4441
4442 pms[0] = ( psk_len >> 8 ) & 0xff;
4443 pms[1] = ( psk_len >> 0 ) & 0xff;
4444 memset( pms + 2, 0, psk_len );
4445 pms[2 + psk_len + 0] = pms[0];
4446 pms[2 + psk_len + 1] = pms[1];
4447 memcpy( pms + 4 + psk_len, psk, psk_len );
4448
Gilles Peskinecbe66502019-05-16 16:59:18 +02004449 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004450 pms, 4 + 2 * psk_len,
4451 hash_alg,
4452 salt, salt_length,
4453 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004454
Gilles Peskine3f108122018-12-07 18:14:53 +01004455 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004456 return( status );
4457}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004458#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004459
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004460/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004461 * to potentially free embedded data structures and wipe confidential data.
4462 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004463static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004465 const uint8_t *secret, size_t secret_length,
4466 psa_algorithm_t alg,
4467 const uint8_t *salt, size_t salt_length,
4468 const uint8_t *label, size_t label_length,
4469 size_t capacity )
4470{
4471 psa_status_t status;
4472 size_t max_capacity;
4473
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474 /* Set operation->alg even on failure so that abort knows what to do. */
4475 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004476
Gilles Peskine751d9652018-09-18 12:05:44 +02004477 if( alg == PSA_ALG_SELECT_RAW )
4478 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004479 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004480 if( salt_length != 0 )
4481 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004482 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004483 if( label_length != 0 )
4484 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485 operation->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4486 if( operation->ctx.buffer.data == NULL )
Gilles Peskine751d9652018-09-18 12:05:44 +02004487 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 memcpy( operation->ctx.buffer.data, secret, secret_length );
4489 operation->ctx.buffer.size = secret_length;
Gilles Peskine751d9652018-09-18 12:05:44 +02004490 max_capacity = secret_length;
4491 status = PSA_SUCCESS;
4492 }
4493 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004494#if defined(MBEDTLS_MD_C)
4495 if( PSA_ALG_IS_HKDF( alg ) )
4496 {
4497 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4498 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4499 if( hash_size == 0 )
4500 return( PSA_ERROR_NOT_SUPPORTED );
4501 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004502 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004503 secret, secret_length,
4504 hash_alg,
4505 salt, salt_length,
4506 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004507 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004508 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4509 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4510 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004511 {
4512 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4513 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4514
4515 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4516 if( hash_alg != PSA_ALG_SHA_256 &&
4517 hash_alg != PSA_ALG_SHA_384 )
4518 {
4519 return( PSA_ERROR_NOT_SUPPORTED );
4520 }
4521
4522 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004523
4524 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4525 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004527 secret, secret_length,
4528 hash_alg, salt, salt_length,
4529 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004530 }
4531 else
4532 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004533 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004535 secret, secret_length,
4536 hash_alg, salt, salt_length,
4537 label, label_length );
4538 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004539 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004540 else
4541#endif
4542 {
4543 return( PSA_ERROR_NOT_SUPPORTED );
4544 }
4545
4546 if( status != PSA_SUCCESS )
4547 return( status );
4548
4549 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004551 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004552 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004553 else
4554 return( PSA_ERROR_INVALID_ARGUMENT );
4555
4556 return( PSA_SUCCESS );
4557}
4558
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004560 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004561 psa_algorithm_t alg,
4562 const uint8_t *salt,
4563 size_t salt_length,
4564 const uint8_t *label,
4565 size_t label_length,
4566 size_t capacity )
4567{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004568 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004569 psa_status_t status;
4570
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004571 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004572 return( PSA_ERROR_BAD_STATE );
4573
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004574 /* Make sure that alg is a key derivation algorithm. This prevents
4575 * key selection algorithms, which psa_key_derivation_internal
4576 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004577 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4578 return( PSA_ERROR_INVALID_ARGUMENT );
4579
Gilles Peskinec5487a82018-12-03 18:08:14 +01004580 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004581 if( status != PSA_SUCCESS )
4582 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004583
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004584 if( slot->type != PSA_KEY_TYPE_DERIVE )
4585 return( PSA_ERROR_INVALID_ARGUMENT );
4586
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004588 slot->data.raw.data,
4589 slot->data.raw.bytes,
4590 alg,
4591 salt, salt_length,
4592 label, label_length,
4593 capacity );
4594 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004596 return( status );
4597}
4598
Gilles Peskine969c5d62019-01-16 15:53:06 +01004599static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004600 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004601 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004602{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004603 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004604#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004605 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4606 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4607 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004608 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004609 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004610 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4611 if( hash_size == 0 )
4612 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004613 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4614 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004615 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004616 {
4617 return( PSA_ERROR_NOT_SUPPORTED );
4618 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004620 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004621 }
4622#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004623 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004624 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004625}
4626
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004628 psa_algorithm_t alg )
4629{
4630 psa_status_t status;
4631
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004633 return( PSA_ERROR_BAD_STATE );
4634
Gilles Peskine6843c292019-01-18 16:44:49 +01004635 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4636 return( PSA_ERROR_INVALID_ARGUMENT );
4637 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004638 {
4639 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004641 }
4642 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4643 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004645 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004646 else
4647 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004648
4649 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004651 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004652}
4653
4654#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004655static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004656 psa_algorithm_t hash_alg,
4657 psa_key_derivation_step_t step,
4658 const uint8_t *data,
4659 size_t data_length )
4660{
4661 psa_status_t status;
4662 switch( step )
4663 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004664 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004665 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004666 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004667 status = psa_hmac_setup_internal( &hkdf->hmac,
4668 data, data_length,
4669 hash_alg );
4670 if( status != PSA_SUCCESS )
4671 return( status );
4672 hkdf->state = HKDF_STATE_STARTED;
4673 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004674 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004675 /* If no salt was provided, use an empty salt. */
4676 if( hkdf->state == HKDF_STATE_INIT )
4677 {
4678 status = psa_hmac_setup_internal( &hkdf->hmac,
4679 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004680 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004681 if( status != PSA_SUCCESS )
4682 return( status );
4683 hkdf->state = HKDF_STATE_STARTED;
4684 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004685 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004686 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004687 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4688 data, data_length );
4689 if( status != PSA_SUCCESS )
4690 return( status );
4691 status = psa_hmac_finish_internal( &hkdf->hmac,
4692 hkdf->prk,
4693 sizeof( hkdf->prk ) );
4694 if( status != PSA_SUCCESS )
4695 return( status );
4696 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4697 hkdf->block_number = 0;
4698 hkdf->state = HKDF_STATE_KEYED;
4699 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004700 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004701 if( hkdf->state == HKDF_STATE_OUTPUT )
4702 return( PSA_ERROR_BAD_STATE );
4703 if( hkdf->info_set )
4704 return( PSA_ERROR_BAD_STATE );
4705 hkdf->info_length = data_length;
4706 if( data_length != 0 )
4707 {
4708 hkdf->info = mbedtls_calloc( 1, data_length );
4709 if( hkdf->info == NULL )
4710 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4711 memcpy( hkdf->info, data, data_length );
4712 }
4713 hkdf->info_set = 1;
4714 return( PSA_SUCCESS );
4715 default:
4716 return( PSA_ERROR_INVALID_ARGUMENT );
4717 }
4718}
4719#endif /* MBEDTLS_MD_C */
4720
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004721static psa_status_t psa_key_derivation_input_raw(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004723 psa_key_derivation_step_t step,
4724 const uint8_t *data,
4725 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004726{
4727 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004729
Gilles Peskine969c5d62019-01-16 15:53:06 +01004730 if( kdf_alg == PSA_ALG_SELECT_RAW )
4731 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 if( operation->capacity != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004733 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 operation->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4735 if( operation->ctx.buffer.data == NULL )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004736 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 memcpy( operation->ctx.buffer.data, data, data_length );
4738 operation->ctx.buffer.size = data_length;
4739 operation->capacity = data_length;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004740 status = PSA_SUCCESS;
4741 }
4742 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004743#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004744 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004745 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004747 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004748 step, data, data_length );
4749 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004750 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004751#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004752#if defined(MBEDTLS_MD_C)
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004753 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004754 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004755 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004756 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004757 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004758 status = PSA_ERROR_NOT_SUPPORTED;
4759 }
4760 else
4761#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004762 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004764 return( PSA_ERROR_BAD_STATE );
4765 }
4766
4767 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004769 return( status );
4770}
4771
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772psa_status_t psa_key_derivation_input_bytes( psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004773 psa_key_derivation_step_t step,
4774 const uint8_t *data,
4775 size_t data_length )
4776{
4777 switch( step )
4778 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004779 case PSA_KEY_DERIVATION_INPUT_LABEL:
4780 case PSA_KEY_DERIVATION_INPUT_SALT:
4781 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 return( psa_key_derivation_input_raw( operation, step,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004783 data, data_length ) );
4784 default:
4785 return( PSA_ERROR_INVALID_ARGUMENT );
4786 }
4787}
4788
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789psa_status_t psa_key_derivation_input_key( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004790 psa_key_derivation_step_t step,
4791 psa_key_handle_t handle )
4792{
4793 psa_key_slot_t *slot;
4794 psa_status_t status;
4795 status = psa_get_key_from_slot( handle, &slot,
4796 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004797 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004798 if( status != PSA_SUCCESS )
4799 return( status );
4800 if( slot->type != PSA_KEY_TYPE_DERIVE )
4801 return( PSA_ERROR_INVALID_ARGUMENT );
4802 /* Don't allow a key to be used as an input that is usually public.
4803 * This is debatable. It's ok from a cryptographic perspective to
4804 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004805 * the material should be dedicated to a particular input step,
4806 * otherwise this may allow the key to be used in an unintended way
4807 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02004808 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004809 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 return( psa_key_derivation_input_raw( operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004811 step,
4812 slot->data.raw.data,
4813 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004814}
4815
Gilles Peskineea0fb492018-07-12 17:17:20 +02004816
4817
4818/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004819/* Key agreement */
4820/****************************************************************/
4821
Gilles Peskinea05219c2018-11-16 16:02:56 +01004822#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004823static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4824 size_t peer_key_length,
4825 const mbedtls_ecp_keypair *our_key,
4826 uint8_t *shared_secret,
4827 size_t shared_secret_size,
4828 size_t *shared_secret_length )
4829{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004830 mbedtls_ecp_keypair *their_key = NULL;
4831 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004832 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004833 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004834
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004835 status = psa_import_ec_public_key(
4836 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4837 peer_key, peer_key_length,
4838 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004839 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004840 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004841
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004842 status = mbedtls_to_psa_error(
4843 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4844 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004845 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004846 status = mbedtls_to_psa_error(
4847 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4848 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004849 goto exit;
4850
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004851 status = mbedtls_to_psa_error(
4852 mbedtls_ecdh_calc_secret( &ecdh,
4853 shared_secret_length,
4854 shared_secret, shared_secret_size,
4855 mbedtls_ctr_drbg_random,
4856 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004857
4858exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004859 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004860 mbedtls_ecp_keypair_free( their_key );
4861 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004862 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004863}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004864#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004865
Gilles Peskine01d718c2018-09-18 12:01:02 +02004866#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4867
Gilles Peskine0216fe12019-04-11 21:23:21 +02004868static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4869 psa_key_slot_t *private_key,
4870 const uint8_t *peer_key,
4871 size_t peer_key_length,
4872 uint8_t *shared_secret,
4873 size_t shared_secret_size,
4874 size_t *shared_secret_length )
4875{
4876 switch( alg )
4877 {
4878#if defined(MBEDTLS_ECDH_C)
4879 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004880 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004881 return( PSA_ERROR_INVALID_ARGUMENT );
4882 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4883 private_key->data.ecp,
4884 shared_secret, shared_secret_size,
4885 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004886#endif /* MBEDTLS_ECDH_C */
4887 default:
4888 (void) private_key;
4889 (void) peer_key;
4890 (void) peer_key_length;
4891 (void) shared_secret;
4892 (void) shared_secret_size;
4893 (void) shared_secret_length;
4894 return( PSA_ERROR_NOT_SUPPORTED );
4895 }
4896}
4897
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004898/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004899 * to potentially free embedded data structures and wipe confidential data.
4900 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004903 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004904 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004906{
4907 psa_status_t status;
4908 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4909 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004911
4912 /* Step 1: run the secret agreement algorithm to generate the shared
4913 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004914 status = psa_key_agreement_raw_internal( ka_alg,
4915 private_key,
4916 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004917 shared_secret,
4918 sizeof( shared_secret ),
4919 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004920 if( status != PSA_SUCCESS )
4921 goto exit;
4922
4923 /* Step 2: set up the key derivation to generate key material from
4924 * the shared secret. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 status = psa_key_derivation_input_raw( operation, step,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 shared_secret, shared_secret_length );
4927
Gilles Peskine01d718c2018-09-18 12:01:02 +02004928exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004929 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 return( status );
4931}
4932
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004934 psa_key_derivation_step_t step,
4935 psa_key_handle_t private_key,
4936 const uint8_t *peer_key,
4937 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004938{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004939 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004940 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004942 return( PSA_ERROR_INVALID_ARGUMENT );
4943 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004944 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004945 if( status != PSA_SUCCESS )
4946 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004948 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004950 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01004952 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004953}
4954
Gilles Peskinebe697d82019-05-16 18:00:41 +02004955psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
4956 psa_key_handle_t private_key,
4957 const uint8_t *peer_key,
4958 size_t peer_key_length,
4959 uint8_t *output,
4960 size_t output_size,
4961 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004962{
4963 psa_key_slot_t *slot;
4964 psa_status_t status;
4965
4966 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4967 {
4968 status = PSA_ERROR_INVALID_ARGUMENT;
4969 goto exit;
4970 }
4971 status = psa_get_key_from_slot( private_key, &slot,
4972 PSA_KEY_USAGE_DERIVE, alg );
4973 if( status != PSA_SUCCESS )
4974 goto exit;
4975
4976 status = psa_key_agreement_raw_internal( alg, slot,
4977 peer_key, peer_key_length,
4978 output, output_size,
4979 output_length );
4980
4981exit:
4982 if( status != PSA_SUCCESS )
4983 {
4984 /* If an error happens and is not handled properly, the output
4985 * may be used as a key to protect sensitive data. Arrange for such
4986 * a key to be random, which is likely to result in decryption or
4987 * verification errors. This is better than filling the buffer with
4988 * some constant data such as zeros, which would result in the data
4989 * being protected with a reproducible, easily knowable key.
4990 */
4991 psa_generate_random( output, output_size );
4992 *output_length = output_size;
4993 }
4994 return( status );
4995}
Gilles Peskine01d718c2018-09-18 12:01:02 +02004996
4997
4998/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004999/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005000/****************************************************************/
5001
5002psa_status_t psa_generate_random( uint8_t *output,
5003 size_t output_size )
5004{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005005 int ret;
5006 GUARD_MODULE_INITIALIZED;
5007
5008 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005009 return( mbedtls_to_psa_error( ret ) );
5010}
5011
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005012#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5013#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005014
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005015psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5016 size_t seed_size )
5017{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005018 if( global_data.initialized )
5019 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005020
5021 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5022 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5023 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5024 return( PSA_ERROR_INVALID_ARGUMENT );
5025
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005026 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005027}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005028#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005029
Gilles Peskinee56e8782019-04-26 17:34:02 +02005030#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5031static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5032 size_t domain_parameters_size,
5033 int *exponent )
5034{
5035 size_t i;
5036 uint32_t acc = 0;
5037
5038 if( domain_parameters_size == 0 )
5039 {
5040 *exponent = 65537;
5041 return( PSA_SUCCESS );
5042 }
5043
5044 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5045 * support values that fit in a 32-bit integer, which is larger than
5046 * int on just about every platform anyway. */
5047 if( domain_parameters_size > sizeof( acc ) )
5048 return( PSA_ERROR_NOT_SUPPORTED );
5049 for( i = 0; i < domain_parameters_size; i++ )
5050 acc = ( acc << 8 ) | domain_parameters[i];
5051 if( acc > INT_MAX )
5052 return( PSA_ERROR_NOT_SUPPORTED );
5053 *exponent = acc;
5054 return( PSA_SUCCESS );
5055}
5056#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5057
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005058static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005059 psa_key_slot_t *slot, size_t bits,
5060 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005061{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005062 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005063
Gilles Peskinee56e8782019-04-26 17:34:02 +02005064 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005065 return( PSA_ERROR_INVALID_ARGUMENT );
5066
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005067 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005068 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005069 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005070 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005071 if( status != PSA_SUCCESS )
5072 return( status );
5073 status = psa_generate_random( slot->data.raw.data,
5074 slot->data.raw.bytes );
5075 if( status != PSA_SUCCESS )
5076 {
5077 mbedtls_free( slot->data.raw.data );
5078 return( status );
5079 }
5080#if defined(MBEDTLS_DES_C)
5081 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005082 psa_des_set_key_parity( slot->data.raw.data,
5083 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005084#endif /* MBEDTLS_DES_C */
5085 }
5086 else
5087
5088#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005089 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005090 {
5091 mbedtls_rsa_context *rsa;
5092 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005093 int exponent;
5094 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005095 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5096 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005097 /* Accept only byte-aligned keys, for the same reasons as
5098 * in psa_import_rsa_key(). */
5099 if( bits % 8 != 0 )
5100 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005101 status = psa_read_rsa_exponent( domain_parameters,
5102 domain_parameters_size,
5103 &exponent );
5104 if( status != PSA_SUCCESS )
5105 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005106 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5107 if( rsa == NULL )
5108 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5109 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5110 ret = mbedtls_rsa_gen_key( rsa,
5111 mbedtls_ctr_drbg_random,
5112 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005113 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005114 exponent );
5115 if( ret != 0 )
5116 {
5117 mbedtls_rsa_free( rsa );
5118 mbedtls_free( rsa );
5119 return( mbedtls_to_psa_error( ret ) );
5120 }
5121 slot->data.rsa = rsa;
5122 }
5123 else
5124#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5125
5126#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005127 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005128 {
5129 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5130 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5131 const mbedtls_ecp_curve_info *curve_info =
5132 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5133 mbedtls_ecp_keypair *ecp;
5134 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005135 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005136 return( PSA_ERROR_NOT_SUPPORTED );
5137 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5138 return( PSA_ERROR_NOT_SUPPORTED );
5139 if( curve_info->bit_size != bits )
5140 return( PSA_ERROR_INVALID_ARGUMENT );
5141 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5142 if( ecp == NULL )
5143 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5144 mbedtls_ecp_keypair_init( ecp );
5145 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5146 mbedtls_ctr_drbg_random,
5147 &global_data.ctr_drbg );
5148 if( ret != 0 )
5149 {
5150 mbedtls_ecp_keypair_free( ecp );
5151 mbedtls_free( ecp );
5152 return( mbedtls_to_psa_error( ret ) );
5153 }
5154 slot->data.ecp = ecp;
5155 }
5156 else
5157#endif /* MBEDTLS_ECP_C */
5158
5159 return( PSA_ERROR_NOT_SUPPORTED );
5160
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005161 return( PSA_SUCCESS );
5162}
5163
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005164psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005165 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005166{
5167 psa_status_t status;
5168 psa_key_slot_t *slot = NULL;
5169 status = psa_start_key_creation( attributes, handle, &slot );
5170 if( status == PSA_SUCCESS )
5171 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005172 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005173 slot, attributes->bits,
5174 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005175 }
5176 if( status == PSA_SUCCESS )
5177 status = psa_finish_key_creation( slot );
5178 if( status != PSA_SUCCESS )
5179 {
5180 psa_fail_key_creation( slot );
5181 *handle = 0;
5182 }
5183 return( status );
5184}
5185
5186
Gilles Peskine05d69892018-06-19 22:00:52 +02005187
5188/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005189/* Module setup */
5190/****************************************************************/
5191
Gilles Peskine5e769522018-11-20 21:59:56 +01005192psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5193 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5194 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5195{
5196 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5197 return( PSA_ERROR_BAD_STATE );
5198 global_data.entropy_init = entropy_init;
5199 global_data.entropy_free = entropy_free;
5200 return( PSA_SUCCESS );
5201}
5202
Gilles Peskinee59236f2018-01-27 23:32:46 +01005203void mbedtls_psa_crypto_free( void )
5204{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005205 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005206 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5207 {
5208 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005209 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005210 }
5211 /* Wipe all remaining data, including configuration.
5212 * In particular, this sets all state indicator to the value
5213 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005214 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskined0890212019-06-24 14:34:43 +02005215 /* Unregister all secure element drivers, so that we restart from
5216 * a pristine state. */
5217 psa_unregister_all_se_drivers( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005218}
5219
5220psa_status_t psa_crypto_init( void )
5221{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005222 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005223 const unsigned char drbg_seed[] = "PSA";
5224
Gilles Peskinec6b69072018-11-20 21:42:52 +01005225 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005226 if( global_data.initialized != 0 )
5227 return( PSA_SUCCESS );
5228
Gilles Peskine5e769522018-11-20 21:59:56 +01005229 /* Set default configuration if
5230 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5231 if( global_data.entropy_init == NULL )
5232 global_data.entropy_init = mbedtls_entropy_init;
5233 if( global_data.entropy_free == NULL )
5234 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005235
Gilles Peskinec6b69072018-11-20 21:42:52 +01005236 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005237 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005238 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005239 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005240 status = mbedtls_to_psa_error(
5241 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5242 mbedtls_entropy_func,
5243 &global_data.entropy,
5244 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5245 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005246 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005247 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005248
Gilles Peskine66fb1262018-12-10 16:29:04 +01005249 status = psa_initialize_key_slots( );
5250 if( status != PSA_SUCCESS )
5251 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005252
5253 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005254 global_data.initialized = 1;
5255
Gilles Peskinee59236f2018-01-27 23:32:46 +01005256exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005257 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005258 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005259 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005260}
5261
5262#endif /* MBEDTLS_PSA_CRYPTO_C */