blob: 3a78f5653da9719b8dcb46aee41209b11d0f6322 [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 ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001830 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001831 &bits );
1832 if( status != PSA_SUCCESS )
1833 goto exit;
1834 if( bits > PSA_MAX_KEY_BITS )
1835 {
1836 status = PSA_ERROR_NOT_SUPPORTED;
1837 goto exit;
1838 }
1839 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001840 }
1841 else
1842#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1843 {
1844 status = psa_import_key_into_slot( slot, data, data_length );
1845 if( status != PSA_SUCCESS )
1846 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001847 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001848 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001849 if( status != PSA_SUCCESS )
1850 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851
Gilles Peskine011e4282019-06-26 18:34:38 +02001852 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 if( status != PSA_SUCCESS )
1855 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001856 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001857 *handle = 0;
1858 }
1859 return( status );
1860}
1861
Gilles Peskined7729582019-08-05 15:55:54 +02001862#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1863psa_status_t mbedtls_psa_register_se_key(
1864 const psa_key_attributes_t *attributes )
1865{
1866 psa_status_t status;
1867 psa_key_slot_t *slot = NULL;
1868 psa_se_drv_table_entry_t *driver = NULL;
1869 const psa_drv_se_t *drv;
1870 psa_key_handle_t handle = 0;
1871
1872 /* Leaving attributes unspecified is not currently supported.
1873 * It could make sense to query the key type and size from the
1874 * secure element, but not all secure elements support this
1875 * and the driver HAL doesn't currently support it. */
1876 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1877 return( PSA_ERROR_NOT_SUPPORTED );
1878 if( psa_get_key_bits( attributes ) == 0 )
1879 return( PSA_ERROR_NOT_SUPPORTED );
1880
1881 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1882 &handle, &slot, &driver );
1883 if( status != PSA_SUCCESS )
1884 goto exit;
1885
1886 if( driver == NULL )
1887 {
1888 status = PSA_ERROR_INVALID_ARGUMENT;
1889 goto exit;
1890 }
1891 drv = psa_get_se_driver_methods( driver );
1892
1893 if ( psa_get_key_slot_number( attributes,
1894 &slot->data.se.slot_number ) != PSA_SUCCESS )
1895 {
1896 /* The application didn't specify a slot number. This doesn't
1897 * make sense when registering a slot. */
1898 status = PSA_ERROR_INVALID_ARGUMENT;
1899 goto exit;
1900 }
1901
Gilles Peskinea5f87492019-08-05 16:46:18 +02001902 /* If the driver has a slot number validation method, call it.
1903 * If it doesn't, it means the secure element is unable to validate
1904 * anything and so we have to trust the application. */
1905 if( drv->key_management != NULL &&
1906 drv->key_management->p_validate_slot_number != NULL )
1907 {
1908 status = drv->key_management->p_validate_slot_number(
1909 psa_get_se_driver_context( driver ),
1910 attributes,
1911 PSA_KEY_CREATION_REGISTER,
1912 slot->data.se.slot_number );
1913 if( status != PSA_SUCCESS )
1914 goto exit;
1915 }
1916
Gilles Peskined7729582019-08-05 15:55:54 +02001917 status = psa_finish_key_creation( slot, driver );
1918
1919exit:
1920 if( status != PSA_SUCCESS )
1921 {
1922 psa_fail_key_creation( slot, driver );
1923 }
1924 /* Registration doesn't keep the key in RAM. */
1925 psa_close_key( handle );
1926 return( status );
1927}
1928#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1929
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001930static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001931 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001932{
1933 psa_status_t status;
1934 uint8_t *buffer = NULL;
1935 size_t buffer_size = 0;
1936 size_t length;
1937
Gilles Peskine8e338702019-07-30 20:06:31 +02001938 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001939 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001940 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001941 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001942 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001943 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1944 if( status != PSA_SUCCESS )
1945 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001946 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001947 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001948
1949exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001950 if( buffer_size != 0 )
1951 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001952 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001953 return( status );
1954}
1955
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001956psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1957 const psa_key_attributes_t *specified_attributes,
1958 psa_key_handle_t *target_handle )
1959{
1960 psa_status_t status;
1961 psa_key_slot_t *source_slot = NULL;
1962 psa_key_slot_t *target_slot = NULL;
1963 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001964 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965
Gilles Peskine28f8f302019-07-24 13:30:31 +02001966 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001967 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968 if( status != PSA_SUCCESS )
1969 goto exit;
1970
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001971 status = psa_validate_optional_attributes( source_slot,
1972 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973 if( status != PSA_SUCCESS )
1974 goto exit;
1975
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001976 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001977 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001978 if( status != PSA_SUCCESS )
1979 goto exit;
1980
Gilles Peskinedf179142019-07-15 22:02:14 +02001981 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1982 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001983 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001984 if( status != PSA_SUCCESS )
1985 goto exit;
1986
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001987#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1988 if( driver != NULL )
1989 {
1990 /* Copying to a secure element is not implemented yet. */
1991 status = PSA_ERROR_NOT_SUPPORTED;
1992 goto exit;
1993 }
1994#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1995
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001996 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997 if( status != PSA_SUCCESS )
1998 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999
Gilles Peskine011e4282019-06-26 18:34:38 +02002000 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002 if( status != PSA_SUCCESS )
2003 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 *target_handle = 0;
2006 }
2007 return( status );
2008}
2009
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002010
2011
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002012/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002013/* Message digests */
2014/****************************************************************/
2015
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002016static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002017{
2018 switch( alg )
2019 {
2020#if defined(MBEDTLS_MD2_C)
2021 case PSA_ALG_MD2:
2022 return( &mbedtls_md2_info );
2023#endif
2024#if defined(MBEDTLS_MD4_C)
2025 case PSA_ALG_MD4:
2026 return( &mbedtls_md4_info );
2027#endif
2028#if defined(MBEDTLS_MD5_C)
2029 case PSA_ALG_MD5:
2030 return( &mbedtls_md5_info );
2031#endif
2032#if defined(MBEDTLS_RIPEMD160_C)
2033 case PSA_ALG_RIPEMD160:
2034 return( &mbedtls_ripemd160_info );
2035#endif
2036#if defined(MBEDTLS_SHA1_C)
2037 case PSA_ALG_SHA_1:
2038 return( &mbedtls_sha1_info );
2039#endif
2040#if defined(MBEDTLS_SHA256_C)
2041 case PSA_ALG_SHA_224:
2042 return( &mbedtls_sha224_info );
2043 case PSA_ALG_SHA_256:
2044 return( &mbedtls_sha256_info );
2045#endif
2046#if defined(MBEDTLS_SHA512_C)
2047 case PSA_ALG_SHA_384:
2048 return( &mbedtls_sha384_info );
2049 case PSA_ALG_SHA_512:
2050 return( &mbedtls_sha512_info );
2051#endif
2052 default:
2053 return( NULL );
2054 }
2055}
2056
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002057psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2058{
2059 switch( operation->alg )
2060 {
Gilles Peskine81736312018-06-26 15:04:31 +02002061 case 0:
2062 /* The object has (apparently) been initialized but it is not
2063 * in use. It's ok to call abort on such an object, and there's
2064 * nothing to do. */
2065 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002066#if defined(MBEDTLS_MD2_C)
2067 case PSA_ALG_MD2:
2068 mbedtls_md2_free( &operation->ctx.md2 );
2069 break;
2070#endif
2071#if defined(MBEDTLS_MD4_C)
2072 case PSA_ALG_MD4:
2073 mbedtls_md4_free( &operation->ctx.md4 );
2074 break;
2075#endif
2076#if defined(MBEDTLS_MD5_C)
2077 case PSA_ALG_MD5:
2078 mbedtls_md5_free( &operation->ctx.md5 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_RIPEMD160_C)
2082 case PSA_ALG_RIPEMD160:
2083 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_SHA1_C)
2087 case PSA_ALG_SHA_1:
2088 mbedtls_sha1_free( &operation->ctx.sha1 );
2089 break;
2090#endif
2091#if defined(MBEDTLS_SHA256_C)
2092 case PSA_ALG_SHA_224:
2093 case PSA_ALG_SHA_256:
2094 mbedtls_sha256_free( &operation->ctx.sha256 );
2095 break;
2096#endif
2097#if defined(MBEDTLS_SHA512_C)
2098 case PSA_ALG_SHA_384:
2099 case PSA_ALG_SHA_512:
2100 mbedtls_sha512_free( &operation->ctx.sha512 );
2101 break;
2102#endif
2103 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002104 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002105 }
2106 operation->alg = 0;
2107 return( PSA_SUCCESS );
2108}
2109
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002110psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002111 psa_algorithm_t alg )
2112{
2113 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002114
2115 /* A context must be freshly initialized before it can be set up. */
2116 if( operation->alg != 0 )
2117 {
2118 return( PSA_ERROR_BAD_STATE );
2119 }
2120
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 switch( alg )
2122 {
2123#if defined(MBEDTLS_MD2_C)
2124 case PSA_ALG_MD2:
2125 mbedtls_md2_init( &operation->ctx.md2 );
2126 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2127 break;
2128#endif
2129#if defined(MBEDTLS_MD4_C)
2130 case PSA_ALG_MD4:
2131 mbedtls_md4_init( &operation->ctx.md4 );
2132 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2133 break;
2134#endif
2135#if defined(MBEDTLS_MD5_C)
2136 case PSA_ALG_MD5:
2137 mbedtls_md5_init( &operation->ctx.md5 );
2138 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2139 break;
2140#endif
2141#if defined(MBEDTLS_RIPEMD160_C)
2142 case PSA_ALG_RIPEMD160:
2143 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2144 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2145 break;
2146#endif
2147#if defined(MBEDTLS_SHA1_C)
2148 case PSA_ALG_SHA_1:
2149 mbedtls_sha1_init( &operation->ctx.sha1 );
2150 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_SHA256_C)
2154 case PSA_ALG_SHA_224:
2155 mbedtls_sha256_init( &operation->ctx.sha256 );
2156 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2157 break;
2158 case PSA_ALG_SHA_256:
2159 mbedtls_sha256_init( &operation->ctx.sha256 );
2160 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA512_C)
2164 case PSA_ALG_SHA_384:
2165 mbedtls_sha512_init( &operation->ctx.sha512 );
2166 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2167 break;
2168 case PSA_ALG_SHA_512:
2169 mbedtls_sha512_init( &operation->ctx.sha512 );
2170 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2171 break;
2172#endif
2173 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002174 return( PSA_ALG_IS_HASH( alg ) ?
2175 PSA_ERROR_NOT_SUPPORTED :
2176 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 }
2178 if( ret == 0 )
2179 operation->alg = alg;
2180 else
2181 psa_hash_abort( operation );
2182 return( mbedtls_to_psa_error( ret ) );
2183}
2184
2185psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2186 const uint8_t *input,
2187 size_t input_length )
2188{
2189 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002190
2191 /* Don't require hash implementations to behave correctly on a
2192 * zero-length input, which may have an invalid pointer. */
2193 if( input_length == 0 )
2194 return( PSA_SUCCESS );
2195
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 switch( operation->alg )
2197 {
2198#if defined(MBEDTLS_MD2_C)
2199 case PSA_ALG_MD2:
2200 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2201 input, input_length );
2202 break;
2203#endif
2204#if defined(MBEDTLS_MD4_C)
2205 case PSA_ALG_MD4:
2206 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2207 input, input_length );
2208 break;
2209#endif
2210#if defined(MBEDTLS_MD5_C)
2211 case PSA_ALG_MD5:
2212 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2213 input, input_length );
2214 break;
2215#endif
2216#if defined(MBEDTLS_RIPEMD160_C)
2217 case PSA_ALG_RIPEMD160:
2218 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2219 input, input_length );
2220 break;
2221#endif
2222#if defined(MBEDTLS_SHA1_C)
2223 case PSA_ALG_SHA_1:
2224 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2225 input, input_length );
2226 break;
2227#endif
2228#if defined(MBEDTLS_SHA256_C)
2229 case PSA_ALG_SHA_224:
2230 case PSA_ALG_SHA_256:
2231 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_SHA512_C)
2236 case PSA_ALG_SHA_384:
2237 case PSA_ALG_SHA_512:
2238 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2239 input, input_length );
2240 break;
2241#endif
2242 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002243 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002245
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246 if( ret != 0 )
2247 psa_hash_abort( operation );
2248 return( mbedtls_to_psa_error( ret ) );
2249}
2250
2251psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2252 uint8_t *hash,
2253 size_t hash_size,
2254 size_t *hash_length )
2255{
itayzafrir40835d42018-08-02 13:14:17 +03002256 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002258 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259
2260 /* Fill the output buffer with something that isn't a valid hash
2261 * (barring an attack on the hash and deliberately-crafted input),
2262 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002263 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002264 /* If hash_size is 0 then hash may be NULL and then the
2265 * call to memset would have undefined behavior. */
2266 if( hash_size != 0 )
2267 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268
2269 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002270 {
2271 status = PSA_ERROR_BUFFER_TOO_SMALL;
2272 goto exit;
2273 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274
2275 switch( operation->alg )
2276 {
2277#if defined(MBEDTLS_MD2_C)
2278 case PSA_ALG_MD2:
2279 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2280 break;
2281#endif
2282#if defined(MBEDTLS_MD4_C)
2283 case PSA_ALG_MD4:
2284 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2285 break;
2286#endif
2287#if defined(MBEDTLS_MD5_C)
2288 case PSA_ALG_MD5:
2289 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2290 break;
2291#endif
2292#if defined(MBEDTLS_RIPEMD160_C)
2293 case PSA_ALG_RIPEMD160:
2294 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2295 break;
2296#endif
2297#if defined(MBEDTLS_SHA1_C)
2298 case PSA_ALG_SHA_1:
2299 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2300 break;
2301#endif
2302#if defined(MBEDTLS_SHA256_C)
2303 case PSA_ALG_SHA_224:
2304 case PSA_ALG_SHA_256:
2305 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_SHA512_C)
2309 case PSA_ALG_SHA_384:
2310 case PSA_ALG_SHA_512:
2311 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2312 break;
2313#endif
2314 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002315 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 }
itayzafrir40835d42018-08-02 13:14:17 +03002317 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318
itayzafrir40835d42018-08-02 13:14:17 +03002319exit:
2320 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002322 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323 return( psa_hash_abort( operation ) );
2324 }
2325 else
2326 {
2327 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002328 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 }
2330}
2331
Gilles Peskine2d277862018-06-18 15:41:12 +02002332psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2333 const uint8_t *hash,
2334 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335{
2336 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2337 size_t actual_hash_length;
2338 psa_status_t status = psa_hash_finish( operation,
2339 actual_hash, sizeof( actual_hash ),
2340 &actual_hash_length );
2341 if( status != PSA_SUCCESS )
2342 return( status );
2343 if( actual_hash_length != hash_length )
2344 return( PSA_ERROR_INVALID_SIGNATURE );
2345 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2346 return( PSA_ERROR_INVALID_SIGNATURE );
2347 return( PSA_SUCCESS );
2348}
2349
Gilles Peskineeb35d782019-01-22 17:56:16 +01002350psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2351 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002352{
2353 if( target_operation->alg != 0 )
2354 return( PSA_ERROR_BAD_STATE );
2355
2356 switch( source_operation->alg )
2357 {
2358 case 0:
2359 return( PSA_ERROR_BAD_STATE );
2360#if defined(MBEDTLS_MD2_C)
2361 case PSA_ALG_MD2:
2362 mbedtls_md2_clone( &target_operation->ctx.md2,
2363 &source_operation->ctx.md2 );
2364 break;
2365#endif
2366#if defined(MBEDTLS_MD4_C)
2367 case PSA_ALG_MD4:
2368 mbedtls_md4_clone( &target_operation->ctx.md4,
2369 &source_operation->ctx.md4 );
2370 break;
2371#endif
2372#if defined(MBEDTLS_MD5_C)
2373 case PSA_ALG_MD5:
2374 mbedtls_md5_clone( &target_operation->ctx.md5,
2375 &source_operation->ctx.md5 );
2376 break;
2377#endif
2378#if defined(MBEDTLS_RIPEMD160_C)
2379 case PSA_ALG_RIPEMD160:
2380 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2381 &source_operation->ctx.ripemd160 );
2382 break;
2383#endif
2384#if defined(MBEDTLS_SHA1_C)
2385 case PSA_ALG_SHA_1:
2386 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2387 &source_operation->ctx.sha1 );
2388 break;
2389#endif
2390#if defined(MBEDTLS_SHA256_C)
2391 case PSA_ALG_SHA_224:
2392 case PSA_ALG_SHA_256:
2393 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2394 &source_operation->ctx.sha256 );
2395 break;
2396#endif
2397#if defined(MBEDTLS_SHA512_C)
2398 case PSA_ALG_SHA_384:
2399 case PSA_ALG_SHA_512:
2400 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2401 &source_operation->ctx.sha512 );
2402 break;
2403#endif
2404 default:
2405 return( PSA_ERROR_NOT_SUPPORTED );
2406 }
2407
2408 target_operation->alg = source_operation->alg;
2409 return( PSA_SUCCESS );
2410}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411
2412
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414/* MAC */
2415/****************************************************************/
2416
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002417static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418 psa_algorithm_t alg,
2419 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002420 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002421 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002424 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002426 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002427 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002428
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2430 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002431 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002433 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002434 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 mode = MBEDTLS_MODE_STREAM;
2436 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437 case PSA_ALG_CTR:
2438 mode = MBEDTLS_MODE_CTR;
2439 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002440 case PSA_ALG_CFB:
2441 mode = MBEDTLS_MODE_CFB;
2442 break;
2443 case PSA_ALG_OFB:
2444 mode = MBEDTLS_MODE_OFB;
2445 break;
2446 case PSA_ALG_CBC_NO_PADDING:
2447 mode = MBEDTLS_MODE_CBC;
2448 break;
2449 case PSA_ALG_CBC_PKCS7:
2450 mode = MBEDTLS_MODE_CBC;
2451 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002452 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 mode = MBEDTLS_MODE_CCM;
2454 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002455 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 mode = MBEDTLS_MODE_GCM;
2457 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002458 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2459 mode = MBEDTLS_MODE_CHACHAPOLY;
2460 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461 default:
2462 return( NULL );
2463 }
2464 }
2465 else if( alg == PSA_ALG_CMAC )
2466 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 else
2468 return( NULL );
2469
2470 switch( key_type )
2471 {
2472 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002473 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002474 break;
2475 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002476 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2477 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002479 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002481 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002482 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2483 * but two-key Triple-DES is functionally three-key Triple-DES
2484 * with K1=K3, so that's how we present it to mbedtls. */
2485 if( key_bits == 128 )
2486 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487 break;
2488 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002489 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 break;
2491 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002494 case PSA_KEY_TYPE_CHACHA20:
2495 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2496 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497 default:
2498 return( NULL );
2499 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002500 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002501 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502
Jaeden Amero23bbb752018-06-26 14:16:54 +01002503 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2504 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505}
2506
Gilles Peskinea05219c2018-11-16 16:02:56 +01002507#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002508static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002509{
Gilles Peskine2d277862018-06-18 15:41:12 +02002510 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002511 {
2512 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002513 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002514 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002515 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002516 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002517 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002518 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002519 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002520 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002521 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002522 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002523 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002524 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002525 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002526 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002527 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002528 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002529 return( 128 );
2530 default:
2531 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002532 }
2533}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002534#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002535
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002536/* Initialize the MAC operation structure. Once this function has been
2537 * called, psa_mac_abort can run and will do the right thing. */
2538static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2539 psa_algorithm_t alg )
2540{
2541 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2542
2543 operation->alg = alg;
2544 operation->key_set = 0;
2545 operation->iv_set = 0;
2546 operation->iv_required = 0;
2547 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002548 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002549
2550#if defined(MBEDTLS_CMAC_C)
2551 if( alg == PSA_ALG_CMAC )
2552 {
2553 operation->iv_required = 0;
2554 mbedtls_cipher_init( &operation->ctx.cmac );
2555 status = PSA_SUCCESS;
2556 }
2557 else
2558#endif /* MBEDTLS_CMAC_C */
2559#if defined(MBEDTLS_MD_C)
2560 if( PSA_ALG_IS_HMAC( operation->alg ) )
2561 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002562 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2563 operation->ctx.hmac.hash_ctx.alg = 0;
2564 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002565 }
2566 else
2567#endif /* MBEDTLS_MD_C */
2568 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002569 if( ! PSA_ALG_IS_MAC( alg ) )
2570 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571 }
2572
2573 if( status != PSA_SUCCESS )
2574 memset( operation, 0, sizeof( *operation ) );
2575 return( status );
2576}
2577
Gilles Peskine01126fa2018-07-12 17:04:55 +02002578#if defined(MBEDTLS_MD_C)
2579static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2580{
Gilles Peskine3f108122018-12-07 18:14:53 +01002581 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002582 return( psa_hash_abort( &hmac->hash_ctx ) );
2583}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002584
Janos Follath999f6482019-06-11 12:04:10 +01002585#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002586static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2587{
2588 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2589 memset( hmac, 0, sizeof( *hmac ) );
2590}
Janos Follath999f6482019-06-11 12:04:10 +01002591#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002592#endif /* MBEDTLS_MD_C */
2593
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2595{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002596 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002598 /* The object has (apparently) been initialized but it is not
2599 * in use. It's ok to call abort on such an object, and there's
2600 * nothing to do. */
2601 return( PSA_SUCCESS );
2602 }
2603 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002605 if( operation->alg == PSA_ALG_CMAC )
2606 {
2607 mbedtls_cipher_free( &operation->ctx.cmac );
2608 }
2609 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002612 if( PSA_ALG_IS_HMAC( operation->alg ) )
2613 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002614 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002615 }
2616 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002618 {
2619 /* Sanity check (shouldn't happen: operation->alg should
2620 * always have been initialized to a valid value). */
2621 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622 }
Moran Peker41deec42018-04-04 15:43:05 +03002623
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 operation->alg = 0;
2625 operation->key_set = 0;
2626 operation->iv_set = 0;
2627 operation->iv_required = 0;
2628 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002629 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002630
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002632
2633bad_state:
2634 /* If abort is called on an uninitialized object, we can't trust
2635 * anything. Wipe the object in case it contains confidential data.
2636 * This may result in a memory leak if a pointer gets overwritten,
2637 * but it's too late to do anything about this. */
2638 memset( operation, 0, sizeof( *operation ) );
2639 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640}
2641
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002642#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002643static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002644 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002645 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002646 const mbedtls_cipher_info_t *cipher_info )
2647{
2648 int ret;
2649
2650 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002651
2652 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2653 if( ret != 0 )
2654 return( ret );
2655
2656 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2657 slot->data.raw.data,
2658 key_bits );
2659 return( ret );
2660}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002661#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002662
Gilles Peskine248051a2018-06-20 16:09:38 +02002663#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002664static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2665 const uint8_t *key,
2666 size_t key_length,
2667 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002668{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002669 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002670 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002671 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002672 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673 psa_status_t status;
2674
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002675 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2676 * overflow below. This should never trigger if the hash algorithm
2677 * is implemented correctly. */
2678 /* The size checks against the ipad and opad buffers cannot be written
2679 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2680 * because that triggers -Wlogical-op on GCC 7.3. */
2681 if( block_size > sizeof( ipad ) )
2682 return( PSA_ERROR_NOT_SUPPORTED );
2683 if( block_size > sizeof( hmac->opad ) )
2684 return( PSA_ERROR_NOT_SUPPORTED );
2685 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002686 return( PSA_ERROR_NOT_SUPPORTED );
2687
Gilles Peskined223b522018-06-11 18:12:58 +02002688 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002690 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002691 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002692 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002693 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002695 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002696 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002697 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002698 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002699 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002700 }
Gilles Peskine96889972018-07-12 17:07:03 +02002701 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2702 * but it is permitted. It is common when HMAC is used in HKDF, for
2703 * example. Don't call `memcpy` in the 0-length because `key` could be
2704 * an invalid pointer which would make the behavior undefined. */
2705 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002706 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002707
Gilles Peskined223b522018-06-11 18:12:58 +02002708 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2709 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002711 ipad[i] ^= 0x36;
2712 memset( ipad + key_length, 0x36, block_size - key_length );
2713
2714 /* Copy the key material from ipad to opad, flipping the requisite bits,
2715 * and filling the rest of opad with the requisite constant. */
2716 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002717 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2718 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719
Gilles Peskine01126fa2018-07-12 17:04:55 +02002720 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002722 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002723
Gilles Peskine01126fa2018-07-12 17:04:55 +02002724 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002725
2726cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002727 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002728
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729 return( status );
2730}
Gilles Peskine248051a2018-06-20 16:09:38 +02002731#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732
Gilles Peskine89167cb2018-07-08 20:12:23 +02002733static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002734 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002735 psa_algorithm_t alg,
2736 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002739 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002741 psa_key_usage_t usage =
2742 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002743 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002744 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002746 /* A context must be freshly initialized before it can be set up. */
2747 if( operation->alg != 0 )
2748 {
2749 return( PSA_ERROR_BAD_STATE );
2750 }
2751
Gilles Peskined911eb72018-08-14 15:18:45 +02002752 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002753 if( status != PSA_SUCCESS )
2754 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002755 if( is_sign )
2756 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757
Gilles Peskine28f8f302019-07-24 13:30:31 +02002758 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002759 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002760 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002761 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002762
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002764 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002765 {
2766 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002767 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002768 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002769 int ret;
2770 if( cipher_info == NULL )
2771 {
2772 status = PSA_ERROR_NOT_SUPPORTED;
2773 goto exit;
2774 }
2775 operation->mac_size = cipher_info->block_size;
2776 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2777 status = mbedtls_to_psa_error( ret );
2778 }
2779 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002782 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002783 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002784 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002785 if( hash_alg == 0 )
2786 {
2787 status = PSA_ERROR_NOT_SUPPORTED;
2788 goto exit;
2789 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002790
2791 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2792 /* Sanity check. This shouldn't fail on a valid configuration. */
2793 if( operation->mac_size == 0 ||
2794 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2795 {
2796 status = PSA_ERROR_NOT_SUPPORTED;
2797 goto exit;
2798 }
2799
Gilles Peskine8e338702019-07-30 20:06:31 +02002800 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002801 {
2802 status = PSA_ERROR_INVALID_ARGUMENT;
2803 goto exit;
2804 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002805
Gilles Peskine01126fa2018-07-12 17:04:55 +02002806 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2807 slot->data.raw.data,
2808 slot->data.raw.bytes,
2809 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002810 }
2811 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002813 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002814 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002815 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002817
Gilles Peskined911eb72018-08-14 15:18:45 +02002818 if( truncated == 0 )
2819 {
2820 /* The "normal" case: untruncated algorithm. Nothing to do. */
2821 }
2822 else if( truncated < 4 )
2823 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002824 /* A very short MAC is too short for security since it can be
2825 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2826 * so we make this our minimum, even though 32 bits is still
2827 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002828 status = PSA_ERROR_NOT_SUPPORTED;
2829 }
2830 else if( truncated > operation->mac_size )
2831 {
2832 /* It's impossible to "truncate" to a larger length. */
2833 status = PSA_ERROR_INVALID_ARGUMENT;
2834 }
2835 else
2836 operation->mac_size = truncated;
2837
Gilles Peskinefbfac682018-07-08 20:51:54 +02002838exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002839 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002841 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002843 else
2844 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002845 operation->key_set = 1;
2846 }
2847 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848}
2849
Gilles Peskine89167cb2018-07-08 20:12:23 +02002850psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002851 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002852 psa_algorithm_t alg )
2853{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002854 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002855}
2856
2857psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002858 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002859 psa_algorithm_t alg )
2860{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002861 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002862}
2863
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2865 const uint8_t *input,
2866 size_t input_length )
2867{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002870 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002872 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873 operation->has_input = 1;
2874
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002877 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002878 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2879 input, input_length );
2880 status = mbedtls_to_psa_error( ret );
2881 }
2882 else
2883#endif /* MBEDTLS_CMAC_C */
2884#if defined(MBEDTLS_MD_C)
2885 if( PSA_ALG_IS_HMAC( operation->alg ) )
2886 {
2887 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2888 input_length );
2889 }
2890 else
2891#endif /* MBEDTLS_MD_C */
2892 {
2893 /* This shouldn't happen if `operation` was initialized by
2894 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002895 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002896 }
2897
Gilles Peskinefbfac682018-07-08 20:51:54 +02002898 if( status != PSA_SUCCESS )
2899 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002900 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901}
2902
Gilles Peskine01126fa2018-07-12 17:04:55 +02002903#if defined(MBEDTLS_MD_C)
2904static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2905 uint8_t *mac,
2906 size_t mac_size )
2907{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002908 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002909 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2910 size_t hash_size = 0;
2911 size_t block_size = psa_get_hash_block_size( hash_alg );
2912 psa_status_t status;
2913
Gilles Peskine01126fa2018-07-12 17:04:55 +02002914 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2915 if( status != PSA_SUCCESS )
2916 return( status );
2917 /* From here on, tmp needs to be wiped. */
2918
2919 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2920 if( status != PSA_SUCCESS )
2921 goto exit;
2922
2923 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2924 if( status != PSA_SUCCESS )
2925 goto exit;
2926
2927 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2928 if( status != PSA_SUCCESS )
2929 goto exit;
2930
Gilles Peskined911eb72018-08-14 15:18:45 +02002931 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2932 if( status != PSA_SUCCESS )
2933 goto exit;
2934
2935 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002936
2937exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002938 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002939 return( status );
2940}
2941#endif /* MBEDTLS_MD_C */
2942
mohammad16036df908f2018-04-02 08:34:15 -07002943static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002944 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002945 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002947 if( ! operation->key_set )
2948 return( PSA_ERROR_BAD_STATE );
2949 if( operation->iv_required && ! operation->iv_set )
2950 return( PSA_ERROR_BAD_STATE );
2951
Gilles Peskine8c9def32018-02-08 10:02:12 +01002952 if( mac_size < operation->mac_size )
2953 return( PSA_ERROR_BUFFER_TOO_SMALL );
2954
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 if( operation->alg == PSA_ALG_CMAC )
2957 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002958 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2959 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2960 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002961 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002962 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002963 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002965 else
2966#endif /* MBEDTLS_CMAC_C */
2967#if defined(MBEDTLS_MD_C)
2968 if( PSA_ALG_IS_HMAC( operation->alg ) )
2969 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002970 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002971 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002972 }
2973 else
2974#endif /* MBEDTLS_MD_C */
2975 {
2976 /* This shouldn't happen if `operation` was initialized by
2977 * a setup function. */
2978 return( PSA_ERROR_BAD_STATE );
2979 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002980}
2981
Gilles Peskineacd4be32018-07-08 19:56:25 +02002982psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2983 uint8_t *mac,
2984 size_t mac_size,
2985 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002986{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002987 psa_status_t status;
2988
Jaeden Amero252ef282019-02-15 14:05:35 +00002989 if( operation->alg == 0 )
2990 {
2991 return( PSA_ERROR_BAD_STATE );
2992 }
2993
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002994 /* Fill the output buffer with something that isn't a valid mac
2995 * (barring an attack on the mac and deliberately-crafted input),
2996 * in case the caller doesn't check the return status properly. */
2997 *mac_length = mac_size;
2998 /* If mac_size is 0 then mac may be NULL and then the
2999 * call to memset would have undefined behavior. */
3000 if( mac_size != 0 )
3001 memset( mac, '!', mac_size );
3002
Gilles Peskine89167cb2018-07-08 20:12:23 +02003003 if( ! operation->is_sign )
3004 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003005 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003006 }
mohammad16036df908f2018-04-02 08:34:15 -07003007
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003008 status = psa_mac_finish_internal( operation, mac, mac_size );
3009
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003010 if( status == PSA_SUCCESS )
3011 {
3012 status = psa_mac_abort( operation );
3013 if( status == PSA_SUCCESS )
3014 *mac_length = operation->mac_size;
3015 else
3016 memset( mac, '!', mac_size );
3017 }
3018 else
3019 psa_mac_abort( operation );
3020 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003021}
3022
Gilles Peskineacd4be32018-07-08 19:56:25 +02003023psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3024 const uint8_t *mac,
3025 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003026{
Gilles Peskine828ed142018-06-18 23:25:51 +02003027 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003028 psa_status_t status;
3029
Jaeden Amero252ef282019-02-15 14:05:35 +00003030 if( operation->alg == 0 )
3031 {
3032 return( PSA_ERROR_BAD_STATE );
3033 }
3034
Gilles Peskine89167cb2018-07-08 20:12:23 +02003035 if( operation->is_sign )
3036 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003037 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003038 }
3039 if( operation->mac_size != mac_length )
3040 {
3041 status = PSA_ERROR_INVALID_SIGNATURE;
3042 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003043 }
mohammad16036df908f2018-04-02 08:34:15 -07003044
3045 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003046 actual_mac, sizeof( actual_mac ) );
3047
3048 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3049 status = PSA_ERROR_INVALID_SIGNATURE;
3050
3051cleanup:
3052 if( status == PSA_SUCCESS )
3053 status = psa_mac_abort( operation );
3054 else
3055 psa_mac_abort( operation );
3056
Gilles Peskine3f108122018-12-07 18:14:53 +01003057 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003058
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003059 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003060}
3061
3062
Gilles Peskine20035e32018-02-03 22:44:14 +01003063
Gilles Peskine20035e32018-02-03 22:44:14 +01003064/****************************************************************/
3065/* Asymmetric cryptography */
3066/****************************************************************/
3067
Gilles Peskine2b450e32018-06-27 15:42:46 +02003068#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003069/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003070 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003071static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3072 size_t hash_length,
3073 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003074{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003075 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003076 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003077 *md_alg = mbedtls_md_get_type( md_info );
3078
3079 /* The Mbed TLS RSA module uses an unsigned int for hash length
3080 * parameters. Validate that it fits so that we don't risk an
3081 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003082#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003083 if( hash_length > UINT_MAX )
3084 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003085#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003086
3087#if defined(MBEDTLS_PKCS1_V15)
3088 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3089 * must be correct. */
3090 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3091 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003092 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003093 if( md_info == NULL )
3094 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003095 if( mbedtls_md_get_size( md_info ) != hash_length )
3096 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003097 }
3098#endif /* MBEDTLS_PKCS1_V15 */
3099
3100#if defined(MBEDTLS_PKCS1_V21)
3101 /* PSS requires a hash internally. */
3102 if( PSA_ALG_IS_RSA_PSS( alg ) )
3103 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003104 if( md_info == NULL )
3105 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003106 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003107#endif /* MBEDTLS_PKCS1_V21 */
3108
Gilles Peskine61b91d42018-06-08 16:09:36 +02003109 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003110}
3111
Gilles Peskine2b450e32018-06-27 15:42:46 +02003112static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3113 psa_algorithm_t alg,
3114 const uint8_t *hash,
3115 size_t hash_length,
3116 uint8_t *signature,
3117 size_t signature_size,
3118 size_t *signature_length )
3119{
3120 psa_status_t status;
3121 int ret;
3122 mbedtls_md_type_t md_alg;
3123
3124 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3125 if( status != PSA_SUCCESS )
3126 return( status );
3127
Gilles Peskine630a18a2018-06-29 17:49:35 +02003128 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003129 return( PSA_ERROR_BUFFER_TOO_SMALL );
3130
3131#if defined(MBEDTLS_PKCS1_V15)
3132 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3133 {
3134 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3135 MBEDTLS_MD_NONE );
3136 ret = mbedtls_rsa_pkcs1_sign( rsa,
3137 mbedtls_ctr_drbg_random,
3138 &global_data.ctr_drbg,
3139 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003140 md_alg,
3141 (unsigned int) hash_length,
3142 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003143 signature );
3144 }
3145 else
3146#endif /* MBEDTLS_PKCS1_V15 */
3147#if defined(MBEDTLS_PKCS1_V21)
3148 if( PSA_ALG_IS_RSA_PSS( alg ) )
3149 {
3150 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3151 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3152 mbedtls_ctr_drbg_random,
3153 &global_data.ctr_drbg,
3154 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003155 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003156 (unsigned int) hash_length,
3157 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003158 signature );
3159 }
3160 else
3161#endif /* MBEDTLS_PKCS1_V21 */
3162 {
3163 return( PSA_ERROR_INVALID_ARGUMENT );
3164 }
3165
3166 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003167 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003168 return( mbedtls_to_psa_error( ret ) );
3169}
3170
3171static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3172 psa_algorithm_t alg,
3173 const uint8_t *hash,
3174 size_t hash_length,
3175 const uint8_t *signature,
3176 size_t signature_length )
3177{
3178 psa_status_t status;
3179 int ret;
3180 mbedtls_md_type_t md_alg;
3181
3182 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3183 if( status != PSA_SUCCESS )
3184 return( status );
3185
Gilles Peskine630a18a2018-06-29 17:49:35 +02003186 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003187 return( PSA_ERROR_BUFFER_TOO_SMALL );
3188
3189#if defined(MBEDTLS_PKCS1_V15)
3190 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3191 {
3192 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3193 MBEDTLS_MD_NONE );
3194 ret = mbedtls_rsa_pkcs1_verify( rsa,
3195 mbedtls_ctr_drbg_random,
3196 &global_data.ctr_drbg,
3197 MBEDTLS_RSA_PUBLIC,
3198 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003199 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003200 hash,
3201 signature );
3202 }
3203 else
3204#endif /* MBEDTLS_PKCS1_V15 */
3205#if defined(MBEDTLS_PKCS1_V21)
3206 if( PSA_ALG_IS_RSA_PSS( alg ) )
3207 {
3208 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3209 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3210 mbedtls_ctr_drbg_random,
3211 &global_data.ctr_drbg,
3212 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003213 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003214 (unsigned int) hash_length,
3215 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003216 signature );
3217 }
3218 else
3219#endif /* MBEDTLS_PKCS1_V21 */
3220 {
3221 return( PSA_ERROR_INVALID_ARGUMENT );
3222 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003223
3224 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3225 * the rest of the signature is invalid". This has little use in
3226 * practice and PSA doesn't report this distinction. */
3227 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3228 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003229 return( mbedtls_to_psa_error( ret ) );
3230}
3231#endif /* MBEDTLS_RSA_C */
3232
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003233#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003234/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3235 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3236 * (even though these functions don't modify it). */
3237static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3238 psa_algorithm_t alg,
3239 const uint8_t *hash,
3240 size_t hash_length,
3241 uint8_t *signature,
3242 size_t signature_size,
3243 size_t *signature_length )
3244{
3245 int ret;
3246 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003247 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003248 mbedtls_mpi_init( &r );
3249 mbedtls_mpi_init( &s );
3250
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003251 if( signature_size < 2 * curve_bytes )
3252 {
3253 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3254 goto cleanup;
3255 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003256
Gilles Peskinea05219c2018-11-16 16:02:56 +01003257#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003258 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3259 {
3260 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3261 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3262 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3263 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3264 hash, hash_length,
3265 md_alg ) );
3266 }
3267 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003268#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003269 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003270 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003271 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3272 hash, hash_length,
3273 mbedtls_ctr_drbg_random,
3274 &global_data.ctr_drbg ) );
3275 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003276
3277 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3278 signature,
3279 curve_bytes ) );
3280 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3281 signature + curve_bytes,
3282 curve_bytes ) );
3283
3284cleanup:
3285 mbedtls_mpi_free( &r );
3286 mbedtls_mpi_free( &s );
3287 if( ret == 0 )
3288 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003289 return( mbedtls_to_psa_error( ret ) );
3290}
3291
3292static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3293 const uint8_t *hash,
3294 size_t hash_length,
3295 const uint8_t *signature,
3296 size_t signature_length )
3297{
3298 int ret;
3299 mbedtls_mpi r, s;
3300 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3301 mbedtls_mpi_init( &r );
3302 mbedtls_mpi_init( &s );
3303
3304 if( signature_length != 2 * curve_bytes )
3305 return( PSA_ERROR_INVALID_SIGNATURE );
3306
3307 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3308 signature,
3309 curve_bytes ) );
3310 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3311 signature + curve_bytes,
3312 curve_bytes ) );
3313
3314 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3315 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003316
3317cleanup:
3318 mbedtls_mpi_free( &r );
3319 mbedtls_mpi_free( &s );
3320 return( mbedtls_to_psa_error( ret ) );
3321}
3322#endif /* MBEDTLS_ECDSA_C */
3323
Gilles Peskinec5487a82018-12-03 18:08:14 +01003324psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003325 psa_algorithm_t alg,
3326 const uint8_t *hash,
3327 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003328 uint8_t *signature,
3329 size_t signature_size,
3330 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003331{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003332 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003333 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003334#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3335 const psa_drv_se_t *drv;
3336 psa_drv_se_context_t *drv_context;
3337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003338
3339 *signature_length = signature_size;
3340
Gilles Peskineedc64242019-08-07 21:05:07 +02003341 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003342 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003343 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003344 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003345 {
3346 status = PSA_ERROR_INVALID_ARGUMENT;
3347 goto exit;
3348 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003349
Gilles Peskineedc64242019-08-07 21:05:07 +02003350#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3351 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3352 {
3353 if( drv->asymmetric == NULL ||
3354 drv->asymmetric->p_sign == NULL )
3355 {
3356 status = PSA_ERROR_NOT_SUPPORTED;
3357 goto exit;
3358 }
3359 status = drv->asymmetric->p_sign( drv_context,
3360 slot->data.se.slot_number,
3361 alg,
3362 hash, hash_length,
3363 signature, signature_size,
3364 signature_length );
3365 }
3366 else
3367#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003368#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003369 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003370 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003371 status = psa_rsa_sign( slot->data.rsa,
3372 alg,
3373 hash, hash_length,
3374 signature, signature_size,
3375 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003376 }
3377 else
3378#endif /* defined(MBEDTLS_RSA_C) */
3379#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003380 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003381 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003382#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003383 if(
3384#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3385 PSA_ALG_IS_ECDSA( alg )
3386#else
3387 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3388#endif
3389 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003390 status = psa_ecdsa_sign( slot->data.ecp,
3391 alg,
3392 hash, hash_length,
3393 signature, signature_size,
3394 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003395 else
3396#endif /* defined(MBEDTLS_ECDSA_C) */
3397 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003398 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003399 }
itayzafrir5c753392018-05-08 11:18:38 +03003400 }
3401 else
3402#endif /* defined(MBEDTLS_ECP_C) */
3403 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003404 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003405 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406
3407exit:
3408 /* Fill the unused part of the output buffer (the whole buffer on error,
3409 * the trailing part on success) with something that isn't a valid mac
3410 * (barring an attack on the mac and deliberately-crafted input),
3411 * in case the caller doesn't check the return status properly. */
3412 if( status == PSA_SUCCESS )
3413 memset( signature + *signature_length, '!',
3414 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003415 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003416 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003417 /* If signature_size is 0 then we have nothing to do. We must not call
3418 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003419 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003420}
3421
Gilles Peskinec5487a82018-12-03 18:08:14 +01003422psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003423 psa_algorithm_t alg,
3424 const uint8_t *hash,
3425 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003426 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003427 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003428{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003429 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003430 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3432 const psa_drv_se_t *drv;
3433 psa_drv_se_context_t *drv_context;
3434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003435
Gilles Peskineedc64242019-08-07 21:05:07 +02003436 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003437 if( status != PSA_SUCCESS )
3438 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003439
Gilles Peskineedc64242019-08-07 21:05:07 +02003440#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3441 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3442 {
3443 if( drv->asymmetric == NULL ||
3444 drv->asymmetric->p_verify == NULL )
3445 return( PSA_ERROR_NOT_SUPPORTED );
3446 return( drv->asymmetric->p_verify( drv_context,
3447 slot->data.se.slot_number,
3448 alg,
3449 hash, hash_length,
3450 signature, signature_length ) );
3451 }
3452 else
3453#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003454#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003455 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003456 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003457 return( psa_rsa_verify( slot->data.rsa,
3458 alg,
3459 hash, hash_length,
3460 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003461 }
3462 else
3463#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003464#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003465 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003466 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467#if defined(MBEDTLS_ECDSA_C)
3468 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003469 return( psa_ecdsa_verify( slot->data.ecp,
3470 hash, hash_length,
3471 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003472 else
3473#endif /* defined(MBEDTLS_ECDSA_C) */
3474 {
3475 return( PSA_ERROR_INVALID_ARGUMENT );
3476 }
itayzafrir5c753392018-05-08 11:18:38 +03003477 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003478 else
3479#endif /* defined(MBEDTLS_ECP_C) */
3480 {
3481 return( PSA_ERROR_NOT_SUPPORTED );
3482 }
3483}
3484
Gilles Peskine072ac562018-06-30 00:21:29 +02003485#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3486static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3487 mbedtls_rsa_context *rsa )
3488{
3489 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3490 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3491 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3492 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3493}
3494#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3495
Gilles Peskinec5487a82018-12-03 18:08:14 +01003496psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003497 psa_algorithm_t alg,
3498 const uint8_t *input,
3499 size_t input_length,
3500 const uint8_t *salt,
3501 size_t salt_length,
3502 uint8_t *output,
3503 size_t output_size,
3504 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003505{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003506 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003507 psa_status_t status;
3508
Darryl Green5cc689a2018-07-24 15:34:10 +01003509 (void) input;
3510 (void) input_length;
3511 (void) salt;
3512 (void) output;
3513 (void) output_size;
3514
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003515 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003516
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003517 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3518 return( PSA_ERROR_INVALID_ARGUMENT );
3519
Gilles Peskine28f8f302019-07-24 13:30:31 +02003520 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003521 if( status != PSA_SUCCESS )
3522 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003523 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3524 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003525 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526
3527#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003528 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529 {
3530 mbedtls_rsa_context *rsa = slot->data.rsa;
3531 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003532 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003533 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003535 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003537 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3538 mbedtls_ctr_drbg_random,
3539 &global_data.ctr_drbg,
3540 MBEDTLS_RSA_PUBLIC,
3541 input_length,
3542 input,
3543 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003544 }
3545 else
3546#endif /* MBEDTLS_PKCS1_V15 */
3547#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003548 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003549 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003550 psa_rsa_oaep_set_padding_mode( alg, rsa );
3551 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3552 mbedtls_ctr_drbg_random,
3553 &global_data.ctr_drbg,
3554 MBEDTLS_RSA_PUBLIC,
3555 salt, salt_length,
3556 input_length,
3557 input,
3558 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559 }
3560 else
3561#endif /* MBEDTLS_PKCS1_V21 */
3562 {
3563 return( PSA_ERROR_INVALID_ARGUMENT );
3564 }
3565 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003566 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567 return( mbedtls_to_psa_error( ret ) );
3568 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003570#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571 {
3572 return( PSA_ERROR_NOT_SUPPORTED );
3573 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003574}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003575
Gilles Peskinec5487a82018-12-03 18:08:14 +01003576psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003577 psa_algorithm_t alg,
3578 const uint8_t *input,
3579 size_t input_length,
3580 const uint8_t *salt,
3581 size_t salt_length,
3582 uint8_t *output,
3583 size_t output_size,
3584 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003585{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003586 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003587 psa_status_t status;
3588
Darryl Green5cc689a2018-07-24 15:34:10 +01003589 (void) input;
3590 (void) input_length;
3591 (void) salt;
3592 (void) output;
3593 (void) output_size;
3594
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003595 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003597 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3598 return( PSA_ERROR_INVALID_ARGUMENT );
3599
Gilles Peskine28f8f302019-07-24 13:30:31 +02003600 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003601 if( status != PSA_SUCCESS )
3602 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003603 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604 return( PSA_ERROR_INVALID_ARGUMENT );
3605
3606#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003607 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608 {
3609 mbedtls_rsa_context *rsa = slot->data.rsa;
3610 int ret;
3611
Gilles Peskine630a18a2018-06-29 17:49:35 +02003612 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613 return( PSA_ERROR_INVALID_ARGUMENT );
3614
3615#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003616 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003618 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3619 mbedtls_ctr_drbg_random,
3620 &global_data.ctr_drbg,
3621 MBEDTLS_RSA_PRIVATE,
3622 output_length,
3623 input,
3624 output,
3625 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003626 }
3627 else
3628#endif /* MBEDTLS_PKCS1_V15 */
3629#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003630 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003632 psa_rsa_oaep_set_padding_mode( alg, rsa );
3633 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3634 mbedtls_ctr_drbg_random,
3635 &global_data.ctr_drbg,
3636 MBEDTLS_RSA_PRIVATE,
3637 salt, salt_length,
3638 output_length,
3639 input,
3640 output,
3641 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 }
3643 else
3644#endif /* MBEDTLS_PKCS1_V21 */
3645 {
3646 return( PSA_ERROR_INVALID_ARGUMENT );
3647 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003648
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649 return( mbedtls_to_psa_error( ret ) );
3650 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003652#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003653 {
3654 return( PSA_ERROR_NOT_SUPPORTED );
3655 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003656}
Gilles Peskine20035e32018-02-03 22:44:14 +01003657
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003658
3659
mohammad1603503973b2018-03-12 15:59:30 +02003660/****************************************************************/
3661/* Symmetric cryptography */
3662/****************************************************************/
3663
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003664/* Initialize the cipher operation structure. Once this function has been
3665 * called, psa_cipher_abort can run and will do the right thing. */
3666static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3667 psa_algorithm_t alg )
3668{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003669 if( ! PSA_ALG_IS_CIPHER( alg ) )
3670 {
3671 memset( operation, 0, sizeof( *operation ) );
3672 return( PSA_ERROR_INVALID_ARGUMENT );
3673 }
3674
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003675 operation->alg = alg;
3676 operation->key_set = 0;
3677 operation->iv_set = 0;
3678 operation->iv_required = 1;
3679 operation->iv_size = 0;
3680 operation->block_size = 0;
3681 mbedtls_cipher_init( &operation->ctx.cipher );
3682 return( PSA_SUCCESS );
3683}
3684
Gilles Peskinee553c652018-06-04 16:22:46 +02003685static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003686 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003687 psa_algorithm_t alg,
3688 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003689{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003690 int ret = 0;
3691 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003692 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003693 size_t key_bits;
3694 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003695 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3696 PSA_KEY_USAGE_ENCRYPT :
3697 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003698
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003699 /* A context must be freshly initialized before it can be set up. */
3700 if( operation->alg != 0 )
3701 {
3702 return( PSA_ERROR_BAD_STATE );
3703 }
3704
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003705 status = psa_cipher_init( operation, alg );
3706 if( status != PSA_SUCCESS )
3707 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003708
Gilles Peskine28f8f302019-07-24 13:30:31 +02003709 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003710 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003711 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003712 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003713
Gilles Peskine8e338702019-07-30 20:06:31 +02003714 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003715 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003716 {
3717 status = PSA_ERROR_NOT_SUPPORTED;
3718 goto exit;
3719 }
mohammad1603503973b2018-03-12 15:59:30 +02003720
mohammad1603503973b2018-03-12 15:59:30 +02003721 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003722 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003723 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003724
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003725#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003726 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003727 {
3728 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003729 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003730 memcpy( keys, slot->data.raw.data, 16 );
3731 memcpy( keys + 16, slot->data.raw.data, 8 );
3732 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3733 keys,
3734 192, cipher_operation );
3735 }
3736 else
3737#endif
3738 {
3739 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3740 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003741 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003742 }
Moran Peker41deec42018-04-04 15:43:05 +03003743 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003744 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003745
mohammad16038481e742018-03-18 13:57:31 +02003746#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003747 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003748 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003749 case PSA_ALG_CBC_NO_PADDING:
3750 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3751 MBEDTLS_PADDING_NONE );
3752 break;
3753 case PSA_ALG_CBC_PKCS7:
3754 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3755 MBEDTLS_PADDING_PKCS7 );
3756 break;
3757 default:
3758 /* The algorithm doesn't involve padding. */
3759 ret = 0;
3760 break;
3761 }
3762 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003763 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003764#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3765
mohammad1603503973b2018-03-12 15:59:30 +02003766 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003767 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003768 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003769 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003770 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003771 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003772 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003773#if defined(MBEDTLS_CHACHA20_C)
3774 else
3775 if( alg == PSA_ALG_CHACHA20 )
3776 operation->iv_size = 12;
3777#endif
mohammad1603503973b2018-03-12 15:59:30 +02003778
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003779exit:
3780 if( status == 0 )
3781 status = mbedtls_to_psa_error( ret );
3782 if( status != 0 )
3783 psa_cipher_abort( operation );
3784 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003785}
3786
Gilles Peskinefe119512018-07-08 21:39:34 +02003787psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003788 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003789 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003790{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003791 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003792}
3793
Gilles Peskinefe119512018-07-08 21:39:34 +02003794psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003795 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003796 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003797{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003798 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003799}
3800
Gilles Peskinefe119512018-07-08 21:39:34 +02003801psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003802 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003803 size_t iv_size,
3804 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003805{
itayzafrir534bd7c2018-08-02 13:56:32 +03003806 psa_status_t status;
3807 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003808 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003809 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003810 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003811 }
Moran Peker41deec42018-04-04 15:43:05 +03003812 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003813 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003814 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003815 goto exit;
3816 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003817 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3818 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003819 if( ret != 0 )
3820 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003821 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003822 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003823 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003824
mohammad16038481e742018-03-18 13:57:31 +02003825 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003826 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003827
Moran Peker395db872018-05-31 14:07:14 +03003828exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003829 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003830 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003831 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003832}
3833
Gilles Peskinefe119512018-07-08 21:39:34 +02003834psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003835 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003836 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003837{
itayzafrir534bd7c2018-08-02 13:56:32 +03003838 psa_status_t status;
3839 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003840 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003841 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003842 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003843 }
Moran Pekera28258c2018-05-29 16:25:04 +03003844 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003845 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003846 status = PSA_ERROR_INVALID_ARGUMENT;
3847 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003848 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003849 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3850 status = mbedtls_to_psa_error( ret );
3851exit:
3852 if( status == PSA_SUCCESS )
3853 operation->iv_set = 1;
3854 else
mohammad1603503973b2018-03-12 15:59:30 +02003855 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003856 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003857}
3858
Gilles Peskinee553c652018-06-04 16:22:46 +02003859psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3860 const uint8_t *input,
3861 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003862 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003863 size_t output_size,
3864 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003865{
itayzafrir534bd7c2018-08-02 13:56:32 +03003866 psa_status_t status;
3867 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003868 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003869
3870 if( operation->alg == 0 )
3871 {
3872 return( PSA_ERROR_BAD_STATE );
3873 }
3874
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003875 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003876 {
3877 /* Take the unprocessed partial block left over from previous
3878 * update calls, if any, plus the input to this call. Remove
3879 * the last partial block, if any. You get the data that will be
3880 * output in this call. */
3881 expected_output_size =
3882 ( operation->ctx.cipher.unprocessed_len + input_length )
3883 / operation->block_size * operation->block_size;
3884 }
3885 else
3886 {
3887 expected_output_size = input_length;
3888 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003889
Gilles Peskine89d789c2018-06-04 17:17:16 +02003890 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003891 {
3892 status = PSA_ERROR_BUFFER_TOO_SMALL;
3893 goto exit;
3894 }
mohammad160382759612018-03-12 18:16:40 +02003895
mohammad1603503973b2018-03-12 15:59:30 +02003896 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003897 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003898 status = mbedtls_to_psa_error( ret );
3899exit:
3900 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003901 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003902 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003903}
3904
Gilles Peskinee553c652018-06-04 16:22:46 +02003905psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3906 uint8_t *output,
3907 size_t output_size,
3908 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003909{
David Saadab4ecc272019-02-14 13:48:10 +02003910 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003911 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003912 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003913
mohammad1603503973b2018-03-12 15:59:30 +02003914 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003915 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003916 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003917 }
3918 if( operation->iv_required && ! operation->iv_set )
3919 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003920 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003921 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003922
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003923 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003924 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3925 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003926 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003927 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003928 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003929 }
3930
Janos Follath315b51c2018-07-09 16:04:51 +01003931 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3932 temp_output_buffer,
3933 output_length );
3934 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003935 {
Janos Follath315b51c2018-07-09 16:04:51 +01003936 status = mbedtls_to_psa_error( cipher_ret );
3937 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003938 }
Janos Follath315b51c2018-07-09 16:04:51 +01003939
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003940 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003941 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003942 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003943 memcpy( output, temp_output_buffer, *output_length );
3944 else
3945 {
Janos Follath315b51c2018-07-09 16:04:51 +01003946 status = PSA_ERROR_BUFFER_TOO_SMALL;
3947 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003948 }
mohammad1603503973b2018-03-12 15:59:30 +02003949
Gilles Peskine3f108122018-12-07 18:14:53 +01003950 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003951 status = psa_cipher_abort( operation );
3952
3953 return( status );
3954
3955error:
3956
3957 *output_length = 0;
3958
Gilles Peskine3f108122018-12-07 18:14:53 +01003959 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003960 (void) psa_cipher_abort( operation );
3961
3962 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003963}
3964
Gilles Peskinee553c652018-06-04 16:22:46 +02003965psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3966{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003967 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003968 {
3969 /* The object has (apparently) been initialized but it is not
3970 * in use. It's ok to call abort on such an object, and there's
3971 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003972 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003973 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003974
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003975 /* Sanity check (shouldn't happen: operation->alg should
3976 * always have been initialized to a valid value). */
3977 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3978 return( PSA_ERROR_BAD_STATE );
3979
mohammad1603503973b2018-03-12 15:59:30 +02003980 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003981
Moran Peker41deec42018-04-04 15:43:05 +03003982 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003983 operation->key_set = 0;
3984 operation->iv_set = 0;
3985 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003986 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003987 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003988
Moran Peker395db872018-05-31 14:07:14 +03003989 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003990}
3991
Gilles Peskinea0655c32018-04-30 17:06:50 +02003992
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003993
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003994
mohammad16035955c982018-04-26 00:53:03 +03003995/****************************************************************/
3996/* AEAD */
3997/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003998
Gilles Peskineedf9a652018-08-17 18:11:56 +02003999typedef struct
4000{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004001 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004002 const mbedtls_cipher_info_t *cipher_info;
4003 union
4004 {
4005#if defined(MBEDTLS_CCM_C)
4006 mbedtls_ccm_context ccm;
4007#endif /* MBEDTLS_CCM_C */
4008#if defined(MBEDTLS_GCM_C)
4009 mbedtls_gcm_context gcm;
4010#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004011#if defined(MBEDTLS_CHACHAPOLY_C)
4012 mbedtls_chachapoly_context chachapoly;
4013#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004014 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004015 psa_algorithm_t core_alg;
4016 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004017 uint8_t tag_length;
4018} aead_operation_t;
4019
Gilles Peskine30a9e412019-01-14 18:36:12 +01004020static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004021{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004022 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004023 {
4024#if defined(MBEDTLS_CCM_C)
4025 case PSA_ALG_CCM:
4026 mbedtls_ccm_free( &operation->ctx.ccm );
4027 break;
4028#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004029#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004030 case PSA_ALG_GCM:
4031 mbedtls_gcm_free( &operation->ctx.gcm );
4032 break;
4033#endif /* MBEDTLS_GCM_C */
4034 }
4035}
4036
4037static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004038 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004039 psa_key_usage_t usage,
4040 psa_algorithm_t alg )
4041{
4042 psa_status_t status;
4043 size_t key_bits;
4044 mbedtls_cipher_id_t cipher_id;
4045
Gilles Peskine28f8f302019-07-24 13:30:31 +02004046 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004047 if( status != PSA_SUCCESS )
4048 return( status );
4049
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004050 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004051
4052 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004053 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004054 &cipher_id );
4055 if( operation->cipher_info == NULL )
4056 return( PSA_ERROR_NOT_SUPPORTED );
4057
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004058 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004059 {
4060#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004061 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4062 operation->core_alg = PSA_ALG_CCM;
4063 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004064 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4065 * The call to mbedtls_ccm_encrypt_and_tag or
4066 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004067 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004068 return( PSA_ERROR_INVALID_ARGUMENT );
4069 mbedtls_ccm_init( &operation->ctx.ccm );
4070 status = mbedtls_to_psa_error(
4071 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4072 operation->slot->data.raw.data,
4073 (unsigned int) key_bits ) );
4074 if( status != 0 )
4075 goto cleanup;
4076 break;
4077#endif /* MBEDTLS_CCM_C */
4078
4079#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004080 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4081 operation->core_alg = PSA_ALG_GCM;
4082 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004083 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4084 * The call to mbedtls_gcm_crypt_and_tag or
4085 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004086 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 return( PSA_ERROR_INVALID_ARGUMENT );
4088 mbedtls_gcm_init( &operation->ctx.gcm );
4089 status = mbedtls_to_psa_error(
4090 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4091 operation->slot->data.raw.data,
4092 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004093 if( status != 0 )
4094 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004095 break;
4096#endif /* MBEDTLS_GCM_C */
4097
Gilles Peskine26869f22019-05-06 15:25:00 +02004098#if defined(MBEDTLS_CHACHAPOLY_C)
4099 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4100 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4101 operation->full_tag_length = 16;
4102 /* We only support the default tag length. */
4103 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4104 return( PSA_ERROR_NOT_SUPPORTED );
4105 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4106 status = mbedtls_to_psa_error(
4107 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4108 operation->slot->data.raw.data ) );
4109 if( status != 0 )
4110 goto cleanup;
4111 break;
4112#endif /* MBEDTLS_CHACHAPOLY_C */
4113
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114 default:
4115 return( PSA_ERROR_NOT_SUPPORTED );
4116 }
4117
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004118 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4119 {
4120 status = PSA_ERROR_INVALID_ARGUMENT;
4121 goto cleanup;
4122 }
4123 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004124
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125 return( PSA_SUCCESS );
4126
4127cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004128 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004129 return( status );
4130}
4131
Gilles Peskinec5487a82018-12-03 18:08:14 +01004132psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004133 psa_algorithm_t alg,
4134 const uint8_t *nonce,
4135 size_t nonce_length,
4136 const uint8_t *additional_data,
4137 size_t additional_data_length,
4138 const uint8_t *plaintext,
4139 size_t plaintext_length,
4140 uint8_t *ciphertext,
4141 size_t ciphertext_size,
4142 size_t *ciphertext_length )
4143{
mohammad16035955c982018-04-26 00:53:03 +03004144 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004145 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004146 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004147
mohammad1603f08a5502018-06-03 15:05:47 +03004148 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004149
Gilles Peskinec5487a82018-12-03 18:08:14 +01004150 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004151 if( status != PSA_SUCCESS )
4152 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004153
Gilles Peskineedf9a652018-08-17 18:11:56 +02004154 /* For all currently supported modes, the tag is at the end of the
4155 * ciphertext. */
4156 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4157 {
4158 status = PSA_ERROR_BUFFER_TOO_SMALL;
4159 goto exit;
4160 }
4161 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004162
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004163#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004164 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004165 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004166 status = mbedtls_to_psa_error(
4167 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4168 MBEDTLS_GCM_ENCRYPT,
4169 plaintext_length,
4170 nonce, nonce_length,
4171 additional_data, additional_data_length,
4172 plaintext, ciphertext,
4173 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004174 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004175 else
4176#endif /* MBEDTLS_GCM_C */
4177#if defined(MBEDTLS_CCM_C)
4178 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004179 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004180 status = mbedtls_to_psa_error(
4181 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4182 plaintext_length,
4183 nonce, nonce_length,
4184 additional_data,
4185 additional_data_length,
4186 plaintext, ciphertext,
4187 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004188 }
mohammad16035c8845f2018-05-09 05:40:09 -07004189 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004190#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004191#if defined(MBEDTLS_CHACHAPOLY_C)
4192 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4193 {
4194 if( nonce_length != 12 || operation.tag_length != 16 )
4195 {
4196 status = PSA_ERROR_NOT_SUPPORTED;
4197 goto exit;
4198 }
4199 status = mbedtls_to_psa_error(
4200 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4201 plaintext_length,
4202 nonce,
4203 additional_data,
4204 additional_data_length,
4205 plaintext,
4206 ciphertext,
4207 tag ) );
4208 }
4209 else
4210#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004211 {
mohammad1603554faad2018-06-03 15:07:38 +03004212 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004213 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004214
Gilles Peskineedf9a652018-08-17 18:11:56 +02004215 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4216 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004217
Gilles Peskineedf9a652018-08-17 18:11:56 +02004218exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004219 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004220 if( status == PSA_SUCCESS )
4221 *ciphertext_length = plaintext_length + operation.tag_length;
4222 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004223}
4224
Gilles Peskineee652a32018-06-01 19:23:52 +02004225/* Locate the tag in a ciphertext buffer containing the encrypted data
4226 * followed by the tag. Return the length of the part preceding the tag in
4227 * *plaintext_length. This is the size of the plaintext in modes where
4228 * the encrypted data has the same size as the plaintext, such as
4229 * CCM and GCM. */
4230static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4231 const uint8_t *ciphertext,
4232 size_t ciphertext_length,
4233 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004234 const uint8_t **p_tag )
4235{
4236 size_t payload_length;
4237 if( tag_length > ciphertext_length )
4238 return( PSA_ERROR_INVALID_ARGUMENT );
4239 payload_length = ciphertext_length - tag_length;
4240 if( payload_length > plaintext_size )
4241 return( PSA_ERROR_BUFFER_TOO_SMALL );
4242 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004243 return( PSA_SUCCESS );
4244}
4245
Gilles Peskinec5487a82018-12-03 18:08:14 +01004246psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004247 psa_algorithm_t alg,
4248 const uint8_t *nonce,
4249 size_t nonce_length,
4250 const uint8_t *additional_data,
4251 size_t additional_data_length,
4252 const uint8_t *ciphertext,
4253 size_t ciphertext_length,
4254 uint8_t *plaintext,
4255 size_t plaintext_size,
4256 size_t *plaintext_length )
4257{
mohammad16035955c982018-04-26 00:53:03 +03004258 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004259 aead_operation_t operation;
4260 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004261
Gilles Peskineee652a32018-06-01 19:23:52 +02004262 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004263
Gilles Peskinec5487a82018-12-03 18:08:14 +01004264 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004265 if( status != PSA_SUCCESS )
4266 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004267
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004268 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4269 ciphertext, ciphertext_length,
4270 plaintext_size, &tag );
4271 if( status != PSA_SUCCESS )
4272 goto exit;
4273
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004274#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004275 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004276 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004277 status = mbedtls_to_psa_error(
4278 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4279 ciphertext_length - operation.tag_length,
4280 nonce, nonce_length,
4281 additional_data,
4282 additional_data_length,
4283 tag, operation.tag_length,
4284 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004285 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004286 else
4287#endif /* MBEDTLS_GCM_C */
4288#if defined(MBEDTLS_CCM_C)
4289 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004290 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004291 status = mbedtls_to_psa_error(
4292 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4293 ciphertext_length - operation.tag_length,
4294 nonce, nonce_length,
4295 additional_data,
4296 additional_data_length,
4297 ciphertext, plaintext,
4298 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004299 }
mohammad160339574652018-06-01 04:39:53 -07004300 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004301#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004302#if defined(MBEDTLS_CHACHAPOLY_C)
4303 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4304 {
4305 if( nonce_length != 12 || operation.tag_length != 16 )
4306 {
4307 status = PSA_ERROR_NOT_SUPPORTED;
4308 goto exit;
4309 }
4310 status = mbedtls_to_psa_error(
4311 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4312 ciphertext_length - operation.tag_length,
4313 nonce,
4314 additional_data,
4315 additional_data_length,
4316 tag,
4317 ciphertext,
4318 plaintext ) );
4319 }
4320 else
4321#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004322 {
mohammad1603554faad2018-06-03 15:07:38 +03004323 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004324 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004325
Gilles Peskineedf9a652018-08-17 18:11:56 +02004326 if( status != PSA_SUCCESS && plaintext_size != 0 )
4327 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004328
Gilles Peskineedf9a652018-08-17 18:11:56 +02004329exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004330 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004331 if( status == PSA_SUCCESS )
4332 *plaintext_length = ciphertext_length - operation.tag_length;
4333 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004334}
4335
Gilles Peskinea0655c32018-04-30 17:06:50 +02004336
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004337
Gilles Peskine20035e32018-02-03 22:44:14 +01004338/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004339/* Generators */
4340/****************************************************************/
4341
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004342#define HKDF_STATE_INIT 0 /* no input yet */
4343#define HKDF_STATE_STARTED 1 /* got salt */
4344#define HKDF_STATE_KEYED 2 /* got key */
4345#define HKDF_STATE_OUTPUT 3 /* output started */
4346
Gilles Peskinecbe66502019-05-16 16:59:18 +02004347static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004348 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004349{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4351 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004352 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004354}
4355
4356
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004357psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004358{
4359 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004360 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004361 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004362 {
4363 /* The object has (apparently) been initialized but it is not
4364 * in use. It's ok to call abort on such an object, and there's
4365 * nothing to do. */
4366 }
4367 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004368#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004369 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004370 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 mbedtls_free( operation->ctx.hkdf.info );
4372 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004373 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004374 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004376 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004377 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004378#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004380 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4382 operation->ctx.tls12_prf.key_len );
4383 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004384 }
Hanno Becker580fba12018-11-13 20:50:45 +00004385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004387 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4389 operation->ctx.tls12_prf.Ai_with_seed_len );
4390 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004391 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004392#else
4393 if( operation->ctx.tls12_prf.seed != NULL )
4394 {
4395 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4396 operation->ctx.tls12_prf.seed_length );
4397 mbedtls_free( operation->ctx.tls12_prf.seed );
4398 }
4399
4400 if( operation->ctx.tls12_prf.label != NULL )
4401 {
4402 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4403 operation->ctx.tls12_prf.label_length );
4404 mbedtls_free( operation->ctx.tls12_prf.label );
4405 }
4406
4407 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4408
4409 /* We leave the fields Ai and output_block to be erased safely by the
4410 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004411#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004412 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 else
4414#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004415 {
4416 status = PSA_ERROR_BAD_STATE;
4417 }
Janos Follath083036a2019-06-11 10:22:26 +01004418 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419 return( status );
4420}
4421
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004423 size_t *capacity)
4424{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004426 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004428 return PSA_ERROR_BAD_STATE;
4429 }
4430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004432 return( PSA_SUCCESS );
4433}
4434
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004436 size_t capacity )
4437{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004441 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004443 return( PSA_SUCCESS );
4444}
4445
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004448 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004449static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004450 psa_algorithm_t hash_alg,
4451 uint8_t *output,
4452 size_t output_length )
4453{
4454 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4455 psa_status_t status;
4456
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004457 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4458 return( PSA_ERROR_BAD_STATE );
4459 hkdf->state = HKDF_STATE_OUTPUT;
4460
Gilles Peskinebef7f142018-07-12 17:22:21 +02004461 while( output_length != 0 )
4462 {
4463 /* Copy what remains of the current block */
4464 uint8_t n = hash_length - hkdf->offset_in_block;
4465 if( n > output_length )
4466 n = (uint8_t) output_length;
4467 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4468 output += n;
4469 output_length -= n;
4470 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004471 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004472 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004473 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004474 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004476 * object was corrupted or if this function is called directly
4477 * inside the library. */
4478 if( hkdf->block_number == 0xff )
4479 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004480
4481 /* We need a new block */
4482 ++hkdf->block_number;
4483 hkdf->offset_in_block = 0;
4484 status = psa_hmac_setup_internal( &hkdf->hmac,
4485 hkdf->prk, hash_length,
4486 hash_alg );
4487 if( status != PSA_SUCCESS )
4488 return( status );
4489 if( hkdf->block_number != 1 )
4490 {
4491 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4492 hkdf->output_block,
4493 hash_length );
4494 if( status != PSA_SUCCESS )
4495 return( status );
4496 }
4497 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4498 hkdf->info,
4499 hkdf->info_length );
4500 if( status != PSA_SUCCESS )
4501 return( status );
4502 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4503 &hkdf->block_number, 1 );
4504 if( status != PSA_SUCCESS )
4505 return( status );
4506 status = psa_hmac_finish_internal( &hkdf->hmac,
4507 hkdf->output_block,
4508 sizeof( hkdf->output_block ) );
4509 if( status != PSA_SUCCESS )
4510 return( status );
4511 }
4512
4513 return( PSA_SUCCESS );
4514}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004515
Janos Follath999f6482019-06-11 12:04:10 +01004516#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004517static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4518 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004519 psa_algorithm_t alg )
4520{
4521 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4522 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4523 psa_hmac_internal_data hmac;
4524 psa_status_t status, cleanup_status;
4525
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004526 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004527 size_t Ai_len;
4528
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004529 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004530 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004532 * object was corrupted or if this function is called directly
4533 * inside the library. */
4534 if( tls12_prf->block_number == 0xff )
4535 return( PSA_ERROR_BAD_STATE );
4536
4537 /* We need a new block */
4538 ++tls12_prf->block_number;
4539 tls12_prf->offset_in_block = 0;
4540
4541 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4542 *
4543 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4544 *
4545 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4546 * HMAC_hash(secret, A(2) + seed) +
4547 * HMAC_hash(secret, A(3) + seed) + ...
4548 *
4549 * A(0) = seed
4550 * A(i) = HMAC_hash( secret, A(i-1) )
4551 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004552 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004553 * `HMAC_hash(secret, A(i) + seed)` from which the output
4554 * is currently extracted as `output_block`, while
4555 * `A(i) + seed` is stored in `Ai_with_seed`.
4556 *
4557 * Generating a new block means recalculating `Ai_with_seed`
4558 * from the A(i)-part of it, and afterwards recalculating
4559 * `output_block`.
4560 *
4561 * A(0) is computed at setup time.
4562 *
4563 */
4564
4565 psa_hmac_init_internal( &hmac );
4566
4567 /* We must distinguish the calculation of A(1) from those
4568 * of A(2) and higher, because A(0)=seed has a different
4569 * length than the other A(i). */
4570 if( tls12_prf->block_number == 1 )
4571 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004572 Ai = tls12_prf->Ai_with_seed + hash_length;
4573 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004574 }
4575 else
4576 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004577 Ai = tls12_prf->Ai_with_seed;
4578 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004579 }
4580
Hanno Becker3b339e22018-11-13 20:56:14 +00004581 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4582 status = psa_hmac_setup_internal( &hmac,
4583 tls12_prf->key,
4584 tls12_prf->key_len,
4585 hash_alg );
4586 if( status != PSA_SUCCESS )
4587 goto cleanup;
4588
4589 status = psa_hash_update( &hmac.hash_ctx,
4590 Ai, Ai_len );
4591 if( status != PSA_SUCCESS )
4592 goto cleanup;
4593
4594 status = psa_hmac_finish_internal( &hmac,
4595 tls12_prf->Ai_with_seed,
4596 hash_length );
4597 if( status != PSA_SUCCESS )
4598 goto cleanup;
4599
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004600 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4601 status = psa_hmac_setup_internal( &hmac,
4602 tls12_prf->key,
4603 tls12_prf->key_len,
4604 hash_alg );
4605 if( status != PSA_SUCCESS )
4606 goto cleanup;
4607
4608 status = psa_hash_update( &hmac.hash_ctx,
4609 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004610 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004611 if( status != PSA_SUCCESS )
4612 goto cleanup;
4613
4614 status = psa_hmac_finish_internal( &hmac,
4615 tls12_prf->output_block,
4616 hash_length );
4617 if( status != PSA_SUCCESS )
4618 goto cleanup;
4619
4620cleanup:
4621
4622 cleanup_status = psa_hmac_abort_internal( &hmac );
4623 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4624 status = cleanup_status;
4625
4626 return( status );
4627}
Janos Follath7742fee2019-06-17 12:58:10 +01004628#else
4629static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4630 psa_tls12_prf_key_derivation_t *tls12_prf,
4631 psa_algorithm_t alg )
4632{
4633 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4634 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004635 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4636 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004637
Janos Follath7742fee2019-06-17 12:58:10 +01004638 /* We can't be wanting more output after block 0xff, otherwise
4639 * the capacity check in psa_key_derivation_output_bytes() would have
4640 * prevented this call. It could happen only if the operation
4641 * object was corrupted or if this function is called directly
4642 * inside the library. */
4643 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004644 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004645
4646 /* We need a new block */
4647 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004648 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004649
4650 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4651 *
4652 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4653 *
4654 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4655 * HMAC_hash(secret, A(2) + seed) +
4656 * HMAC_hash(secret, A(3) + seed) + ...
4657 *
4658 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004659 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004660 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004661 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004662 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004663 * is currently extracted as `output_block` and where i is
4664 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004665 */
4666
Janos Follathea29bfb2019-06-19 12:21:20 +01004667 /* Save the hash context before using it, to preserve the hash state with
4668 * only the inner padding in it. We need this, because inner padding depends
4669 * on the key (secret in the RFC's terminology). */
4670 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4671 if( status != PSA_SUCCESS )
4672 goto cleanup;
4673
4674 /* Calculate A(i) where i = tls12_prf->block_number. */
4675 if( tls12_prf->block_number == 1 )
4676 {
4677 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4678 * the variable seed and in this instance means it in the context of the
4679 * P_hash function, where seed = label + seed.) */
4680 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4681 tls12_prf->label, tls12_prf->label_length );
4682 if( status != PSA_SUCCESS )
4683 goto cleanup;
4684 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4685 tls12_prf->seed, tls12_prf->seed_length );
4686 if( status != PSA_SUCCESS )
4687 goto cleanup;
4688 }
4689 else
4690 {
4691 /* A(i) = HMAC_hash(secret, A(i-1)) */
4692 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4693 tls12_prf->Ai, hash_length );
4694 if( status != PSA_SUCCESS )
4695 goto cleanup;
4696 }
4697
4698 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4699 tls12_prf->Ai, hash_length );
4700 if( status != PSA_SUCCESS )
4701 goto cleanup;
4702 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4703 if( status != PSA_SUCCESS )
4704 goto cleanup;
4705
4706 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4707 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4708 tls12_prf->Ai, hash_length );
4709 if( status != PSA_SUCCESS )
4710 goto cleanup;
4711 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4712 tls12_prf->label, tls12_prf->label_length );
4713 if( status != PSA_SUCCESS )
4714 goto cleanup;
4715 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4716 tls12_prf->seed, tls12_prf->seed_length );
4717 if( status != PSA_SUCCESS )
4718 goto cleanup;
4719 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4720 tls12_prf->output_block, hash_length );
4721 if( status != PSA_SUCCESS )
4722 goto cleanup;
4723 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4724 if( status != PSA_SUCCESS )
4725 goto cleanup;
4726
Janos Follath7742fee2019-06-17 12:58:10 +01004727
4728cleanup:
4729
Janos Follathea29bfb2019-06-19 12:21:20 +01004730 cleanup_status = psa_hash_abort( &backup );
4731 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4732 status = cleanup_status;
4733
4734 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004735}
Janos Follath999f6482019-06-11 12:04:10 +01004736#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004737
Janos Follath999f6482019-06-11 12:04:10 +01004738#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004740 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004741static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004742 psa_tls12_prf_key_derivation_t *tls12_prf,
4743 psa_algorithm_t alg,
4744 uint8_t *output,
4745 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004746{
4747 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4748 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4749 psa_status_t status;
4750
4751 while( output_length != 0 )
4752 {
4753 /* Copy what remains of the current block */
4754 uint8_t n = hash_length - tls12_prf->offset_in_block;
4755
4756 /* Check if we have fully processed the current block. */
4757 if( n == 0 )
4758 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004759 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004760 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004761 if( status != PSA_SUCCESS )
4762 return( status );
4763
4764 continue;
4765 }
4766
4767 if( n > output_length )
4768 n = (uint8_t) output_length;
4769 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4770 n );
4771 output += n;
4772 output_length -= n;
4773 tls12_prf->offset_in_block += n;
4774 }
4775
4776 return( PSA_SUCCESS );
4777}
Janos Follath844eb0e2019-06-19 12:10:49 +01004778#else
4779static psa_status_t psa_key_derivation_tls12_prf_read(
4780 psa_tls12_prf_key_derivation_t *tls12_prf,
4781 psa_algorithm_t alg,
4782 uint8_t *output,
4783 size_t output_length )
4784{
4785 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4786 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4787 psa_status_t status;
4788 uint8_t offset, length;
4789
4790 while( output_length != 0 )
4791 {
4792 /* Check if we have fully processed the current block. */
4793 if( tls12_prf->left_in_block == 0 )
4794 {
4795 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4796 alg );
4797 if( status != PSA_SUCCESS )
4798 return( status );
4799
4800 continue;
4801 }
4802
4803 if( tls12_prf->left_in_block > output_length )
4804 length = (uint8_t) output_length;
4805 else
4806 length = tls12_prf->left_in_block;
4807
4808 offset = hash_length - tls12_prf->left_in_block;
4809 memcpy( output, tls12_prf->output_block + offset, length );
4810 output += length;
4811 output_length -= length;
4812 tls12_prf->left_in_block -= length;
4813 }
4814
4815 return( PSA_SUCCESS );
4816}
Janos Follath999f6482019-06-11 12:04:10 +01004817#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004818#endif /* MBEDTLS_MD_C */
4819
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004820psa_status_t psa_key_derivation_output_bytes(
4821 psa_key_derivation_operation_t *operation,
4822 uint8_t *output,
4823 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004824{
4825 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004827
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004829 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004831 return PSA_ERROR_BAD_STATE;
4832 }
4833
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004835 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004837 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004839 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004840 goto exit;
4841 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004843 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004845 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004846 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4847 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004849 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004850 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004851 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004853
Gilles Peskinebef7f142018-07-12 17:22:21 +02004854#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004855 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004856 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004857 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004859 output, output_length );
4860 }
Janos Follathadbec812019-06-14 11:05:39 +01004861 else
Janos Follathadbec812019-06-14 11:05:39 +01004862 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004863 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004864 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004866 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004867 output_length );
4868 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004869 else
4870#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004871 {
4872 return( PSA_ERROR_BAD_STATE );
4873 }
4874
4875exit:
4876 if( status != PSA_SUCCESS )
4877 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004878 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 * This allows us to differentiate between exhausted operations and
4880 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4881 * operations. */
4882 psa_algorithm_t alg = operation->alg;
4883 psa_key_derivation_abort( operation );
4884 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004885 memset( output, '!', output_length );
4886 }
4887 return( status );
4888}
4889
Gilles Peskine08542d82018-07-19 17:05:42 +02004890#if defined(MBEDTLS_DES_C)
4891static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4892{
4893 if( data_size >= 8 )
4894 mbedtls_des_key_set_parity( data );
4895 if( data_size >= 16 )
4896 mbedtls_des_key_set_parity( data + 8 );
4897 if( data_size >= 24 )
4898 mbedtls_des_key_set_parity( data + 16 );
4899}
4900#endif /* MBEDTLS_DES_C */
4901
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004902static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004903 psa_key_slot_t *slot,
4904 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004906{
4907 uint8_t *data = NULL;
4908 size_t bytes = PSA_BITS_TO_BYTES( bits );
4909 psa_status_t status;
4910
Gilles Peskine8e338702019-07-30 20:06:31 +02004911 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004912 return( PSA_ERROR_INVALID_ARGUMENT );
4913 if( bits % 8 != 0 )
4914 return( PSA_ERROR_INVALID_ARGUMENT );
4915 data = mbedtls_calloc( 1, bytes );
4916 if( data == NULL )
4917 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004920 if( status != PSA_SUCCESS )
4921 goto exit;
4922#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004923 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004924 psa_des_set_key_parity( data, bytes );
4925#endif /* MBEDTLS_DES_C */
4926 status = psa_import_key_into_slot( slot, data, bytes );
4927
4928exit:
4929 mbedtls_free( data );
4930 return( status );
4931}
4932
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004933psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004935 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004936{
4937 psa_status_t status;
4938 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004939 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004940 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4941 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4943 if( driver != NULL )
4944 {
4945 /* Deriving a key in a secure element is not implemented yet. */
4946 status = PSA_ERROR_NOT_SUPPORTED;
4947 }
4948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004949 if( status == PSA_SUCCESS )
4950 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004951 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004952 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004954 }
4955 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004956 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004957 if( status != PSA_SUCCESS )
4958 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004959 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004960 *handle = 0;
4961 }
4962 return( status );
4963}
4964
Gilles Peskineeab56e42018-07-12 17:12:33 +02004965
4966
4967/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004968/* Key derivation */
4969/****************************************************************/
4970
Gilles Peskinea05219c2018-11-16 16:02:56 +01004971#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004972#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004974 * of the HKDF algorithm.
4975 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004976 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004977 * to potentially free embedded data structures and wipe confidential data.
4978 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004979static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004980 const uint8_t *secret,
4981 size_t secret_length,
4982 psa_algorithm_t hash_alg,
4983 const uint8_t *salt,
4984 size_t salt_length,
4985 const uint8_t *label,
4986 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004987{
4988 psa_status_t status;
4989 status = psa_hmac_setup_internal( &hkdf->hmac,
4990 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004991 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004992 if( status != PSA_SUCCESS )
4993 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004994 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004995 if( status != PSA_SUCCESS )
4996 return( status );
4997 status = psa_hmac_finish_internal( &hkdf->hmac,
4998 hkdf->prk,
4999 sizeof( hkdf->prk ) );
5000 if( status != PSA_SUCCESS )
5001 return( status );
5002 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5003 hkdf->block_number = 0;
5004 hkdf->info_length = label_length;
5005 if( label_length != 0 )
5006 {
5007 hkdf->info = mbedtls_calloc( 1, label_length );
5008 if( hkdf->info == NULL )
5009 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5010 memcpy( hkdf->info, label, label_length );
5011 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012 hkdf->state = HKDF_STATE_KEYED;
5013 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005014 return( PSA_SUCCESS );
5015}
Janos Follath71a4c912019-06-11 09:14:47 +01005016#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005017#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005018
Gilles Peskinea05219c2018-11-16 16:02:56 +01005019#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01005020#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01005022 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005023 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005024 * to potentially free embedded data structures and wipe confidential data.
5025 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005026static psa_status_t psa_key_derivation_tls12_prf_setup(
5027 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005028 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005029 size_t key_len,
5030 psa_algorithm_t hash_alg,
5031 const uint8_t *salt,
5032 size_t salt_length,
5033 const uint8_t *label,
5034 size_t label_length )
5035{
5036 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00005037 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
5038 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005039
5040 tls12_prf->key = mbedtls_calloc( 1, key_len );
5041 if( tls12_prf->key == NULL )
5042 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5043 tls12_prf->key_len = key_len;
5044 memcpy( tls12_prf->key, key, key_len );
5045
Hanno Becker580fba12018-11-13 20:50:45 +00005046 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005047 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00005048 if( overflow )
5049 return( PSA_ERROR_INVALID_ARGUMENT );
5050
5051 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
5052 if( tls12_prf->Ai_with_seed == NULL )
5053 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5054 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
5055
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005056 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
5057 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00005058 if( label_length != 0 )
5059 {
5060 memcpy( tls12_prf->Ai_with_seed + hash_length,
5061 label, label_length );
5062 }
5063
5064 if( salt_length != 0 )
5065 {
5066 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
5067 salt, salt_length );
5068 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005069
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005070 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005071 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005072 tls12_prf->block_number = 0;
5073 tls12_prf->offset_in_block = hash_length;
5074
5075 return( PSA_SUCCESS );
5076}
Janos Follath71a4c912019-06-11 09:14:47 +01005077#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00005078
Janos Follath71a4c912019-06-11 09:14:47 +01005079#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005081static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
5082 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005083 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005084 size_t psk_len,
5085 psa_algorithm_t hash_alg,
5086 const uint8_t *salt,
5087 size_t salt_length,
5088 const uint8_t *label,
5089 size_t label_length )
5090{
5091 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005092 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00005093
5094 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5095 return( PSA_ERROR_INVALID_ARGUMENT );
5096
5097 /* Quoting RFC 4279, Section 2:
5098 *
5099 * The premaster secret is formed as follows: if the PSK is N octets
5100 * long, concatenate a uint16 with the value N, N zero octets, a second
5101 * uint16 with the value N, and the PSK itself.
5102 */
5103
5104 pms[0] = ( psk_len >> 8 ) & 0xff;
5105 pms[1] = ( psk_len >> 0 ) & 0xff;
5106 memset( pms + 2, 0, psk_len );
5107 pms[2 + psk_len + 0] = pms[0];
5108 pms[2 + psk_len + 1] = pms[1];
5109 memcpy( pms + 4 + psk_len, psk, psk_len );
5110
Gilles Peskinecbe66502019-05-16 16:59:18 +02005111 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005112 pms, 4 + 2 * psk_len,
5113 hash_alg,
5114 salt, salt_length,
5115 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005116
Gilles Peskine3f108122018-12-07 18:14:53 +01005117 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005118 return( status );
5119}
Janos Follath71a4c912019-06-11 09:14:47 +01005120#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005121#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005122
Janos Follath71a4c912019-06-11 09:14:47 +01005123#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005124/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005125 * to potentially free embedded data structures and wipe confidential data.
5126 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005127static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005129 const uint8_t *secret, size_t secret_length,
5130 psa_algorithm_t alg,
5131 const uint8_t *salt, size_t salt_length,
5132 const uint8_t *label, size_t label_length,
5133 size_t capacity )
5134{
5135 psa_status_t status;
5136 size_t max_capacity;
5137
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005138 /* Set operation->alg even on failure so that abort knows what to do. */
5139 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005140
5141#if defined(MBEDTLS_MD_C)
5142 if( PSA_ALG_IS_HKDF( alg ) )
5143 {
5144 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5145 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5146 if( hash_size == 0 )
5147 return( PSA_ERROR_NOT_SUPPORTED );
5148 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005150 secret, secret_length,
5151 hash_alg,
5152 salt, salt_length,
5153 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005154 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005155 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5156 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5157 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005158 {
5159 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5160 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5161
5162 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5163 if( hash_alg != PSA_ALG_SHA_256 &&
5164 hash_alg != PSA_ALG_SHA_384 )
5165 {
5166 return( PSA_ERROR_NOT_SUPPORTED );
5167 }
5168
5169 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005170
5171 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5172 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005174 secret, secret_length,
5175 hash_alg, salt, salt_length,
5176 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005177 }
5178 else
5179 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005180 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005182 secret, secret_length,
5183 hash_alg, salt, salt_length,
5184 label, label_length );
5185 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005186 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005187 else
5188#endif
5189 {
5190 return( PSA_ERROR_NOT_SUPPORTED );
5191 }
5192
5193 if( status != PSA_SUCCESS )
5194 return( status );
5195
5196 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005198 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005200 else
5201 return( PSA_ERROR_INVALID_ARGUMENT );
5202
5203 return( PSA_SUCCESS );
5204}
Janos Follath71a4c912019-06-11 09:14:47 +01005205#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005206
Janos Follath71a4c912019-06-11 09:14:47 +01005207#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005209 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005210 psa_algorithm_t alg,
5211 const uint8_t *salt,
5212 size_t salt_length,
5213 const uint8_t *label,
5214 size_t label_length,
5215 size_t capacity )
5216{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005217 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005218 psa_status_t status;
5219
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005221 return( PSA_ERROR_BAD_STATE );
5222
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005223 /* Make sure that alg is a key derivation algorithm. This prevents
5224 * key selection algorithms, which psa_key_derivation_internal
5225 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005226 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5227 return( PSA_ERROR_INVALID_ARGUMENT );
5228
Gilles Peskine28f8f302019-07-24 13:30:31 +02005229 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005230 if( status != PSA_SUCCESS )
5231 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005232
Gilles Peskine8e338702019-07-30 20:06:31 +02005233 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005234 return( PSA_ERROR_INVALID_ARGUMENT );
5235
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005237 slot->data.raw.data,
5238 slot->data.raw.bytes,
5239 alg,
5240 salt, salt_length,
5241 label, label_length,
5242 capacity );
5243 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005245 return( status );
5246}
Janos Follath71a4c912019-06-11 09:14:47 +01005247#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005248
Gilles Peskine969c5d62019-01-16 15:53:06 +01005249static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005251 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252{
Janos Follath5fe19732019-06-20 15:09:30 +01005253 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5254 * initialisers for this union leave some bytes unspecified.) */
5255 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5256
Gilles Peskine969c5d62019-01-16 15:53:06 +01005257 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005258#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005259 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5260 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5261 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005262 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005263 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005264 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5265 if( hash_size == 0 )
5266 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005267 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5268 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005269 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005270 {
5271 return( PSA_ERROR_NOT_SUPPORTED );
5272 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005274 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 }
5276#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005277 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005278 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005279}
5280
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005282 psa_algorithm_t alg )
5283{
5284 psa_status_t status;
5285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005287 return( PSA_ERROR_BAD_STATE );
5288
Gilles Peskine6843c292019-01-18 16:44:49 +01005289 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5290 return( PSA_ERROR_INVALID_ARGUMENT );
5291 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292 {
5293 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005295 }
5296 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5297 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005299 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 else
5301 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005302
5303 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005304 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005305 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005306}
5307
5308#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005309static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005310 psa_algorithm_t hash_alg,
5311 psa_key_derivation_step_t step,
5312 const uint8_t *data,
5313 size_t data_length )
5314{
5315 psa_status_t status;
5316 switch( step )
5317 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005318 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005319 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005320 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005321 status = psa_hmac_setup_internal( &hkdf->hmac,
5322 data, data_length,
5323 hash_alg );
5324 if( status != PSA_SUCCESS )
5325 return( status );
5326 hkdf->state = HKDF_STATE_STARTED;
5327 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005328 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005329 /* If no salt was provided, use an empty salt. */
5330 if( hkdf->state == HKDF_STATE_INIT )
5331 {
5332 status = psa_hmac_setup_internal( &hkdf->hmac,
5333 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005334 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335 if( status != PSA_SUCCESS )
5336 return( status );
5337 hkdf->state = HKDF_STATE_STARTED;
5338 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005339 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005340 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005341 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5342 data, data_length );
5343 if( status != PSA_SUCCESS )
5344 return( status );
5345 status = psa_hmac_finish_internal( &hkdf->hmac,
5346 hkdf->prk,
5347 sizeof( hkdf->prk ) );
5348 if( status != PSA_SUCCESS )
5349 return( status );
5350 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5351 hkdf->block_number = 0;
5352 hkdf->state = HKDF_STATE_KEYED;
5353 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005354 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005355 if( hkdf->state == HKDF_STATE_OUTPUT )
5356 return( PSA_ERROR_BAD_STATE );
5357 if( hkdf->info_set )
5358 return( PSA_ERROR_BAD_STATE );
5359 hkdf->info_length = data_length;
5360 if( data_length != 0 )
5361 {
5362 hkdf->info = mbedtls_calloc( 1, data_length );
5363 if( hkdf->info == NULL )
5364 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5365 memcpy( hkdf->info, data, data_length );
5366 }
5367 hkdf->info_set = 1;
5368 return( PSA_SUCCESS );
5369 default:
5370 return( PSA_ERROR_INVALID_ARGUMENT );
5371 }
5372}
Janos Follathb03233e2019-06-11 15:30:30 +01005373
5374#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5375static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5376 psa_algorithm_t hash_alg,
5377 psa_key_derivation_step_t step,
5378 const uint8_t *data,
5379 size_t data_length )
5380{
5381 (void) prf;
5382 (void) hash_alg;
5383 (void) step;
5384 (void) data;
5385 (void) data_length;
5386
5387 return( PSA_ERROR_INVALID_ARGUMENT );
5388}
Janos Follath6660f0e2019-06-17 08:44:03 +01005389
5390static psa_status_t psa_tls12_prf_psk_to_ms_input(
5391 psa_tls12_prf_key_derivation_t *prf,
5392 psa_algorithm_t hash_alg,
5393 psa_key_derivation_step_t step,
5394 const uint8_t *data,
5395 size_t data_length )
5396{
5397 (void) prf;
5398 (void) hash_alg;
5399 (void) step;
5400 (void) data;
5401 (void) data_length;
5402
5403 return( PSA_ERROR_INVALID_ARGUMENT );
5404}
Janos Follathb03233e2019-06-11 15:30:30 +01005405#else
Janos Follathf08e2652019-06-13 09:05:41 +01005406static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5407 const uint8_t *data,
5408 size_t data_length )
5409{
5410 if( prf->state != TLS12_PRF_STATE_INIT )
5411 return( PSA_ERROR_BAD_STATE );
5412
Janos Follathd6dce9f2019-07-04 09:11:38 +01005413 if( data_length != 0 )
5414 {
5415 prf->seed = mbedtls_calloc( 1, data_length );
5416 if( prf->seed == NULL )
5417 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005418
Janos Follathd6dce9f2019-07-04 09:11:38 +01005419 memcpy( prf->seed, data, data_length );
5420 prf->seed_length = data_length;
5421 }
Janos Follathf08e2652019-06-13 09:05:41 +01005422
5423 prf->state = TLS12_PRF_STATE_SEED_SET;
5424
5425 return( PSA_SUCCESS );
5426}
5427
Janos Follath81550542019-06-13 14:26:34 +01005428static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5429 psa_algorithm_t hash_alg,
5430 const uint8_t *data,
5431 size_t data_length )
5432{
5433 psa_status_t status;
5434 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5435 return( PSA_ERROR_BAD_STATE );
5436
5437 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5438 if( status != PSA_SUCCESS )
5439 return( status );
5440
5441 prf->state = TLS12_PRF_STATE_KEY_SET;
5442
5443 return( PSA_SUCCESS );
5444}
5445
Janos Follath6660f0e2019-06-17 08:44:03 +01005446static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5447 psa_tls12_prf_key_derivation_t *prf,
5448 psa_algorithm_t hash_alg,
5449 const uint8_t *data,
5450 size_t data_length )
5451{
5452 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005453 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5454 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005455
5456 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5457 return( PSA_ERROR_INVALID_ARGUMENT );
5458
5459 /* Quoting RFC 4279, Section 2:
5460 *
5461 * The premaster secret is formed as follows: if the PSK is N octets
5462 * long, concatenate a uint16 with the value N, N zero octets, a second
5463 * uint16 with the value N, and the PSK itself.
5464 */
5465
Janos Follath40e13932019-06-26 13:22:29 +01005466 *cur++ = ( data_length >> 8 ) & 0xff;
5467 *cur++ = ( data_length >> 0 ) & 0xff;
5468 memset( cur, 0, data_length );
5469 cur += data_length;
5470 *cur++ = pms[0];
5471 *cur++ = pms[1];
5472 memcpy( cur, data, data_length );
5473 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005474
Janos Follath40e13932019-06-26 13:22:29 +01005475 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005476
5477 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5478 return( status );
5479}
5480
Janos Follath63028dd2019-06-13 09:15:47 +01005481static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5482 const uint8_t *data,
5483 size_t data_length )
5484{
5485 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5486 return( PSA_ERROR_BAD_STATE );
5487
Janos Follathd6dce9f2019-07-04 09:11:38 +01005488 if( data_length != 0 )
5489 {
5490 prf->label = mbedtls_calloc( 1, data_length );
5491 if( prf->label == NULL )
5492 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005493
Janos Follathd6dce9f2019-07-04 09:11:38 +01005494 memcpy( prf->label, data, data_length );
5495 prf->label_length = data_length;
5496 }
Janos Follath63028dd2019-06-13 09:15:47 +01005497
5498 prf->state = TLS12_PRF_STATE_LABEL_SET;
5499
5500 return( PSA_SUCCESS );
5501}
5502
Janos Follathb03233e2019-06-11 15:30:30 +01005503static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5504 psa_algorithm_t hash_alg,
5505 psa_key_derivation_step_t step,
5506 const uint8_t *data,
5507 size_t data_length )
5508{
Janos Follathb03233e2019-06-11 15:30:30 +01005509 switch( step )
5510 {
Janos Follathf08e2652019-06-13 09:05:41 +01005511 case PSA_KEY_DERIVATION_INPUT_SEED:
5512 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005513 case PSA_KEY_DERIVATION_INPUT_SECRET:
5514 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005515 case PSA_KEY_DERIVATION_INPUT_LABEL:
5516 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005517 default:
5518 return( PSA_ERROR_INVALID_ARGUMENT );
5519 }
5520}
Janos Follath6660f0e2019-06-17 08:44:03 +01005521
5522static psa_status_t psa_tls12_prf_psk_to_ms_input(
5523 psa_tls12_prf_key_derivation_t *prf,
5524 psa_algorithm_t hash_alg,
5525 psa_key_derivation_step_t step,
5526 const uint8_t *data,
5527 size_t data_length )
5528{
5529 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005530 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005531 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5532 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005533 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005534
5535 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5536}
Janos Follathb03233e2019-06-11 15:30:30 +01005537#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005538#endif /* MBEDTLS_MD_C */
5539
Janos Follathb80a94e2019-06-12 15:54:46 +01005540static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005542 psa_key_derivation_step_t step,
5543 const uint8_t *data,
5544 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005545{
5546 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005547 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005548
5549#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005550 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005551 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005552 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005553 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005554 step, data, data_length );
5555 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005556 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005557#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005558#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005559 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005560 {
Janos Follathb03233e2019-06-11 15:30:30 +01005561 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5562 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5563 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005564 }
5565 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5566 {
5567 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5568 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5569 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570 }
5571 else
5572#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005574 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005575 return( PSA_ERROR_BAD_STATE );
5576 }
5577
5578 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005580 return( status );
5581}
5582
Janos Follath51f4a0f2019-06-14 11:35:55 +01005583psa_status_t psa_key_derivation_input_bytes(
5584 psa_key_derivation_operation_t *operation,
5585 psa_key_derivation_step_t step,
5586 const uint8_t *data,
5587 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005588{
Janos Follathc5621512019-06-14 11:27:57 +01005589 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5590 return( PSA_ERROR_INVALID_ARGUMENT );
5591
5592 return( psa_key_derivation_input_internal( operation, step,
5593 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005594}
5595
Janos Follath51f4a0f2019-06-14 11:35:55 +01005596psa_status_t psa_key_derivation_input_key(
5597 psa_key_derivation_operation_t *operation,
5598 psa_key_derivation_step_t step,
5599 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005600{
5601 psa_key_slot_t *slot;
5602 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005603 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005604 PSA_KEY_USAGE_DERIVE,
5605 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005606 if( status != PSA_SUCCESS )
5607 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005608 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005609 return( PSA_ERROR_INVALID_ARGUMENT );
5610 /* Don't allow a key to be used as an input that is usually public.
5611 * This is debatable. It's ok from a cryptographic perspective to
5612 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005613 * the material should be dedicated to a particular input step,
5614 * otherwise this may allow the key to be used in an unintended way
5615 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005616 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005618 return( psa_key_derivation_input_internal( operation,
5619 step,
5620 slot->data.raw.data,
5621 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005622}
5623
Gilles Peskineea0fb492018-07-12 17:17:20 +02005624
5625
5626/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005627/* Key agreement */
5628/****************************************************************/
5629
Gilles Peskinea05219c2018-11-16 16:02:56 +01005630#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005631static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5632 size_t peer_key_length,
5633 const mbedtls_ecp_keypair *our_key,
5634 uint8_t *shared_secret,
5635 size_t shared_secret_size,
5636 size_t *shared_secret_length )
5637{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005638 mbedtls_ecp_keypair *their_key = NULL;
5639 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005640 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005641 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005642
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005643 status = psa_import_ec_public_key(
5644 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5645 peer_key, peer_key_length,
5646 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005647 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005648 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005649
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005650 status = mbedtls_to_psa_error(
5651 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5652 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005653 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005654 status = mbedtls_to_psa_error(
5655 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5656 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005657 goto exit;
5658
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005659 status = mbedtls_to_psa_error(
5660 mbedtls_ecdh_calc_secret( &ecdh,
5661 shared_secret_length,
5662 shared_secret, shared_secret_size,
5663 mbedtls_ctr_drbg_random,
5664 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005665
5666exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005667 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005668 mbedtls_ecp_keypair_free( their_key );
5669 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005670 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005671}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005672#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005673
Gilles Peskine01d718c2018-09-18 12:01:02 +02005674#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5675
Gilles Peskine0216fe12019-04-11 21:23:21 +02005676static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5677 psa_key_slot_t *private_key,
5678 const uint8_t *peer_key,
5679 size_t peer_key_length,
5680 uint8_t *shared_secret,
5681 size_t shared_secret_size,
5682 size_t *shared_secret_length )
5683{
5684 switch( alg )
5685 {
5686#if defined(MBEDTLS_ECDH_C)
5687 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005688 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005689 return( PSA_ERROR_INVALID_ARGUMENT );
5690 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5691 private_key->data.ecp,
5692 shared_secret, shared_secret_size,
5693 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005694#endif /* MBEDTLS_ECDH_C */
5695 default:
5696 (void) private_key;
5697 (void) peer_key;
5698 (void) peer_key_length;
5699 (void) shared_secret;
5700 (void) shared_secret_size;
5701 (void) shared_secret_length;
5702 return( PSA_ERROR_NOT_SUPPORTED );
5703 }
5704}
5705
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005706/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005707 * to potentially free embedded data structures and wipe confidential data.
5708 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005709static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005710 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005711 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005712 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005713 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005714{
5715 psa_status_t status;
5716 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5717 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005718 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005719
5720 /* Step 1: run the secret agreement algorithm to generate the shared
5721 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005722 status = psa_key_agreement_raw_internal( ka_alg,
5723 private_key,
5724 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005725 shared_secret,
5726 sizeof( shared_secret ),
5727 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005728 if( status != PSA_SUCCESS )
5729 goto exit;
5730
5731 /* Step 2: set up the key derivation to generate key material from
5732 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005733 status = psa_key_derivation_input_internal( operation, step,
5734 shared_secret,
5735 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005736
Gilles Peskine01d718c2018-09-18 12:01:02 +02005737exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005738 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005739 return( status );
5740}
5741
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005743 psa_key_derivation_step_t step,
5744 psa_key_handle_t private_key,
5745 const uint8_t *peer_key,
5746 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005747{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005748 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005749 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005751 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005752 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005753 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005754 if( status != PSA_SUCCESS )
5755 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005757 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005758 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005759 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005761 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005762}
5763
Gilles Peskinebe697d82019-05-16 18:00:41 +02005764psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5765 psa_key_handle_t private_key,
5766 const uint8_t *peer_key,
5767 size_t peer_key_length,
5768 uint8_t *output,
5769 size_t output_size,
5770 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005771{
5772 psa_key_slot_t *slot;
5773 psa_status_t status;
5774
5775 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5776 {
5777 status = PSA_ERROR_INVALID_ARGUMENT;
5778 goto exit;
5779 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005780 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005781 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005782 if( status != PSA_SUCCESS )
5783 goto exit;
5784
5785 status = psa_key_agreement_raw_internal( alg, slot,
5786 peer_key, peer_key_length,
5787 output, output_size,
5788 output_length );
5789
5790exit:
5791 if( status != PSA_SUCCESS )
5792 {
5793 /* If an error happens and is not handled properly, the output
5794 * may be used as a key to protect sensitive data. Arrange for such
5795 * a key to be random, which is likely to result in decryption or
5796 * verification errors. This is better than filling the buffer with
5797 * some constant data such as zeros, which would result in the data
5798 * being protected with a reproducible, easily knowable key.
5799 */
5800 psa_generate_random( output, output_size );
5801 *output_length = output_size;
5802 }
5803 return( status );
5804}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005805
5806
5807/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005808/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005809/****************************************************************/
5810
5811psa_status_t psa_generate_random( uint8_t *output,
5812 size_t output_size )
5813{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005814 int ret;
5815 GUARD_MODULE_INITIALIZED;
5816
Gilles Peskinef181eca2019-08-07 13:49:00 +02005817 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5818 {
5819 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5820 output,
5821 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5822 if( ret != 0 )
5823 return( mbedtls_to_psa_error( ret ) );
5824 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5825 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5826 }
5827
itayzafrir0adf0fc2018-09-06 16:24:41 +03005828 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005829 return( mbedtls_to_psa_error( ret ) );
5830}
5831
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005832#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5833#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005834
Gilles Peskine7228da22019-07-15 11:06:15 +02005835psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005836 size_t seed_size )
5837{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005838 if( global_data.initialized )
5839 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005840
5841 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5842 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5843 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5844 return( PSA_ERROR_INVALID_ARGUMENT );
5845
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005846 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005847}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005848#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005849
Gilles Peskinee56e8782019-04-26 17:34:02 +02005850#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5851static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5852 size_t domain_parameters_size,
5853 int *exponent )
5854{
5855 size_t i;
5856 uint32_t acc = 0;
5857
5858 if( domain_parameters_size == 0 )
5859 {
5860 *exponent = 65537;
5861 return( PSA_SUCCESS );
5862 }
5863
5864 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5865 * support values that fit in a 32-bit integer, which is larger than
5866 * int on just about every platform anyway. */
5867 if( domain_parameters_size > sizeof( acc ) )
5868 return( PSA_ERROR_NOT_SUPPORTED );
5869 for( i = 0; i < domain_parameters_size; i++ )
5870 acc = ( acc << 8 ) | domain_parameters[i];
5871 if( acc > INT_MAX )
5872 return( PSA_ERROR_NOT_SUPPORTED );
5873 *exponent = acc;
5874 return( PSA_SUCCESS );
5875}
5876#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5877
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005878static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005879 psa_key_slot_t *slot, size_t bits,
5880 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005881{
Gilles Peskine8e338702019-07-30 20:06:31 +02005882 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005883
Gilles Peskinee56e8782019-04-26 17:34:02 +02005884 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005885 return( PSA_ERROR_INVALID_ARGUMENT );
5886
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005887 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005888 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005889 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005890 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005891 if( status != PSA_SUCCESS )
5892 return( status );
5893 status = psa_generate_random( slot->data.raw.data,
5894 slot->data.raw.bytes );
5895 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005896 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005897#if defined(MBEDTLS_DES_C)
5898 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005899 psa_des_set_key_parity( slot->data.raw.data,
5900 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005901#endif /* MBEDTLS_DES_C */
5902 }
5903 else
5904
5905#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005906 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005907 {
5908 mbedtls_rsa_context *rsa;
5909 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005910 int exponent;
5911 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005912 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5913 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005914 /* Accept only byte-aligned keys, for the same reasons as
5915 * in psa_import_rsa_key(). */
5916 if( bits % 8 != 0 )
5917 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005918 status = psa_read_rsa_exponent( domain_parameters,
5919 domain_parameters_size,
5920 &exponent );
5921 if( status != PSA_SUCCESS )
5922 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005923 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5924 if( rsa == NULL )
5925 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5926 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5927 ret = mbedtls_rsa_gen_key( rsa,
5928 mbedtls_ctr_drbg_random,
5929 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005930 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005931 exponent );
5932 if( ret != 0 )
5933 {
5934 mbedtls_rsa_free( rsa );
5935 mbedtls_free( rsa );
5936 return( mbedtls_to_psa_error( ret ) );
5937 }
5938 slot->data.rsa = rsa;
5939 }
5940 else
5941#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5942
5943#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005944 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005945 {
5946 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5947 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5948 const mbedtls_ecp_curve_info *curve_info =
5949 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5950 mbedtls_ecp_keypair *ecp;
5951 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005952 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005953 return( PSA_ERROR_NOT_SUPPORTED );
5954 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5955 return( PSA_ERROR_NOT_SUPPORTED );
5956 if( curve_info->bit_size != bits )
5957 return( PSA_ERROR_INVALID_ARGUMENT );
5958 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5959 if( ecp == NULL )
5960 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5961 mbedtls_ecp_keypair_init( ecp );
5962 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5963 mbedtls_ctr_drbg_random,
5964 &global_data.ctr_drbg );
5965 if( ret != 0 )
5966 {
5967 mbedtls_ecp_keypair_free( ecp );
5968 mbedtls_free( ecp );
5969 return( mbedtls_to_psa_error( ret ) );
5970 }
5971 slot->data.ecp = ecp;
5972 }
5973 else
5974#endif /* MBEDTLS_ECP_C */
5975
5976 return( PSA_ERROR_NOT_SUPPORTED );
5977
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005978 return( PSA_SUCCESS );
5979}
5980
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005981psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005982 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005983{
5984 psa_status_t status;
5985 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005986 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005987
Gilles Peskinedf179142019-07-15 22:02:14 +02005988 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5989 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005990 if( status != PSA_SUCCESS )
5991 goto exit;
5992
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005993#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5994 if( driver != NULL )
5995 {
Gilles Peskine11792082019-08-06 18:36:36 +02005996 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5997 size_t pubkey_length = 0; /* We don't support this feature yet */
5998 if( drv->key_management == NULL ||
5999 drv->key_management->p_generate == NULL )
6000 {
6001 status = PSA_ERROR_NOT_SUPPORTED;
6002 goto exit;
6003 }
6004 status = drv->key_management->p_generate(
6005 psa_get_se_driver_context( driver ),
6006 slot->data.se.slot_number, attributes,
6007 NULL, 0, &pubkey_length );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006008 }
Gilles Peskine11792082019-08-06 18:36:36 +02006009 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006010#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006011 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006012 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006013 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006014 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006015 }
Gilles Peskine11792082019-08-06 18:36:36 +02006016
6017exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006018 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006019 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006020 if( status != PSA_SUCCESS )
6021 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006022 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006023 *handle = 0;
6024 }
6025 return( status );
6026}
6027
6028
Gilles Peskine05d69892018-06-19 22:00:52 +02006029
6030/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01006031/* Module setup */
6032/****************************************************************/
6033
Gilles Peskine5e769522018-11-20 21:59:56 +01006034psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6035 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6036 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6037{
6038 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6039 return( PSA_ERROR_BAD_STATE );
6040 global_data.entropy_init = entropy_init;
6041 global_data.entropy_free = entropy_free;
6042 return( PSA_SUCCESS );
6043}
6044
Gilles Peskinee59236f2018-01-27 23:32:46 +01006045void mbedtls_psa_crypto_free( void )
6046{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006047 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006048 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6049 {
6050 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006051 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006052 }
6053 /* Wipe all remaining data, including configuration.
6054 * In particular, this sets all state indicator to the value
6055 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006056 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006057#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006058 /* Unregister all secure element drivers, so that we restart from
6059 * a pristine state. */
6060 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006061#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006062}
6063
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006064#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6065/** Recover a transaction that was interrupted by a power failure.
6066 *
6067 * This function is called during initialization, before psa_crypto_init()
6068 * returns. If this function returns a failure status, the initialization
6069 * fails.
6070 */
6071static psa_status_t psa_crypto_recover_transaction(
6072 const psa_crypto_transaction_t *transaction )
6073{
6074 switch( transaction->unknown.type )
6075 {
6076 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6077 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
6078 /* TOnogrepDO - fall through to the failure case until this
6079 * is implemented */
6080 default:
6081 /* We found an unsupported transaction in the storage.
6082 * We don't know what state the storage is in. Give up. */
6083 return( PSA_ERROR_STORAGE_FAILURE );
6084 }
6085}
6086#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6087
Gilles Peskinee59236f2018-01-27 23:32:46 +01006088psa_status_t psa_crypto_init( void )
6089{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006090 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006091 const unsigned char drbg_seed[] = "PSA";
6092
Gilles Peskinec6b69072018-11-20 21:42:52 +01006093 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006094 if( global_data.initialized != 0 )
6095 return( PSA_SUCCESS );
6096
Gilles Peskine5e769522018-11-20 21:59:56 +01006097 /* Set default configuration if
6098 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6099 if( global_data.entropy_init == NULL )
6100 global_data.entropy_init = mbedtls_entropy_init;
6101 if( global_data.entropy_free == NULL )
6102 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006103
Gilles Peskinec6b69072018-11-20 21:42:52 +01006104 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006105 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006106#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6107 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6108 /* The PSA entropy injection feature depends on using NV seed as an entropy
6109 * source. Add NV seed as an entropy source for PSA entropy injection. */
6110 mbedtls_entropy_add_source( &global_data.entropy,
6111 mbedtls_nv_seed_poll, NULL,
6112 MBEDTLS_ENTROPY_BLOCK_SIZE,
6113 MBEDTLS_ENTROPY_SOURCE_STRONG );
6114#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006115 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006116 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006117 status = mbedtls_to_psa_error(
6118 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6119 mbedtls_entropy_func,
6120 &global_data.entropy,
6121 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6122 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006123 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006124 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006125
Gilles Peskine66fb1262018-12-10 16:29:04 +01006126 status = psa_initialize_key_slots( );
6127 if( status != PSA_SUCCESS )
6128 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006129
Gilles Peskine4b734222019-07-24 15:56:31 +02006130#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006131 status = psa_crypto_load_transaction( );
6132 if( status == PSA_SUCCESS )
6133 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006134 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6135 if( status != PSA_SUCCESS )
6136 goto exit;
6137 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006138 }
6139 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6140 {
6141 /* There's no transaction to complete. It's all good. */
6142 status = PSA_SUCCESS;
6143 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006144#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006145
Gilles Peskinec6b69072018-11-20 21:42:52 +01006146 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006147 global_data.initialized = 1;
6148
Gilles Peskinee59236f2018-01-27 23:32:46 +01006149exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006150 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006152 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006153}
6154
6155#endif /* MBEDTLS_PSA_CRYPTO_C */