blob: 5cb88de7e93e2d466a6e5c2fa13a4491e340037e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
597static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000772 {
773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000777 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 else
779#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200792
793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000800 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100801}
802
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100815 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
958 {
959 /* No key material to clean. */
960 }
961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100997static void psa_abort_operations_using_key( psa_key_slot_t *slot )
998{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200999 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001000 (void) slot;
1001}
1002
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003/** Completely wipe a slot in memory, including its policy.
1004 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001005psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006{
1007 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001008 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001009 /* At this point, key material and other type-specific content has
1010 * been wiped. Clear remaining metadata. We can call memset and not
1011 * zeroize because the metadata is not particularly sensitive. */
1012 memset( slot, 0, sizeof( *slot ) );
1013 return( status );
1014}
1015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001018 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001019 psa_status_t status = PSA_SUCCESS;
1020 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001021#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1022 psa_se_drv_table_entry_t *driver;
1023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024
Gilles Peskinec5487a82018-12-03 18:08:14 +01001025 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001026 if( status != PSA_SUCCESS )
1027 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
1029#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001031 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001033 /* For a key in a secure element, we need to do three things:
1034 * remove the key file in internal storage, destroy the
1035 * key inside the secure element, and update the driver's
1036 * persistent data. Start a transaction that will encompass these
1037 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001041 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 status = psa_crypto_save_transaction( );
1043 if( status != PSA_SUCCESS )
1044 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001045 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 /* TOnogrepDO: destroy what can be destroyed anyway */
1047 return( status );
1048 }
1049
Gilles Peskine354f7672019-07-12 23:46:38 +02001050 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001055 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001057 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001058 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001059 }
1060#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001061
Gilles Peskinefc762652019-07-22 19:30:34 +02001062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1063 if( driver != NULL )
1064 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001065 psa_status_t status2;
1066 status = psa_save_se_persistent_data( driver );
1067 status2 = psa_crypto_stop_transaction( );
1068 if( status == PSA_SUCCESS )
1069 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001070 if( status != PSA_SUCCESS )
1071 {
1072 /* TOnogrepDO: destroy what can be destroyed anyway */
1073 return( status );
1074 }
1075 }
1076#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1077
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001079 if( status != PSA_SUCCESS )
1080 return( status );
1081 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082}
1083
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001084void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1085{
Gilles Peskineb699f072019-04-26 16:06:02 +02001086 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087 memset( attributes, 0, sizeof( *attributes ) );
1088}
1089
Gilles Peskineb699f072019-04-26 16:06:02 +02001090psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1091 psa_key_type_t type,
1092 const uint8_t *data,
1093 size_t data_length )
1094{
1095 uint8_t *copy = NULL;
1096
1097 if( data_length != 0 )
1098 {
1099 copy = mbedtls_calloc( 1, data_length );
1100 if( copy == NULL )
1101 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1102 memcpy( copy, data, data_length );
1103 }
1104 /* After this point, this function is guaranteed to succeed, so it
1105 * can start modifying `*attributes`. */
1106
1107 if( attributes->domain_parameters != NULL )
1108 {
1109 mbedtls_free( attributes->domain_parameters );
1110 attributes->domain_parameters = NULL;
1111 attributes->domain_parameters_size = 0;
1112 }
1113
1114 attributes->domain_parameters = copy;
1115 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001116 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001117 return( PSA_SUCCESS );
1118}
1119
1120psa_status_t psa_get_key_domain_parameters(
1121 const psa_key_attributes_t *attributes,
1122 uint8_t *data, size_t data_size, size_t *data_length )
1123{
1124 if( attributes->domain_parameters_size > data_size )
1125 return( PSA_ERROR_BUFFER_TOO_SMALL );
1126 *data_length = attributes->domain_parameters_size;
1127 if( attributes->domain_parameters_size != 0 )
1128 memcpy( data, attributes->domain_parameters,
1129 attributes->domain_parameters_size );
1130 return( PSA_SUCCESS );
1131}
1132
1133#if defined(MBEDTLS_RSA_C)
1134static psa_status_t psa_get_rsa_public_exponent(
1135 const mbedtls_rsa_context *rsa,
1136 psa_key_attributes_t *attributes )
1137{
1138 mbedtls_mpi mpi;
1139 int ret;
1140 uint8_t *buffer = NULL;
1141 size_t buflen;
1142 mbedtls_mpi_init( &mpi );
1143
1144 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1145 if( ret != 0 )
1146 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001147 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1148 {
1149 /* It's the default value, which is reported as an empty string,
1150 * so there's nothing to do. */
1151 goto exit;
1152 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001153
1154 buflen = mbedtls_mpi_size( &mpi );
1155 buffer = mbedtls_calloc( 1, buflen );
1156 if( buffer == NULL )
1157 {
1158 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1159 goto exit;
1160 }
1161 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1162 if( ret != 0 )
1163 goto exit;
1164 attributes->domain_parameters = buffer;
1165 attributes->domain_parameters_size = buflen;
1166
1167exit:
1168 mbedtls_mpi_free( &mpi );
1169 if( ret != 0 )
1170 mbedtls_free( buffer );
1171 return( mbedtls_to_psa_error( ret ) );
1172}
1173#endif /* MBEDTLS_RSA_C */
1174
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001175/** Retrieve all the publicly-accessible attributes of a key.
1176 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1178 psa_key_attributes_t *attributes )
1179{
1180 psa_key_slot_t *slot;
1181 psa_status_t status;
1182
1183 psa_reset_key_attributes( attributes );
1184
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001185 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001186 if( status != PSA_SUCCESS )
1187 return( status );
1188
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001189 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001190 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1191 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1192
1193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1194 if( psa_key_slot_is_external( slot ) )
1195 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001197
Gilles Peskine8e338702019-07-30 20:06:31 +02001198 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001199 {
1200#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001201 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001202 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1204 /* TOnogrepDO: reporting the public exponent for opaque keys
1205 * is not yet implemented. */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001206 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001207 break;
1208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1210 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001211#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 default:
1213 /* Nothing else to do. */
1214 break;
1215 }
1216
1217 if( status != PSA_SUCCESS )
1218 psa_reset_key_attributes( attributes );
1219 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001220}
1221
Gilles Peskinec8000c02019-08-02 20:15:51 +02001222#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1223psa_status_t psa_get_key_slot_number(
1224 const psa_key_attributes_t *attributes,
1225 psa_key_slot_number_t *slot_number )
1226{
1227 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1228 {
1229 *slot_number = attributes->slot_number;
1230 return( PSA_SUCCESS );
1231 }
1232 else
1233 return( PSA_ERROR_INVALID_ARGUMENT );
1234}
1235#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1236
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001237#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001238static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1239 unsigned char *buf, size_t size )
1240{
1241 int ret;
1242 unsigned char *c;
1243 size_t len = 0;
1244
1245 c = buf + size;
1246
1247 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1248
1249 return( (int) len );
1250}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001251#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001252
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001253static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1254 uint8_t *data,
1255 size_t data_size,
1256 size_t *data_length,
1257 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258{
Gilles Peskine5d309672019-07-12 23:47:28 +02001259#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1260 const psa_drv_se_t *drv;
1261 psa_drv_se_context_t *drv_context;
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1263
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001264 *data_length = 0;
1265
Gilles Peskine8e338702019-07-30 20:06:31 +02001266 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001267 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001268
Gilles Peskine5d309672019-07-12 23:47:28 +02001269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001270 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001271 {
1272 psa_drv_se_export_key_t method;
1273 if( drv->key_management == NULL )
1274 return( PSA_ERROR_NOT_SUPPORTED );
1275 method = ( export_public_key ?
1276 drv->key_management->p_export_public :
1277 drv->key_management->p_export );
1278 if( method == NULL )
1279 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001280 return( method( drv_context,
1281 slot->data.se.slot_number,
1282 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001283 }
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Gilles Peskine8e338702019-07-30 20:06:31 +02001286 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001287 {
1288 if( slot->data.raw.bytes > data_size )
1289 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001290 if( data_size != 0 )
1291 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001292 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001293 memset( data + slot->data.raw.bytes, 0,
1294 data_size - slot->data.raw.bytes );
1295 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296 *data_length = slot->data.raw.bytes;
1297 return( PSA_SUCCESS );
1298 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001299#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001301 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001302 psa_status_t status;
1303
Gilles Peskineb46bef22019-07-30 21:32:04 +02001304 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001305 if( bytes > data_size )
1306 return( PSA_ERROR_BUFFER_TOO_SMALL );
1307 status = mbedtls_to_psa_error(
1308 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1309 if( status != PSA_SUCCESS )
1310 return( status );
1311 memset( data + bytes, 0, data_size - bytes );
1312 *data_length = bytes;
1313 return( PSA_SUCCESS );
1314 }
1315#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316 else
Moran Peker17e36e12018-05-02 12:55:20 +03001317 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001318#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001319 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1320 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001321 {
Moran Pekera998bc62018-04-16 18:16:20 +03001322 mbedtls_pk_context pk;
1323 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001324 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001325 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001326#if defined(MBEDTLS_RSA_C)
1327 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001328 pk.pk_info = &mbedtls_rsa_info;
1329 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001330#else
1331 return( PSA_ERROR_NOT_SUPPORTED );
1332#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001333 }
1334 else
1335 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001336#if defined(MBEDTLS_ECP_C)
1337 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001338 pk.pk_info = &mbedtls_eckey_info;
1339 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001340#else
1341 return( PSA_ERROR_NOT_SUPPORTED );
1342#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001343 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001345 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001346 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001347 }
Moran Peker17e36e12018-05-02 12:55:20 +03001348 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001349 {
Moran Peker17e36e12018-05-02 12:55:20 +03001350 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001351 }
Moran Peker60364322018-04-29 11:34:58 +03001352 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001353 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001354 /* If data_size is 0 then data may be NULL and then the
1355 * call to memset would have undefined behavior. */
1356 if( data_size != 0 )
1357 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001358 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001359 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001360 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1361 * Move the data to the beginning and erase remaining data
1362 * at the original location. */
1363 if( 2 * (size_t) ret <= data_size )
1364 {
1365 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001366 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001367 }
1368 else if( (size_t) ret < data_size )
1369 {
1370 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001371 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001372 }
Moran Pekera998bc62018-04-16 18:16:20 +03001373 *data_length = ret;
1374 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001375 }
1376 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001377#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001378 {
1379 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001380 it is valid for a special-purpose implementation to omit
1381 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001382 return( PSA_ERROR_NOT_SUPPORTED );
1383 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384 }
1385}
1386
Gilles Peskinec5487a82018-12-03 18:08:14 +01001387psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001388 uint8_t *data,
1389 size_t data_size,
1390 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001392 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001393 psa_status_t status;
1394
1395 /* Set the key to empty now, so that even when there are errors, we always
1396 * set data_length to a value between 0 and data_size. On error, setting
1397 * the key to empty is a good choice because an empty key representation is
1398 * unlikely to be accepted anywhere. */
1399 *data_length = 0;
1400
1401 /* Export requires the EXPORT flag. There is an exception for public keys,
1402 * which don't require any flag, but psa_get_key_from_slot takes
1403 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001404 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001405 if( status != PSA_SUCCESS )
1406 return( status );
1407 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001408 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001409}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410
Gilles Peskinec5487a82018-12-03 18:08:14 +01001411psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001412 uint8_t *data,
1413 size_t data_size,
1414 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001415{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001416 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001417 psa_status_t status;
1418
1419 /* Set the key to empty now, so that even when there are errors, we always
1420 * set data_length to a value between 0 and data_size. On error, setting
1421 * the key to empty is a good choice because an empty key representation is
1422 * unlikely to be accepted anywhere. */
1423 *data_length = 0;
1424
1425 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
1429 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001430 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001431}
1432
Gilles Peskine91e8c332019-08-02 19:19:39 +02001433#if defined(static_assert)
1434static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1435 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001436static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001437 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001438static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001439 "One or more key attribute flag is listed as both internal-only and external-only" );
1440#endif
1441
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001442/** Validate that a key policy is internally well-formed.
1443 *
1444 * This function only rejects invalid policies. It does not validate the
1445 * consistency of the policy with respect to other attributes of the key
1446 * such as the key type.
1447 */
1448static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001449{
1450 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001451 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 PSA_KEY_USAGE_ENCRYPT |
1453 PSA_KEY_USAGE_DECRYPT |
1454 PSA_KEY_USAGE_SIGN |
1455 PSA_KEY_USAGE_VERIFY |
1456 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1457 return( PSA_ERROR_INVALID_ARGUMENT );
1458
Gilles Peskine4747d192019-04-17 15:05:45 +02001459 return( PSA_SUCCESS );
1460}
1461
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001462/** Validate the internal consistency of key attributes.
1463 *
1464 * This function only rejects invalid attribute values. If does not
1465 * validate the consistency of the attributes with any key data that may
1466 * be involved in the creation of the key.
1467 *
1468 * Call this function early in the key creation process.
1469 *
1470 * \param[in] attributes Key attributes for the new key.
1471 * \param[out] p_drv On any return, the driver for the key, if any.
1472 * NULL for a transparent key.
1473 *
1474 */
1475static psa_status_t psa_validate_key_attributes(
1476 const psa_key_attributes_t *attributes,
1477 psa_se_drv_table_entry_t **p_drv )
1478{
1479 psa_status_t status;
1480
1481 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1482 {
1483 status = psa_validate_persistent_key_parameters(
1484 attributes->core.lifetime, attributes->core.id,
1485 p_drv, 1 );
1486 if( status != PSA_SUCCESS )
1487 return( status );
1488 }
1489
1490 status = psa_validate_key_policy( &attributes->core.policy );
1491 if( status != PSA_SUCCESS )
1492 return( status );
1493
1494 /* Refuse to create overly large keys.
1495 * Note that this doesn't trigger on import if the attributes don't
1496 * explicitly specify a size (so psa_get_key_bits returns 0), so
1497 * psa_import_key() needs its own checks. */
1498 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1499 return( PSA_ERROR_NOT_SUPPORTED );
1500
Gilles Peskine91e8c332019-08-02 19:19:39 +02001501 /* Reject invalid flags. These should not be reachable through the API. */
1502 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1503 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1504 return( PSA_ERROR_INVALID_ARGUMENT );
1505
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506 return( PSA_SUCCESS );
1507}
1508
Gilles Peskine4747d192019-04-17 15:05:45 +02001509/** Prepare a key slot to receive key material.
1510 *
1511 * This function allocates a key slot and sets its metadata.
1512 *
1513 * If this function fails, call psa_fail_key_creation().
1514 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001515 * This function is intended to be used as follows:
1516 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1517 * it with the specified attributes, and assign it a handle.
1518 * -# Populate the slot with the key material.
1519 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1520 * In case of failure at any step, stop the sequence and call
1521 * psa_fail_key_creation().
1522 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001523 * \param[in] attributes Key attributes for the new key.
1524 * \param[out] handle On success, a handle for the allocated slot.
1525 * \param[out] p_slot On success, a pointer to the prepared slot.
1526 * \param[out] p_drv On any return, the driver for the key, if any.
1527 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001528 *
1529 * \retval #PSA_SUCCESS
1530 * The key slot is ready to receive key material.
1531 * \return If this function fails, the key slot is an invalid state.
1532 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001533 */
1534static psa_status_t psa_start_key_creation(
1535 const psa_key_attributes_t *attributes,
1536 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001538 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001539{
1540 psa_status_t status;
1541 psa_key_slot_t *slot;
1542
Gilles Peskine011e4282019-06-26 18:34:38 +02001543 *p_drv = NULL;
1544
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001545 status = psa_validate_key_attributes( attributes, p_drv );
1546 if( status != PSA_SUCCESS )
1547 return( status );
1548
Gilles Peskineedbed562019-08-07 18:19:59 +02001549 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 if( status != PSA_SUCCESS )
1551 return( status );
1552 slot = *p_slot;
1553
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001554 /* We're storing the declared bit-size of the key. It's up to each
1555 * creation mechanism to verify that this information is correct.
1556 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001557 * an input (generate, device) but not for those where the bit-size
1558 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001559
1560 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001561
Gilles Peskine91e8c332019-08-02 19:19:39 +02001562 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001563 * external-only flags, query `attributes`. Thanks to the check
1564 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001565 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001566 * may have set. */
1567 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001568
Gilles Peskinecbaff462019-07-12 23:46:04 +02001569#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001570 /* For a key in a secure element, we need to do three things:
1571 * create the key file in internal storage, create the
1572 * key inside the secure element, and update the driver's
1573 * persistent data. Start a transaction that will encompass these
1574 * three actions. */
1575 /* The first thing to do is to find a slot number for the new key.
1576 * We save the slot number in persistent storage as part of the
1577 * transaction data. It will be needed to recover if the power
1578 * fails during the key creation process, to clean up on the secure
1579 * element side after restarting. Obtaining a slot number from the
1580 * secure element driver updates its persistent state, but we do not yet
1581 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001582 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001583 if( *p_drv != NULL )
1584 {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001585 /* Choosing a slot number is not supported yet. */
1586 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1587 return( PSA_ERROR_NOT_SUPPORTED );
1588
Gilles Peskinecbaff462019-07-12 23:46:04 +02001589 status = psa_find_se_slot_for_key( attributes, *p_drv,
1590 &slot->data.se.slot_number );
1591 if( status != PSA_SUCCESS )
1592 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001593 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001594 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001595 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001596 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001597 status = psa_crypto_save_transaction( );
1598 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001599 {
1600 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001601 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001602 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001603 }
1604#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1605
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 return( status );
1607}
1608
1609/** Finalize the creation of a key once its key material has been set.
1610 *
1611 * This entails writing the key to persistent storage.
1612 *
1613 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001614 * See the documentation of psa_start_key_creation() for the intended use
1615 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001616 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 * \param[in,out] slot Pointer to the slot with key material.
1618 * \param[in] driver The secure element driver for the key,
1619 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 *
1621 * \retval #PSA_SUCCESS
1622 * The key was successfully created. The handle is now valid.
1623 * \return If this function fails, the key slot is an invalid state.
1624 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001626static psa_status_t psa_finish_key_creation(
1627 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001628 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001629{
1630 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001631 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001632 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001633
1634#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001635 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001637#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1638 if( driver != NULL )
1639 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001640 psa_se_key_data_storage_t data;
1641#if defined(static_assert)
1642 static_assert( sizeof( slot->data.se.slot_number ) ==
1643 sizeof( data.slot_number ),
1644 "Slot number size does not match psa_se_key_data_storage_t" );
1645 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1646 "Bit-size size does not match psa_se_key_data_storage_t" );
1647#endif
1648 memcpy( &data.slot_number, &slot->data.se.slot_number,
1649 sizeof( slot->data.se.slot_number ) );
1650 memcpy( &data.bits, &slot->attr.bits,
1651 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001652 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001653 (uint8_t*) &data,
1654 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001655 }
1656 else
1657#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1658 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001659 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001660 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001661 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001662 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1663 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001664 if( buffer == NULL && buffer_size != 0 )
1665 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1666 status = psa_internal_export_key( slot,
1667 buffer, buffer_size, &length,
1668 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001669 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001670 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001671 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001672
Gilles Peskine1df83d42019-07-23 16:13:14 +02001673 if( buffer_size != 0 )
1674 mbedtls_platform_zeroize( buffer, buffer_size );
1675 mbedtls_free( buffer );
1676 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 }
1678#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1679
Gilles Peskinecbaff462019-07-12 23:46:04 +02001680#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1681 if( driver != NULL )
1682 {
1683 status = psa_save_se_persistent_data( driver );
1684 if( status != PSA_SUCCESS )
1685 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001686 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687 return( status );
1688 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001689 status = psa_crypto_stop_transaction( );
1690 if( status != PSA_SUCCESS )
1691 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001692 }
1693#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1694
Gilles Peskine4747d192019-04-17 15:05:45 +02001695 return( status );
1696}
1697
1698/** Abort the creation of a key.
1699 *
1700 * You may call this function after calling psa_start_key_creation(),
1701 * or after psa_finish_key_creation() fails. In other circumstances, this
1702 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001703 * See the documentation of psa_start_key_creation() for the intended use
1704 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001706 * \param[in,out] slot Pointer to the slot with key material.
1707 * \param[in] driver The secure element driver for the key,
1708 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001710static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001711 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001712{
Gilles Peskine011e4282019-06-26 18:34:38 +02001713 (void) driver;
1714
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 if( slot == NULL )
1716 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001717
1718#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1719 /* TOnogrepDO: If the key has already been created in the secure
1720 * element, and the failure happened later (when saving metadata
1721 * to internal storage), we need to destroy the key in the secure
1722 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001723
1724 /* Abort the ongoing transaction if any. We already did what it
1725 * takes to undo any partial creation. All that's left is to update
1726 * the transaction data itself. */
1727 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001728#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1729
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 psa_wipe_key_slot( slot );
1731}
1732
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001733/** Validate optional attributes during key creation.
1734 *
1735 * Some key attributes are optional during key creation. If they are
1736 * specified in the attributes structure, check that they are consistent
1737 * with the data in the slot.
1738 *
1739 * This function should be called near the end of key creation, after
1740 * the slot in memory is fully populated but before saving persistent data.
1741 */
1742static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001743 const psa_key_slot_t *slot,
1744 const psa_key_attributes_t *attributes )
1745{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001746 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001747 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001748 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001749 return( PSA_ERROR_INVALID_ARGUMENT );
1750 }
1751
1752 if( attributes->domain_parameters_size != 0 )
1753 {
1754#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001755 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001756 {
1757 mbedtls_mpi actual, required;
1758 int ret;
1759 mbedtls_mpi_init( &actual );
1760 mbedtls_mpi_init( &required );
1761 ret = mbedtls_rsa_export( slot->data.rsa,
1762 NULL, NULL, NULL, NULL, &actual );
1763 if( ret != 0 )
1764 goto rsa_exit;
1765 ret = mbedtls_mpi_read_binary( &required,
1766 attributes->domain_parameters,
1767 attributes->domain_parameters_size );
1768 if( ret != 0 )
1769 goto rsa_exit;
1770 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1771 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1772 rsa_exit:
1773 mbedtls_mpi_free( &actual );
1774 mbedtls_mpi_free( &required );
1775 if( ret != 0)
1776 return( mbedtls_to_psa_error( ret ) );
1777 }
1778 else
1779#endif
1780 {
1781 return( PSA_ERROR_INVALID_ARGUMENT );
1782 }
1783 }
1784
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001785 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001786 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001787 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001788 return( PSA_ERROR_INVALID_ARGUMENT );
1789 }
1790
1791 return( PSA_SUCCESS );
1792}
1793
Gilles Peskine4747d192019-04-17 15:05:45 +02001794psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001795 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001796 size_t data_length,
1797 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001798{
1799 psa_status_t status;
1800 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001801 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802
Gilles Peskine011e4282019-06-26 18:34:38 +02001803 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 if( status != PSA_SUCCESS )
1805 goto exit;
1806
Gilles Peskine5d309672019-07-12 23:47:28 +02001807#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1808 if( driver != NULL )
1809 {
1810 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001811 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001812 if( drv->key_management == NULL ||
1813 drv->key_management->p_import == NULL )
1814 {
1815 status = PSA_ERROR_NOT_SUPPORTED;
1816 goto exit;
1817 }
1818 status = drv->key_management->p_import(
1819 psa_get_se_driver_context( driver ),
1820 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001821 slot->attr.lifetime, slot->attr.type,
1822 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001823 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001824 &bits );
1825 if( status != PSA_SUCCESS )
1826 goto exit;
1827 if( bits > PSA_MAX_KEY_BITS )
1828 {
1829 status = PSA_ERROR_NOT_SUPPORTED;
1830 goto exit;
1831 }
1832 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001833 }
1834 else
1835#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1836 {
1837 status = psa_import_key_into_slot( slot, data, data_length );
1838 if( status != PSA_SUCCESS )
1839 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001840 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001841 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001842 if( status != PSA_SUCCESS )
1843 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001844
Gilles Peskine011e4282019-06-26 18:34:38 +02001845 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001847 if( status != PSA_SUCCESS )
1848 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001849 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001850 *handle = 0;
1851 }
1852 return( status );
1853}
1854
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001855static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001856 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001857{
1858 psa_status_t status;
1859 uint8_t *buffer = NULL;
1860 size_t buffer_size = 0;
1861 size_t length;
1862
Gilles Peskine8e338702019-07-30 20:06:31 +02001863 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001864 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001865 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001866 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001867 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001868 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1869 if( status != PSA_SUCCESS )
1870 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001871 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001872 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001873
1874exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001875 if( buffer_size != 0 )
1876 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001877 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001878 return( status );
1879}
1880
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001881psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1882 const psa_key_attributes_t *specified_attributes,
1883 psa_key_handle_t *target_handle )
1884{
1885 psa_status_t status;
1886 psa_key_slot_t *source_slot = NULL;
1887 psa_key_slot_t *target_slot = NULL;
1888 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001889 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001890
Gilles Peskine28f8f302019-07-24 13:30:31 +02001891 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001892 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001893 if( status != PSA_SUCCESS )
1894 goto exit;
1895
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001896 status = psa_validate_optional_attributes( source_slot,
1897 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 if( status != PSA_SUCCESS )
1899 goto exit;
1900
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001901 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001902 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001903 if( status != PSA_SUCCESS )
1904 goto exit;
1905
1906 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001907 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001908 if( status != PSA_SUCCESS )
1909 goto exit;
1910
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001911#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1912 if( driver != NULL )
1913 {
1914 /* Copying to a secure element is not implemented yet. */
1915 status = PSA_ERROR_NOT_SUPPORTED;
1916 goto exit;
1917 }
1918#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1919
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001920 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 if( status != PSA_SUCCESS )
1922 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001923
Gilles Peskine011e4282019-06-26 18:34:38 +02001924 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001925exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001926 if( status != PSA_SUCCESS )
1927 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001928 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001929 *target_handle = 0;
1930 }
1931 return( status );
1932}
1933
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001934
1935
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001936/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001937/* Message digests */
1938/****************************************************************/
1939
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001940static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001941{
1942 switch( alg )
1943 {
1944#if defined(MBEDTLS_MD2_C)
1945 case PSA_ALG_MD2:
1946 return( &mbedtls_md2_info );
1947#endif
1948#if defined(MBEDTLS_MD4_C)
1949 case PSA_ALG_MD4:
1950 return( &mbedtls_md4_info );
1951#endif
1952#if defined(MBEDTLS_MD5_C)
1953 case PSA_ALG_MD5:
1954 return( &mbedtls_md5_info );
1955#endif
1956#if defined(MBEDTLS_RIPEMD160_C)
1957 case PSA_ALG_RIPEMD160:
1958 return( &mbedtls_ripemd160_info );
1959#endif
1960#if defined(MBEDTLS_SHA1_C)
1961 case PSA_ALG_SHA_1:
1962 return( &mbedtls_sha1_info );
1963#endif
1964#if defined(MBEDTLS_SHA256_C)
1965 case PSA_ALG_SHA_224:
1966 return( &mbedtls_sha224_info );
1967 case PSA_ALG_SHA_256:
1968 return( &mbedtls_sha256_info );
1969#endif
1970#if defined(MBEDTLS_SHA512_C)
1971 case PSA_ALG_SHA_384:
1972 return( &mbedtls_sha384_info );
1973 case PSA_ALG_SHA_512:
1974 return( &mbedtls_sha512_info );
1975#endif
1976 default:
1977 return( NULL );
1978 }
1979}
1980
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001981psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1982{
1983 switch( operation->alg )
1984 {
Gilles Peskine81736312018-06-26 15:04:31 +02001985 case 0:
1986 /* The object has (apparently) been initialized but it is not
1987 * in use. It's ok to call abort on such an object, and there's
1988 * nothing to do. */
1989 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001990#if defined(MBEDTLS_MD2_C)
1991 case PSA_ALG_MD2:
1992 mbedtls_md2_free( &operation->ctx.md2 );
1993 break;
1994#endif
1995#if defined(MBEDTLS_MD4_C)
1996 case PSA_ALG_MD4:
1997 mbedtls_md4_free( &operation->ctx.md4 );
1998 break;
1999#endif
2000#if defined(MBEDTLS_MD5_C)
2001 case PSA_ALG_MD5:
2002 mbedtls_md5_free( &operation->ctx.md5 );
2003 break;
2004#endif
2005#if defined(MBEDTLS_RIPEMD160_C)
2006 case PSA_ALG_RIPEMD160:
2007 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2008 break;
2009#endif
2010#if defined(MBEDTLS_SHA1_C)
2011 case PSA_ALG_SHA_1:
2012 mbedtls_sha1_free( &operation->ctx.sha1 );
2013 break;
2014#endif
2015#if defined(MBEDTLS_SHA256_C)
2016 case PSA_ALG_SHA_224:
2017 case PSA_ALG_SHA_256:
2018 mbedtls_sha256_free( &operation->ctx.sha256 );
2019 break;
2020#endif
2021#if defined(MBEDTLS_SHA512_C)
2022 case PSA_ALG_SHA_384:
2023 case PSA_ALG_SHA_512:
2024 mbedtls_sha512_free( &operation->ctx.sha512 );
2025 break;
2026#endif
2027 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002028 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002029 }
2030 operation->alg = 0;
2031 return( PSA_SUCCESS );
2032}
2033
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002034psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002035 psa_algorithm_t alg )
2036{
2037 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002038
2039 /* A context must be freshly initialized before it can be set up. */
2040 if( operation->alg != 0 )
2041 {
2042 return( PSA_ERROR_BAD_STATE );
2043 }
2044
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002045 switch( alg )
2046 {
2047#if defined(MBEDTLS_MD2_C)
2048 case PSA_ALG_MD2:
2049 mbedtls_md2_init( &operation->ctx.md2 );
2050 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2051 break;
2052#endif
2053#if defined(MBEDTLS_MD4_C)
2054 case PSA_ALG_MD4:
2055 mbedtls_md4_init( &operation->ctx.md4 );
2056 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2057 break;
2058#endif
2059#if defined(MBEDTLS_MD5_C)
2060 case PSA_ALG_MD5:
2061 mbedtls_md5_init( &operation->ctx.md5 );
2062 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2063 break;
2064#endif
2065#if defined(MBEDTLS_RIPEMD160_C)
2066 case PSA_ALG_RIPEMD160:
2067 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2068 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2069 break;
2070#endif
2071#if defined(MBEDTLS_SHA1_C)
2072 case PSA_ALG_SHA_1:
2073 mbedtls_sha1_init( &operation->ctx.sha1 );
2074 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2075 break;
2076#endif
2077#if defined(MBEDTLS_SHA256_C)
2078 case PSA_ALG_SHA_224:
2079 mbedtls_sha256_init( &operation->ctx.sha256 );
2080 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2081 break;
2082 case PSA_ALG_SHA_256:
2083 mbedtls_sha256_init( &operation->ctx.sha256 );
2084 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_SHA512_C)
2088 case PSA_ALG_SHA_384:
2089 mbedtls_sha512_init( &operation->ctx.sha512 );
2090 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2091 break;
2092 case PSA_ALG_SHA_512:
2093 mbedtls_sha512_init( &operation->ctx.sha512 );
2094 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2095 break;
2096#endif
2097 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002098 return( PSA_ALG_IS_HASH( alg ) ?
2099 PSA_ERROR_NOT_SUPPORTED :
2100 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002101 }
2102 if( ret == 0 )
2103 operation->alg = alg;
2104 else
2105 psa_hash_abort( operation );
2106 return( mbedtls_to_psa_error( ret ) );
2107}
2108
2109psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2110 const uint8_t *input,
2111 size_t input_length )
2112{
2113 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002114
2115 /* Don't require hash implementations to behave correctly on a
2116 * zero-length input, which may have an invalid pointer. */
2117 if( input_length == 0 )
2118 return( PSA_SUCCESS );
2119
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 switch( operation->alg )
2121 {
2122#if defined(MBEDTLS_MD2_C)
2123 case PSA_ALG_MD2:
2124 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2125 input, input_length );
2126 break;
2127#endif
2128#if defined(MBEDTLS_MD4_C)
2129 case PSA_ALG_MD4:
2130 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2131 input, input_length );
2132 break;
2133#endif
2134#if defined(MBEDTLS_MD5_C)
2135 case PSA_ALG_MD5:
2136 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2137 input, input_length );
2138 break;
2139#endif
2140#if defined(MBEDTLS_RIPEMD160_C)
2141 case PSA_ALG_RIPEMD160:
2142 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2143 input, input_length );
2144 break;
2145#endif
2146#if defined(MBEDTLS_SHA1_C)
2147 case PSA_ALG_SHA_1:
2148 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2149 input, input_length );
2150 break;
2151#endif
2152#if defined(MBEDTLS_SHA256_C)
2153 case PSA_ALG_SHA_224:
2154 case PSA_ALG_SHA_256:
2155 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2156 input, input_length );
2157 break;
2158#endif
2159#if defined(MBEDTLS_SHA512_C)
2160 case PSA_ALG_SHA_384:
2161 case PSA_ALG_SHA_512:
2162 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2163 input, input_length );
2164 break;
2165#endif
2166 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002167 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002168 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002169
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002170 if( ret != 0 )
2171 psa_hash_abort( operation );
2172 return( mbedtls_to_psa_error( ret ) );
2173}
2174
2175psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2176 uint8_t *hash,
2177 size_t hash_size,
2178 size_t *hash_length )
2179{
itayzafrir40835d42018-08-02 13:14:17 +03002180 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002182 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002183
2184 /* Fill the output buffer with something that isn't a valid hash
2185 * (barring an attack on the hash and deliberately-crafted input),
2186 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002187 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002188 /* If hash_size is 0 then hash may be NULL and then the
2189 * call to memset would have undefined behavior. */
2190 if( hash_size != 0 )
2191 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192
2193 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002194 {
2195 status = PSA_ERROR_BUFFER_TOO_SMALL;
2196 goto exit;
2197 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198
2199 switch( operation->alg )
2200 {
2201#if defined(MBEDTLS_MD2_C)
2202 case PSA_ALG_MD2:
2203 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2204 break;
2205#endif
2206#if defined(MBEDTLS_MD4_C)
2207 case PSA_ALG_MD4:
2208 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2209 break;
2210#endif
2211#if defined(MBEDTLS_MD5_C)
2212 case PSA_ALG_MD5:
2213 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2214 break;
2215#endif
2216#if defined(MBEDTLS_RIPEMD160_C)
2217 case PSA_ALG_RIPEMD160:
2218 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2219 break;
2220#endif
2221#if defined(MBEDTLS_SHA1_C)
2222 case PSA_ALG_SHA_1:
2223 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2224 break;
2225#endif
2226#if defined(MBEDTLS_SHA256_C)
2227 case PSA_ALG_SHA_224:
2228 case PSA_ALG_SHA_256:
2229 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2230 break;
2231#endif
2232#if defined(MBEDTLS_SHA512_C)
2233 case PSA_ALG_SHA_384:
2234 case PSA_ALG_SHA_512:
2235 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2236 break;
2237#endif
2238 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002239 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240 }
itayzafrir40835d42018-08-02 13:14:17 +03002241 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242
itayzafrir40835d42018-08-02 13:14:17 +03002243exit:
2244 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002246 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 return( psa_hash_abort( operation ) );
2248 }
2249 else
2250 {
2251 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002252 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253 }
2254}
2255
Gilles Peskine2d277862018-06-18 15:41:12 +02002256psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2257 const uint8_t *hash,
2258 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259{
2260 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2261 size_t actual_hash_length;
2262 psa_status_t status = psa_hash_finish( operation,
2263 actual_hash, sizeof( actual_hash ),
2264 &actual_hash_length );
2265 if( status != PSA_SUCCESS )
2266 return( status );
2267 if( actual_hash_length != hash_length )
2268 return( PSA_ERROR_INVALID_SIGNATURE );
2269 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2270 return( PSA_ERROR_INVALID_SIGNATURE );
2271 return( PSA_SUCCESS );
2272}
2273
Gilles Peskineeb35d782019-01-22 17:56:16 +01002274psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2275 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002276{
2277 if( target_operation->alg != 0 )
2278 return( PSA_ERROR_BAD_STATE );
2279
2280 switch( source_operation->alg )
2281 {
2282 case 0:
2283 return( PSA_ERROR_BAD_STATE );
2284#if defined(MBEDTLS_MD2_C)
2285 case PSA_ALG_MD2:
2286 mbedtls_md2_clone( &target_operation->ctx.md2,
2287 &source_operation->ctx.md2 );
2288 break;
2289#endif
2290#if defined(MBEDTLS_MD4_C)
2291 case PSA_ALG_MD4:
2292 mbedtls_md4_clone( &target_operation->ctx.md4,
2293 &source_operation->ctx.md4 );
2294 break;
2295#endif
2296#if defined(MBEDTLS_MD5_C)
2297 case PSA_ALG_MD5:
2298 mbedtls_md5_clone( &target_operation->ctx.md5,
2299 &source_operation->ctx.md5 );
2300 break;
2301#endif
2302#if defined(MBEDTLS_RIPEMD160_C)
2303 case PSA_ALG_RIPEMD160:
2304 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2305 &source_operation->ctx.ripemd160 );
2306 break;
2307#endif
2308#if defined(MBEDTLS_SHA1_C)
2309 case PSA_ALG_SHA_1:
2310 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2311 &source_operation->ctx.sha1 );
2312 break;
2313#endif
2314#if defined(MBEDTLS_SHA256_C)
2315 case PSA_ALG_SHA_224:
2316 case PSA_ALG_SHA_256:
2317 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2318 &source_operation->ctx.sha256 );
2319 break;
2320#endif
2321#if defined(MBEDTLS_SHA512_C)
2322 case PSA_ALG_SHA_384:
2323 case PSA_ALG_SHA_512:
2324 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2325 &source_operation->ctx.sha512 );
2326 break;
2327#endif
2328 default:
2329 return( PSA_ERROR_NOT_SUPPORTED );
2330 }
2331
2332 target_operation->alg = source_operation->alg;
2333 return( PSA_SUCCESS );
2334}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335
2336
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338/* MAC */
2339/****************************************************************/
2340
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002341static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002342 psa_algorithm_t alg,
2343 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002344 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002345 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002348 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002350 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002351 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002352
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2354 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002355 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002357 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002358 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359 mode = MBEDTLS_MODE_STREAM;
2360 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002361 case PSA_ALG_CTR:
2362 mode = MBEDTLS_MODE_CTR;
2363 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002364 case PSA_ALG_CFB:
2365 mode = MBEDTLS_MODE_CFB;
2366 break;
2367 case PSA_ALG_OFB:
2368 mode = MBEDTLS_MODE_OFB;
2369 break;
2370 case PSA_ALG_CBC_NO_PADDING:
2371 mode = MBEDTLS_MODE_CBC;
2372 break;
2373 case PSA_ALG_CBC_PKCS7:
2374 mode = MBEDTLS_MODE_CBC;
2375 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002376 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377 mode = MBEDTLS_MODE_CCM;
2378 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002379 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380 mode = MBEDTLS_MODE_GCM;
2381 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002382 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2383 mode = MBEDTLS_MODE_CHACHAPOLY;
2384 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385 default:
2386 return( NULL );
2387 }
2388 }
2389 else if( alg == PSA_ALG_CMAC )
2390 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391 else
2392 return( NULL );
2393
2394 switch( key_type )
2395 {
2396 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002397 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002398 break;
2399 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002400 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2401 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002403 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002405 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002406 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2407 * but two-key Triple-DES is functionally three-key Triple-DES
2408 * with K1=K3, so that's how we present it to mbedtls. */
2409 if( key_bits == 128 )
2410 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 break;
2412 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002413 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414 break;
2415 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002416 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002418 case PSA_KEY_TYPE_CHACHA20:
2419 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2420 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421 default:
2422 return( NULL );
2423 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002424 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002425 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426
Jaeden Amero23bbb752018-06-26 14:16:54 +01002427 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2428 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429}
2430
Gilles Peskinea05219c2018-11-16 16:02:56 +01002431#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002432static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002433{
Gilles Peskine2d277862018-06-18 15:41:12 +02002434 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002435 {
2436 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002437 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002438 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002439 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002440 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002441 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002442 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002443 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002444 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002445 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002446 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002447 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002448 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002449 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002450 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002451 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002452 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002453 return( 128 );
2454 default:
2455 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002456 }
2457}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002458#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002459
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002460/* Initialize the MAC operation structure. Once this function has been
2461 * called, psa_mac_abort can run and will do the right thing. */
2462static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2463 psa_algorithm_t alg )
2464{
2465 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2466
2467 operation->alg = alg;
2468 operation->key_set = 0;
2469 operation->iv_set = 0;
2470 operation->iv_required = 0;
2471 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002472 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473
2474#if defined(MBEDTLS_CMAC_C)
2475 if( alg == PSA_ALG_CMAC )
2476 {
2477 operation->iv_required = 0;
2478 mbedtls_cipher_init( &operation->ctx.cmac );
2479 status = PSA_SUCCESS;
2480 }
2481 else
2482#endif /* MBEDTLS_CMAC_C */
2483#if defined(MBEDTLS_MD_C)
2484 if( PSA_ALG_IS_HMAC( operation->alg ) )
2485 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002486 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2487 operation->ctx.hmac.hash_ctx.alg = 0;
2488 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002489 }
2490 else
2491#endif /* MBEDTLS_MD_C */
2492 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002493 if( ! PSA_ALG_IS_MAC( alg ) )
2494 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495 }
2496
2497 if( status != PSA_SUCCESS )
2498 memset( operation, 0, sizeof( *operation ) );
2499 return( status );
2500}
2501
Gilles Peskine01126fa2018-07-12 17:04:55 +02002502#if defined(MBEDTLS_MD_C)
2503static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2504{
Gilles Peskine3f108122018-12-07 18:14:53 +01002505 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002506 return( psa_hash_abort( &hmac->hash_ctx ) );
2507}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002508
Janos Follath999f6482019-06-11 12:04:10 +01002509#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002510static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2511{
2512 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2513 memset( hmac, 0, sizeof( *hmac ) );
2514}
Janos Follath999f6482019-06-11 12:04:10 +01002515#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002516#endif /* MBEDTLS_MD_C */
2517
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2519{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002520 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002522 /* The object has (apparently) been initialized but it is not
2523 * in use. It's ok to call abort on such an object, and there's
2524 * nothing to do. */
2525 return( PSA_SUCCESS );
2526 }
2527 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002529 if( operation->alg == PSA_ALG_CMAC )
2530 {
2531 mbedtls_cipher_free( &operation->ctx.cmac );
2532 }
2533 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002536 if( PSA_ALG_IS_HMAC( operation->alg ) )
2537 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002538 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002539 }
2540 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002542 {
2543 /* Sanity check (shouldn't happen: operation->alg should
2544 * always have been initialized to a valid value). */
2545 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 }
Moran Peker41deec42018-04-04 15:43:05 +03002547
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 operation->alg = 0;
2549 operation->key_set = 0;
2550 operation->iv_set = 0;
2551 operation->iv_required = 0;
2552 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002553 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002554
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002556
2557bad_state:
2558 /* If abort is called on an uninitialized object, we can't trust
2559 * anything. Wipe the object in case it contains confidential data.
2560 * This may result in a memory leak if a pointer gets overwritten,
2561 * but it's too late to do anything about this. */
2562 memset( operation, 0, sizeof( *operation ) );
2563 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564}
2565
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002566#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002567static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002568 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002569 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002570 const mbedtls_cipher_info_t *cipher_info )
2571{
2572 int ret;
2573
2574 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002575
2576 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2577 if( ret != 0 )
2578 return( ret );
2579
2580 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2581 slot->data.raw.data,
2582 key_bits );
2583 return( ret );
2584}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002585#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002586
Gilles Peskine248051a2018-06-20 16:09:38 +02002587#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002588static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2589 const uint8_t *key,
2590 size_t key_length,
2591 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002592{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002593 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002594 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002595 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002596 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002597 psa_status_t status;
2598
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002599 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2600 * overflow below. This should never trigger if the hash algorithm
2601 * is implemented correctly. */
2602 /* The size checks against the ipad and opad buffers cannot be written
2603 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2604 * because that triggers -Wlogical-op on GCC 7.3. */
2605 if( block_size > sizeof( ipad ) )
2606 return( PSA_ERROR_NOT_SUPPORTED );
2607 if( block_size > sizeof( hmac->opad ) )
2608 return( PSA_ERROR_NOT_SUPPORTED );
2609 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002610 return( PSA_ERROR_NOT_SUPPORTED );
2611
Gilles Peskined223b522018-06-11 18:12:58 +02002612 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002613 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002614 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002615 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002616 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002617 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002618 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002619 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002620 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002621 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002622 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002623 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002624 }
Gilles Peskine96889972018-07-12 17:07:03 +02002625 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2626 * but it is permitted. It is common when HMAC is used in HKDF, for
2627 * example. Don't call `memcpy` in the 0-length because `key` could be
2628 * an invalid pointer which would make the behavior undefined. */
2629 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002630 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002631
Gilles Peskined223b522018-06-11 18:12:58 +02002632 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2633 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002634 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002635 ipad[i] ^= 0x36;
2636 memset( ipad + key_length, 0x36, block_size - key_length );
2637
2638 /* Copy the key material from ipad to opad, flipping the requisite bits,
2639 * and filling the rest of opad with the requisite constant. */
2640 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002641 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2642 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002643
Gilles Peskine01126fa2018-07-12 17:04:55 +02002644 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002645 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002646 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002647
Gilles Peskine01126fa2018-07-12 17:04:55 +02002648 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002649
2650cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002651 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002652
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002653 return( status );
2654}
Gilles Peskine248051a2018-06-20 16:09:38 +02002655#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002656
Gilles Peskine89167cb2018-07-08 20:12:23 +02002657static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002658 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002659 psa_algorithm_t alg,
2660 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002663 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002665 psa_key_usage_t usage =
2666 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002667 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002668 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002670 /* A context must be freshly initialized before it can be set up. */
2671 if( operation->alg != 0 )
2672 {
2673 return( PSA_ERROR_BAD_STATE );
2674 }
2675
Gilles Peskined911eb72018-08-14 15:18:45 +02002676 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002677 if( status != PSA_SUCCESS )
2678 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002679 if( is_sign )
2680 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681
Gilles Peskine28f8f302019-07-24 13:30:31 +02002682 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002683 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002684 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002685 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002686
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002688 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002689 {
2690 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002691 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002692 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002693 int ret;
2694 if( cipher_info == NULL )
2695 {
2696 status = PSA_ERROR_NOT_SUPPORTED;
2697 goto exit;
2698 }
2699 operation->mac_size = cipher_info->block_size;
2700 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2701 status = mbedtls_to_psa_error( ret );
2702 }
2703 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002706 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002708 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 if( hash_alg == 0 )
2710 {
2711 status = PSA_ERROR_NOT_SUPPORTED;
2712 goto exit;
2713 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002714
2715 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2716 /* Sanity check. This shouldn't fail on a valid configuration. */
2717 if( operation->mac_size == 0 ||
2718 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2719 {
2720 status = PSA_ERROR_NOT_SUPPORTED;
2721 goto exit;
2722 }
2723
Gilles Peskine8e338702019-07-30 20:06:31 +02002724 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002725 {
2726 status = PSA_ERROR_INVALID_ARGUMENT;
2727 goto exit;
2728 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002729
Gilles Peskine01126fa2018-07-12 17:04:55 +02002730 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2731 slot->data.raw.data,
2732 slot->data.raw.bytes,
2733 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002734 }
2735 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002737 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002738 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002739 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002741
Gilles Peskined911eb72018-08-14 15:18:45 +02002742 if( truncated == 0 )
2743 {
2744 /* The "normal" case: untruncated algorithm. Nothing to do. */
2745 }
2746 else if( truncated < 4 )
2747 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002748 /* A very short MAC is too short for security since it can be
2749 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2750 * so we make this our minimum, even though 32 bits is still
2751 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002752 status = PSA_ERROR_NOT_SUPPORTED;
2753 }
2754 else if( truncated > operation->mac_size )
2755 {
2756 /* It's impossible to "truncate" to a larger length. */
2757 status = PSA_ERROR_INVALID_ARGUMENT;
2758 }
2759 else
2760 operation->mac_size = truncated;
2761
Gilles Peskinefbfac682018-07-08 20:51:54 +02002762exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002765 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002767 else
2768 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002769 operation->key_set = 1;
2770 }
2771 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772}
2773
Gilles Peskine89167cb2018-07-08 20:12:23 +02002774psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002775 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002776 psa_algorithm_t alg )
2777{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002778 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002779}
2780
2781psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002782 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002783 psa_algorithm_t alg )
2784{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002785 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002786}
2787
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2789 const uint8_t *input,
2790 size_t input_length )
2791{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002792 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002794 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002796 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 operation->has_input = 1;
2798
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002800 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002801 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002802 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2803 input, input_length );
2804 status = mbedtls_to_psa_error( ret );
2805 }
2806 else
2807#endif /* MBEDTLS_CMAC_C */
2808#if defined(MBEDTLS_MD_C)
2809 if( PSA_ALG_IS_HMAC( operation->alg ) )
2810 {
2811 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2812 input_length );
2813 }
2814 else
2815#endif /* MBEDTLS_MD_C */
2816 {
2817 /* This shouldn't happen if `operation` was initialized by
2818 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002819 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002820 }
2821
Gilles Peskinefbfac682018-07-08 20:51:54 +02002822 if( status != PSA_SUCCESS )
2823 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002824 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825}
2826
Gilles Peskine01126fa2018-07-12 17:04:55 +02002827#if defined(MBEDTLS_MD_C)
2828static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2829 uint8_t *mac,
2830 size_t mac_size )
2831{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002832 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002833 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2834 size_t hash_size = 0;
2835 size_t block_size = psa_get_hash_block_size( hash_alg );
2836 psa_status_t status;
2837
Gilles Peskine01126fa2018-07-12 17:04:55 +02002838 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2839 if( status != PSA_SUCCESS )
2840 return( status );
2841 /* From here on, tmp needs to be wiped. */
2842
2843 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2844 if( status != PSA_SUCCESS )
2845 goto exit;
2846
2847 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2848 if( status != PSA_SUCCESS )
2849 goto exit;
2850
2851 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2852 if( status != PSA_SUCCESS )
2853 goto exit;
2854
Gilles Peskined911eb72018-08-14 15:18:45 +02002855 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2856 if( status != PSA_SUCCESS )
2857 goto exit;
2858
2859 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002860
2861exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002862 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002863 return( status );
2864}
2865#endif /* MBEDTLS_MD_C */
2866
mohammad16036df908f2018-04-02 08:34:15 -07002867static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002868 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002869 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002871 if( ! operation->key_set )
2872 return( PSA_ERROR_BAD_STATE );
2873 if( operation->iv_required && ! operation->iv_set )
2874 return( PSA_ERROR_BAD_STATE );
2875
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 if( mac_size < operation->mac_size )
2877 return( PSA_ERROR_BUFFER_TOO_SMALL );
2878
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002880 if( operation->alg == PSA_ALG_CMAC )
2881 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002882 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2883 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2884 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002885 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002886 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002887 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889 else
2890#endif /* MBEDTLS_CMAC_C */
2891#if defined(MBEDTLS_MD_C)
2892 if( PSA_ALG_IS_HMAC( operation->alg ) )
2893 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002894 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002895 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002896 }
2897 else
2898#endif /* MBEDTLS_MD_C */
2899 {
2900 /* This shouldn't happen if `operation` was initialized by
2901 * a setup function. */
2902 return( PSA_ERROR_BAD_STATE );
2903 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904}
2905
Gilles Peskineacd4be32018-07-08 19:56:25 +02002906psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2907 uint8_t *mac,
2908 size_t mac_size,
2909 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002910{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002911 psa_status_t status;
2912
Jaeden Amero252ef282019-02-15 14:05:35 +00002913 if( operation->alg == 0 )
2914 {
2915 return( PSA_ERROR_BAD_STATE );
2916 }
2917
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002918 /* Fill the output buffer with something that isn't a valid mac
2919 * (barring an attack on the mac and deliberately-crafted input),
2920 * in case the caller doesn't check the return status properly. */
2921 *mac_length = mac_size;
2922 /* If mac_size is 0 then mac may be NULL and then the
2923 * call to memset would have undefined behavior. */
2924 if( mac_size != 0 )
2925 memset( mac, '!', mac_size );
2926
Gilles Peskine89167cb2018-07-08 20:12:23 +02002927 if( ! operation->is_sign )
2928 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002929 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002930 }
mohammad16036df908f2018-04-02 08:34:15 -07002931
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002932 status = psa_mac_finish_internal( operation, mac, mac_size );
2933
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002934 if( status == PSA_SUCCESS )
2935 {
2936 status = psa_mac_abort( operation );
2937 if( status == PSA_SUCCESS )
2938 *mac_length = operation->mac_size;
2939 else
2940 memset( mac, '!', mac_size );
2941 }
2942 else
2943 psa_mac_abort( operation );
2944 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002945}
2946
Gilles Peskineacd4be32018-07-08 19:56:25 +02002947psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2948 const uint8_t *mac,
2949 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950{
Gilles Peskine828ed142018-06-18 23:25:51 +02002951 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002952 psa_status_t status;
2953
Jaeden Amero252ef282019-02-15 14:05:35 +00002954 if( operation->alg == 0 )
2955 {
2956 return( PSA_ERROR_BAD_STATE );
2957 }
2958
Gilles Peskine89167cb2018-07-08 20:12:23 +02002959 if( operation->is_sign )
2960 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002961 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002962 }
2963 if( operation->mac_size != mac_length )
2964 {
2965 status = PSA_ERROR_INVALID_SIGNATURE;
2966 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002967 }
mohammad16036df908f2018-04-02 08:34:15 -07002968
2969 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002970 actual_mac, sizeof( actual_mac ) );
2971
2972 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2973 status = PSA_ERROR_INVALID_SIGNATURE;
2974
2975cleanup:
2976 if( status == PSA_SUCCESS )
2977 status = psa_mac_abort( operation );
2978 else
2979 psa_mac_abort( operation );
2980
Gilles Peskine3f108122018-12-07 18:14:53 +01002981 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002982
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002983 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984}
2985
2986
Gilles Peskine20035e32018-02-03 22:44:14 +01002987
Gilles Peskine20035e32018-02-03 22:44:14 +01002988/****************************************************************/
2989/* Asymmetric cryptography */
2990/****************************************************************/
2991
Gilles Peskine2b450e32018-06-27 15:42:46 +02002992#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002993/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002994 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002995static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2996 size_t hash_length,
2997 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002998{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002999 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003000 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003001 *md_alg = mbedtls_md_get_type( md_info );
3002
3003 /* The Mbed TLS RSA module uses an unsigned int for hash length
3004 * parameters. Validate that it fits so that we don't risk an
3005 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003006#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003007 if( hash_length > UINT_MAX )
3008 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003009#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003010
3011#if defined(MBEDTLS_PKCS1_V15)
3012 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3013 * must be correct. */
3014 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3015 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003016 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003017 if( md_info == NULL )
3018 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003019 if( mbedtls_md_get_size( md_info ) != hash_length )
3020 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003021 }
3022#endif /* MBEDTLS_PKCS1_V15 */
3023
3024#if defined(MBEDTLS_PKCS1_V21)
3025 /* PSS requires a hash internally. */
3026 if( PSA_ALG_IS_RSA_PSS( alg ) )
3027 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003028 if( md_info == NULL )
3029 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003030 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003031#endif /* MBEDTLS_PKCS1_V21 */
3032
Gilles Peskine61b91d42018-06-08 16:09:36 +02003033 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003034}
3035
Gilles Peskine2b450e32018-06-27 15:42:46 +02003036static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3037 psa_algorithm_t alg,
3038 const uint8_t *hash,
3039 size_t hash_length,
3040 uint8_t *signature,
3041 size_t signature_size,
3042 size_t *signature_length )
3043{
3044 psa_status_t status;
3045 int ret;
3046 mbedtls_md_type_t md_alg;
3047
3048 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3049 if( status != PSA_SUCCESS )
3050 return( status );
3051
Gilles Peskine630a18a2018-06-29 17:49:35 +02003052 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003053 return( PSA_ERROR_BUFFER_TOO_SMALL );
3054
3055#if defined(MBEDTLS_PKCS1_V15)
3056 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3057 {
3058 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3059 MBEDTLS_MD_NONE );
3060 ret = mbedtls_rsa_pkcs1_sign( rsa,
3061 mbedtls_ctr_drbg_random,
3062 &global_data.ctr_drbg,
3063 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003064 md_alg,
3065 (unsigned int) hash_length,
3066 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003067 signature );
3068 }
3069 else
3070#endif /* MBEDTLS_PKCS1_V15 */
3071#if defined(MBEDTLS_PKCS1_V21)
3072 if( PSA_ALG_IS_RSA_PSS( alg ) )
3073 {
3074 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3075 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3076 mbedtls_ctr_drbg_random,
3077 &global_data.ctr_drbg,
3078 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003079 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003080 (unsigned int) hash_length,
3081 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003082 signature );
3083 }
3084 else
3085#endif /* MBEDTLS_PKCS1_V21 */
3086 {
3087 return( PSA_ERROR_INVALID_ARGUMENT );
3088 }
3089
3090 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003091 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003092 return( mbedtls_to_psa_error( ret ) );
3093}
3094
3095static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3096 psa_algorithm_t alg,
3097 const uint8_t *hash,
3098 size_t hash_length,
3099 const uint8_t *signature,
3100 size_t signature_length )
3101{
3102 psa_status_t status;
3103 int ret;
3104 mbedtls_md_type_t md_alg;
3105
3106 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3107 if( status != PSA_SUCCESS )
3108 return( status );
3109
Gilles Peskine630a18a2018-06-29 17:49:35 +02003110 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003111 return( PSA_ERROR_BUFFER_TOO_SMALL );
3112
3113#if defined(MBEDTLS_PKCS1_V15)
3114 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3115 {
3116 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3117 MBEDTLS_MD_NONE );
3118 ret = mbedtls_rsa_pkcs1_verify( rsa,
3119 mbedtls_ctr_drbg_random,
3120 &global_data.ctr_drbg,
3121 MBEDTLS_RSA_PUBLIC,
3122 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003123 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003124 hash,
3125 signature );
3126 }
3127 else
3128#endif /* MBEDTLS_PKCS1_V15 */
3129#if defined(MBEDTLS_PKCS1_V21)
3130 if( PSA_ALG_IS_RSA_PSS( alg ) )
3131 {
3132 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3133 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3134 mbedtls_ctr_drbg_random,
3135 &global_data.ctr_drbg,
3136 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003137 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003138 (unsigned int) hash_length,
3139 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003140 signature );
3141 }
3142 else
3143#endif /* MBEDTLS_PKCS1_V21 */
3144 {
3145 return( PSA_ERROR_INVALID_ARGUMENT );
3146 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003147
3148 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3149 * the rest of the signature is invalid". This has little use in
3150 * practice and PSA doesn't report this distinction. */
3151 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3152 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003153 return( mbedtls_to_psa_error( ret ) );
3154}
3155#endif /* MBEDTLS_RSA_C */
3156
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003157#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003158/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3159 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3160 * (even though these functions don't modify it). */
3161static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3162 psa_algorithm_t alg,
3163 const uint8_t *hash,
3164 size_t hash_length,
3165 uint8_t *signature,
3166 size_t signature_size,
3167 size_t *signature_length )
3168{
3169 int ret;
3170 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003171 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003172 mbedtls_mpi_init( &r );
3173 mbedtls_mpi_init( &s );
3174
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003175 if( signature_size < 2 * curve_bytes )
3176 {
3177 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3178 goto cleanup;
3179 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003180
Gilles Peskinea05219c2018-11-16 16:02:56 +01003181#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003182 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3183 {
3184 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3185 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3186 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3187 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3188 hash, hash_length,
3189 md_alg ) );
3190 }
3191 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003192#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003193 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003194 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003195 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3196 hash, hash_length,
3197 mbedtls_ctr_drbg_random,
3198 &global_data.ctr_drbg ) );
3199 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003200
3201 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3202 signature,
3203 curve_bytes ) );
3204 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3205 signature + curve_bytes,
3206 curve_bytes ) );
3207
3208cleanup:
3209 mbedtls_mpi_free( &r );
3210 mbedtls_mpi_free( &s );
3211 if( ret == 0 )
3212 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003213 return( mbedtls_to_psa_error( ret ) );
3214}
3215
3216static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3217 const uint8_t *hash,
3218 size_t hash_length,
3219 const uint8_t *signature,
3220 size_t signature_length )
3221{
3222 int ret;
3223 mbedtls_mpi r, s;
3224 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3225 mbedtls_mpi_init( &r );
3226 mbedtls_mpi_init( &s );
3227
3228 if( signature_length != 2 * curve_bytes )
3229 return( PSA_ERROR_INVALID_SIGNATURE );
3230
3231 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3232 signature,
3233 curve_bytes ) );
3234 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3235 signature + curve_bytes,
3236 curve_bytes ) );
3237
3238 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3239 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003240
3241cleanup:
3242 mbedtls_mpi_free( &r );
3243 mbedtls_mpi_free( &s );
3244 return( mbedtls_to_psa_error( ret ) );
3245}
3246#endif /* MBEDTLS_ECDSA_C */
3247
Gilles Peskinec5487a82018-12-03 18:08:14 +01003248psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003249 psa_algorithm_t alg,
3250 const uint8_t *hash,
3251 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003252 uint8_t *signature,
3253 size_t signature_size,
3254 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003255{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003256 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003257 psa_status_t status;
3258
3259 *signature_length = signature_size;
3260
Gilles Peskine28f8f302019-07-24 13:30:31 +02003261 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003262 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003263 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003264 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003265 {
3266 status = PSA_ERROR_INVALID_ARGUMENT;
3267 goto exit;
3268 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003269
Gilles Peskine20035e32018-02-03 22:44:14 +01003270#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003271 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003272 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003273 status = psa_rsa_sign( slot->data.rsa,
3274 alg,
3275 hash, hash_length,
3276 signature, signature_size,
3277 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003278 }
3279 else
3280#endif /* defined(MBEDTLS_RSA_C) */
3281#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003282 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003283 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003284#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003285 if(
3286#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3287 PSA_ALG_IS_ECDSA( alg )
3288#else
3289 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3290#endif
3291 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003292 status = psa_ecdsa_sign( slot->data.ecp,
3293 alg,
3294 hash, hash_length,
3295 signature, signature_size,
3296 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003297 else
3298#endif /* defined(MBEDTLS_ECDSA_C) */
3299 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003300 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003301 }
itayzafrir5c753392018-05-08 11:18:38 +03003302 }
3303 else
3304#endif /* defined(MBEDTLS_ECP_C) */
3305 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003306 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003307 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003308
3309exit:
3310 /* Fill the unused part of the output buffer (the whole buffer on error,
3311 * the trailing part on success) with something that isn't a valid mac
3312 * (barring an attack on the mac and deliberately-crafted input),
3313 * in case the caller doesn't check the return status properly. */
3314 if( status == PSA_SUCCESS )
3315 memset( signature + *signature_length, '!',
3316 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003317 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003318 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003319 /* If signature_size is 0 then we have nothing to do. We must not call
3320 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003321 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003322}
3323
Gilles Peskinec5487a82018-12-03 18:08:14 +01003324psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003325 psa_algorithm_t alg,
3326 const uint8_t *hash,
3327 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003328 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003329 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003330{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003331 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003332 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003333
Gilles Peskine28f8f302019-07-24 13:30:31 +02003334 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003335 if( status != PSA_SUCCESS )
3336 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003337
Gilles Peskine61b91d42018-06-08 16:09:36 +02003338#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003339 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003340 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003341 return( psa_rsa_verify( slot->data.rsa,
3342 alg,
3343 hash, hash_length,
3344 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345 }
3346 else
3347#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003348#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003349 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003350 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003351#if defined(MBEDTLS_ECDSA_C)
3352 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003353 return( psa_ecdsa_verify( slot->data.ecp,
3354 hash, hash_length,
3355 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003356 else
3357#endif /* defined(MBEDTLS_ECDSA_C) */
3358 {
3359 return( PSA_ERROR_INVALID_ARGUMENT );
3360 }
itayzafrir5c753392018-05-08 11:18:38 +03003361 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003362 else
3363#endif /* defined(MBEDTLS_ECP_C) */
3364 {
3365 return( PSA_ERROR_NOT_SUPPORTED );
3366 }
3367}
3368
Gilles Peskine072ac562018-06-30 00:21:29 +02003369#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3370static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3371 mbedtls_rsa_context *rsa )
3372{
3373 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3374 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3375 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3376 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3377}
3378#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3379
Gilles Peskinec5487a82018-12-03 18:08:14 +01003380psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003381 psa_algorithm_t alg,
3382 const uint8_t *input,
3383 size_t input_length,
3384 const uint8_t *salt,
3385 size_t salt_length,
3386 uint8_t *output,
3387 size_t output_size,
3388 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003389{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003390 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003391 psa_status_t status;
3392
Darryl Green5cc689a2018-07-24 15:34:10 +01003393 (void) input;
3394 (void) input_length;
3395 (void) salt;
3396 (void) output;
3397 (void) output_size;
3398
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003399 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003400
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003401 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3402 return( PSA_ERROR_INVALID_ARGUMENT );
3403
Gilles Peskine28f8f302019-07-24 13:30:31 +02003404 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003405 if( status != PSA_SUCCESS )
3406 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003407 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3408 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003409 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003410
3411#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003412 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003413 {
3414 mbedtls_rsa_context *rsa = slot->data.rsa;
3415 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003416 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003417 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003418#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003419 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003420 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003421 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3422 mbedtls_ctr_drbg_random,
3423 &global_data.ctr_drbg,
3424 MBEDTLS_RSA_PUBLIC,
3425 input_length,
3426 input,
3427 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003428 }
3429 else
3430#endif /* MBEDTLS_PKCS1_V15 */
3431#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003432 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003433 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003434 psa_rsa_oaep_set_padding_mode( alg, rsa );
3435 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3436 mbedtls_ctr_drbg_random,
3437 &global_data.ctr_drbg,
3438 MBEDTLS_RSA_PUBLIC,
3439 salt, salt_length,
3440 input_length,
3441 input,
3442 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003443 }
3444 else
3445#endif /* MBEDTLS_PKCS1_V21 */
3446 {
3447 return( PSA_ERROR_INVALID_ARGUMENT );
3448 }
3449 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003450 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451 return( mbedtls_to_psa_error( ret ) );
3452 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003454#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003455 {
3456 return( PSA_ERROR_NOT_SUPPORTED );
3457 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003458}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459
Gilles Peskinec5487a82018-12-03 18:08:14 +01003460psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003461 psa_algorithm_t alg,
3462 const uint8_t *input,
3463 size_t input_length,
3464 const uint8_t *salt,
3465 size_t salt_length,
3466 uint8_t *output,
3467 size_t output_size,
3468 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003469{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003470 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003471 psa_status_t status;
3472
Darryl Green5cc689a2018-07-24 15:34:10 +01003473 (void) input;
3474 (void) input_length;
3475 (void) salt;
3476 (void) output;
3477 (void) output_size;
3478
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003479 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003480
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003481 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3482 return( PSA_ERROR_INVALID_ARGUMENT );
3483
Gilles Peskine28f8f302019-07-24 13:30:31 +02003484 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003485 if( status != PSA_SUCCESS )
3486 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003487 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488 return( PSA_ERROR_INVALID_ARGUMENT );
3489
3490#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003491 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003492 {
3493 mbedtls_rsa_context *rsa = slot->data.rsa;
3494 int ret;
3495
Gilles Peskine630a18a2018-06-29 17:49:35 +02003496 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003497 return( PSA_ERROR_INVALID_ARGUMENT );
3498
3499#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003500 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003501 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003502 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3503 mbedtls_ctr_drbg_random,
3504 &global_data.ctr_drbg,
3505 MBEDTLS_RSA_PRIVATE,
3506 output_length,
3507 input,
3508 output,
3509 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003510 }
3511 else
3512#endif /* MBEDTLS_PKCS1_V15 */
3513#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003514 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003515 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003516 psa_rsa_oaep_set_padding_mode( alg, rsa );
3517 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3518 mbedtls_ctr_drbg_random,
3519 &global_data.ctr_drbg,
3520 MBEDTLS_RSA_PRIVATE,
3521 salt, salt_length,
3522 output_length,
3523 input,
3524 output,
3525 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526 }
3527 else
3528#endif /* MBEDTLS_PKCS1_V21 */
3529 {
3530 return( PSA_ERROR_INVALID_ARGUMENT );
3531 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003532
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003533 return( mbedtls_to_psa_error( ret ) );
3534 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003536#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003537 {
3538 return( PSA_ERROR_NOT_SUPPORTED );
3539 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003540}
Gilles Peskine20035e32018-02-03 22:44:14 +01003541
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003542
3543
mohammad1603503973b2018-03-12 15:59:30 +02003544/****************************************************************/
3545/* Symmetric cryptography */
3546/****************************************************************/
3547
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003548/* Initialize the cipher operation structure. Once this function has been
3549 * called, psa_cipher_abort can run and will do the right thing. */
3550static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3551 psa_algorithm_t alg )
3552{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003553 if( ! PSA_ALG_IS_CIPHER( alg ) )
3554 {
3555 memset( operation, 0, sizeof( *operation ) );
3556 return( PSA_ERROR_INVALID_ARGUMENT );
3557 }
3558
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003559 operation->alg = alg;
3560 operation->key_set = 0;
3561 operation->iv_set = 0;
3562 operation->iv_required = 1;
3563 operation->iv_size = 0;
3564 operation->block_size = 0;
3565 mbedtls_cipher_init( &operation->ctx.cipher );
3566 return( PSA_SUCCESS );
3567}
3568
Gilles Peskinee553c652018-06-04 16:22:46 +02003569static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003570 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003571 psa_algorithm_t alg,
3572 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003573{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003574 int ret = 0;
3575 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003576 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003577 size_t key_bits;
3578 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3580 PSA_KEY_USAGE_ENCRYPT :
3581 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003582
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003583 /* A context must be freshly initialized before it can be set up. */
3584 if( operation->alg != 0 )
3585 {
3586 return( PSA_ERROR_BAD_STATE );
3587 }
3588
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003589 status = psa_cipher_init( operation, alg );
3590 if( status != PSA_SUCCESS )
3591 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003592
Gilles Peskine28f8f302019-07-24 13:30:31 +02003593 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003594 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003595 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003596 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003597
Gilles Peskine8e338702019-07-30 20:06:31 +02003598 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003599 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003600 {
3601 status = PSA_ERROR_NOT_SUPPORTED;
3602 goto exit;
3603 }
mohammad1603503973b2018-03-12 15:59:30 +02003604
mohammad1603503973b2018-03-12 15:59:30 +02003605 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003606 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003607 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003608
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003609#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003610 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003611 {
3612 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003613 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003614 memcpy( keys, slot->data.raw.data, 16 );
3615 memcpy( keys + 16, slot->data.raw.data, 8 );
3616 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3617 keys,
3618 192, cipher_operation );
3619 }
3620 else
3621#endif
3622 {
3623 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3624 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003625 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003626 }
Moran Peker41deec42018-04-04 15:43:05 +03003627 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003628 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003629
mohammad16038481e742018-03-18 13:57:31 +02003630#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003631 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003632 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003633 case PSA_ALG_CBC_NO_PADDING:
3634 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3635 MBEDTLS_PADDING_NONE );
3636 break;
3637 case PSA_ALG_CBC_PKCS7:
3638 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3639 MBEDTLS_PADDING_PKCS7 );
3640 break;
3641 default:
3642 /* The algorithm doesn't involve padding. */
3643 ret = 0;
3644 break;
3645 }
3646 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003647 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003648#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3649
mohammad1603503973b2018-03-12 15:59:30 +02003650 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003651 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003652 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003653 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003654 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003655 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003656 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003657#if defined(MBEDTLS_CHACHA20_C)
3658 else
3659 if( alg == PSA_ALG_CHACHA20 )
3660 operation->iv_size = 12;
3661#endif
mohammad1603503973b2018-03-12 15:59:30 +02003662
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003663exit:
3664 if( status == 0 )
3665 status = mbedtls_to_psa_error( ret );
3666 if( status != 0 )
3667 psa_cipher_abort( operation );
3668 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003669}
3670
Gilles Peskinefe119512018-07-08 21:39:34 +02003671psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003672 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003673 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003674{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003675 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003676}
3677
Gilles Peskinefe119512018-07-08 21:39:34 +02003678psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003679 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003680 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003681{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003682 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003683}
3684
Gilles Peskinefe119512018-07-08 21:39:34 +02003685psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003686 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003687 size_t iv_size,
3688 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003689{
itayzafrir534bd7c2018-08-02 13:56:32 +03003690 psa_status_t status;
3691 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003692 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003693 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003694 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003695 }
Moran Peker41deec42018-04-04 15:43:05 +03003696 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003697 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003698 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003699 goto exit;
3700 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003701 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3702 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003703 if( ret != 0 )
3704 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003705 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003706 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003707 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003708
mohammad16038481e742018-03-18 13:57:31 +02003709 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003710 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003711
Moran Peker395db872018-05-31 14:07:14 +03003712exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003713 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003714 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003715 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003716}
3717
Gilles Peskinefe119512018-07-08 21:39:34 +02003718psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003719 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003720 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003721{
itayzafrir534bd7c2018-08-02 13:56:32 +03003722 psa_status_t status;
3723 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003724 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003725 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003726 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003727 }
Moran Pekera28258c2018-05-29 16:25:04 +03003728 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003729 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003730 status = PSA_ERROR_INVALID_ARGUMENT;
3731 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003732 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003733 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3734 status = mbedtls_to_psa_error( ret );
3735exit:
3736 if( status == PSA_SUCCESS )
3737 operation->iv_set = 1;
3738 else
mohammad1603503973b2018-03-12 15:59:30 +02003739 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003740 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003741}
3742
Gilles Peskinee553c652018-06-04 16:22:46 +02003743psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3744 const uint8_t *input,
3745 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003746 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003747 size_t output_size,
3748 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003749{
itayzafrir534bd7c2018-08-02 13:56:32 +03003750 psa_status_t status;
3751 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003752 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003753
3754 if( operation->alg == 0 )
3755 {
3756 return( PSA_ERROR_BAD_STATE );
3757 }
3758
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003759 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003760 {
3761 /* Take the unprocessed partial block left over from previous
3762 * update calls, if any, plus the input to this call. Remove
3763 * the last partial block, if any. You get the data that will be
3764 * output in this call. */
3765 expected_output_size =
3766 ( operation->ctx.cipher.unprocessed_len + input_length )
3767 / operation->block_size * operation->block_size;
3768 }
3769 else
3770 {
3771 expected_output_size = input_length;
3772 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003773
Gilles Peskine89d789c2018-06-04 17:17:16 +02003774 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003775 {
3776 status = PSA_ERROR_BUFFER_TOO_SMALL;
3777 goto exit;
3778 }
mohammad160382759612018-03-12 18:16:40 +02003779
mohammad1603503973b2018-03-12 15:59:30 +02003780 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003781 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003782 status = mbedtls_to_psa_error( ret );
3783exit:
3784 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003785 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003786 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003787}
3788
Gilles Peskinee553c652018-06-04 16:22:46 +02003789psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3790 uint8_t *output,
3791 size_t output_size,
3792 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003793{
David Saadab4ecc272019-02-14 13:48:10 +02003794 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003795 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003796 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003797
mohammad1603503973b2018-03-12 15:59:30 +02003798 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003799 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003800 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003801 }
3802 if( operation->iv_required && ! operation->iv_set )
3803 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003804 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003805 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003806
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003807 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003808 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3809 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003810 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003811 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003812 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003813 }
3814
Janos Follath315b51c2018-07-09 16:04:51 +01003815 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3816 temp_output_buffer,
3817 output_length );
3818 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003819 {
Janos Follath315b51c2018-07-09 16:04:51 +01003820 status = mbedtls_to_psa_error( cipher_ret );
3821 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003822 }
Janos Follath315b51c2018-07-09 16:04:51 +01003823
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003824 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003825 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003826 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003827 memcpy( output, temp_output_buffer, *output_length );
3828 else
3829 {
Janos Follath315b51c2018-07-09 16:04:51 +01003830 status = PSA_ERROR_BUFFER_TOO_SMALL;
3831 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003832 }
mohammad1603503973b2018-03-12 15:59:30 +02003833
Gilles Peskine3f108122018-12-07 18:14:53 +01003834 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003835 status = psa_cipher_abort( operation );
3836
3837 return( status );
3838
3839error:
3840
3841 *output_length = 0;
3842
Gilles Peskine3f108122018-12-07 18:14:53 +01003843 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003844 (void) psa_cipher_abort( operation );
3845
3846 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003847}
3848
Gilles Peskinee553c652018-06-04 16:22:46 +02003849psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3850{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003851 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003852 {
3853 /* The object has (apparently) been initialized but it is not
3854 * in use. It's ok to call abort on such an object, and there's
3855 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003856 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003857 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003858
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003859 /* Sanity check (shouldn't happen: operation->alg should
3860 * always have been initialized to a valid value). */
3861 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3862 return( PSA_ERROR_BAD_STATE );
3863
mohammad1603503973b2018-03-12 15:59:30 +02003864 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003865
Moran Peker41deec42018-04-04 15:43:05 +03003866 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003867 operation->key_set = 0;
3868 operation->iv_set = 0;
3869 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003870 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003871 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003872
Moran Peker395db872018-05-31 14:07:14 +03003873 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003874}
3875
Gilles Peskinea0655c32018-04-30 17:06:50 +02003876
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003877
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003878
mohammad16035955c982018-04-26 00:53:03 +03003879/****************************************************************/
3880/* AEAD */
3881/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003882
Gilles Peskineedf9a652018-08-17 18:11:56 +02003883typedef struct
3884{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003885 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003886 const mbedtls_cipher_info_t *cipher_info;
3887 union
3888 {
3889#if defined(MBEDTLS_CCM_C)
3890 mbedtls_ccm_context ccm;
3891#endif /* MBEDTLS_CCM_C */
3892#if defined(MBEDTLS_GCM_C)
3893 mbedtls_gcm_context gcm;
3894#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003895#if defined(MBEDTLS_CHACHAPOLY_C)
3896 mbedtls_chachapoly_context chachapoly;
3897#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003898 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003899 psa_algorithm_t core_alg;
3900 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003901 uint8_t tag_length;
3902} aead_operation_t;
3903
Gilles Peskine30a9e412019-01-14 18:36:12 +01003904static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003905{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003906 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003907 {
3908#if defined(MBEDTLS_CCM_C)
3909 case PSA_ALG_CCM:
3910 mbedtls_ccm_free( &operation->ctx.ccm );
3911 break;
3912#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003913#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003914 case PSA_ALG_GCM:
3915 mbedtls_gcm_free( &operation->ctx.gcm );
3916 break;
3917#endif /* MBEDTLS_GCM_C */
3918 }
3919}
3920
3921static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003922 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003923 psa_key_usage_t usage,
3924 psa_algorithm_t alg )
3925{
3926 psa_status_t status;
3927 size_t key_bits;
3928 mbedtls_cipher_id_t cipher_id;
3929
Gilles Peskine28f8f302019-07-24 13:30:31 +02003930 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003931 if( status != PSA_SUCCESS )
3932 return( status );
3933
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003934 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003935
3936 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003937 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003938 &cipher_id );
3939 if( operation->cipher_info == NULL )
3940 return( PSA_ERROR_NOT_SUPPORTED );
3941
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003942 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003943 {
3944#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003945 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3946 operation->core_alg = PSA_ALG_CCM;
3947 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003948 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3949 * The call to mbedtls_ccm_encrypt_and_tag or
3950 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003951 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003952 return( PSA_ERROR_INVALID_ARGUMENT );
3953 mbedtls_ccm_init( &operation->ctx.ccm );
3954 status = mbedtls_to_psa_error(
3955 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3956 operation->slot->data.raw.data,
3957 (unsigned int) key_bits ) );
3958 if( status != 0 )
3959 goto cleanup;
3960 break;
3961#endif /* MBEDTLS_CCM_C */
3962
3963#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003964 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3965 operation->core_alg = PSA_ALG_GCM;
3966 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003967 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3968 * The call to mbedtls_gcm_crypt_and_tag or
3969 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003970 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003971 return( PSA_ERROR_INVALID_ARGUMENT );
3972 mbedtls_gcm_init( &operation->ctx.gcm );
3973 status = mbedtls_to_psa_error(
3974 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3975 operation->slot->data.raw.data,
3976 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003977 if( status != 0 )
3978 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003979 break;
3980#endif /* MBEDTLS_GCM_C */
3981
Gilles Peskine26869f22019-05-06 15:25:00 +02003982#if defined(MBEDTLS_CHACHAPOLY_C)
3983 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3984 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3985 operation->full_tag_length = 16;
3986 /* We only support the default tag length. */
3987 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3988 return( PSA_ERROR_NOT_SUPPORTED );
3989 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3990 status = mbedtls_to_psa_error(
3991 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3992 operation->slot->data.raw.data ) );
3993 if( status != 0 )
3994 goto cleanup;
3995 break;
3996#endif /* MBEDTLS_CHACHAPOLY_C */
3997
Gilles Peskineedf9a652018-08-17 18:11:56 +02003998 default:
3999 return( PSA_ERROR_NOT_SUPPORTED );
4000 }
4001
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004002 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4003 {
4004 status = PSA_ERROR_INVALID_ARGUMENT;
4005 goto cleanup;
4006 }
4007 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004008
Gilles Peskineedf9a652018-08-17 18:11:56 +02004009 return( PSA_SUCCESS );
4010
4011cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004012 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004013 return( status );
4014}
4015
Gilles Peskinec5487a82018-12-03 18:08:14 +01004016psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004017 psa_algorithm_t alg,
4018 const uint8_t *nonce,
4019 size_t nonce_length,
4020 const uint8_t *additional_data,
4021 size_t additional_data_length,
4022 const uint8_t *plaintext,
4023 size_t plaintext_length,
4024 uint8_t *ciphertext,
4025 size_t ciphertext_size,
4026 size_t *ciphertext_length )
4027{
mohammad16035955c982018-04-26 00:53:03 +03004028 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004029 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004030 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004031
mohammad1603f08a5502018-06-03 15:05:47 +03004032 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004033
Gilles Peskinec5487a82018-12-03 18:08:14 +01004034 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004035 if( status != PSA_SUCCESS )
4036 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004037
Gilles Peskineedf9a652018-08-17 18:11:56 +02004038 /* For all currently supported modes, the tag is at the end of the
4039 * ciphertext. */
4040 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4041 {
4042 status = PSA_ERROR_BUFFER_TOO_SMALL;
4043 goto exit;
4044 }
4045 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004046
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004047#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004048 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004049 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004050 status = mbedtls_to_psa_error(
4051 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4052 MBEDTLS_GCM_ENCRYPT,
4053 plaintext_length,
4054 nonce, nonce_length,
4055 additional_data, additional_data_length,
4056 plaintext, ciphertext,
4057 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004058 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004059 else
4060#endif /* MBEDTLS_GCM_C */
4061#if defined(MBEDTLS_CCM_C)
4062 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004063 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004064 status = mbedtls_to_psa_error(
4065 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4066 plaintext_length,
4067 nonce, nonce_length,
4068 additional_data,
4069 additional_data_length,
4070 plaintext, ciphertext,
4071 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004072 }
mohammad16035c8845f2018-05-09 05:40:09 -07004073 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004074#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004075#if defined(MBEDTLS_CHACHAPOLY_C)
4076 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4077 {
4078 if( nonce_length != 12 || operation.tag_length != 16 )
4079 {
4080 status = PSA_ERROR_NOT_SUPPORTED;
4081 goto exit;
4082 }
4083 status = mbedtls_to_psa_error(
4084 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4085 plaintext_length,
4086 nonce,
4087 additional_data,
4088 additional_data_length,
4089 plaintext,
4090 ciphertext,
4091 tag ) );
4092 }
4093 else
4094#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004095 {
mohammad1603554faad2018-06-03 15:07:38 +03004096 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004097 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004098
Gilles Peskineedf9a652018-08-17 18:11:56 +02004099 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4100 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004101
Gilles Peskineedf9a652018-08-17 18:11:56 +02004102exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004103 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004104 if( status == PSA_SUCCESS )
4105 *ciphertext_length = plaintext_length + operation.tag_length;
4106 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004107}
4108
Gilles Peskineee652a32018-06-01 19:23:52 +02004109/* Locate the tag in a ciphertext buffer containing the encrypted data
4110 * followed by the tag. Return the length of the part preceding the tag in
4111 * *plaintext_length. This is the size of the plaintext in modes where
4112 * the encrypted data has the same size as the plaintext, such as
4113 * CCM and GCM. */
4114static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4115 const uint8_t *ciphertext,
4116 size_t ciphertext_length,
4117 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004118 const uint8_t **p_tag )
4119{
4120 size_t payload_length;
4121 if( tag_length > ciphertext_length )
4122 return( PSA_ERROR_INVALID_ARGUMENT );
4123 payload_length = ciphertext_length - tag_length;
4124 if( payload_length > plaintext_size )
4125 return( PSA_ERROR_BUFFER_TOO_SMALL );
4126 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004127 return( PSA_SUCCESS );
4128}
4129
Gilles Peskinec5487a82018-12-03 18:08:14 +01004130psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004131 psa_algorithm_t alg,
4132 const uint8_t *nonce,
4133 size_t nonce_length,
4134 const uint8_t *additional_data,
4135 size_t additional_data_length,
4136 const uint8_t *ciphertext,
4137 size_t ciphertext_length,
4138 uint8_t *plaintext,
4139 size_t plaintext_size,
4140 size_t *plaintext_length )
4141{
mohammad16035955c982018-04-26 00:53:03 +03004142 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143 aead_operation_t operation;
4144 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004145
Gilles Peskineee652a32018-06-01 19:23:52 +02004146 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004147
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004149 if( status != PSA_SUCCESS )
4150 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004151
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004152 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4153 ciphertext, ciphertext_length,
4154 plaintext_size, &tag );
4155 if( status != PSA_SUCCESS )
4156 goto exit;
4157
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004158#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004159 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004160 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 status = mbedtls_to_psa_error(
4162 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4163 ciphertext_length - operation.tag_length,
4164 nonce, nonce_length,
4165 additional_data,
4166 additional_data_length,
4167 tag, operation.tag_length,
4168 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004169 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004170 else
4171#endif /* MBEDTLS_GCM_C */
4172#if defined(MBEDTLS_CCM_C)
4173 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004174 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004175 status = mbedtls_to_psa_error(
4176 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4177 ciphertext_length - operation.tag_length,
4178 nonce, nonce_length,
4179 additional_data,
4180 additional_data_length,
4181 ciphertext, plaintext,
4182 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004183 }
mohammad160339574652018-06-01 04:39:53 -07004184 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004185#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004186#if defined(MBEDTLS_CHACHAPOLY_C)
4187 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4188 {
4189 if( nonce_length != 12 || operation.tag_length != 16 )
4190 {
4191 status = PSA_ERROR_NOT_SUPPORTED;
4192 goto exit;
4193 }
4194 status = mbedtls_to_psa_error(
4195 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4196 ciphertext_length - operation.tag_length,
4197 nonce,
4198 additional_data,
4199 additional_data_length,
4200 tag,
4201 ciphertext,
4202 plaintext ) );
4203 }
4204 else
4205#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004206 {
mohammad1603554faad2018-06-03 15:07:38 +03004207 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004208 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004209
Gilles Peskineedf9a652018-08-17 18:11:56 +02004210 if( status != PSA_SUCCESS && plaintext_size != 0 )
4211 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004212
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004214 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004215 if( status == PSA_SUCCESS )
4216 *plaintext_length = ciphertext_length - operation.tag_length;
4217 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004218}
4219
Gilles Peskinea0655c32018-04-30 17:06:50 +02004220
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004221
Gilles Peskine20035e32018-02-03 22:44:14 +01004222/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004223/* Generators */
4224/****************************************************************/
4225
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004226#define HKDF_STATE_INIT 0 /* no input yet */
4227#define HKDF_STATE_STARTED 1 /* got salt */
4228#define HKDF_STATE_KEYED 2 /* got key */
4229#define HKDF_STATE_OUTPUT 3 /* output started */
4230
Gilles Peskinecbe66502019-05-16 16:59:18 +02004231static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004232 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004233{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004234 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4235 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004236 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004237 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004238}
4239
4240
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004241psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004242{
4243 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004244 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004245 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004246 {
4247 /* The object has (apparently) been initialized but it is not
4248 * in use. It's ok to call abort on such an object, and there's
4249 * nothing to do. */
4250 }
4251 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004252#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004253 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004254 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004255 mbedtls_free( operation->ctx.hkdf.info );
4256 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004257 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004258 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004259 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004260 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004261 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004262#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004264 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004265 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4266 operation->ctx.tls12_prf.key_len );
4267 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004268 }
Hanno Becker580fba12018-11-13 20:50:45 +00004269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004270 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004271 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004272 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4273 operation->ctx.tls12_prf.Ai_with_seed_len );
4274 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004275 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004276#else
4277 if( operation->ctx.tls12_prf.seed != NULL )
4278 {
4279 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4280 operation->ctx.tls12_prf.seed_length );
4281 mbedtls_free( operation->ctx.tls12_prf.seed );
4282 }
4283
4284 if( operation->ctx.tls12_prf.label != NULL )
4285 {
4286 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4287 operation->ctx.tls12_prf.label_length );
4288 mbedtls_free( operation->ctx.tls12_prf.label );
4289 }
4290
4291 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4292
4293 /* We leave the fields Ai and output_block to be erased safely by the
4294 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004295#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004296 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004297 else
4298#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299 {
4300 status = PSA_ERROR_BAD_STATE;
4301 }
Janos Follath083036a2019-06-11 10:22:26 +01004302 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004303 return( status );
4304}
4305
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004307 size_t *capacity)
4308{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004309 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004310 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004312 return PSA_ERROR_BAD_STATE;
4313 }
4314
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004315 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004316 return( PSA_SUCCESS );
4317}
4318
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004319psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004320 size_t capacity )
4321{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004323 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004324 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004325 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004326 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004327 return( PSA_SUCCESS );
4328}
4329
Gilles Peskinebef7f142018-07-12 17:22:21 +02004330#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004331/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004332 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004333static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004334 psa_algorithm_t hash_alg,
4335 uint8_t *output,
4336 size_t output_length )
4337{
4338 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4339 psa_status_t status;
4340
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004341 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4342 return( PSA_ERROR_BAD_STATE );
4343 hkdf->state = HKDF_STATE_OUTPUT;
4344
Gilles Peskinebef7f142018-07-12 17:22:21 +02004345 while( output_length != 0 )
4346 {
4347 /* Copy what remains of the current block */
4348 uint8_t n = hash_length - hkdf->offset_in_block;
4349 if( n > output_length )
4350 n = (uint8_t) output_length;
4351 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4352 output += n;
4353 output_length -= n;
4354 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004355 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004356 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004357 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004358 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004360 * object was corrupted or if this function is called directly
4361 * inside the library. */
4362 if( hkdf->block_number == 0xff )
4363 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004364
4365 /* We need a new block */
4366 ++hkdf->block_number;
4367 hkdf->offset_in_block = 0;
4368 status = psa_hmac_setup_internal( &hkdf->hmac,
4369 hkdf->prk, hash_length,
4370 hash_alg );
4371 if( status != PSA_SUCCESS )
4372 return( status );
4373 if( hkdf->block_number != 1 )
4374 {
4375 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4376 hkdf->output_block,
4377 hash_length );
4378 if( status != PSA_SUCCESS )
4379 return( status );
4380 }
4381 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4382 hkdf->info,
4383 hkdf->info_length );
4384 if( status != PSA_SUCCESS )
4385 return( status );
4386 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4387 &hkdf->block_number, 1 );
4388 if( status != PSA_SUCCESS )
4389 return( status );
4390 status = psa_hmac_finish_internal( &hkdf->hmac,
4391 hkdf->output_block,
4392 sizeof( hkdf->output_block ) );
4393 if( status != PSA_SUCCESS )
4394 return( status );
4395 }
4396
4397 return( PSA_SUCCESS );
4398}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004399
Janos Follath999f6482019-06-11 12:04:10 +01004400#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004401static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4402 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004403 psa_algorithm_t alg )
4404{
4405 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4406 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4407 psa_hmac_internal_data hmac;
4408 psa_status_t status, cleanup_status;
4409
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004410 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004411 size_t Ai_len;
4412
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004413 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004414 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004416 * object was corrupted or if this function is called directly
4417 * inside the library. */
4418 if( tls12_prf->block_number == 0xff )
4419 return( PSA_ERROR_BAD_STATE );
4420
4421 /* We need a new block */
4422 ++tls12_prf->block_number;
4423 tls12_prf->offset_in_block = 0;
4424
4425 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4426 *
4427 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4428 *
4429 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4430 * HMAC_hash(secret, A(2) + seed) +
4431 * HMAC_hash(secret, A(3) + seed) + ...
4432 *
4433 * A(0) = seed
4434 * A(i) = HMAC_hash( secret, A(i-1) )
4435 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004436 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004437 * `HMAC_hash(secret, A(i) + seed)` from which the output
4438 * is currently extracted as `output_block`, while
4439 * `A(i) + seed` is stored in `Ai_with_seed`.
4440 *
4441 * Generating a new block means recalculating `Ai_with_seed`
4442 * from the A(i)-part of it, and afterwards recalculating
4443 * `output_block`.
4444 *
4445 * A(0) is computed at setup time.
4446 *
4447 */
4448
4449 psa_hmac_init_internal( &hmac );
4450
4451 /* We must distinguish the calculation of A(1) from those
4452 * of A(2) and higher, because A(0)=seed has a different
4453 * length than the other A(i). */
4454 if( tls12_prf->block_number == 1 )
4455 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004456 Ai = tls12_prf->Ai_with_seed + hash_length;
4457 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004458 }
4459 else
4460 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004461 Ai = tls12_prf->Ai_with_seed;
4462 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004463 }
4464
Hanno Becker3b339e22018-11-13 20:56:14 +00004465 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4466 status = psa_hmac_setup_internal( &hmac,
4467 tls12_prf->key,
4468 tls12_prf->key_len,
4469 hash_alg );
4470 if( status != PSA_SUCCESS )
4471 goto cleanup;
4472
4473 status = psa_hash_update( &hmac.hash_ctx,
4474 Ai, Ai_len );
4475 if( status != PSA_SUCCESS )
4476 goto cleanup;
4477
4478 status = psa_hmac_finish_internal( &hmac,
4479 tls12_prf->Ai_with_seed,
4480 hash_length );
4481 if( status != PSA_SUCCESS )
4482 goto cleanup;
4483
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004484 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4485 status = psa_hmac_setup_internal( &hmac,
4486 tls12_prf->key,
4487 tls12_prf->key_len,
4488 hash_alg );
4489 if( status != PSA_SUCCESS )
4490 goto cleanup;
4491
4492 status = psa_hash_update( &hmac.hash_ctx,
4493 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004494 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004495 if( status != PSA_SUCCESS )
4496 goto cleanup;
4497
4498 status = psa_hmac_finish_internal( &hmac,
4499 tls12_prf->output_block,
4500 hash_length );
4501 if( status != PSA_SUCCESS )
4502 goto cleanup;
4503
4504cleanup:
4505
4506 cleanup_status = psa_hmac_abort_internal( &hmac );
4507 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4508 status = cleanup_status;
4509
4510 return( status );
4511}
Janos Follath7742fee2019-06-17 12:58:10 +01004512#else
4513static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4514 psa_tls12_prf_key_derivation_t *tls12_prf,
4515 psa_algorithm_t alg )
4516{
4517 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4518 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004519 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4520 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004521
Janos Follath7742fee2019-06-17 12:58:10 +01004522 /* We can't be wanting more output after block 0xff, otherwise
4523 * the capacity check in psa_key_derivation_output_bytes() would have
4524 * prevented this call. It could happen only if the operation
4525 * object was corrupted or if this function is called directly
4526 * inside the library. */
4527 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004528 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004529
4530 /* We need a new block */
4531 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004532 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004533
4534 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4535 *
4536 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4537 *
4538 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4539 * HMAC_hash(secret, A(2) + seed) +
4540 * HMAC_hash(secret, A(3) + seed) + ...
4541 *
4542 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004543 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004544 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004545 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004546 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004547 * is currently extracted as `output_block` and where i is
4548 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004549 */
4550
Janos Follathea29bfb2019-06-19 12:21:20 +01004551 /* Save the hash context before using it, to preserve the hash state with
4552 * only the inner padding in it. We need this, because inner padding depends
4553 * on the key (secret in the RFC's terminology). */
4554 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4555 if( status != PSA_SUCCESS )
4556 goto cleanup;
4557
4558 /* Calculate A(i) where i = tls12_prf->block_number. */
4559 if( tls12_prf->block_number == 1 )
4560 {
4561 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4562 * the variable seed and in this instance means it in the context of the
4563 * P_hash function, where seed = label + seed.) */
4564 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4565 tls12_prf->label, tls12_prf->label_length );
4566 if( status != PSA_SUCCESS )
4567 goto cleanup;
4568 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4569 tls12_prf->seed, tls12_prf->seed_length );
4570 if( status != PSA_SUCCESS )
4571 goto cleanup;
4572 }
4573 else
4574 {
4575 /* A(i) = HMAC_hash(secret, A(i-1)) */
4576 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4577 tls12_prf->Ai, hash_length );
4578 if( status != PSA_SUCCESS )
4579 goto cleanup;
4580 }
4581
4582 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4583 tls12_prf->Ai, hash_length );
4584 if( status != PSA_SUCCESS )
4585 goto cleanup;
4586 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4587 if( status != PSA_SUCCESS )
4588 goto cleanup;
4589
4590 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4591 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4592 tls12_prf->Ai, hash_length );
4593 if( status != PSA_SUCCESS )
4594 goto cleanup;
4595 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4596 tls12_prf->label, tls12_prf->label_length );
4597 if( status != PSA_SUCCESS )
4598 goto cleanup;
4599 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4600 tls12_prf->seed, tls12_prf->seed_length );
4601 if( status != PSA_SUCCESS )
4602 goto cleanup;
4603 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4604 tls12_prf->output_block, hash_length );
4605 if( status != PSA_SUCCESS )
4606 goto cleanup;
4607 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4608 if( status != PSA_SUCCESS )
4609 goto cleanup;
4610
Janos Follath7742fee2019-06-17 12:58:10 +01004611
4612cleanup:
4613
Janos Follathea29bfb2019-06-19 12:21:20 +01004614 cleanup_status = psa_hash_abort( &backup );
4615 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4616 status = cleanup_status;
4617
4618 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004619}
Janos Follath999f6482019-06-11 12:04:10 +01004620#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004621
Janos Follath999f6482019-06-11 12:04:10 +01004622#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004624 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004625static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004626 psa_tls12_prf_key_derivation_t *tls12_prf,
4627 psa_algorithm_t alg,
4628 uint8_t *output,
4629 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004630{
4631 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4632 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4633 psa_status_t status;
4634
4635 while( output_length != 0 )
4636 {
4637 /* Copy what remains of the current block */
4638 uint8_t n = hash_length - tls12_prf->offset_in_block;
4639
4640 /* Check if we have fully processed the current block. */
4641 if( n == 0 )
4642 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004643 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004644 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004645 if( status != PSA_SUCCESS )
4646 return( status );
4647
4648 continue;
4649 }
4650
4651 if( n > output_length )
4652 n = (uint8_t) output_length;
4653 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4654 n );
4655 output += n;
4656 output_length -= n;
4657 tls12_prf->offset_in_block += n;
4658 }
4659
4660 return( PSA_SUCCESS );
4661}
Janos Follath844eb0e2019-06-19 12:10:49 +01004662#else
4663static psa_status_t psa_key_derivation_tls12_prf_read(
4664 psa_tls12_prf_key_derivation_t *tls12_prf,
4665 psa_algorithm_t alg,
4666 uint8_t *output,
4667 size_t output_length )
4668{
4669 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4670 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4671 psa_status_t status;
4672 uint8_t offset, length;
4673
4674 while( output_length != 0 )
4675 {
4676 /* Check if we have fully processed the current block. */
4677 if( tls12_prf->left_in_block == 0 )
4678 {
4679 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4680 alg );
4681 if( status != PSA_SUCCESS )
4682 return( status );
4683
4684 continue;
4685 }
4686
4687 if( tls12_prf->left_in_block > output_length )
4688 length = (uint8_t) output_length;
4689 else
4690 length = tls12_prf->left_in_block;
4691
4692 offset = hash_length - tls12_prf->left_in_block;
4693 memcpy( output, tls12_prf->output_block + offset, length );
4694 output += length;
4695 output_length -= length;
4696 tls12_prf->left_in_block -= length;
4697 }
4698
4699 return( PSA_SUCCESS );
4700}
Janos Follath999f6482019-06-11 12:04:10 +01004701#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004702#endif /* MBEDTLS_MD_C */
4703
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004704psa_status_t psa_key_derivation_output_bytes(
4705 psa_key_derivation_operation_t *operation,
4706 uint8_t *output,
4707 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708{
4709 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004711
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004713 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004715 return PSA_ERROR_BAD_STATE;
4716 }
4717
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004719 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004723 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004724 goto exit;
4725 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004729 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004730 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4731 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004733 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004734 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737
Gilles Peskinebef7f142018-07-12 17:22:21 +02004738#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004739 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004740 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004741 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004743 output, output_length );
4744 }
Janos Follathadbec812019-06-14 11:05:39 +01004745 else
Janos Follathadbec812019-06-14 11:05:39 +01004746 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004747 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004748 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004750 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004751 output_length );
4752 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004753 else
4754#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004755 {
4756 return( PSA_ERROR_BAD_STATE );
4757 }
4758
4759exit:
4760 if( status != PSA_SUCCESS )
4761 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004762 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 * This allows us to differentiate between exhausted operations and
4764 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4765 * operations. */
4766 psa_algorithm_t alg = operation->alg;
4767 psa_key_derivation_abort( operation );
4768 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004769 memset( output, '!', output_length );
4770 }
4771 return( status );
4772}
4773
Gilles Peskine08542d82018-07-19 17:05:42 +02004774#if defined(MBEDTLS_DES_C)
4775static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4776{
4777 if( data_size >= 8 )
4778 mbedtls_des_key_set_parity( data );
4779 if( data_size >= 16 )
4780 mbedtls_des_key_set_parity( data + 8 );
4781 if( data_size >= 24 )
4782 mbedtls_des_key_set_parity( data + 16 );
4783}
4784#endif /* MBEDTLS_DES_C */
4785
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004786static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004787 psa_key_slot_t *slot,
4788 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004790{
4791 uint8_t *data = NULL;
4792 size_t bytes = PSA_BITS_TO_BYTES( bits );
4793 psa_status_t status;
4794
Gilles Peskine8e338702019-07-30 20:06:31 +02004795 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004796 return( PSA_ERROR_INVALID_ARGUMENT );
4797 if( bits % 8 != 0 )
4798 return( PSA_ERROR_INVALID_ARGUMENT );
4799 data = mbedtls_calloc( 1, bytes );
4800 if( data == NULL )
4801 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004804 if( status != PSA_SUCCESS )
4805 goto exit;
4806#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004807 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004808 psa_des_set_key_parity( data, bytes );
4809#endif /* MBEDTLS_DES_C */
4810 status = psa_import_key_into_slot( slot, data, bytes );
4811
4812exit:
4813 mbedtls_free( data );
4814 return( status );
4815}
4816
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004817psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004819 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004820{
4821 psa_status_t status;
4822 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004823 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004824 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004825#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4826 if( driver != NULL )
4827 {
4828 /* Deriving a key in a secure element is not implemented yet. */
4829 status = PSA_ERROR_NOT_SUPPORTED;
4830 }
4831#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004832 if( status == PSA_SUCCESS )
4833 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004834 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004835 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004837 }
4838 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004839 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004840 if( status != PSA_SUCCESS )
4841 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004842 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004843 *handle = 0;
4844 }
4845 return( status );
4846}
4847
Gilles Peskineeab56e42018-07-12 17:12:33 +02004848
4849
4850/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004851/* Key derivation */
4852/****************************************************************/
4853
Gilles Peskinea05219c2018-11-16 16:02:56 +01004854#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004855#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004857 * of the HKDF algorithm.
4858 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004859 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004860 * to potentially free embedded data structures and wipe confidential data.
4861 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004862static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004863 const uint8_t *secret,
4864 size_t secret_length,
4865 psa_algorithm_t hash_alg,
4866 const uint8_t *salt,
4867 size_t salt_length,
4868 const uint8_t *label,
4869 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004870{
4871 psa_status_t status;
4872 status = psa_hmac_setup_internal( &hkdf->hmac,
4873 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004874 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004875 if( status != PSA_SUCCESS )
4876 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004877 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004878 if( status != PSA_SUCCESS )
4879 return( status );
4880 status = psa_hmac_finish_internal( &hkdf->hmac,
4881 hkdf->prk,
4882 sizeof( hkdf->prk ) );
4883 if( status != PSA_SUCCESS )
4884 return( status );
4885 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4886 hkdf->block_number = 0;
4887 hkdf->info_length = label_length;
4888 if( label_length != 0 )
4889 {
4890 hkdf->info = mbedtls_calloc( 1, label_length );
4891 if( hkdf->info == NULL )
4892 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4893 memcpy( hkdf->info, label, label_length );
4894 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 hkdf->state = HKDF_STATE_KEYED;
4896 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004897 return( PSA_SUCCESS );
4898}
Janos Follath71a4c912019-06-11 09:14:47 +01004899#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004900#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004901
Gilles Peskinea05219c2018-11-16 16:02:56 +01004902#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004903#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004905 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004906 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004907 * to potentially free embedded data structures and wipe confidential data.
4908 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004909static psa_status_t psa_key_derivation_tls12_prf_setup(
4910 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004911 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004912 size_t key_len,
4913 psa_algorithm_t hash_alg,
4914 const uint8_t *salt,
4915 size_t salt_length,
4916 const uint8_t *label,
4917 size_t label_length )
4918{
4919 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004920 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4921 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004922
4923 tls12_prf->key = mbedtls_calloc( 1, key_len );
4924 if( tls12_prf->key == NULL )
4925 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4926 tls12_prf->key_len = key_len;
4927 memcpy( tls12_prf->key, key, key_len );
4928
Hanno Becker580fba12018-11-13 20:50:45 +00004929 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004930 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004931 if( overflow )
4932 return( PSA_ERROR_INVALID_ARGUMENT );
4933
4934 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4935 if( tls12_prf->Ai_with_seed == NULL )
4936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4937 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4938
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004939 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4940 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004941 if( label_length != 0 )
4942 {
4943 memcpy( tls12_prf->Ai_with_seed + hash_length,
4944 label, label_length );
4945 }
4946
4947 if( salt_length != 0 )
4948 {
4949 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4950 salt, salt_length );
4951 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004952
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004953 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004954 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004955 tls12_prf->block_number = 0;
4956 tls12_prf->offset_in_block = hash_length;
4957
4958 return( PSA_SUCCESS );
4959}
Janos Follath71a4c912019-06-11 09:14:47 +01004960#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004961
Janos Follath71a4c912019-06-11 09:14:47 +01004962#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004964static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4965 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004966 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004967 size_t psk_len,
4968 psa_algorithm_t hash_alg,
4969 const uint8_t *salt,
4970 size_t salt_length,
4971 const uint8_t *label,
4972 size_t label_length )
4973{
4974 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004975 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004976
4977 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4978 return( PSA_ERROR_INVALID_ARGUMENT );
4979
4980 /* Quoting RFC 4279, Section 2:
4981 *
4982 * The premaster secret is formed as follows: if the PSK is N octets
4983 * long, concatenate a uint16 with the value N, N zero octets, a second
4984 * uint16 with the value N, and the PSK itself.
4985 */
4986
4987 pms[0] = ( psk_len >> 8 ) & 0xff;
4988 pms[1] = ( psk_len >> 0 ) & 0xff;
4989 memset( pms + 2, 0, psk_len );
4990 pms[2 + psk_len + 0] = pms[0];
4991 pms[2 + psk_len + 1] = pms[1];
4992 memcpy( pms + 4 + psk_len, psk, psk_len );
4993
Gilles Peskinecbe66502019-05-16 16:59:18 +02004994 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004995 pms, 4 + 2 * psk_len,
4996 hash_alg,
4997 salt, salt_length,
4998 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004999
Gilles Peskine3f108122018-12-07 18:14:53 +01005000 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005001 return( status );
5002}
Janos Follath71a4c912019-06-11 09:14:47 +01005003#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005004#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005005
Janos Follath71a4c912019-06-11 09:14:47 +01005006#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005007/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005008 * to potentially free embedded data structures and wipe confidential data.
5009 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005010static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005012 const uint8_t *secret, size_t secret_length,
5013 psa_algorithm_t alg,
5014 const uint8_t *salt, size_t salt_length,
5015 const uint8_t *label, size_t label_length,
5016 size_t capacity )
5017{
5018 psa_status_t status;
5019 size_t max_capacity;
5020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 /* Set operation->alg even on failure so that abort knows what to do. */
5022 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005023
5024#if defined(MBEDTLS_MD_C)
5025 if( PSA_ALG_IS_HKDF( alg ) )
5026 {
5027 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5028 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5029 if( hash_size == 0 )
5030 return( PSA_ERROR_NOT_SUPPORTED );
5031 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005033 secret, secret_length,
5034 hash_alg,
5035 salt, salt_length,
5036 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005037 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005038 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5039 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5040 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005041 {
5042 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5043 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5044
5045 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5046 if( hash_alg != PSA_ALG_SHA_256 &&
5047 hash_alg != PSA_ALG_SHA_384 )
5048 {
5049 return( PSA_ERROR_NOT_SUPPORTED );
5050 }
5051
5052 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005053
5054 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5055 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005056 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005057 secret, secret_length,
5058 hash_alg, salt, salt_length,
5059 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005060 }
5061 else
5062 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005063 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005065 secret, secret_length,
5066 hash_alg, salt, salt_length,
5067 label, label_length );
5068 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005069 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005070 else
5071#endif
5072 {
5073 return( PSA_ERROR_NOT_SUPPORTED );
5074 }
5075
5076 if( status != PSA_SUCCESS )
5077 return( status );
5078
5079 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005081 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005082 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005083 else
5084 return( PSA_ERROR_INVALID_ARGUMENT );
5085
5086 return( PSA_SUCCESS );
5087}
Janos Follath71a4c912019-06-11 09:14:47 +01005088#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005089
Janos Follath71a4c912019-06-11 09:14:47 +01005090#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005092 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005093 psa_algorithm_t alg,
5094 const uint8_t *salt,
5095 size_t salt_length,
5096 const uint8_t *label,
5097 size_t label_length,
5098 size_t capacity )
5099{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005100 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005101 psa_status_t status;
5102
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005104 return( PSA_ERROR_BAD_STATE );
5105
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005106 /* Make sure that alg is a key derivation algorithm. This prevents
5107 * key selection algorithms, which psa_key_derivation_internal
5108 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005109 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5110 return( PSA_ERROR_INVALID_ARGUMENT );
5111
Gilles Peskine28f8f302019-07-24 13:30:31 +02005112 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005113 if( status != PSA_SUCCESS )
5114 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005115
Gilles Peskine8e338702019-07-30 20:06:31 +02005116 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005117 return( PSA_ERROR_INVALID_ARGUMENT );
5118
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005119 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005120 slot->data.raw.data,
5121 slot->data.raw.bytes,
5122 alg,
5123 salt, salt_length,
5124 label, label_length,
5125 capacity );
5126 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005128 return( status );
5129}
Janos Follath71a4c912019-06-11 09:14:47 +01005130#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005131
Gilles Peskine969c5d62019-01-16 15:53:06 +01005132static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005134 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135{
Janos Follath5fe19732019-06-20 15:09:30 +01005136 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5137 * initialisers for this union leave some bytes unspecified.) */
5138 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5139
Gilles Peskine969c5d62019-01-16 15:53:06 +01005140 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005141#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005142 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5143 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5144 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005146 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005147 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5148 if( hash_size == 0 )
5149 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005150 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5151 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005152 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005153 {
5154 return( PSA_ERROR_NOT_SUPPORTED );
5155 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005157 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158 }
5159#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005160 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005161 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005162}
5163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005165 psa_algorithm_t alg )
5166{
5167 psa_status_t status;
5168
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005170 return( PSA_ERROR_BAD_STATE );
5171
Gilles Peskine6843c292019-01-18 16:44:49 +01005172 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5173 return( PSA_ERROR_INVALID_ARGUMENT );
5174 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005175 {
5176 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005178 }
5179 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5180 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005182 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183 else
5184 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005185
5186 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005188 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189}
5190
5191#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005192static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193 psa_algorithm_t hash_alg,
5194 psa_key_derivation_step_t step,
5195 const uint8_t *data,
5196 size_t data_length )
5197{
5198 psa_status_t status;
5199 switch( step )
5200 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005201 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005202 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005204 status = psa_hmac_setup_internal( &hkdf->hmac,
5205 data, data_length,
5206 hash_alg );
5207 if( status != PSA_SUCCESS )
5208 return( status );
5209 hkdf->state = HKDF_STATE_STARTED;
5210 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005211 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 /* If no salt was provided, use an empty salt. */
5213 if( hkdf->state == HKDF_STATE_INIT )
5214 {
5215 status = psa_hmac_setup_internal( &hkdf->hmac,
5216 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005217 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005218 if( status != PSA_SUCCESS )
5219 return( status );
5220 hkdf->state = HKDF_STATE_STARTED;
5221 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005222 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005224 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5225 data, data_length );
5226 if( status != PSA_SUCCESS )
5227 return( status );
5228 status = psa_hmac_finish_internal( &hkdf->hmac,
5229 hkdf->prk,
5230 sizeof( hkdf->prk ) );
5231 if( status != PSA_SUCCESS )
5232 return( status );
5233 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5234 hkdf->block_number = 0;
5235 hkdf->state = HKDF_STATE_KEYED;
5236 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005237 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005238 if( hkdf->state == HKDF_STATE_OUTPUT )
5239 return( PSA_ERROR_BAD_STATE );
5240 if( hkdf->info_set )
5241 return( PSA_ERROR_BAD_STATE );
5242 hkdf->info_length = data_length;
5243 if( data_length != 0 )
5244 {
5245 hkdf->info = mbedtls_calloc( 1, data_length );
5246 if( hkdf->info == NULL )
5247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5248 memcpy( hkdf->info, data, data_length );
5249 }
5250 hkdf->info_set = 1;
5251 return( PSA_SUCCESS );
5252 default:
5253 return( PSA_ERROR_INVALID_ARGUMENT );
5254 }
5255}
Janos Follathb03233e2019-06-11 15:30:30 +01005256
5257#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5258static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5259 psa_algorithm_t hash_alg,
5260 psa_key_derivation_step_t step,
5261 const uint8_t *data,
5262 size_t data_length )
5263{
5264 (void) prf;
5265 (void) hash_alg;
5266 (void) step;
5267 (void) data;
5268 (void) data_length;
5269
5270 return( PSA_ERROR_INVALID_ARGUMENT );
5271}
Janos Follath6660f0e2019-06-17 08:44:03 +01005272
5273static psa_status_t psa_tls12_prf_psk_to_ms_input(
5274 psa_tls12_prf_key_derivation_t *prf,
5275 psa_algorithm_t hash_alg,
5276 psa_key_derivation_step_t step,
5277 const uint8_t *data,
5278 size_t data_length )
5279{
5280 (void) prf;
5281 (void) hash_alg;
5282 (void) step;
5283 (void) data;
5284 (void) data_length;
5285
5286 return( PSA_ERROR_INVALID_ARGUMENT );
5287}
Janos Follathb03233e2019-06-11 15:30:30 +01005288#else
Janos Follathf08e2652019-06-13 09:05:41 +01005289static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5290 const uint8_t *data,
5291 size_t data_length )
5292{
5293 if( prf->state != TLS12_PRF_STATE_INIT )
5294 return( PSA_ERROR_BAD_STATE );
5295
Janos Follathd6dce9f2019-07-04 09:11:38 +01005296 if( data_length != 0 )
5297 {
5298 prf->seed = mbedtls_calloc( 1, data_length );
5299 if( prf->seed == NULL )
5300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005301
Janos Follathd6dce9f2019-07-04 09:11:38 +01005302 memcpy( prf->seed, data, data_length );
5303 prf->seed_length = data_length;
5304 }
Janos Follathf08e2652019-06-13 09:05:41 +01005305
5306 prf->state = TLS12_PRF_STATE_SEED_SET;
5307
5308 return( PSA_SUCCESS );
5309}
5310
Janos Follath81550542019-06-13 14:26:34 +01005311static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5312 psa_algorithm_t hash_alg,
5313 const uint8_t *data,
5314 size_t data_length )
5315{
5316 psa_status_t status;
5317 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5318 return( PSA_ERROR_BAD_STATE );
5319
5320 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5321 if( status != PSA_SUCCESS )
5322 return( status );
5323
5324 prf->state = TLS12_PRF_STATE_KEY_SET;
5325
5326 return( PSA_SUCCESS );
5327}
5328
Janos Follath6660f0e2019-06-17 08:44:03 +01005329static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5330 psa_tls12_prf_key_derivation_t *prf,
5331 psa_algorithm_t hash_alg,
5332 const uint8_t *data,
5333 size_t data_length )
5334{
5335 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005336 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5337 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005338
5339 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5340 return( PSA_ERROR_INVALID_ARGUMENT );
5341
5342 /* Quoting RFC 4279, Section 2:
5343 *
5344 * The premaster secret is formed as follows: if the PSK is N octets
5345 * long, concatenate a uint16 with the value N, N zero octets, a second
5346 * uint16 with the value N, and the PSK itself.
5347 */
5348
Janos Follath40e13932019-06-26 13:22:29 +01005349 *cur++ = ( data_length >> 8 ) & 0xff;
5350 *cur++ = ( data_length >> 0 ) & 0xff;
5351 memset( cur, 0, data_length );
5352 cur += data_length;
5353 *cur++ = pms[0];
5354 *cur++ = pms[1];
5355 memcpy( cur, data, data_length );
5356 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005357
Janos Follath40e13932019-06-26 13:22:29 +01005358 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005359
5360 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5361 return( status );
5362}
5363
Janos Follath63028dd2019-06-13 09:15:47 +01005364static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5365 const uint8_t *data,
5366 size_t data_length )
5367{
5368 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5369 return( PSA_ERROR_BAD_STATE );
5370
Janos Follathd6dce9f2019-07-04 09:11:38 +01005371 if( data_length != 0 )
5372 {
5373 prf->label = mbedtls_calloc( 1, data_length );
5374 if( prf->label == NULL )
5375 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005376
Janos Follathd6dce9f2019-07-04 09:11:38 +01005377 memcpy( prf->label, data, data_length );
5378 prf->label_length = data_length;
5379 }
Janos Follath63028dd2019-06-13 09:15:47 +01005380
5381 prf->state = TLS12_PRF_STATE_LABEL_SET;
5382
5383 return( PSA_SUCCESS );
5384}
5385
Janos Follathb03233e2019-06-11 15:30:30 +01005386static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5387 psa_algorithm_t hash_alg,
5388 psa_key_derivation_step_t step,
5389 const uint8_t *data,
5390 size_t data_length )
5391{
Janos Follathb03233e2019-06-11 15:30:30 +01005392 switch( step )
5393 {
Janos Follathf08e2652019-06-13 09:05:41 +01005394 case PSA_KEY_DERIVATION_INPUT_SEED:
5395 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005396 case PSA_KEY_DERIVATION_INPUT_SECRET:
5397 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005398 case PSA_KEY_DERIVATION_INPUT_LABEL:
5399 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005400 default:
5401 return( PSA_ERROR_INVALID_ARGUMENT );
5402 }
5403}
Janos Follath6660f0e2019-06-17 08:44:03 +01005404
5405static psa_status_t psa_tls12_prf_psk_to_ms_input(
5406 psa_tls12_prf_key_derivation_t *prf,
5407 psa_algorithm_t hash_alg,
5408 psa_key_derivation_step_t step,
5409 const uint8_t *data,
5410 size_t data_length )
5411{
5412 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005413 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005414 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5415 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005416 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005417
5418 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5419}
Janos Follathb03233e2019-06-11 15:30:30 +01005420#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005421#endif /* MBEDTLS_MD_C */
5422
Janos Follathb80a94e2019-06-12 15:54:46 +01005423static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005425 psa_key_derivation_step_t step,
5426 const uint8_t *data,
5427 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005428{
5429 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005431
5432#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005433 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005434 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005436 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005437 step, data, data_length );
5438 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005439 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005440#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005442 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005443 {
Janos Follathb03233e2019-06-11 15:30:30 +01005444 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5445 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5446 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005447 }
5448 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5449 {
5450 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5451 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5452 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005453 }
5454 else
5455#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005456 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005458 return( PSA_ERROR_BAD_STATE );
5459 }
5460
5461 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005463 return( status );
5464}
5465
Janos Follath51f4a0f2019-06-14 11:35:55 +01005466psa_status_t psa_key_derivation_input_bytes(
5467 psa_key_derivation_operation_t *operation,
5468 psa_key_derivation_step_t step,
5469 const uint8_t *data,
5470 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005471{
Janos Follathc5621512019-06-14 11:27:57 +01005472 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5473 return( PSA_ERROR_INVALID_ARGUMENT );
5474
5475 return( psa_key_derivation_input_internal( operation, step,
5476 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005477}
5478
Janos Follath51f4a0f2019-06-14 11:35:55 +01005479psa_status_t psa_key_derivation_input_key(
5480 psa_key_derivation_operation_t *operation,
5481 psa_key_derivation_step_t step,
5482 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005483{
5484 psa_key_slot_t *slot;
5485 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005486 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005487 PSA_KEY_USAGE_DERIVE,
5488 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005489 if( status != PSA_SUCCESS )
5490 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005491 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005492 return( PSA_ERROR_INVALID_ARGUMENT );
5493 /* Don't allow a key to be used as an input that is usually public.
5494 * This is debatable. It's ok from a cryptographic perspective to
5495 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005496 * the material should be dedicated to a particular input step,
5497 * otherwise this may allow the key to be used in an unintended way
5498 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005499 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005500 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005501 return( psa_key_derivation_input_internal( operation,
5502 step,
5503 slot->data.raw.data,
5504 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005505}
5506
Gilles Peskineea0fb492018-07-12 17:17:20 +02005507
5508
5509/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005510/* Key agreement */
5511/****************************************************************/
5512
Gilles Peskinea05219c2018-11-16 16:02:56 +01005513#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005514static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5515 size_t peer_key_length,
5516 const mbedtls_ecp_keypair *our_key,
5517 uint8_t *shared_secret,
5518 size_t shared_secret_size,
5519 size_t *shared_secret_length )
5520{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005521 mbedtls_ecp_keypair *their_key = NULL;
5522 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005523 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005524 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005525
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005526 status = psa_import_ec_public_key(
5527 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5528 peer_key, peer_key_length,
5529 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005530 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005531 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005532
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005533 status = mbedtls_to_psa_error(
5534 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5535 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005536 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005537 status = mbedtls_to_psa_error(
5538 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5539 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005540 goto exit;
5541
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005542 status = mbedtls_to_psa_error(
5543 mbedtls_ecdh_calc_secret( &ecdh,
5544 shared_secret_length,
5545 shared_secret, shared_secret_size,
5546 mbedtls_ctr_drbg_random,
5547 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005548
5549exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005550 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005551 mbedtls_ecp_keypair_free( their_key );
5552 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005553 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005554}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005555#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005556
Gilles Peskine01d718c2018-09-18 12:01:02 +02005557#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5558
Gilles Peskine0216fe12019-04-11 21:23:21 +02005559static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5560 psa_key_slot_t *private_key,
5561 const uint8_t *peer_key,
5562 size_t peer_key_length,
5563 uint8_t *shared_secret,
5564 size_t shared_secret_size,
5565 size_t *shared_secret_length )
5566{
5567 switch( alg )
5568 {
5569#if defined(MBEDTLS_ECDH_C)
5570 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005571 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005572 return( PSA_ERROR_INVALID_ARGUMENT );
5573 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5574 private_key->data.ecp,
5575 shared_secret, shared_secret_size,
5576 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005577#endif /* MBEDTLS_ECDH_C */
5578 default:
5579 (void) private_key;
5580 (void) peer_key;
5581 (void) peer_key_length;
5582 (void) shared_secret;
5583 (void) shared_secret_size;
5584 (void) shared_secret_length;
5585 return( PSA_ERROR_NOT_SUPPORTED );
5586 }
5587}
5588
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005589/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005590 * to potentially free embedded data structures and wipe confidential data.
5591 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005593 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005594 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005595 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005596 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005597{
5598 psa_status_t status;
5599 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5600 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005601 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005602
5603 /* Step 1: run the secret agreement algorithm to generate the shared
5604 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005605 status = psa_key_agreement_raw_internal( ka_alg,
5606 private_key,
5607 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005608 shared_secret,
5609 sizeof( shared_secret ),
5610 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005611 if( status != PSA_SUCCESS )
5612 goto exit;
5613
5614 /* Step 2: set up the key derivation to generate key material from
5615 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005616 status = psa_key_derivation_input_internal( operation, step,
5617 shared_secret,
5618 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005619
Gilles Peskine01d718c2018-09-18 12:01:02 +02005620exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005621 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005622 return( status );
5623}
5624
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005625psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005626 psa_key_derivation_step_t step,
5627 psa_key_handle_t private_key,
5628 const uint8_t *peer_key,
5629 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005630{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005631 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005632 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005634 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005635 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005636 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005637 if( status != PSA_SUCCESS )
5638 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005640 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005641 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005642 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005643 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005644 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005645}
5646
Gilles Peskinebe697d82019-05-16 18:00:41 +02005647psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5648 psa_key_handle_t private_key,
5649 const uint8_t *peer_key,
5650 size_t peer_key_length,
5651 uint8_t *output,
5652 size_t output_size,
5653 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005654{
5655 psa_key_slot_t *slot;
5656 psa_status_t status;
5657
5658 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5659 {
5660 status = PSA_ERROR_INVALID_ARGUMENT;
5661 goto exit;
5662 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005663 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005664 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005665 if( status != PSA_SUCCESS )
5666 goto exit;
5667
5668 status = psa_key_agreement_raw_internal( alg, slot,
5669 peer_key, peer_key_length,
5670 output, output_size,
5671 output_length );
5672
5673exit:
5674 if( status != PSA_SUCCESS )
5675 {
5676 /* If an error happens and is not handled properly, the output
5677 * may be used as a key to protect sensitive data. Arrange for such
5678 * a key to be random, which is likely to result in decryption or
5679 * verification errors. This is better than filling the buffer with
5680 * some constant data such as zeros, which would result in the data
5681 * being protected with a reproducible, easily knowable key.
5682 */
5683 psa_generate_random( output, output_size );
5684 *output_length = output_size;
5685 }
5686 return( status );
5687}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005688
5689
5690/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005691/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005692/****************************************************************/
5693
5694psa_status_t psa_generate_random( uint8_t *output,
5695 size_t output_size )
5696{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005697 int ret;
5698 GUARD_MODULE_INITIALIZED;
5699
Gilles Peskinef181eca2019-08-07 13:49:00 +02005700 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5701 {
5702 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5703 output,
5704 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5705 if( ret != 0 )
5706 return( mbedtls_to_psa_error( ret ) );
5707 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5708 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5709 }
5710
itayzafrir0adf0fc2018-09-06 16:24:41 +03005711 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005712 return( mbedtls_to_psa_error( ret ) );
5713}
5714
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005715#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5716#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005717
Gilles Peskine7228da22019-07-15 11:06:15 +02005718psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005719 size_t seed_size )
5720{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005721 if( global_data.initialized )
5722 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005723
5724 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5725 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5726 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5727 return( PSA_ERROR_INVALID_ARGUMENT );
5728
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005729 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005730}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005731#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005732
Gilles Peskinee56e8782019-04-26 17:34:02 +02005733#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5734static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5735 size_t domain_parameters_size,
5736 int *exponent )
5737{
5738 size_t i;
5739 uint32_t acc = 0;
5740
5741 if( domain_parameters_size == 0 )
5742 {
5743 *exponent = 65537;
5744 return( PSA_SUCCESS );
5745 }
5746
5747 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5748 * support values that fit in a 32-bit integer, which is larger than
5749 * int on just about every platform anyway. */
5750 if( domain_parameters_size > sizeof( acc ) )
5751 return( PSA_ERROR_NOT_SUPPORTED );
5752 for( i = 0; i < domain_parameters_size; i++ )
5753 acc = ( acc << 8 ) | domain_parameters[i];
5754 if( acc > INT_MAX )
5755 return( PSA_ERROR_NOT_SUPPORTED );
5756 *exponent = acc;
5757 return( PSA_SUCCESS );
5758}
5759#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5760
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005761static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005762 psa_key_slot_t *slot, size_t bits,
5763 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005764{
Gilles Peskine8e338702019-07-30 20:06:31 +02005765 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005766
Gilles Peskinee56e8782019-04-26 17:34:02 +02005767 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005768 return( PSA_ERROR_INVALID_ARGUMENT );
5769
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005770 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005771 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005772 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005773 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005774 if( status != PSA_SUCCESS )
5775 return( status );
5776 status = psa_generate_random( slot->data.raw.data,
5777 slot->data.raw.bytes );
5778 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005779 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005780#if defined(MBEDTLS_DES_C)
5781 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005782 psa_des_set_key_parity( slot->data.raw.data,
5783 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005784#endif /* MBEDTLS_DES_C */
5785 }
5786 else
5787
5788#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005789 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005790 {
5791 mbedtls_rsa_context *rsa;
5792 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005793 int exponent;
5794 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005795 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5796 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005797 /* Accept only byte-aligned keys, for the same reasons as
5798 * in psa_import_rsa_key(). */
5799 if( bits % 8 != 0 )
5800 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005801 status = psa_read_rsa_exponent( domain_parameters,
5802 domain_parameters_size,
5803 &exponent );
5804 if( status != PSA_SUCCESS )
5805 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005806 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5807 if( rsa == NULL )
5808 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5809 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5810 ret = mbedtls_rsa_gen_key( rsa,
5811 mbedtls_ctr_drbg_random,
5812 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005813 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005814 exponent );
5815 if( ret != 0 )
5816 {
5817 mbedtls_rsa_free( rsa );
5818 mbedtls_free( rsa );
5819 return( mbedtls_to_psa_error( ret ) );
5820 }
5821 slot->data.rsa = rsa;
5822 }
5823 else
5824#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5825
5826#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005827 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005828 {
5829 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5830 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5831 const mbedtls_ecp_curve_info *curve_info =
5832 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5833 mbedtls_ecp_keypair *ecp;
5834 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005835 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005836 return( PSA_ERROR_NOT_SUPPORTED );
5837 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5838 return( PSA_ERROR_NOT_SUPPORTED );
5839 if( curve_info->bit_size != bits )
5840 return( PSA_ERROR_INVALID_ARGUMENT );
5841 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5842 if( ecp == NULL )
5843 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5844 mbedtls_ecp_keypair_init( ecp );
5845 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5846 mbedtls_ctr_drbg_random,
5847 &global_data.ctr_drbg );
5848 if( ret != 0 )
5849 {
5850 mbedtls_ecp_keypair_free( ecp );
5851 mbedtls_free( ecp );
5852 return( mbedtls_to_psa_error( ret ) );
5853 }
5854 slot->data.ecp = ecp;
5855 }
5856 else
5857#endif /* MBEDTLS_ECP_C */
5858
5859 return( PSA_ERROR_NOT_SUPPORTED );
5860
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005861 return( PSA_SUCCESS );
5862}
5863
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005864psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005865 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005866{
5867 psa_status_t status;
5868 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005869 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005870 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005871#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5872 if( driver != NULL )
5873 {
5874 /* Generating a key in a secure element is not implemented yet. */
5875 status = PSA_ERROR_NOT_SUPPORTED;
5876 }
5877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005878 if( status == PSA_SUCCESS )
5879 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005880 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005881 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005882 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005883 }
5884 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005885 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005886 if( status != PSA_SUCCESS )
5887 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005888 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005889 *handle = 0;
5890 }
5891 return( status );
5892}
5893
5894
Gilles Peskine05d69892018-06-19 22:00:52 +02005895
5896/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005897/* Module setup */
5898/****************************************************************/
5899
Gilles Peskine5e769522018-11-20 21:59:56 +01005900psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5901 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5902 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5903{
5904 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5905 return( PSA_ERROR_BAD_STATE );
5906 global_data.entropy_init = entropy_init;
5907 global_data.entropy_free = entropy_free;
5908 return( PSA_SUCCESS );
5909}
5910
Gilles Peskinee59236f2018-01-27 23:32:46 +01005911void mbedtls_psa_crypto_free( void )
5912{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005913 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005914 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5915 {
5916 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005917 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005918 }
5919 /* Wipe all remaining data, including configuration.
5920 * In particular, this sets all state indicator to the value
5921 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005922 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005923#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005924 /* Unregister all secure element drivers, so that we restart from
5925 * a pristine state. */
5926 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005927#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005928}
5929
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005930#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5931/** Recover a transaction that was interrupted by a power failure.
5932 *
5933 * This function is called during initialization, before psa_crypto_init()
5934 * returns. If this function returns a failure status, the initialization
5935 * fails.
5936 */
5937static psa_status_t psa_crypto_recover_transaction(
5938 const psa_crypto_transaction_t *transaction )
5939{
5940 switch( transaction->unknown.type )
5941 {
5942 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5943 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5944 /* TOnogrepDO - fall through to the failure case until this
5945 * is implemented */
5946 default:
5947 /* We found an unsupported transaction in the storage.
5948 * We don't know what state the storage is in. Give up. */
5949 return( PSA_ERROR_STORAGE_FAILURE );
5950 }
5951}
5952#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5953
Gilles Peskinee59236f2018-01-27 23:32:46 +01005954psa_status_t psa_crypto_init( void )
5955{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005956 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005957 const unsigned char drbg_seed[] = "PSA";
5958
Gilles Peskinec6b69072018-11-20 21:42:52 +01005959 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005960 if( global_data.initialized != 0 )
5961 return( PSA_SUCCESS );
5962
Gilles Peskine5e769522018-11-20 21:59:56 +01005963 /* Set default configuration if
5964 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5965 if( global_data.entropy_init == NULL )
5966 global_data.entropy_init = mbedtls_entropy_init;
5967 if( global_data.entropy_free == NULL )
5968 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005969
Gilles Peskinec6b69072018-11-20 21:42:52 +01005970 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005971 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005972#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5973 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5974 /* The PSA entropy injection feature depends on using NV seed as an entropy
5975 * source. Add NV seed as an entropy source for PSA entropy injection. */
5976 mbedtls_entropy_add_source( &global_data.entropy,
5977 mbedtls_nv_seed_poll, NULL,
5978 MBEDTLS_ENTROPY_BLOCK_SIZE,
5979 MBEDTLS_ENTROPY_SOURCE_STRONG );
5980#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005981 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005982 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005983 status = mbedtls_to_psa_error(
5984 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5985 mbedtls_entropy_func,
5986 &global_data.entropy,
5987 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5988 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005989 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005990 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005991
Gilles Peskine66fb1262018-12-10 16:29:04 +01005992 status = psa_initialize_key_slots( );
5993 if( status != PSA_SUCCESS )
5994 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005995
Gilles Peskine4b734222019-07-24 15:56:31 +02005996#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005997 status = psa_crypto_load_transaction( );
5998 if( status == PSA_SUCCESS )
5999 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006000 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6001 if( status != PSA_SUCCESS )
6002 goto exit;
6003 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006004 }
6005 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6006 {
6007 /* There's no transaction to complete. It's all good. */
6008 status = PSA_SUCCESS;
6009 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006010#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006011
Gilles Peskinec6b69072018-11-20 21:42:52 +01006012 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006013 global_data.initialized = 1;
6014
Gilles Peskinee59236f2018-01-27 23:32:46 +01006015exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006016 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006017 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006018 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006019}
6020
6021#endif /* MBEDTLS_PSA_CRYPTO_C */