blob: bce777b151cc209b28b7019aee7854de85c4ffed [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;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001017 psa_status_t status; /* status of the last operation */
1018 psa_status_t overall_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 Peskine4b7f3402019-08-13 15:58:36 +02001044 /* We should still try to destroy the key in the secure
1045 * element and the key metadata in storage. This is especially
1046 * important if the error is that the storage is full.
1047 * But how to do it exactly without risking an inconsistent
1048 * state after a reset?
1049 * https://github.com/ARMmbed/mbed-crypto/issues/215
1050 */
1051 overall_status = status;
1052 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001053 }
1054
Gilles Peskine354f7672019-07-12 23:46:38 +02001055 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 if( overall_status == PSA_SUCCESS )
1057 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001058 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1060
Darryl Greend49a4992018-06-18 17:27:26 +01001061#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001062 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001063 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 status = psa_destroy_persistent_key( slot->attr.id );
1065 if( overall_status == PSA_SUCCESS )
1066 overall_status = status;
1067
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001068 /* TODO: other slots may have a copy of the same key. We should
1069 * invalidate them.
1070 * https://github.com/ARMmbed/mbed-crypto/issues/214
1071 */
Darryl Greend49a4992018-06-18 17:27:26 +01001072 }
1073#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001074
Gilles Peskinefc762652019-07-22 19:30:34 +02001075#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1076 if( driver != NULL )
1077 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001078 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001079 if( overall_status == PSA_SUCCESS )
1080 overall_status = status;
1081 status = psa_crypto_stop_transaction( );
1082 if( overall_status == PSA_SUCCESS )
1083 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001084 }
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1086
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088exit:
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001090 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1092 if( overall_status == PSA_SUCCESS )
1093 overall_status = status;
1094 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095}
1096
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001097void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1098{
Gilles Peskineb699f072019-04-26 16:06:02 +02001099 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100 memset( attributes, 0, sizeof( *attributes ) );
1101}
1102
Gilles Peskineb699f072019-04-26 16:06:02 +02001103psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1104 psa_key_type_t type,
1105 const uint8_t *data,
1106 size_t data_length )
1107{
1108 uint8_t *copy = NULL;
1109
1110 if( data_length != 0 )
1111 {
1112 copy = mbedtls_calloc( 1, data_length );
1113 if( copy == NULL )
1114 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1115 memcpy( copy, data, data_length );
1116 }
1117 /* After this point, this function is guaranteed to succeed, so it
1118 * can start modifying `*attributes`. */
1119
1120 if( attributes->domain_parameters != NULL )
1121 {
1122 mbedtls_free( attributes->domain_parameters );
1123 attributes->domain_parameters = NULL;
1124 attributes->domain_parameters_size = 0;
1125 }
1126
1127 attributes->domain_parameters = copy;
1128 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001129 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001130 return( PSA_SUCCESS );
1131}
1132
1133psa_status_t psa_get_key_domain_parameters(
1134 const psa_key_attributes_t *attributes,
1135 uint8_t *data, size_t data_size, size_t *data_length )
1136{
1137 if( attributes->domain_parameters_size > data_size )
1138 return( PSA_ERROR_BUFFER_TOO_SMALL );
1139 *data_length = attributes->domain_parameters_size;
1140 if( attributes->domain_parameters_size != 0 )
1141 memcpy( data, attributes->domain_parameters,
1142 attributes->domain_parameters_size );
1143 return( PSA_SUCCESS );
1144}
1145
1146#if defined(MBEDTLS_RSA_C)
1147static psa_status_t psa_get_rsa_public_exponent(
1148 const mbedtls_rsa_context *rsa,
1149 psa_key_attributes_t *attributes )
1150{
1151 mbedtls_mpi mpi;
1152 int ret;
1153 uint8_t *buffer = NULL;
1154 size_t buflen;
1155 mbedtls_mpi_init( &mpi );
1156
1157 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1158 if( ret != 0 )
1159 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001160 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1161 {
1162 /* It's the default value, which is reported as an empty string,
1163 * so there's nothing to do. */
1164 goto exit;
1165 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001166
1167 buflen = mbedtls_mpi_size( &mpi );
1168 buffer = mbedtls_calloc( 1, buflen );
1169 if( buffer == NULL )
1170 {
1171 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1172 goto exit;
1173 }
1174 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1175 if( ret != 0 )
1176 goto exit;
1177 attributes->domain_parameters = buffer;
1178 attributes->domain_parameters_size = buflen;
1179
1180exit:
1181 mbedtls_mpi_free( &mpi );
1182 if( ret != 0 )
1183 mbedtls_free( buffer );
1184 return( mbedtls_to_psa_error( ret ) );
1185}
1186#endif /* MBEDTLS_RSA_C */
1187
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001188/** Retrieve all the publicly-accessible attributes of a key.
1189 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1191 psa_key_attributes_t *attributes )
1192{
1193 psa_key_slot_t *slot;
1194 psa_status_t status;
1195
1196 psa_reset_key_attributes( attributes );
1197
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001198 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001199 if( status != PSA_SUCCESS )
1200 return( status );
1201
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001202 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001203 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1204 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1205
1206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1207 if( psa_key_slot_is_external( slot ) )
1208 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 {
1213#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001214 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001215 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001216#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1217 /* TOnogrepDO: reporting the public exponent for opaque keys
1218 * is not yet implemented. */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001219 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001220 break;
1221#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1223 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001224#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001225 default:
1226 /* Nothing else to do. */
1227 break;
1228 }
1229
1230 if( status != PSA_SUCCESS )
1231 psa_reset_key_attributes( attributes );
1232 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001233}
1234
Gilles Peskinec8000c02019-08-02 20:15:51 +02001235#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1236psa_status_t psa_get_key_slot_number(
1237 const psa_key_attributes_t *attributes,
1238 psa_key_slot_number_t *slot_number )
1239{
1240 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1241 {
1242 *slot_number = attributes->slot_number;
1243 return( PSA_SUCCESS );
1244 }
1245 else
1246 return( PSA_ERROR_INVALID_ARGUMENT );
1247}
1248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1249
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001250#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001251static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1252 unsigned char *buf, size_t size )
1253{
1254 int ret;
1255 unsigned char *c;
1256 size_t len = 0;
1257
1258 c = buf + size;
1259
1260 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1261
1262 return( (int) len );
1263}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001264#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001265
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001266static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1267 uint8_t *data,
1268 size_t data_size,
1269 size_t *data_length,
1270 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001271{
Gilles Peskine5d309672019-07-12 23:47:28 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1273 const psa_drv_se_t *drv;
1274 psa_drv_se_context_t *drv_context;
1275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1276
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001277 *data_length = 0;
1278
Gilles Peskine8e338702019-07-30 20:06:31 +02001279 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001280 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001281
Gilles Peskine5d309672019-07-12 23:47:28 +02001282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001283 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001284 {
1285 psa_drv_se_export_key_t method;
1286 if( drv->key_management == NULL )
1287 return( PSA_ERROR_NOT_SUPPORTED );
1288 method = ( export_public_key ?
1289 drv->key_management->p_export_public :
1290 drv->key_management->p_export );
1291 if( method == NULL )
1292 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001293 return( method( drv_context,
1294 slot->data.se.slot_number,
1295 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001296 }
1297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1298
Gilles Peskine8e338702019-07-30 20:06:31 +02001299 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001300 {
1301 if( slot->data.raw.bytes > data_size )
1302 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001303 if( data_size != 0 )
1304 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001305 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001306 memset( data + slot->data.raw.bytes, 0,
1307 data_size - slot->data.raw.bytes );
1308 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309 *data_length = slot->data.raw.bytes;
1310 return( PSA_SUCCESS );
1311 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001312#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001313 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001314 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001315 psa_status_t status;
1316
Gilles Peskineb46bef22019-07-30 21:32:04 +02001317 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001318 if( bytes > data_size )
1319 return( PSA_ERROR_BUFFER_TOO_SMALL );
1320 status = mbedtls_to_psa_error(
1321 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1322 if( status != PSA_SUCCESS )
1323 return( status );
1324 memset( data + bytes, 0, data_size - bytes );
1325 *data_length = bytes;
1326 return( PSA_SUCCESS );
1327 }
1328#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 else
Moran Peker17e36e12018-05-02 12:55:20 +03001330 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001331#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001332 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1333 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001334 {
Moran Pekera998bc62018-04-16 18:16:20 +03001335 mbedtls_pk_context pk;
1336 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001337 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001338 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001339#if defined(MBEDTLS_RSA_C)
1340 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001341 pk.pk_info = &mbedtls_rsa_info;
1342 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001343#else
1344 return( PSA_ERROR_NOT_SUPPORTED );
1345#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001346 }
1347 else
1348 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001349#if defined(MBEDTLS_ECP_C)
1350 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001351 pk.pk_info = &mbedtls_eckey_info;
1352 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001353#else
1354 return( PSA_ERROR_NOT_SUPPORTED );
1355#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001356 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001357 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001358 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001359 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001360 }
Moran Peker17e36e12018-05-02 12:55:20 +03001361 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001362 {
Moran Peker17e36e12018-05-02 12:55:20 +03001363 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001364 }
Moran Peker60364322018-04-29 11:34:58 +03001365 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001366 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001367 /* If data_size is 0 then data may be NULL and then the
1368 * call to memset would have undefined behavior. */
1369 if( data_size != 0 )
1370 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001371 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001372 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001373 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1374 * Move the data to the beginning and erase remaining data
1375 * at the original location. */
1376 if( 2 * (size_t) ret <= data_size )
1377 {
1378 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 }
1381 else if( (size_t) ret < data_size )
1382 {
1383 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001384 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001385 }
Moran Pekera998bc62018-04-16 18:16:20 +03001386 *data_length = ret;
1387 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001388 }
1389 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001390#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001391 {
1392 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001393 it is valid for a special-purpose implementation to omit
1394 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001395 return( PSA_ERROR_NOT_SUPPORTED );
1396 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397 }
1398}
1399
Gilles Peskinec5487a82018-12-03 18:08:14 +01001400psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001401 uint8_t *data,
1402 size_t data_size,
1403 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001404{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001405 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001406 psa_status_t status;
1407
1408 /* Set the key to empty now, so that even when there are errors, we always
1409 * set data_length to a value between 0 and data_size. On error, setting
1410 * the key to empty is a good choice because an empty key representation is
1411 * unlikely to be accepted anywhere. */
1412 *data_length = 0;
1413
1414 /* Export requires the EXPORT flag. There is an exception for public keys,
1415 * which don't require any flag, but psa_get_key_from_slot takes
1416 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001417 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001418 if( status != PSA_SUCCESS )
1419 return( status );
1420 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001421 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001422}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423
Gilles Peskinec5487a82018-12-03 18:08:14 +01001424psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001425 uint8_t *data,
1426 size_t data_size,
1427 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001428{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001429 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001430 psa_status_t status;
1431
1432 /* Set the key to empty now, so that even when there are errors, we always
1433 * set data_length to a value between 0 and data_size. On error, setting
1434 * the key to empty is a good choice because an empty key representation is
1435 * unlikely to be accepted anywhere. */
1436 *data_length = 0;
1437
1438 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001439 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001440 if( status != PSA_SUCCESS )
1441 return( status );
1442 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001443 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001444}
1445
Gilles Peskine91e8c332019-08-02 19:19:39 +02001446#if defined(static_assert)
1447static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1448 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001449static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001450 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001451static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452 "One or more key attribute flag is listed as both internal-only and external-only" );
1453#endif
1454
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001455/** Validate that a key policy is internally well-formed.
1456 *
1457 * This function only rejects invalid policies. It does not validate the
1458 * consistency of the policy with respect to other attributes of the key
1459 * such as the key type.
1460 */
1461static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001462{
1463 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001464 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001465 PSA_KEY_USAGE_ENCRYPT |
1466 PSA_KEY_USAGE_DECRYPT |
1467 PSA_KEY_USAGE_SIGN |
1468 PSA_KEY_USAGE_VERIFY |
1469 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1470 return( PSA_ERROR_INVALID_ARGUMENT );
1471
Gilles Peskine4747d192019-04-17 15:05:45 +02001472 return( PSA_SUCCESS );
1473}
1474
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001475/** Validate the internal consistency of key attributes.
1476 *
1477 * This function only rejects invalid attribute values. If does not
1478 * validate the consistency of the attributes with any key data that may
1479 * be involved in the creation of the key.
1480 *
1481 * Call this function early in the key creation process.
1482 *
1483 * \param[in] attributes Key attributes for the new key.
1484 * \param[out] p_drv On any return, the driver for the key, if any.
1485 * NULL for a transparent key.
1486 *
1487 */
1488static psa_status_t psa_validate_key_attributes(
1489 const psa_key_attributes_t *attributes,
1490 psa_se_drv_table_entry_t **p_drv )
1491{
1492 psa_status_t status;
1493
1494 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1495 {
1496 status = psa_validate_persistent_key_parameters(
1497 attributes->core.lifetime, attributes->core.id,
1498 p_drv, 1 );
1499 if( status != PSA_SUCCESS )
1500 return( status );
1501 }
1502
1503 status = psa_validate_key_policy( &attributes->core.policy );
1504 if( status != PSA_SUCCESS )
1505 return( status );
1506
1507 /* Refuse to create overly large keys.
1508 * Note that this doesn't trigger on import if the attributes don't
1509 * explicitly specify a size (so psa_get_key_bits returns 0), so
1510 * psa_import_key() needs its own checks. */
1511 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1512 return( PSA_ERROR_NOT_SUPPORTED );
1513
Gilles Peskine91e8c332019-08-02 19:19:39 +02001514 /* Reject invalid flags. These should not be reachable through the API. */
1515 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1516 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1517 return( PSA_ERROR_INVALID_ARGUMENT );
1518
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001519 return( PSA_SUCCESS );
1520}
1521
Gilles Peskine4747d192019-04-17 15:05:45 +02001522/** Prepare a key slot to receive key material.
1523 *
1524 * This function allocates a key slot and sets its metadata.
1525 *
1526 * If this function fails, call psa_fail_key_creation().
1527 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001528 * This function is intended to be used as follows:
1529 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1530 * it with the specified attributes, and assign it a handle.
1531 * -# Populate the slot with the key material.
1532 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1533 * In case of failure at any step, stop the sequence and call
1534 * psa_fail_key_creation().
1535 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001536 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 * \param[in] attributes Key attributes for the new key.
1538 * \param[out] handle On success, a handle for the allocated slot.
1539 * \param[out] p_slot On success, a pointer to the prepared slot.
1540 * \param[out] p_drv On any return, the driver for the key, if any.
1541 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * The key slot is ready to receive key material.
1545 * \return If this function fails, the key slot is an invalid state.
1546 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001547 */
1548static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001549 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 const psa_key_attributes_t *attributes,
1551 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001552 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001553 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001554{
1555 psa_status_t status;
1556 psa_key_slot_t *slot;
1557
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001559 *p_drv = NULL;
1560
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561 status = psa_validate_key_attributes( attributes, p_drv );
1562 if( status != PSA_SUCCESS )
1563 return( status );
1564
Gilles Peskineedbed562019-08-07 18:19:59 +02001565 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001566 if( status != PSA_SUCCESS )
1567 return( status );
1568 slot = *p_slot;
1569
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001570 /* We're storing the declared bit-size of the key. It's up to each
1571 * creation mechanism to verify that this information is correct.
1572 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001573 * an input (generate, device) but not for those where the bit-size
1574 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001575
1576 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001577
Gilles Peskine91e8c332019-08-02 19:19:39 +02001578 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001579 * external-only flags, query `attributes`. Thanks to the check
1580 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001581 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001582 * may have set. */
1583 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001584
Gilles Peskinecbaff462019-07-12 23:46:04 +02001585#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001586 /* For a key in a secure element, we need to do three things
1587 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001588 * create the key file in internal storage, create the
1589 * key inside the secure element, and update the driver's
1590 * persistent data. Start a transaction that will encompass these
1591 * three actions. */
1592 /* The first thing to do is to find a slot number for the new key.
1593 * We save the slot number in persistent storage as part of the
1594 * transaction data. It will be needed to recover if the power
1595 * fails during the key creation process, to clean up on the secure
1596 * element side after restarting. Obtaining a slot number from the
1597 * secure element driver updates its persistent state, but we do not yet
1598 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001599 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001600 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001601 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001602 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001603 &slot->data.se.slot_number );
1604 if( status != PSA_SUCCESS )
1605 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001606 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001607 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001609 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001610 status = psa_crypto_save_transaction( );
1611 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001612 {
1613 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001614 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001615 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001616 }
1617#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1618
Gilles Peskine4747d192019-04-17 15:05:45 +02001619 return( status );
1620}
1621
1622/** Finalize the creation of a key once its key material has been set.
1623 *
1624 * This entails writing the key to persistent storage.
1625 *
1626 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001627 * See the documentation of psa_start_key_creation() for the intended use
1628 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 * \param[in,out] slot Pointer to the slot with key material.
1631 * \param[in] driver The secure element driver for the key,
1632 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001633 *
1634 * \retval #PSA_SUCCESS
1635 * The key was successfully created. The handle is now valid.
1636 * \return If this function fails, the key slot is an invalid state.
1637 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001639static psa_status_t psa_finish_key_creation(
1640 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001641 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001642{
1643 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001644 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001645 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001646
1647#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001648 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001650#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1651 if( driver != NULL )
1652 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001653 psa_se_key_data_storage_t data;
1654#if defined(static_assert)
1655 static_assert( sizeof( slot->data.se.slot_number ) ==
1656 sizeof( data.slot_number ),
1657 "Slot number size does not match psa_se_key_data_storage_t" );
1658 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1659 "Bit-size size does not match psa_se_key_data_storage_t" );
1660#endif
1661 memcpy( &data.slot_number, &slot->data.se.slot_number,
1662 sizeof( slot->data.se.slot_number ) );
1663 memcpy( &data.bits, &slot->attr.bits,
1664 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001665 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001666 (uint8_t*) &data,
1667 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001668 }
1669 else
1670#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1671 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001672 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001673 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001674 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001675 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1676 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001677 if( buffer == NULL && buffer_size != 0 )
1678 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1679 status = psa_internal_export_key( slot,
1680 buffer, buffer_size, &length,
1681 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001682 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001683 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001684 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001685
Gilles Peskine1df83d42019-07-23 16:13:14 +02001686 if( buffer_size != 0 )
1687 mbedtls_platform_zeroize( buffer, buffer_size );
1688 mbedtls_free( buffer );
1689 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 }
1691#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1692
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001694 /* Finish the transaction for a key creation. This does not
1695 * happen when registering an existing key. Detect this case
1696 * by checking whether a transaction is in progress (actual
1697 * creation of a key in a secure element requires a transaction,
1698 * but registration doesn't use one). */
1699 if( driver != NULL &&
1700 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001701 {
1702 status = psa_save_se_persistent_data( driver );
1703 if( status != PSA_SUCCESS )
1704 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001705 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706 return( status );
1707 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001708 status = psa_crypto_stop_transaction( );
1709 if( status != PSA_SUCCESS )
1710 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001711 }
1712#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1713
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 return( status );
1715}
1716
1717/** Abort the creation of a key.
1718 *
1719 * You may call this function after calling psa_start_key_creation(),
1720 * or after psa_finish_key_creation() fails. In other circumstances, this
1721 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001722 * See the documentation of psa_start_key_creation() for the intended use
1723 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001725 * \param[in,out] slot Pointer to the slot with key material.
1726 * \param[in] driver The secure element driver for the key,
1727 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001729static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001730 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001731{
Gilles Peskine011e4282019-06-26 18:34:38 +02001732 (void) driver;
1733
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 if( slot == NULL )
1735 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001736
1737#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1738 /* TOnogrepDO: If the key has already been created in the secure
1739 * element, and the failure happened later (when saving metadata
1740 * to internal storage), we need to destroy the key in the secure
1741 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001742
Gilles Peskined7729582019-08-05 15:55:54 +02001743 /* Abort the ongoing transaction if any (there may not be one if
1744 * the creation process failed before starting one, or if the
1745 * key creation is a registration of a key in a secure element).
1746 * Earlier functions must already have done what it takes to undo any
1747 * partial creation. All that's left is to update the transaction data
1748 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001749 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001750#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1751
Gilles Peskine4747d192019-04-17 15:05:45 +02001752 psa_wipe_key_slot( slot );
1753}
1754
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001755/** Validate optional attributes during key creation.
1756 *
1757 * Some key attributes are optional during key creation. If they are
1758 * specified in the attributes structure, check that they are consistent
1759 * with the data in the slot.
1760 *
1761 * This function should be called near the end of key creation, after
1762 * the slot in memory is fully populated but before saving persistent data.
1763 */
1764static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001765 const psa_key_slot_t *slot,
1766 const psa_key_attributes_t *attributes )
1767{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001768 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001769 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001770 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001771 return( PSA_ERROR_INVALID_ARGUMENT );
1772 }
1773
1774 if( attributes->domain_parameters_size != 0 )
1775 {
1776#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001777 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001778 {
1779 mbedtls_mpi actual, required;
1780 int ret;
1781 mbedtls_mpi_init( &actual );
1782 mbedtls_mpi_init( &required );
1783 ret = mbedtls_rsa_export( slot->data.rsa,
1784 NULL, NULL, NULL, NULL, &actual );
1785 if( ret != 0 )
1786 goto rsa_exit;
1787 ret = mbedtls_mpi_read_binary( &required,
1788 attributes->domain_parameters,
1789 attributes->domain_parameters_size );
1790 if( ret != 0 )
1791 goto rsa_exit;
1792 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1793 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1794 rsa_exit:
1795 mbedtls_mpi_free( &actual );
1796 mbedtls_mpi_free( &required );
1797 if( ret != 0)
1798 return( mbedtls_to_psa_error( ret ) );
1799 }
1800 else
1801#endif
1802 {
1803 return( PSA_ERROR_INVALID_ARGUMENT );
1804 }
1805 }
1806
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001807 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001808 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001809 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 return( PSA_ERROR_INVALID_ARGUMENT );
1811 }
1812
1813 return( PSA_SUCCESS );
1814}
1815
Gilles Peskine4747d192019-04-17 15:05:45 +02001816psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001818 size_t data_length,
1819 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001820{
1821 psa_status_t status;
1822 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001823 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001824
Gilles Peskinedf179142019-07-15 22:02:14 +02001825 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1826 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001827 if( status != PSA_SUCCESS )
1828 goto exit;
1829
Gilles Peskine5d309672019-07-12 23:47:28 +02001830#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1831 if( driver != NULL )
1832 {
1833 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001834 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001835 if( drv->key_management == NULL ||
1836 drv->key_management->p_import == NULL )
1837 {
1838 status = PSA_ERROR_NOT_SUPPORTED;
1839 goto exit;
1840 }
1841 status = drv->key_management->p_import(
1842 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001843 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001844 &bits );
1845 if( status != PSA_SUCCESS )
1846 goto exit;
1847 if( bits > PSA_MAX_KEY_BITS )
1848 {
1849 status = PSA_ERROR_NOT_SUPPORTED;
1850 goto exit;
1851 }
1852 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001853 }
1854 else
1855#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1856 {
1857 status = psa_import_key_into_slot( slot, data, data_length );
1858 if( status != PSA_SUCCESS )
1859 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001860 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001861 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001862 if( status != PSA_SUCCESS )
1863 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001864
Gilles Peskine011e4282019-06-26 18:34:38 +02001865 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001866exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 if( status != PSA_SUCCESS )
1868 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 *handle = 0;
1871 }
1872 return( status );
1873}
1874
Gilles Peskined7729582019-08-05 15:55:54 +02001875#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1876psa_status_t mbedtls_psa_register_se_key(
1877 const psa_key_attributes_t *attributes )
1878{
1879 psa_status_t status;
1880 psa_key_slot_t *slot = NULL;
1881 psa_se_drv_table_entry_t *driver = NULL;
1882 const psa_drv_se_t *drv;
1883 psa_key_handle_t handle = 0;
1884
1885 /* Leaving attributes unspecified is not currently supported.
1886 * It could make sense to query the key type and size from the
1887 * secure element, but not all secure elements support this
1888 * and the driver HAL doesn't currently support it. */
1889 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1890 return( PSA_ERROR_NOT_SUPPORTED );
1891 if( psa_get_key_bits( attributes ) == 0 )
1892 return( PSA_ERROR_NOT_SUPPORTED );
1893
1894 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1895 &handle, &slot, &driver );
1896 if( status != PSA_SUCCESS )
1897 goto exit;
1898
1899 if( driver == NULL )
1900 {
1901 status = PSA_ERROR_INVALID_ARGUMENT;
1902 goto exit;
1903 }
1904 drv = psa_get_se_driver_methods( driver );
1905
1906 if ( psa_get_key_slot_number( attributes,
1907 &slot->data.se.slot_number ) != PSA_SUCCESS )
1908 {
1909 /* The application didn't specify a slot number. This doesn't
1910 * make sense when registering a slot. */
1911 status = PSA_ERROR_INVALID_ARGUMENT;
1912 goto exit;
1913 }
1914
Gilles Peskinea5f87492019-08-05 16:46:18 +02001915 /* If the driver has a slot number validation method, call it.
1916 * If it doesn't, it means the secure element is unable to validate
1917 * anything and so we have to trust the application. */
1918 if( drv->key_management != NULL &&
1919 drv->key_management->p_validate_slot_number != NULL )
1920 {
1921 status = drv->key_management->p_validate_slot_number(
1922 psa_get_se_driver_context( driver ),
1923 attributes,
1924 PSA_KEY_CREATION_REGISTER,
1925 slot->data.se.slot_number );
1926 if( status != PSA_SUCCESS )
1927 goto exit;
1928 }
1929
Gilles Peskined7729582019-08-05 15:55:54 +02001930 status = psa_finish_key_creation( slot, driver );
1931
1932exit:
1933 if( status != PSA_SUCCESS )
1934 {
1935 psa_fail_key_creation( slot, driver );
1936 }
1937 /* Registration doesn't keep the key in RAM. */
1938 psa_close_key( handle );
1939 return( status );
1940}
1941#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1942
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001943static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001944 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001945{
1946 psa_status_t status;
1947 uint8_t *buffer = NULL;
1948 size_t buffer_size = 0;
1949 size_t length;
1950
Gilles Peskine8e338702019-07-30 20:06:31 +02001951 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001952 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001953 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001954 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001955 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001956 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1957 if( status != PSA_SUCCESS )
1958 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001959 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001960 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001961
1962exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001963 if( buffer_size != 0 )
1964 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001965 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001966 return( status );
1967}
1968
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1970 const psa_key_attributes_t *specified_attributes,
1971 psa_key_handle_t *target_handle )
1972{
1973 psa_status_t status;
1974 psa_key_slot_t *source_slot = NULL;
1975 psa_key_slot_t *target_slot = NULL;
1976 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001977 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001978
Gilles Peskine28f8f302019-07-24 13:30:31 +02001979 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001980 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001981 if( status != PSA_SUCCESS )
1982 goto exit;
1983
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001984 status = psa_validate_optional_attributes( source_slot,
1985 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986 if( status != PSA_SUCCESS )
1987 goto exit;
1988
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001989 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001990 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
Gilles Peskinedf179142019-07-15 22:02:14 +02001994 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1995 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001996 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 if( status != PSA_SUCCESS )
1998 goto exit;
1999
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002000#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2001 if( driver != NULL )
2002 {
2003 /* Copying to a secure element is not implemented yet. */
2004 status = PSA_ERROR_NOT_SUPPORTED;
2005 goto exit;
2006 }
2007#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2008
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002009 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 if( status != PSA_SUCCESS )
2011 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012
Gilles Peskine011e4282019-06-26 18:34:38 +02002013 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002015 if( status != PSA_SUCCESS )
2016 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002017 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 *target_handle = 0;
2019 }
2020 return( status );
2021}
2022
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002023
2024
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002025/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002026/* Message digests */
2027/****************************************************************/
2028
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002029static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002030{
2031 switch( alg )
2032 {
2033#if defined(MBEDTLS_MD2_C)
2034 case PSA_ALG_MD2:
2035 return( &mbedtls_md2_info );
2036#endif
2037#if defined(MBEDTLS_MD4_C)
2038 case PSA_ALG_MD4:
2039 return( &mbedtls_md4_info );
2040#endif
2041#if defined(MBEDTLS_MD5_C)
2042 case PSA_ALG_MD5:
2043 return( &mbedtls_md5_info );
2044#endif
2045#if defined(MBEDTLS_RIPEMD160_C)
2046 case PSA_ALG_RIPEMD160:
2047 return( &mbedtls_ripemd160_info );
2048#endif
2049#if defined(MBEDTLS_SHA1_C)
2050 case PSA_ALG_SHA_1:
2051 return( &mbedtls_sha1_info );
2052#endif
2053#if defined(MBEDTLS_SHA256_C)
2054 case PSA_ALG_SHA_224:
2055 return( &mbedtls_sha224_info );
2056 case PSA_ALG_SHA_256:
2057 return( &mbedtls_sha256_info );
2058#endif
2059#if defined(MBEDTLS_SHA512_C)
2060 case PSA_ALG_SHA_384:
2061 return( &mbedtls_sha384_info );
2062 case PSA_ALG_SHA_512:
2063 return( &mbedtls_sha512_info );
2064#endif
2065 default:
2066 return( NULL );
2067 }
2068}
2069
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002070psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2071{
2072 switch( operation->alg )
2073 {
Gilles Peskine81736312018-06-26 15:04:31 +02002074 case 0:
2075 /* The object has (apparently) been initialized but it is not
2076 * in use. It's ok to call abort on such an object, and there's
2077 * nothing to do. */
2078 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002079#if defined(MBEDTLS_MD2_C)
2080 case PSA_ALG_MD2:
2081 mbedtls_md2_free( &operation->ctx.md2 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_MD4_C)
2085 case PSA_ALG_MD4:
2086 mbedtls_md4_free( &operation->ctx.md4 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_MD5_C)
2090 case PSA_ALG_MD5:
2091 mbedtls_md5_free( &operation->ctx.md5 );
2092 break;
2093#endif
2094#if defined(MBEDTLS_RIPEMD160_C)
2095 case PSA_ALG_RIPEMD160:
2096 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA1_C)
2100 case PSA_ALG_SHA_1:
2101 mbedtls_sha1_free( &operation->ctx.sha1 );
2102 break;
2103#endif
2104#if defined(MBEDTLS_SHA256_C)
2105 case PSA_ALG_SHA_224:
2106 case PSA_ALG_SHA_256:
2107 mbedtls_sha256_free( &operation->ctx.sha256 );
2108 break;
2109#endif
2110#if defined(MBEDTLS_SHA512_C)
2111 case PSA_ALG_SHA_384:
2112 case PSA_ALG_SHA_512:
2113 mbedtls_sha512_free( &operation->ctx.sha512 );
2114 break;
2115#endif
2116 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002117 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002118 }
2119 operation->alg = 0;
2120 return( PSA_SUCCESS );
2121}
2122
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002123psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124 psa_algorithm_t alg )
2125{
2126 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002127
2128 /* A context must be freshly initialized before it can be set up. */
2129 if( operation->alg != 0 )
2130 {
2131 return( PSA_ERROR_BAD_STATE );
2132 }
2133
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134 switch( alg )
2135 {
2136#if defined(MBEDTLS_MD2_C)
2137 case PSA_ALG_MD2:
2138 mbedtls_md2_init( &operation->ctx.md2 );
2139 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2140 break;
2141#endif
2142#if defined(MBEDTLS_MD4_C)
2143 case PSA_ALG_MD4:
2144 mbedtls_md4_init( &operation->ctx.md4 );
2145 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_MD5_C)
2149 case PSA_ALG_MD5:
2150 mbedtls_md5_init( &operation->ctx.md5 );
2151 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_RIPEMD160_C)
2155 case PSA_ALG_RIPEMD160:
2156 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2157 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_SHA1_C)
2161 case PSA_ALG_SHA_1:
2162 mbedtls_sha1_init( &operation->ctx.sha1 );
2163 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_SHA256_C)
2167 case PSA_ALG_SHA_224:
2168 mbedtls_sha256_init( &operation->ctx.sha256 );
2169 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2170 break;
2171 case PSA_ALG_SHA_256:
2172 mbedtls_sha256_init( &operation->ctx.sha256 );
2173 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2174 break;
2175#endif
2176#if defined(MBEDTLS_SHA512_C)
2177 case PSA_ALG_SHA_384:
2178 mbedtls_sha512_init( &operation->ctx.sha512 );
2179 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2180 break;
2181 case PSA_ALG_SHA_512:
2182 mbedtls_sha512_init( &operation->ctx.sha512 );
2183 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2184 break;
2185#endif
2186 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002187 return( PSA_ALG_IS_HASH( alg ) ?
2188 PSA_ERROR_NOT_SUPPORTED :
2189 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190 }
2191 if( ret == 0 )
2192 operation->alg = alg;
2193 else
2194 psa_hash_abort( operation );
2195 return( mbedtls_to_psa_error( ret ) );
2196}
2197
2198psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2199 const uint8_t *input,
2200 size_t input_length )
2201{
2202 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002203
2204 /* Don't require hash implementations to behave correctly on a
2205 * zero-length input, which may have an invalid pointer. */
2206 if( input_length == 0 )
2207 return( PSA_SUCCESS );
2208
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209 switch( operation->alg )
2210 {
2211#if defined(MBEDTLS_MD2_C)
2212 case PSA_ALG_MD2:
2213 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2214 input, input_length );
2215 break;
2216#endif
2217#if defined(MBEDTLS_MD4_C)
2218 case PSA_ALG_MD4:
2219 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2220 input, input_length );
2221 break;
2222#endif
2223#if defined(MBEDTLS_MD5_C)
2224 case PSA_ALG_MD5:
2225 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2226 input, input_length );
2227 break;
2228#endif
2229#if defined(MBEDTLS_RIPEMD160_C)
2230 case PSA_ALG_RIPEMD160:
2231 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_SHA1_C)
2236 case PSA_ALG_SHA_1:
2237 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2238 input, input_length );
2239 break;
2240#endif
2241#if defined(MBEDTLS_SHA256_C)
2242 case PSA_ALG_SHA_224:
2243 case PSA_ALG_SHA_256:
2244 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2245 input, input_length );
2246 break;
2247#endif
2248#if defined(MBEDTLS_SHA512_C)
2249 case PSA_ALG_SHA_384:
2250 case PSA_ALG_SHA_512:
2251 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2252 input, input_length );
2253 break;
2254#endif
2255 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002256 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002258
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259 if( ret != 0 )
2260 psa_hash_abort( operation );
2261 return( mbedtls_to_psa_error( ret ) );
2262}
2263
2264psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2265 uint8_t *hash,
2266 size_t hash_size,
2267 size_t *hash_length )
2268{
itayzafrir40835d42018-08-02 13:14:17 +03002269 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002271 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272
2273 /* Fill the output buffer with something that isn't a valid hash
2274 * (barring an attack on the hash and deliberately-crafted input),
2275 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002276 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002277 /* If hash_size is 0 then hash may be NULL and then the
2278 * call to memset would have undefined behavior. */
2279 if( hash_size != 0 )
2280 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281
2282 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002283 {
2284 status = PSA_ERROR_BUFFER_TOO_SMALL;
2285 goto exit;
2286 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287
2288 switch( operation->alg )
2289 {
2290#if defined(MBEDTLS_MD2_C)
2291 case PSA_ALG_MD2:
2292 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2293 break;
2294#endif
2295#if defined(MBEDTLS_MD4_C)
2296 case PSA_ALG_MD4:
2297 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2298 break;
2299#endif
2300#if defined(MBEDTLS_MD5_C)
2301 case PSA_ALG_MD5:
2302 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2303 break;
2304#endif
2305#if defined(MBEDTLS_RIPEMD160_C)
2306 case PSA_ALG_RIPEMD160:
2307 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2308 break;
2309#endif
2310#if defined(MBEDTLS_SHA1_C)
2311 case PSA_ALG_SHA_1:
2312 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2313 break;
2314#endif
2315#if defined(MBEDTLS_SHA256_C)
2316 case PSA_ALG_SHA_224:
2317 case PSA_ALG_SHA_256:
2318 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2319 break;
2320#endif
2321#if defined(MBEDTLS_SHA512_C)
2322 case PSA_ALG_SHA_384:
2323 case PSA_ALG_SHA_512:
2324 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2325 break;
2326#endif
2327 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002328 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 }
itayzafrir40835d42018-08-02 13:14:17 +03002330 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331
itayzafrir40835d42018-08-02 13:14:17 +03002332exit:
2333 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002335 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 return( psa_hash_abort( operation ) );
2337 }
2338 else
2339 {
2340 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002341 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 }
2343}
2344
Gilles Peskine2d277862018-06-18 15:41:12 +02002345psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2346 const uint8_t *hash,
2347 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348{
2349 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2350 size_t actual_hash_length;
2351 psa_status_t status = psa_hash_finish( operation,
2352 actual_hash, sizeof( actual_hash ),
2353 &actual_hash_length );
2354 if( status != PSA_SUCCESS )
2355 return( status );
2356 if( actual_hash_length != hash_length )
2357 return( PSA_ERROR_INVALID_SIGNATURE );
2358 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2359 return( PSA_ERROR_INVALID_SIGNATURE );
2360 return( PSA_SUCCESS );
2361}
2362
Gilles Peskineeb35d782019-01-22 17:56:16 +01002363psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2364 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002365{
2366 if( target_operation->alg != 0 )
2367 return( PSA_ERROR_BAD_STATE );
2368
2369 switch( source_operation->alg )
2370 {
2371 case 0:
2372 return( PSA_ERROR_BAD_STATE );
2373#if defined(MBEDTLS_MD2_C)
2374 case PSA_ALG_MD2:
2375 mbedtls_md2_clone( &target_operation->ctx.md2,
2376 &source_operation->ctx.md2 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_MD4_C)
2380 case PSA_ALG_MD4:
2381 mbedtls_md4_clone( &target_operation->ctx.md4,
2382 &source_operation->ctx.md4 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_MD5_C)
2386 case PSA_ALG_MD5:
2387 mbedtls_md5_clone( &target_operation->ctx.md5,
2388 &source_operation->ctx.md5 );
2389 break;
2390#endif
2391#if defined(MBEDTLS_RIPEMD160_C)
2392 case PSA_ALG_RIPEMD160:
2393 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2394 &source_operation->ctx.ripemd160 );
2395 break;
2396#endif
2397#if defined(MBEDTLS_SHA1_C)
2398 case PSA_ALG_SHA_1:
2399 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2400 &source_operation->ctx.sha1 );
2401 break;
2402#endif
2403#if defined(MBEDTLS_SHA256_C)
2404 case PSA_ALG_SHA_224:
2405 case PSA_ALG_SHA_256:
2406 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2407 &source_operation->ctx.sha256 );
2408 break;
2409#endif
2410#if defined(MBEDTLS_SHA512_C)
2411 case PSA_ALG_SHA_384:
2412 case PSA_ALG_SHA_512:
2413 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2414 &source_operation->ctx.sha512 );
2415 break;
2416#endif
2417 default:
2418 return( PSA_ERROR_NOT_SUPPORTED );
2419 }
2420
2421 target_operation->alg = source_operation->alg;
2422 return( PSA_SUCCESS );
2423}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424
2425
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427/* MAC */
2428/****************************************************************/
2429
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002430static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431 psa_algorithm_t alg,
2432 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002433 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002434 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002437 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002439 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002440 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002441
Gilles Peskine8c9def32018-02-08 10:02:12 +01002442 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2443 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002444 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002446 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002447 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 mode = MBEDTLS_MODE_STREAM;
2449 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450 case PSA_ALG_CTR:
2451 mode = MBEDTLS_MODE_CTR;
2452 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002453 case PSA_ALG_CFB:
2454 mode = MBEDTLS_MODE_CFB;
2455 break;
2456 case PSA_ALG_OFB:
2457 mode = MBEDTLS_MODE_OFB;
2458 break;
2459 case PSA_ALG_CBC_NO_PADDING:
2460 mode = MBEDTLS_MODE_CBC;
2461 break;
2462 case PSA_ALG_CBC_PKCS7:
2463 mode = MBEDTLS_MODE_CBC;
2464 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002465 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466 mode = MBEDTLS_MODE_CCM;
2467 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002468 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469 mode = MBEDTLS_MODE_GCM;
2470 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002471 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2472 mode = MBEDTLS_MODE_CHACHAPOLY;
2473 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002474 default:
2475 return( NULL );
2476 }
2477 }
2478 else if( alg == PSA_ALG_CMAC )
2479 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 else
2481 return( NULL );
2482
2483 switch( key_type )
2484 {
2485 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002486 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487 break;
2488 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002489 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2490 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002494 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002495 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2496 * but two-key Triple-DES is functionally three-key Triple-DES
2497 * with K1=K3, so that's how we present it to mbedtls. */
2498 if( key_bits == 128 )
2499 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 break;
2501 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002502 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 break;
2504 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002505 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002507 case PSA_KEY_TYPE_CHACHA20:
2508 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2509 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 default:
2511 return( NULL );
2512 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002513 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002514 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515
Jaeden Amero23bbb752018-06-26 14:16:54 +01002516 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2517 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518}
2519
Gilles Peskinea05219c2018-11-16 16:02:56 +01002520#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002521static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002522{
Gilles Peskine2d277862018-06-18 15:41:12 +02002523 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002524 {
2525 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002526 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002527 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002528 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002529 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002530 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002531 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002532 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002533 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002534 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002535 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002536 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002537 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002538 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002539 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002540 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002541 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002542 return( 128 );
2543 default:
2544 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002545 }
2546}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002547#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002548
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002549/* Initialize the MAC operation structure. Once this function has been
2550 * called, psa_mac_abort can run and will do the right thing. */
2551static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2552 psa_algorithm_t alg )
2553{
2554 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2555
2556 operation->alg = alg;
2557 operation->key_set = 0;
2558 operation->iv_set = 0;
2559 operation->iv_required = 0;
2560 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002561 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562
2563#if defined(MBEDTLS_CMAC_C)
2564 if( alg == PSA_ALG_CMAC )
2565 {
2566 operation->iv_required = 0;
2567 mbedtls_cipher_init( &operation->ctx.cmac );
2568 status = PSA_SUCCESS;
2569 }
2570 else
2571#endif /* MBEDTLS_CMAC_C */
2572#if defined(MBEDTLS_MD_C)
2573 if( PSA_ALG_IS_HMAC( operation->alg ) )
2574 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002575 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2576 operation->ctx.hmac.hash_ctx.alg = 0;
2577 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002578 }
2579 else
2580#endif /* MBEDTLS_MD_C */
2581 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002582 if( ! PSA_ALG_IS_MAC( alg ) )
2583 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584 }
2585
2586 if( status != PSA_SUCCESS )
2587 memset( operation, 0, sizeof( *operation ) );
2588 return( status );
2589}
2590
Gilles Peskine01126fa2018-07-12 17:04:55 +02002591#if defined(MBEDTLS_MD_C)
2592static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2593{
Gilles Peskine3f108122018-12-07 18:14:53 +01002594 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002595 return( psa_hash_abort( &hmac->hash_ctx ) );
2596}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002597
Janos Follath999f6482019-06-11 12:04:10 +01002598#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002599static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2600{
2601 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2602 memset( hmac, 0, sizeof( *hmac ) );
2603}
Janos Follath999f6482019-06-11 12:04:10 +01002604#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002605#endif /* MBEDTLS_MD_C */
2606
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2608{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002609 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002611 /* The object has (apparently) been initialized but it is not
2612 * in use. It's ok to call abort on such an object, and there's
2613 * nothing to do. */
2614 return( PSA_SUCCESS );
2615 }
2616 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002618 if( operation->alg == PSA_ALG_CMAC )
2619 {
2620 mbedtls_cipher_free( &operation->ctx.cmac );
2621 }
2622 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625 if( PSA_ALG_IS_HMAC( operation->alg ) )
2626 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002627 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002628 }
2629 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002631 {
2632 /* Sanity check (shouldn't happen: operation->alg should
2633 * always have been initialized to a valid value). */
2634 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635 }
Moran Peker41deec42018-04-04 15:43:05 +03002636
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637 operation->alg = 0;
2638 operation->key_set = 0;
2639 operation->iv_set = 0;
2640 operation->iv_required = 0;
2641 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002642 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002643
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002645
2646bad_state:
2647 /* If abort is called on an uninitialized object, we can't trust
2648 * anything. Wipe the object in case it contains confidential data.
2649 * This may result in a memory leak if a pointer gets overwritten,
2650 * but it's too late to do anything about this. */
2651 memset( operation, 0, sizeof( *operation ) );
2652 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002653}
2654
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002655#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002656static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002657 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002658 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002659 const mbedtls_cipher_info_t *cipher_info )
2660{
2661 int ret;
2662
2663 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002664
2665 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2666 if( ret != 0 )
2667 return( ret );
2668
2669 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2670 slot->data.raw.data,
2671 key_bits );
2672 return( ret );
2673}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002674#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002675
Gilles Peskine248051a2018-06-20 16:09:38 +02002676#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002677static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2678 const uint8_t *key,
2679 size_t key_length,
2680 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002682 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002683 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002684 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002685 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002686 psa_status_t status;
2687
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002688 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2689 * overflow below. This should never trigger if the hash algorithm
2690 * is implemented correctly. */
2691 /* The size checks against the ipad and opad buffers cannot be written
2692 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2693 * because that triggers -Wlogical-op on GCC 7.3. */
2694 if( block_size > sizeof( ipad ) )
2695 return( PSA_ERROR_NOT_SUPPORTED );
2696 if( block_size > sizeof( hmac->opad ) )
2697 return( PSA_ERROR_NOT_SUPPORTED );
2698 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002699 return( PSA_ERROR_NOT_SUPPORTED );
2700
Gilles Peskined223b522018-06-11 18:12:58 +02002701 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002702 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002703 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002704 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002705 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002706 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002707 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002708 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002710 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002712 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 }
Gilles Peskine96889972018-07-12 17:07:03 +02002714 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2715 * but it is permitted. It is common when HMAC is used in HKDF, for
2716 * example. Don't call `memcpy` in the 0-length because `key` could be
2717 * an invalid pointer which would make the behavior undefined. */
2718 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002719 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002720
Gilles Peskined223b522018-06-11 18:12:58 +02002721 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2722 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002723 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002724 ipad[i] ^= 0x36;
2725 memset( ipad + key_length, 0x36, block_size - key_length );
2726
2727 /* Copy the key material from ipad to opad, flipping the requisite bits,
2728 * and filling the rest of opad with the requisite constant. */
2729 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002730 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2731 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732
Gilles Peskine01126fa2018-07-12 17:04:55 +02002733 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002735 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002736
Gilles Peskine01126fa2018-07-12 17:04:55 +02002737 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002738
2739cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002740 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002741
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002742 return( status );
2743}
Gilles Peskine248051a2018-06-20 16:09:38 +02002744#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002745
Gilles Peskine89167cb2018-07-08 20:12:23 +02002746static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002747 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002748 psa_algorithm_t alg,
2749 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002752 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002754 psa_key_usage_t usage =
2755 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002756 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002757 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002759 /* A context must be freshly initialized before it can be set up. */
2760 if( operation->alg != 0 )
2761 {
2762 return( PSA_ERROR_BAD_STATE );
2763 }
2764
Gilles Peskined911eb72018-08-14 15:18:45 +02002765 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002766 if( status != PSA_SUCCESS )
2767 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002768 if( is_sign )
2769 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770
Gilles Peskine28f8f302019-07-24 13:30:31 +02002771 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002772 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002774 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002775
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002777 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002778 {
2779 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002780 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002781 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002782 int ret;
2783 if( cipher_info == NULL )
2784 {
2785 status = PSA_ERROR_NOT_SUPPORTED;
2786 goto exit;
2787 }
2788 operation->mac_size = cipher_info->block_size;
2789 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2790 status = mbedtls_to_psa_error( ret );
2791 }
2792 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002795 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002796 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002797 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002798 if( hash_alg == 0 )
2799 {
2800 status = PSA_ERROR_NOT_SUPPORTED;
2801 goto exit;
2802 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002803
2804 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2805 /* Sanity check. This shouldn't fail on a valid configuration. */
2806 if( operation->mac_size == 0 ||
2807 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2808 {
2809 status = PSA_ERROR_NOT_SUPPORTED;
2810 goto exit;
2811 }
2812
Gilles Peskine8e338702019-07-30 20:06:31 +02002813 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 {
2815 status = PSA_ERROR_INVALID_ARGUMENT;
2816 goto exit;
2817 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002818
Gilles Peskine01126fa2018-07-12 17:04:55 +02002819 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2820 slot->data.raw.data,
2821 slot->data.raw.bytes,
2822 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 }
2824 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002826 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002827 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002828 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002830
Gilles Peskined911eb72018-08-14 15:18:45 +02002831 if( truncated == 0 )
2832 {
2833 /* The "normal" case: untruncated algorithm. Nothing to do. */
2834 }
2835 else if( truncated < 4 )
2836 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002837 /* A very short MAC is too short for security since it can be
2838 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2839 * so we make this our minimum, even though 32 bits is still
2840 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002841 status = PSA_ERROR_NOT_SUPPORTED;
2842 }
2843 else if( truncated > operation->mac_size )
2844 {
2845 /* It's impossible to "truncate" to a larger length. */
2846 status = PSA_ERROR_INVALID_ARGUMENT;
2847 }
2848 else
2849 operation->mac_size = truncated;
2850
Gilles Peskinefbfac682018-07-08 20:51:54 +02002851exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002852 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002854 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002856 else
2857 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002858 operation->key_set = 1;
2859 }
2860 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861}
2862
Gilles Peskine89167cb2018-07-08 20:12:23 +02002863psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002864 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002865 psa_algorithm_t alg )
2866{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002867 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002868}
2869
2870psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002871 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002872 psa_algorithm_t alg )
2873{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002874 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002875}
2876
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2878 const uint8_t *input,
2879 size_t input_length )
2880{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002881 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002882 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002883 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002884 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002885 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886 operation->has_input = 1;
2887
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002890 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002891 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2892 input, input_length );
2893 status = mbedtls_to_psa_error( ret );
2894 }
2895 else
2896#endif /* MBEDTLS_CMAC_C */
2897#if defined(MBEDTLS_MD_C)
2898 if( PSA_ALG_IS_HMAC( operation->alg ) )
2899 {
2900 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2901 input_length );
2902 }
2903 else
2904#endif /* MBEDTLS_MD_C */
2905 {
2906 /* This shouldn't happen if `operation` was initialized by
2907 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002908 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002909 }
2910
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 if( status != PSA_SUCCESS )
2912 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002913 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002914}
2915
Gilles Peskine01126fa2018-07-12 17:04:55 +02002916#if defined(MBEDTLS_MD_C)
2917static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2918 uint8_t *mac,
2919 size_t mac_size )
2920{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002921 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002922 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2923 size_t hash_size = 0;
2924 size_t block_size = psa_get_hash_block_size( hash_alg );
2925 psa_status_t status;
2926
Gilles Peskine01126fa2018-07-12 17:04:55 +02002927 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2928 if( status != PSA_SUCCESS )
2929 return( status );
2930 /* From here on, tmp needs to be wiped. */
2931
2932 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2933 if( status != PSA_SUCCESS )
2934 goto exit;
2935
2936 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2937 if( status != PSA_SUCCESS )
2938 goto exit;
2939
2940 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2941 if( status != PSA_SUCCESS )
2942 goto exit;
2943
Gilles Peskined911eb72018-08-14 15:18:45 +02002944 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2945 if( status != PSA_SUCCESS )
2946 goto exit;
2947
2948 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002949
2950exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002951 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002952 return( status );
2953}
2954#endif /* MBEDTLS_MD_C */
2955
mohammad16036df908f2018-04-02 08:34:15 -07002956static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002957 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002958 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002960 if( ! operation->key_set )
2961 return( PSA_ERROR_BAD_STATE );
2962 if( operation->iv_required && ! operation->iv_set )
2963 return( PSA_ERROR_BAD_STATE );
2964
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965 if( mac_size < operation->mac_size )
2966 return( PSA_ERROR_BUFFER_TOO_SMALL );
2967
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002969 if( operation->alg == PSA_ALG_CMAC )
2970 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002971 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2972 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2973 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002974 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002975 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002976 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002977 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002978 else
2979#endif /* MBEDTLS_CMAC_C */
2980#if defined(MBEDTLS_MD_C)
2981 if( PSA_ALG_IS_HMAC( operation->alg ) )
2982 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002983 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002984 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002985 }
2986 else
2987#endif /* MBEDTLS_MD_C */
2988 {
2989 /* This shouldn't happen if `operation` was initialized by
2990 * a setup function. */
2991 return( PSA_ERROR_BAD_STATE );
2992 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993}
2994
Gilles Peskineacd4be32018-07-08 19:56:25 +02002995psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2996 uint8_t *mac,
2997 size_t mac_size,
2998 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002999{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003000 psa_status_t status;
3001
Jaeden Amero252ef282019-02-15 14:05:35 +00003002 if( operation->alg == 0 )
3003 {
3004 return( PSA_ERROR_BAD_STATE );
3005 }
3006
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003007 /* Fill the output buffer with something that isn't a valid mac
3008 * (barring an attack on the mac and deliberately-crafted input),
3009 * in case the caller doesn't check the return status properly. */
3010 *mac_length = mac_size;
3011 /* If mac_size is 0 then mac may be NULL and then the
3012 * call to memset would have undefined behavior. */
3013 if( mac_size != 0 )
3014 memset( mac, '!', mac_size );
3015
Gilles Peskine89167cb2018-07-08 20:12:23 +02003016 if( ! operation->is_sign )
3017 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003018 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003019 }
mohammad16036df908f2018-04-02 08:34:15 -07003020
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003021 status = psa_mac_finish_internal( operation, mac, mac_size );
3022
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003023 if( status == PSA_SUCCESS )
3024 {
3025 status = psa_mac_abort( operation );
3026 if( status == PSA_SUCCESS )
3027 *mac_length = operation->mac_size;
3028 else
3029 memset( mac, '!', mac_size );
3030 }
3031 else
3032 psa_mac_abort( operation );
3033 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003034}
3035
Gilles Peskineacd4be32018-07-08 19:56:25 +02003036psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3037 const uint8_t *mac,
3038 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039{
Gilles Peskine828ed142018-06-18 23:25:51 +02003040 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003041 psa_status_t status;
3042
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 if( operation->alg == 0 )
3044 {
3045 return( PSA_ERROR_BAD_STATE );
3046 }
3047
Gilles Peskine89167cb2018-07-08 20:12:23 +02003048 if( operation->is_sign )
3049 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003050 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003051 }
3052 if( operation->mac_size != mac_length )
3053 {
3054 status = PSA_ERROR_INVALID_SIGNATURE;
3055 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003056 }
mohammad16036df908f2018-04-02 08:34:15 -07003057
3058 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003059 actual_mac, sizeof( actual_mac ) );
3060
3061 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3062 status = PSA_ERROR_INVALID_SIGNATURE;
3063
3064cleanup:
3065 if( status == PSA_SUCCESS )
3066 status = psa_mac_abort( operation );
3067 else
3068 psa_mac_abort( operation );
3069
Gilles Peskine3f108122018-12-07 18:14:53 +01003070 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003071
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003072 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073}
3074
3075
Gilles Peskine20035e32018-02-03 22:44:14 +01003076
Gilles Peskine20035e32018-02-03 22:44:14 +01003077/****************************************************************/
3078/* Asymmetric cryptography */
3079/****************************************************************/
3080
Gilles Peskine2b450e32018-06-27 15:42:46 +02003081#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003082/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003083 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003084static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3085 size_t hash_length,
3086 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003087{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003088 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003089 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003090 *md_alg = mbedtls_md_get_type( md_info );
3091
3092 /* The Mbed TLS RSA module uses an unsigned int for hash length
3093 * parameters. Validate that it fits so that we don't risk an
3094 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003095#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003096 if( hash_length > UINT_MAX )
3097 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003098#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003099
3100#if defined(MBEDTLS_PKCS1_V15)
3101 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3102 * must be correct. */
3103 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3104 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003105 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003106 if( md_info == NULL )
3107 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003108 if( mbedtls_md_get_size( md_info ) != hash_length )
3109 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003110 }
3111#endif /* MBEDTLS_PKCS1_V15 */
3112
3113#if defined(MBEDTLS_PKCS1_V21)
3114 /* PSS requires a hash internally. */
3115 if( PSA_ALG_IS_RSA_PSS( alg ) )
3116 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003117 if( md_info == NULL )
3118 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003119 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003120#endif /* MBEDTLS_PKCS1_V21 */
3121
Gilles Peskine61b91d42018-06-08 16:09:36 +02003122 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003123}
3124
Gilles Peskine2b450e32018-06-27 15:42:46 +02003125static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3126 psa_algorithm_t alg,
3127 const uint8_t *hash,
3128 size_t hash_length,
3129 uint8_t *signature,
3130 size_t signature_size,
3131 size_t *signature_length )
3132{
3133 psa_status_t status;
3134 int ret;
3135 mbedtls_md_type_t md_alg;
3136
3137 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3138 if( status != PSA_SUCCESS )
3139 return( status );
3140
Gilles Peskine630a18a2018-06-29 17:49:35 +02003141 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003142 return( PSA_ERROR_BUFFER_TOO_SMALL );
3143
3144#if defined(MBEDTLS_PKCS1_V15)
3145 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3146 {
3147 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3148 MBEDTLS_MD_NONE );
3149 ret = mbedtls_rsa_pkcs1_sign( rsa,
3150 mbedtls_ctr_drbg_random,
3151 &global_data.ctr_drbg,
3152 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003153 md_alg,
3154 (unsigned int) hash_length,
3155 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003156 signature );
3157 }
3158 else
3159#endif /* MBEDTLS_PKCS1_V15 */
3160#if defined(MBEDTLS_PKCS1_V21)
3161 if( PSA_ALG_IS_RSA_PSS( alg ) )
3162 {
3163 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3164 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3165 mbedtls_ctr_drbg_random,
3166 &global_data.ctr_drbg,
3167 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003168 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003169 (unsigned int) hash_length,
3170 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003171 signature );
3172 }
3173 else
3174#endif /* MBEDTLS_PKCS1_V21 */
3175 {
3176 return( PSA_ERROR_INVALID_ARGUMENT );
3177 }
3178
3179 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003180 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003181 return( mbedtls_to_psa_error( ret ) );
3182}
3183
3184static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3185 psa_algorithm_t alg,
3186 const uint8_t *hash,
3187 size_t hash_length,
3188 const uint8_t *signature,
3189 size_t signature_length )
3190{
3191 psa_status_t status;
3192 int ret;
3193 mbedtls_md_type_t md_alg;
3194
3195 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3196 if( status != PSA_SUCCESS )
3197 return( status );
3198
Gilles Peskine630a18a2018-06-29 17:49:35 +02003199 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003200 return( PSA_ERROR_BUFFER_TOO_SMALL );
3201
3202#if defined(MBEDTLS_PKCS1_V15)
3203 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3204 {
3205 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3206 MBEDTLS_MD_NONE );
3207 ret = mbedtls_rsa_pkcs1_verify( rsa,
3208 mbedtls_ctr_drbg_random,
3209 &global_data.ctr_drbg,
3210 MBEDTLS_RSA_PUBLIC,
3211 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003212 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003213 hash,
3214 signature );
3215 }
3216 else
3217#endif /* MBEDTLS_PKCS1_V15 */
3218#if defined(MBEDTLS_PKCS1_V21)
3219 if( PSA_ALG_IS_RSA_PSS( alg ) )
3220 {
3221 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3222 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3223 mbedtls_ctr_drbg_random,
3224 &global_data.ctr_drbg,
3225 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003226 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003227 (unsigned int) hash_length,
3228 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003229 signature );
3230 }
3231 else
3232#endif /* MBEDTLS_PKCS1_V21 */
3233 {
3234 return( PSA_ERROR_INVALID_ARGUMENT );
3235 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003236
3237 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3238 * the rest of the signature is invalid". This has little use in
3239 * practice and PSA doesn't report this distinction. */
3240 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3241 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003242 return( mbedtls_to_psa_error( ret ) );
3243}
3244#endif /* MBEDTLS_RSA_C */
3245
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003246#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003247/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3248 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3249 * (even though these functions don't modify it). */
3250static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3251 psa_algorithm_t alg,
3252 const uint8_t *hash,
3253 size_t hash_length,
3254 uint8_t *signature,
3255 size_t signature_size,
3256 size_t *signature_length )
3257{
3258 int ret;
3259 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003260 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003261 mbedtls_mpi_init( &r );
3262 mbedtls_mpi_init( &s );
3263
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003264 if( signature_size < 2 * curve_bytes )
3265 {
3266 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3267 goto cleanup;
3268 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003269
Gilles Peskinea05219c2018-11-16 16:02:56 +01003270#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003271 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3272 {
3273 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3274 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3275 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3276 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3277 hash, hash_length,
3278 md_alg ) );
3279 }
3280 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003281#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003282 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003283 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003284 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3285 hash, hash_length,
3286 mbedtls_ctr_drbg_random,
3287 &global_data.ctr_drbg ) );
3288 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003289
3290 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3291 signature,
3292 curve_bytes ) );
3293 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3294 signature + curve_bytes,
3295 curve_bytes ) );
3296
3297cleanup:
3298 mbedtls_mpi_free( &r );
3299 mbedtls_mpi_free( &s );
3300 if( ret == 0 )
3301 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003302 return( mbedtls_to_psa_error( ret ) );
3303}
3304
3305static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3306 const uint8_t *hash,
3307 size_t hash_length,
3308 const uint8_t *signature,
3309 size_t signature_length )
3310{
3311 int ret;
3312 mbedtls_mpi r, s;
3313 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3314 mbedtls_mpi_init( &r );
3315 mbedtls_mpi_init( &s );
3316
3317 if( signature_length != 2 * curve_bytes )
3318 return( PSA_ERROR_INVALID_SIGNATURE );
3319
3320 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3321 signature,
3322 curve_bytes ) );
3323 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3324 signature + curve_bytes,
3325 curve_bytes ) );
3326
3327 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3328 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003329
3330cleanup:
3331 mbedtls_mpi_free( &r );
3332 mbedtls_mpi_free( &s );
3333 return( mbedtls_to_psa_error( ret ) );
3334}
3335#endif /* MBEDTLS_ECDSA_C */
3336
Gilles Peskinec5487a82018-12-03 18:08:14 +01003337psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003338 psa_algorithm_t alg,
3339 const uint8_t *hash,
3340 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003341 uint8_t *signature,
3342 size_t signature_size,
3343 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003344{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003345 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003346 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3348 const psa_drv_se_t *drv;
3349 psa_drv_se_context_t *drv_context;
3350#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003351
3352 *signature_length = signature_size;
3353
Gilles Peskineedc64242019-08-07 21:05:07 +02003354 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003355 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003356 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003357 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003358 {
3359 status = PSA_ERROR_INVALID_ARGUMENT;
3360 goto exit;
3361 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003362
Gilles Peskineedc64242019-08-07 21:05:07 +02003363#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3364 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3365 {
3366 if( drv->asymmetric == NULL ||
3367 drv->asymmetric->p_sign == NULL )
3368 {
3369 status = PSA_ERROR_NOT_SUPPORTED;
3370 goto exit;
3371 }
3372 status = drv->asymmetric->p_sign( drv_context,
3373 slot->data.se.slot_number,
3374 alg,
3375 hash, hash_length,
3376 signature, signature_size,
3377 signature_length );
3378 }
3379 else
3380#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003381#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003382 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003383 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003384 status = psa_rsa_sign( slot->data.rsa,
3385 alg,
3386 hash, hash_length,
3387 signature, signature_size,
3388 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003389 }
3390 else
3391#endif /* defined(MBEDTLS_RSA_C) */
3392#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003393 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003394 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003395#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003396 if(
3397#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3398 PSA_ALG_IS_ECDSA( alg )
3399#else
3400 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3401#endif
3402 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003403 status = psa_ecdsa_sign( slot->data.ecp,
3404 alg,
3405 hash, hash_length,
3406 signature, signature_size,
3407 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003408 else
3409#endif /* defined(MBEDTLS_ECDSA_C) */
3410 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003412 }
itayzafrir5c753392018-05-08 11:18:38 +03003413 }
3414 else
3415#endif /* defined(MBEDTLS_ECP_C) */
3416 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003418 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003419
3420exit:
3421 /* Fill the unused part of the output buffer (the whole buffer on error,
3422 * the trailing part on success) with something that isn't a valid mac
3423 * (barring an attack on the mac and deliberately-crafted input),
3424 * in case the caller doesn't check the return status properly. */
3425 if( status == PSA_SUCCESS )
3426 memset( signature + *signature_length, '!',
3427 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003428 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003429 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003430 /* If signature_size is 0 then we have nothing to do. We must not call
3431 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003432 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003433}
3434
Gilles Peskinec5487a82018-12-03 18:08:14 +01003435psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003436 psa_algorithm_t alg,
3437 const uint8_t *hash,
3438 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003439 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003440 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003441{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003442 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003443 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003444#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3445 const psa_drv_se_t *drv;
3446 psa_drv_se_context_t *drv_context;
3447#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003448
Gilles Peskineedc64242019-08-07 21:05:07 +02003449 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003450 if( status != PSA_SUCCESS )
3451 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003452
Gilles Peskineedc64242019-08-07 21:05:07 +02003453#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3454 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3455 {
3456 if( drv->asymmetric == NULL ||
3457 drv->asymmetric->p_verify == NULL )
3458 return( PSA_ERROR_NOT_SUPPORTED );
3459 return( drv->asymmetric->p_verify( drv_context,
3460 slot->data.se.slot_number,
3461 alg,
3462 hash, hash_length,
3463 signature, signature_length ) );
3464 }
3465 else
3466#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003467#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003468 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003469 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003470 return( psa_rsa_verify( slot->data.rsa,
3471 alg,
3472 hash, hash_length,
3473 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003474 }
3475 else
3476#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003477#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003478 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003479 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003480#if defined(MBEDTLS_ECDSA_C)
3481 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003482 return( psa_ecdsa_verify( slot->data.ecp,
3483 hash, hash_length,
3484 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003485 else
3486#endif /* defined(MBEDTLS_ECDSA_C) */
3487 {
3488 return( PSA_ERROR_INVALID_ARGUMENT );
3489 }
itayzafrir5c753392018-05-08 11:18:38 +03003490 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003491 else
3492#endif /* defined(MBEDTLS_ECP_C) */
3493 {
3494 return( PSA_ERROR_NOT_SUPPORTED );
3495 }
3496}
3497
Gilles Peskine072ac562018-06-30 00:21:29 +02003498#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3499static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3500 mbedtls_rsa_context *rsa )
3501{
3502 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3503 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3504 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3505 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3506}
3507#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3508
Gilles Peskinec5487a82018-12-03 18:08:14 +01003509psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003510 psa_algorithm_t alg,
3511 const uint8_t *input,
3512 size_t input_length,
3513 const uint8_t *salt,
3514 size_t salt_length,
3515 uint8_t *output,
3516 size_t output_size,
3517 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003518{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003519 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003520 psa_status_t status;
3521
Darryl Green5cc689a2018-07-24 15:34:10 +01003522 (void) input;
3523 (void) input_length;
3524 (void) salt;
3525 (void) output;
3526 (void) output_size;
3527
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003528 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003530 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3531 return( PSA_ERROR_INVALID_ARGUMENT );
3532
Gilles Peskine28f8f302019-07-24 13:30:31 +02003533 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003534 if( status != PSA_SUCCESS )
3535 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003536 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3537 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539
3540#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003541 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 {
3543 mbedtls_rsa_context *rsa = slot->data.rsa;
3544 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003545 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003546 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003548 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003549 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003550 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3551 mbedtls_ctr_drbg_random,
3552 &global_data.ctr_drbg,
3553 MBEDTLS_RSA_PUBLIC,
3554 input_length,
3555 input,
3556 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 }
3558 else
3559#endif /* MBEDTLS_PKCS1_V15 */
3560#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003561 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003563 psa_rsa_oaep_set_padding_mode( alg, rsa );
3564 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3565 mbedtls_ctr_drbg_random,
3566 &global_data.ctr_drbg,
3567 MBEDTLS_RSA_PUBLIC,
3568 salt, salt_length,
3569 input_length,
3570 input,
3571 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572 }
3573 else
3574#endif /* MBEDTLS_PKCS1_V21 */
3575 {
3576 return( PSA_ERROR_INVALID_ARGUMENT );
3577 }
3578 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003579 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580 return( mbedtls_to_psa_error( ret ) );
3581 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003583#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584 {
3585 return( PSA_ERROR_NOT_SUPPORTED );
3586 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003587}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588
Gilles Peskinec5487a82018-12-03 18:08:14 +01003589psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003590 psa_algorithm_t alg,
3591 const uint8_t *input,
3592 size_t input_length,
3593 const uint8_t *salt,
3594 size_t salt_length,
3595 uint8_t *output,
3596 size_t output_size,
3597 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003599 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003600 psa_status_t status;
3601
Darryl Green5cc689a2018-07-24 15:34:10 +01003602 (void) input;
3603 (void) input_length;
3604 (void) salt;
3605 (void) output;
3606 (void) output_size;
3607
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003608 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003610 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3611 return( PSA_ERROR_INVALID_ARGUMENT );
3612
Gilles Peskine28f8f302019-07-24 13:30:31 +02003613 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003614 if( status != PSA_SUCCESS )
3615 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003616 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617 return( PSA_ERROR_INVALID_ARGUMENT );
3618
3619#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003620 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621 {
3622 mbedtls_rsa_context *rsa = slot->data.rsa;
3623 int ret;
3624
Gilles Peskine630a18a2018-06-29 17:49:35 +02003625 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003626 return( PSA_ERROR_INVALID_ARGUMENT );
3627
3628#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003629 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003631 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3632 mbedtls_ctr_drbg_random,
3633 &global_data.ctr_drbg,
3634 MBEDTLS_RSA_PRIVATE,
3635 output_length,
3636 input,
3637 output,
3638 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 }
3640 else
3641#endif /* MBEDTLS_PKCS1_V15 */
3642#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003643 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003645 psa_rsa_oaep_set_padding_mode( alg, rsa );
3646 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3647 mbedtls_ctr_drbg_random,
3648 &global_data.ctr_drbg,
3649 MBEDTLS_RSA_PRIVATE,
3650 salt, salt_length,
3651 output_length,
3652 input,
3653 output,
3654 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003655 }
3656 else
3657#endif /* MBEDTLS_PKCS1_V21 */
3658 {
3659 return( PSA_ERROR_INVALID_ARGUMENT );
3660 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003661
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662 return( mbedtls_to_psa_error( ret ) );
3663 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003665#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003666 {
3667 return( PSA_ERROR_NOT_SUPPORTED );
3668 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003669}
Gilles Peskine20035e32018-02-03 22:44:14 +01003670
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003671
3672
mohammad1603503973b2018-03-12 15:59:30 +02003673/****************************************************************/
3674/* Symmetric cryptography */
3675/****************************************************************/
3676
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003677/* Initialize the cipher operation structure. Once this function has been
3678 * called, psa_cipher_abort can run and will do the right thing. */
3679static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3680 psa_algorithm_t alg )
3681{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003682 if( ! PSA_ALG_IS_CIPHER( alg ) )
3683 {
3684 memset( operation, 0, sizeof( *operation ) );
3685 return( PSA_ERROR_INVALID_ARGUMENT );
3686 }
3687
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003688 operation->alg = alg;
3689 operation->key_set = 0;
3690 operation->iv_set = 0;
3691 operation->iv_required = 1;
3692 operation->iv_size = 0;
3693 operation->block_size = 0;
3694 mbedtls_cipher_init( &operation->ctx.cipher );
3695 return( PSA_SUCCESS );
3696}
3697
Gilles Peskinee553c652018-06-04 16:22:46 +02003698static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003699 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003700 psa_algorithm_t alg,
3701 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003702{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003703 int ret = 0;
3704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003705 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003706 size_t key_bits;
3707 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003708 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3709 PSA_KEY_USAGE_ENCRYPT :
3710 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003711
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003712 /* A context must be freshly initialized before it can be set up. */
3713 if( operation->alg != 0 )
3714 {
3715 return( PSA_ERROR_BAD_STATE );
3716 }
3717
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003718 status = psa_cipher_init( operation, alg );
3719 if( status != PSA_SUCCESS )
3720 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003721
Gilles Peskine28f8f302019-07-24 13:30:31 +02003722 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003723 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003724 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003725 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003726
Gilles Peskine8e338702019-07-30 20:06:31 +02003727 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003728 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003729 {
3730 status = PSA_ERROR_NOT_SUPPORTED;
3731 goto exit;
3732 }
mohammad1603503973b2018-03-12 15:59:30 +02003733
mohammad1603503973b2018-03-12 15:59:30 +02003734 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003735 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003736 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003737
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003738#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003739 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003740 {
3741 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003742 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003743 memcpy( keys, slot->data.raw.data, 16 );
3744 memcpy( keys + 16, slot->data.raw.data, 8 );
3745 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3746 keys,
3747 192, cipher_operation );
3748 }
3749 else
3750#endif
3751 {
3752 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3753 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003754 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003755 }
Moran Peker41deec42018-04-04 15:43:05 +03003756 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003757 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003758
mohammad16038481e742018-03-18 13:57:31 +02003759#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003760 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003761 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003762 case PSA_ALG_CBC_NO_PADDING:
3763 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3764 MBEDTLS_PADDING_NONE );
3765 break;
3766 case PSA_ALG_CBC_PKCS7:
3767 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3768 MBEDTLS_PADDING_PKCS7 );
3769 break;
3770 default:
3771 /* The algorithm doesn't involve padding. */
3772 ret = 0;
3773 break;
3774 }
3775 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003776 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003777#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3778
mohammad1603503973b2018-03-12 15:59:30 +02003779 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003780 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003781 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003782 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003783 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003784 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003785 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003786#if defined(MBEDTLS_CHACHA20_C)
3787 else
3788 if( alg == PSA_ALG_CHACHA20 )
3789 operation->iv_size = 12;
3790#endif
mohammad1603503973b2018-03-12 15:59:30 +02003791
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003792exit:
3793 if( status == 0 )
3794 status = mbedtls_to_psa_error( ret );
3795 if( status != 0 )
3796 psa_cipher_abort( operation );
3797 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003798}
3799
Gilles Peskinefe119512018-07-08 21:39:34 +02003800psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003801 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003802 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003803{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003804 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003805}
3806
Gilles Peskinefe119512018-07-08 21:39:34 +02003807psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003808 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003809 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003810{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003811 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003812}
3813
Gilles Peskinefe119512018-07-08 21:39:34 +02003814psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003815 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003816 size_t iv_size,
3817 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003818{
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 psa_status_t status;
3820 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003821 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003822 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003823 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003824 }
Moran Peker41deec42018-04-04 15:43:05 +03003825 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003826 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003827 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003828 goto exit;
3829 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003830 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3831 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003832 if( ret != 0 )
3833 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003834 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003835 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003836 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003837
mohammad16038481e742018-03-18 13:57:31 +02003838 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003840
Moran Peker395db872018-05-31 14:07:14 +03003841exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003842 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003843 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003844 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003845}
3846
Gilles Peskinefe119512018-07-08 21:39:34 +02003847psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003848 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003849 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003850{
itayzafrir534bd7c2018-08-02 13:56:32 +03003851 psa_status_t status;
3852 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003853 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003854 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003855 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003856 }
Moran Pekera28258c2018-05-29 16:25:04 +03003857 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003858 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003859 status = PSA_ERROR_INVALID_ARGUMENT;
3860 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003861 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003862 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3863 status = mbedtls_to_psa_error( ret );
3864exit:
3865 if( status == PSA_SUCCESS )
3866 operation->iv_set = 1;
3867 else
mohammad1603503973b2018-03-12 15:59:30 +02003868 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003869 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003870}
3871
Gilles Peskinee553c652018-06-04 16:22:46 +02003872psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3873 const uint8_t *input,
3874 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003875 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003876 size_t output_size,
3877 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003878{
itayzafrir534bd7c2018-08-02 13:56:32 +03003879 psa_status_t status;
3880 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003881 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003882
3883 if( operation->alg == 0 )
3884 {
3885 return( PSA_ERROR_BAD_STATE );
3886 }
3887
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003888 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003889 {
3890 /* Take the unprocessed partial block left over from previous
3891 * update calls, if any, plus the input to this call. Remove
3892 * the last partial block, if any. You get the data that will be
3893 * output in this call. */
3894 expected_output_size =
3895 ( operation->ctx.cipher.unprocessed_len + input_length )
3896 / operation->block_size * operation->block_size;
3897 }
3898 else
3899 {
3900 expected_output_size = input_length;
3901 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003902
Gilles Peskine89d789c2018-06-04 17:17:16 +02003903 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 {
3905 status = PSA_ERROR_BUFFER_TOO_SMALL;
3906 goto exit;
3907 }
mohammad160382759612018-03-12 18:16:40 +02003908
mohammad1603503973b2018-03-12 15:59:30 +02003909 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003910 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003911 status = mbedtls_to_psa_error( ret );
3912exit:
3913 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003914 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003916}
3917
Gilles Peskinee553c652018-06-04 16:22:46 +02003918psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3919 uint8_t *output,
3920 size_t output_size,
3921 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003922{
David Saadab4ecc272019-02-14 13:48:10 +02003923 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003924 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003925 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003926
mohammad1603503973b2018-03-12 15:59:30 +02003927 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003928 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003929 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003930 }
3931 if( operation->iv_required && ! operation->iv_set )
3932 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003933 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003934 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003935
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003936 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003937 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3938 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003939 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003940 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003941 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003942 }
3943
Janos Follath315b51c2018-07-09 16:04:51 +01003944 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3945 temp_output_buffer,
3946 output_length );
3947 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003948 {
Janos Follath315b51c2018-07-09 16:04:51 +01003949 status = mbedtls_to_psa_error( cipher_ret );
3950 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003951 }
Janos Follath315b51c2018-07-09 16:04:51 +01003952
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003953 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003954 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003955 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003956 memcpy( output, temp_output_buffer, *output_length );
3957 else
3958 {
Janos Follath315b51c2018-07-09 16:04:51 +01003959 status = PSA_ERROR_BUFFER_TOO_SMALL;
3960 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003961 }
mohammad1603503973b2018-03-12 15:59:30 +02003962
Gilles Peskine3f108122018-12-07 18:14:53 +01003963 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003964 status = psa_cipher_abort( operation );
3965
3966 return( status );
3967
3968error:
3969
3970 *output_length = 0;
3971
Gilles Peskine3f108122018-12-07 18:14:53 +01003972 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003973 (void) psa_cipher_abort( operation );
3974
3975 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003976}
3977
Gilles Peskinee553c652018-06-04 16:22:46 +02003978psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3979{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003980 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003981 {
3982 /* The object has (apparently) been initialized but it is not
3983 * in use. It's ok to call abort on such an object, and there's
3984 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003985 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003986 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003987
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003988 /* Sanity check (shouldn't happen: operation->alg should
3989 * always have been initialized to a valid value). */
3990 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3991 return( PSA_ERROR_BAD_STATE );
3992
mohammad1603503973b2018-03-12 15:59:30 +02003993 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003994
Moran Peker41deec42018-04-04 15:43:05 +03003995 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003996 operation->key_set = 0;
3997 operation->iv_set = 0;
3998 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003999 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004000 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004001
Moran Peker395db872018-05-31 14:07:14 +03004002 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004003}
4004
Gilles Peskinea0655c32018-04-30 17:06:50 +02004005
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004006
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004007
mohammad16035955c982018-04-26 00:53:03 +03004008/****************************************************************/
4009/* AEAD */
4010/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004011
Gilles Peskineedf9a652018-08-17 18:11:56 +02004012typedef struct
4013{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004014 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004015 const mbedtls_cipher_info_t *cipher_info;
4016 union
4017 {
4018#if defined(MBEDTLS_CCM_C)
4019 mbedtls_ccm_context ccm;
4020#endif /* MBEDTLS_CCM_C */
4021#if defined(MBEDTLS_GCM_C)
4022 mbedtls_gcm_context gcm;
4023#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004024#if defined(MBEDTLS_CHACHAPOLY_C)
4025 mbedtls_chachapoly_context chachapoly;
4026#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004027 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004028 psa_algorithm_t core_alg;
4029 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004030 uint8_t tag_length;
4031} aead_operation_t;
4032
Gilles Peskine30a9e412019-01-14 18:36:12 +01004033static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004034{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004035 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004036 {
4037#if defined(MBEDTLS_CCM_C)
4038 case PSA_ALG_CCM:
4039 mbedtls_ccm_free( &operation->ctx.ccm );
4040 break;
4041#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004042#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004043 case PSA_ALG_GCM:
4044 mbedtls_gcm_free( &operation->ctx.gcm );
4045 break;
4046#endif /* MBEDTLS_GCM_C */
4047 }
4048}
4049
4050static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004051 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052 psa_key_usage_t usage,
4053 psa_algorithm_t alg )
4054{
4055 psa_status_t status;
4056 size_t key_bits;
4057 mbedtls_cipher_id_t cipher_id;
4058
Gilles Peskine28f8f302019-07-24 13:30:31 +02004059 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 if( status != PSA_SUCCESS )
4061 return( status );
4062
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004063 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004064
4065 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004066 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004067 &cipher_id );
4068 if( operation->cipher_info == NULL )
4069 return( PSA_ERROR_NOT_SUPPORTED );
4070
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004071 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004072 {
4073#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004074 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4075 operation->core_alg = PSA_ALG_CCM;
4076 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004077 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4078 * The call to mbedtls_ccm_encrypt_and_tag or
4079 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004080 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004081 return( PSA_ERROR_INVALID_ARGUMENT );
4082 mbedtls_ccm_init( &operation->ctx.ccm );
4083 status = mbedtls_to_psa_error(
4084 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4085 operation->slot->data.raw.data,
4086 (unsigned int) key_bits ) );
4087 if( status != 0 )
4088 goto cleanup;
4089 break;
4090#endif /* MBEDTLS_CCM_C */
4091
4092#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004093 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4094 operation->core_alg = PSA_ALG_GCM;
4095 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004096 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4097 * The call to mbedtls_gcm_crypt_and_tag or
4098 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004099 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004100 return( PSA_ERROR_INVALID_ARGUMENT );
4101 mbedtls_gcm_init( &operation->ctx.gcm );
4102 status = mbedtls_to_psa_error(
4103 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4104 operation->slot->data.raw.data,
4105 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004106 if( status != 0 )
4107 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004108 break;
4109#endif /* MBEDTLS_GCM_C */
4110
Gilles Peskine26869f22019-05-06 15:25:00 +02004111#if defined(MBEDTLS_CHACHAPOLY_C)
4112 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4113 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4114 operation->full_tag_length = 16;
4115 /* We only support the default tag length. */
4116 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4117 return( PSA_ERROR_NOT_SUPPORTED );
4118 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4119 status = mbedtls_to_psa_error(
4120 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4121 operation->slot->data.raw.data ) );
4122 if( status != 0 )
4123 goto cleanup;
4124 break;
4125#endif /* MBEDTLS_CHACHAPOLY_C */
4126
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 default:
4128 return( PSA_ERROR_NOT_SUPPORTED );
4129 }
4130
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004131 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4132 {
4133 status = PSA_ERROR_INVALID_ARGUMENT;
4134 goto cleanup;
4135 }
4136 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004137
Gilles Peskineedf9a652018-08-17 18:11:56 +02004138 return( PSA_SUCCESS );
4139
4140cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004141 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004142 return( status );
4143}
4144
Gilles Peskinec5487a82018-12-03 18:08:14 +01004145psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004146 psa_algorithm_t alg,
4147 const uint8_t *nonce,
4148 size_t nonce_length,
4149 const uint8_t *additional_data,
4150 size_t additional_data_length,
4151 const uint8_t *plaintext,
4152 size_t plaintext_length,
4153 uint8_t *ciphertext,
4154 size_t ciphertext_size,
4155 size_t *ciphertext_length )
4156{
mohammad16035955c982018-04-26 00:53:03 +03004157 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004158 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004159 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004160
mohammad1603f08a5502018-06-03 15:05:47 +03004161 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004162
Gilles Peskinec5487a82018-12-03 18:08:14 +01004163 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004164 if( status != PSA_SUCCESS )
4165 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004166
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 /* For all currently supported modes, the tag is at the end of the
4168 * ciphertext. */
4169 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4170 {
4171 status = PSA_ERROR_BUFFER_TOO_SMALL;
4172 goto exit;
4173 }
4174 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004175
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004176#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004177 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004178 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004179 status = mbedtls_to_psa_error(
4180 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4181 MBEDTLS_GCM_ENCRYPT,
4182 plaintext_length,
4183 nonce, nonce_length,
4184 additional_data, additional_data_length,
4185 plaintext, ciphertext,
4186 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004187 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004188 else
4189#endif /* MBEDTLS_GCM_C */
4190#if defined(MBEDTLS_CCM_C)
4191 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004192 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004193 status = mbedtls_to_psa_error(
4194 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4195 plaintext_length,
4196 nonce, nonce_length,
4197 additional_data,
4198 additional_data_length,
4199 plaintext, ciphertext,
4200 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004201 }
mohammad16035c8845f2018-05-09 05:40:09 -07004202 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004203#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004204#if defined(MBEDTLS_CHACHAPOLY_C)
4205 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4206 {
4207 if( nonce_length != 12 || operation.tag_length != 16 )
4208 {
4209 status = PSA_ERROR_NOT_SUPPORTED;
4210 goto exit;
4211 }
4212 status = mbedtls_to_psa_error(
4213 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4214 plaintext_length,
4215 nonce,
4216 additional_data,
4217 additional_data_length,
4218 plaintext,
4219 ciphertext,
4220 tag ) );
4221 }
4222 else
4223#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004224 {
mohammad1603554faad2018-06-03 15:07:38 +03004225 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004226 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004227
Gilles Peskineedf9a652018-08-17 18:11:56 +02004228 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4229 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004230
Gilles Peskineedf9a652018-08-17 18:11:56 +02004231exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004232 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004233 if( status == PSA_SUCCESS )
4234 *ciphertext_length = plaintext_length + operation.tag_length;
4235 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004236}
4237
Gilles Peskineee652a32018-06-01 19:23:52 +02004238/* Locate the tag in a ciphertext buffer containing the encrypted data
4239 * followed by the tag. Return the length of the part preceding the tag in
4240 * *plaintext_length. This is the size of the plaintext in modes where
4241 * the encrypted data has the same size as the plaintext, such as
4242 * CCM and GCM. */
4243static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4244 const uint8_t *ciphertext,
4245 size_t ciphertext_length,
4246 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004247 const uint8_t **p_tag )
4248{
4249 size_t payload_length;
4250 if( tag_length > ciphertext_length )
4251 return( PSA_ERROR_INVALID_ARGUMENT );
4252 payload_length = ciphertext_length - tag_length;
4253 if( payload_length > plaintext_size )
4254 return( PSA_ERROR_BUFFER_TOO_SMALL );
4255 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004256 return( PSA_SUCCESS );
4257}
4258
Gilles Peskinec5487a82018-12-03 18:08:14 +01004259psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004260 psa_algorithm_t alg,
4261 const uint8_t *nonce,
4262 size_t nonce_length,
4263 const uint8_t *additional_data,
4264 size_t additional_data_length,
4265 const uint8_t *ciphertext,
4266 size_t ciphertext_length,
4267 uint8_t *plaintext,
4268 size_t plaintext_size,
4269 size_t *plaintext_length )
4270{
mohammad16035955c982018-04-26 00:53:03 +03004271 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004272 aead_operation_t operation;
4273 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004274
Gilles Peskineee652a32018-06-01 19:23:52 +02004275 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004276
Gilles Peskinec5487a82018-12-03 18:08:14 +01004277 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004278 if( status != PSA_SUCCESS )
4279 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004280
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004281 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4282 ciphertext, ciphertext_length,
4283 plaintext_size, &tag );
4284 if( status != PSA_SUCCESS )
4285 goto exit;
4286
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004287#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004288 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004289 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004290 status = mbedtls_to_psa_error(
4291 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4292 ciphertext_length - operation.tag_length,
4293 nonce, nonce_length,
4294 additional_data,
4295 additional_data_length,
4296 tag, operation.tag_length,
4297 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004298 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004299 else
4300#endif /* MBEDTLS_GCM_C */
4301#if defined(MBEDTLS_CCM_C)
4302 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004303 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004304 status = mbedtls_to_psa_error(
4305 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4306 ciphertext_length - operation.tag_length,
4307 nonce, nonce_length,
4308 additional_data,
4309 additional_data_length,
4310 ciphertext, plaintext,
4311 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004312 }
mohammad160339574652018-06-01 04:39:53 -07004313 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004314#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004315#if defined(MBEDTLS_CHACHAPOLY_C)
4316 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4317 {
4318 if( nonce_length != 12 || operation.tag_length != 16 )
4319 {
4320 status = PSA_ERROR_NOT_SUPPORTED;
4321 goto exit;
4322 }
4323 status = mbedtls_to_psa_error(
4324 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4325 ciphertext_length - operation.tag_length,
4326 nonce,
4327 additional_data,
4328 additional_data_length,
4329 tag,
4330 ciphertext,
4331 plaintext ) );
4332 }
4333 else
4334#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004335 {
mohammad1603554faad2018-06-03 15:07:38 +03004336 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004337 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004338
Gilles Peskineedf9a652018-08-17 18:11:56 +02004339 if( status != PSA_SUCCESS && plaintext_size != 0 )
4340 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004341
Gilles Peskineedf9a652018-08-17 18:11:56 +02004342exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004343 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004344 if( status == PSA_SUCCESS )
4345 *plaintext_length = ciphertext_length - operation.tag_length;
4346 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004347}
4348
Gilles Peskinea0655c32018-04-30 17:06:50 +02004349
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004350
Gilles Peskine20035e32018-02-03 22:44:14 +01004351/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004352/* Generators */
4353/****************************************************************/
4354
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004355#define HKDF_STATE_INIT 0 /* no input yet */
4356#define HKDF_STATE_STARTED 1 /* got salt */
4357#define HKDF_STATE_KEYED 2 /* got key */
4358#define HKDF_STATE_OUTPUT 3 /* output started */
4359
Gilles Peskinecbe66502019-05-16 16:59:18 +02004360static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004362{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4364 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004365 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004367}
4368
4369
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004371{
4372 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004374 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004375 {
4376 /* The object has (apparently) been initialized but it is not
4377 * in use. It's ok to call abort on such an object, and there's
4378 * nothing to do. */
4379 }
4380 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004381#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004382 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004383 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 mbedtls_free( operation->ctx.hkdf.info );
4385 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004386 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004387 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004389 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004390 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004391#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004393 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4395 operation->ctx.tls12_prf.key_len );
4396 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004397 }
Hanno Becker580fba12018-11-13 20:50:45 +00004398
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004400 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4402 operation->ctx.tls12_prf.Ai_with_seed_len );
4403 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004404 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004405#else
4406 if( operation->ctx.tls12_prf.seed != NULL )
4407 {
4408 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4409 operation->ctx.tls12_prf.seed_length );
4410 mbedtls_free( operation->ctx.tls12_prf.seed );
4411 }
4412
4413 if( operation->ctx.tls12_prf.label != NULL )
4414 {
4415 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4416 operation->ctx.tls12_prf.label_length );
4417 mbedtls_free( operation->ctx.tls12_prf.label );
4418 }
4419
4420 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4421
4422 /* We leave the fields Ai and output_block to be erased safely by the
4423 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004424#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004425 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004426 else
4427#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004428 {
4429 status = PSA_ERROR_BAD_STATE;
4430 }
Janos Follath083036a2019-06-11 10:22:26 +01004431 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004432 return( status );
4433}
4434
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004436 size_t *capacity)
4437{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004439 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004441 return PSA_ERROR_BAD_STATE;
4442 }
4443
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004445 return( PSA_SUCCESS );
4446}
4447
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004449 size_t capacity )
4450{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004452 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004454 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004456 return( PSA_SUCCESS );
4457}
4458
Gilles Peskinebef7f142018-07-12 17:22:21 +02004459#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004460/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004461 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004462static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004463 psa_algorithm_t hash_alg,
4464 uint8_t *output,
4465 size_t output_length )
4466{
4467 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4468 psa_status_t status;
4469
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004470 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4471 return( PSA_ERROR_BAD_STATE );
4472 hkdf->state = HKDF_STATE_OUTPUT;
4473
Gilles Peskinebef7f142018-07-12 17:22:21 +02004474 while( output_length != 0 )
4475 {
4476 /* Copy what remains of the current block */
4477 uint8_t n = hash_length - hkdf->offset_in_block;
4478 if( n > output_length )
4479 n = (uint8_t) output_length;
4480 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4481 output += n;
4482 output_length -= n;
4483 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004484 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004485 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004486 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004487 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004489 * object was corrupted or if this function is called directly
4490 * inside the library. */
4491 if( hkdf->block_number == 0xff )
4492 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004493
4494 /* We need a new block */
4495 ++hkdf->block_number;
4496 hkdf->offset_in_block = 0;
4497 status = psa_hmac_setup_internal( &hkdf->hmac,
4498 hkdf->prk, hash_length,
4499 hash_alg );
4500 if( status != PSA_SUCCESS )
4501 return( status );
4502 if( hkdf->block_number != 1 )
4503 {
4504 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4505 hkdf->output_block,
4506 hash_length );
4507 if( status != PSA_SUCCESS )
4508 return( status );
4509 }
4510 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4511 hkdf->info,
4512 hkdf->info_length );
4513 if( status != PSA_SUCCESS )
4514 return( status );
4515 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4516 &hkdf->block_number, 1 );
4517 if( status != PSA_SUCCESS )
4518 return( status );
4519 status = psa_hmac_finish_internal( &hkdf->hmac,
4520 hkdf->output_block,
4521 sizeof( hkdf->output_block ) );
4522 if( status != PSA_SUCCESS )
4523 return( status );
4524 }
4525
4526 return( PSA_SUCCESS );
4527}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004528
Janos Follath999f6482019-06-11 12:04:10 +01004529#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004530static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4531 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004532 psa_algorithm_t alg )
4533{
4534 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4535 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4536 psa_hmac_internal_data hmac;
4537 psa_status_t status, cleanup_status;
4538
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004539 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004540 size_t Ai_len;
4541
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004542 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004543 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004545 * object was corrupted or if this function is called directly
4546 * inside the library. */
4547 if( tls12_prf->block_number == 0xff )
4548 return( PSA_ERROR_BAD_STATE );
4549
4550 /* We need a new block */
4551 ++tls12_prf->block_number;
4552 tls12_prf->offset_in_block = 0;
4553
4554 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4555 *
4556 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4557 *
4558 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4559 * HMAC_hash(secret, A(2) + seed) +
4560 * HMAC_hash(secret, A(3) + seed) + ...
4561 *
4562 * A(0) = seed
4563 * A(i) = HMAC_hash( secret, A(i-1) )
4564 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004565 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004566 * `HMAC_hash(secret, A(i) + seed)` from which the output
4567 * is currently extracted as `output_block`, while
4568 * `A(i) + seed` is stored in `Ai_with_seed`.
4569 *
4570 * Generating a new block means recalculating `Ai_with_seed`
4571 * from the A(i)-part of it, and afterwards recalculating
4572 * `output_block`.
4573 *
4574 * A(0) is computed at setup time.
4575 *
4576 */
4577
4578 psa_hmac_init_internal( &hmac );
4579
4580 /* We must distinguish the calculation of A(1) from those
4581 * of A(2) and higher, because A(0)=seed has a different
4582 * length than the other A(i). */
4583 if( tls12_prf->block_number == 1 )
4584 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004585 Ai = tls12_prf->Ai_with_seed + hash_length;
4586 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004587 }
4588 else
4589 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004590 Ai = tls12_prf->Ai_with_seed;
4591 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004592 }
4593
Hanno Becker3b339e22018-11-13 20:56:14 +00004594 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4595 status = psa_hmac_setup_internal( &hmac,
4596 tls12_prf->key,
4597 tls12_prf->key_len,
4598 hash_alg );
4599 if( status != PSA_SUCCESS )
4600 goto cleanup;
4601
4602 status = psa_hash_update( &hmac.hash_ctx,
4603 Ai, Ai_len );
4604 if( status != PSA_SUCCESS )
4605 goto cleanup;
4606
4607 status = psa_hmac_finish_internal( &hmac,
4608 tls12_prf->Ai_with_seed,
4609 hash_length );
4610 if( status != PSA_SUCCESS )
4611 goto cleanup;
4612
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004613 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4614 status = psa_hmac_setup_internal( &hmac,
4615 tls12_prf->key,
4616 tls12_prf->key_len,
4617 hash_alg );
4618 if( status != PSA_SUCCESS )
4619 goto cleanup;
4620
4621 status = psa_hash_update( &hmac.hash_ctx,
4622 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004623 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004624 if( status != PSA_SUCCESS )
4625 goto cleanup;
4626
4627 status = psa_hmac_finish_internal( &hmac,
4628 tls12_prf->output_block,
4629 hash_length );
4630 if( status != PSA_SUCCESS )
4631 goto cleanup;
4632
4633cleanup:
4634
4635 cleanup_status = psa_hmac_abort_internal( &hmac );
4636 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4637 status = cleanup_status;
4638
4639 return( status );
4640}
Janos Follath7742fee2019-06-17 12:58:10 +01004641#else
4642static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4643 psa_tls12_prf_key_derivation_t *tls12_prf,
4644 psa_algorithm_t alg )
4645{
4646 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4647 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004648 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4649 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004650
Janos Follath7742fee2019-06-17 12:58:10 +01004651 /* We can't be wanting more output after block 0xff, otherwise
4652 * the capacity check in psa_key_derivation_output_bytes() would have
4653 * prevented this call. It could happen only if the operation
4654 * object was corrupted or if this function is called directly
4655 * inside the library. */
4656 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004657 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004658
4659 /* We need a new block */
4660 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004661 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004662
4663 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4664 *
4665 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4666 *
4667 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4668 * HMAC_hash(secret, A(2) + seed) +
4669 * HMAC_hash(secret, A(3) + seed) + ...
4670 *
4671 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004672 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004673 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004674 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004675 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004676 * is currently extracted as `output_block` and where i is
4677 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004678 */
4679
Janos Follathea29bfb2019-06-19 12:21:20 +01004680 /* Save the hash context before using it, to preserve the hash state with
4681 * only the inner padding in it. We need this, because inner padding depends
4682 * on the key (secret in the RFC's terminology). */
4683 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4684 if( status != PSA_SUCCESS )
4685 goto cleanup;
4686
4687 /* Calculate A(i) where i = tls12_prf->block_number. */
4688 if( tls12_prf->block_number == 1 )
4689 {
4690 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4691 * the variable seed and in this instance means it in the context of the
4692 * P_hash function, where seed = label + seed.) */
4693 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4694 tls12_prf->label, tls12_prf->label_length );
4695 if( status != PSA_SUCCESS )
4696 goto cleanup;
4697 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4698 tls12_prf->seed, tls12_prf->seed_length );
4699 if( status != PSA_SUCCESS )
4700 goto cleanup;
4701 }
4702 else
4703 {
4704 /* A(i) = HMAC_hash(secret, A(i-1)) */
4705 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4706 tls12_prf->Ai, hash_length );
4707 if( status != PSA_SUCCESS )
4708 goto cleanup;
4709 }
4710
4711 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4712 tls12_prf->Ai, hash_length );
4713 if( status != PSA_SUCCESS )
4714 goto cleanup;
4715 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4716 if( status != PSA_SUCCESS )
4717 goto cleanup;
4718
4719 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4720 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4721 tls12_prf->Ai, hash_length );
4722 if( status != PSA_SUCCESS )
4723 goto cleanup;
4724 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4725 tls12_prf->label, tls12_prf->label_length );
4726 if( status != PSA_SUCCESS )
4727 goto cleanup;
4728 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4729 tls12_prf->seed, tls12_prf->seed_length );
4730 if( status != PSA_SUCCESS )
4731 goto cleanup;
4732 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4733 tls12_prf->output_block, hash_length );
4734 if( status != PSA_SUCCESS )
4735 goto cleanup;
4736 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4737 if( status != PSA_SUCCESS )
4738 goto cleanup;
4739
Janos Follath7742fee2019-06-17 12:58:10 +01004740
4741cleanup:
4742
Janos Follathea29bfb2019-06-19 12:21:20 +01004743 cleanup_status = psa_hash_abort( &backup );
4744 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4745 status = cleanup_status;
4746
4747 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004748}
Janos Follath999f6482019-06-11 12:04:10 +01004749#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004750
Janos Follath999f6482019-06-11 12:04:10 +01004751#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004753 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004754static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004755 psa_tls12_prf_key_derivation_t *tls12_prf,
4756 psa_algorithm_t alg,
4757 uint8_t *output,
4758 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004759{
4760 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4761 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4762 psa_status_t status;
4763
4764 while( output_length != 0 )
4765 {
4766 /* Copy what remains of the current block */
4767 uint8_t n = hash_length - tls12_prf->offset_in_block;
4768
4769 /* Check if we have fully processed the current block. */
4770 if( n == 0 )
4771 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004772 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004773 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004774 if( status != PSA_SUCCESS )
4775 return( status );
4776
4777 continue;
4778 }
4779
4780 if( n > output_length )
4781 n = (uint8_t) output_length;
4782 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4783 n );
4784 output += n;
4785 output_length -= n;
4786 tls12_prf->offset_in_block += n;
4787 }
4788
4789 return( PSA_SUCCESS );
4790}
Janos Follath844eb0e2019-06-19 12:10:49 +01004791#else
4792static psa_status_t psa_key_derivation_tls12_prf_read(
4793 psa_tls12_prf_key_derivation_t *tls12_prf,
4794 psa_algorithm_t alg,
4795 uint8_t *output,
4796 size_t output_length )
4797{
4798 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4799 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4800 psa_status_t status;
4801 uint8_t offset, length;
4802
4803 while( output_length != 0 )
4804 {
4805 /* Check if we have fully processed the current block. */
4806 if( tls12_prf->left_in_block == 0 )
4807 {
4808 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4809 alg );
4810 if( status != PSA_SUCCESS )
4811 return( status );
4812
4813 continue;
4814 }
4815
4816 if( tls12_prf->left_in_block > output_length )
4817 length = (uint8_t) output_length;
4818 else
4819 length = tls12_prf->left_in_block;
4820
4821 offset = hash_length - tls12_prf->left_in_block;
4822 memcpy( output, tls12_prf->output_block + offset, length );
4823 output += length;
4824 output_length -= length;
4825 tls12_prf->left_in_block -= length;
4826 }
4827
4828 return( PSA_SUCCESS );
4829}
Janos Follath999f6482019-06-11 12:04:10 +01004830#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004831#endif /* MBEDTLS_MD_C */
4832
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004833psa_status_t psa_key_derivation_output_bytes(
4834 psa_key_derivation_operation_t *operation,
4835 uint8_t *output,
4836 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004837{
4838 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004840
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004841 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004842 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004844 return PSA_ERROR_BAD_STATE;
4845 }
4846
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004848 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004850 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004852 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004853 goto exit;
4854 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004856 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004857 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004858 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004859 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4860 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004862 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004863 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004864 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004866
Gilles Peskinebef7f142018-07-12 17:22:21 +02004867#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004868 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004869 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004870 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004872 output, output_length );
4873 }
Janos Follathadbec812019-06-14 11:05:39 +01004874 else
Janos Follathadbec812019-06-14 11:05:39 +01004875 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004876 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004877 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004879 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004880 output_length );
4881 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004882 else
4883#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004884 {
4885 return( PSA_ERROR_BAD_STATE );
4886 }
4887
4888exit:
4889 if( status != PSA_SUCCESS )
4890 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004891 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004892 * This allows us to differentiate between exhausted operations and
4893 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4894 * operations. */
4895 psa_algorithm_t alg = operation->alg;
4896 psa_key_derivation_abort( operation );
4897 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004898 memset( output, '!', output_length );
4899 }
4900 return( status );
4901}
4902
Gilles Peskine08542d82018-07-19 17:05:42 +02004903#if defined(MBEDTLS_DES_C)
4904static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4905{
4906 if( data_size >= 8 )
4907 mbedtls_des_key_set_parity( data );
4908 if( data_size >= 16 )
4909 mbedtls_des_key_set_parity( data + 8 );
4910 if( data_size >= 24 )
4911 mbedtls_des_key_set_parity( data + 16 );
4912}
4913#endif /* MBEDTLS_DES_C */
4914
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004915static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004916 psa_key_slot_t *slot,
4917 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004919{
4920 uint8_t *data = NULL;
4921 size_t bytes = PSA_BITS_TO_BYTES( bits );
4922 psa_status_t status;
4923
Gilles Peskine8e338702019-07-30 20:06:31 +02004924 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004925 return( PSA_ERROR_INVALID_ARGUMENT );
4926 if( bits % 8 != 0 )
4927 return( PSA_ERROR_INVALID_ARGUMENT );
4928 data = mbedtls_calloc( 1, bytes );
4929 if( data == NULL )
4930 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4931
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004933 if( status != PSA_SUCCESS )
4934 goto exit;
4935#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004936 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004937 psa_des_set_key_parity( data, bytes );
4938#endif /* MBEDTLS_DES_C */
4939 status = psa_import_key_into_slot( slot, data, bytes );
4940
4941exit:
4942 mbedtls_free( data );
4943 return( status );
4944}
4945
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004946psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004948 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004949{
4950 psa_status_t status;
4951 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004952 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004953 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4954 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004955#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4956 if( driver != NULL )
4957 {
4958 /* Deriving a key in a secure element is not implemented yet. */
4959 status = PSA_ERROR_NOT_SUPPORTED;
4960 }
4961#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004962 if( status == PSA_SUCCESS )
4963 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004964 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004965 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004967 }
4968 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004969 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004970 if( status != PSA_SUCCESS )
4971 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004972 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004973 *handle = 0;
4974 }
4975 return( status );
4976}
4977
Gilles Peskineeab56e42018-07-12 17:12:33 +02004978
4979
4980/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004981/* Key derivation */
4982/****************************************************************/
4983
Gilles Peskinea05219c2018-11-16 16:02:56 +01004984#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004985#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004987 * of the HKDF algorithm.
4988 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004989 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004990 * to potentially free embedded data structures and wipe confidential data.
4991 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004992static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004993 const uint8_t *secret,
4994 size_t secret_length,
4995 psa_algorithm_t hash_alg,
4996 const uint8_t *salt,
4997 size_t salt_length,
4998 const uint8_t *label,
4999 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005000{
5001 psa_status_t status;
5002 status = psa_hmac_setup_internal( &hkdf->hmac,
5003 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005004 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005005 if( status != PSA_SUCCESS )
5006 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005007 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005008 if( status != PSA_SUCCESS )
5009 return( status );
5010 status = psa_hmac_finish_internal( &hkdf->hmac,
5011 hkdf->prk,
5012 sizeof( hkdf->prk ) );
5013 if( status != PSA_SUCCESS )
5014 return( status );
5015 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5016 hkdf->block_number = 0;
5017 hkdf->info_length = label_length;
5018 if( label_length != 0 )
5019 {
5020 hkdf->info = mbedtls_calloc( 1, label_length );
5021 if( hkdf->info == NULL )
5022 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5023 memcpy( hkdf->info, label, label_length );
5024 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025 hkdf->state = HKDF_STATE_KEYED;
5026 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005027 return( PSA_SUCCESS );
5028}
Janos Follath71a4c912019-06-11 09:14:47 +01005029#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005030#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005031
Gilles Peskinea05219c2018-11-16 16:02:56 +01005032#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01005033#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01005035 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005036 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005037 * to potentially free embedded data structures and wipe confidential data.
5038 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005039static psa_status_t psa_key_derivation_tls12_prf_setup(
5040 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005041 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005042 size_t key_len,
5043 psa_algorithm_t hash_alg,
5044 const uint8_t *salt,
5045 size_t salt_length,
5046 const uint8_t *label,
5047 size_t label_length )
5048{
5049 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00005050 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
5051 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005052
5053 tls12_prf->key = mbedtls_calloc( 1, key_len );
5054 if( tls12_prf->key == NULL )
5055 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5056 tls12_prf->key_len = key_len;
5057 memcpy( tls12_prf->key, key, key_len );
5058
Hanno Becker580fba12018-11-13 20:50:45 +00005059 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005060 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00005061 if( overflow )
5062 return( PSA_ERROR_INVALID_ARGUMENT );
5063
5064 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
5065 if( tls12_prf->Ai_with_seed == NULL )
5066 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5067 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
5068
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005069 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
5070 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00005071 if( label_length != 0 )
5072 {
5073 memcpy( tls12_prf->Ai_with_seed + hash_length,
5074 label, label_length );
5075 }
5076
5077 if( salt_length != 0 )
5078 {
5079 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
5080 salt, salt_length );
5081 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005082
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005083 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005084 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005085 tls12_prf->block_number = 0;
5086 tls12_prf->offset_in_block = hash_length;
5087
5088 return( PSA_SUCCESS );
5089}
Janos Follath71a4c912019-06-11 09:14:47 +01005090#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00005091
Janos Follath71a4c912019-06-11 09:14:47 +01005092#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005094static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
5095 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005096 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005097 size_t psk_len,
5098 psa_algorithm_t hash_alg,
5099 const uint8_t *salt,
5100 size_t salt_length,
5101 const uint8_t *label,
5102 size_t label_length )
5103{
5104 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005105 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00005106
5107 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5108 return( PSA_ERROR_INVALID_ARGUMENT );
5109
5110 /* Quoting RFC 4279, Section 2:
5111 *
5112 * The premaster secret is formed as follows: if the PSK is N octets
5113 * long, concatenate a uint16 with the value N, N zero octets, a second
5114 * uint16 with the value N, and the PSK itself.
5115 */
5116
5117 pms[0] = ( psk_len >> 8 ) & 0xff;
5118 pms[1] = ( psk_len >> 0 ) & 0xff;
5119 memset( pms + 2, 0, psk_len );
5120 pms[2 + psk_len + 0] = pms[0];
5121 pms[2 + psk_len + 1] = pms[1];
5122 memcpy( pms + 4 + psk_len, psk, psk_len );
5123
Gilles Peskinecbe66502019-05-16 16:59:18 +02005124 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005125 pms, 4 + 2 * psk_len,
5126 hash_alg,
5127 salt, salt_length,
5128 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005129
Gilles Peskine3f108122018-12-07 18:14:53 +01005130 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005131 return( status );
5132}
Janos Follath71a4c912019-06-11 09:14:47 +01005133#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005134#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005135
Janos Follath71a4c912019-06-11 09:14:47 +01005136#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005137/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005138 * to potentially free embedded data structures and wipe confidential data.
5139 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005140static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005141 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005142 const uint8_t *secret, size_t secret_length,
5143 psa_algorithm_t alg,
5144 const uint8_t *salt, size_t salt_length,
5145 const uint8_t *label, size_t label_length,
5146 size_t capacity )
5147{
5148 psa_status_t status;
5149 size_t max_capacity;
5150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 /* Set operation->alg even on failure so that abort knows what to do. */
5152 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005153
5154#if defined(MBEDTLS_MD_C)
5155 if( PSA_ALG_IS_HKDF( alg ) )
5156 {
5157 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5158 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5159 if( hash_size == 0 )
5160 return( PSA_ERROR_NOT_SUPPORTED );
5161 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005163 secret, secret_length,
5164 hash_alg,
5165 salt, salt_length,
5166 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005167 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005168 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5169 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5170 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005171 {
5172 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5173 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5174
5175 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5176 if( hash_alg != PSA_ALG_SHA_256 &&
5177 hash_alg != PSA_ALG_SHA_384 )
5178 {
5179 return( PSA_ERROR_NOT_SUPPORTED );
5180 }
5181
5182 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005183
5184 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5185 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005187 secret, secret_length,
5188 hash_alg, salt, salt_length,
5189 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005190 }
5191 else
5192 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005193 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005195 secret, secret_length,
5196 hash_alg, salt, salt_length,
5197 label, label_length );
5198 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005199 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005200 else
5201#endif
5202 {
5203 return( PSA_ERROR_NOT_SUPPORTED );
5204 }
5205
5206 if( status != PSA_SUCCESS )
5207 return( status );
5208
5209 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005211 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005213 else
5214 return( PSA_ERROR_INVALID_ARGUMENT );
5215
5216 return( PSA_SUCCESS );
5217}
Janos Follath71a4c912019-06-11 09:14:47 +01005218#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005219
Janos Follath71a4c912019-06-11 09:14:47 +01005220#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005222 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005223 psa_algorithm_t alg,
5224 const uint8_t *salt,
5225 size_t salt_length,
5226 const uint8_t *label,
5227 size_t label_length,
5228 size_t capacity )
5229{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005230 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005231 psa_status_t status;
5232
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005234 return( PSA_ERROR_BAD_STATE );
5235
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005236 /* Make sure that alg is a key derivation algorithm. This prevents
5237 * key selection algorithms, which psa_key_derivation_internal
5238 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005239 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5240 return( PSA_ERROR_INVALID_ARGUMENT );
5241
Gilles Peskine28f8f302019-07-24 13:30:31 +02005242 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005243 if( status != PSA_SUCCESS )
5244 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005245
Gilles Peskine8e338702019-07-30 20:06:31 +02005246 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005247 return( PSA_ERROR_INVALID_ARGUMENT );
5248
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005250 slot->data.raw.data,
5251 slot->data.raw.bytes,
5252 alg,
5253 salt, salt_length,
5254 label, label_length,
5255 capacity );
5256 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005258 return( status );
5259}
Janos Follath71a4c912019-06-11 09:14:47 +01005260#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005261
Gilles Peskine969c5d62019-01-16 15:53:06 +01005262static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005264 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005265{
Janos Follath5fe19732019-06-20 15:09:30 +01005266 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5267 * initialisers for this union leave some bytes unspecified.) */
5268 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5269
Gilles Peskine969c5d62019-01-16 15:53:06 +01005270 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5273 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5274 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005276 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005277 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5278 if( hash_size == 0 )
5279 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005280 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5281 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005282 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 {
5284 return( PSA_ERROR_NOT_SUPPORTED );
5285 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005287 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 }
5289#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005290 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005291 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292}
5293
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005295 psa_algorithm_t alg )
5296{
5297 psa_status_t status;
5298
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300 return( PSA_ERROR_BAD_STATE );
5301
Gilles Peskine6843c292019-01-18 16:44:49 +01005302 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5303 return( PSA_ERROR_INVALID_ARGUMENT );
5304 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005305 {
5306 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005308 }
5309 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5310 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005312 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313 else
5314 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005315
5316 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319}
5320
5321#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005322static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323 psa_algorithm_t hash_alg,
5324 psa_key_derivation_step_t step,
5325 const uint8_t *data,
5326 size_t data_length )
5327{
5328 psa_status_t status;
5329 switch( step )
5330 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005331 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005332 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005333 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005334 status = psa_hmac_setup_internal( &hkdf->hmac,
5335 data, data_length,
5336 hash_alg );
5337 if( status != PSA_SUCCESS )
5338 return( status );
5339 hkdf->state = HKDF_STATE_STARTED;
5340 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005341 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005342 /* If no salt was provided, use an empty salt. */
5343 if( hkdf->state == HKDF_STATE_INIT )
5344 {
5345 status = psa_hmac_setup_internal( &hkdf->hmac,
5346 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005347 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005348 if( status != PSA_SUCCESS )
5349 return( status );
5350 hkdf->state = HKDF_STATE_STARTED;
5351 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005352 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005353 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005354 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5355 data, data_length );
5356 if( status != PSA_SUCCESS )
5357 return( status );
5358 status = psa_hmac_finish_internal( &hkdf->hmac,
5359 hkdf->prk,
5360 sizeof( hkdf->prk ) );
5361 if( status != PSA_SUCCESS )
5362 return( status );
5363 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5364 hkdf->block_number = 0;
5365 hkdf->state = HKDF_STATE_KEYED;
5366 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005367 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005368 if( hkdf->state == HKDF_STATE_OUTPUT )
5369 return( PSA_ERROR_BAD_STATE );
5370 if( hkdf->info_set )
5371 return( PSA_ERROR_BAD_STATE );
5372 hkdf->info_length = data_length;
5373 if( data_length != 0 )
5374 {
5375 hkdf->info = mbedtls_calloc( 1, data_length );
5376 if( hkdf->info == NULL )
5377 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5378 memcpy( hkdf->info, data, data_length );
5379 }
5380 hkdf->info_set = 1;
5381 return( PSA_SUCCESS );
5382 default:
5383 return( PSA_ERROR_INVALID_ARGUMENT );
5384 }
5385}
Janos Follathb03233e2019-06-11 15:30:30 +01005386
5387#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5388static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5389 psa_algorithm_t hash_alg,
5390 psa_key_derivation_step_t step,
5391 const uint8_t *data,
5392 size_t data_length )
5393{
5394 (void) prf;
5395 (void) hash_alg;
5396 (void) step;
5397 (void) data;
5398 (void) data_length;
5399
5400 return( PSA_ERROR_INVALID_ARGUMENT );
5401}
Janos Follath6660f0e2019-06-17 08:44:03 +01005402
5403static psa_status_t psa_tls12_prf_psk_to_ms_input(
5404 psa_tls12_prf_key_derivation_t *prf,
5405 psa_algorithm_t hash_alg,
5406 psa_key_derivation_step_t step,
5407 const uint8_t *data,
5408 size_t data_length )
5409{
5410 (void) prf;
5411 (void) hash_alg;
5412 (void) step;
5413 (void) data;
5414 (void) data_length;
5415
5416 return( PSA_ERROR_INVALID_ARGUMENT );
5417}
Janos Follathb03233e2019-06-11 15:30:30 +01005418#else
Janos Follathf08e2652019-06-13 09:05:41 +01005419static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5420 const uint8_t *data,
5421 size_t data_length )
5422{
5423 if( prf->state != TLS12_PRF_STATE_INIT )
5424 return( PSA_ERROR_BAD_STATE );
5425
Janos Follathd6dce9f2019-07-04 09:11:38 +01005426 if( data_length != 0 )
5427 {
5428 prf->seed = mbedtls_calloc( 1, data_length );
5429 if( prf->seed == NULL )
5430 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005431
Janos Follathd6dce9f2019-07-04 09:11:38 +01005432 memcpy( prf->seed, data, data_length );
5433 prf->seed_length = data_length;
5434 }
Janos Follathf08e2652019-06-13 09:05:41 +01005435
5436 prf->state = TLS12_PRF_STATE_SEED_SET;
5437
5438 return( PSA_SUCCESS );
5439}
5440
Janos Follath81550542019-06-13 14:26:34 +01005441static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5442 psa_algorithm_t hash_alg,
5443 const uint8_t *data,
5444 size_t data_length )
5445{
5446 psa_status_t status;
5447 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5448 return( PSA_ERROR_BAD_STATE );
5449
5450 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5451 if( status != PSA_SUCCESS )
5452 return( status );
5453
5454 prf->state = TLS12_PRF_STATE_KEY_SET;
5455
5456 return( PSA_SUCCESS );
5457}
5458
Janos Follath6660f0e2019-06-17 08:44:03 +01005459static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5460 psa_tls12_prf_key_derivation_t *prf,
5461 psa_algorithm_t hash_alg,
5462 const uint8_t *data,
5463 size_t data_length )
5464{
5465 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005466 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5467 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005468
5469 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5470 return( PSA_ERROR_INVALID_ARGUMENT );
5471
5472 /* Quoting RFC 4279, Section 2:
5473 *
5474 * The premaster secret is formed as follows: if the PSK is N octets
5475 * long, concatenate a uint16 with the value N, N zero octets, a second
5476 * uint16 with the value N, and the PSK itself.
5477 */
5478
Janos Follath40e13932019-06-26 13:22:29 +01005479 *cur++ = ( data_length >> 8 ) & 0xff;
5480 *cur++ = ( data_length >> 0 ) & 0xff;
5481 memset( cur, 0, data_length );
5482 cur += data_length;
5483 *cur++ = pms[0];
5484 *cur++ = pms[1];
5485 memcpy( cur, data, data_length );
5486 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005487
Janos Follath40e13932019-06-26 13:22:29 +01005488 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005489
5490 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5491 return( status );
5492}
5493
Janos Follath63028dd2019-06-13 09:15:47 +01005494static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5495 const uint8_t *data,
5496 size_t data_length )
5497{
5498 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5499 return( PSA_ERROR_BAD_STATE );
5500
Janos Follathd6dce9f2019-07-04 09:11:38 +01005501 if( data_length != 0 )
5502 {
5503 prf->label = mbedtls_calloc( 1, data_length );
5504 if( prf->label == NULL )
5505 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005506
Janos Follathd6dce9f2019-07-04 09:11:38 +01005507 memcpy( prf->label, data, data_length );
5508 prf->label_length = data_length;
5509 }
Janos Follath63028dd2019-06-13 09:15:47 +01005510
5511 prf->state = TLS12_PRF_STATE_LABEL_SET;
5512
5513 return( PSA_SUCCESS );
5514}
5515
Janos Follathb03233e2019-06-11 15:30:30 +01005516static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5517 psa_algorithm_t hash_alg,
5518 psa_key_derivation_step_t step,
5519 const uint8_t *data,
5520 size_t data_length )
5521{
Janos Follathb03233e2019-06-11 15:30:30 +01005522 switch( step )
5523 {
Janos Follathf08e2652019-06-13 09:05:41 +01005524 case PSA_KEY_DERIVATION_INPUT_SEED:
5525 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005526 case PSA_KEY_DERIVATION_INPUT_SECRET:
5527 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005528 case PSA_KEY_DERIVATION_INPUT_LABEL:
5529 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005530 default:
5531 return( PSA_ERROR_INVALID_ARGUMENT );
5532 }
5533}
Janos Follath6660f0e2019-06-17 08:44:03 +01005534
5535static psa_status_t psa_tls12_prf_psk_to_ms_input(
5536 psa_tls12_prf_key_derivation_t *prf,
5537 psa_algorithm_t hash_alg,
5538 psa_key_derivation_step_t step,
5539 const uint8_t *data,
5540 size_t data_length )
5541{
5542 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005543 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005544 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5545 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005546 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005547
5548 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5549}
Janos Follathb03233e2019-06-11 15:30:30 +01005550#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005551#endif /* MBEDTLS_MD_C */
5552
Janos Follathb80a94e2019-06-12 15:54:46 +01005553static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005554 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005555 psa_key_derivation_step_t step,
5556 const uint8_t *data,
5557 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005558{
5559 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005561
5562#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005563 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005564 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005566 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005567 step, data, data_length );
5568 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005569 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005572 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573 {
Janos Follathb03233e2019-06-11 15:30:30 +01005574 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5575 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5576 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005577 }
5578 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5579 {
5580 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5581 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5582 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005583 }
5584 else
5585#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005586 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005588 return( PSA_ERROR_BAD_STATE );
5589 }
5590
5591 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005593 return( status );
5594}
5595
Janos Follath51f4a0f2019-06-14 11:35:55 +01005596psa_status_t psa_key_derivation_input_bytes(
5597 psa_key_derivation_operation_t *operation,
5598 psa_key_derivation_step_t step,
5599 const uint8_t *data,
5600 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005601{
Janos Follathc5621512019-06-14 11:27:57 +01005602 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5603 return( PSA_ERROR_INVALID_ARGUMENT );
5604
5605 return( psa_key_derivation_input_internal( operation, step,
5606 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005607}
5608
Janos Follath51f4a0f2019-06-14 11:35:55 +01005609psa_status_t psa_key_derivation_input_key(
5610 psa_key_derivation_operation_t *operation,
5611 psa_key_derivation_step_t step,
5612 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005613{
5614 psa_key_slot_t *slot;
5615 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005616 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005617 PSA_KEY_USAGE_DERIVE,
5618 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005619 if( status != PSA_SUCCESS )
5620 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005621 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005622 return( PSA_ERROR_INVALID_ARGUMENT );
5623 /* Don't allow a key to be used as an input that is usually public.
5624 * This is debatable. It's ok from a cryptographic perspective to
5625 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005626 * the material should be dedicated to a particular input step,
5627 * otherwise this may allow the key to be used in an unintended way
5628 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005629 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005630 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005631 return( psa_key_derivation_input_internal( operation,
5632 step,
5633 slot->data.raw.data,
5634 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005635}
5636
Gilles Peskineea0fb492018-07-12 17:17:20 +02005637
5638
5639/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005640/* Key agreement */
5641/****************************************************************/
5642
Gilles Peskinea05219c2018-11-16 16:02:56 +01005643#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005644static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5645 size_t peer_key_length,
5646 const mbedtls_ecp_keypair *our_key,
5647 uint8_t *shared_secret,
5648 size_t shared_secret_size,
5649 size_t *shared_secret_length )
5650{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005651 mbedtls_ecp_keypair *their_key = NULL;
5652 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005653 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005654 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005655
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005656 status = psa_import_ec_public_key(
5657 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5658 peer_key, peer_key_length,
5659 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005660 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005661 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005662
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005663 status = mbedtls_to_psa_error(
5664 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5665 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005666 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005667 status = mbedtls_to_psa_error(
5668 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5669 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005670 goto exit;
5671
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005672 status = mbedtls_to_psa_error(
5673 mbedtls_ecdh_calc_secret( &ecdh,
5674 shared_secret_length,
5675 shared_secret, shared_secret_size,
5676 mbedtls_ctr_drbg_random,
5677 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005678
5679exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005680 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005681 mbedtls_ecp_keypair_free( their_key );
5682 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005683 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005684}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005685#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005686
Gilles Peskine01d718c2018-09-18 12:01:02 +02005687#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5688
Gilles Peskine0216fe12019-04-11 21:23:21 +02005689static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5690 psa_key_slot_t *private_key,
5691 const uint8_t *peer_key,
5692 size_t peer_key_length,
5693 uint8_t *shared_secret,
5694 size_t shared_secret_size,
5695 size_t *shared_secret_length )
5696{
5697 switch( alg )
5698 {
5699#if defined(MBEDTLS_ECDH_C)
5700 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005701 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005702 return( PSA_ERROR_INVALID_ARGUMENT );
5703 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5704 private_key->data.ecp,
5705 shared_secret, shared_secret_size,
5706 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005707#endif /* MBEDTLS_ECDH_C */
5708 default:
5709 (void) private_key;
5710 (void) peer_key;
5711 (void) peer_key_length;
5712 (void) shared_secret;
5713 (void) shared_secret_size;
5714 (void) shared_secret_length;
5715 return( PSA_ERROR_NOT_SUPPORTED );
5716 }
5717}
5718
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005719/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005720 * to potentially free embedded data structures and wipe confidential data.
5721 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005723 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005724 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005725 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005726 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005727{
5728 psa_status_t status;
5729 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5730 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005732
5733 /* Step 1: run the secret agreement algorithm to generate the shared
5734 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005735 status = psa_key_agreement_raw_internal( ka_alg,
5736 private_key,
5737 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005738 shared_secret,
5739 sizeof( shared_secret ),
5740 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005741 if( status != PSA_SUCCESS )
5742 goto exit;
5743
5744 /* Step 2: set up the key derivation to generate key material from
5745 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005746 status = psa_key_derivation_input_internal( operation, step,
5747 shared_secret,
5748 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005749
Gilles Peskine01d718c2018-09-18 12:01:02 +02005750exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005751 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005752 return( status );
5753}
5754
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005756 psa_key_derivation_step_t step,
5757 psa_key_handle_t private_key,
5758 const uint8_t *peer_key,
5759 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005760{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005761 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005762 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005763 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005764 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005765 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005766 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005767 if( status != PSA_SUCCESS )
5768 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005769 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005770 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005771 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005772 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005773 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005774 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005775}
5776
Gilles Peskinebe697d82019-05-16 18:00:41 +02005777psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5778 psa_key_handle_t private_key,
5779 const uint8_t *peer_key,
5780 size_t peer_key_length,
5781 uint8_t *output,
5782 size_t output_size,
5783 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005784{
5785 psa_key_slot_t *slot;
5786 psa_status_t status;
5787
5788 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5789 {
5790 status = PSA_ERROR_INVALID_ARGUMENT;
5791 goto exit;
5792 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005793 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005794 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005795 if( status != PSA_SUCCESS )
5796 goto exit;
5797
5798 status = psa_key_agreement_raw_internal( alg, slot,
5799 peer_key, peer_key_length,
5800 output, output_size,
5801 output_length );
5802
5803exit:
5804 if( status != PSA_SUCCESS )
5805 {
5806 /* If an error happens and is not handled properly, the output
5807 * may be used as a key to protect sensitive data. Arrange for such
5808 * a key to be random, which is likely to result in decryption or
5809 * verification errors. This is better than filling the buffer with
5810 * some constant data such as zeros, which would result in the data
5811 * being protected with a reproducible, easily knowable key.
5812 */
5813 psa_generate_random( output, output_size );
5814 *output_length = output_size;
5815 }
5816 return( status );
5817}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005818
5819
5820/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005821/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005822/****************************************************************/
5823
5824psa_status_t psa_generate_random( uint8_t *output,
5825 size_t output_size )
5826{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005827 int ret;
5828 GUARD_MODULE_INITIALIZED;
5829
Gilles Peskinef181eca2019-08-07 13:49:00 +02005830 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5831 {
5832 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5833 output,
5834 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5835 if( ret != 0 )
5836 return( mbedtls_to_psa_error( ret ) );
5837 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5838 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5839 }
5840
itayzafrir0adf0fc2018-09-06 16:24:41 +03005841 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005842 return( mbedtls_to_psa_error( ret ) );
5843}
5844
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005845#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5846#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005847
Gilles Peskine7228da22019-07-15 11:06:15 +02005848psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005849 size_t seed_size )
5850{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005851 if( global_data.initialized )
5852 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005853
5854 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5855 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5856 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5857 return( PSA_ERROR_INVALID_ARGUMENT );
5858
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005859 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005860}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005861#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005862
Gilles Peskinee56e8782019-04-26 17:34:02 +02005863#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5864static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5865 size_t domain_parameters_size,
5866 int *exponent )
5867{
5868 size_t i;
5869 uint32_t acc = 0;
5870
5871 if( domain_parameters_size == 0 )
5872 {
5873 *exponent = 65537;
5874 return( PSA_SUCCESS );
5875 }
5876
5877 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5878 * support values that fit in a 32-bit integer, which is larger than
5879 * int on just about every platform anyway. */
5880 if( domain_parameters_size > sizeof( acc ) )
5881 return( PSA_ERROR_NOT_SUPPORTED );
5882 for( i = 0; i < domain_parameters_size; i++ )
5883 acc = ( acc << 8 ) | domain_parameters[i];
5884 if( acc > INT_MAX )
5885 return( PSA_ERROR_NOT_SUPPORTED );
5886 *exponent = acc;
5887 return( PSA_SUCCESS );
5888}
5889#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5890
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005891static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005892 psa_key_slot_t *slot, size_t bits,
5893 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005894{
Gilles Peskine8e338702019-07-30 20:06:31 +02005895 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005896
Gilles Peskinee56e8782019-04-26 17:34:02 +02005897 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005898 return( PSA_ERROR_INVALID_ARGUMENT );
5899
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005900 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005901 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005902 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005903 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005904 if( status != PSA_SUCCESS )
5905 return( status );
5906 status = psa_generate_random( slot->data.raw.data,
5907 slot->data.raw.bytes );
5908 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005909 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005910#if defined(MBEDTLS_DES_C)
5911 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005912 psa_des_set_key_parity( slot->data.raw.data,
5913 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005914#endif /* MBEDTLS_DES_C */
5915 }
5916 else
5917
5918#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005919 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005920 {
5921 mbedtls_rsa_context *rsa;
5922 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005923 int exponent;
5924 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005925 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5926 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005927 /* Accept only byte-aligned keys, for the same reasons as
5928 * in psa_import_rsa_key(). */
5929 if( bits % 8 != 0 )
5930 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005931 status = psa_read_rsa_exponent( domain_parameters,
5932 domain_parameters_size,
5933 &exponent );
5934 if( status != PSA_SUCCESS )
5935 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005936 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5937 if( rsa == NULL )
5938 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5939 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5940 ret = mbedtls_rsa_gen_key( rsa,
5941 mbedtls_ctr_drbg_random,
5942 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005943 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005944 exponent );
5945 if( ret != 0 )
5946 {
5947 mbedtls_rsa_free( rsa );
5948 mbedtls_free( rsa );
5949 return( mbedtls_to_psa_error( ret ) );
5950 }
5951 slot->data.rsa = rsa;
5952 }
5953 else
5954#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5955
5956#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005957 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005958 {
5959 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5960 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5961 const mbedtls_ecp_curve_info *curve_info =
5962 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5963 mbedtls_ecp_keypair *ecp;
5964 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005965 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005966 return( PSA_ERROR_NOT_SUPPORTED );
5967 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5968 return( PSA_ERROR_NOT_SUPPORTED );
5969 if( curve_info->bit_size != bits )
5970 return( PSA_ERROR_INVALID_ARGUMENT );
5971 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5972 if( ecp == NULL )
5973 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5974 mbedtls_ecp_keypair_init( ecp );
5975 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5976 mbedtls_ctr_drbg_random,
5977 &global_data.ctr_drbg );
5978 if( ret != 0 )
5979 {
5980 mbedtls_ecp_keypair_free( ecp );
5981 mbedtls_free( ecp );
5982 return( mbedtls_to_psa_error( ret ) );
5983 }
5984 slot->data.ecp = ecp;
5985 }
5986 else
5987#endif /* MBEDTLS_ECP_C */
5988
5989 return( PSA_ERROR_NOT_SUPPORTED );
5990
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005991 return( PSA_SUCCESS );
5992}
5993
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005994psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005995 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005996{
5997 psa_status_t status;
5998 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005999 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006000
Gilles Peskinedf179142019-07-15 22:02:14 +02006001 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6002 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006003 if( status != PSA_SUCCESS )
6004 goto exit;
6005
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006006#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6007 if( driver != NULL )
6008 {
Gilles Peskine11792082019-08-06 18:36:36 +02006009 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6010 size_t pubkey_length = 0; /* We don't support this feature yet */
6011 if( drv->key_management == NULL ||
6012 drv->key_management->p_generate == NULL )
6013 {
6014 status = PSA_ERROR_NOT_SUPPORTED;
6015 goto exit;
6016 }
6017 status = drv->key_management->p_generate(
6018 psa_get_se_driver_context( driver ),
6019 slot->data.se.slot_number, attributes,
6020 NULL, 0, &pubkey_length );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006021 }
Gilles Peskine11792082019-08-06 18:36:36 +02006022 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006024 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006025 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006026 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006027 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006028 }
Gilles Peskine11792082019-08-06 18:36:36 +02006029
6030exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006031 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006032 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006033 if( status != PSA_SUCCESS )
6034 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006035 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006036 *handle = 0;
6037 }
6038 return( status );
6039}
6040
6041
Gilles Peskine05d69892018-06-19 22:00:52 +02006042
6043/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01006044/* Module setup */
6045/****************************************************************/
6046
Gilles Peskine5e769522018-11-20 21:59:56 +01006047psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6048 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6049 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6050{
6051 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6052 return( PSA_ERROR_BAD_STATE );
6053 global_data.entropy_init = entropy_init;
6054 global_data.entropy_free = entropy_free;
6055 return( PSA_SUCCESS );
6056}
6057
Gilles Peskinee59236f2018-01-27 23:32:46 +01006058void mbedtls_psa_crypto_free( void )
6059{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006060 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006061 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6062 {
6063 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006064 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006065 }
6066 /* Wipe all remaining data, including configuration.
6067 * In particular, this sets all state indicator to the value
6068 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006069 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006070#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006071 /* Unregister all secure element drivers, so that we restart from
6072 * a pristine state. */
6073 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006074#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006075}
6076
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006077#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6078/** Recover a transaction that was interrupted by a power failure.
6079 *
6080 * This function is called during initialization, before psa_crypto_init()
6081 * returns. If this function returns a failure status, the initialization
6082 * fails.
6083 */
6084static psa_status_t psa_crypto_recover_transaction(
6085 const psa_crypto_transaction_t *transaction )
6086{
6087 switch( transaction->unknown.type )
6088 {
6089 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6090 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
6091 /* TOnogrepDO - fall through to the failure case until this
6092 * is implemented */
6093 default:
6094 /* We found an unsupported transaction in the storage.
6095 * We don't know what state the storage is in. Give up. */
6096 return( PSA_ERROR_STORAGE_FAILURE );
6097 }
6098}
6099#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6100
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101psa_status_t psa_crypto_init( void )
6102{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006103 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 const unsigned char drbg_seed[] = "PSA";
6105
Gilles Peskinec6b69072018-11-20 21:42:52 +01006106 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006107 if( global_data.initialized != 0 )
6108 return( PSA_SUCCESS );
6109
Gilles Peskine5e769522018-11-20 21:59:56 +01006110 /* Set default configuration if
6111 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6112 if( global_data.entropy_init == NULL )
6113 global_data.entropy_init = mbedtls_entropy_init;
6114 if( global_data.entropy_free == NULL )
6115 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006116
Gilles Peskinec6b69072018-11-20 21:42:52 +01006117 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006118 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006119#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6120 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6121 /* The PSA entropy injection feature depends on using NV seed as an entropy
6122 * source. Add NV seed as an entropy source for PSA entropy injection. */
6123 mbedtls_entropy_add_source( &global_data.entropy,
6124 mbedtls_nv_seed_poll, NULL,
6125 MBEDTLS_ENTROPY_BLOCK_SIZE,
6126 MBEDTLS_ENTROPY_SOURCE_STRONG );
6127#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006128 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006129 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006130 status = mbedtls_to_psa_error(
6131 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6132 mbedtls_entropy_func,
6133 &global_data.entropy,
6134 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6135 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006136 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006137 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006138
Gilles Peskine66fb1262018-12-10 16:29:04 +01006139 status = psa_initialize_key_slots( );
6140 if( status != PSA_SUCCESS )
6141 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006142
Gilles Peskine4b734222019-07-24 15:56:31 +02006143#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006144 status = psa_crypto_load_transaction( );
6145 if( status == PSA_SUCCESS )
6146 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006147 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6148 if( status != PSA_SUCCESS )
6149 goto exit;
6150 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006151 }
6152 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6153 {
6154 /* There's no transaction to complete. It's all good. */
6155 status = PSA_SUCCESS;
6156 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006157#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006158
Gilles Peskinec6b69072018-11-20 21:42:52 +01006159 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006160 global_data.initialized = 1;
6161
Gilles Peskinee59236f2018-01-27 23:32:46 +01006162exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006163 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006164 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006165 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006166}
6167
6168#endif /* MBEDTLS_PSA_CRYPTO_C */