blob: 87ac037b603f3a468b85e865178116b4ffbbac34 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
597static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000772 {
773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000777 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 else
779#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200792
793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000800 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100801}
802
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100815 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
958 {
959 /* No key material to clean. */
960 }
961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100997static void psa_abort_operations_using_key( psa_key_slot_t *slot )
998{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200999 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001000 (void) slot;
1001}
1002
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003/** Completely wipe a slot in memory, including its policy.
1004 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001005psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006{
1007 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001008 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001009 /* At this point, key material and other type-specific content has
1010 * been wiped. Clear remaining metadata. We can call memset and not
1011 * zeroize because the metadata is not particularly sensitive. */
1012 memset( slot, 0, sizeof( *slot ) );
1013 return( status );
1014}
1015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001018 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001019 psa_status_t status = PSA_SUCCESS;
1020 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001021#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1022 psa_se_drv_table_entry_t *driver;
1023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024
Gilles Peskinec5487a82018-12-03 18:08:14 +01001025 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001026 if( status != PSA_SUCCESS )
1027 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
1029#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001031 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001033 /* For a key in a secure element, we need to do three things:
1034 * remove the key file in internal storage, destroy the
1035 * key inside the secure element, and update the driver's
1036 * persistent data. Start a transaction that will encompass these
1037 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001041 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 status = psa_crypto_save_transaction( );
1043 if( status != PSA_SUCCESS )
1044 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001045 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 /* TOnogrepDO: destroy what can be destroyed anyway */
1047 return( status );
1048 }
1049
Gilles Peskine354f7672019-07-12 23:46:38 +02001050 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001055 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001057 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001058 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001059 }
1060#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001061
Gilles Peskinefc762652019-07-22 19:30:34 +02001062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1063 if( driver != NULL )
1064 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001065 psa_status_t status2;
1066 status = psa_save_se_persistent_data( driver );
1067 status2 = psa_crypto_stop_transaction( );
1068 if( status == PSA_SUCCESS )
1069 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001070 if( status != PSA_SUCCESS )
1071 {
1072 /* TOnogrepDO: destroy what can be destroyed anyway */
1073 return( status );
1074 }
1075 }
1076#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1077
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001079 if( status != PSA_SUCCESS )
1080 return( status );
1081 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082}
1083
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001084void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1085{
Gilles Peskineb699f072019-04-26 16:06:02 +02001086 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087 memset( attributes, 0, sizeof( *attributes ) );
1088}
1089
Gilles Peskineb699f072019-04-26 16:06:02 +02001090psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1091 psa_key_type_t type,
1092 const uint8_t *data,
1093 size_t data_length )
1094{
1095 uint8_t *copy = NULL;
1096
1097 if( data_length != 0 )
1098 {
1099 copy = mbedtls_calloc( 1, data_length );
1100 if( copy == NULL )
1101 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1102 memcpy( copy, data, data_length );
1103 }
1104 /* After this point, this function is guaranteed to succeed, so it
1105 * can start modifying `*attributes`. */
1106
1107 if( attributes->domain_parameters != NULL )
1108 {
1109 mbedtls_free( attributes->domain_parameters );
1110 attributes->domain_parameters = NULL;
1111 attributes->domain_parameters_size = 0;
1112 }
1113
1114 attributes->domain_parameters = copy;
1115 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001116 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001117 return( PSA_SUCCESS );
1118}
1119
1120psa_status_t psa_get_key_domain_parameters(
1121 const psa_key_attributes_t *attributes,
1122 uint8_t *data, size_t data_size, size_t *data_length )
1123{
1124 if( attributes->domain_parameters_size > data_size )
1125 return( PSA_ERROR_BUFFER_TOO_SMALL );
1126 *data_length = attributes->domain_parameters_size;
1127 if( attributes->domain_parameters_size != 0 )
1128 memcpy( data, attributes->domain_parameters,
1129 attributes->domain_parameters_size );
1130 return( PSA_SUCCESS );
1131}
1132
1133#if defined(MBEDTLS_RSA_C)
1134static psa_status_t psa_get_rsa_public_exponent(
1135 const mbedtls_rsa_context *rsa,
1136 psa_key_attributes_t *attributes )
1137{
1138 mbedtls_mpi mpi;
1139 int ret;
1140 uint8_t *buffer = NULL;
1141 size_t buflen;
1142 mbedtls_mpi_init( &mpi );
1143
1144 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1145 if( ret != 0 )
1146 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001147 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1148 {
1149 /* It's the default value, which is reported as an empty string,
1150 * so there's nothing to do. */
1151 goto exit;
1152 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001153
1154 buflen = mbedtls_mpi_size( &mpi );
1155 buffer = mbedtls_calloc( 1, buflen );
1156 if( buffer == NULL )
1157 {
1158 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1159 goto exit;
1160 }
1161 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1162 if( ret != 0 )
1163 goto exit;
1164 attributes->domain_parameters = buffer;
1165 attributes->domain_parameters_size = buflen;
1166
1167exit:
1168 mbedtls_mpi_free( &mpi );
1169 if( ret != 0 )
1170 mbedtls_free( buffer );
1171 return( mbedtls_to_psa_error( ret ) );
1172}
1173#endif /* MBEDTLS_RSA_C */
1174
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001175/** Retrieve all the publicly-accessible attributes of a key.
1176 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1178 psa_key_attributes_t *attributes )
1179{
1180 psa_key_slot_t *slot;
1181 psa_status_t status;
1182
1183 psa_reset_key_attributes( attributes );
1184
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001185 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001186 if( status != PSA_SUCCESS )
1187 return( status );
1188
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001189 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001190 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1191 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1192
1193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1194 if( psa_key_slot_is_external( slot ) )
1195 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001197
Gilles Peskine8e338702019-07-30 20:06:31 +02001198 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001199 {
1200#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001201 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001202 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1204 /* TOnogrepDO: reporting the public exponent for opaque keys
1205 * is not yet implemented. */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001206 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001207 break;
1208#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1210 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001211#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 default:
1213 /* Nothing else to do. */
1214 break;
1215 }
1216
1217 if( status != PSA_SUCCESS )
1218 psa_reset_key_attributes( attributes );
1219 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001220}
1221
Gilles Peskinec8000c02019-08-02 20:15:51 +02001222#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1223psa_status_t psa_get_key_slot_number(
1224 const psa_key_attributes_t *attributes,
1225 psa_key_slot_number_t *slot_number )
1226{
1227 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1228 {
1229 *slot_number = attributes->slot_number;
1230 return( PSA_SUCCESS );
1231 }
1232 else
1233 return( PSA_ERROR_INVALID_ARGUMENT );
1234}
1235#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1236
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001237#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001238static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1239 unsigned char *buf, size_t size )
1240{
1241 int ret;
1242 unsigned char *c;
1243 size_t len = 0;
1244
1245 c = buf + size;
1246
1247 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1248
1249 return( (int) len );
1250}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001251#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001252
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001253static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1254 uint8_t *data,
1255 size_t data_size,
1256 size_t *data_length,
1257 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258{
Gilles Peskine5d309672019-07-12 23:47:28 +02001259#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1260 const psa_drv_se_t *drv;
1261 psa_drv_se_context_t *drv_context;
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1263
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001264 *data_length = 0;
1265
Gilles Peskine8e338702019-07-30 20:06:31 +02001266 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001267 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001268
Gilles Peskine5d309672019-07-12 23:47:28 +02001269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001270 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001271 {
1272 psa_drv_se_export_key_t method;
1273 if( drv->key_management == NULL )
1274 return( PSA_ERROR_NOT_SUPPORTED );
1275 method = ( export_public_key ?
1276 drv->key_management->p_export_public :
1277 drv->key_management->p_export );
1278 if( method == NULL )
1279 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001280 return( method( drv_context,
1281 slot->data.se.slot_number,
1282 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001283 }
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Gilles Peskine8e338702019-07-30 20:06:31 +02001286 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001287 {
1288 if( slot->data.raw.bytes > data_size )
1289 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001290 if( data_size != 0 )
1291 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001292 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001293 memset( data + slot->data.raw.bytes, 0,
1294 data_size - slot->data.raw.bytes );
1295 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296 *data_length = slot->data.raw.bytes;
1297 return( PSA_SUCCESS );
1298 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001299#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001301 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001302 psa_status_t status;
1303
Gilles Peskineb46bef22019-07-30 21:32:04 +02001304 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001305 if( bytes > data_size )
1306 return( PSA_ERROR_BUFFER_TOO_SMALL );
1307 status = mbedtls_to_psa_error(
1308 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1309 if( status != PSA_SUCCESS )
1310 return( status );
1311 memset( data + bytes, 0, data_size - bytes );
1312 *data_length = bytes;
1313 return( PSA_SUCCESS );
1314 }
1315#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316 else
Moran Peker17e36e12018-05-02 12:55:20 +03001317 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001318#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001319 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1320 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001321 {
Moran Pekera998bc62018-04-16 18:16:20 +03001322 mbedtls_pk_context pk;
1323 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001324 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001325 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001326#if defined(MBEDTLS_RSA_C)
1327 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001328 pk.pk_info = &mbedtls_rsa_info;
1329 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001330#else
1331 return( PSA_ERROR_NOT_SUPPORTED );
1332#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001333 }
1334 else
1335 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001336#if defined(MBEDTLS_ECP_C)
1337 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001338 pk.pk_info = &mbedtls_eckey_info;
1339 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001340#else
1341 return( PSA_ERROR_NOT_SUPPORTED );
1342#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001343 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001345 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001346 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001347 }
Moran Peker17e36e12018-05-02 12:55:20 +03001348 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001349 {
Moran Peker17e36e12018-05-02 12:55:20 +03001350 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001351 }
Moran Peker60364322018-04-29 11:34:58 +03001352 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001353 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001354 /* If data_size is 0 then data may be NULL and then the
1355 * call to memset would have undefined behavior. */
1356 if( data_size != 0 )
1357 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001358 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001359 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001360 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1361 * Move the data to the beginning and erase remaining data
1362 * at the original location. */
1363 if( 2 * (size_t) ret <= data_size )
1364 {
1365 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001366 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001367 }
1368 else if( (size_t) ret < data_size )
1369 {
1370 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001371 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001372 }
Moran Pekera998bc62018-04-16 18:16:20 +03001373 *data_length = ret;
1374 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001375 }
1376 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001377#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001378 {
1379 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001380 it is valid for a special-purpose implementation to omit
1381 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001382 return( PSA_ERROR_NOT_SUPPORTED );
1383 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384 }
1385}
1386
Gilles Peskinec5487a82018-12-03 18:08:14 +01001387psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001388 uint8_t *data,
1389 size_t data_size,
1390 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001392 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001393 psa_status_t status;
1394
1395 /* Set the key to empty now, so that even when there are errors, we always
1396 * set data_length to a value between 0 and data_size. On error, setting
1397 * the key to empty is a good choice because an empty key representation is
1398 * unlikely to be accepted anywhere. */
1399 *data_length = 0;
1400
1401 /* Export requires the EXPORT flag. There is an exception for public keys,
1402 * which don't require any flag, but psa_get_key_from_slot takes
1403 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001404 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001405 if( status != PSA_SUCCESS )
1406 return( status );
1407 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001408 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001409}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410
Gilles Peskinec5487a82018-12-03 18:08:14 +01001411psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001412 uint8_t *data,
1413 size_t data_size,
1414 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001415{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001416 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001417 psa_status_t status;
1418
1419 /* Set the key to empty now, so that even when there are errors, we always
1420 * set data_length to a value between 0 and data_size. On error, setting
1421 * the key to empty is a good choice because an empty key representation is
1422 * unlikely to be accepted anywhere. */
1423 *data_length = 0;
1424
1425 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
1429 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001430 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001431}
1432
Gilles Peskine91e8c332019-08-02 19:19:39 +02001433#if defined(static_assert)
1434static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1435 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001436static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001437 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001438static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001439 "One or more key attribute flag is listed as both internal-only and external-only" );
1440#endif
1441
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001442/** Validate that a key policy is internally well-formed.
1443 *
1444 * This function only rejects invalid policies. It does not validate the
1445 * consistency of the policy with respect to other attributes of the key
1446 * such as the key type.
1447 */
1448static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001449{
1450 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001451 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 PSA_KEY_USAGE_ENCRYPT |
1453 PSA_KEY_USAGE_DECRYPT |
1454 PSA_KEY_USAGE_SIGN |
1455 PSA_KEY_USAGE_VERIFY |
1456 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1457 return( PSA_ERROR_INVALID_ARGUMENT );
1458
Gilles Peskine4747d192019-04-17 15:05:45 +02001459 return( PSA_SUCCESS );
1460}
1461
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001462/** Validate the internal consistency of key attributes.
1463 *
1464 * This function only rejects invalid attribute values. If does not
1465 * validate the consistency of the attributes with any key data that may
1466 * be involved in the creation of the key.
1467 *
1468 * Call this function early in the key creation process.
1469 *
1470 * \param[in] attributes Key attributes for the new key.
1471 * \param[out] p_drv On any return, the driver for the key, if any.
1472 * NULL for a transparent key.
1473 *
1474 */
1475static psa_status_t psa_validate_key_attributes(
1476 const psa_key_attributes_t *attributes,
1477 psa_se_drv_table_entry_t **p_drv )
1478{
1479 psa_status_t status;
1480
1481 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1482 {
1483 status = psa_validate_persistent_key_parameters(
1484 attributes->core.lifetime, attributes->core.id,
1485 p_drv, 1 );
1486 if( status != PSA_SUCCESS )
1487 return( status );
1488 }
1489
1490 status = psa_validate_key_policy( &attributes->core.policy );
1491 if( status != PSA_SUCCESS )
1492 return( status );
1493
1494 /* Refuse to create overly large keys.
1495 * Note that this doesn't trigger on import if the attributes don't
1496 * explicitly specify a size (so psa_get_key_bits returns 0), so
1497 * psa_import_key() needs its own checks. */
1498 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1499 return( PSA_ERROR_NOT_SUPPORTED );
1500
Gilles Peskine91e8c332019-08-02 19:19:39 +02001501 /* Reject invalid flags. These should not be reachable through the API. */
1502 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1503 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1504 return( PSA_ERROR_INVALID_ARGUMENT );
1505
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506 return( PSA_SUCCESS );
1507}
1508
Gilles Peskine4747d192019-04-17 15:05:45 +02001509/** Prepare a key slot to receive key material.
1510 *
1511 * This function allocates a key slot and sets its metadata.
1512 *
1513 * If this function fails, call psa_fail_key_creation().
1514 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001515 * This function is intended to be used as follows:
1516 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1517 * it with the specified attributes, and assign it a handle.
1518 * -# Populate the slot with the key material.
1519 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1520 * In case of failure at any step, stop the sequence and call
1521 * psa_fail_key_creation().
1522 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001523 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001524 * \param[in] attributes Key attributes for the new key.
1525 * \param[out] handle On success, a handle for the allocated slot.
1526 * \param[out] p_slot On success, a pointer to the prepared slot.
1527 * \param[out] p_drv On any return, the driver for the key, if any.
1528 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001529 *
1530 * \retval #PSA_SUCCESS
1531 * The key slot is ready to receive key material.
1532 * \return If this function fails, the key slot is an invalid state.
1533 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001534 */
1535static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001536 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001537 const psa_key_attributes_t *attributes,
1538 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001539 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001540 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001541{
1542 psa_status_t status;
1543 psa_key_slot_t *slot;
1544
Gilles Peskinedf179142019-07-15 22:02:14 +02001545 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001546 *p_drv = NULL;
1547
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001548 status = psa_validate_key_attributes( attributes, p_drv );
1549 if( status != PSA_SUCCESS )
1550 return( status );
1551
Gilles Peskineedbed562019-08-07 18:19:59 +02001552 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001553 if( status != PSA_SUCCESS )
1554 return( status );
1555 slot = *p_slot;
1556
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001557 /* We're storing the declared bit-size of the key. It's up to each
1558 * creation mechanism to verify that this information is correct.
1559 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001560 * an input (generate, device) but not for those where the bit-size
1561 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001562
1563 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001564
Gilles Peskine91e8c332019-08-02 19:19:39 +02001565 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001566 * external-only flags, query `attributes`. Thanks to the check
1567 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001568 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001569 * may have set. */
1570 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001571
Gilles Peskinecbaff462019-07-12 23:46:04 +02001572#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001573 /* For a key in a secure element, we need to do three things
1574 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001575 * create the key file in internal storage, create the
1576 * key inside the secure element, and update the driver's
1577 * persistent data. Start a transaction that will encompass these
1578 * three actions. */
1579 /* The first thing to do is to find a slot number for the new key.
1580 * We save the slot number in persistent storage as part of the
1581 * transaction data. It will be needed to recover if the power
1582 * fails during the key creation process, to clean up on the secure
1583 * element side after restarting. Obtaining a slot number from the
1584 * secure element driver updates its persistent state, but we do not yet
1585 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001586 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001587 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001588 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001589 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001590 &slot->data.se.slot_number );
1591 if( status != PSA_SUCCESS )
1592 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001593 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001594 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001595 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001596 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001597 status = psa_crypto_save_transaction( );
1598 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001599 {
1600 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001601 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001602 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001603 }
1604#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1605
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 return( status );
1607}
1608
1609/** Finalize the creation of a key once its key material has been set.
1610 *
1611 * This entails writing the key to persistent storage.
1612 *
1613 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001614 * See the documentation of psa_start_key_creation() for the intended use
1615 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001616 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 * \param[in,out] slot Pointer to the slot with key material.
1618 * \param[in] driver The secure element driver for the key,
1619 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 *
1621 * \retval #PSA_SUCCESS
1622 * The key was successfully created. The handle is now valid.
1623 * \return If this function fails, the key slot is an invalid state.
1624 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001626static psa_status_t psa_finish_key_creation(
1627 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001628 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001629{
1630 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001631 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001632 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001633
1634#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001635 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001637#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1638 if( driver != NULL )
1639 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001640 psa_se_key_data_storage_t data;
1641#if defined(static_assert)
1642 static_assert( sizeof( slot->data.se.slot_number ) ==
1643 sizeof( data.slot_number ),
1644 "Slot number size does not match psa_se_key_data_storage_t" );
1645 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1646 "Bit-size size does not match psa_se_key_data_storage_t" );
1647#endif
1648 memcpy( &data.slot_number, &slot->data.se.slot_number,
1649 sizeof( slot->data.se.slot_number ) );
1650 memcpy( &data.bits, &slot->attr.bits,
1651 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001652 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001653 (uint8_t*) &data,
1654 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001655 }
1656 else
1657#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1658 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001659 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001660 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001661 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001662 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1663 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001664 if( buffer == NULL && buffer_size != 0 )
1665 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1666 status = psa_internal_export_key( slot,
1667 buffer, buffer_size, &length,
1668 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001669 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001670 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001671 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001672
Gilles Peskine1df83d42019-07-23 16:13:14 +02001673 if( buffer_size != 0 )
1674 mbedtls_platform_zeroize( buffer, buffer_size );
1675 mbedtls_free( buffer );
1676 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 }
1678#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1679
Gilles Peskinecbaff462019-07-12 23:46:04 +02001680#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001681 /* Finish the transaction for a key creation. This does not
1682 * happen when registering an existing key. Detect this case
1683 * by checking whether a transaction is in progress (actual
1684 * creation of a key in a secure element requires a transaction,
1685 * but registration doesn't use one). */
1686 if( driver != NULL &&
1687 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001688 {
1689 status = psa_save_se_persistent_data( driver );
1690 if( status != PSA_SUCCESS )
1691 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001692 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693 return( status );
1694 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001695 status = psa_crypto_stop_transaction( );
1696 if( status != PSA_SUCCESS )
1697 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001698 }
1699#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1700
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 return( status );
1702}
1703
1704/** Abort the creation of a key.
1705 *
1706 * You may call this function after calling psa_start_key_creation(),
1707 * or after psa_finish_key_creation() fails. In other circumstances, this
1708 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001709 * See the documentation of psa_start_key_creation() for the intended use
1710 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001711 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001712 * \param[in,out] slot Pointer to the slot with key material.
1713 * \param[in] driver The secure element driver for the key,
1714 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001716static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001717 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001718{
Gilles Peskine011e4282019-06-26 18:34:38 +02001719 (void) driver;
1720
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 if( slot == NULL )
1722 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001723
1724#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1725 /* TOnogrepDO: If the key has already been created in the secure
1726 * element, and the failure happened later (when saving metadata
1727 * to internal storage), we need to destroy the key in the secure
1728 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001729
Gilles Peskined7729582019-08-05 15:55:54 +02001730 /* Abort the ongoing transaction if any (there may not be one if
1731 * the creation process failed before starting one, or if the
1732 * key creation is a registration of a key in a secure element).
1733 * Earlier functions must already have done what it takes to undo any
1734 * partial creation. All that's left is to update the transaction data
1735 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001736 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001737#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1738
Gilles Peskine4747d192019-04-17 15:05:45 +02001739 psa_wipe_key_slot( slot );
1740}
1741
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001742/** Validate optional attributes during key creation.
1743 *
1744 * Some key attributes are optional during key creation. If they are
1745 * specified in the attributes structure, check that they are consistent
1746 * with the data in the slot.
1747 *
1748 * This function should be called near the end of key creation, after
1749 * the slot in memory is fully populated but before saving persistent data.
1750 */
1751static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001752 const psa_key_slot_t *slot,
1753 const psa_key_attributes_t *attributes )
1754{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001755 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001756 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001757 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001758 return( PSA_ERROR_INVALID_ARGUMENT );
1759 }
1760
1761 if( attributes->domain_parameters_size != 0 )
1762 {
1763#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001764 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001765 {
1766 mbedtls_mpi actual, required;
1767 int ret;
1768 mbedtls_mpi_init( &actual );
1769 mbedtls_mpi_init( &required );
1770 ret = mbedtls_rsa_export( slot->data.rsa,
1771 NULL, NULL, NULL, NULL, &actual );
1772 if( ret != 0 )
1773 goto rsa_exit;
1774 ret = mbedtls_mpi_read_binary( &required,
1775 attributes->domain_parameters,
1776 attributes->domain_parameters_size );
1777 if( ret != 0 )
1778 goto rsa_exit;
1779 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1780 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1781 rsa_exit:
1782 mbedtls_mpi_free( &actual );
1783 mbedtls_mpi_free( &required );
1784 if( ret != 0)
1785 return( mbedtls_to_psa_error( ret ) );
1786 }
1787 else
1788#endif
1789 {
1790 return( PSA_ERROR_INVALID_ARGUMENT );
1791 }
1792 }
1793
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001794 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001795 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001796 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001797 return( PSA_ERROR_INVALID_ARGUMENT );
1798 }
1799
1800 return( PSA_SUCCESS );
1801}
1802
Gilles Peskine4747d192019-04-17 15:05:45 +02001803psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001805 size_t data_length,
1806 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001807{
1808 psa_status_t status;
1809 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001810 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001811
Gilles Peskinedf179142019-07-15 22:02:14 +02001812 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1813 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001814 if( status != PSA_SUCCESS )
1815 goto exit;
1816
Gilles Peskine5d309672019-07-12 23:47:28 +02001817#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1818 if( driver != NULL )
1819 {
1820 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001821 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001822 if( drv->key_management == NULL ||
1823 drv->key_management->p_import == NULL )
1824 {
1825 status = PSA_ERROR_NOT_SUPPORTED;
1826 goto exit;
1827 }
1828 status = drv->key_management->p_import(
1829 psa_get_se_driver_context( driver ),
1830 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001831 slot->attr.lifetime, slot->attr.type,
1832 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001833 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001834 &bits );
1835 if( status != PSA_SUCCESS )
1836 goto exit;
1837 if( bits > PSA_MAX_KEY_BITS )
1838 {
1839 status = PSA_ERROR_NOT_SUPPORTED;
1840 goto exit;
1841 }
1842 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001843 }
1844 else
1845#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1846 {
1847 status = psa_import_key_into_slot( slot, data, data_length );
1848 if( status != PSA_SUCCESS )
1849 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001850 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001851 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001852 if( status != PSA_SUCCESS )
1853 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854
Gilles Peskine011e4282019-06-26 18:34:38 +02001855 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001856exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001857 if( status != PSA_SUCCESS )
1858 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001859 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001860 *handle = 0;
1861 }
1862 return( status );
1863}
1864
Gilles Peskined7729582019-08-05 15:55:54 +02001865#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1866psa_status_t mbedtls_psa_register_se_key(
1867 const psa_key_attributes_t *attributes )
1868{
1869 psa_status_t status;
1870 psa_key_slot_t *slot = NULL;
1871 psa_se_drv_table_entry_t *driver = NULL;
1872 const psa_drv_se_t *drv;
1873 psa_key_handle_t handle = 0;
1874
1875 /* Leaving attributes unspecified is not currently supported.
1876 * It could make sense to query the key type and size from the
1877 * secure element, but not all secure elements support this
1878 * and the driver HAL doesn't currently support it. */
1879 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1880 return( PSA_ERROR_NOT_SUPPORTED );
1881 if( psa_get_key_bits( attributes ) == 0 )
1882 return( PSA_ERROR_NOT_SUPPORTED );
1883
1884 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1885 &handle, &slot, &driver );
1886 if( status != PSA_SUCCESS )
1887 goto exit;
1888
1889 if( driver == NULL )
1890 {
1891 status = PSA_ERROR_INVALID_ARGUMENT;
1892 goto exit;
1893 }
1894 drv = psa_get_se_driver_methods( driver );
1895
1896 if ( psa_get_key_slot_number( attributes,
1897 &slot->data.se.slot_number ) != PSA_SUCCESS )
1898 {
1899 /* The application didn't specify a slot number. This doesn't
1900 * make sense when registering a slot. */
1901 status = PSA_ERROR_INVALID_ARGUMENT;
1902 goto exit;
1903 }
1904
Gilles Peskinea5f87492019-08-05 16:46:18 +02001905 /* If the driver has a slot number validation method, call it.
1906 * If it doesn't, it means the secure element is unable to validate
1907 * anything and so we have to trust the application. */
1908 if( drv->key_management != NULL &&
1909 drv->key_management->p_validate_slot_number != NULL )
1910 {
1911 status = drv->key_management->p_validate_slot_number(
1912 psa_get_se_driver_context( driver ),
1913 attributes,
1914 PSA_KEY_CREATION_REGISTER,
1915 slot->data.se.slot_number );
1916 if( status != PSA_SUCCESS )
1917 goto exit;
1918 }
1919
Gilles Peskined7729582019-08-05 15:55:54 +02001920 status = psa_finish_key_creation( slot, driver );
1921
1922exit:
1923 if( status != PSA_SUCCESS )
1924 {
1925 psa_fail_key_creation( slot, driver );
1926 }
1927 /* Registration doesn't keep the key in RAM. */
1928 psa_close_key( handle );
1929 return( status );
1930}
1931#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1932
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001933static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001934 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001935{
1936 psa_status_t status;
1937 uint8_t *buffer = NULL;
1938 size_t buffer_size = 0;
1939 size_t length;
1940
Gilles Peskine8e338702019-07-30 20:06:31 +02001941 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001942 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001943 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001944 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001945 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001946 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1947 if( status != PSA_SUCCESS )
1948 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001949 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001950 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001951
1952exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001953 if( buffer_size != 0 )
1954 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001955 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001956 return( status );
1957}
1958
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001959psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1960 const psa_key_attributes_t *specified_attributes,
1961 psa_key_handle_t *target_handle )
1962{
1963 psa_status_t status;
1964 psa_key_slot_t *source_slot = NULL;
1965 psa_key_slot_t *target_slot = NULL;
1966 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001967 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968
Gilles Peskine28f8f302019-07-24 13:30:31 +02001969 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001970 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 if( status != PSA_SUCCESS )
1972 goto exit;
1973
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001974 status = psa_validate_optional_attributes( source_slot,
1975 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976 if( status != PSA_SUCCESS )
1977 goto exit;
1978
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001979 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001980 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001981 if( status != PSA_SUCCESS )
1982 goto exit;
1983
Gilles Peskinedf179142019-07-15 22:02:14 +02001984 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1985 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001986 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001990#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1991 if( driver != NULL )
1992 {
1993 /* Copying to a secure element is not implemented yet. */
1994 status = PSA_ERROR_NOT_SUPPORTED;
1995 goto exit;
1996 }
1997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1998
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000 if( status != PSA_SUCCESS )
2001 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002
Gilles Peskine011e4282019-06-26 18:34:38 +02002003 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 if( status != PSA_SUCCESS )
2006 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002007 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 *target_handle = 0;
2009 }
2010 return( status );
2011}
2012
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002013
2014
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002015/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002016/* Message digests */
2017/****************************************************************/
2018
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002019static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002020{
2021 switch( alg )
2022 {
2023#if defined(MBEDTLS_MD2_C)
2024 case PSA_ALG_MD2:
2025 return( &mbedtls_md2_info );
2026#endif
2027#if defined(MBEDTLS_MD4_C)
2028 case PSA_ALG_MD4:
2029 return( &mbedtls_md4_info );
2030#endif
2031#if defined(MBEDTLS_MD5_C)
2032 case PSA_ALG_MD5:
2033 return( &mbedtls_md5_info );
2034#endif
2035#if defined(MBEDTLS_RIPEMD160_C)
2036 case PSA_ALG_RIPEMD160:
2037 return( &mbedtls_ripemd160_info );
2038#endif
2039#if defined(MBEDTLS_SHA1_C)
2040 case PSA_ALG_SHA_1:
2041 return( &mbedtls_sha1_info );
2042#endif
2043#if defined(MBEDTLS_SHA256_C)
2044 case PSA_ALG_SHA_224:
2045 return( &mbedtls_sha224_info );
2046 case PSA_ALG_SHA_256:
2047 return( &mbedtls_sha256_info );
2048#endif
2049#if defined(MBEDTLS_SHA512_C)
2050 case PSA_ALG_SHA_384:
2051 return( &mbedtls_sha384_info );
2052 case PSA_ALG_SHA_512:
2053 return( &mbedtls_sha512_info );
2054#endif
2055 default:
2056 return( NULL );
2057 }
2058}
2059
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002060psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2061{
2062 switch( operation->alg )
2063 {
Gilles Peskine81736312018-06-26 15:04:31 +02002064 case 0:
2065 /* The object has (apparently) been initialized but it is not
2066 * in use. It's ok to call abort on such an object, and there's
2067 * nothing to do. */
2068 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002069#if defined(MBEDTLS_MD2_C)
2070 case PSA_ALG_MD2:
2071 mbedtls_md2_free( &operation->ctx.md2 );
2072 break;
2073#endif
2074#if defined(MBEDTLS_MD4_C)
2075 case PSA_ALG_MD4:
2076 mbedtls_md4_free( &operation->ctx.md4 );
2077 break;
2078#endif
2079#if defined(MBEDTLS_MD5_C)
2080 case PSA_ALG_MD5:
2081 mbedtls_md5_free( &operation->ctx.md5 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_RIPEMD160_C)
2085 case PSA_ALG_RIPEMD160:
2086 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_SHA1_C)
2090 case PSA_ALG_SHA_1:
2091 mbedtls_sha1_free( &operation->ctx.sha1 );
2092 break;
2093#endif
2094#if defined(MBEDTLS_SHA256_C)
2095 case PSA_ALG_SHA_224:
2096 case PSA_ALG_SHA_256:
2097 mbedtls_sha256_free( &operation->ctx.sha256 );
2098 break;
2099#endif
2100#if defined(MBEDTLS_SHA512_C)
2101 case PSA_ALG_SHA_384:
2102 case PSA_ALG_SHA_512:
2103 mbedtls_sha512_free( &operation->ctx.sha512 );
2104 break;
2105#endif
2106 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002107 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002108 }
2109 operation->alg = 0;
2110 return( PSA_SUCCESS );
2111}
2112
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002113psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 psa_algorithm_t alg )
2115{
2116 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002117
2118 /* A context must be freshly initialized before it can be set up. */
2119 if( operation->alg != 0 )
2120 {
2121 return( PSA_ERROR_BAD_STATE );
2122 }
2123
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124 switch( alg )
2125 {
2126#if defined(MBEDTLS_MD2_C)
2127 case PSA_ALG_MD2:
2128 mbedtls_md2_init( &operation->ctx.md2 );
2129 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2130 break;
2131#endif
2132#if defined(MBEDTLS_MD4_C)
2133 case PSA_ALG_MD4:
2134 mbedtls_md4_init( &operation->ctx.md4 );
2135 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2136 break;
2137#endif
2138#if defined(MBEDTLS_MD5_C)
2139 case PSA_ALG_MD5:
2140 mbedtls_md5_init( &operation->ctx.md5 );
2141 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2142 break;
2143#endif
2144#if defined(MBEDTLS_RIPEMD160_C)
2145 case PSA_ALG_RIPEMD160:
2146 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2147 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2148 break;
2149#endif
2150#if defined(MBEDTLS_SHA1_C)
2151 case PSA_ALG_SHA_1:
2152 mbedtls_sha1_init( &operation->ctx.sha1 );
2153 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2154 break;
2155#endif
2156#if defined(MBEDTLS_SHA256_C)
2157 case PSA_ALG_SHA_224:
2158 mbedtls_sha256_init( &operation->ctx.sha256 );
2159 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2160 break;
2161 case PSA_ALG_SHA_256:
2162 mbedtls_sha256_init( &operation->ctx.sha256 );
2163 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_SHA512_C)
2167 case PSA_ALG_SHA_384:
2168 mbedtls_sha512_init( &operation->ctx.sha512 );
2169 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2170 break;
2171 case PSA_ALG_SHA_512:
2172 mbedtls_sha512_init( &operation->ctx.sha512 );
2173 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2174 break;
2175#endif
2176 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002177 return( PSA_ALG_IS_HASH( alg ) ?
2178 PSA_ERROR_NOT_SUPPORTED :
2179 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180 }
2181 if( ret == 0 )
2182 operation->alg = alg;
2183 else
2184 psa_hash_abort( operation );
2185 return( mbedtls_to_psa_error( ret ) );
2186}
2187
2188psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2189 const uint8_t *input,
2190 size_t input_length )
2191{
2192 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002193
2194 /* Don't require hash implementations to behave correctly on a
2195 * zero-length input, which may have an invalid pointer. */
2196 if( input_length == 0 )
2197 return( PSA_SUCCESS );
2198
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199 switch( operation->alg )
2200 {
2201#if defined(MBEDTLS_MD2_C)
2202 case PSA_ALG_MD2:
2203 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2204 input, input_length );
2205 break;
2206#endif
2207#if defined(MBEDTLS_MD4_C)
2208 case PSA_ALG_MD4:
2209 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2210 input, input_length );
2211 break;
2212#endif
2213#if defined(MBEDTLS_MD5_C)
2214 case PSA_ALG_MD5:
2215 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2216 input, input_length );
2217 break;
2218#endif
2219#if defined(MBEDTLS_RIPEMD160_C)
2220 case PSA_ALG_RIPEMD160:
2221 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2222 input, input_length );
2223 break;
2224#endif
2225#if defined(MBEDTLS_SHA1_C)
2226 case PSA_ALG_SHA_1:
2227 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2228 input, input_length );
2229 break;
2230#endif
2231#if defined(MBEDTLS_SHA256_C)
2232 case PSA_ALG_SHA_224:
2233 case PSA_ALG_SHA_256:
2234 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2235 input, input_length );
2236 break;
2237#endif
2238#if defined(MBEDTLS_SHA512_C)
2239 case PSA_ALG_SHA_384:
2240 case PSA_ALG_SHA_512:
2241 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2242 input, input_length );
2243 break;
2244#endif
2245 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002246 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002248
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 if( ret != 0 )
2250 psa_hash_abort( operation );
2251 return( mbedtls_to_psa_error( ret ) );
2252}
2253
2254psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2255 uint8_t *hash,
2256 size_t hash_size,
2257 size_t *hash_length )
2258{
itayzafrir40835d42018-08-02 13:14:17 +03002259 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002261 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262
2263 /* Fill the output buffer with something that isn't a valid hash
2264 * (barring an attack on the hash and deliberately-crafted input),
2265 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002266 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002267 /* If hash_size is 0 then hash may be NULL and then the
2268 * call to memset would have undefined behavior. */
2269 if( hash_size != 0 )
2270 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271
2272 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002273 {
2274 status = PSA_ERROR_BUFFER_TOO_SMALL;
2275 goto exit;
2276 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277
2278 switch( operation->alg )
2279 {
2280#if defined(MBEDTLS_MD2_C)
2281 case PSA_ALG_MD2:
2282 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2283 break;
2284#endif
2285#if defined(MBEDTLS_MD4_C)
2286 case PSA_ALG_MD4:
2287 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2288 break;
2289#endif
2290#if defined(MBEDTLS_MD5_C)
2291 case PSA_ALG_MD5:
2292 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2293 break;
2294#endif
2295#if defined(MBEDTLS_RIPEMD160_C)
2296 case PSA_ALG_RIPEMD160:
2297 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2298 break;
2299#endif
2300#if defined(MBEDTLS_SHA1_C)
2301 case PSA_ALG_SHA_1:
2302 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2303 break;
2304#endif
2305#if defined(MBEDTLS_SHA256_C)
2306 case PSA_ALG_SHA_224:
2307 case PSA_ALG_SHA_256:
2308 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2309 break;
2310#endif
2311#if defined(MBEDTLS_SHA512_C)
2312 case PSA_ALG_SHA_384:
2313 case PSA_ALG_SHA_512:
2314 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2315 break;
2316#endif
2317 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002318 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 }
itayzafrir40835d42018-08-02 13:14:17 +03002320 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321
itayzafrir40835d42018-08-02 13:14:17 +03002322exit:
2323 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002325 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 return( psa_hash_abort( operation ) );
2327 }
2328 else
2329 {
2330 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002331 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 }
2333}
2334
Gilles Peskine2d277862018-06-18 15:41:12 +02002335psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2336 const uint8_t *hash,
2337 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338{
2339 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2340 size_t actual_hash_length;
2341 psa_status_t status = psa_hash_finish( operation,
2342 actual_hash, sizeof( actual_hash ),
2343 &actual_hash_length );
2344 if( status != PSA_SUCCESS )
2345 return( status );
2346 if( actual_hash_length != hash_length )
2347 return( PSA_ERROR_INVALID_SIGNATURE );
2348 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2349 return( PSA_ERROR_INVALID_SIGNATURE );
2350 return( PSA_SUCCESS );
2351}
2352
Gilles Peskineeb35d782019-01-22 17:56:16 +01002353psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2354 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002355{
2356 if( target_operation->alg != 0 )
2357 return( PSA_ERROR_BAD_STATE );
2358
2359 switch( source_operation->alg )
2360 {
2361 case 0:
2362 return( PSA_ERROR_BAD_STATE );
2363#if defined(MBEDTLS_MD2_C)
2364 case PSA_ALG_MD2:
2365 mbedtls_md2_clone( &target_operation->ctx.md2,
2366 &source_operation->ctx.md2 );
2367 break;
2368#endif
2369#if defined(MBEDTLS_MD4_C)
2370 case PSA_ALG_MD4:
2371 mbedtls_md4_clone( &target_operation->ctx.md4,
2372 &source_operation->ctx.md4 );
2373 break;
2374#endif
2375#if defined(MBEDTLS_MD5_C)
2376 case PSA_ALG_MD5:
2377 mbedtls_md5_clone( &target_operation->ctx.md5,
2378 &source_operation->ctx.md5 );
2379 break;
2380#endif
2381#if defined(MBEDTLS_RIPEMD160_C)
2382 case PSA_ALG_RIPEMD160:
2383 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2384 &source_operation->ctx.ripemd160 );
2385 break;
2386#endif
2387#if defined(MBEDTLS_SHA1_C)
2388 case PSA_ALG_SHA_1:
2389 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2390 &source_operation->ctx.sha1 );
2391 break;
2392#endif
2393#if defined(MBEDTLS_SHA256_C)
2394 case PSA_ALG_SHA_224:
2395 case PSA_ALG_SHA_256:
2396 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2397 &source_operation->ctx.sha256 );
2398 break;
2399#endif
2400#if defined(MBEDTLS_SHA512_C)
2401 case PSA_ALG_SHA_384:
2402 case PSA_ALG_SHA_512:
2403 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2404 &source_operation->ctx.sha512 );
2405 break;
2406#endif
2407 default:
2408 return( PSA_ERROR_NOT_SUPPORTED );
2409 }
2410
2411 target_operation->alg = source_operation->alg;
2412 return( PSA_SUCCESS );
2413}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414
2415
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002416/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417/* MAC */
2418/****************************************************************/
2419
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002420static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421 psa_algorithm_t alg,
2422 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002423 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002424 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002427 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002429 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002430 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002431
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2433 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002434 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002436 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002437 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438 mode = MBEDTLS_MODE_STREAM;
2439 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440 case PSA_ALG_CTR:
2441 mode = MBEDTLS_MODE_CTR;
2442 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002443 case PSA_ALG_CFB:
2444 mode = MBEDTLS_MODE_CFB;
2445 break;
2446 case PSA_ALG_OFB:
2447 mode = MBEDTLS_MODE_OFB;
2448 break;
2449 case PSA_ALG_CBC_NO_PADDING:
2450 mode = MBEDTLS_MODE_CBC;
2451 break;
2452 case PSA_ALG_CBC_PKCS7:
2453 mode = MBEDTLS_MODE_CBC;
2454 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002455 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 mode = MBEDTLS_MODE_CCM;
2457 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002458 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 mode = MBEDTLS_MODE_GCM;
2460 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002461 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2462 mode = MBEDTLS_MODE_CHACHAPOLY;
2463 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464 default:
2465 return( NULL );
2466 }
2467 }
2468 else if( alg == PSA_ALG_CMAC )
2469 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470 else
2471 return( NULL );
2472
2473 switch( key_type )
2474 {
2475 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002476 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 break;
2478 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002479 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2480 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002482 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002484 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002485 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2486 * but two-key Triple-DES is functionally three-key Triple-DES
2487 * with K1=K3, so that's how we present it to mbedtls. */
2488 if( key_bits == 128 )
2489 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 break;
2491 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 break;
2494 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002495 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002497 case PSA_KEY_TYPE_CHACHA20:
2498 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2499 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 default:
2501 return( NULL );
2502 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002503 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002504 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505
Jaeden Amero23bbb752018-06-26 14:16:54 +01002506 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2507 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508}
2509
Gilles Peskinea05219c2018-11-16 16:02:56 +01002510#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002511static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002512{
Gilles Peskine2d277862018-06-18 15:41:12 +02002513 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002514 {
2515 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002516 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002517 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002518 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002519 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002520 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002521 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002522 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002523 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002524 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002525 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002526 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002527 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002528 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002529 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002530 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002531 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002532 return( 128 );
2533 default:
2534 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002535 }
2536}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002537#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002538
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002539/* Initialize the MAC operation structure. Once this function has been
2540 * called, psa_mac_abort can run and will do the right thing. */
2541static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2542 psa_algorithm_t alg )
2543{
2544 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2545
2546 operation->alg = alg;
2547 operation->key_set = 0;
2548 operation->iv_set = 0;
2549 operation->iv_required = 0;
2550 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002551 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552
2553#if defined(MBEDTLS_CMAC_C)
2554 if( alg == PSA_ALG_CMAC )
2555 {
2556 operation->iv_required = 0;
2557 mbedtls_cipher_init( &operation->ctx.cmac );
2558 status = PSA_SUCCESS;
2559 }
2560 else
2561#endif /* MBEDTLS_CMAC_C */
2562#if defined(MBEDTLS_MD_C)
2563 if( PSA_ALG_IS_HMAC( operation->alg ) )
2564 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002565 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2566 operation->ctx.hmac.hash_ctx.alg = 0;
2567 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568 }
2569 else
2570#endif /* MBEDTLS_MD_C */
2571 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002572 if( ! PSA_ALG_IS_MAC( alg ) )
2573 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002574 }
2575
2576 if( status != PSA_SUCCESS )
2577 memset( operation, 0, sizeof( *operation ) );
2578 return( status );
2579}
2580
Gilles Peskine01126fa2018-07-12 17:04:55 +02002581#if defined(MBEDTLS_MD_C)
2582static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2583{
Gilles Peskine3f108122018-12-07 18:14:53 +01002584 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002585 return( psa_hash_abort( &hmac->hash_ctx ) );
2586}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002587
Janos Follath999f6482019-06-11 12:04:10 +01002588#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002589static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2590{
2591 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2592 memset( hmac, 0, sizeof( *hmac ) );
2593}
Janos Follath999f6482019-06-11 12:04:10 +01002594#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002595#endif /* MBEDTLS_MD_C */
2596
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2598{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002599 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002601 /* The object has (apparently) been initialized but it is not
2602 * in use. It's ok to call abort on such an object, and there's
2603 * nothing to do. */
2604 return( PSA_SUCCESS );
2605 }
2606 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002608 if( operation->alg == PSA_ALG_CMAC )
2609 {
2610 mbedtls_cipher_free( &operation->ctx.cmac );
2611 }
2612 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002615 if( PSA_ALG_IS_HMAC( operation->alg ) )
2616 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002617 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002618 }
2619 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002621 {
2622 /* Sanity check (shouldn't happen: operation->alg should
2623 * always have been initialized to a valid value). */
2624 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625 }
Moran Peker41deec42018-04-04 15:43:05 +03002626
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627 operation->alg = 0;
2628 operation->key_set = 0;
2629 operation->iv_set = 0;
2630 operation->iv_required = 0;
2631 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002633
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002635
2636bad_state:
2637 /* If abort is called on an uninitialized object, we can't trust
2638 * anything. Wipe the object in case it contains confidential data.
2639 * This may result in a memory leak if a pointer gets overwritten,
2640 * but it's too late to do anything about this. */
2641 memset( operation, 0, sizeof( *operation ) );
2642 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643}
2644
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002645#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002647 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002648 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002649 const mbedtls_cipher_info_t *cipher_info )
2650{
2651 int ret;
2652
2653 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002654
2655 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2656 if( ret != 0 )
2657 return( ret );
2658
2659 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2660 slot->data.raw.data,
2661 key_bits );
2662 return( ret );
2663}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002664#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002665
Gilles Peskine248051a2018-06-20 16:09:38 +02002666#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002667static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2668 const uint8_t *key,
2669 size_t key_length,
2670 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002671{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002672 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002674 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002675 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676 psa_status_t status;
2677
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002678 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2679 * overflow below. This should never trigger if the hash algorithm
2680 * is implemented correctly. */
2681 /* The size checks against the ipad and opad buffers cannot be written
2682 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2683 * because that triggers -Wlogical-op on GCC 7.3. */
2684 if( block_size > sizeof( ipad ) )
2685 return( PSA_ERROR_NOT_SUPPORTED );
2686 if( block_size > sizeof( hmac->opad ) )
2687 return( PSA_ERROR_NOT_SUPPORTED );
2688 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689 return( PSA_ERROR_NOT_SUPPORTED );
2690
Gilles Peskined223b522018-06-11 18:12:58 +02002691 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002692 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002693 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002695 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002696 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002698 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002699 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002700 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002701 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002702 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002703 }
Gilles Peskine96889972018-07-12 17:07:03 +02002704 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2705 * but it is permitted. It is common when HMAC is used in HKDF, for
2706 * example. Don't call `memcpy` in the 0-length because `key` could be
2707 * an invalid pointer which would make the behavior undefined. */
2708 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710
Gilles Peskined223b522018-06-11 18:12:58 +02002711 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2712 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002714 ipad[i] ^= 0x36;
2715 memset( ipad + key_length, 0x36, block_size - key_length );
2716
2717 /* Copy the key material from ipad to opad, flipping the requisite bits,
2718 * and filling the rest of opad with the requisite constant. */
2719 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002720 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2721 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722
Gilles Peskine01126fa2018-07-12 17:04:55 +02002723 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002724 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002725 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002726
Gilles Peskine01126fa2018-07-12 17:04:55 +02002727 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002728
2729cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002730 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002731
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732 return( status );
2733}
Gilles Peskine248051a2018-06-20 16:09:38 +02002734#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002735
Gilles Peskine89167cb2018-07-08 20:12:23 +02002736static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002737 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002738 psa_algorithm_t alg,
2739 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002742 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002744 psa_key_usage_t usage =
2745 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002746 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002747 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002749 /* A context must be freshly initialized before it can be set up. */
2750 if( operation->alg != 0 )
2751 {
2752 return( PSA_ERROR_BAD_STATE );
2753 }
2754
Gilles Peskined911eb72018-08-14 15:18:45 +02002755 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002756 if( status != PSA_SUCCESS )
2757 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002758 if( is_sign )
2759 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002760
Gilles Peskine28f8f302019-07-24 13:30:31 +02002761 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002762 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002764 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002765
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002767 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002768 {
2769 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002770 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002771 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002772 int ret;
2773 if( cipher_info == NULL )
2774 {
2775 status = PSA_ERROR_NOT_SUPPORTED;
2776 goto exit;
2777 }
2778 operation->mac_size = cipher_info->block_size;
2779 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2780 status = mbedtls_to_psa_error( ret );
2781 }
2782 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002783#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002785 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002786 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002787 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002788 if( hash_alg == 0 )
2789 {
2790 status = PSA_ERROR_NOT_SUPPORTED;
2791 goto exit;
2792 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002793
2794 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2795 /* Sanity check. This shouldn't fail on a valid configuration. */
2796 if( operation->mac_size == 0 ||
2797 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2798 {
2799 status = PSA_ERROR_NOT_SUPPORTED;
2800 goto exit;
2801 }
2802
Gilles Peskine8e338702019-07-30 20:06:31 +02002803 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002804 {
2805 status = PSA_ERROR_INVALID_ARGUMENT;
2806 goto exit;
2807 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002808
Gilles Peskine01126fa2018-07-12 17:04:55 +02002809 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2810 slot->data.raw.data,
2811 slot->data.raw.bytes,
2812 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002813 }
2814 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002816 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002817 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002820
Gilles Peskined911eb72018-08-14 15:18:45 +02002821 if( truncated == 0 )
2822 {
2823 /* The "normal" case: untruncated algorithm. Nothing to do. */
2824 }
2825 else if( truncated < 4 )
2826 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002827 /* A very short MAC is too short for security since it can be
2828 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2829 * so we make this our minimum, even though 32 bits is still
2830 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002831 status = PSA_ERROR_NOT_SUPPORTED;
2832 }
2833 else if( truncated > operation->mac_size )
2834 {
2835 /* It's impossible to "truncate" to a larger length. */
2836 status = PSA_ERROR_INVALID_ARGUMENT;
2837 }
2838 else
2839 operation->mac_size = truncated;
2840
Gilles Peskinefbfac682018-07-08 20:51:54 +02002841exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002842 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002844 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002846 else
2847 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002848 operation->key_set = 1;
2849 }
2850 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851}
2852
Gilles Peskine89167cb2018-07-08 20:12:23 +02002853psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002854 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002855 psa_algorithm_t alg )
2856{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002857 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002858}
2859
2860psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002861 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002862 psa_algorithm_t alg )
2863{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002864 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002865}
2866
Gilles Peskine8c9def32018-02-08 10:02:12 +01002867psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2868 const uint8_t *input,
2869 size_t input_length )
2870{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002873 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002875 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 operation->has_input = 1;
2877
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002880 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002881 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2882 input, input_length );
2883 status = mbedtls_to_psa_error( ret );
2884 }
2885 else
2886#endif /* MBEDTLS_CMAC_C */
2887#if defined(MBEDTLS_MD_C)
2888 if( PSA_ALG_IS_HMAC( operation->alg ) )
2889 {
2890 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2891 input_length );
2892 }
2893 else
2894#endif /* MBEDTLS_MD_C */
2895 {
2896 /* This shouldn't happen if `operation` was initialized by
2897 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002898 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002899 }
2900
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901 if( status != PSA_SUCCESS )
2902 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002903 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904}
2905
Gilles Peskine01126fa2018-07-12 17:04:55 +02002906#if defined(MBEDTLS_MD_C)
2907static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2908 uint8_t *mac,
2909 size_t mac_size )
2910{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002911 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002912 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2913 size_t hash_size = 0;
2914 size_t block_size = psa_get_hash_block_size( hash_alg );
2915 psa_status_t status;
2916
Gilles Peskine01126fa2018-07-12 17:04:55 +02002917 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2918 if( status != PSA_SUCCESS )
2919 return( status );
2920 /* From here on, tmp needs to be wiped. */
2921
2922 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2923 if( status != PSA_SUCCESS )
2924 goto exit;
2925
2926 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2927 if( status != PSA_SUCCESS )
2928 goto exit;
2929
2930 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2931 if( status != PSA_SUCCESS )
2932 goto exit;
2933
Gilles Peskined911eb72018-08-14 15:18:45 +02002934 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2935 if( status != PSA_SUCCESS )
2936 goto exit;
2937
2938 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002939
2940exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002941 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002942 return( status );
2943}
2944#endif /* MBEDTLS_MD_C */
2945
mohammad16036df908f2018-04-02 08:34:15 -07002946static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002947 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002948 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002950 if( ! operation->key_set )
2951 return( PSA_ERROR_BAD_STATE );
2952 if( operation->iv_required && ! operation->iv_set )
2953 return( PSA_ERROR_BAD_STATE );
2954
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955 if( mac_size < operation->mac_size )
2956 return( PSA_ERROR_BUFFER_TOO_SMALL );
2957
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002959 if( operation->alg == PSA_ALG_CMAC )
2960 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002961 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2962 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2963 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002964 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002965 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002966 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002968 else
2969#endif /* MBEDTLS_CMAC_C */
2970#if defined(MBEDTLS_MD_C)
2971 if( PSA_ALG_IS_HMAC( operation->alg ) )
2972 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002973 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002974 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002975 }
2976 else
2977#endif /* MBEDTLS_MD_C */
2978 {
2979 /* This shouldn't happen if `operation` was initialized by
2980 * a setup function. */
2981 return( PSA_ERROR_BAD_STATE );
2982 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002983}
2984
Gilles Peskineacd4be32018-07-08 19:56:25 +02002985psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2986 uint8_t *mac,
2987 size_t mac_size,
2988 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002989{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002990 psa_status_t status;
2991
Jaeden Amero252ef282019-02-15 14:05:35 +00002992 if( operation->alg == 0 )
2993 {
2994 return( PSA_ERROR_BAD_STATE );
2995 }
2996
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002997 /* Fill the output buffer with something that isn't a valid mac
2998 * (barring an attack on the mac and deliberately-crafted input),
2999 * in case the caller doesn't check the return status properly. */
3000 *mac_length = mac_size;
3001 /* If mac_size is 0 then mac may be NULL and then the
3002 * call to memset would have undefined behavior. */
3003 if( mac_size != 0 )
3004 memset( mac, '!', mac_size );
3005
Gilles Peskine89167cb2018-07-08 20:12:23 +02003006 if( ! operation->is_sign )
3007 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003008 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003009 }
mohammad16036df908f2018-04-02 08:34:15 -07003010
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003011 status = psa_mac_finish_internal( operation, mac, mac_size );
3012
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003013 if( status == PSA_SUCCESS )
3014 {
3015 status = psa_mac_abort( operation );
3016 if( status == PSA_SUCCESS )
3017 *mac_length = operation->mac_size;
3018 else
3019 memset( mac, '!', mac_size );
3020 }
3021 else
3022 psa_mac_abort( operation );
3023 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003024}
3025
Gilles Peskineacd4be32018-07-08 19:56:25 +02003026psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3027 const uint8_t *mac,
3028 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029{
Gilles Peskine828ed142018-06-18 23:25:51 +02003030 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003031 psa_status_t status;
3032
Jaeden Amero252ef282019-02-15 14:05:35 +00003033 if( operation->alg == 0 )
3034 {
3035 return( PSA_ERROR_BAD_STATE );
3036 }
3037
Gilles Peskine89167cb2018-07-08 20:12:23 +02003038 if( operation->is_sign )
3039 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003040 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003041 }
3042 if( operation->mac_size != mac_length )
3043 {
3044 status = PSA_ERROR_INVALID_SIGNATURE;
3045 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003046 }
mohammad16036df908f2018-04-02 08:34:15 -07003047
3048 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003049 actual_mac, sizeof( actual_mac ) );
3050
3051 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3052 status = PSA_ERROR_INVALID_SIGNATURE;
3053
3054cleanup:
3055 if( status == PSA_SUCCESS )
3056 status = psa_mac_abort( operation );
3057 else
3058 psa_mac_abort( operation );
3059
Gilles Peskine3f108122018-12-07 18:14:53 +01003060 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003061
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003062 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063}
3064
3065
Gilles Peskine20035e32018-02-03 22:44:14 +01003066
Gilles Peskine20035e32018-02-03 22:44:14 +01003067/****************************************************************/
3068/* Asymmetric cryptography */
3069/****************************************************************/
3070
Gilles Peskine2b450e32018-06-27 15:42:46 +02003071#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003072/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003073 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003074static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3075 size_t hash_length,
3076 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003077{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003078 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003079 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003080 *md_alg = mbedtls_md_get_type( md_info );
3081
3082 /* The Mbed TLS RSA module uses an unsigned int for hash length
3083 * parameters. Validate that it fits so that we don't risk an
3084 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003085#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003086 if( hash_length > UINT_MAX )
3087 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003088#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003089
3090#if defined(MBEDTLS_PKCS1_V15)
3091 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3092 * must be correct. */
3093 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3094 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003095 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003096 if( md_info == NULL )
3097 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003098 if( mbedtls_md_get_size( md_info ) != hash_length )
3099 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003100 }
3101#endif /* MBEDTLS_PKCS1_V15 */
3102
3103#if defined(MBEDTLS_PKCS1_V21)
3104 /* PSS requires a hash internally. */
3105 if( PSA_ALG_IS_RSA_PSS( alg ) )
3106 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003107 if( md_info == NULL )
3108 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003109 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003110#endif /* MBEDTLS_PKCS1_V21 */
3111
Gilles Peskine61b91d42018-06-08 16:09:36 +02003112 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003113}
3114
Gilles Peskine2b450e32018-06-27 15:42:46 +02003115static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3116 psa_algorithm_t alg,
3117 const uint8_t *hash,
3118 size_t hash_length,
3119 uint8_t *signature,
3120 size_t signature_size,
3121 size_t *signature_length )
3122{
3123 psa_status_t status;
3124 int ret;
3125 mbedtls_md_type_t md_alg;
3126
3127 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3128 if( status != PSA_SUCCESS )
3129 return( status );
3130
Gilles Peskine630a18a2018-06-29 17:49:35 +02003131 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003132 return( PSA_ERROR_BUFFER_TOO_SMALL );
3133
3134#if defined(MBEDTLS_PKCS1_V15)
3135 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3136 {
3137 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3138 MBEDTLS_MD_NONE );
3139 ret = mbedtls_rsa_pkcs1_sign( rsa,
3140 mbedtls_ctr_drbg_random,
3141 &global_data.ctr_drbg,
3142 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003143 md_alg,
3144 (unsigned int) hash_length,
3145 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003146 signature );
3147 }
3148 else
3149#endif /* MBEDTLS_PKCS1_V15 */
3150#if defined(MBEDTLS_PKCS1_V21)
3151 if( PSA_ALG_IS_RSA_PSS( alg ) )
3152 {
3153 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3154 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3155 mbedtls_ctr_drbg_random,
3156 &global_data.ctr_drbg,
3157 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003158 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003159 (unsigned int) hash_length,
3160 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003161 signature );
3162 }
3163 else
3164#endif /* MBEDTLS_PKCS1_V21 */
3165 {
3166 return( PSA_ERROR_INVALID_ARGUMENT );
3167 }
3168
3169 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003170 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003171 return( mbedtls_to_psa_error( ret ) );
3172}
3173
3174static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3175 psa_algorithm_t alg,
3176 const uint8_t *hash,
3177 size_t hash_length,
3178 const uint8_t *signature,
3179 size_t signature_length )
3180{
3181 psa_status_t status;
3182 int ret;
3183 mbedtls_md_type_t md_alg;
3184
3185 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3186 if( status != PSA_SUCCESS )
3187 return( status );
3188
Gilles Peskine630a18a2018-06-29 17:49:35 +02003189 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003190 return( PSA_ERROR_BUFFER_TOO_SMALL );
3191
3192#if defined(MBEDTLS_PKCS1_V15)
3193 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3194 {
3195 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3196 MBEDTLS_MD_NONE );
3197 ret = mbedtls_rsa_pkcs1_verify( rsa,
3198 mbedtls_ctr_drbg_random,
3199 &global_data.ctr_drbg,
3200 MBEDTLS_RSA_PUBLIC,
3201 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003202 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003203 hash,
3204 signature );
3205 }
3206 else
3207#endif /* MBEDTLS_PKCS1_V15 */
3208#if defined(MBEDTLS_PKCS1_V21)
3209 if( PSA_ALG_IS_RSA_PSS( alg ) )
3210 {
3211 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3212 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3213 mbedtls_ctr_drbg_random,
3214 &global_data.ctr_drbg,
3215 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003216 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003217 (unsigned int) hash_length,
3218 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003219 signature );
3220 }
3221 else
3222#endif /* MBEDTLS_PKCS1_V21 */
3223 {
3224 return( PSA_ERROR_INVALID_ARGUMENT );
3225 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003226
3227 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3228 * the rest of the signature is invalid". This has little use in
3229 * practice and PSA doesn't report this distinction. */
3230 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3231 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003232 return( mbedtls_to_psa_error( ret ) );
3233}
3234#endif /* MBEDTLS_RSA_C */
3235
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003236#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003237/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3238 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3239 * (even though these functions don't modify it). */
3240static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3241 psa_algorithm_t alg,
3242 const uint8_t *hash,
3243 size_t hash_length,
3244 uint8_t *signature,
3245 size_t signature_size,
3246 size_t *signature_length )
3247{
3248 int ret;
3249 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003250 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003251 mbedtls_mpi_init( &r );
3252 mbedtls_mpi_init( &s );
3253
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003254 if( signature_size < 2 * curve_bytes )
3255 {
3256 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3257 goto cleanup;
3258 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003259
Gilles Peskinea05219c2018-11-16 16:02:56 +01003260#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003261 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3262 {
3263 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3264 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3265 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3266 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3267 hash, hash_length,
3268 md_alg ) );
3269 }
3270 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003271#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003272 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003273 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003274 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3275 hash, hash_length,
3276 mbedtls_ctr_drbg_random,
3277 &global_data.ctr_drbg ) );
3278 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003279
3280 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3281 signature,
3282 curve_bytes ) );
3283 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3284 signature + curve_bytes,
3285 curve_bytes ) );
3286
3287cleanup:
3288 mbedtls_mpi_free( &r );
3289 mbedtls_mpi_free( &s );
3290 if( ret == 0 )
3291 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003292 return( mbedtls_to_psa_error( ret ) );
3293}
3294
3295static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3296 const uint8_t *hash,
3297 size_t hash_length,
3298 const uint8_t *signature,
3299 size_t signature_length )
3300{
3301 int ret;
3302 mbedtls_mpi r, s;
3303 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3304 mbedtls_mpi_init( &r );
3305 mbedtls_mpi_init( &s );
3306
3307 if( signature_length != 2 * curve_bytes )
3308 return( PSA_ERROR_INVALID_SIGNATURE );
3309
3310 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3311 signature,
3312 curve_bytes ) );
3313 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3314 signature + curve_bytes,
3315 curve_bytes ) );
3316
3317 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3318 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003319
3320cleanup:
3321 mbedtls_mpi_free( &r );
3322 mbedtls_mpi_free( &s );
3323 return( mbedtls_to_psa_error( ret ) );
3324}
3325#endif /* MBEDTLS_ECDSA_C */
3326
Gilles Peskinec5487a82018-12-03 18:08:14 +01003327psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003328 psa_algorithm_t alg,
3329 const uint8_t *hash,
3330 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003331 uint8_t *signature,
3332 size_t signature_size,
3333 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003334{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003335 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003336 psa_status_t status;
3337
3338 *signature_length = signature_size;
3339
Gilles Peskine28f8f302019-07-24 13:30:31 +02003340 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003341 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003342 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003343 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003344 {
3345 status = PSA_ERROR_INVALID_ARGUMENT;
3346 goto exit;
3347 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003348
Gilles Peskine20035e32018-02-03 22:44:14 +01003349#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003350 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003351 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003352 status = psa_rsa_sign( slot->data.rsa,
3353 alg,
3354 hash, hash_length,
3355 signature, signature_size,
3356 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003357 }
3358 else
3359#endif /* defined(MBEDTLS_RSA_C) */
3360#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003361 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003362 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003363#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003364 if(
3365#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3366 PSA_ALG_IS_ECDSA( alg )
3367#else
3368 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3369#endif
3370 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003371 status = psa_ecdsa_sign( slot->data.ecp,
3372 alg,
3373 hash, hash_length,
3374 signature, signature_size,
3375 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003376 else
3377#endif /* defined(MBEDTLS_ECDSA_C) */
3378 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003379 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003380 }
itayzafrir5c753392018-05-08 11:18:38 +03003381 }
3382 else
3383#endif /* defined(MBEDTLS_ECP_C) */
3384 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003385 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003386 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003387
3388exit:
3389 /* Fill the unused part of the output buffer (the whole buffer on error,
3390 * the trailing part on success) with something that isn't a valid mac
3391 * (barring an attack on the mac and deliberately-crafted input),
3392 * in case the caller doesn't check the return status properly. */
3393 if( status == PSA_SUCCESS )
3394 memset( signature + *signature_length, '!',
3395 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003396 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003397 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003398 /* If signature_size is 0 then we have nothing to do. We must not call
3399 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003400 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003401}
3402
Gilles Peskinec5487a82018-12-03 18:08:14 +01003403psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003404 psa_algorithm_t alg,
3405 const uint8_t *hash,
3406 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003407 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003408 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003409{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003410 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003411 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003412
Gilles Peskine28f8f302019-07-24 13:30:31 +02003413 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003414 if( status != PSA_SUCCESS )
3415 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003416
Gilles Peskine61b91d42018-06-08 16:09:36 +02003417#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003418 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003419 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003420 return( psa_rsa_verify( slot->data.rsa,
3421 alg,
3422 hash, hash_length,
3423 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003424 }
3425 else
3426#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003427#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003428 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003429 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003430#if defined(MBEDTLS_ECDSA_C)
3431 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003432 return( psa_ecdsa_verify( slot->data.ecp,
3433 hash, hash_length,
3434 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003435 else
3436#endif /* defined(MBEDTLS_ECDSA_C) */
3437 {
3438 return( PSA_ERROR_INVALID_ARGUMENT );
3439 }
itayzafrir5c753392018-05-08 11:18:38 +03003440 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003441 else
3442#endif /* defined(MBEDTLS_ECP_C) */
3443 {
3444 return( PSA_ERROR_NOT_SUPPORTED );
3445 }
3446}
3447
Gilles Peskine072ac562018-06-30 00:21:29 +02003448#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3449static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3450 mbedtls_rsa_context *rsa )
3451{
3452 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3453 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3454 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3455 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3456}
3457#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3458
Gilles Peskinec5487a82018-12-03 18:08:14 +01003459psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003460 psa_algorithm_t alg,
3461 const uint8_t *input,
3462 size_t input_length,
3463 const uint8_t *salt,
3464 size_t salt_length,
3465 uint8_t *output,
3466 size_t output_size,
3467 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003468{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003469 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003470 psa_status_t status;
3471
Darryl Green5cc689a2018-07-24 15:34:10 +01003472 (void) input;
3473 (void) input_length;
3474 (void) salt;
3475 (void) output;
3476 (void) output_size;
3477
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003478 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003479
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003480 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3481 return( PSA_ERROR_INVALID_ARGUMENT );
3482
Gilles Peskine28f8f302019-07-24 13:30:31 +02003483 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003484 if( status != PSA_SUCCESS )
3485 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003486 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3487 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003489
3490#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003491 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003492 {
3493 mbedtls_rsa_context *rsa = slot->data.rsa;
3494 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003495 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003496 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003497#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003498 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003499 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003500 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3501 mbedtls_ctr_drbg_random,
3502 &global_data.ctr_drbg,
3503 MBEDTLS_RSA_PUBLIC,
3504 input_length,
3505 input,
3506 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003507 }
3508 else
3509#endif /* MBEDTLS_PKCS1_V15 */
3510#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003511 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003512 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003513 psa_rsa_oaep_set_padding_mode( alg, rsa );
3514 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3515 mbedtls_ctr_drbg_random,
3516 &global_data.ctr_drbg,
3517 MBEDTLS_RSA_PUBLIC,
3518 salt, salt_length,
3519 input_length,
3520 input,
3521 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522 }
3523 else
3524#endif /* MBEDTLS_PKCS1_V21 */
3525 {
3526 return( PSA_ERROR_INVALID_ARGUMENT );
3527 }
3528 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003529 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003530 return( mbedtls_to_psa_error( ret ) );
3531 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003533#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534 {
3535 return( PSA_ERROR_NOT_SUPPORTED );
3536 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003537}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538
Gilles Peskinec5487a82018-12-03 18:08:14 +01003539psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003540 psa_algorithm_t alg,
3541 const uint8_t *input,
3542 size_t input_length,
3543 const uint8_t *salt,
3544 size_t salt_length,
3545 uint8_t *output,
3546 size_t output_size,
3547 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003548{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003549 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003550 psa_status_t status;
3551
Darryl Green5cc689a2018-07-24 15:34:10 +01003552 (void) input;
3553 (void) input_length;
3554 (void) salt;
3555 (void) output;
3556 (void) output_size;
3557
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003558 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003560 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3561 return( PSA_ERROR_INVALID_ARGUMENT );
3562
Gilles Peskine28f8f302019-07-24 13:30:31 +02003563 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003564 if( status != PSA_SUCCESS )
3565 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003566 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567 return( PSA_ERROR_INVALID_ARGUMENT );
3568
3569#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003570 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571 {
3572 mbedtls_rsa_context *rsa = slot->data.rsa;
3573 int ret;
3574
Gilles Peskine630a18a2018-06-29 17:49:35 +02003575 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576 return( PSA_ERROR_INVALID_ARGUMENT );
3577
3578#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003579 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003581 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3582 mbedtls_ctr_drbg_random,
3583 &global_data.ctr_drbg,
3584 MBEDTLS_RSA_PRIVATE,
3585 output_length,
3586 input,
3587 output,
3588 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589 }
3590 else
3591#endif /* MBEDTLS_PKCS1_V15 */
3592#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003593 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003595 psa_rsa_oaep_set_padding_mode( alg, rsa );
3596 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3597 mbedtls_ctr_drbg_random,
3598 &global_data.ctr_drbg,
3599 MBEDTLS_RSA_PRIVATE,
3600 salt, salt_length,
3601 output_length,
3602 input,
3603 output,
3604 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605 }
3606 else
3607#endif /* MBEDTLS_PKCS1_V21 */
3608 {
3609 return( PSA_ERROR_INVALID_ARGUMENT );
3610 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003611
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612 return( mbedtls_to_psa_error( ret ) );
3613 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003614 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003615#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 {
3617 return( PSA_ERROR_NOT_SUPPORTED );
3618 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003619}
Gilles Peskine20035e32018-02-03 22:44:14 +01003620
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003621
3622
mohammad1603503973b2018-03-12 15:59:30 +02003623/****************************************************************/
3624/* Symmetric cryptography */
3625/****************************************************************/
3626
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003627/* Initialize the cipher operation structure. Once this function has been
3628 * called, psa_cipher_abort can run and will do the right thing. */
3629static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3630 psa_algorithm_t alg )
3631{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003632 if( ! PSA_ALG_IS_CIPHER( alg ) )
3633 {
3634 memset( operation, 0, sizeof( *operation ) );
3635 return( PSA_ERROR_INVALID_ARGUMENT );
3636 }
3637
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003638 operation->alg = alg;
3639 operation->key_set = 0;
3640 operation->iv_set = 0;
3641 operation->iv_required = 1;
3642 operation->iv_size = 0;
3643 operation->block_size = 0;
3644 mbedtls_cipher_init( &operation->ctx.cipher );
3645 return( PSA_SUCCESS );
3646}
3647
Gilles Peskinee553c652018-06-04 16:22:46 +02003648static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003649 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003650 psa_algorithm_t alg,
3651 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003652{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003653 int ret = 0;
3654 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003655 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003656 size_t key_bits;
3657 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003658 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3659 PSA_KEY_USAGE_ENCRYPT :
3660 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003661
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003662 /* A context must be freshly initialized before it can be set up. */
3663 if( operation->alg != 0 )
3664 {
3665 return( PSA_ERROR_BAD_STATE );
3666 }
3667
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003668 status = psa_cipher_init( operation, alg );
3669 if( status != PSA_SUCCESS )
3670 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003671
Gilles Peskine28f8f302019-07-24 13:30:31 +02003672 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003673 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003674 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003675 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003676
Gilles Peskine8e338702019-07-30 20:06:31 +02003677 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003678 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003679 {
3680 status = PSA_ERROR_NOT_SUPPORTED;
3681 goto exit;
3682 }
mohammad1603503973b2018-03-12 15:59:30 +02003683
mohammad1603503973b2018-03-12 15:59:30 +02003684 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003685 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003686 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003687
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003688#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003689 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003690 {
3691 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003692 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003693 memcpy( keys, slot->data.raw.data, 16 );
3694 memcpy( keys + 16, slot->data.raw.data, 8 );
3695 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3696 keys,
3697 192, cipher_operation );
3698 }
3699 else
3700#endif
3701 {
3702 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3703 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003704 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003705 }
Moran Peker41deec42018-04-04 15:43:05 +03003706 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003707 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003708
mohammad16038481e742018-03-18 13:57:31 +02003709#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003710 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003711 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003712 case PSA_ALG_CBC_NO_PADDING:
3713 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3714 MBEDTLS_PADDING_NONE );
3715 break;
3716 case PSA_ALG_CBC_PKCS7:
3717 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3718 MBEDTLS_PADDING_PKCS7 );
3719 break;
3720 default:
3721 /* The algorithm doesn't involve padding. */
3722 ret = 0;
3723 break;
3724 }
3725 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003726 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003727#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3728
mohammad1603503973b2018-03-12 15:59:30 +02003729 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003730 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003731 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003732 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003733 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003734 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003735 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003736#if defined(MBEDTLS_CHACHA20_C)
3737 else
3738 if( alg == PSA_ALG_CHACHA20 )
3739 operation->iv_size = 12;
3740#endif
mohammad1603503973b2018-03-12 15:59:30 +02003741
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003742exit:
3743 if( status == 0 )
3744 status = mbedtls_to_psa_error( ret );
3745 if( status != 0 )
3746 psa_cipher_abort( operation );
3747 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003748}
3749
Gilles Peskinefe119512018-07-08 21:39:34 +02003750psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003751 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003752 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003753{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003754 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003755}
3756
Gilles Peskinefe119512018-07-08 21:39:34 +02003757psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003758 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003759 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003760{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003761 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003762}
3763
Gilles Peskinefe119512018-07-08 21:39:34 +02003764psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003765 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003766 size_t iv_size,
3767 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003768{
itayzafrir534bd7c2018-08-02 13:56:32 +03003769 psa_status_t status;
3770 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003771 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003772 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003773 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003774 }
Moran Peker41deec42018-04-04 15:43:05 +03003775 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003776 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003777 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003778 goto exit;
3779 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003780 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3781 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003782 if( ret != 0 )
3783 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003784 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003785 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003786 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003787
mohammad16038481e742018-03-18 13:57:31 +02003788 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003789 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003790
Moran Peker395db872018-05-31 14:07:14 +03003791exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003792 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003793 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003794 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003795}
3796
Gilles Peskinefe119512018-07-08 21:39:34 +02003797psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003798 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003799 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003800{
itayzafrir534bd7c2018-08-02 13:56:32 +03003801 psa_status_t status;
3802 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003803 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003804 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003805 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003806 }
Moran Pekera28258c2018-05-29 16:25:04 +03003807 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003808 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003809 status = PSA_ERROR_INVALID_ARGUMENT;
3810 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003811 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003812 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3813 status = mbedtls_to_psa_error( ret );
3814exit:
3815 if( status == PSA_SUCCESS )
3816 operation->iv_set = 1;
3817 else
mohammad1603503973b2018-03-12 15:59:30 +02003818 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003820}
3821
Gilles Peskinee553c652018-06-04 16:22:46 +02003822psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3823 const uint8_t *input,
3824 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003825 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003826 size_t output_size,
3827 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003828{
itayzafrir534bd7c2018-08-02 13:56:32 +03003829 psa_status_t status;
3830 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003831 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003832
3833 if( operation->alg == 0 )
3834 {
3835 return( PSA_ERROR_BAD_STATE );
3836 }
3837
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003838 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003839 {
3840 /* Take the unprocessed partial block left over from previous
3841 * update calls, if any, plus the input to this call. Remove
3842 * the last partial block, if any. You get the data that will be
3843 * output in this call. */
3844 expected_output_size =
3845 ( operation->ctx.cipher.unprocessed_len + input_length )
3846 / operation->block_size * operation->block_size;
3847 }
3848 else
3849 {
3850 expected_output_size = input_length;
3851 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003852
Gilles Peskine89d789c2018-06-04 17:17:16 +02003853 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003854 {
3855 status = PSA_ERROR_BUFFER_TOO_SMALL;
3856 goto exit;
3857 }
mohammad160382759612018-03-12 18:16:40 +02003858
mohammad1603503973b2018-03-12 15:59:30 +02003859 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003860 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003861 status = mbedtls_to_psa_error( ret );
3862exit:
3863 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003864 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003865 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003866}
3867
Gilles Peskinee553c652018-06-04 16:22:46 +02003868psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3869 uint8_t *output,
3870 size_t output_size,
3871 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003872{
David Saadab4ecc272019-02-14 13:48:10 +02003873 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003874 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003875 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003876
mohammad1603503973b2018-03-12 15:59:30 +02003877 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003878 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003879 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003880 }
3881 if( operation->iv_required && ! operation->iv_set )
3882 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003883 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003884 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003885
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003886 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003887 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3888 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003889 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003890 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003891 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003892 }
3893
Janos Follath315b51c2018-07-09 16:04:51 +01003894 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3895 temp_output_buffer,
3896 output_length );
3897 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003898 {
Janos Follath315b51c2018-07-09 16:04:51 +01003899 status = mbedtls_to_psa_error( cipher_ret );
3900 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003901 }
Janos Follath315b51c2018-07-09 16:04:51 +01003902
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003903 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003904 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003905 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003906 memcpy( output, temp_output_buffer, *output_length );
3907 else
3908 {
Janos Follath315b51c2018-07-09 16:04:51 +01003909 status = PSA_ERROR_BUFFER_TOO_SMALL;
3910 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003911 }
mohammad1603503973b2018-03-12 15:59:30 +02003912
Gilles Peskine3f108122018-12-07 18:14:53 +01003913 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003914 status = psa_cipher_abort( operation );
3915
3916 return( status );
3917
3918error:
3919
3920 *output_length = 0;
3921
Gilles Peskine3f108122018-12-07 18:14:53 +01003922 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003923 (void) psa_cipher_abort( operation );
3924
3925 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003926}
3927
Gilles Peskinee553c652018-06-04 16:22:46 +02003928psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3929{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003930 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003931 {
3932 /* The object has (apparently) been initialized but it is not
3933 * in use. It's ok to call abort on such an object, and there's
3934 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003935 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003936 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003937
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003938 /* Sanity check (shouldn't happen: operation->alg should
3939 * always have been initialized to a valid value). */
3940 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3941 return( PSA_ERROR_BAD_STATE );
3942
mohammad1603503973b2018-03-12 15:59:30 +02003943 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003944
Moran Peker41deec42018-04-04 15:43:05 +03003945 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003946 operation->key_set = 0;
3947 operation->iv_set = 0;
3948 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003949 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003950 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003951
Moran Peker395db872018-05-31 14:07:14 +03003952 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003953}
3954
Gilles Peskinea0655c32018-04-30 17:06:50 +02003955
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003956
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003957
mohammad16035955c982018-04-26 00:53:03 +03003958/****************************************************************/
3959/* AEAD */
3960/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003961
Gilles Peskineedf9a652018-08-17 18:11:56 +02003962typedef struct
3963{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003964 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003965 const mbedtls_cipher_info_t *cipher_info;
3966 union
3967 {
3968#if defined(MBEDTLS_CCM_C)
3969 mbedtls_ccm_context ccm;
3970#endif /* MBEDTLS_CCM_C */
3971#if defined(MBEDTLS_GCM_C)
3972 mbedtls_gcm_context gcm;
3973#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003974#if defined(MBEDTLS_CHACHAPOLY_C)
3975 mbedtls_chachapoly_context chachapoly;
3976#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003977 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003978 psa_algorithm_t core_alg;
3979 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003980 uint8_t tag_length;
3981} aead_operation_t;
3982
Gilles Peskine30a9e412019-01-14 18:36:12 +01003983static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003984{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003985 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003986 {
3987#if defined(MBEDTLS_CCM_C)
3988 case PSA_ALG_CCM:
3989 mbedtls_ccm_free( &operation->ctx.ccm );
3990 break;
3991#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003992#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003993 case PSA_ALG_GCM:
3994 mbedtls_gcm_free( &operation->ctx.gcm );
3995 break;
3996#endif /* MBEDTLS_GCM_C */
3997 }
3998}
3999
4000static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004001 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004002 psa_key_usage_t usage,
4003 psa_algorithm_t alg )
4004{
4005 psa_status_t status;
4006 size_t key_bits;
4007 mbedtls_cipher_id_t cipher_id;
4008
Gilles Peskine28f8f302019-07-24 13:30:31 +02004009 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004010 if( status != PSA_SUCCESS )
4011 return( status );
4012
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004013 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004014
4015 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004016 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004017 &cipher_id );
4018 if( operation->cipher_info == NULL )
4019 return( PSA_ERROR_NOT_SUPPORTED );
4020
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004021 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004022 {
4023#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004024 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4025 operation->core_alg = PSA_ALG_CCM;
4026 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004027 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4028 * The call to mbedtls_ccm_encrypt_and_tag or
4029 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004030 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004031 return( PSA_ERROR_INVALID_ARGUMENT );
4032 mbedtls_ccm_init( &operation->ctx.ccm );
4033 status = mbedtls_to_psa_error(
4034 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4035 operation->slot->data.raw.data,
4036 (unsigned int) key_bits ) );
4037 if( status != 0 )
4038 goto cleanup;
4039 break;
4040#endif /* MBEDTLS_CCM_C */
4041
4042#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004043 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4044 operation->core_alg = PSA_ALG_GCM;
4045 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004046 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4047 * The call to mbedtls_gcm_crypt_and_tag or
4048 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004049 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004050 return( PSA_ERROR_INVALID_ARGUMENT );
4051 mbedtls_gcm_init( &operation->ctx.gcm );
4052 status = mbedtls_to_psa_error(
4053 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4054 operation->slot->data.raw.data,
4055 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004056 if( status != 0 )
4057 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004058 break;
4059#endif /* MBEDTLS_GCM_C */
4060
Gilles Peskine26869f22019-05-06 15:25:00 +02004061#if defined(MBEDTLS_CHACHAPOLY_C)
4062 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4063 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4064 operation->full_tag_length = 16;
4065 /* We only support the default tag length. */
4066 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4067 return( PSA_ERROR_NOT_SUPPORTED );
4068 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4069 status = mbedtls_to_psa_error(
4070 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4071 operation->slot->data.raw.data ) );
4072 if( status != 0 )
4073 goto cleanup;
4074 break;
4075#endif /* MBEDTLS_CHACHAPOLY_C */
4076
Gilles Peskineedf9a652018-08-17 18:11:56 +02004077 default:
4078 return( PSA_ERROR_NOT_SUPPORTED );
4079 }
4080
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004081 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4082 {
4083 status = PSA_ERROR_INVALID_ARGUMENT;
4084 goto cleanup;
4085 }
4086 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004087
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 return( PSA_SUCCESS );
4089
4090cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004091 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004092 return( status );
4093}
4094
Gilles Peskinec5487a82018-12-03 18:08:14 +01004095psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004096 psa_algorithm_t alg,
4097 const uint8_t *nonce,
4098 size_t nonce_length,
4099 const uint8_t *additional_data,
4100 size_t additional_data_length,
4101 const uint8_t *plaintext,
4102 size_t plaintext_length,
4103 uint8_t *ciphertext,
4104 size_t ciphertext_size,
4105 size_t *ciphertext_length )
4106{
mohammad16035955c982018-04-26 00:53:03 +03004107 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004108 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004109 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004110
mohammad1603f08a5502018-06-03 15:05:47 +03004111 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004112
Gilles Peskinec5487a82018-12-03 18:08:14 +01004113 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004114 if( status != PSA_SUCCESS )
4115 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004116
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 /* For all currently supported modes, the tag is at the end of the
4118 * ciphertext. */
4119 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4120 {
4121 status = PSA_ERROR_BUFFER_TOO_SMALL;
4122 goto exit;
4123 }
4124 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004125
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004126#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004127 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004128 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004129 status = mbedtls_to_psa_error(
4130 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4131 MBEDTLS_GCM_ENCRYPT,
4132 plaintext_length,
4133 nonce, nonce_length,
4134 additional_data, additional_data_length,
4135 plaintext, ciphertext,
4136 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004137 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004138 else
4139#endif /* MBEDTLS_GCM_C */
4140#if defined(MBEDTLS_CCM_C)
4141 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004142 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143 status = mbedtls_to_psa_error(
4144 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4145 plaintext_length,
4146 nonce, nonce_length,
4147 additional_data,
4148 additional_data_length,
4149 plaintext, ciphertext,
4150 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004151 }
mohammad16035c8845f2018-05-09 05:40:09 -07004152 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004153#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004154#if defined(MBEDTLS_CHACHAPOLY_C)
4155 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4156 {
4157 if( nonce_length != 12 || operation.tag_length != 16 )
4158 {
4159 status = PSA_ERROR_NOT_SUPPORTED;
4160 goto exit;
4161 }
4162 status = mbedtls_to_psa_error(
4163 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4164 plaintext_length,
4165 nonce,
4166 additional_data,
4167 additional_data_length,
4168 plaintext,
4169 ciphertext,
4170 tag ) );
4171 }
4172 else
4173#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004174 {
mohammad1603554faad2018-06-03 15:07:38 +03004175 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004176 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004177
Gilles Peskineedf9a652018-08-17 18:11:56 +02004178 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4179 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004180
Gilles Peskineedf9a652018-08-17 18:11:56 +02004181exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004182 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004183 if( status == PSA_SUCCESS )
4184 *ciphertext_length = plaintext_length + operation.tag_length;
4185 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004186}
4187
Gilles Peskineee652a32018-06-01 19:23:52 +02004188/* Locate the tag in a ciphertext buffer containing the encrypted data
4189 * followed by the tag. Return the length of the part preceding the tag in
4190 * *plaintext_length. This is the size of the plaintext in modes where
4191 * the encrypted data has the same size as the plaintext, such as
4192 * CCM and GCM. */
4193static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4194 const uint8_t *ciphertext,
4195 size_t ciphertext_length,
4196 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004197 const uint8_t **p_tag )
4198{
4199 size_t payload_length;
4200 if( tag_length > ciphertext_length )
4201 return( PSA_ERROR_INVALID_ARGUMENT );
4202 payload_length = ciphertext_length - tag_length;
4203 if( payload_length > plaintext_size )
4204 return( PSA_ERROR_BUFFER_TOO_SMALL );
4205 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004206 return( PSA_SUCCESS );
4207}
4208
Gilles Peskinec5487a82018-12-03 18:08:14 +01004209psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004210 psa_algorithm_t alg,
4211 const uint8_t *nonce,
4212 size_t nonce_length,
4213 const uint8_t *additional_data,
4214 size_t additional_data_length,
4215 const uint8_t *ciphertext,
4216 size_t ciphertext_length,
4217 uint8_t *plaintext,
4218 size_t plaintext_size,
4219 size_t *plaintext_length )
4220{
mohammad16035955c982018-04-26 00:53:03 +03004221 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004222 aead_operation_t operation;
4223 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004224
Gilles Peskineee652a32018-06-01 19:23:52 +02004225 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004226
Gilles Peskinec5487a82018-12-03 18:08:14 +01004227 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004228 if( status != PSA_SUCCESS )
4229 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004230
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004231 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4232 ciphertext, ciphertext_length,
4233 plaintext_size, &tag );
4234 if( status != PSA_SUCCESS )
4235 goto exit;
4236
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004237#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004238 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004239 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004240 status = mbedtls_to_psa_error(
4241 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4242 ciphertext_length - operation.tag_length,
4243 nonce, nonce_length,
4244 additional_data,
4245 additional_data_length,
4246 tag, operation.tag_length,
4247 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004248 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004249 else
4250#endif /* MBEDTLS_GCM_C */
4251#if defined(MBEDTLS_CCM_C)
4252 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004253 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004254 status = mbedtls_to_psa_error(
4255 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4256 ciphertext_length - operation.tag_length,
4257 nonce, nonce_length,
4258 additional_data,
4259 additional_data_length,
4260 ciphertext, plaintext,
4261 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004262 }
mohammad160339574652018-06-01 04:39:53 -07004263 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004264#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004265#if defined(MBEDTLS_CHACHAPOLY_C)
4266 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4267 {
4268 if( nonce_length != 12 || operation.tag_length != 16 )
4269 {
4270 status = PSA_ERROR_NOT_SUPPORTED;
4271 goto exit;
4272 }
4273 status = mbedtls_to_psa_error(
4274 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4275 ciphertext_length - operation.tag_length,
4276 nonce,
4277 additional_data,
4278 additional_data_length,
4279 tag,
4280 ciphertext,
4281 plaintext ) );
4282 }
4283 else
4284#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004285 {
mohammad1603554faad2018-06-03 15:07:38 +03004286 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004287 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004288
Gilles Peskineedf9a652018-08-17 18:11:56 +02004289 if( status != PSA_SUCCESS && plaintext_size != 0 )
4290 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004291
Gilles Peskineedf9a652018-08-17 18:11:56 +02004292exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004293 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004294 if( status == PSA_SUCCESS )
4295 *plaintext_length = ciphertext_length - operation.tag_length;
4296 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004297}
4298
Gilles Peskinea0655c32018-04-30 17:06:50 +02004299
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004300
Gilles Peskine20035e32018-02-03 22:44:14 +01004301/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004302/* Generators */
4303/****************************************************************/
4304
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004305#define HKDF_STATE_INIT 0 /* no input yet */
4306#define HKDF_STATE_STARTED 1 /* got salt */
4307#define HKDF_STATE_KEYED 2 /* got key */
4308#define HKDF_STATE_OUTPUT 3 /* output started */
4309
Gilles Peskinecbe66502019-05-16 16:59:18 +02004310static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004312{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004313 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4314 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004315 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004316 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004317}
4318
4319
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004320psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004321{
4322 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004323 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004324 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004325 {
4326 /* The object has (apparently) been initialized but it is not
4327 * in use. It's ok to call abort on such an object, and there's
4328 * nothing to do. */
4329 }
4330 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004331#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004332 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004333 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004334 mbedtls_free( operation->ctx.hkdf.info );
4335 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004336 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004337 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004339 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004340 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004341#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004342 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004343 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4345 operation->ctx.tls12_prf.key_len );
4346 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004347 }
Hanno Becker580fba12018-11-13 20:50:45 +00004348
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004349 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004350 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4352 operation->ctx.tls12_prf.Ai_with_seed_len );
4353 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004354 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004355#else
4356 if( operation->ctx.tls12_prf.seed != NULL )
4357 {
4358 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4359 operation->ctx.tls12_prf.seed_length );
4360 mbedtls_free( operation->ctx.tls12_prf.seed );
4361 }
4362
4363 if( operation->ctx.tls12_prf.label != NULL )
4364 {
4365 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4366 operation->ctx.tls12_prf.label_length );
4367 mbedtls_free( operation->ctx.tls12_prf.label );
4368 }
4369
4370 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4371
4372 /* We leave the fields Ai and output_block to be erased safely by the
4373 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004374#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004375 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004376 else
4377#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004378 {
4379 status = PSA_ERROR_BAD_STATE;
4380 }
Janos Follath083036a2019-06-11 10:22:26 +01004381 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004382 return( status );
4383}
4384
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004385psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004386 size_t *capacity)
4387{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004389 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004391 return PSA_ERROR_BAD_STATE;
4392 }
4393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004395 return( PSA_SUCCESS );
4396}
4397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004399 size_t capacity )
4400{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004402 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004404 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004406 return( PSA_SUCCESS );
4407}
4408
Gilles Peskinebef7f142018-07-12 17:22:21 +02004409#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004411 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004412static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 psa_algorithm_t hash_alg,
4414 uint8_t *output,
4415 size_t output_length )
4416{
4417 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4418 psa_status_t status;
4419
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004420 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4421 return( PSA_ERROR_BAD_STATE );
4422 hkdf->state = HKDF_STATE_OUTPUT;
4423
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424 while( output_length != 0 )
4425 {
4426 /* Copy what remains of the current block */
4427 uint8_t n = hash_length - hkdf->offset_in_block;
4428 if( n > output_length )
4429 n = (uint8_t) output_length;
4430 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4431 output += n;
4432 output_length -= n;
4433 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004434 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004435 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004436 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004437 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004439 * object was corrupted or if this function is called directly
4440 * inside the library. */
4441 if( hkdf->block_number == 0xff )
4442 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004443
4444 /* We need a new block */
4445 ++hkdf->block_number;
4446 hkdf->offset_in_block = 0;
4447 status = psa_hmac_setup_internal( &hkdf->hmac,
4448 hkdf->prk, hash_length,
4449 hash_alg );
4450 if( status != PSA_SUCCESS )
4451 return( status );
4452 if( hkdf->block_number != 1 )
4453 {
4454 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4455 hkdf->output_block,
4456 hash_length );
4457 if( status != PSA_SUCCESS )
4458 return( status );
4459 }
4460 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4461 hkdf->info,
4462 hkdf->info_length );
4463 if( status != PSA_SUCCESS )
4464 return( status );
4465 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4466 &hkdf->block_number, 1 );
4467 if( status != PSA_SUCCESS )
4468 return( status );
4469 status = psa_hmac_finish_internal( &hkdf->hmac,
4470 hkdf->output_block,
4471 sizeof( hkdf->output_block ) );
4472 if( status != PSA_SUCCESS )
4473 return( status );
4474 }
4475
4476 return( PSA_SUCCESS );
4477}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004478
Janos Follath999f6482019-06-11 12:04:10 +01004479#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004480static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4481 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004482 psa_algorithm_t alg )
4483{
4484 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4485 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4486 psa_hmac_internal_data hmac;
4487 psa_status_t status, cleanup_status;
4488
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004489 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004490 size_t Ai_len;
4491
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004492 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004493 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004495 * object was corrupted or if this function is called directly
4496 * inside the library. */
4497 if( tls12_prf->block_number == 0xff )
4498 return( PSA_ERROR_BAD_STATE );
4499
4500 /* We need a new block */
4501 ++tls12_prf->block_number;
4502 tls12_prf->offset_in_block = 0;
4503
4504 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4505 *
4506 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4507 *
4508 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4509 * HMAC_hash(secret, A(2) + seed) +
4510 * HMAC_hash(secret, A(3) + seed) + ...
4511 *
4512 * A(0) = seed
4513 * A(i) = HMAC_hash( secret, A(i-1) )
4514 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004515 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004516 * `HMAC_hash(secret, A(i) + seed)` from which the output
4517 * is currently extracted as `output_block`, while
4518 * `A(i) + seed` is stored in `Ai_with_seed`.
4519 *
4520 * Generating a new block means recalculating `Ai_with_seed`
4521 * from the A(i)-part of it, and afterwards recalculating
4522 * `output_block`.
4523 *
4524 * A(0) is computed at setup time.
4525 *
4526 */
4527
4528 psa_hmac_init_internal( &hmac );
4529
4530 /* We must distinguish the calculation of A(1) from those
4531 * of A(2) and higher, because A(0)=seed has a different
4532 * length than the other A(i). */
4533 if( tls12_prf->block_number == 1 )
4534 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004535 Ai = tls12_prf->Ai_with_seed + hash_length;
4536 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004537 }
4538 else
4539 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004540 Ai = tls12_prf->Ai_with_seed;
4541 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004542 }
4543
Hanno Becker3b339e22018-11-13 20:56:14 +00004544 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4545 status = psa_hmac_setup_internal( &hmac,
4546 tls12_prf->key,
4547 tls12_prf->key_len,
4548 hash_alg );
4549 if( status != PSA_SUCCESS )
4550 goto cleanup;
4551
4552 status = psa_hash_update( &hmac.hash_ctx,
4553 Ai, Ai_len );
4554 if( status != PSA_SUCCESS )
4555 goto cleanup;
4556
4557 status = psa_hmac_finish_internal( &hmac,
4558 tls12_prf->Ai_with_seed,
4559 hash_length );
4560 if( status != PSA_SUCCESS )
4561 goto cleanup;
4562
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004563 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4564 status = psa_hmac_setup_internal( &hmac,
4565 tls12_prf->key,
4566 tls12_prf->key_len,
4567 hash_alg );
4568 if( status != PSA_SUCCESS )
4569 goto cleanup;
4570
4571 status = psa_hash_update( &hmac.hash_ctx,
4572 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004573 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004574 if( status != PSA_SUCCESS )
4575 goto cleanup;
4576
4577 status = psa_hmac_finish_internal( &hmac,
4578 tls12_prf->output_block,
4579 hash_length );
4580 if( status != PSA_SUCCESS )
4581 goto cleanup;
4582
4583cleanup:
4584
4585 cleanup_status = psa_hmac_abort_internal( &hmac );
4586 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4587 status = cleanup_status;
4588
4589 return( status );
4590}
Janos Follath7742fee2019-06-17 12:58:10 +01004591#else
4592static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4593 psa_tls12_prf_key_derivation_t *tls12_prf,
4594 psa_algorithm_t alg )
4595{
4596 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4597 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004598 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4599 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004600
Janos Follath7742fee2019-06-17 12:58:10 +01004601 /* We can't be wanting more output after block 0xff, otherwise
4602 * the capacity check in psa_key_derivation_output_bytes() would have
4603 * prevented this call. It could happen only if the operation
4604 * object was corrupted or if this function is called directly
4605 * inside the library. */
4606 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004607 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004608
4609 /* We need a new block */
4610 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004611 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004612
4613 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4614 *
4615 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4616 *
4617 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4618 * HMAC_hash(secret, A(2) + seed) +
4619 * HMAC_hash(secret, A(3) + seed) + ...
4620 *
4621 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004622 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004623 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004624 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004625 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004626 * is currently extracted as `output_block` and where i is
4627 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004628 */
4629
Janos Follathea29bfb2019-06-19 12:21:20 +01004630 /* Save the hash context before using it, to preserve the hash state with
4631 * only the inner padding in it. We need this, because inner padding depends
4632 * on the key (secret in the RFC's terminology). */
4633 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4634 if( status != PSA_SUCCESS )
4635 goto cleanup;
4636
4637 /* Calculate A(i) where i = tls12_prf->block_number. */
4638 if( tls12_prf->block_number == 1 )
4639 {
4640 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4641 * the variable seed and in this instance means it in the context of the
4642 * P_hash function, where seed = label + seed.) */
4643 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4644 tls12_prf->label, tls12_prf->label_length );
4645 if( status != PSA_SUCCESS )
4646 goto cleanup;
4647 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4648 tls12_prf->seed, tls12_prf->seed_length );
4649 if( status != PSA_SUCCESS )
4650 goto cleanup;
4651 }
4652 else
4653 {
4654 /* A(i) = HMAC_hash(secret, A(i-1)) */
4655 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4656 tls12_prf->Ai, hash_length );
4657 if( status != PSA_SUCCESS )
4658 goto cleanup;
4659 }
4660
4661 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4662 tls12_prf->Ai, hash_length );
4663 if( status != PSA_SUCCESS )
4664 goto cleanup;
4665 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4666 if( status != PSA_SUCCESS )
4667 goto cleanup;
4668
4669 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4670 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4671 tls12_prf->Ai, hash_length );
4672 if( status != PSA_SUCCESS )
4673 goto cleanup;
4674 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4675 tls12_prf->label, tls12_prf->label_length );
4676 if( status != PSA_SUCCESS )
4677 goto cleanup;
4678 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4679 tls12_prf->seed, tls12_prf->seed_length );
4680 if( status != PSA_SUCCESS )
4681 goto cleanup;
4682 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4683 tls12_prf->output_block, hash_length );
4684 if( status != PSA_SUCCESS )
4685 goto cleanup;
4686 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4687 if( status != PSA_SUCCESS )
4688 goto cleanup;
4689
Janos Follath7742fee2019-06-17 12:58:10 +01004690
4691cleanup:
4692
Janos Follathea29bfb2019-06-19 12:21:20 +01004693 cleanup_status = psa_hash_abort( &backup );
4694 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4695 status = cleanup_status;
4696
4697 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004698}
Janos Follath999f6482019-06-11 12:04:10 +01004699#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004700
Janos Follath999f6482019-06-11 12:04:10 +01004701#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004703 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004704static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004705 psa_tls12_prf_key_derivation_t *tls12_prf,
4706 psa_algorithm_t alg,
4707 uint8_t *output,
4708 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004709{
4710 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4711 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4712 psa_status_t status;
4713
4714 while( output_length != 0 )
4715 {
4716 /* Copy what remains of the current block */
4717 uint8_t n = hash_length - tls12_prf->offset_in_block;
4718
4719 /* Check if we have fully processed the current block. */
4720 if( n == 0 )
4721 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004722 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004723 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004724 if( status != PSA_SUCCESS )
4725 return( status );
4726
4727 continue;
4728 }
4729
4730 if( n > output_length )
4731 n = (uint8_t) output_length;
4732 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4733 n );
4734 output += n;
4735 output_length -= n;
4736 tls12_prf->offset_in_block += n;
4737 }
4738
4739 return( PSA_SUCCESS );
4740}
Janos Follath844eb0e2019-06-19 12:10:49 +01004741#else
4742static psa_status_t psa_key_derivation_tls12_prf_read(
4743 psa_tls12_prf_key_derivation_t *tls12_prf,
4744 psa_algorithm_t alg,
4745 uint8_t *output,
4746 size_t output_length )
4747{
4748 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4749 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4750 psa_status_t status;
4751 uint8_t offset, length;
4752
4753 while( output_length != 0 )
4754 {
4755 /* Check if we have fully processed the current block. */
4756 if( tls12_prf->left_in_block == 0 )
4757 {
4758 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4759 alg );
4760 if( status != PSA_SUCCESS )
4761 return( status );
4762
4763 continue;
4764 }
4765
4766 if( tls12_prf->left_in_block > output_length )
4767 length = (uint8_t) output_length;
4768 else
4769 length = tls12_prf->left_in_block;
4770
4771 offset = hash_length - tls12_prf->left_in_block;
4772 memcpy( output, tls12_prf->output_block + offset, length );
4773 output += length;
4774 output_length -= length;
4775 tls12_prf->left_in_block -= length;
4776 }
4777
4778 return( PSA_SUCCESS );
4779}
Janos Follath999f6482019-06-11 12:04:10 +01004780#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004781#endif /* MBEDTLS_MD_C */
4782
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004783psa_status_t psa_key_derivation_output_bytes(
4784 psa_key_derivation_operation_t *operation,
4785 uint8_t *output,
4786 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004787{
4788 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004792 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004794 return PSA_ERROR_BAD_STATE;
4795 }
4796
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004797 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004798 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004800 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004802 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004803 goto exit;
4804 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004808 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4810 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004813 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004816
Gilles Peskinebef7f142018-07-12 17:22:21 +02004817#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004818 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004819 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004820 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004822 output, output_length );
4823 }
Janos Follathadbec812019-06-14 11:05:39 +01004824 else
Janos Follathadbec812019-06-14 11:05:39 +01004825 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004826 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004827 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004830 output_length );
4831 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004832 else
4833#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004834 {
4835 return( PSA_ERROR_BAD_STATE );
4836 }
4837
4838exit:
4839 if( status != PSA_SUCCESS )
4840 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004841 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 * This allows us to differentiate between exhausted operations and
4843 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4844 * operations. */
4845 psa_algorithm_t alg = operation->alg;
4846 psa_key_derivation_abort( operation );
4847 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004848 memset( output, '!', output_length );
4849 }
4850 return( status );
4851}
4852
Gilles Peskine08542d82018-07-19 17:05:42 +02004853#if defined(MBEDTLS_DES_C)
4854static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4855{
4856 if( data_size >= 8 )
4857 mbedtls_des_key_set_parity( data );
4858 if( data_size >= 16 )
4859 mbedtls_des_key_set_parity( data + 8 );
4860 if( data_size >= 24 )
4861 mbedtls_des_key_set_parity( data + 16 );
4862}
4863#endif /* MBEDTLS_DES_C */
4864
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004865static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 psa_key_slot_t *slot,
4867 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004868 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004869{
4870 uint8_t *data = NULL;
4871 size_t bytes = PSA_BITS_TO_BYTES( bits );
4872 psa_status_t status;
4873
Gilles Peskine8e338702019-07-30 20:06:31 +02004874 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 return( PSA_ERROR_INVALID_ARGUMENT );
4876 if( bits % 8 != 0 )
4877 return( PSA_ERROR_INVALID_ARGUMENT );
4878 data = mbedtls_calloc( 1, bytes );
4879 if( data == NULL )
4880 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4881
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004883 if( status != PSA_SUCCESS )
4884 goto exit;
4885#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004886 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004887 psa_des_set_key_parity( data, bytes );
4888#endif /* MBEDTLS_DES_C */
4889 status = psa_import_key_into_slot( slot, data, bytes );
4890
4891exit:
4892 mbedtls_free( data );
4893 return( status );
4894}
4895
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004896psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004898 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004899{
4900 psa_status_t status;
4901 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004902 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004903 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4904 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004905#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4906 if( driver != NULL )
4907 {
4908 /* Deriving a key in a secure element is not implemented yet. */
4909 status = PSA_ERROR_NOT_SUPPORTED;
4910 }
4911#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004912 if( status == PSA_SUCCESS )
4913 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004914 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004915 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004917 }
4918 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004919 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004920 if( status != PSA_SUCCESS )
4921 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004922 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004923 *handle = 0;
4924 }
4925 return( status );
4926}
4927
Gilles Peskineeab56e42018-07-12 17:12:33 +02004928
4929
4930/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004931/* Key derivation */
4932/****************************************************************/
4933
Gilles Peskinea05219c2018-11-16 16:02:56 +01004934#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004935#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004937 * of the HKDF algorithm.
4938 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004939 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004940 * to potentially free embedded data structures and wipe confidential data.
4941 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004942static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004943 const uint8_t *secret,
4944 size_t secret_length,
4945 psa_algorithm_t hash_alg,
4946 const uint8_t *salt,
4947 size_t salt_length,
4948 const uint8_t *label,
4949 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004950{
4951 psa_status_t status;
4952 status = psa_hmac_setup_internal( &hkdf->hmac,
4953 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004954 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004955 if( status != PSA_SUCCESS )
4956 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004957 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004958 if( status != PSA_SUCCESS )
4959 return( status );
4960 status = psa_hmac_finish_internal( &hkdf->hmac,
4961 hkdf->prk,
4962 sizeof( hkdf->prk ) );
4963 if( status != PSA_SUCCESS )
4964 return( status );
4965 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4966 hkdf->block_number = 0;
4967 hkdf->info_length = label_length;
4968 if( label_length != 0 )
4969 {
4970 hkdf->info = mbedtls_calloc( 1, label_length );
4971 if( hkdf->info == NULL )
4972 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4973 memcpy( hkdf->info, label, label_length );
4974 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004975 hkdf->state = HKDF_STATE_KEYED;
4976 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004977 return( PSA_SUCCESS );
4978}
Janos Follath71a4c912019-06-11 09:14:47 +01004979#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004980#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004981
Gilles Peskinea05219c2018-11-16 16:02:56 +01004982#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004983#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004985 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004986 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004987 * to potentially free embedded data structures and wipe confidential data.
4988 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004989static psa_status_t psa_key_derivation_tls12_prf_setup(
4990 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004991 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004992 size_t key_len,
4993 psa_algorithm_t hash_alg,
4994 const uint8_t *salt,
4995 size_t salt_length,
4996 const uint8_t *label,
4997 size_t label_length )
4998{
4999 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00005000 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
5001 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005002
5003 tls12_prf->key = mbedtls_calloc( 1, key_len );
5004 if( tls12_prf->key == NULL )
5005 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5006 tls12_prf->key_len = key_len;
5007 memcpy( tls12_prf->key, key, key_len );
5008
Hanno Becker580fba12018-11-13 20:50:45 +00005009 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005010 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00005011 if( overflow )
5012 return( PSA_ERROR_INVALID_ARGUMENT );
5013
5014 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
5015 if( tls12_prf->Ai_with_seed == NULL )
5016 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5017 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
5018
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005019 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
5020 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00005021 if( label_length != 0 )
5022 {
5023 memcpy( tls12_prf->Ai_with_seed + hash_length,
5024 label, label_length );
5025 }
5026
5027 if( salt_length != 0 )
5028 {
5029 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
5030 salt, salt_length );
5031 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005032
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005033 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005034 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005035 tls12_prf->block_number = 0;
5036 tls12_prf->offset_in_block = hash_length;
5037
5038 return( PSA_SUCCESS );
5039}
Janos Follath71a4c912019-06-11 09:14:47 +01005040#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00005041
Janos Follath71a4c912019-06-11 09:14:47 +01005042#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005044static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
5045 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005046 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005047 size_t psk_len,
5048 psa_algorithm_t hash_alg,
5049 const uint8_t *salt,
5050 size_t salt_length,
5051 const uint8_t *label,
5052 size_t label_length )
5053{
5054 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005055 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00005056
5057 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5058 return( PSA_ERROR_INVALID_ARGUMENT );
5059
5060 /* Quoting RFC 4279, Section 2:
5061 *
5062 * The premaster secret is formed as follows: if the PSK is N octets
5063 * long, concatenate a uint16 with the value N, N zero octets, a second
5064 * uint16 with the value N, and the PSK itself.
5065 */
5066
5067 pms[0] = ( psk_len >> 8 ) & 0xff;
5068 pms[1] = ( psk_len >> 0 ) & 0xff;
5069 memset( pms + 2, 0, psk_len );
5070 pms[2 + psk_len + 0] = pms[0];
5071 pms[2 + psk_len + 1] = pms[1];
5072 memcpy( pms + 4 + psk_len, psk, psk_len );
5073
Gilles Peskinecbe66502019-05-16 16:59:18 +02005074 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005075 pms, 4 + 2 * psk_len,
5076 hash_alg,
5077 salt, salt_length,
5078 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005079
Gilles Peskine3f108122018-12-07 18:14:53 +01005080 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005081 return( status );
5082}
Janos Follath71a4c912019-06-11 09:14:47 +01005083#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005084#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005085
Janos Follath71a4c912019-06-11 09:14:47 +01005086#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005087/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005088 * to potentially free embedded data structures and wipe confidential data.
5089 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005090static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005092 const uint8_t *secret, size_t secret_length,
5093 psa_algorithm_t alg,
5094 const uint8_t *salt, size_t salt_length,
5095 const uint8_t *label, size_t label_length,
5096 size_t capacity )
5097{
5098 psa_status_t status;
5099 size_t max_capacity;
5100
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 /* Set operation->alg even on failure so that abort knows what to do. */
5102 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005103
5104#if defined(MBEDTLS_MD_C)
5105 if( PSA_ALG_IS_HKDF( alg ) )
5106 {
5107 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5108 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5109 if( hash_size == 0 )
5110 return( PSA_ERROR_NOT_SUPPORTED );
5111 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005113 secret, secret_length,
5114 hash_alg,
5115 salt, salt_length,
5116 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005117 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005118 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5119 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5120 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005121 {
5122 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5123 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5124
5125 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5126 if( hash_alg != PSA_ALG_SHA_256 &&
5127 hash_alg != PSA_ALG_SHA_384 )
5128 {
5129 return( PSA_ERROR_NOT_SUPPORTED );
5130 }
5131
5132 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005133
5134 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5135 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005137 secret, secret_length,
5138 hash_alg, salt, salt_length,
5139 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005140 }
5141 else
5142 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005143 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005145 secret, secret_length,
5146 hash_alg, salt, salt_length,
5147 label, label_length );
5148 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005149 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005150 else
5151#endif
5152 {
5153 return( PSA_ERROR_NOT_SUPPORTED );
5154 }
5155
5156 if( status != PSA_SUCCESS )
5157 return( status );
5158
5159 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005161 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005163 else
5164 return( PSA_ERROR_INVALID_ARGUMENT );
5165
5166 return( PSA_SUCCESS );
5167}
Janos Follath71a4c912019-06-11 09:14:47 +01005168#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005169
Janos Follath71a4c912019-06-11 09:14:47 +01005170#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005172 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005173 psa_algorithm_t alg,
5174 const uint8_t *salt,
5175 size_t salt_length,
5176 const uint8_t *label,
5177 size_t label_length,
5178 size_t capacity )
5179{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005180 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005181 psa_status_t status;
5182
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005183 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005184 return( PSA_ERROR_BAD_STATE );
5185
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005186 /* Make sure that alg is a key derivation algorithm. This prevents
5187 * key selection algorithms, which psa_key_derivation_internal
5188 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005189 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5190 return( PSA_ERROR_INVALID_ARGUMENT );
5191
Gilles Peskine28f8f302019-07-24 13:30:31 +02005192 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005193 if( status != PSA_SUCCESS )
5194 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005195
Gilles Peskine8e338702019-07-30 20:06:31 +02005196 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005197 return( PSA_ERROR_INVALID_ARGUMENT );
5198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005200 slot->data.raw.data,
5201 slot->data.raw.bytes,
5202 alg,
5203 salt, salt_length,
5204 label, label_length,
5205 capacity );
5206 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005208 return( status );
5209}
Janos Follath71a4c912019-06-11 09:14:47 +01005210#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005211
Gilles Peskine969c5d62019-01-16 15:53:06 +01005212static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215{
Janos Follath5fe19732019-06-20 15:09:30 +01005216 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5217 * initialisers for this union leave some bytes unspecified.) */
5218 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5219
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005222 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5223 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5224 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005226 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5228 if( hash_size == 0 )
5229 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005230 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5231 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005232 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005233 {
5234 return( PSA_ERROR_NOT_SUPPORTED );
5235 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005237 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005238 }
5239#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005242}
5243
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005245 psa_algorithm_t alg )
5246{
5247 psa_status_t status;
5248
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005250 return( PSA_ERROR_BAD_STATE );
5251
Gilles Peskine6843c292019-01-18 16:44:49 +01005252 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5253 return( PSA_ERROR_INVALID_ARGUMENT );
5254 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005255 {
5256 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005258 }
5259 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5260 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005262 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005263 else
5264 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005265
5266 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005269}
5270
5271#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005272static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273 psa_algorithm_t hash_alg,
5274 psa_key_derivation_step_t step,
5275 const uint8_t *data,
5276 size_t data_length )
5277{
5278 psa_status_t status;
5279 switch( step )
5280 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005281 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005282 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005284 status = psa_hmac_setup_internal( &hkdf->hmac,
5285 data, data_length,
5286 hash_alg );
5287 if( status != PSA_SUCCESS )
5288 return( status );
5289 hkdf->state = HKDF_STATE_STARTED;
5290 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005291 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005292 /* If no salt was provided, use an empty salt. */
5293 if( hkdf->state == HKDF_STATE_INIT )
5294 {
5295 status = psa_hmac_setup_internal( &hkdf->hmac,
5296 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005297 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298 if( status != PSA_SUCCESS )
5299 return( status );
5300 hkdf->state = HKDF_STATE_STARTED;
5301 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005302 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005303 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005304 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5305 data, data_length );
5306 if( status != PSA_SUCCESS )
5307 return( status );
5308 status = psa_hmac_finish_internal( &hkdf->hmac,
5309 hkdf->prk,
5310 sizeof( hkdf->prk ) );
5311 if( status != PSA_SUCCESS )
5312 return( status );
5313 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5314 hkdf->block_number = 0;
5315 hkdf->state = HKDF_STATE_KEYED;
5316 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005317 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318 if( hkdf->state == HKDF_STATE_OUTPUT )
5319 return( PSA_ERROR_BAD_STATE );
5320 if( hkdf->info_set )
5321 return( PSA_ERROR_BAD_STATE );
5322 hkdf->info_length = data_length;
5323 if( data_length != 0 )
5324 {
5325 hkdf->info = mbedtls_calloc( 1, data_length );
5326 if( hkdf->info == NULL )
5327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5328 memcpy( hkdf->info, data, data_length );
5329 }
5330 hkdf->info_set = 1;
5331 return( PSA_SUCCESS );
5332 default:
5333 return( PSA_ERROR_INVALID_ARGUMENT );
5334 }
5335}
Janos Follathb03233e2019-06-11 15:30:30 +01005336
5337#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5338static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5339 psa_algorithm_t hash_alg,
5340 psa_key_derivation_step_t step,
5341 const uint8_t *data,
5342 size_t data_length )
5343{
5344 (void) prf;
5345 (void) hash_alg;
5346 (void) step;
5347 (void) data;
5348 (void) data_length;
5349
5350 return( PSA_ERROR_INVALID_ARGUMENT );
5351}
Janos Follath6660f0e2019-06-17 08:44:03 +01005352
5353static psa_status_t psa_tls12_prf_psk_to_ms_input(
5354 psa_tls12_prf_key_derivation_t *prf,
5355 psa_algorithm_t hash_alg,
5356 psa_key_derivation_step_t step,
5357 const uint8_t *data,
5358 size_t data_length )
5359{
5360 (void) prf;
5361 (void) hash_alg;
5362 (void) step;
5363 (void) data;
5364 (void) data_length;
5365
5366 return( PSA_ERROR_INVALID_ARGUMENT );
5367}
Janos Follathb03233e2019-06-11 15:30:30 +01005368#else
Janos Follathf08e2652019-06-13 09:05:41 +01005369static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5370 const uint8_t *data,
5371 size_t data_length )
5372{
5373 if( prf->state != TLS12_PRF_STATE_INIT )
5374 return( PSA_ERROR_BAD_STATE );
5375
Janos Follathd6dce9f2019-07-04 09:11:38 +01005376 if( data_length != 0 )
5377 {
5378 prf->seed = mbedtls_calloc( 1, data_length );
5379 if( prf->seed == NULL )
5380 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005381
Janos Follathd6dce9f2019-07-04 09:11:38 +01005382 memcpy( prf->seed, data, data_length );
5383 prf->seed_length = data_length;
5384 }
Janos Follathf08e2652019-06-13 09:05:41 +01005385
5386 prf->state = TLS12_PRF_STATE_SEED_SET;
5387
5388 return( PSA_SUCCESS );
5389}
5390
Janos Follath81550542019-06-13 14:26:34 +01005391static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5392 psa_algorithm_t hash_alg,
5393 const uint8_t *data,
5394 size_t data_length )
5395{
5396 psa_status_t status;
5397 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5398 return( PSA_ERROR_BAD_STATE );
5399
5400 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5401 if( status != PSA_SUCCESS )
5402 return( status );
5403
5404 prf->state = TLS12_PRF_STATE_KEY_SET;
5405
5406 return( PSA_SUCCESS );
5407}
5408
Janos Follath6660f0e2019-06-17 08:44:03 +01005409static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5410 psa_tls12_prf_key_derivation_t *prf,
5411 psa_algorithm_t hash_alg,
5412 const uint8_t *data,
5413 size_t data_length )
5414{
5415 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005416 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5417 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005418
5419 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5420 return( PSA_ERROR_INVALID_ARGUMENT );
5421
5422 /* Quoting RFC 4279, Section 2:
5423 *
5424 * The premaster secret is formed as follows: if the PSK is N octets
5425 * long, concatenate a uint16 with the value N, N zero octets, a second
5426 * uint16 with the value N, and the PSK itself.
5427 */
5428
Janos Follath40e13932019-06-26 13:22:29 +01005429 *cur++ = ( data_length >> 8 ) & 0xff;
5430 *cur++ = ( data_length >> 0 ) & 0xff;
5431 memset( cur, 0, data_length );
5432 cur += data_length;
5433 *cur++ = pms[0];
5434 *cur++ = pms[1];
5435 memcpy( cur, data, data_length );
5436 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005437
Janos Follath40e13932019-06-26 13:22:29 +01005438 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005439
5440 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5441 return( status );
5442}
5443
Janos Follath63028dd2019-06-13 09:15:47 +01005444static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5445 const uint8_t *data,
5446 size_t data_length )
5447{
5448 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5449 return( PSA_ERROR_BAD_STATE );
5450
Janos Follathd6dce9f2019-07-04 09:11:38 +01005451 if( data_length != 0 )
5452 {
5453 prf->label = mbedtls_calloc( 1, data_length );
5454 if( prf->label == NULL )
5455 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005456
Janos Follathd6dce9f2019-07-04 09:11:38 +01005457 memcpy( prf->label, data, data_length );
5458 prf->label_length = data_length;
5459 }
Janos Follath63028dd2019-06-13 09:15:47 +01005460
5461 prf->state = TLS12_PRF_STATE_LABEL_SET;
5462
5463 return( PSA_SUCCESS );
5464}
5465
Janos Follathb03233e2019-06-11 15:30:30 +01005466static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5467 psa_algorithm_t hash_alg,
5468 psa_key_derivation_step_t step,
5469 const uint8_t *data,
5470 size_t data_length )
5471{
Janos Follathb03233e2019-06-11 15:30:30 +01005472 switch( step )
5473 {
Janos Follathf08e2652019-06-13 09:05:41 +01005474 case PSA_KEY_DERIVATION_INPUT_SEED:
5475 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005476 case PSA_KEY_DERIVATION_INPUT_SECRET:
5477 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005478 case PSA_KEY_DERIVATION_INPUT_LABEL:
5479 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005480 default:
5481 return( PSA_ERROR_INVALID_ARGUMENT );
5482 }
5483}
Janos Follath6660f0e2019-06-17 08:44:03 +01005484
5485static psa_status_t psa_tls12_prf_psk_to_ms_input(
5486 psa_tls12_prf_key_derivation_t *prf,
5487 psa_algorithm_t hash_alg,
5488 psa_key_derivation_step_t step,
5489 const uint8_t *data,
5490 size_t data_length )
5491{
5492 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005493 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005494 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5495 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005496 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005497
5498 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5499}
Janos Follathb03233e2019-06-11 15:30:30 +01005500#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501#endif /* MBEDTLS_MD_C */
5502
Janos Follathb80a94e2019-06-12 15:54:46 +01005503static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005505 psa_key_derivation_step_t step,
5506 const uint8_t *data,
5507 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005508{
5509 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005511
5512#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005513 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005514 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005516 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005517 step, data, data_length );
5518 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005519 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005520#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005522 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005523 {
Janos Follathb03233e2019-06-11 15:30:30 +01005524 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5525 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5526 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005527 }
5528 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5529 {
5530 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5531 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5532 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005533 }
5534 else
5535#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005536 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005538 return( PSA_ERROR_BAD_STATE );
5539 }
5540
5541 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005542 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005543 return( status );
5544}
5545
Janos Follath51f4a0f2019-06-14 11:35:55 +01005546psa_status_t psa_key_derivation_input_bytes(
5547 psa_key_derivation_operation_t *operation,
5548 psa_key_derivation_step_t step,
5549 const uint8_t *data,
5550 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005551{
Janos Follathc5621512019-06-14 11:27:57 +01005552 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5553 return( PSA_ERROR_INVALID_ARGUMENT );
5554
5555 return( psa_key_derivation_input_internal( operation, step,
5556 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005557}
5558
Janos Follath51f4a0f2019-06-14 11:35:55 +01005559psa_status_t psa_key_derivation_input_key(
5560 psa_key_derivation_operation_t *operation,
5561 psa_key_derivation_step_t step,
5562 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005563{
5564 psa_key_slot_t *slot;
5565 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005566 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005567 PSA_KEY_USAGE_DERIVE,
5568 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005569 if( status != PSA_SUCCESS )
5570 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005571 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005572 return( PSA_ERROR_INVALID_ARGUMENT );
5573 /* Don't allow a key to be used as an input that is usually public.
5574 * This is debatable. It's ok from a cryptographic perspective to
5575 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005576 * the material should be dedicated to a particular input step,
5577 * otherwise this may allow the key to be used in an unintended way
5578 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005579 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005580 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005581 return( psa_key_derivation_input_internal( operation,
5582 step,
5583 slot->data.raw.data,
5584 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005585}
5586
Gilles Peskineea0fb492018-07-12 17:17:20 +02005587
5588
5589/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005590/* Key agreement */
5591/****************************************************************/
5592
Gilles Peskinea05219c2018-11-16 16:02:56 +01005593#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005594static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5595 size_t peer_key_length,
5596 const mbedtls_ecp_keypair *our_key,
5597 uint8_t *shared_secret,
5598 size_t shared_secret_size,
5599 size_t *shared_secret_length )
5600{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005601 mbedtls_ecp_keypair *their_key = NULL;
5602 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005603 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005604 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005605
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005606 status = psa_import_ec_public_key(
5607 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5608 peer_key, peer_key_length,
5609 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005610 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005611 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005612
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005613 status = mbedtls_to_psa_error(
5614 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5615 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005616 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005617 status = mbedtls_to_psa_error(
5618 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5619 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005620 goto exit;
5621
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005622 status = mbedtls_to_psa_error(
5623 mbedtls_ecdh_calc_secret( &ecdh,
5624 shared_secret_length,
5625 shared_secret, shared_secret_size,
5626 mbedtls_ctr_drbg_random,
5627 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005628
5629exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005630 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005631 mbedtls_ecp_keypair_free( their_key );
5632 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005633 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005634}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005635#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005636
Gilles Peskine01d718c2018-09-18 12:01:02 +02005637#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5638
Gilles Peskine0216fe12019-04-11 21:23:21 +02005639static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5640 psa_key_slot_t *private_key,
5641 const uint8_t *peer_key,
5642 size_t peer_key_length,
5643 uint8_t *shared_secret,
5644 size_t shared_secret_size,
5645 size_t *shared_secret_length )
5646{
5647 switch( alg )
5648 {
5649#if defined(MBEDTLS_ECDH_C)
5650 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005651 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005652 return( PSA_ERROR_INVALID_ARGUMENT );
5653 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5654 private_key->data.ecp,
5655 shared_secret, shared_secret_size,
5656 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005657#endif /* MBEDTLS_ECDH_C */
5658 default:
5659 (void) private_key;
5660 (void) peer_key;
5661 (void) peer_key_length;
5662 (void) shared_secret;
5663 (void) shared_secret_size;
5664 (void) shared_secret_length;
5665 return( PSA_ERROR_NOT_SUPPORTED );
5666 }
5667}
5668
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005669/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005670 * to potentially free embedded data structures and wipe confidential data.
5671 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005672static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005673 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005674 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005675 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005676 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005677{
5678 psa_status_t status;
5679 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5680 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005682
5683 /* Step 1: run the secret agreement algorithm to generate the shared
5684 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005685 status = psa_key_agreement_raw_internal( ka_alg,
5686 private_key,
5687 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005688 shared_secret,
5689 sizeof( shared_secret ),
5690 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005691 if( status != PSA_SUCCESS )
5692 goto exit;
5693
5694 /* Step 2: set up the key derivation to generate key material from
5695 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005696 status = psa_key_derivation_input_internal( operation, step,
5697 shared_secret,
5698 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005699
Gilles Peskine01d718c2018-09-18 12:01:02 +02005700exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005701 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005702 return( status );
5703}
5704
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005705psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005706 psa_key_derivation_step_t step,
5707 psa_key_handle_t private_key,
5708 const uint8_t *peer_key,
5709 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005710{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005711 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005712 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005714 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005715 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005716 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005717 if( status != PSA_SUCCESS )
5718 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005720 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005722 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005724 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005725}
5726
Gilles Peskinebe697d82019-05-16 18:00:41 +02005727psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5728 psa_key_handle_t private_key,
5729 const uint8_t *peer_key,
5730 size_t peer_key_length,
5731 uint8_t *output,
5732 size_t output_size,
5733 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005734{
5735 psa_key_slot_t *slot;
5736 psa_status_t status;
5737
5738 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5739 {
5740 status = PSA_ERROR_INVALID_ARGUMENT;
5741 goto exit;
5742 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005743 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005744 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005745 if( status != PSA_SUCCESS )
5746 goto exit;
5747
5748 status = psa_key_agreement_raw_internal( alg, slot,
5749 peer_key, peer_key_length,
5750 output, output_size,
5751 output_length );
5752
5753exit:
5754 if( status != PSA_SUCCESS )
5755 {
5756 /* If an error happens and is not handled properly, the output
5757 * may be used as a key to protect sensitive data. Arrange for such
5758 * a key to be random, which is likely to result in decryption or
5759 * verification errors. This is better than filling the buffer with
5760 * some constant data such as zeros, which would result in the data
5761 * being protected with a reproducible, easily knowable key.
5762 */
5763 psa_generate_random( output, output_size );
5764 *output_length = output_size;
5765 }
5766 return( status );
5767}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005768
5769
5770/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005771/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005772/****************************************************************/
5773
5774psa_status_t psa_generate_random( uint8_t *output,
5775 size_t output_size )
5776{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005777 int ret;
5778 GUARD_MODULE_INITIALIZED;
5779
Gilles Peskinef181eca2019-08-07 13:49:00 +02005780 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5781 {
5782 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5783 output,
5784 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5785 if( ret != 0 )
5786 return( mbedtls_to_psa_error( ret ) );
5787 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5788 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5789 }
5790
itayzafrir0adf0fc2018-09-06 16:24:41 +03005791 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005792 return( mbedtls_to_psa_error( ret ) );
5793}
5794
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005795#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5796#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005797
Gilles Peskine7228da22019-07-15 11:06:15 +02005798psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005799 size_t seed_size )
5800{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005801 if( global_data.initialized )
5802 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005803
5804 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5805 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5806 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5807 return( PSA_ERROR_INVALID_ARGUMENT );
5808
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005809 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005810}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005811#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005812
Gilles Peskinee56e8782019-04-26 17:34:02 +02005813#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5814static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5815 size_t domain_parameters_size,
5816 int *exponent )
5817{
5818 size_t i;
5819 uint32_t acc = 0;
5820
5821 if( domain_parameters_size == 0 )
5822 {
5823 *exponent = 65537;
5824 return( PSA_SUCCESS );
5825 }
5826
5827 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5828 * support values that fit in a 32-bit integer, which is larger than
5829 * int on just about every platform anyway. */
5830 if( domain_parameters_size > sizeof( acc ) )
5831 return( PSA_ERROR_NOT_SUPPORTED );
5832 for( i = 0; i < domain_parameters_size; i++ )
5833 acc = ( acc << 8 ) | domain_parameters[i];
5834 if( acc > INT_MAX )
5835 return( PSA_ERROR_NOT_SUPPORTED );
5836 *exponent = acc;
5837 return( PSA_SUCCESS );
5838}
5839#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5840
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005841static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005842 psa_key_slot_t *slot, size_t bits,
5843 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005844{
Gilles Peskine8e338702019-07-30 20:06:31 +02005845 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005846
Gilles Peskinee56e8782019-04-26 17:34:02 +02005847 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005848 return( PSA_ERROR_INVALID_ARGUMENT );
5849
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005850 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005851 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005852 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005853 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005854 if( status != PSA_SUCCESS )
5855 return( status );
5856 status = psa_generate_random( slot->data.raw.data,
5857 slot->data.raw.bytes );
5858 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005859 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005860#if defined(MBEDTLS_DES_C)
5861 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005862 psa_des_set_key_parity( slot->data.raw.data,
5863 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005864#endif /* MBEDTLS_DES_C */
5865 }
5866 else
5867
5868#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005869 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005870 {
5871 mbedtls_rsa_context *rsa;
5872 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005873 int exponent;
5874 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005875 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5876 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005877 /* Accept only byte-aligned keys, for the same reasons as
5878 * in psa_import_rsa_key(). */
5879 if( bits % 8 != 0 )
5880 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005881 status = psa_read_rsa_exponent( domain_parameters,
5882 domain_parameters_size,
5883 &exponent );
5884 if( status != PSA_SUCCESS )
5885 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005886 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5887 if( rsa == NULL )
5888 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5889 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5890 ret = mbedtls_rsa_gen_key( rsa,
5891 mbedtls_ctr_drbg_random,
5892 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005893 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894 exponent );
5895 if( ret != 0 )
5896 {
5897 mbedtls_rsa_free( rsa );
5898 mbedtls_free( rsa );
5899 return( mbedtls_to_psa_error( ret ) );
5900 }
5901 slot->data.rsa = rsa;
5902 }
5903 else
5904#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5905
5906#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005907 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005908 {
5909 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5910 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5911 const mbedtls_ecp_curve_info *curve_info =
5912 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5913 mbedtls_ecp_keypair *ecp;
5914 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005915 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005916 return( PSA_ERROR_NOT_SUPPORTED );
5917 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5918 return( PSA_ERROR_NOT_SUPPORTED );
5919 if( curve_info->bit_size != bits )
5920 return( PSA_ERROR_INVALID_ARGUMENT );
5921 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5922 if( ecp == NULL )
5923 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5924 mbedtls_ecp_keypair_init( ecp );
5925 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5926 mbedtls_ctr_drbg_random,
5927 &global_data.ctr_drbg );
5928 if( ret != 0 )
5929 {
5930 mbedtls_ecp_keypair_free( ecp );
5931 mbedtls_free( ecp );
5932 return( mbedtls_to_psa_error( ret ) );
5933 }
5934 slot->data.ecp = ecp;
5935 }
5936 else
5937#endif /* MBEDTLS_ECP_C */
5938
5939 return( PSA_ERROR_NOT_SUPPORTED );
5940
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005941 return( PSA_SUCCESS );
5942}
5943
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005944psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005945 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005946{
5947 psa_status_t status;
5948 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005949 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02005950 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5951 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005952#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5953 if( driver != NULL )
5954 {
5955 /* Generating a key in a secure element is not implemented yet. */
5956 status = PSA_ERROR_NOT_SUPPORTED;
5957 }
5958#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005959 if( status == PSA_SUCCESS )
5960 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005961 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005962 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005963 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005964 }
5965 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005966 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005967 if( status != PSA_SUCCESS )
5968 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005969 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005970 *handle = 0;
5971 }
5972 return( status );
5973}
5974
5975
Gilles Peskine05d69892018-06-19 22:00:52 +02005976
5977/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005978/* Module setup */
5979/****************************************************************/
5980
Gilles Peskine5e769522018-11-20 21:59:56 +01005981psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5982 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5983 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5984{
5985 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5986 return( PSA_ERROR_BAD_STATE );
5987 global_data.entropy_init = entropy_init;
5988 global_data.entropy_free = entropy_free;
5989 return( PSA_SUCCESS );
5990}
5991
Gilles Peskinee59236f2018-01-27 23:32:46 +01005992void mbedtls_psa_crypto_free( void )
5993{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005994 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005995 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5996 {
5997 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005998 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005999 }
6000 /* Wipe all remaining data, including configuration.
6001 * In particular, this sets all state indicator to the value
6002 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006003 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006004#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006005 /* Unregister all secure element drivers, so that we restart from
6006 * a pristine state. */
6007 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006008#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006009}
6010
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006011#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6012/** Recover a transaction that was interrupted by a power failure.
6013 *
6014 * This function is called during initialization, before psa_crypto_init()
6015 * returns. If this function returns a failure status, the initialization
6016 * fails.
6017 */
6018static psa_status_t psa_crypto_recover_transaction(
6019 const psa_crypto_transaction_t *transaction )
6020{
6021 switch( transaction->unknown.type )
6022 {
6023 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6024 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
6025 /* TOnogrepDO - fall through to the failure case until this
6026 * is implemented */
6027 default:
6028 /* We found an unsupported transaction in the storage.
6029 * We don't know what state the storage is in. Give up. */
6030 return( PSA_ERROR_STORAGE_FAILURE );
6031 }
6032}
6033#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6034
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035psa_status_t psa_crypto_init( void )
6036{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006037 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038 const unsigned char drbg_seed[] = "PSA";
6039
Gilles Peskinec6b69072018-11-20 21:42:52 +01006040 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006041 if( global_data.initialized != 0 )
6042 return( PSA_SUCCESS );
6043
Gilles Peskine5e769522018-11-20 21:59:56 +01006044 /* Set default configuration if
6045 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6046 if( global_data.entropy_init == NULL )
6047 global_data.entropy_init = mbedtls_entropy_init;
6048 if( global_data.entropy_free == NULL )
6049 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006050
Gilles Peskinec6b69072018-11-20 21:42:52 +01006051 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006052 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006053#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6054 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6055 /* The PSA entropy injection feature depends on using NV seed as an entropy
6056 * source. Add NV seed as an entropy source for PSA entropy injection. */
6057 mbedtls_entropy_add_source( &global_data.entropy,
6058 mbedtls_nv_seed_poll, NULL,
6059 MBEDTLS_ENTROPY_BLOCK_SIZE,
6060 MBEDTLS_ENTROPY_SOURCE_STRONG );
6061#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006062 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006063 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006064 status = mbedtls_to_psa_error(
6065 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6066 mbedtls_entropy_func,
6067 &global_data.entropy,
6068 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6069 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006071 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072
Gilles Peskine66fb1262018-12-10 16:29:04 +01006073 status = psa_initialize_key_slots( );
6074 if( status != PSA_SUCCESS )
6075 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006076
Gilles Peskine4b734222019-07-24 15:56:31 +02006077#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006078 status = psa_crypto_load_transaction( );
6079 if( status == PSA_SUCCESS )
6080 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006081 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6082 if( status != PSA_SUCCESS )
6083 goto exit;
6084 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006085 }
6086 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6087 {
6088 /* There's no transaction to complete. It's all good. */
6089 status = PSA_SUCCESS;
6090 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006091#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006092
Gilles Peskinec6b69072018-11-20 21:42:52 +01006093 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006094 global_data.initialized = 1;
6095
Gilles Peskinee59236f2018-01-27 23:32:46 +01006096exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006097 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006098 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006099 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006100}
6101
6102#endif /* MBEDTLS_PSA_CRYPTO_C */