blob: 66c515108c85b060bb192f03b56c4f5a370a4f16 [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 Peskinef77ed1f2018-12-03 11:58:46 +0100997/** Completely wipe a slot in memory, including its policy.
998 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100999psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000{
1001 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001002 /* Multipart operations may still be using the key. This is safe
1003 * because all multipart operation objects are independent from
1004 * the key slot: if they need to access the key after the setup
1005 * phase, they have a copy of the key. Note that this means that
1006 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007 /* At this point, key material and other type-specific content has
1008 * been wiped. Clear remaining metadata. We can call memset and not
1009 * zeroize because the metadata is not particularly sensitive. */
1010 memset( slot, 0, sizeof( *slot ) );
1011 return( status );
1012}
1013
Gilles Peskinec5487a82018-12-03 18:08:14 +01001014psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001016 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001017 psa_status_t status = PSA_SUCCESS;
1018 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1020 psa_se_drv_table_entry_t *driver;
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskinec5487a82018-12-03 18:08:14 +01001023 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001024 if( status != PSA_SUCCESS )
1025 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001026
1027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001030 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001031 /* For a key in a secure element, we need to do three things:
1032 * remove the key file in internal storage, destroy the
1033 * key inside the secure element, and update the driver's
1034 * persistent data. Start a transaction that will encompass these
1035 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 status = psa_crypto_save_transaction( );
1041 if( status != PSA_SUCCESS )
1042 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001043 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 /* TOnogrepDO: destroy what can be destroyed anyway */
1045 return( status );
1046 }
1047
Gilles Peskine354f7672019-07-12 23:46:38 +02001048 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001050#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1051
Darryl Greend49a4992018-06-18 17:27:26 +01001052#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001053 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001054 {
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001055 storage_status = psa_destroy_persistent_key( slot->attr.id );
1056 /* TODO: other slots may have a copy of the same key. We should
1057 * invalidate them.
1058 * https://github.com/ARMmbed/mbed-crypto/issues/214
1059 */
Darryl Greend49a4992018-06-18 17:27:26 +01001060 }
1061#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001062
Gilles Peskinefc762652019-07-22 19:30:34 +02001063#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1064 if( driver != NULL )
1065 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001066 psa_status_t status2;
1067 status = psa_save_se_persistent_data( driver );
1068 status2 = psa_crypto_stop_transaction( );
1069 if( status == PSA_SUCCESS )
1070 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001071 if( status != PSA_SUCCESS )
1072 {
1073 /* TOnogrepDO: destroy what can be destroyed anyway */
1074 return( status );
1075 }
1076 }
1077#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1078
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001079 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001080 if( status != PSA_SUCCESS )
1081 return( status );
1082 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083}
1084
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001085void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1086{
Gilles Peskineb699f072019-04-26 16:06:02 +02001087 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001088 memset( attributes, 0, sizeof( *attributes ) );
1089}
1090
Gilles Peskineb699f072019-04-26 16:06:02 +02001091psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1092 psa_key_type_t type,
1093 const uint8_t *data,
1094 size_t data_length )
1095{
1096 uint8_t *copy = NULL;
1097
1098 if( data_length != 0 )
1099 {
1100 copy = mbedtls_calloc( 1, data_length );
1101 if( copy == NULL )
1102 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1103 memcpy( copy, data, data_length );
1104 }
1105 /* After this point, this function is guaranteed to succeed, so it
1106 * can start modifying `*attributes`. */
1107
1108 if( attributes->domain_parameters != NULL )
1109 {
1110 mbedtls_free( attributes->domain_parameters );
1111 attributes->domain_parameters = NULL;
1112 attributes->domain_parameters_size = 0;
1113 }
1114
1115 attributes->domain_parameters = copy;
1116 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001117 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001118 return( PSA_SUCCESS );
1119}
1120
1121psa_status_t psa_get_key_domain_parameters(
1122 const psa_key_attributes_t *attributes,
1123 uint8_t *data, size_t data_size, size_t *data_length )
1124{
1125 if( attributes->domain_parameters_size > data_size )
1126 return( PSA_ERROR_BUFFER_TOO_SMALL );
1127 *data_length = attributes->domain_parameters_size;
1128 if( attributes->domain_parameters_size != 0 )
1129 memcpy( data, attributes->domain_parameters,
1130 attributes->domain_parameters_size );
1131 return( PSA_SUCCESS );
1132}
1133
1134#if defined(MBEDTLS_RSA_C)
1135static psa_status_t psa_get_rsa_public_exponent(
1136 const mbedtls_rsa_context *rsa,
1137 psa_key_attributes_t *attributes )
1138{
1139 mbedtls_mpi mpi;
1140 int ret;
1141 uint8_t *buffer = NULL;
1142 size_t buflen;
1143 mbedtls_mpi_init( &mpi );
1144
1145 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1146 if( ret != 0 )
1147 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001148 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1149 {
1150 /* It's the default value, which is reported as an empty string,
1151 * so there's nothing to do. */
1152 goto exit;
1153 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001154
1155 buflen = mbedtls_mpi_size( &mpi );
1156 buffer = mbedtls_calloc( 1, buflen );
1157 if( buffer == NULL )
1158 {
1159 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1160 goto exit;
1161 }
1162 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1163 if( ret != 0 )
1164 goto exit;
1165 attributes->domain_parameters = buffer;
1166 attributes->domain_parameters_size = buflen;
1167
1168exit:
1169 mbedtls_mpi_free( &mpi );
1170 if( ret != 0 )
1171 mbedtls_free( buffer );
1172 return( mbedtls_to_psa_error( ret ) );
1173}
1174#endif /* MBEDTLS_RSA_C */
1175
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001176/** Retrieve all the publicly-accessible attributes of a key.
1177 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001178psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1179 psa_key_attributes_t *attributes )
1180{
1181 psa_key_slot_t *slot;
1182 psa_status_t status;
1183
1184 psa_reset_key_attributes( attributes );
1185
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001186 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187 if( status != PSA_SUCCESS )
1188 return( status );
1189
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001190 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001191 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1192 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1193
1194#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1195 if( psa_key_slot_is_external( slot ) )
1196 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1197#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001198
Gilles Peskine8e338702019-07-30 20:06:31 +02001199 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 {
1201#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001202 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001203 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205 /* TOnogrepDO: reporting the public exponent for opaque keys
1206 * is not yet implemented. */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001207 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001208 break;
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1211 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001212#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 default:
1214 /* Nothing else to do. */
1215 break;
1216 }
1217
1218 if( status != PSA_SUCCESS )
1219 psa_reset_key_attributes( attributes );
1220 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001221}
1222
Gilles Peskinec8000c02019-08-02 20:15:51 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1224psa_status_t psa_get_key_slot_number(
1225 const psa_key_attributes_t *attributes,
1226 psa_key_slot_number_t *slot_number )
1227{
1228 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1229 {
1230 *slot_number = attributes->slot_number;
1231 return( PSA_SUCCESS );
1232 }
1233 else
1234 return( PSA_ERROR_INVALID_ARGUMENT );
1235}
1236#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1237
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001238#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001239static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1240 unsigned char *buf, size_t size )
1241{
1242 int ret;
1243 unsigned char *c;
1244 size_t len = 0;
1245
1246 c = buf + size;
1247
1248 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1249
1250 return( (int) len );
1251}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001252#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001253
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001254static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1255 uint8_t *data,
1256 size_t data_size,
1257 size_t *data_length,
1258 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259{
Gilles Peskine5d309672019-07-12 23:47:28 +02001260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1261 const psa_drv_se_t *drv;
1262 psa_drv_se_context_t *drv_context;
1263#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1264
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001265 *data_length = 0;
1266
Gilles Peskine8e338702019-07-30 20:06:31 +02001267 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001268 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001269
Gilles Peskine5d309672019-07-12 23:47:28 +02001270#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001271 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001272 {
1273 psa_drv_se_export_key_t method;
1274 if( drv->key_management == NULL )
1275 return( PSA_ERROR_NOT_SUPPORTED );
1276 method = ( export_public_key ?
1277 drv->key_management->p_export_public :
1278 drv->key_management->p_export );
1279 if( method == NULL )
1280 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001281 return( method( drv_context,
1282 slot->data.se.slot_number,
1283 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001284 }
1285#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1286
Gilles Peskine8e338702019-07-30 20:06:31 +02001287 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288 {
1289 if( slot->data.raw.bytes > data_size )
1290 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001291 if( data_size != 0 )
1292 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001293 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001294 memset( data + slot->data.raw.bytes, 0,
1295 data_size - slot->data.raw.bytes );
1296 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001297 *data_length = slot->data.raw.bytes;
1298 return( PSA_SUCCESS );
1299 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001300#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001301 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001302 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001303 psa_status_t status;
1304
Gilles Peskineb46bef22019-07-30 21:32:04 +02001305 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001306 if( bytes > data_size )
1307 return( PSA_ERROR_BUFFER_TOO_SMALL );
1308 status = mbedtls_to_psa_error(
1309 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1310 if( status != PSA_SUCCESS )
1311 return( status );
1312 memset( data + bytes, 0, data_size - bytes );
1313 *data_length = bytes;
1314 return( PSA_SUCCESS );
1315 }
1316#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 else
Moran Peker17e36e12018-05-02 12:55:20 +03001318 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001319#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001320 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1321 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001322 {
Moran Pekera998bc62018-04-16 18:16:20 +03001323 mbedtls_pk_context pk;
1324 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001325 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001326 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001327#if defined(MBEDTLS_RSA_C)
1328 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001329 pk.pk_info = &mbedtls_rsa_info;
1330 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001331#else
1332 return( PSA_ERROR_NOT_SUPPORTED );
1333#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001334 }
1335 else
1336 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001337#if defined(MBEDTLS_ECP_C)
1338 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001339 pk.pk_info = &mbedtls_eckey_info;
1340 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#else
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001344 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001345 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001346 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001347 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001348 }
Moran Peker17e36e12018-05-02 12:55:20 +03001349 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001350 {
Moran Peker17e36e12018-05-02 12:55:20 +03001351 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001352 }
Moran Peker60364322018-04-29 11:34:58 +03001353 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001354 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001355 /* If data_size is 0 then data may be NULL and then the
1356 * call to memset would have undefined behavior. */
1357 if( data_size != 0 )
1358 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001359 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001360 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001361 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1362 * Move the data to the beginning and erase remaining data
1363 * at the original location. */
1364 if( 2 * (size_t) ret <= data_size )
1365 {
1366 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001368 }
1369 else if( (size_t) ret < data_size )
1370 {
1371 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001372 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001373 }
Moran Pekera998bc62018-04-16 18:16:20 +03001374 *data_length = ret;
1375 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001376 }
1377 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001378#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001379 {
1380 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001381 it is valid for a special-purpose implementation to omit
1382 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001383 return( PSA_ERROR_NOT_SUPPORTED );
1384 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001385 }
1386}
1387
Gilles Peskinec5487a82018-12-03 18:08:14 +01001388psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001389 uint8_t *data,
1390 size_t data_size,
1391 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001392{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001393 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001394 psa_status_t status;
1395
1396 /* Set the key to empty now, so that even when there are errors, we always
1397 * set data_length to a value between 0 and data_size. On error, setting
1398 * the key to empty is a good choice because an empty key representation is
1399 * unlikely to be accepted anywhere. */
1400 *data_length = 0;
1401
1402 /* Export requires the EXPORT flag. There is an exception for public keys,
1403 * which don't require any flag, but psa_get_key_from_slot takes
1404 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001405 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001406 if( status != PSA_SUCCESS )
1407 return( status );
1408 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001409 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411
Gilles Peskinec5487a82018-12-03 18:08:14 +01001412psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001413 uint8_t *data,
1414 size_t data_size,
1415 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001416{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001417 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001418 psa_status_t status;
1419
1420 /* Set the key to empty now, so that even when there are errors, we always
1421 * set data_length to a value between 0 and data_size. On error, setting
1422 * the key to empty is a good choice because an empty key representation is
1423 * unlikely to be accepted anywhere. */
1424 *data_length = 0;
1425
1426 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001427 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 if( status != PSA_SUCCESS )
1429 return( status );
1430 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001431 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001432}
1433
Gilles Peskine91e8c332019-08-02 19:19:39 +02001434#if defined(static_assert)
1435static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1436 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001437static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001438 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001439static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001440 "One or more key attribute flag is listed as both internal-only and external-only" );
1441#endif
1442
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001443/** Validate that a key policy is internally well-formed.
1444 *
1445 * This function only rejects invalid policies. It does not validate the
1446 * consistency of the policy with respect to other attributes of the key
1447 * such as the key type.
1448 */
1449static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001450{
1451 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001452 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 PSA_KEY_USAGE_ENCRYPT |
1454 PSA_KEY_USAGE_DECRYPT |
1455 PSA_KEY_USAGE_SIGN |
1456 PSA_KEY_USAGE_VERIFY |
1457 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1458 return( PSA_ERROR_INVALID_ARGUMENT );
1459
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 return( PSA_SUCCESS );
1461}
1462
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001463/** Validate the internal consistency of key attributes.
1464 *
1465 * This function only rejects invalid attribute values. If does not
1466 * validate the consistency of the attributes with any key data that may
1467 * be involved in the creation of the key.
1468 *
1469 * Call this function early in the key creation process.
1470 *
1471 * \param[in] attributes Key attributes for the new key.
1472 * \param[out] p_drv On any return, the driver for the key, if any.
1473 * NULL for a transparent key.
1474 *
1475 */
1476static psa_status_t psa_validate_key_attributes(
1477 const psa_key_attributes_t *attributes,
1478 psa_se_drv_table_entry_t **p_drv )
1479{
1480 psa_status_t status;
1481
1482 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1483 {
1484 status = psa_validate_persistent_key_parameters(
1485 attributes->core.lifetime, attributes->core.id,
1486 p_drv, 1 );
1487 if( status != PSA_SUCCESS )
1488 return( status );
1489 }
1490
1491 status = psa_validate_key_policy( &attributes->core.policy );
1492 if( status != PSA_SUCCESS )
1493 return( status );
1494
1495 /* Refuse to create overly large keys.
1496 * Note that this doesn't trigger on import if the attributes don't
1497 * explicitly specify a size (so psa_get_key_bits returns 0), so
1498 * psa_import_key() needs its own checks. */
1499 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1500 return( PSA_ERROR_NOT_SUPPORTED );
1501
Gilles Peskine91e8c332019-08-02 19:19:39 +02001502 /* Reject invalid flags. These should not be reachable through the API. */
1503 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1504 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1505 return( PSA_ERROR_INVALID_ARGUMENT );
1506
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507 return( PSA_SUCCESS );
1508}
1509
Gilles Peskine4747d192019-04-17 15:05:45 +02001510/** Prepare a key slot to receive key material.
1511 *
1512 * This function allocates a key slot and sets its metadata.
1513 *
1514 * If this function fails, call psa_fail_key_creation().
1515 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001516 * This function is intended to be used as follows:
1517 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1518 * it with the specified attributes, and assign it a handle.
1519 * -# Populate the slot with the key material.
1520 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1521 * In case of failure at any step, stop the sequence and call
1522 * psa_fail_key_creation().
1523 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001524 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001525 * \param[in] attributes Key attributes for the new key.
1526 * \param[out] handle On success, a handle for the allocated slot.
1527 * \param[out] p_slot On success, a pointer to the prepared slot.
1528 * \param[out] p_drv On any return, the driver for the key, if any.
1529 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001530 *
1531 * \retval #PSA_SUCCESS
1532 * The key slot is ready to receive key material.
1533 * \return If this function fails, the key slot is an invalid state.
1534 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 */
1536static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001537 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001538 const psa_key_attributes_t *attributes,
1539 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001540 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001541 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001542{
1543 psa_status_t status;
1544 psa_key_slot_t *slot;
1545
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 *p_drv = NULL;
1548
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001549 status = psa_validate_key_attributes( attributes, p_drv );
1550 if( status != PSA_SUCCESS )
1551 return( status );
1552
Gilles Peskineedbed562019-08-07 18:19:59 +02001553 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 if( status != PSA_SUCCESS )
1555 return( status );
1556 slot = *p_slot;
1557
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001558 /* We're storing the declared bit-size of the key. It's up to each
1559 * creation mechanism to verify that this information is correct.
1560 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001561 * an input (generate, device) but not for those where the bit-size
1562 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001563
1564 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001565
Gilles Peskine91e8c332019-08-02 19:19:39 +02001566 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001567 * external-only flags, query `attributes`. Thanks to the check
1568 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001569 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001570 * may have set. */
1571 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001572
Gilles Peskinecbaff462019-07-12 23:46:04 +02001573#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001574 /* For a key in a secure element, we need to do three things
1575 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001576 * create the key file in internal storage, create the
1577 * key inside the secure element, and update the driver's
1578 * persistent data. Start a transaction that will encompass these
1579 * three actions. */
1580 /* The first thing to do is to find a slot number for the new key.
1581 * We save the slot number in persistent storage as part of the
1582 * transaction data. It will be needed to recover if the power
1583 * fails during the key creation process, to clean up on the secure
1584 * element side after restarting. Obtaining a slot number from the
1585 * secure element driver updates its persistent state, but we do not yet
1586 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001587 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001588 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001589 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001590 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001591 &slot->data.se.slot_number );
1592 if( status != PSA_SUCCESS )
1593 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001594 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001595 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001596 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001597 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001598 status = psa_crypto_save_transaction( );
1599 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001600 {
1601 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001602 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001603 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001604 }
1605#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1606
Gilles Peskine4747d192019-04-17 15:05:45 +02001607 return( status );
1608}
1609
1610/** Finalize the creation of a key once its key material has been set.
1611 *
1612 * This entails writing the key to persistent storage.
1613 *
1614 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001615 * See the documentation of psa_start_key_creation() for the intended use
1616 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001618 * \param[in,out] slot Pointer to the slot with key material.
1619 * \param[in] driver The secure element driver for the key,
1620 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001621 *
1622 * \retval #PSA_SUCCESS
1623 * The key was successfully created. The handle is now valid.
1624 * \return If this function fails, the key slot is an invalid state.
1625 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001626 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001627static psa_status_t psa_finish_key_creation(
1628 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001629 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001630{
1631 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001632 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001633 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001634
1635#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001636 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001638#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1639 if( driver != NULL )
1640 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001641 psa_se_key_data_storage_t data;
1642#if defined(static_assert)
1643 static_assert( sizeof( slot->data.se.slot_number ) ==
1644 sizeof( data.slot_number ),
1645 "Slot number size does not match psa_se_key_data_storage_t" );
1646 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1647 "Bit-size size does not match psa_se_key_data_storage_t" );
1648#endif
1649 memcpy( &data.slot_number, &slot->data.se.slot_number,
1650 sizeof( slot->data.se.slot_number ) );
1651 memcpy( &data.bits, &slot->attr.bits,
1652 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001653 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001654 (uint8_t*) &data,
1655 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001656 }
1657 else
1658#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1659 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001660 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001661 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001662 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001663 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1664 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001665 if( buffer == NULL && buffer_size != 0 )
1666 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1667 status = psa_internal_export_key( slot,
1668 buffer, buffer_size, &length,
1669 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001670 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001671 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001672 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001673
Gilles Peskine1df83d42019-07-23 16:13:14 +02001674 if( buffer_size != 0 )
1675 mbedtls_platform_zeroize( buffer, buffer_size );
1676 mbedtls_free( buffer );
1677 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 }
1679#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1680
Gilles Peskinecbaff462019-07-12 23:46:04 +02001681#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001682 /* Finish the transaction for a key creation. This does not
1683 * happen when registering an existing key. Detect this case
1684 * by checking whether a transaction is in progress (actual
1685 * creation of a key in a secure element requires a transaction,
1686 * but registration doesn't use one). */
1687 if( driver != NULL &&
1688 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001689 {
1690 status = psa_save_se_persistent_data( driver );
1691 if( status != PSA_SUCCESS )
1692 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001693 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001694 return( status );
1695 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001696 status = psa_crypto_stop_transaction( );
1697 if( status != PSA_SUCCESS )
1698 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001699 }
1700#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1701
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 return( status );
1703}
1704
1705/** Abort the creation of a key.
1706 *
1707 * You may call this function after calling psa_start_key_creation(),
1708 * or after psa_finish_key_creation() fails. In other circumstances, this
1709 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001710 * See the documentation of psa_start_key_creation() for the intended use
1711 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001712 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001713 * \param[in,out] slot Pointer to the slot with key material.
1714 * \param[in] driver The secure element driver for the key,
1715 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001717static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001718 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001719{
Gilles Peskine011e4282019-06-26 18:34:38 +02001720 (void) driver;
1721
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 if( slot == NULL )
1723 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001724
1725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1726 /* TOnogrepDO: If the key has already been created in the secure
1727 * element, and the failure happened later (when saving metadata
1728 * to internal storage), we need to destroy the key in the secure
1729 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001730
Gilles Peskined7729582019-08-05 15:55:54 +02001731 /* Abort the ongoing transaction if any (there may not be one if
1732 * the creation process failed before starting one, or if the
1733 * key creation is a registration of a key in a secure element).
1734 * Earlier functions must already have done what it takes to undo any
1735 * partial creation. All that's left is to update the transaction data
1736 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001737 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001738#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1739
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 psa_wipe_key_slot( slot );
1741}
1742
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001743/** Validate optional attributes during key creation.
1744 *
1745 * Some key attributes are optional during key creation. If they are
1746 * specified in the attributes structure, check that they are consistent
1747 * with the data in the slot.
1748 *
1749 * This function should be called near the end of key creation, after
1750 * the slot in memory is fully populated but before saving persistent data.
1751 */
1752static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001753 const psa_key_slot_t *slot,
1754 const psa_key_attributes_t *attributes )
1755{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001756 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001757 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001758 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001759 return( PSA_ERROR_INVALID_ARGUMENT );
1760 }
1761
1762 if( attributes->domain_parameters_size != 0 )
1763 {
1764#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001765 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001766 {
1767 mbedtls_mpi actual, required;
1768 int ret;
1769 mbedtls_mpi_init( &actual );
1770 mbedtls_mpi_init( &required );
1771 ret = mbedtls_rsa_export( slot->data.rsa,
1772 NULL, NULL, NULL, NULL, &actual );
1773 if( ret != 0 )
1774 goto rsa_exit;
1775 ret = mbedtls_mpi_read_binary( &required,
1776 attributes->domain_parameters,
1777 attributes->domain_parameters_size );
1778 if( ret != 0 )
1779 goto rsa_exit;
1780 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1781 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1782 rsa_exit:
1783 mbedtls_mpi_free( &actual );
1784 mbedtls_mpi_free( &required );
1785 if( ret != 0)
1786 return( mbedtls_to_psa_error( ret ) );
1787 }
1788 else
1789#endif
1790 {
1791 return( PSA_ERROR_INVALID_ARGUMENT );
1792 }
1793 }
1794
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001795 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001796 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001798 return( PSA_ERROR_INVALID_ARGUMENT );
1799 }
1800
1801 return( PSA_SUCCESS );
1802}
1803
Gilles Peskine4747d192019-04-17 15:05:45 +02001804psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001806 size_t data_length,
1807 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001808{
1809 psa_status_t status;
1810 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001811 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812
Gilles Peskinedf179142019-07-15 22:02:14 +02001813 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1814 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 if( status != PSA_SUCCESS )
1816 goto exit;
1817
Gilles Peskine5d309672019-07-12 23:47:28 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1819 if( driver != NULL )
1820 {
1821 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001822 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001823 if( drv->key_management == NULL ||
1824 drv->key_management->p_import == NULL )
1825 {
1826 status = PSA_ERROR_NOT_SUPPORTED;
1827 goto exit;
1828 }
1829 status = drv->key_management->p_import(
1830 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001831 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001832 &bits );
1833 if( status != PSA_SUCCESS )
1834 goto exit;
1835 if( bits > PSA_MAX_KEY_BITS )
1836 {
1837 status = PSA_ERROR_NOT_SUPPORTED;
1838 goto exit;
1839 }
1840 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001841 }
1842 else
1843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1844 {
1845 status = psa_import_key_into_slot( slot, data, data_length );
1846 if( status != PSA_SUCCESS )
1847 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001848 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001849 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001850 if( status != PSA_SUCCESS )
1851 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001852
Gilles Peskine011e4282019-06-26 18:34:38 +02001853 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 if( status != PSA_SUCCESS )
1856 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001858 *handle = 0;
1859 }
1860 return( status );
1861}
1862
Gilles Peskined7729582019-08-05 15:55:54 +02001863#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1864psa_status_t mbedtls_psa_register_se_key(
1865 const psa_key_attributes_t *attributes )
1866{
1867 psa_status_t status;
1868 psa_key_slot_t *slot = NULL;
1869 psa_se_drv_table_entry_t *driver = NULL;
1870 const psa_drv_se_t *drv;
1871 psa_key_handle_t handle = 0;
1872
1873 /* Leaving attributes unspecified is not currently supported.
1874 * It could make sense to query the key type and size from the
1875 * secure element, but not all secure elements support this
1876 * and the driver HAL doesn't currently support it. */
1877 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1878 return( PSA_ERROR_NOT_SUPPORTED );
1879 if( psa_get_key_bits( attributes ) == 0 )
1880 return( PSA_ERROR_NOT_SUPPORTED );
1881
1882 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1883 &handle, &slot, &driver );
1884 if( status != PSA_SUCCESS )
1885 goto exit;
1886
1887 if( driver == NULL )
1888 {
1889 status = PSA_ERROR_INVALID_ARGUMENT;
1890 goto exit;
1891 }
1892 drv = psa_get_se_driver_methods( driver );
1893
1894 if ( psa_get_key_slot_number( attributes,
1895 &slot->data.se.slot_number ) != PSA_SUCCESS )
1896 {
1897 /* The application didn't specify a slot number. This doesn't
1898 * make sense when registering a slot. */
1899 status = PSA_ERROR_INVALID_ARGUMENT;
1900 goto exit;
1901 }
1902
Gilles Peskinea5f87492019-08-05 16:46:18 +02001903 /* If the driver has a slot number validation method, call it.
1904 * If it doesn't, it means the secure element is unable to validate
1905 * anything and so we have to trust the application. */
1906 if( drv->key_management != NULL &&
1907 drv->key_management->p_validate_slot_number != NULL )
1908 {
1909 status = drv->key_management->p_validate_slot_number(
1910 psa_get_se_driver_context( driver ),
1911 attributes,
1912 PSA_KEY_CREATION_REGISTER,
1913 slot->data.se.slot_number );
1914 if( status != PSA_SUCCESS )
1915 goto exit;
1916 }
1917
Gilles Peskined7729582019-08-05 15:55:54 +02001918 status = psa_finish_key_creation( slot, driver );
1919
1920exit:
1921 if( status != PSA_SUCCESS )
1922 {
1923 psa_fail_key_creation( slot, driver );
1924 }
1925 /* Registration doesn't keep the key in RAM. */
1926 psa_close_key( handle );
1927 return( status );
1928}
1929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1930
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001931static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001932 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001933{
1934 psa_status_t status;
1935 uint8_t *buffer = NULL;
1936 size_t buffer_size = 0;
1937 size_t length;
1938
Gilles Peskine8e338702019-07-30 20:06:31 +02001939 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001940 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001941 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001942 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001943 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001944 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1945 if( status != PSA_SUCCESS )
1946 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001947 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001948 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001949
1950exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001951 if( buffer_size != 0 )
1952 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001953 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001954 return( status );
1955}
1956
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001957psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1958 const psa_key_attributes_t *specified_attributes,
1959 psa_key_handle_t *target_handle )
1960{
1961 psa_status_t status;
1962 psa_key_slot_t *source_slot = NULL;
1963 psa_key_slot_t *target_slot = NULL;
1964 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001965 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001966
Gilles Peskine28f8f302019-07-24 13:30:31 +02001967 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001968 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969 if( status != PSA_SUCCESS )
1970 goto exit;
1971
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001972 status = psa_validate_optional_attributes( source_slot,
1973 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974 if( status != PSA_SUCCESS )
1975 goto exit;
1976
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001977 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001978 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 if( status != PSA_SUCCESS )
1980 goto exit;
1981
Gilles Peskinedf179142019-07-15 22:02:14 +02001982 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1983 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001984 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 if( status != PSA_SUCCESS )
1986 goto exit;
1987
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1989 if( driver != NULL )
1990 {
1991 /* Copying to a secure element is not implemented yet. */
1992 status = PSA_ERROR_NOT_SUPPORTED;
1993 goto exit;
1994 }
1995#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1996
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001998 if( status != PSA_SUCCESS )
1999 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000
Gilles Peskine011e4282019-06-26 18:34:38 +02002001 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 if( status != PSA_SUCCESS )
2004 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002005 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006 *target_handle = 0;
2007 }
2008 return( status );
2009}
2010
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002011
2012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002013/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002014/* Message digests */
2015/****************************************************************/
2016
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002017static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002018{
2019 switch( alg )
2020 {
2021#if defined(MBEDTLS_MD2_C)
2022 case PSA_ALG_MD2:
2023 return( &mbedtls_md2_info );
2024#endif
2025#if defined(MBEDTLS_MD4_C)
2026 case PSA_ALG_MD4:
2027 return( &mbedtls_md4_info );
2028#endif
2029#if defined(MBEDTLS_MD5_C)
2030 case PSA_ALG_MD5:
2031 return( &mbedtls_md5_info );
2032#endif
2033#if defined(MBEDTLS_RIPEMD160_C)
2034 case PSA_ALG_RIPEMD160:
2035 return( &mbedtls_ripemd160_info );
2036#endif
2037#if defined(MBEDTLS_SHA1_C)
2038 case PSA_ALG_SHA_1:
2039 return( &mbedtls_sha1_info );
2040#endif
2041#if defined(MBEDTLS_SHA256_C)
2042 case PSA_ALG_SHA_224:
2043 return( &mbedtls_sha224_info );
2044 case PSA_ALG_SHA_256:
2045 return( &mbedtls_sha256_info );
2046#endif
2047#if defined(MBEDTLS_SHA512_C)
2048 case PSA_ALG_SHA_384:
2049 return( &mbedtls_sha384_info );
2050 case PSA_ALG_SHA_512:
2051 return( &mbedtls_sha512_info );
2052#endif
2053 default:
2054 return( NULL );
2055 }
2056}
2057
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002058psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2059{
2060 switch( operation->alg )
2061 {
Gilles Peskine81736312018-06-26 15:04:31 +02002062 case 0:
2063 /* The object has (apparently) been initialized but it is not
2064 * in use. It's ok to call abort on such an object, and there's
2065 * nothing to do. */
2066 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067#if defined(MBEDTLS_MD2_C)
2068 case PSA_ALG_MD2:
2069 mbedtls_md2_free( &operation->ctx.md2 );
2070 break;
2071#endif
2072#if defined(MBEDTLS_MD4_C)
2073 case PSA_ALG_MD4:
2074 mbedtls_md4_free( &operation->ctx.md4 );
2075 break;
2076#endif
2077#if defined(MBEDTLS_MD5_C)
2078 case PSA_ALG_MD5:
2079 mbedtls_md5_free( &operation->ctx.md5 );
2080 break;
2081#endif
2082#if defined(MBEDTLS_RIPEMD160_C)
2083 case PSA_ALG_RIPEMD160:
2084 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_SHA1_C)
2088 case PSA_ALG_SHA_1:
2089 mbedtls_sha1_free( &operation->ctx.sha1 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_SHA256_C)
2093 case PSA_ALG_SHA_224:
2094 case PSA_ALG_SHA_256:
2095 mbedtls_sha256_free( &operation->ctx.sha256 );
2096 break;
2097#endif
2098#if defined(MBEDTLS_SHA512_C)
2099 case PSA_ALG_SHA_384:
2100 case PSA_ALG_SHA_512:
2101 mbedtls_sha512_free( &operation->ctx.sha512 );
2102 break;
2103#endif
2104 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002105 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002106 }
2107 operation->alg = 0;
2108 return( PSA_SUCCESS );
2109}
2110
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002111psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 psa_algorithm_t alg )
2113{
2114 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002115
2116 /* A context must be freshly initialized before it can be set up. */
2117 if( operation->alg != 0 )
2118 {
2119 return( PSA_ERROR_BAD_STATE );
2120 }
2121
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 switch( alg )
2123 {
2124#if defined(MBEDTLS_MD2_C)
2125 case PSA_ALG_MD2:
2126 mbedtls_md2_init( &operation->ctx.md2 );
2127 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2128 break;
2129#endif
2130#if defined(MBEDTLS_MD4_C)
2131 case PSA_ALG_MD4:
2132 mbedtls_md4_init( &operation->ctx.md4 );
2133 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2134 break;
2135#endif
2136#if defined(MBEDTLS_MD5_C)
2137 case PSA_ALG_MD5:
2138 mbedtls_md5_init( &operation->ctx.md5 );
2139 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2140 break;
2141#endif
2142#if defined(MBEDTLS_RIPEMD160_C)
2143 case PSA_ALG_RIPEMD160:
2144 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2145 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_SHA1_C)
2149 case PSA_ALG_SHA_1:
2150 mbedtls_sha1_init( &operation->ctx.sha1 );
2151 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_SHA256_C)
2155 case PSA_ALG_SHA_224:
2156 mbedtls_sha256_init( &operation->ctx.sha256 );
2157 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2158 break;
2159 case PSA_ALG_SHA_256:
2160 mbedtls_sha256_init( &operation->ctx.sha256 );
2161 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_SHA512_C)
2165 case PSA_ALG_SHA_384:
2166 mbedtls_sha512_init( &operation->ctx.sha512 );
2167 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2168 break;
2169 case PSA_ALG_SHA_512:
2170 mbedtls_sha512_init( &operation->ctx.sha512 );
2171 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2172 break;
2173#endif
2174 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002175 return( PSA_ALG_IS_HASH( alg ) ?
2176 PSA_ERROR_NOT_SUPPORTED :
2177 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178 }
2179 if( ret == 0 )
2180 operation->alg = alg;
2181 else
2182 psa_hash_abort( operation );
2183 return( mbedtls_to_psa_error( ret ) );
2184}
2185
2186psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2187 const uint8_t *input,
2188 size_t input_length )
2189{
2190 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002191
2192 /* Don't require hash implementations to behave correctly on a
2193 * zero-length input, which may have an invalid pointer. */
2194 if( input_length == 0 )
2195 return( PSA_SUCCESS );
2196
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 switch( operation->alg )
2198 {
2199#if defined(MBEDTLS_MD2_C)
2200 case PSA_ALG_MD2:
2201 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2202 input, input_length );
2203 break;
2204#endif
2205#if defined(MBEDTLS_MD4_C)
2206 case PSA_ALG_MD4:
2207 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2208 input, input_length );
2209 break;
2210#endif
2211#if defined(MBEDTLS_MD5_C)
2212 case PSA_ALG_MD5:
2213 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2214 input, input_length );
2215 break;
2216#endif
2217#if defined(MBEDTLS_RIPEMD160_C)
2218 case PSA_ALG_RIPEMD160:
2219 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2220 input, input_length );
2221 break;
2222#endif
2223#if defined(MBEDTLS_SHA1_C)
2224 case PSA_ALG_SHA_1:
2225 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2226 input, input_length );
2227 break;
2228#endif
2229#if defined(MBEDTLS_SHA256_C)
2230 case PSA_ALG_SHA_224:
2231 case PSA_ALG_SHA_256:
2232 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2233 input, input_length );
2234 break;
2235#endif
2236#if defined(MBEDTLS_SHA512_C)
2237 case PSA_ALG_SHA_384:
2238 case PSA_ALG_SHA_512:
2239 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2240 input, input_length );
2241 break;
2242#endif
2243 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002244 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002246
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 if( ret != 0 )
2248 psa_hash_abort( operation );
2249 return( mbedtls_to_psa_error( ret ) );
2250}
2251
2252psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2253 uint8_t *hash,
2254 size_t hash_size,
2255 size_t *hash_length )
2256{
itayzafrir40835d42018-08-02 13:14:17 +03002257 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002259 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260
2261 /* Fill the output buffer with something that isn't a valid hash
2262 * (barring an attack on the hash and deliberately-crafted input),
2263 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002264 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002265 /* If hash_size is 0 then hash may be NULL and then the
2266 * call to memset would have undefined behavior. */
2267 if( hash_size != 0 )
2268 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269
2270 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002271 {
2272 status = PSA_ERROR_BUFFER_TOO_SMALL;
2273 goto exit;
2274 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275
2276 switch( operation->alg )
2277 {
2278#if defined(MBEDTLS_MD2_C)
2279 case PSA_ALG_MD2:
2280 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2281 break;
2282#endif
2283#if defined(MBEDTLS_MD4_C)
2284 case PSA_ALG_MD4:
2285 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2286 break;
2287#endif
2288#if defined(MBEDTLS_MD5_C)
2289 case PSA_ALG_MD5:
2290 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2291 break;
2292#endif
2293#if defined(MBEDTLS_RIPEMD160_C)
2294 case PSA_ALG_RIPEMD160:
2295 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_SHA1_C)
2299 case PSA_ALG_SHA_1:
2300 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_SHA256_C)
2304 case PSA_ALG_SHA_224:
2305 case PSA_ALG_SHA_256:
2306 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2307 break;
2308#endif
2309#if defined(MBEDTLS_SHA512_C)
2310 case PSA_ALG_SHA_384:
2311 case PSA_ALG_SHA_512:
2312 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2313 break;
2314#endif
2315 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002316 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317 }
itayzafrir40835d42018-08-02 13:14:17 +03002318 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319
itayzafrir40835d42018-08-02 13:14:17 +03002320exit:
2321 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002323 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 return( psa_hash_abort( operation ) );
2325 }
2326 else
2327 {
2328 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002329 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 }
2331}
2332
Gilles Peskine2d277862018-06-18 15:41:12 +02002333psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2334 const uint8_t *hash,
2335 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336{
2337 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2338 size_t actual_hash_length;
2339 psa_status_t status = psa_hash_finish( operation,
2340 actual_hash, sizeof( actual_hash ),
2341 &actual_hash_length );
2342 if( status != PSA_SUCCESS )
2343 return( status );
2344 if( actual_hash_length != hash_length )
2345 return( PSA_ERROR_INVALID_SIGNATURE );
2346 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2347 return( PSA_ERROR_INVALID_SIGNATURE );
2348 return( PSA_SUCCESS );
2349}
2350
Gilles Peskineeb35d782019-01-22 17:56:16 +01002351psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2352 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002353{
2354 if( target_operation->alg != 0 )
2355 return( PSA_ERROR_BAD_STATE );
2356
2357 switch( source_operation->alg )
2358 {
2359 case 0:
2360 return( PSA_ERROR_BAD_STATE );
2361#if defined(MBEDTLS_MD2_C)
2362 case PSA_ALG_MD2:
2363 mbedtls_md2_clone( &target_operation->ctx.md2,
2364 &source_operation->ctx.md2 );
2365 break;
2366#endif
2367#if defined(MBEDTLS_MD4_C)
2368 case PSA_ALG_MD4:
2369 mbedtls_md4_clone( &target_operation->ctx.md4,
2370 &source_operation->ctx.md4 );
2371 break;
2372#endif
2373#if defined(MBEDTLS_MD5_C)
2374 case PSA_ALG_MD5:
2375 mbedtls_md5_clone( &target_operation->ctx.md5,
2376 &source_operation->ctx.md5 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_RIPEMD160_C)
2380 case PSA_ALG_RIPEMD160:
2381 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2382 &source_operation->ctx.ripemd160 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_SHA1_C)
2386 case PSA_ALG_SHA_1:
2387 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2388 &source_operation->ctx.sha1 );
2389 break;
2390#endif
2391#if defined(MBEDTLS_SHA256_C)
2392 case PSA_ALG_SHA_224:
2393 case PSA_ALG_SHA_256:
2394 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2395 &source_operation->ctx.sha256 );
2396 break;
2397#endif
2398#if defined(MBEDTLS_SHA512_C)
2399 case PSA_ALG_SHA_384:
2400 case PSA_ALG_SHA_512:
2401 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2402 &source_operation->ctx.sha512 );
2403 break;
2404#endif
2405 default:
2406 return( PSA_ERROR_NOT_SUPPORTED );
2407 }
2408
2409 target_operation->alg = source_operation->alg;
2410 return( PSA_SUCCESS );
2411}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002412
2413
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415/* MAC */
2416/****************************************************************/
2417
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002418static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419 psa_algorithm_t alg,
2420 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002421 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002422 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002425 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002427 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002428 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002429
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2431 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002432 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002434 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002435 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436 mode = MBEDTLS_MODE_STREAM;
2437 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438 case PSA_ALG_CTR:
2439 mode = MBEDTLS_MODE_CTR;
2440 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002441 case PSA_ALG_CFB:
2442 mode = MBEDTLS_MODE_CFB;
2443 break;
2444 case PSA_ALG_OFB:
2445 mode = MBEDTLS_MODE_OFB;
2446 break;
2447 case PSA_ALG_CBC_NO_PADDING:
2448 mode = MBEDTLS_MODE_CBC;
2449 break;
2450 case PSA_ALG_CBC_PKCS7:
2451 mode = MBEDTLS_MODE_CBC;
2452 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002453 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 mode = MBEDTLS_MODE_CCM;
2455 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002456 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457 mode = MBEDTLS_MODE_GCM;
2458 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002459 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2460 mode = MBEDTLS_MODE_CHACHAPOLY;
2461 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462 default:
2463 return( NULL );
2464 }
2465 }
2466 else if( alg == PSA_ALG_CMAC )
2467 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468 else
2469 return( NULL );
2470
2471 switch( key_type )
2472 {
2473 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002474 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475 break;
2476 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002477 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2478 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002480 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002482 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002483 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2484 * but two-key Triple-DES is functionally three-key Triple-DES
2485 * with K1=K3, so that's how we present it to mbedtls. */
2486 if( key_bits == 128 )
2487 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 break;
2489 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002490 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 break;
2492 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002493 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002495 case PSA_KEY_TYPE_CHACHA20:
2496 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2497 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498 default:
2499 return( NULL );
2500 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002501 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002502 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503
Jaeden Amero23bbb752018-06-26 14:16:54 +01002504 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2505 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506}
2507
Gilles Peskinea05219c2018-11-16 16:02:56 +01002508#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002509static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002510{
Gilles Peskine2d277862018-06-18 15:41:12 +02002511 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002512 {
2513 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002514 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002515 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002516 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002517 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002518 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002519 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002520 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002521 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002522 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002523 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002524 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002525 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002526 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002527 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002528 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002529 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002530 return( 128 );
2531 default:
2532 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002533 }
2534}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002535#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002536
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002537/* Initialize the MAC operation structure. Once this function has been
2538 * called, psa_mac_abort can run and will do the right thing. */
2539static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2540 psa_algorithm_t alg )
2541{
2542 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2543
2544 operation->alg = alg;
2545 operation->key_set = 0;
2546 operation->iv_set = 0;
2547 operation->iv_required = 0;
2548 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002549 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002550
2551#if defined(MBEDTLS_CMAC_C)
2552 if( alg == PSA_ALG_CMAC )
2553 {
2554 operation->iv_required = 0;
2555 mbedtls_cipher_init( &operation->ctx.cmac );
2556 status = PSA_SUCCESS;
2557 }
2558 else
2559#endif /* MBEDTLS_CMAC_C */
2560#if defined(MBEDTLS_MD_C)
2561 if( PSA_ALG_IS_HMAC( operation->alg ) )
2562 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002563 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2564 operation->ctx.hmac.hash_ctx.alg = 0;
2565 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566 }
2567 else
2568#endif /* MBEDTLS_MD_C */
2569 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002570 if( ! PSA_ALG_IS_MAC( alg ) )
2571 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002572 }
2573
2574 if( status != PSA_SUCCESS )
2575 memset( operation, 0, sizeof( *operation ) );
2576 return( status );
2577}
2578
Gilles Peskine01126fa2018-07-12 17:04:55 +02002579#if defined(MBEDTLS_MD_C)
2580static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2581{
Gilles Peskine3f108122018-12-07 18:14:53 +01002582 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002583 return( psa_hash_abort( &hmac->hash_ctx ) );
2584}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002585
Janos Follath999f6482019-06-11 12:04:10 +01002586#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002587static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2588{
2589 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2590 memset( hmac, 0, sizeof( *hmac ) );
2591}
Janos Follath999f6482019-06-11 12:04:10 +01002592#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002593#endif /* MBEDTLS_MD_C */
2594
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2596{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002597 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002599 /* The object has (apparently) been initialized but it is not
2600 * in use. It's ok to call abort on such an object, and there's
2601 * nothing to do. */
2602 return( PSA_SUCCESS );
2603 }
2604 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002606 if( operation->alg == PSA_ALG_CMAC )
2607 {
2608 mbedtls_cipher_free( &operation->ctx.cmac );
2609 }
2610 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 if( PSA_ALG_IS_HMAC( operation->alg ) )
2614 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002615 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002616 }
2617 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002619 {
2620 /* Sanity check (shouldn't happen: operation->alg should
2621 * always have been initialized to a valid value). */
2622 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623 }
Moran Peker41deec42018-04-04 15:43:05 +03002624
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625 operation->alg = 0;
2626 operation->key_set = 0;
2627 operation->iv_set = 0;
2628 operation->iv_required = 0;
2629 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002630 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002631
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002633
2634bad_state:
2635 /* If abort is called on an uninitialized object, we can't trust
2636 * anything. Wipe the object in case it contains confidential data.
2637 * This may result in a memory leak if a pointer gets overwritten,
2638 * but it's too late to do anything about this. */
2639 memset( operation, 0, sizeof( *operation ) );
2640 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641}
2642
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002643#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002644static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002645 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002646 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002647 const mbedtls_cipher_info_t *cipher_info )
2648{
2649 int ret;
2650
2651 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652
2653 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2654 if( ret != 0 )
2655 return( ret );
2656
2657 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2658 slot->data.raw.data,
2659 key_bits );
2660 return( ret );
2661}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002662#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002663
Gilles Peskine248051a2018-06-20 16:09:38 +02002664#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002665static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2666 const uint8_t *key,
2667 size_t key_length,
2668 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002669{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002670 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002671 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002672 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002673 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002674 psa_status_t status;
2675
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002676 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2677 * overflow below. This should never trigger if the hash algorithm
2678 * is implemented correctly. */
2679 /* The size checks against the ipad and opad buffers cannot be written
2680 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2681 * because that triggers -Wlogical-op on GCC 7.3. */
2682 if( block_size > sizeof( ipad ) )
2683 return( PSA_ERROR_NOT_SUPPORTED );
2684 if( block_size > sizeof( hmac->opad ) )
2685 return( PSA_ERROR_NOT_SUPPORTED );
2686 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002687 return( PSA_ERROR_NOT_SUPPORTED );
2688
Gilles Peskined223b522018-06-11 18:12:58 +02002689 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002690 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002691 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002692 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002693 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002694 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002695 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002696 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002697 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002698 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002699 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002700 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002701 }
Gilles Peskine96889972018-07-12 17:07:03 +02002702 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2703 * but it is permitted. It is common when HMAC is used in HKDF, for
2704 * example. Don't call `memcpy` in the 0-length because `key` could be
2705 * an invalid pointer which would make the behavior undefined. */
2706 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002707 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708
Gilles Peskined223b522018-06-11 18:12:58 +02002709 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2710 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002712 ipad[i] ^= 0x36;
2713 memset( ipad + key_length, 0x36, block_size - key_length );
2714
2715 /* Copy the key material from ipad to opad, flipping the requisite bits,
2716 * and filling the rest of opad with the requisite constant. */
2717 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002718 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2719 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002720
Gilles Peskine01126fa2018-07-12 17:04:55 +02002721 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002723 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002724
Gilles Peskine01126fa2018-07-12 17:04:55 +02002725 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002726
2727cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002728 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002729
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730 return( status );
2731}
Gilles Peskine248051a2018-06-20 16:09:38 +02002732#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002733
Gilles Peskine89167cb2018-07-08 20:12:23 +02002734static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002735 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002736 psa_algorithm_t alg,
2737 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002740 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002742 psa_key_usage_t usage =
2743 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002744 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002745 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002747 /* A context must be freshly initialized before it can be set up. */
2748 if( operation->alg != 0 )
2749 {
2750 return( PSA_ERROR_BAD_STATE );
2751 }
2752
Gilles Peskined911eb72018-08-14 15:18:45 +02002753 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002754 if( status != PSA_SUCCESS )
2755 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002756 if( is_sign )
2757 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758
Gilles Peskine28f8f302019-07-24 13:30:31 +02002759 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002760 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002761 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002762 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002763
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002765 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002766 {
2767 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002768 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002769 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002770 int ret;
2771 if( cipher_info == NULL )
2772 {
2773 status = PSA_ERROR_NOT_SUPPORTED;
2774 goto exit;
2775 }
2776 operation->mac_size = cipher_info->block_size;
2777 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2778 status = mbedtls_to_psa_error( ret );
2779 }
2780 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002783 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002784 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002785 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002786 if( hash_alg == 0 )
2787 {
2788 status = PSA_ERROR_NOT_SUPPORTED;
2789 goto exit;
2790 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002791
2792 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2793 /* Sanity check. This shouldn't fail on a valid configuration. */
2794 if( operation->mac_size == 0 ||
2795 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2796 {
2797 status = PSA_ERROR_NOT_SUPPORTED;
2798 goto exit;
2799 }
2800
Gilles Peskine8e338702019-07-30 20:06:31 +02002801 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002802 {
2803 status = PSA_ERROR_INVALID_ARGUMENT;
2804 goto exit;
2805 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002806
Gilles Peskine01126fa2018-07-12 17:04:55 +02002807 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2808 slot->data.raw.data,
2809 slot->data.raw.bytes,
2810 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002811 }
2812 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002814 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002815 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002816 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002818
Gilles Peskined911eb72018-08-14 15:18:45 +02002819 if( truncated == 0 )
2820 {
2821 /* The "normal" case: untruncated algorithm. Nothing to do. */
2822 }
2823 else if( truncated < 4 )
2824 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002825 /* A very short MAC is too short for security since it can be
2826 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2827 * so we make this our minimum, even though 32 bits is still
2828 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002829 status = PSA_ERROR_NOT_SUPPORTED;
2830 }
2831 else if( truncated > operation->mac_size )
2832 {
2833 /* It's impossible to "truncate" to a larger length. */
2834 status = PSA_ERROR_INVALID_ARGUMENT;
2835 }
2836 else
2837 operation->mac_size = truncated;
2838
Gilles Peskinefbfac682018-07-08 20:51:54 +02002839exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002840 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002842 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002844 else
2845 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002846 operation->key_set = 1;
2847 }
2848 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849}
2850
Gilles Peskine89167cb2018-07-08 20:12:23 +02002851psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002852 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002853 psa_algorithm_t alg )
2854{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002855 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002856}
2857
2858psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002859 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002860 psa_algorithm_t alg )
2861{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002862 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002863}
2864
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2866 const uint8_t *input,
2867 size_t input_length )
2868{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002869 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002871 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002873 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 operation->has_input = 1;
2875
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002877 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002878 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2880 input, input_length );
2881 status = mbedtls_to_psa_error( ret );
2882 }
2883 else
2884#endif /* MBEDTLS_CMAC_C */
2885#if defined(MBEDTLS_MD_C)
2886 if( PSA_ALG_IS_HMAC( operation->alg ) )
2887 {
2888 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2889 input_length );
2890 }
2891 else
2892#endif /* MBEDTLS_MD_C */
2893 {
2894 /* This shouldn't happen if `operation` was initialized by
2895 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002896 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002897 }
2898
Gilles Peskinefbfac682018-07-08 20:51:54 +02002899 if( status != PSA_SUCCESS )
2900 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002901 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902}
2903
Gilles Peskine01126fa2018-07-12 17:04:55 +02002904#if defined(MBEDTLS_MD_C)
2905static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2906 uint8_t *mac,
2907 size_t mac_size )
2908{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002909 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002910 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2911 size_t hash_size = 0;
2912 size_t block_size = psa_get_hash_block_size( hash_alg );
2913 psa_status_t status;
2914
Gilles Peskine01126fa2018-07-12 17:04:55 +02002915 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2916 if( status != PSA_SUCCESS )
2917 return( status );
2918 /* From here on, tmp needs to be wiped. */
2919
2920 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2921 if( status != PSA_SUCCESS )
2922 goto exit;
2923
2924 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2925 if( status != PSA_SUCCESS )
2926 goto exit;
2927
2928 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2929 if( status != PSA_SUCCESS )
2930 goto exit;
2931
Gilles Peskined911eb72018-08-14 15:18:45 +02002932 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2933 if( status != PSA_SUCCESS )
2934 goto exit;
2935
2936 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002937
2938exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002939 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002940 return( status );
2941}
2942#endif /* MBEDTLS_MD_C */
2943
mohammad16036df908f2018-04-02 08:34:15 -07002944static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002945 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002946 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002948 if( ! operation->key_set )
2949 return( PSA_ERROR_BAD_STATE );
2950 if( operation->iv_required && ! operation->iv_set )
2951 return( PSA_ERROR_BAD_STATE );
2952
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953 if( mac_size < operation->mac_size )
2954 return( PSA_ERROR_BUFFER_TOO_SMALL );
2955
Gilles Peskine8c9def32018-02-08 10:02:12 +01002956#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002957 if( operation->alg == PSA_ALG_CMAC )
2958 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002959 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2960 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2961 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002962 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002963 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002964 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002966 else
2967#endif /* MBEDTLS_CMAC_C */
2968#if defined(MBEDTLS_MD_C)
2969 if( PSA_ALG_IS_HMAC( operation->alg ) )
2970 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002972 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002973 }
2974 else
2975#endif /* MBEDTLS_MD_C */
2976 {
2977 /* This shouldn't happen if `operation` was initialized by
2978 * a setup function. */
2979 return( PSA_ERROR_BAD_STATE );
2980 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981}
2982
Gilles Peskineacd4be32018-07-08 19:56:25 +02002983psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2984 uint8_t *mac,
2985 size_t mac_size,
2986 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002987{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002988 psa_status_t status;
2989
Jaeden Amero252ef282019-02-15 14:05:35 +00002990 if( operation->alg == 0 )
2991 {
2992 return( PSA_ERROR_BAD_STATE );
2993 }
2994
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002995 /* Fill the output buffer with something that isn't a valid mac
2996 * (barring an attack on the mac and deliberately-crafted input),
2997 * in case the caller doesn't check the return status properly. */
2998 *mac_length = mac_size;
2999 /* If mac_size is 0 then mac may be NULL and then the
3000 * call to memset would have undefined behavior. */
3001 if( mac_size != 0 )
3002 memset( mac, '!', mac_size );
3003
Gilles Peskine89167cb2018-07-08 20:12:23 +02003004 if( ! operation->is_sign )
3005 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003006 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003007 }
mohammad16036df908f2018-04-02 08:34:15 -07003008
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003009 status = psa_mac_finish_internal( operation, mac, mac_size );
3010
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003011 if( status == PSA_SUCCESS )
3012 {
3013 status = psa_mac_abort( operation );
3014 if( status == PSA_SUCCESS )
3015 *mac_length = operation->mac_size;
3016 else
3017 memset( mac, '!', mac_size );
3018 }
3019 else
3020 psa_mac_abort( operation );
3021 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003022}
3023
Gilles Peskineacd4be32018-07-08 19:56:25 +02003024psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3025 const uint8_t *mac,
3026 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027{
Gilles Peskine828ed142018-06-18 23:25:51 +02003028 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003029 psa_status_t status;
3030
Jaeden Amero252ef282019-02-15 14:05:35 +00003031 if( operation->alg == 0 )
3032 {
3033 return( PSA_ERROR_BAD_STATE );
3034 }
3035
Gilles Peskine89167cb2018-07-08 20:12:23 +02003036 if( operation->is_sign )
3037 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003038 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003039 }
3040 if( operation->mac_size != mac_length )
3041 {
3042 status = PSA_ERROR_INVALID_SIGNATURE;
3043 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003044 }
mohammad16036df908f2018-04-02 08:34:15 -07003045
3046 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003047 actual_mac, sizeof( actual_mac ) );
3048
3049 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3050 status = PSA_ERROR_INVALID_SIGNATURE;
3051
3052cleanup:
3053 if( status == PSA_SUCCESS )
3054 status = psa_mac_abort( operation );
3055 else
3056 psa_mac_abort( operation );
3057
Gilles Peskine3f108122018-12-07 18:14:53 +01003058 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003059
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003060 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003061}
3062
3063
Gilles Peskine20035e32018-02-03 22:44:14 +01003064
Gilles Peskine20035e32018-02-03 22:44:14 +01003065/****************************************************************/
3066/* Asymmetric cryptography */
3067/****************************************************************/
3068
Gilles Peskine2b450e32018-06-27 15:42:46 +02003069#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003070/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003071 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003072static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3073 size_t hash_length,
3074 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003075{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003076 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003077 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003078 *md_alg = mbedtls_md_get_type( md_info );
3079
3080 /* The Mbed TLS RSA module uses an unsigned int for hash length
3081 * parameters. Validate that it fits so that we don't risk an
3082 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003083#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003084 if( hash_length > UINT_MAX )
3085 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003086#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003087
3088#if defined(MBEDTLS_PKCS1_V15)
3089 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3090 * must be correct. */
3091 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3092 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003093 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003094 if( md_info == NULL )
3095 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003096 if( mbedtls_md_get_size( md_info ) != hash_length )
3097 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003098 }
3099#endif /* MBEDTLS_PKCS1_V15 */
3100
3101#if defined(MBEDTLS_PKCS1_V21)
3102 /* PSS requires a hash internally. */
3103 if( PSA_ALG_IS_RSA_PSS( alg ) )
3104 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003105 if( md_info == NULL )
3106 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003107 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003108#endif /* MBEDTLS_PKCS1_V21 */
3109
Gilles Peskine61b91d42018-06-08 16:09:36 +02003110 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003111}
3112
Gilles Peskine2b450e32018-06-27 15:42:46 +02003113static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3114 psa_algorithm_t alg,
3115 const uint8_t *hash,
3116 size_t hash_length,
3117 uint8_t *signature,
3118 size_t signature_size,
3119 size_t *signature_length )
3120{
3121 psa_status_t status;
3122 int ret;
3123 mbedtls_md_type_t md_alg;
3124
3125 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3126 if( status != PSA_SUCCESS )
3127 return( status );
3128
Gilles Peskine630a18a2018-06-29 17:49:35 +02003129 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003130 return( PSA_ERROR_BUFFER_TOO_SMALL );
3131
3132#if defined(MBEDTLS_PKCS1_V15)
3133 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3134 {
3135 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3136 MBEDTLS_MD_NONE );
3137 ret = mbedtls_rsa_pkcs1_sign( rsa,
3138 mbedtls_ctr_drbg_random,
3139 &global_data.ctr_drbg,
3140 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003141 md_alg,
3142 (unsigned int) hash_length,
3143 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003144 signature );
3145 }
3146 else
3147#endif /* MBEDTLS_PKCS1_V15 */
3148#if defined(MBEDTLS_PKCS1_V21)
3149 if( PSA_ALG_IS_RSA_PSS( alg ) )
3150 {
3151 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3152 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3153 mbedtls_ctr_drbg_random,
3154 &global_data.ctr_drbg,
3155 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003156 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003157 (unsigned int) hash_length,
3158 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003159 signature );
3160 }
3161 else
3162#endif /* MBEDTLS_PKCS1_V21 */
3163 {
3164 return( PSA_ERROR_INVALID_ARGUMENT );
3165 }
3166
3167 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003168 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003169 return( mbedtls_to_psa_error( ret ) );
3170}
3171
3172static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3173 psa_algorithm_t alg,
3174 const uint8_t *hash,
3175 size_t hash_length,
3176 const uint8_t *signature,
3177 size_t signature_length )
3178{
3179 psa_status_t status;
3180 int ret;
3181 mbedtls_md_type_t md_alg;
3182
3183 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3184 if( status != PSA_SUCCESS )
3185 return( status );
3186
Gilles Peskine630a18a2018-06-29 17:49:35 +02003187 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003188 return( PSA_ERROR_BUFFER_TOO_SMALL );
3189
3190#if defined(MBEDTLS_PKCS1_V15)
3191 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3192 {
3193 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3194 MBEDTLS_MD_NONE );
3195 ret = mbedtls_rsa_pkcs1_verify( rsa,
3196 mbedtls_ctr_drbg_random,
3197 &global_data.ctr_drbg,
3198 MBEDTLS_RSA_PUBLIC,
3199 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003200 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003201 hash,
3202 signature );
3203 }
3204 else
3205#endif /* MBEDTLS_PKCS1_V15 */
3206#if defined(MBEDTLS_PKCS1_V21)
3207 if( PSA_ALG_IS_RSA_PSS( alg ) )
3208 {
3209 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3210 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3211 mbedtls_ctr_drbg_random,
3212 &global_data.ctr_drbg,
3213 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003214 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003215 (unsigned int) hash_length,
3216 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003217 signature );
3218 }
3219 else
3220#endif /* MBEDTLS_PKCS1_V21 */
3221 {
3222 return( PSA_ERROR_INVALID_ARGUMENT );
3223 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003224
3225 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3226 * the rest of the signature is invalid". This has little use in
3227 * practice and PSA doesn't report this distinction. */
3228 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3229 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003230 return( mbedtls_to_psa_error( ret ) );
3231}
3232#endif /* MBEDTLS_RSA_C */
3233
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003234#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003235/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3236 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3237 * (even though these functions don't modify it). */
3238static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3239 psa_algorithm_t alg,
3240 const uint8_t *hash,
3241 size_t hash_length,
3242 uint8_t *signature,
3243 size_t signature_size,
3244 size_t *signature_length )
3245{
3246 int ret;
3247 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003248 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003249 mbedtls_mpi_init( &r );
3250 mbedtls_mpi_init( &s );
3251
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003252 if( signature_size < 2 * curve_bytes )
3253 {
3254 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3255 goto cleanup;
3256 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003257
Gilles Peskinea05219c2018-11-16 16:02:56 +01003258#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003259 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3260 {
3261 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3262 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3263 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3264 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3265 hash, hash_length,
3266 md_alg ) );
3267 }
3268 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003269#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003270 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003271 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003272 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3273 hash, hash_length,
3274 mbedtls_ctr_drbg_random,
3275 &global_data.ctr_drbg ) );
3276 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003277
3278 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3279 signature,
3280 curve_bytes ) );
3281 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3282 signature + curve_bytes,
3283 curve_bytes ) );
3284
3285cleanup:
3286 mbedtls_mpi_free( &r );
3287 mbedtls_mpi_free( &s );
3288 if( ret == 0 )
3289 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003290 return( mbedtls_to_psa_error( ret ) );
3291}
3292
3293static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3294 const uint8_t *hash,
3295 size_t hash_length,
3296 const uint8_t *signature,
3297 size_t signature_length )
3298{
3299 int ret;
3300 mbedtls_mpi r, s;
3301 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3302 mbedtls_mpi_init( &r );
3303 mbedtls_mpi_init( &s );
3304
3305 if( signature_length != 2 * curve_bytes )
3306 return( PSA_ERROR_INVALID_SIGNATURE );
3307
3308 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3309 signature,
3310 curve_bytes ) );
3311 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3312 signature + curve_bytes,
3313 curve_bytes ) );
3314
3315 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3316 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003317
3318cleanup:
3319 mbedtls_mpi_free( &r );
3320 mbedtls_mpi_free( &s );
3321 return( mbedtls_to_psa_error( ret ) );
3322}
3323#endif /* MBEDTLS_ECDSA_C */
3324
Gilles Peskinec5487a82018-12-03 18:08:14 +01003325psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003326 psa_algorithm_t alg,
3327 const uint8_t *hash,
3328 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003329 uint8_t *signature,
3330 size_t signature_size,
3331 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003332{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003333 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003334 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003335#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3336 const psa_drv_se_t *drv;
3337 psa_drv_se_context_t *drv_context;
3338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003339
3340 *signature_length = signature_size;
3341
Gilles Peskineedc64242019-08-07 21:05:07 +02003342 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003343 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003344 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003345 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003346 {
3347 status = PSA_ERROR_INVALID_ARGUMENT;
3348 goto exit;
3349 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003350
Gilles Peskineedc64242019-08-07 21:05:07 +02003351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3352 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3353 {
3354 if( drv->asymmetric == NULL ||
3355 drv->asymmetric->p_sign == NULL )
3356 {
3357 status = PSA_ERROR_NOT_SUPPORTED;
3358 goto exit;
3359 }
3360 status = drv->asymmetric->p_sign( drv_context,
3361 slot->data.se.slot_number,
3362 alg,
3363 hash, hash_length,
3364 signature, signature_size,
3365 signature_length );
3366 }
3367 else
3368#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003369#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003370 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003371 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003372 status = psa_rsa_sign( slot->data.rsa,
3373 alg,
3374 hash, hash_length,
3375 signature, signature_size,
3376 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003377 }
3378 else
3379#endif /* defined(MBEDTLS_RSA_C) */
3380#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003381 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003382 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003383#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003384 if(
3385#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3386 PSA_ALG_IS_ECDSA( alg )
3387#else
3388 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3389#endif
3390 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003391 status = psa_ecdsa_sign( slot->data.ecp,
3392 alg,
3393 hash, hash_length,
3394 signature, signature_size,
3395 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003396 else
3397#endif /* defined(MBEDTLS_ECDSA_C) */
3398 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003399 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003400 }
itayzafrir5c753392018-05-08 11:18:38 +03003401 }
3402 else
3403#endif /* defined(MBEDTLS_ECP_C) */
3404 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003405 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003406 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003407
3408exit:
3409 /* Fill the unused part of the output buffer (the whole buffer on error,
3410 * the trailing part on success) with something that isn't a valid mac
3411 * (barring an attack on the mac and deliberately-crafted input),
3412 * in case the caller doesn't check the return status properly. */
3413 if( status == PSA_SUCCESS )
3414 memset( signature + *signature_length, '!',
3415 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003416 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003418 /* If signature_size is 0 then we have nothing to do. We must not call
3419 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003420 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003421}
3422
Gilles Peskinec5487a82018-12-03 18:08:14 +01003423psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003424 psa_algorithm_t alg,
3425 const uint8_t *hash,
3426 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003427 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003428 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003429{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003430 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003431 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003432#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3433 const psa_drv_se_t *drv;
3434 psa_drv_se_context_t *drv_context;
3435#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003436
Gilles Peskineedc64242019-08-07 21:05:07 +02003437 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003438 if( status != PSA_SUCCESS )
3439 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003440
Gilles Peskineedc64242019-08-07 21:05:07 +02003441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3442 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3443 {
3444 if( drv->asymmetric == NULL ||
3445 drv->asymmetric->p_verify == NULL )
3446 return( PSA_ERROR_NOT_SUPPORTED );
3447 return( drv->asymmetric->p_verify( drv_context,
3448 slot->data.se.slot_number,
3449 alg,
3450 hash, hash_length,
3451 signature, signature_length ) );
3452 }
3453 else
3454#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003455#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003456 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003457 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003458 return( psa_rsa_verify( slot->data.rsa,
3459 alg,
3460 hash, hash_length,
3461 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003462 }
3463 else
3464#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003465#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003466 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003467 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003468#if defined(MBEDTLS_ECDSA_C)
3469 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003470 return( psa_ecdsa_verify( slot->data.ecp,
3471 hash, hash_length,
3472 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003473 else
3474#endif /* defined(MBEDTLS_ECDSA_C) */
3475 {
3476 return( PSA_ERROR_INVALID_ARGUMENT );
3477 }
itayzafrir5c753392018-05-08 11:18:38 +03003478 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003479 else
3480#endif /* defined(MBEDTLS_ECP_C) */
3481 {
3482 return( PSA_ERROR_NOT_SUPPORTED );
3483 }
3484}
3485
Gilles Peskine072ac562018-06-30 00:21:29 +02003486#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3487static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3488 mbedtls_rsa_context *rsa )
3489{
3490 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3491 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3492 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3493 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3494}
3495#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3496
Gilles Peskinec5487a82018-12-03 18:08:14 +01003497psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003498 psa_algorithm_t alg,
3499 const uint8_t *input,
3500 size_t input_length,
3501 const uint8_t *salt,
3502 size_t salt_length,
3503 uint8_t *output,
3504 size_t output_size,
3505 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003506{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003507 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003508 psa_status_t status;
3509
Darryl Green5cc689a2018-07-24 15:34:10 +01003510 (void) input;
3511 (void) input_length;
3512 (void) salt;
3513 (void) output;
3514 (void) output_size;
3515
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003516 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003517
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003518 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3519 return( PSA_ERROR_INVALID_ARGUMENT );
3520
Gilles Peskine28f8f302019-07-24 13:30:31 +02003521 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003522 if( status != PSA_SUCCESS )
3523 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003524 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3525 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527
3528#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003529 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003530 {
3531 mbedtls_rsa_context *rsa = slot->data.rsa;
3532 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003533 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003534 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003536 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003537 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003538 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3539 mbedtls_ctr_drbg_random,
3540 &global_data.ctr_drbg,
3541 MBEDTLS_RSA_PUBLIC,
3542 input_length,
3543 input,
3544 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545 }
3546 else
3547#endif /* MBEDTLS_PKCS1_V15 */
3548#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003549 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003551 psa_rsa_oaep_set_padding_mode( alg, rsa );
3552 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3553 mbedtls_ctr_drbg_random,
3554 &global_data.ctr_drbg,
3555 MBEDTLS_RSA_PUBLIC,
3556 salt, salt_length,
3557 input_length,
3558 input,
3559 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003560 }
3561 else
3562#endif /* MBEDTLS_PKCS1_V21 */
3563 {
3564 return( PSA_ERROR_INVALID_ARGUMENT );
3565 }
3566 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003567 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568 return( mbedtls_to_psa_error( ret ) );
3569 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003570 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003571#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572 {
3573 return( PSA_ERROR_NOT_SUPPORTED );
3574 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003575}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576
Gilles Peskinec5487a82018-12-03 18:08:14 +01003577psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003578 psa_algorithm_t alg,
3579 const uint8_t *input,
3580 size_t input_length,
3581 const uint8_t *salt,
3582 size_t salt_length,
3583 uint8_t *output,
3584 size_t output_size,
3585 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003587 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003588 psa_status_t status;
3589
Darryl Green5cc689a2018-07-24 15:34:10 +01003590 (void) input;
3591 (void) input_length;
3592 (void) salt;
3593 (void) output;
3594 (void) output_size;
3595
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003596 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003598 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3599 return( PSA_ERROR_INVALID_ARGUMENT );
3600
Gilles Peskine28f8f302019-07-24 13:30:31 +02003601 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003602 if( status != PSA_SUCCESS )
3603 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003604 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605 return( PSA_ERROR_INVALID_ARGUMENT );
3606
3607#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003608 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609 {
3610 mbedtls_rsa_context *rsa = slot->data.rsa;
3611 int ret;
3612
Gilles Peskine630a18a2018-06-29 17:49:35 +02003613 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003614 return( PSA_ERROR_INVALID_ARGUMENT );
3615
3616#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003617 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003619 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3620 mbedtls_ctr_drbg_random,
3621 &global_data.ctr_drbg,
3622 MBEDTLS_RSA_PRIVATE,
3623 output_length,
3624 input,
3625 output,
3626 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627 }
3628 else
3629#endif /* MBEDTLS_PKCS1_V15 */
3630#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003631 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003633 psa_rsa_oaep_set_padding_mode( alg, rsa );
3634 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3635 mbedtls_ctr_drbg_random,
3636 &global_data.ctr_drbg,
3637 MBEDTLS_RSA_PRIVATE,
3638 salt, salt_length,
3639 output_length,
3640 input,
3641 output,
3642 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 }
3644 else
3645#endif /* MBEDTLS_PKCS1_V21 */
3646 {
3647 return( PSA_ERROR_INVALID_ARGUMENT );
3648 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003649
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650 return( mbedtls_to_psa_error( ret ) );
3651 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003653#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654 {
3655 return( PSA_ERROR_NOT_SUPPORTED );
3656 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003657}
Gilles Peskine20035e32018-02-03 22:44:14 +01003658
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003659
3660
mohammad1603503973b2018-03-12 15:59:30 +02003661/****************************************************************/
3662/* Symmetric cryptography */
3663/****************************************************************/
3664
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003665/* Initialize the cipher operation structure. Once this function has been
3666 * called, psa_cipher_abort can run and will do the right thing. */
3667static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3668 psa_algorithm_t alg )
3669{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003670 if( ! PSA_ALG_IS_CIPHER( alg ) )
3671 {
3672 memset( operation, 0, sizeof( *operation ) );
3673 return( PSA_ERROR_INVALID_ARGUMENT );
3674 }
3675
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003676 operation->alg = alg;
3677 operation->key_set = 0;
3678 operation->iv_set = 0;
3679 operation->iv_required = 1;
3680 operation->iv_size = 0;
3681 operation->block_size = 0;
3682 mbedtls_cipher_init( &operation->ctx.cipher );
3683 return( PSA_SUCCESS );
3684}
3685
Gilles Peskinee553c652018-06-04 16:22:46 +02003686static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003687 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003688 psa_algorithm_t alg,
3689 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003690{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003691 int ret = 0;
3692 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003693 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003694 size_t key_bits;
3695 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003696 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3697 PSA_KEY_USAGE_ENCRYPT :
3698 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003699
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003700 /* A context must be freshly initialized before it can be set up. */
3701 if( operation->alg != 0 )
3702 {
3703 return( PSA_ERROR_BAD_STATE );
3704 }
3705
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003706 status = psa_cipher_init( operation, alg );
3707 if( status != PSA_SUCCESS )
3708 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003709
Gilles Peskine28f8f302019-07-24 13:30:31 +02003710 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003711 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003712 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003713 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003714
Gilles Peskine8e338702019-07-30 20:06:31 +02003715 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003716 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003717 {
3718 status = PSA_ERROR_NOT_SUPPORTED;
3719 goto exit;
3720 }
mohammad1603503973b2018-03-12 15:59:30 +02003721
mohammad1603503973b2018-03-12 15:59:30 +02003722 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003723 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003724 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003725
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003726#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003727 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003728 {
3729 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003730 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003731 memcpy( keys, slot->data.raw.data, 16 );
3732 memcpy( keys + 16, slot->data.raw.data, 8 );
3733 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3734 keys,
3735 192, cipher_operation );
3736 }
3737 else
3738#endif
3739 {
3740 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3741 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003742 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003743 }
Moran Peker41deec42018-04-04 15:43:05 +03003744 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003745 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003746
mohammad16038481e742018-03-18 13:57:31 +02003747#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003748 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003749 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003750 case PSA_ALG_CBC_NO_PADDING:
3751 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3752 MBEDTLS_PADDING_NONE );
3753 break;
3754 case PSA_ALG_CBC_PKCS7:
3755 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3756 MBEDTLS_PADDING_PKCS7 );
3757 break;
3758 default:
3759 /* The algorithm doesn't involve padding. */
3760 ret = 0;
3761 break;
3762 }
3763 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003764 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003765#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3766
mohammad1603503973b2018-03-12 15:59:30 +02003767 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003768 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003769 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003770 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003771 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003772 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003773 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003774#if defined(MBEDTLS_CHACHA20_C)
3775 else
3776 if( alg == PSA_ALG_CHACHA20 )
3777 operation->iv_size = 12;
3778#endif
mohammad1603503973b2018-03-12 15:59:30 +02003779
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003780exit:
3781 if( status == 0 )
3782 status = mbedtls_to_psa_error( ret );
3783 if( status != 0 )
3784 psa_cipher_abort( operation );
3785 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003786}
3787
Gilles Peskinefe119512018-07-08 21:39:34 +02003788psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003789 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003790 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003791{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003792 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003793}
3794
Gilles Peskinefe119512018-07-08 21:39:34 +02003795psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003796 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003797 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003798{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003799 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003800}
3801
Gilles Peskinefe119512018-07-08 21:39:34 +02003802psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003803 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003804 size_t iv_size,
3805 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003806{
itayzafrir534bd7c2018-08-02 13:56:32 +03003807 psa_status_t status;
3808 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003809 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003810 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003811 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003812 }
Moran Peker41deec42018-04-04 15:43:05 +03003813 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003814 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003815 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003816 goto exit;
3817 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003818 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3819 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003820 if( ret != 0 )
3821 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003822 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003823 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003824 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003825
mohammad16038481e742018-03-18 13:57:31 +02003826 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003827 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003828
Moran Peker395db872018-05-31 14:07:14 +03003829exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003830 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003831 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003832 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003833}
3834
Gilles Peskinefe119512018-07-08 21:39:34 +02003835psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003836 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003837 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003838{
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 psa_status_t status;
3840 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003841 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003842 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003843 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003844 }
Moran Pekera28258c2018-05-29 16:25:04 +03003845 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003846 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003847 status = PSA_ERROR_INVALID_ARGUMENT;
3848 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003849 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003850 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3851 status = mbedtls_to_psa_error( ret );
3852exit:
3853 if( status == PSA_SUCCESS )
3854 operation->iv_set = 1;
3855 else
mohammad1603503973b2018-03-12 15:59:30 +02003856 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003857 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003858}
3859
Gilles Peskinee553c652018-06-04 16:22:46 +02003860psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3861 const uint8_t *input,
3862 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003863 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003864 size_t output_size,
3865 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003866{
itayzafrir534bd7c2018-08-02 13:56:32 +03003867 psa_status_t status;
3868 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003869 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003870
3871 if( operation->alg == 0 )
3872 {
3873 return( PSA_ERROR_BAD_STATE );
3874 }
3875
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003876 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003877 {
3878 /* Take the unprocessed partial block left over from previous
3879 * update calls, if any, plus the input to this call. Remove
3880 * the last partial block, if any. You get the data that will be
3881 * output in this call. */
3882 expected_output_size =
3883 ( operation->ctx.cipher.unprocessed_len + input_length )
3884 / operation->block_size * operation->block_size;
3885 }
3886 else
3887 {
3888 expected_output_size = input_length;
3889 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003890
Gilles Peskine89d789c2018-06-04 17:17:16 +02003891 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 {
3893 status = PSA_ERROR_BUFFER_TOO_SMALL;
3894 goto exit;
3895 }
mohammad160382759612018-03-12 18:16:40 +02003896
mohammad1603503973b2018-03-12 15:59:30 +02003897 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003898 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 status = mbedtls_to_psa_error( ret );
3900exit:
3901 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003902 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003903 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003904}
3905
Gilles Peskinee553c652018-06-04 16:22:46 +02003906psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3907 uint8_t *output,
3908 size_t output_size,
3909 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003910{
David Saadab4ecc272019-02-14 13:48:10 +02003911 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003912 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003913 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003914
mohammad1603503973b2018-03-12 15:59:30 +02003915 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003916 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003917 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003918 }
3919 if( operation->iv_required && ! operation->iv_set )
3920 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003921 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003922 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003923
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003924 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003925 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3926 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003927 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003928 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003929 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003930 }
3931
Janos Follath315b51c2018-07-09 16:04:51 +01003932 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3933 temp_output_buffer,
3934 output_length );
3935 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003936 {
Janos Follath315b51c2018-07-09 16:04:51 +01003937 status = mbedtls_to_psa_error( cipher_ret );
3938 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003939 }
Janos Follath315b51c2018-07-09 16:04:51 +01003940
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003941 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003942 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003943 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003944 memcpy( output, temp_output_buffer, *output_length );
3945 else
3946 {
Janos Follath315b51c2018-07-09 16:04:51 +01003947 status = PSA_ERROR_BUFFER_TOO_SMALL;
3948 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003949 }
mohammad1603503973b2018-03-12 15:59:30 +02003950
Gilles Peskine3f108122018-12-07 18:14:53 +01003951 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003952 status = psa_cipher_abort( operation );
3953
3954 return( status );
3955
3956error:
3957
3958 *output_length = 0;
3959
Gilles Peskine3f108122018-12-07 18:14:53 +01003960 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003961 (void) psa_cipher_abort( operation );
3962
3963 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003964}
3965
Gilles Peskinee553c652018-06-04 16:22:46 +02003966psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3967{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003968 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003969 {
3970 /* The object has (apparently) been initialized but it is not
3971 * in use. It's ok to call abort on such an object, and there's
3972 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003973 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003974 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003975
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003976 /* Sanity check (shouldn't happen: operation->alg should
3977 * always have been initialized to a valid value). */
3978 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3979 return( PSA_ERROR_BAD_STATE );
3980
mohammad1603503973b2018-03-12 15:59:30 +02003981 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003982
Moran Peker41deec42018-04-04 15:43:05 +03003983 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003984 operation->key_set = 0;
3985 operation->iv_set = 0;
3986 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003987 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003988 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003989
Moran Peker395db872018-05-31 14:07:14 +03003990 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003991}
3992
Gilles Peskinea0655c32018-04-30 17:06:50 +02003993
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003994
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003995
mohammad16035955c982018-04-26 00:53:03 +03003996/****************************************************************/
3997/* AEAD */
3998/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003999
Gilles Peskineedf9a652018-08-17 18:11:56 +02004000typedef struct
4001{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004002 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004003 const mbedtls_cipher_info_t *cipher_info;
4004 union
4005 {
4006#if defined(MBEDTLS_CCM_C)
4007 mbedtls_ccm_context ccm;
4008#endif /* MBEDTLS_CCM_C */
4009#if defined(MBEDTLS_GCM_C)
4010 mbedtls_gcm_context gcm;
4011#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004012#if defined(MBEDTLS_CHACHAPOLY_C)
4013 mbedtls_chachapoly_context chachapoly;
4014#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004015 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004016 psa_algorithm_t core_alg;
4017 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004018 uint8_t tag_length;
4019} aead_operation_t;
4020
Gilles Peskine30a9e412019-01-14 18:36:12 +01004021static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004022{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004023 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004024 {
4025#if defined(MBEDTLS_CCM_C)
4026 case PSA_ALG_CCM:
4027 mbedtls_ccm_free( &operation->ctx.ccm );
4028 break;
4029#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004030#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004031 case PSA_ALG_GCM:
4032 mbedtls_gcm_free( &operation->ctx.gcm );
4033 break;
4034#endif /* MBEDTLS_GCM_C */
4035 }
4036}
4037
4038static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004039 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004040 psa_key_usage_t usage,
4041 psa_algorithm_t alg )
4042{
4043 psa_status_t status;
4044 size_t key_bits;
4045 mbedtls_cipher_id_t cipher_id;
4046
Gilles Peskine28f8f302019-07-24 13:30:31 +02004047 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004048 if( status != PSA_SUCCESS )
4049 return( status );
4050
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004051 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052
4053 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004054 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004055 &cipher_id );
4056 if( operation->cipher_info == NULL )
4057 return( PSA_ERROR_NOT_SUPPORTED );
4058
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004059 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 {
4061#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004062 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4063 operation->core_alg = PSA_ALG_CCM;
4064 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004065 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4066 * The call to mbedtls_ccm_encrypt_and_tag or
4067 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004068 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004069 return( PSA_ERROR_INVALID_ARGUMENT );
4070 mbedtls_ccm_init( &operation->ctx.ccm );
4071 status = mbedtls_to_psa_error(
4072 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4073 operation->slot->data.raw.data,
4074 (unsigned int) key_bits ) );
4075 if( status != 0 )
4076 goto cleanup;
4077 break;
4078#endif /* MBEDTLS_CCM_C */
4079
4080#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004081 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4082 operation->core_alg = PSA_ALG_GCM;
4083 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004084 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4085 * The call to mbedtls_gcm_crypt_and_tag or
4086 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004087 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 return( PSA_ERROR_INVALID_ARGUMENT );
4089 mbedtls_gcm_init( &operation->ctx.gcm );
4090 status = mbedtls_to_psa_error(
4091 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4092 operation->slot->data.raw.data,
4093 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004094 if( status != 0 )
4095 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 break;
4097#endif /* MBEDTLS_GCM_C */
4098
Gilles Peskine26869f22019-05-06 15:25:00 +02004099#if defined(MBEDTLS_CHACHAPOLY_C)
4100 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4101 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4102 operation->full_tag_length = 16;
4103 /* We only support the default tag length. */
4104 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4105 return( PSA_ERROR_NOT_SUPPORTED );
4106 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4107 status = mbedtls_to_psa_error(
4108 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4109 operation->slot->data.raw.data ) );
4110 if( status != 0 )
4111 goto cleanup;
4112 break;
4113#endif /* MBEDTLS_CHACHAPOLY_C */
4114
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115 default:
4116 return( PSA_ERROR_NOT_SUPPORTED );
4117 }
4118
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004119 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4120 {
4121 status = PSA_ERROR_INVALID_ARGUMENT;
4122 goto cleanup;
4123 }
4124 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004125
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 return( PSA_SUCCESS );
4127
4128cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004129 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004130 return( status );
4131}
4132
Gilles Peskinec5487a82018-12-03 18:08:14 +01004133psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004134 psa_algorithm_t alg,
4135 const uint8_t *nonce,
4136 size_t nonce_length,
4137 const uint8_t *additional_data,
4138 size_t additional_data_length,
4139 const uint8_t *plaintext,
4140 size_t plaintext_length,
4141 uint8_t *ciphertext,
4142 size_t ciphertext_size,
4143 size_t *ciphertext_length )
4144{
mohammad16035955c982018-04-26 00:53:03 +03004145 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004146 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004147 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004148
mohammad1603f08a5502018-06-03 15:05:47 +03004149 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004150
Gilles Peskinec5487a82018-12-03 18:08:14 +01004151 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004152 if( status != PSA_SUCCESS )
4153 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004154
Gilles Peskineedf9a652018-08-17 18:11:56 +02004155 /* For all currently supported modes, the tag is at the end of the
4156 * ciphertext. */
4157 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4158 {
4159 status = PSA_ERROR_BUFFER_TOO_SMALL;
4160 goto exit;
4161 }
4162 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004163
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004164#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004165 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004166 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 status = mbedtls_to_psa_error(
4168 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4169 MBEDTLS_GCM_ENCRYPT,
4170 plaintext_length,
4171 nonce, nonce_length,
4172 additional_data, additional_data_length,
4173 plaintext, ciphertext,
4174 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004175 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004176 else
4177#endif /* MBEDTLS_GCM_C */
4178#if defined(MBEDTLS_CCM_C)
4179 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004180 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004181 status = mbedtls_to_psa_error(
4182 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4183 plaintext_length,
4184 nonce, nonce_length,
4185 additional_data,
4186 additional_data_length,
4187 plaintext, ciphertext,
4188 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004189 }
mohammad16035c8845f2018-05-09 05:40:09 -07004190 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004191#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004192#if defined(MBEDTLS_CHACHAPOLY_C)
4193 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4194 {
4195 if( nonce_length != 12 || operation.tag_length != 16 )
4196 {
4197 status = PSA_ERROR_NOT_SUPPORTED;
4198 goto exit;
4199 }
4200 status = mbedtls_to_psa_error(
4201 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4202 plaintext_length,
4203 nonce,
4204 additional_data,
4205 additional_data_length,
4206 plaintext,
4207 ciphertext,
4208 tag ) );
4209 }
4210 else
4211#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004212 {
mohammad1603554faad2018-06-03 15:07:38 +03004213 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004214 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004215
Gilles Peskineedf9a652018-08-17 18:11:56 +02004216 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4217 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004218
Gilles Peskineedf9a652018-08-17 18:11:56 +02004219exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004220 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004221 if( status == PSA_SUCCESS )
4222 *ciphertext_length = plaintext_length + operation.tag_length;
4223 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004224}
4225
Gilles Peskineee652a32018-06-01 19:23:52 +02004226/* Locate the tag in a ciphertext buffer containing the encrypted data
4227 * followed by the tag. Return the length of the part preceding the tag in
4228 * *plaintext_length. This is the size of the plaintext in modes where
4229 * the encrypted data has the same size as the plaintext, such as
4230 * CCM and GCM. */
4231static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4232 const uint8_t *ciphertext,
4233 size_t ciphertext_length,
4234 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004235 const uint8_t **p_tag )
4236{
4237 size_t payload_length;
4238 if( tag_length > ciphertext_length )
4239 return( PSA_ERROR_INVALID_ARGUMENT );
4240 payload_length = ciphertext_length - tag_length;
4241 if( payload_length > plaintext_size )
4242 return( PSA_ERROR_BUFFER_TOO_SMALL );
4243 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004244 return( PSA_SUCCESS );
4245}
4246
Gilles Peskinec5487a82018-12-03 18:08:14 +01004247psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004248 psa_algorithm_t alg,
4249 const uint8_t *nonce,
4250 size_t nonce_length,
4251 const uint8_t *additional_data,
4252 size_t additional_data_length,
4253 const uint8_t *ciphertext,
4254 size_t ciphertext_length,
4255 uint8_t *plaintext,
4256 size_t plaintext_size,
4257 size_t *plaintext_length )
4258{
mohammad16035955c982018-04-26 00:53:03 +03004259 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004260 aead_operation_t operation;
4261 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004262
Gilles Peskineee652a32018-06-01 19:23:52 +02004263 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004264
Gilles Peskinec5487a82018-12-03 18:08:14 +01004265 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004266 if( status != PSA_SUCCESS )
4267 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004268
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004269 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4270 ciphertext, ciphertext_length,
4271 plaintext_size, &tag );
4272 if( status != PSA_SUCCESS )
4273 goto exit;
4274
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004275#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004276 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004277 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004278 status = mbedtls_to_psa_error(
4279 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4280 ciphertext_length - operation.tag_length,
4281 nonce, nonce_length,
4282 additional_data,
4283 additional_data_length,
4284 tag, operation.tag_length,
4285 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004286 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004287 else
4288#endif /* MBEDTLS_GCM_C */
4289#if defined(MBEDTLS_CCM_C)
4290 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004291 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004292 status = mbedtls_to_psa_error(
4293 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4294 ciphertext_length - operation.tag_length,
4295 nonce, nonce_length,
4296 additional_data,
4297 additional_data_length,
4298 ciphertext, plaintext,
4299 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004300 }
mohammad160339574652018-06-01 04:39:53 -07004301 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004302#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004303#if defined(MBEDTLS_CHACHAPOLY_C)
4304 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4305 {
4306 if( nonce_length != 12 || operation.tag_length != 16 )
4307 {
4308 status = PSA_ERROR_NOT_SUPPORTED;
4309 goto exit;
4310 }
4311 status = mbedtls_to_psa_error(
4312 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4313 ciphertext_length - operation.tag_length,
4314 nonce,
4315 additional_data,
4316 additional_data_length,
4317 tag,
4318 ciphertext,
4319 plaintext ) );
4320 }
4321 else
4322#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004323 {
mohammad1603554faad2018-06-03 15:07:38 +03004324 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004325 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004326
Gilles Peskineedf9a652018-08-17 18:11:56 +02004327 if( status != PSA_SUCCESS && plaintext_size != 0 )
4328 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004329
Gilles Peskineedf9a652018-08-17 18:11:56 +02004330exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004331 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004332 if( status == PSA_SUCCESS )
4333 *plaintext_length = ciphertext_length - operation.tag_length;
4334 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004335}
4336
Gilles Peskinea0655c32018-04-30 17:06:50 +02004337
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004338
Gilles Peskine20035e32018-02-03 22:44:14 +01004339/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004340/* Generators */
4341/****************************************************************/
4342
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004343#define HKDF_STATE_INIT 0 /* no input yet */
4344#define HKDF_STATE_STARTED 1 /* got salt */
4345#define HKDF_STATE_KEYED 2 /* got key */
4346#define HKDF_STATE_OUTPUT 3 /* output started */
4347
Gilles Peskinecbe66502019-05-16 16:59:18 +02004348static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004349 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004350{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4352 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004353 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004355}
4356
4357
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004358psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004359{
4360 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004362 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004363 {
4364 /* The object has (apparently) been initialized but it is not
4365 * in use. It's ok to call abort on such an object, and there's
4366 * nothing to do. */
4367 }
4368 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004369#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004370 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004371 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 mbedtls_free( operation->ctx.hkdf.info );
4373 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004374 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004375 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004377 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004378 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004379#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004380 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004381 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4383 operation->ctx.tls12_prf.key_len );
4384 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004385 }
Hanno Becker580fba12018-11-13 20:50:45 +00004386
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004387 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004388 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004389 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4390 operation->ctx.tls12_prf.Ai_with_seed_len );
4391 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004392 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004393#else
4394 if( operation->ctx.tls12_prf.seed != NULL )
4395 {
4396 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4397 operation->ctx.tls12_prf.seed_length );
4398 mbedtls_free( operation->ctx.tls12_prf.seed );
4399 }
4400
4401 if( operation->ctx.tls12_prf.label != NULL )
4402 {
4403 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4404 operation->ctx.tls12_prf.label_length );
4405 mbedtls_free( operation->ctx.tls12_prf.label );
4406 }
4407
4408 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4409
4410 /* We leave the fields Ai and output_block to be erased safely by the
4411 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004412#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004413 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004414 else
4415#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004416 {
4417 status = PSA_ERROR_BAD_STATE;
4418 }
Janos Follath083036a2019-06-11 10:22:26 +01004419 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004420 return( status );
4421}
4422
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004424 size_t *capacity)
4425{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004427 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004429 return PSA_ERROR_BAD_STATE;
4430 }
4431
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004433 return( PSA_SUCCESS );
4434}
4435
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004437 size_t capacity )
4438{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004440 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004442 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004444 return( PSA_SUCCESS );
4445}
4446
Gilles Peskinebef7f142018-07-12 17:22:21 +02004447#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004450static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004451 psa_algorithm_t hash_alg,
4452 uint8_t *output,
4453 size_t output_length )
4454{
4455 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4456 psa_status_t status;
4457
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004458 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4459 return( PSA_ERROR_BAD_STATE );
4460 hkdf->state = HKDF_STATE_OUTPUT;
4461
Gilles Peskinebef7f142018-07-12 17:22:21 +02004462 while( output_length != 0 )
4463 {
4464 /* Copy what remains of the current block */
4465 uint8_t n = hash_length - hkdf->offset_in_block;
4466 if( n > output_length )
4467 n = (uint8_t) output_length;
4468 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4469 output += n;
4470 output_length -= n;
4471 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004472 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004473 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004474 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004475 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004476 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004477 * object was corrupted or if this function is called directly
4478 * inside the library. */
4479 if( hkdf->block_number == 0xff )
4480 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004481
4482 /* We need a new block */
4483 ++hkdf->block_number;
4484 hkdf->offset_in_block = 0;
4485 status = psa_hmac_setup_internal( &hkdf->hmac,
4486 hkdf->prk, hash_length,
4487 hash_alg );
4488 if( status != PSA_SUCCESS )
4489 return( status );
4490 if( hkdf->block_number != 1 )
4491 {
4492 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4493 hkdf->output_block,
4494 hash_length );
4495 if( status != PSA_SUCCESS )
4496 return( status );
4497 }
4498 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4499 hkdf->info,
4500 hkdf->info_length );
4501 if( status != PSA_SUCCESS )
4502 return( status );
4503 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4504 &hkdf->block_number, 1 );
4505 if( status != PSA_SUCCESS )
4506 return( status );
4507 status = psa_hmac_finish_internal( &hkdf->hmac,
4508 hkdf->output_block,
4509 sizeof( hkdf->output_block ) );
4510 if( status != PSA_SUCCESS )
4511 return( status );
4512 }
4513
4514 return( PSA_SUCCESS );
4515}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004516
Janos Follath999f6482019-06-11 12:04:10 +01004517#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004518static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4519 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004520 psa_algorithm_t alg )
4521{
4522 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4523 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4524 psa_hmac_internal_data hmac;
4525 psa_status_t status, cleanup_status;
4526
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004527 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004528 size_t Ai_len;
4529
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004530 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004531 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004533 * object was corrupted or if this function is called directly
4534 * inside the library. */
4535 if( tls12_prf->block_number == 0xff )
4536 return( PSA_ERROR_BAD_STATE );
4537
4538 /* We need a new block */
4539 ++tls12_prf->block_number;
4540 tls12_prf->offset_in_block = 0;
4541
4542 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4543 *
4544 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4545 *
4546 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4547 * HMAC_hash(secret, A(2) + seed) +
4548 * HMAC_hash(secret, A(3) + seed) + ...
4549 *
4550 * A(0) = seed
4551 * A(i) = HMAC_hash( secret, A(i-1) )
4552 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004553 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004554 * `HMAC_hash(secret, A(i) + seed)` from which the output
4555 * is currently extracted as `output_block`, while
4556 * `A(i) + seed` is stored in `Ai_with_seed`.
4557 *
4558 * Generating a new block means recalculating `Ai_with_seed`
4559 * from the A(i)-part of it, and afterwards recalculating
4560 * `output_block`.
4561 *
4562 * A(0) is computed at setup time.
4563 *
4564 */
4565
4566 psa_hmac_init_internal( &hmac );
4567
4568 /* We must distinguish the calculation of A(1) from those
4569 * of A(2) and higher, because A(0)=seed has a different
4570 * length than the other A(i). */
4571 if( tls12_prf->block_number == 1 )
4572 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004573 Ai = tls12_prf->Ai_with_seed + hash_length;
4574 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004575 }
4576 else
4577 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004578 Ai = tls12_prf->Ai_with_seed;
4579 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004580 }
4581
Hanno Becker3b339e22018-11-13 20:56:14 +00004582 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4583 status = psa_hmac_setup_internal( &hmac,
4584 tls12_prf->key,
4585 tls12_prf->key_len,
4586 hash_alg );
4587 if( status != PSA_SUCCESS )
4588 goto cleanup;
4589
4590 status = psa_hash_update( &hmac.hash_ctx,
4591 Ai, Ai_len );
4592 if( status != PSA_SUCCESS )
4593 goto cleanup;
4594
4595 status = psa_hmac_finish_internal( &hmac,
4596 tls12_prf->Ai_with_seed,
4597 hash_length );
4598 if( status != PSA_SUCCESS )
4599 goto cleanup;
4600
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004601 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4602 status = psa_hmac_setup_internal( &hmac,
4603 tls12_prf->key,
4604 tls12_prf->key_len,
4605 hash_alg );
4606 if( status != PSA_SUCCESS )
4607 goto cleanup;
4608
4609 status = psa_hash_update( &hmac.hash_ctx,
4610 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004611 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004612 if( status != PSA_SUCCESS )
4613 goto cleanup;
4614
4615 status = psa_hmac_finish_internal( &hmac,
4616 tls12_prf->output_block,
4617 hash_length );
4618 if( status != PSA_SUCCESS )
4619 goto cleanup;
4620
4621cleanup:
4622
4623 cleanup_status = psa_hmac_abort_internal( &hmac );
4624 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4625 status = cleanup_status;
4626
4627 return( status );
4628}
Janos Follath7742fee2019-06-17 12:58:10 +01004629#else
4630static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4631 psa_tls12_prf_key_derivation_t *tls12_prf,
4632 psa_algorithm_t alg )
4633{
4634 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4635 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004636 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4637 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004638
Janos Follath7742fee2019-06-17 12:58:10 +01004639 /* We can't be wanting more output after block 0xff, otherwise
4640 * the capacity check in psa_key_derivation_output_bytes() would have
4641 * prevented this call. It could happen only if the operation
4642 * object was corrupted or if this function is called directly
4643 * inside the library. */
4644 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004645 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004646
4647 /* We need a new block */
4648 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004649 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004650
4651 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4652 *
4653 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4654 *
4655 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4656 * HMAC_hash(secret, A(2) + seed) +
4657 * HMAC_hash(secret, A(3) + seed) + ...
4658 *
4659 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004660 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004661 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004662 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004663 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004664 * is currently extracted as `output_block` and where i is
4665 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004666 */
4667
Janos Follathea29bfb2019-06-19 12:21:20 +01004668 /* Save the hash context before using it, to preserve the hash state with
4669 * only the inner padding in it. We need this, because inner padding depends
4670 * on the key (secret in the RFC's terminology). */
4671 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4672 if( status != PSA_SUCCESS )
4673 goto cleanup;
4674
4675 /* Calculate A(i) where i = tls12_prf->block_number. */
4676 if( tls12_prf->block_number == 1 )
4677 {
4678 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4679 * the variable seed and in this instance means it in the context of the
4680 * P_hash function, where seed = label + seed.) */
4681 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4682 tls12_prf->label, tls12_prf->label_length );
4683 if( status != PSA_SUCCESS )
4684 goto cleanup;
4685 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4686 tls12_prf->seed, tls12_prf->seed_length );
4687 if( status != PSA_SUCCESS )
4688 goto cleanup;
4689 }
4690 else
4691 {
4692 /* A(i) = HMAC_hash(secret, A(i-1)) */
4693 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4694 tls12_prf->Ai, hash_length );
4695 if( status != PSA_SUCCESS )
4696 goto cleanup;
4697 }
4698
4699 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4700 tls12_prf->Ai, hash_length );
4701 if( status != PSA_SUCCESS )
4702 goto cleanup;
4703 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4704 if( status != PSA_SUCCESS )
4705 goto cleanup;
4706
4707 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4708 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4709 tls12_prf->Ai, hash_length );
4710 if( status != PSA_SUCCESS )
4711 goto cleanup;
4712 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4713 tls12_prf->label, tls12_prf->label_length );
4714 if( status != PSA_SUCCESS )
4715 goto cleanup;
4716 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4717 tls12_prf->seed, tls12_prf->seed_length );
4718 if( status != PSA_SUCCESS )
4719 goto cleanup;
4720 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4721 tls12_prf->output_block, hash_length );
4722 if( status != PSA_SUCCESS )
4723 goto cleanup;
4724 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4725 if( status != PSA_SUCCESS )
4726 goto cleanup;
4727
Janos Follath7742fee2019-06-17 12:58:10 +01004728
4729cleanup:
4730
Janos Follathea29bfb2019-06-19 12:21:20 +01004731 cleanup_status = psa_hash_abort( &backup );
4732 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4733 status = cleanup_status;
4734
4735 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004736}
Janos Follath999f6482019-06-11 12:04:10 +01004737#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004738
Janos Follath999f6482019-06-11 12:04:10 +01004739#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004741 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004742static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004743 psa_tls12_prf_key_derivation_t *tls12_prf,
4744 psa_algorithm_t alg,
4745 uint8_t *output,
4746 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004747{
4748 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4749 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4750 psa_status_t status;
4751
4752 while( output_length != 0 )
4753 {
4754 /* Copy what remains of the current block */
4755 uint8_t n = hash_length - tls12_prf->offset_in_block;
4756
4757 /* Check if we have fully processed the current block. */
4758 if( n == 0 )
4759 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004760 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004761 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004762 if( status != PSA_SUCCESS )
4763 return( status );
4764
4765 continue;
4766 }
4767
4768 if( n > output_length )
4769 n = (uint8_t) output_length;
4770 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4771 n );
4772 output += n;
4773 output_length -= n;
4774 tls12_prf->offset_in_block += n;
4775 }
4776
4777 return( PSA_SUCCESS );
4778}
Janos Follath844eb0e2019-06-19 12:10:49 +01004779#else
4780static psa_status_t psa_key_derivation_tls12_prf_read(
4781 psa_tls12_prf_key_derivation_t *tls12_prf,
4782 psa_algorithm_t alg,
4783 uint8_t *output,
4784 size_t output_length )
4785{
4786 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4787 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4788 psa_status_t status;
4789 uint8_t offset, length;
4790
4791 while( output_length != 0 )
4792 {
4793 /* Check if we have fully processed the current block. */
4794 if( tls12_prf->left_in_block == 0 )
4795 {
4796 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4797 alg );
4798 if( status != PSA_SUCCESS )
4799 return( status );
4800
4801 continue;
4802 }
4803
4804 if( tls12_prf->left_in_block > output_length )
4805 length = (uint8_t) output_length;
4806 else
4807 length = tls12_prf->left_in_block;
4808
4809 offset = hash_length - tls12_prf->left_in_block;
4810 memcpy( output, tls12_prf->output_block + offset, length );
4811 output += length;
4812 output_length -= length;
4813 tls12_prf->left_in_block -= length;
4814 }
4815
4816 return( PSA_SUCCESS );
4817}
Janos Follath999f6482019-06-11 12:04:10 +01004818#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004819#endif /* MBEDTLS_MD_C */
4820
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004821psa_status_t psa_key_derivation_output_bytes(
4822 psa_key_derivation_operation_t *operation,
4823 uint8_t *output,
4824 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004825{
4826 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004828
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004830 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004832 return PSA_ERROR_BAD_STATE;
4833 }
4834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004836 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004838 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004840 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004841 goto exit;
4842 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004844 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004846 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004847 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4848 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004850 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004851 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004852 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004854
Gilles Peskinebef7f142018-07-12 17:22:21 +02004855#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004857 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004858 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004860 output, output_length );
4861 }
Janos Follathadbec812019-06-14 11:05:39 +01004862 else
Janos Follathadbec812019-06-14 11:05:39 +01004863 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004864 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004865 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004867 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004868 output_length );
4869 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004870 else
4871#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004872 {
4873 return( PSA_ERROR_BAD_STATE );
4874 }
4875
4876exit:
4877 if( status != PSA_SUCCESS )
4878 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004879 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880 * This allows us to differentiate between exhausted operations and
4881 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4882 * operations. */
4883 psa_algorithm_t alg = operation->alg;
4884 psa_key_derivation_abort( operation );
4885 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004886 memset( output, '!', output_length );
4887 }
4888 return( status );
4889}
4890
Gilles Peskine08542d82018-07-19 17:05:42 +02004891#if defined(MBEDTLS_DES_C)
4892static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4893{
4894 if( data_size >= 8 )
4895 mbedtls_des_key_set_parity( data );
4896 if( data_size >= 16 )
4897 mbedtls_des_key_set_parity( data + 8 );
4898 if( data_size >= 24 )
4899 mbedtls_des_key_set_parity( data + 16 );
4900}
4901#endif /* MBEDTLS_DES_C */
4902
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004903static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004904 psa_key_slot_t *slot,
4905 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004907{
4908 uint8_t *data = NULL;
4909 size_t bytes = PSA_BITS_TO_BYTES( bits );
4910 psa_status_t status;
4911
Gilles Peskine8e338702019-07-30 20:06:31 +02004912 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004913 return( PSA_ERROR_INVALID_ARGUMENT );
4914 if( bits % 8 != 0 )
4915 return( PSA_ERROR_INVALID_ARGUMENT );
4916 data = mbedtls_calloc( 1, bytes );
4917 if( data == NULL )
4918 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004921 if( status != PSA_SUCCESS )
4922 goto exit;
4923#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004924 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004925 psa_des_set_key_parity( data, bytes );
4926#endif /* MBEDTLS_DES_C */
4927 status = psa_import_key_into_slot( slot, data, bytes );
4928
4929exit:
4930 mbedtls_free( data );
4931 return( status );
4932}
4933
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004934psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004936 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004937{
4938 psa_status_t status;
4939 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004940 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004941 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4942 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004943#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4944 if( driver != NULL )
4945 {
4946 /* Deriving a key in a secure element is not implemented yet. */
4947 status = PSA_ERROR_NOT_SUPPORTED;
4948 }
4949#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004950 if( status == PSA_SUCCESS )
4951 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004952 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004953 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004955 }
4956 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004957 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004958 if( status != PSA_SUCCESS )
4959 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004960 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004961 *handle = 0;
4962 }
4963 return( status );
4964}
4965
Gilles Peskineeab56e42018-07-12 17:12:33 +02004966
4967
4968/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004969/* Key derivation */
4970/****************************************************************/
4971
Gilles Peskinea05219c2018-11-16 16:02:56 +01004972#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004973#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004975 * of the HKDF algorithm.
4976 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004977 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004978 * to potentially free embedded data structures and wipe confidential data.
4979 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004980static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004981 const uint8_t *secret,
4982 size_t secret_length,
4983 psa_algorithm_t hash_alg,
4984 const uint8_t *salt,
4985 size_t salt_length,
4986 const uint8_t *label,
4987 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004988{
4989 psa_status_t status;
4990 status = psa_hmac_setup_internal( &hkdf->hmac,
4991 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004992 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004993 if( status != PSA_SUCCESS )
4994 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004995 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004996 if( status != PSA_SUCCESS )
4997 return( status );
4998 status = psa_hmac_finish_internal( &hkdf->hmac,
4999 hkdf->prk,
5000 sizeof( hkdf->prk ) );
5001 if( status != PSA_SUCCESS )
5002 return( status );
5003 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5004 hkdf->block_number = 0;
5005 hkdf->info_length = label_length;
5006 if( label_length != 0 )
5007 {
5008 hkdf->info = mbedtls_calloc( 1, label_length );
5009 if( hkdf->info == NULL )
5010 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5011 memcpy( hkdf->info, label, label_length );
5012 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005013 hkdf->state = HKDF_STATE_KEYED;
5014 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005015 return( PSA_SUCCESS );
5016}
Janos Follath71a4c912019-06-11 09:14:47 +01005017#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005018#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005019
Gilles Peskinea05219c2018-11-16 16:02:56 +01005020#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01005021#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01005023 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005024 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005025 * to potentially free embedded data structures and wipe confidential data.
5026 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005027static psa_status_t psa_key_derivation_tls12_prf_setup(
5028 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005029 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005030 size_t key_len,
5031 psa_algorithm_t hash_alg,
5032 const uint8_t *salt,
5033 size_t salt_length,
5034 const uint8_t *label,
5035 size_t label_length )
5036{
5037 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00005038 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
5039 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005040
5041 tls12_prf->key = mbedtls_calloc( 1, key_len );
5042 if( tls12_prf->key == NULL )
5043 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5044 tls12_prf->key_len = key_len;
5045 memcpy( tls12_prf->key, key, key_len );
5046
Hanno Becker580fba12018-11-13 20:50:45 +00005047 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005048 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00005049 if( overflow )
5050 return( PSA_ERROR_INVALID_ARGUMENT );
5051
5052 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
5053 if( tls12_prf->Ai_with_seed == NULL )
5054 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5055 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
5056
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005057 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
5058 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00005059 if( label_length != 0 )
5060 {
5061 memcpy( tls12_prf->Ai_with_seed + hash_length,
5062 label, label_length );
5063 }
5064
5065 if( salt_length != 0 )
5066 {
5067 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
5068 salt, salt_length );
5069 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005070
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005071 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005072 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005073 tls12_prf->block_number = 0;
5074 tls12_prf->offset_in_block = hash_length;
5075
5076 return( PSA_SUCCESS );
5077}
Janos Follath71a4c912019-06-11 09:14:47 +01005078#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00005079
Janos Follath71a4c912019-06-11 09:14:47 +01005080#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005082static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
5083 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005084 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005085 size_t psk_len,
5086 psa_algorithm_t hash_alg,
5087 const uint8_t *salt,
5088 size_t salt_length,
5089 const uint8_t *label,
5090 size_t label_length )
5091{
5092 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005093 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00005094
5095 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5096 return( PSA_ERROR_INVALID_ARGUMENT );
5097
5098 /* Quoting RFC 4279, Section 2:
5099 *
5100 * The premaster secret is formed as follows: if the PSK is N octets
5101 * long, concatenate a uint16 with the value N, N zero octets, a second
5102 * uint16 with the value N, and the PSK itself.
5103 */
5104
5105 pms[0] = ( psk_len >> 8 ) & 0xff;
5106 pms[1] = ( psk_len >> 0 ) & 0xff;
5107 memset( pms + 2, 0, psk_len );
5108 pms[2 + psk_len + 0] = pms[0];
5109 pms[2 + psk_len + 1] = pms[1];
5110 memcpy( pms + 4 + psk_len, psk, psk_len );
5111
Gilles Peskinecbe66502019-05-16 16:59:18 +02005112 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005113 pms, 4 + 2 * psk_len,
5114 hash_alg,
5115 salt, salt_length,
5116 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005117
Gilles Peskine3f108122018-12-07 18:14:53 +01005118 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005119 return( status );
5120}
Janos Follath71a4c912019-06-11 09:14:47 +01005121#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005122#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005123
Janos Follath71a4c912019-06-11 09:14:47 +01005124#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005125/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005126 * to potentially free embedded data structures and wipe confidential data.
5127 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005128static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005130 const uint8_t *secret, size_t secret_length,
5131 psa_algorithm_t alg,
5132 const uint8_t *salt, size_t salt_length,
5133 const uint8_t *label, size_t label_length,
5134 size_t capacity )
5135{
5136 psa_status_t status;
5137 size_t max_capacity;
5138
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 /* Set operation->alg even on failure so that abort knows what to do. */
5140 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005141
5142#if defined(MBEDTLS_MD_C)
5143 if( PSA_ALG_IS_HKDF( alg ) )
5144 {
5145 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5146 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5147 if( hash_size == 0 )
5148 return( PSA_ERROR_NOT_SUPPORTED );
5149 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005151 secret, secret_length,
5152 hash_alg,
5153 salt, salt_length,
5154 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005155 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005156 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5157 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5158 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005159 {
5160 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5161 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5162
5163 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5164 if( hash_alg != PSA_ALG_SHA_256 &&
5165 hash_alg != PSA_ALG_SHA_384 )
5166 {
5167 return( PSA_ERROR_NOT_SUPPORTED );
5168 }
5169
5170 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005171
5172 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5173 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005175 secret, secret_length,
5176 hash_alg, salt, salt_length,
5177 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005178 }
5179 else
5180 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005181 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005183 secret, secret_length,
5184 hash_alg, salt, salt_length,
5185 label, label_length );
5186 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005187 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005188 else
5189#endif
5190 {
5191 return( PSA_ERROR_NOT_SUPPORTED );
5192 }
5193
5194 if( status != PSA_SUCCESS )
5195 return( status );
5196
5197 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005198 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005199 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005201 else
5202 return( PSA_ERROR_INVALID_ARGUMENT );
5203
5204 return( PSA_SUCCESS );
5205}
Janos Follath71a4c912019-06-11 09:14:47 +01005206#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005207
Janos Follath71a4c912019-06-11 09:14:47 +01005208#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005210 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005211 psa_algorithm_t alg,
5212 const uint8_t *salt,
5213 size_t salt_length,
5214 const uint8_t *label,
5215 size_t label_length,
5216 size_t capacity )
5217{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005218 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005219 psa_status_t status;
5220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005222 return( PSA_ERROR_BAD_STATE );
5223
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005224 /* Make sure that alg is a key derivation algorithm. This prevents
5225 * key selection algorithms, which psa_key_derivation_internal
5226 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005227 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5228 return( PSA_ERROR_INVALID_ARGUMENT );
5229
Gilles Peskine28f8f302019-07-24 13:30:31 +02005230 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005231 if( status != PSA_SUCCESS )
5232 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005233
Gilles Peskine8e338702019-07-30 20:06:31 +02005234 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005235 return( PSA_ERROR_INVALID_ARGUMENT );
5236
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005237 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005238 slot->data.raw.data,
5239 slot->data.raw.bytes,
5240 alg,
5241 salt, salt_length,
5242 label, label_length,
5243 capacity );
5244 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005246 return( status );
5247}
Janos Follath71a4c912019-06-11 09:14:47 +01005248#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005249
Gilles Peskine969c5d62019-01-16 15:53:06 +01005250static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253{
Janos Follath5fe19732019-06-20 15:09:30 +01005254 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5255 * initialisers for this union leave some bytes unspecified.) */
5256 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5257
Gilles Peskine969c5d62019-01-16 15:53:06 +01005258 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005259#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005260 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5261 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5262 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005263 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005264 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005265 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5266 if( hash_size == 0 )
5267 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5269 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005270 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271 {
5272 return( PSA_ERROR_NOT_SUPPORTED );
5273 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005275 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276 }
5277#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005278 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005280}
5281
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005283 psa_algorithm_t alg )
5284{
5285 psa_status_t status;
5286
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005288 return( PSA_ERROR_BAD_STATE );
5289
Gilles Peskine6843c292019-01-18 16:44:49 +01005290 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5291 return( PSA_ERROR_INVALID_ARGUMENT );
5292 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005293 {
5294 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005295 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005296 }
5297 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5298 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 else
5302 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005303
5304 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005305 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005306 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005307}
5308
5309#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005310static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005311 psa_algorithm_t hash_alg,
5312 psa_key_derivation_step_t step,
5313 const uint8_t *data,
5314 size_t data_length )
5315{
5316 psa_status_t status;
5317 switch( step )
5318 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005319 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005320 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005322 status = psa_hmac_setup_internal( &hkdf->hmac,
5323 data, data_length,
5324 hash_alg );
5325 if( status != PSA_SUCCESS )
5326 return( status );
5327 hkdf->state = HKDF_STATE_STARTED;
5328 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005329 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005330 /* If no salt was provided, use an empty salt. */
5331 if( hkdf->state == HKDF_STATE_INIT )
5332 {
5333 status = psa_hmac_setup_internal( &hkdf->hmac,
5334 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005335 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005336 if( status != PSA_SUCCESS )
5337 return( status );
5338 hkdf->state = HKDF_STATE_STARTED;
5339 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005340 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005341 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005342 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5343 data, data_length );
5344 if( status != PSA_SUCCESS )
5345 return( status );
5346 status = psa_hmac_finish_internal( &hkdf->hmac,
5347 hkdf->prk,
5348 sizeof( hkdf->prk ) );
5349 if( status != PSA_SUCCESS )
5350 return( status );
5351 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5352 hkdf->block_number = 0;
5353 hkdf->state = HKDF_STATE_KEYED;
5354 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005355 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005356 if( hkdf->state == HKDF_STATE_OUTPUT )
5357 return( PSA_ERROR_BAD_STATE );
5358 if( hkdf->info_set )
5359 return( PSA_ERROR_BAD_STATE );
5360 hkdf->info_length = data_length;
5361 if( data_length != 0 )
5362 {
5363 hkdf->info = mbedtls_calloc( 1, data_length );
5364 if( hkdf->info == NULL )
5365 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5366 memcpy( hkdf->info, data, data_length );
5367 }
5368 hkdf->info_set = 1;
5369 return( PSA_SUCCESS );
5370 default:
5371 return( PSA_ERROR_INVALID_ARGUMENT );
5372 }
5373}
Janos Follathb03233e2019-06-11 15:30:30 +01005374
5375#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5376static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5377 psa_algorithm_t hash_alg,
5378 psa_key_derivation_step_t step,
5379 const uint8_t *data,
5380 size_t data_length )
5381{
5382 (void) prf;
5383 (void) hash_alg;
5384 (void) step;
5385 (void) data;
5386 (void) data_length;
5387
5388 return( PSA_ERROR_INVALID_ARGUMENT );
5389}
Janos Follath6660f0e2019-06-17 08:44:03 +01005390
5391static psa_status_t psa_tls12_prf_psk_to_ms_input(
5392 psa_tls12_prf_key_derivation_t *prf,
5393 psa_algorithm_t hash_alg,
5394 psa_key_derivation_step_t step,
5395 const uint8_t *data,
5396 size_t data_length )
5397{
5398 (void) prf;
5399 (void) hash_alg;
5400 (void) step;
5401 (void) data;
5402 (void) data_length;
5403
5404 return( PSA_ERROR_INVALID_ARGUMENT );
5405}
Janos Follathb03233e2019-06-11 15:30:30 +01005406#else
Janos Follathf08e2652019-06-13 09:05:41 +01005407static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5408 const uint8_t *data,
5409 size_t data_length )
5410{
5411 if( prf->state != TLS12_PRF_STATE_INIT )
5412 return( PSA_ERROR_BAD_STATE );
5413
Janos Follathd6dce9f2019-07-04 09:11:38 +01005414 if( data_length != 0 )
5415 {
5416 prf->seed = mbedtls_calloc( 1, data_length );
5417 if( prf->seed == NULL )
5418 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005419
Janos Follathd6dce9f2019-07-04 09:11:38 +01005420 memcpy( prf->seed, data, data_length );
5421 prf->seed_length = data_length;
5422 }
Janos Follathf08e2652019-06-13 09:05:41 +01005423
5424 prf->state = TLS12_PRF_STATE_SEED_SET;
5425
5426 return( PSA_SUCCESS );
5427}
5428
Janos Follath81550542019-06-13 14:26:34 +01005429static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5430 psa_algorithm_t hash_alg,
5431 const uint8_t *data,
5432 size_t data_length )
5433{
5434 psa_status_t status;
5435 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5436 return( PSA_ERROR_BAD_STATE );
5437
5438 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5439 if( status != PSA_SUCCESS )
5440 return( status );
5441
5442 prf->state = TLS12_PRF_STATE_KEY_SET;
5443
5444 return( PSA_SUCCESS );
5445}
5446
Janos Follath6660f0e2019-06-17 08:44:03 +01005447static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5448 psa_tls12_prf_key_derivation_t *prf,
5449 psa_algorithm_t hash_alg,
5450 const uint8_t *data,
5451 size_t data_length )
5452{
5453 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005454 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5455 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005456
5457 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5458 return( PSA_ERROR_INVALID_ARGUMENT );
5459
5460 /* Quoting RFC 4279, Section 2:
5461 *
5462 * The premaster secret is formed as follows: if the PSK is N octets
5463 * long, concatenate a uint16 with the value N, N zero octets, a second
5464 * uint16 with the value N, and the PSK itself.
5465 */
5466
Janos Follath40e13932019-06-26 13:22:29 +01005467 *cur++ = ( data_length >> 8 ) & 0xff;
5468 *cur++ = ( data_length >> 0 ) & 0xff;
5469 memset( cur, 0, data_length );
5470 cur += data_length;
5471 *cur++ = pms[0];
5472 *cur++ = pms[1];
5473 memcpy( cur, data, data_length );
5474 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005475
Janos Follath40e13932019-06-26 13:22:29 +01005476 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005477
5478 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5479 return( status );
5480}
5481
Janos Follath63028dd2019-06-13 09:15:47 +01005482static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5483 const uint8_t *data,
5484 size_t data_length )
5485{
5486 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5487 return( PSA_ERROR_BAD_STATE );
5488
Janos Follathd6dce9f2019-07-04 09:11:38 +01005489 if( data_length != 0 )
5490 {
5491 prf->label = mbedtls_calloc( 1, data_length );
5492 if( prf->label == NULL )
5493 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005494
Janos Follathd6dce9f2019-07-04 09:11:38 +01005495 memcpy( prf->label, data, data_length );
5496 prf->label_length = data_length;
5497 }
Janos Follath63028dd2019-06-13 09:15:47 +01005498
5499 prf->state = TLS12_PRF_STATE_LABEL_SET;
5500
5501 return( PSA_SUCCESS );
5502}
5503
Janos Follathb03233e2019-06-11 15:30:30 +01005504static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5505 psa_algorithm_t hash_alg,
5506 psa_key_derivation_step_t step,
5507 const uint8_t *data,
5508 size_t data_length )
5509{
Janos Follathb03233e2019-06-11 15:30:30 +01005510 switch( step )
5511 {
Janos Follathf08e2652019-06-13 09:05:41 +01005512 case PSA_KEY_DERIVATION_INPUT_SEED:
5513 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005514 case PSA_KEY_DERIVATION_INPUT_SECRET:
5515 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005516 case PSA_KEY_DERIVATION_INPUT_LABEL:
5517 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005518 default:
5519 return( PSA_ERROR_INVALID_ARGUMENT );
5520 }
5521}
Janos Follath6660f0e2019-06-17 08:44:03 +01005522
5523static psa_status_t psa_tls12_prf_psk_to_ms_input(
5524 psa_tls12_prf_key_derivation_t *prf,
5525 psa_algorithm_t hash_alg,
5526 psa_key_derivation_step_t step,
5527 const uint8_t *data,
5528 size_t data_length )
5529{
5530 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005531 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005532 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5533 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005534 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005535
5536 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5537}
Janos Follathb03233e2019-06-11 15:30:30 +01005538#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005539#endif /* MBEDTLS_MD_C */
5540
Janos Follathb80a94e2019-06-12 15:54:46 +01005541static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005542 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005543 psa_key_derivation_step_t step,
5544 const uint8_t *data,
5545 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005546{
5547 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005549
5550#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005551 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005552 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005553 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005554 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005555 step, data, data_length );
5556 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005557 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005558#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005559#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005560 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005561 {
Janos Follathb03233e2019-06-11 15:30:30 +01005562 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5563 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5564 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005565 }
5566 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5567 {
5568 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5569 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5570 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571 }
5572 else
5573#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005576 return( PSA_ERROR_BAD_STATE );
5577 }
5578
5579 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005580 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005581 return( status );
5582}
5583
Janos Follath51f4a0f2019-06-14 11:35:55 +01005584psa_status_t psa_key_derivation_input_bytes(
5585 psa_key_derivation_operation_t *operation,
5586 psa_key_derivation_step_t step,
5587 const uint8_t *data,
5588 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005589{
Janos Follathc5621512019-06-14 11:27:57 +01005590 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5591 return( PSA_ERROR_INVALID_ARGUMENT );
5592
5593 return( psa_key_derivation_input_internal( operation, step,
5594 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005595}
5596
Janos Follath51f4a0f2019-06-14 11:35:55 +01005597psa_status_t psa_key_derivation_input_key(
5598 psa_key_derivation_operation_t *operation,
5599 psa_key_derivation_step_t step,
5600 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005601{
5602 psa_key_slot_t *slot;
5603 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005604 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005605 PSA_KEY_USAGE_DERIVE,
5606 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005607 if( status != PSA_SUCCESS )
5608 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005609 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005610 return( PSA_ERROR_INVALID_ARGUMENT );
5611 /* Don't allow a key to be used as an input that is usually public.
5612 * This is debatable. It's ok from a cryptographic perspective to
5613 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005614 * the material should be dedicated to a particular input step,
5615 * otherwise this may allow the key to be used in an unintended way
5616 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005617 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005618 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005619 return( psa_key_derivation_input_internal( operation,
5620 step,
5621 slot->data.raw.data,
5622 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005623}
5624
Gilles Peskineea0fb492018-07-12 17:17:20 +02005625
5626
5627/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005628/* Key agreement */
5629/****************************************************************/
5630
Gilles Peskinea05219c2018-11-16 16:02:56 +01005631#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005632static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5633 size_t peer_key_length,
5634 const mbedtls_ecp_keypair *our_key,
5635 uint8_t *shared_secret,
5636 size_t shared_secret_size,
5637 size_t *shared_secret_length )
5638{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005639 mbedtls_ecp_keypair *their_key = NULL;
5640 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005641 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005642 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005643
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005644 status = psa_import_ec_public_key(
5645 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5646 peer_key, peer_key_length,
5647 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005648 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005649 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005650
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005651 status = mbedtls_to_psa_error(
5652 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5653 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005654 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005655 status = mbedtls_to_psa_error(
5656 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5657 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005658 goto exit;
5659
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005660 status = mbedtls_to_psa_error(
5661 mbedtls_ecdh_calc_secret( &ecdh,
5662 shared_secret_length,
5663 shared_secret, shared_secret_size,
5664 mbedtls_ctr_drbg_random,
5665 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005666
5667exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005668 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005669 mbedtls_ecp_keypair_free( their_key );
5670 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005671 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005672}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005673#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005674
Gilles Peskine01d718c2018-09-18 12:01:02 +02005675#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5676
Gilles Peskine0216fe12019-04-11 21:23:21 +02005677static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5678 psa_key_slot_t *private_key,
5679 const uint8_t *peer_key,
5680 size_t peer_key_length,
5681 uint8_t *shared_secret,
5682 size_t shared_secret_size,
5683 size_t *shared_secret_length )
5684{
5685 switch( alg )
5686 {
5687#if defined(MBEDTLS_ECDH_C)
5688 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005689 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005690 return( PSA_ERROR_INVALID_ARGUMENT );
5691 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5692 private_key->data.ecp,
5693 shared_secret, shared_secret_size,
5694 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005695#endif /* MBEDTLS_ECDH_C */
5696 default:
5697 (void) private_key;
5698 (void) peer_key;
5699 (void) peer_key_length;
5700 (void) shared_secret;
5701 (void) shared_secret_size;
5702 (void) shared_secret_length;
5703 return( PSA_ERROR_NOT_SUPPORTED );
5704 }
5705}
5706
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005707/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005708 * to potentially free embedded data structures and wipe confidential data.
5709 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005711 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005712 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005713 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005714 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005715{
5716 psa_status_t status;
5717 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5718 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005720
5721 /* Step 1: run the secret agreement algorithm to generate the shared
5722 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005723 status = psa_key_agreement_raw_internal( ka_alg,
5724 private_key,
5725 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005726 shared_secret,
5727 sizeof( shared_secret ),
5728 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005729 if( status != PSA_SUCCESS )
5730 goto exit;
5731
5732 /* Step 2: set up the key derivation to generate key material from
5733 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005734 status = psa_key_derivation_input_internal( operation, step,
5735 shared_secret,
5736 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005737
Gilles Peskine01d718c2018-09-18 12:01:02 +02005738exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005739 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005740 return( status );
5741}
5742
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005744 psa_key_derivation_step_t step,
5745 psa_key_handle_t private_key,
5746 const uint8_t *peer_key,
5747 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005748{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005749 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005750 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005752 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005753 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005754 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005755 if( status != PSA_SUCCESS )
5756 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005757 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005758 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005759 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005760 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005761 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005762 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005763}
5764
Gilles Peskinebe697d82019-05-16 18:00:41 +02005765psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5766 psa_key_handle_t private_key,
5767 const uint8_t *peer_key,
5768 size_t peer_key_length,
5769 uint8_t *output,
5770 size_t output_size,
5771 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005772{
5773 psa_key_slot_t *slot;
5774 psa_status_t status;
5775
5776 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5777 {
5778 status = PSA_ERROR_INVALID_ARGUMENT;
5779 goto exit;
5780 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005781 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005782 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005783 if( status != PSA_SUCCESS )
5784 goto exit;
5785
5786 status = psa_key_agreement_raw_internal( alg, slot,
5787 peer_key, peer_key_length,
5788 output, output_size,
5789 output_length );
5790
5791exit:
5792 if( status != PSA_SUCCESS )
5793 {
5794 /* If an error happens and is not handled properly, the output
5795 * may be used as a key to protect sensitive data. Arrange for such
5796 * a key to be random, which is likely to result in decryption or
5797 * verification errors. This is better than filling the buffer with
5798 * some constant data such as zeros, which would result in the data
5799 * being protected with a reproducible, easily knowable key.
5800 */
5801 psa_generate_random( output, output_size );
5802 *output_length = output_size;
5803 }
5804 return( status );
5805}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005806
5807
5808/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005809/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005810/****************************************************************/
5811
5812psa_status_t psa_generate_random( uint8_t *output,
5813 size_t output_size )
5814{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005815 int ret;
5816 GUARD_MODULE_INITIALIZED;
5817
Gilles Peskinef181eca2019-08-07 13:49:00 +02005818 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5819 {
5820 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5821 output,
5822 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5823 if( ret != 0 )
5824 return( mbedtls_to_psa_error( ret ) );
5825 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5826 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5827 }
5828
itayzafrir0adf0fc2018-09-06 16:24:41 +03005829 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005830 return( mbedtls_to_psa_error( ret ) );
5831}
5832
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005833#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5834#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005835
Gilles Peskine7228da22019-07-15 11:06:15 +02005836psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005837 size_t seed_size )
5838{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005839 if( global_data.initialized )
5840 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005841
5842 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5843 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5844 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5845 return( PSA_ERROR_INVALID_ARGUMENT );
5846
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005847 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005848}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005849#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005850
Gilles Peskinee56e8782019-04-26 17:34:02 +02005851#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5852static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5853 size_t domain_parameters_size,
5854 int *exponent )
5855{
5856 size_t i;
5857 uint32_t acc = 0;
5858
5859 if( domain_parameters_size == 0 )
5860 {
5861 *exponent = 65537;
5862 return( PSA_SUCCESS );
5863 }
5864
5865 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5866 * support values that fit in a 32-bit integer, which is larger than
5867 * int on just about every platform anyway. */
5868 if( domain_parameters_size > sizeof( acc ) )
5869 return( PSA_ERROR_NOT_SUPPORTED );
5870 for( i = 0; i < domain_parameters_size; i++ )
5871 acc = ( acc << 8 ) | domain_parameters[i];
5872 if( acc > INT_MAX )
5873 return( PSA_ERROR_NOT_SUPPORTED );
5874 *exponent = acc;
5875 return( PSA_SUCCESS );
5876}
5877#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5878
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005879static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005880 psa_key_slot_t *slot, size_t bits,
5881 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005882{
Gilles Peskine8e338702019-07-30 20:06:31 +02005883 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005884
Gilles Peskinee56e8782019-04-26 17:34:02 +02005885 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005886 return( PSA_ERROR_INVALID_ARGUMENT );
5887
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005888 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005889 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005890 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005891 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005892 if( status != PSA_SUCCESS )
5893 return( status );
5894 status = psa_generate_random( slot->data.raw.data,
5895 slot->data.raw.bytes );
5896 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005897 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005898#if defined(MBEDTLS_DES_C)
5899 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005900 psa_des_set_key_parity( slot->data.raw.data,
5901 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005902#endif /* MBEDTLS_DES_C */
5903 }
5904 else
5905
5906#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005907 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005908 {
5909 mbedtls_rsa_context *rsa;
5910 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005911 int exponent;
5912 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005913 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5914 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005915 /* Accept only byte-aligned keys, for the same reasons as
5916 * in psa_import_rsa_key(). */
5917 if( bits % 8 != 0 )
5918 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005919 status = psa_read_rsa_exponent( domain_parameters,
5920 domain_parameters_size,
5921 &exponent );
5922 if( status != PSA_SUCCESS )
5923 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005924 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5925 if( rsa == NULL )
5926 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5927 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5928 ret = mbedtls_rsa_gen_key( rsa,
5929 mbedtls_ctr_drbg_random,
5930 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005931 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005932 exponent );
5933 if( ret != 0 )
5934 {
5935 mbedtls_rsa_free( rsa );
5936 mbedtls_free( rsa );
5937 return( mbedtls_to_psa_error( ret ) );
5938 }
5939 slot->data.rsa = rsa;
5940 }
5941 else
5942#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5943
5944#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005945 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005946 {
5947 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5948 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5949 const mbedtls_ecp_curve_info *curve_info =
5950 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5951 mbedtls_ecp_keypair *ecp;
5952 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005953 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005954 return( PSA_ERROR_NOT_SUPPORTED );
5955 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5956 return( PSA_ERROR_NOT_SUPPORTED );
5957 if( curve_info->bit_size != bits )
5958 return( PSA_ERROR_INVALID_ARGUMENT );
5959 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5960 if( ecp == NULL )
5961 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5962 mbedtls_ecp_keypair_init( ecp );
5963 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5964 mbedtls_ctr_drbg_random,
5965 &global_data.ctr_drbg );
5966 if( ret != 0 )
5967 {
5968 mbedtls_ecp_keypair_free( ecp );
5969 mbedtls_free( ecp );
5970 return( mbedtls_to_psa_error( ret ) );
5971 }
5972 slot->data.ecp = ecp;
5973 }
5974 else
5975#endif /* MBEDTLS_ECP_C */
5976
5977 return( PSA_ERROR_NOT_SUPPORTED );
5978
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005979 return( PSA_SUCCESS );
5980}
5981
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005982psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005983 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005984{
5985 psa_status_t status;
5986 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005987 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005988
Gilles Peskinedf179142019-07-15 22:02:14 +02005989 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5990 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005991 if( status != PSA_SUCCESS )
5992 goto exit;
5993
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005994#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5995 if( driver != NULL )
5996 {
Gilles Peskine11792082019-08-06 18:36:36 +02005997 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5998 size_t pubkey_length = 0; /* We don't support this feature yet */
5999 if( drv->key_management == NULL ||
6000 drv->key_management->p_generate == NULL )
6001 {
6002 status = PSA_ERROR_NOT_SUPPORTED;
6003 goto exit;
6004 }
6005 status = drv->key_management->p_generate(
6006 psa_get_se_driver_context( driver ),
6007 slot->data.se.slot_number, attributes,
6008 NULL, 0, &pubkey_length );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006009 }
Gilles Peskine11792082019-08-06 18:36:36 +02006010 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006011#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006012 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006013 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006014 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006015 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006016 }
Gilles Peskine11792082019-08-06 18:36:36 +02006017
6018exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006019 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006020 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006021 if( status != PSA_SUCCESS )
6022 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006023 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006024 *handle = 0;
6025 }
6026 return( status );
6027}
6028
6029
Gilles Peskine05d69892018-06-19 22:00:52 +02006030
6031/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01006032/* Module setup */
6033/****************************************************************/
6034
Gilles Peskine5e769522018-11-20 21:59:56 +01006035psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6036 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6037 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6038{
6039 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6040 return( PSA_ERROR_BAD_STATE );
6041 global_data.entropy_init = entropy_init;
6042 global_data.entropy_free = entropy_free;
6043 return( PSA_SUCCESS );
6044}
6045
Gilles Peskinee59236f2018-01-27 23:32:46 +01006046void mbedtls_psa_crypto_free( void )
6047{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006048 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006049 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6050 {
6051 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006052 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006053 }
6054 /* Wipe all remaining data, including configuration.
6055 * In particular, this sets all state indicator to the value
6056 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006057 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006058#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006059 /* Unregister all secure element drivers, so that we restart from
6060 * a pristine state. */
6061 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006062#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006063}
6064
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006065#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6066/** Recover a transaction that was interrupted by a power failure.
6067 *
6068 * This function is called during initialization, before psa_crypto_init()
6069 * returns. If this function returns a failure status, the initialization
6070 * fails.
6071 */
6072static psa_status_t psa_crypto_recover_transaction(
6073 const psa_crypto_transaction_t *transaction )
6074{
6075 switch( transaction->unknown.type )
6076 {
6077 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6078 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
6079 /* TOnogrepDO - fall through to the failure case until this
6080 * is implemented */
6081 default:
6082 /* We found an unsupported transaction in the storage.
6083 * We don't know what state the storage is in. Give up. */
6084 return( PSA_ERROR_STORAGE_FAILURE );
6085 }
6086}
6087#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6088
Gilles Peskinee59236f2018-01-27 23:32:46 +01006089psa_status_t psa_crypto_init( void )
6090{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006091 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006092 const unsigned char drbg_seed[] = "PSA";
6093
Gilles Peskinec6b69072018-11-20 21:42:52 +01006094 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006095 if( global_data.initialized != 0 )
6096 return( PSA_SUCCESS );
6097
Gilles Peskine5e769522018-11-20 21:59:56 +01006098 /* Set default configuration if
6099 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6100 if( global_data.entropy_init == NULL )
6101 global_data.entropy_init = mbedtls_entropy_init;
6102 if( global_data.entropy_free == NULL )
6103 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104
Gilles Peskinec6b69072018-11-20 21:42:52 +01006105 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006106 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006107#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6108 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6109 /* The PSA entropy injection feature depends on using NV seed as an entropy
6110 * source. Add NV seed as an entropy source for PSA entropy injection. */
6111 mbedtls_entropy_add_source( &global_data.entropy,
6112 mbedtls_nv_seed_poll, NULL,
6113 MBEDTLS_ENTROPY_BLOCK_SIZE,
6114 MBEDTLS_ENTROPY_SOURCE_STRONG );
6115#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006116 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006117 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006118 status = mbedtls_to_psa_error(
6119 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6120 mbedtls_entropy_func,
6121 &global_data.entropy,
6122 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6123 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006124 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006125 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006126
Gilles Peskine66fb1262018-12-10 16:29:04 +01006127 status = psa_initialize_key_slots( );
6128 if( status != PSA_SUCCESS )
6129 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006130
Gilles Peskine4b734222019-07-24 15:56:31 +02006131#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006132 status = psa_crypto_load_transaction( );
6133 if( status == PSA_SUCCESS )
6134 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006135 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6136 if( status != PSA_SUCCESS )
6137 goto exit;
6138 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006139 }
6140 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6141 {
6142 /* There's no transaction to complete. It's all good. */
6143 status = PSA_SUCCESS;
6144 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006145#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006146
Gilles Peskinec6b69072018-11-20 21:42:52 +01006147 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006148 global_data.initialized = 1;
6149
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006151 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006152 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006153 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006154}
6155
6156#endif /* MBEDTLS_PSA_CRYPTO_C */