blob: 34a02319065b0cb41f70986a22e5d8c56bb03c92 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
597static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000772 {
773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000777 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 else
779#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200792
793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000800 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100801}
802
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100815 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
958 {
959 /* No key material to clean. */
960 }
961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997/** Completely wipe a slot in memory, including its policy.
998 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100999psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000{
1001 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001002 /* Multipart operations may still be using the key. This is safe
1003 * because all multipart operation objects are independent from
1004 * the key slot: if they need to access the key after the setup
1005 * phase, they have a copy of the key. Note that this means that
1006 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007 /* At this point, key material and other type-specific content has
1008 * been wiped. Clear remaining metadata. We can call memset and not
1009 * zeroize because the metadata is not particularly sensitive. */
1010 memset( slot, 0, sizeof( *slot ) );
1011 return( status );
1012}
1013
Gilles Peskinec5487a82018-12-03 18:08:14 +01001014psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001016 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001017 psa_status_t status; /* status of the last operation */
1018 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1020 psa_se_drv_table_entry_t *driver;
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskinec5487a82018-12-03 18:08:14 +01001023 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001024 if( status != PSA_SUCCESS )
1025 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001026
1027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001030 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001031 /* For a key in a secure element, we need to do three things:
1032 * remove the key file in internal storage, destroy the
1033 * key inside the secure element, and update the driver's
1034 * persistent data. Start a transaction that will encompass these
1035 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 status = psa_crypto_save_transaction( );
1041 if( status != PSA_SUCCESS )
1042 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001043 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001044 /* We should still try to destroy the key in the secure
1045 * element and the key metadata in storage. This is especially
1046 * important if the error is that the storage is full.
1047 * But how to do it exactly without risking an inconsistent
1048 * state after a reset?
1049 * https://github.com/ARMmbed/mbed-crypto/issues/215
1050 */
1051 overall_status = status;
1052 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001053 }
1054
Gilles Peskine354f7672019-07-12 23:46:38 +02001055 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 if( overall_status == PSA_SUCCESS )
1057 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001058 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1060
Darryl Greend49a4992018-06-18 17:27:26 +01001061#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001062 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001063 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 status = psa_destroy_persistent_key( slot->attr.id );
1065 if( overall_status == PSA_SUCCESS )
1066 overall_status = status;
1067
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001068 /* TODO: other slots may have a copy of the same key. We should
1069 * invalidate them.
1070 * https://github.com/ARMmbed/mbed-crypto/issues/214
1071 */
Darryl Greend49a4992018-06-18 17:27:26 +01001072 }
1073#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001074
Gilles Peskinefc762652019-07-22 19:30:34 +02001075#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1076 if( driver != NULL )
1077 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001078 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001079 if( overall_status == PSA_SUCCESS )
1080 overall_status = status;
1081 status = psa_crypto_stop_transaction( );
1082 if( overall_status == PSA_SUCCESS )
1083 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001084 }
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1086
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088exit:
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001090 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1092 if( overall_status == PSA_SUCCESS )
1093 overall_status = status;
1094 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095}
1096
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001097void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1098{
Gilles Peskineb699f072019-04-26 16:06:02 +02001099 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100 memset( attributes, 0, sizeof( *attributes ) );
1101}
1102
Gilles Peskineb699f072019-04-26 16:06:02 +02001103psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1104 psa_key_type_t type,
1105 const uint8_t *data,
1106 size_t data_length )
1107{
1108 uint8_t *copy = NULL;
1109
1110 if( data_length != 0 )
1111 {
1112 copy = mbedtls_calloc( 1, data_length );
1113 if( copy == NULL )
1114 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1115 memcpy( copy, data, data_length );
1116 }
1117 /* After this point, this function is guaranteed to succeed, so it
1118 * can start modifying `*attributes`. */
1119
1120 if( attributes->domain_parameters != NULL )
1121 {
1122 mbedtls_free( attributes->domain_parameters );
1123 attributes->domain_parameters = NULL;
1124 attributes->domain_parameters_size = 0;
1125 }
1126
1127 attributes->domain_parameters = copy;
1128 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001129 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001130 return( PSA_SUCCESS );
1131}
1132
1133psa_status_t psa_get_key_domain_parameters(
1134 const psa_key_attributes_t *attributes,
1135 uint8_t *data, size_t data_size, size_t *data_length )
1136{
1137 if( attributes->domain_parameters_size > data_size )
1138 return( PSA_ERROR_BUFFER_TOO_SMALL );
1139 *data_length = attributes->domain_parameters_size;
1140 if( attributes->domain_parameters_size != 0 )
1141 memcpy( data, attributes->domain_parameters,
1142 attributes->domain_parameters_size );
1143 return( PSA_SUCCESS );
1144}
1145
1146#if defined(MBEDTLS_RSA_C)
1147static psa_status_t psa_get_rsa_public_exponent(
1148 const mbedtls_rsa_context *rsa,
1149 psa_key_attributes_t *attributes )
1150{
1151 mbedtls_mpi mpi;
1152 int ret;
1153 uint8_t *buffer = NULL;
1154 size_t buflen;
1155 mbedtls_mpi_init( &mpi );
1156
1157 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1158 if( ret != 0 )
1159 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001160 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1161 {
1162 /* It's the default value, which is reported as an empty string,
1163 * so there's nothing to do. */
1164 goto exit;
1165 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001166
1167 buflen = mbedtls_mpi_size( &mpi );
1168 buffer = mbedtls_calloc( 1, buflen );
1169 if( buffer == NULL )
1170 {
1171 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1172 goto exit;
1173 }
1174 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1175 if( ret != 0 )
1176 goto exit;
1177 attributes->domain_parameters = buffer;
1178 attributes->domain_parameters_size = buflen;
1179
1180exit:
1181 mbedtls_mpi_free( &mpi );
1182 if( ret != 0 )
1183 mbedtls_free( buffer );
1184 return( mbedtls_to_psa_error( ret ) );
1185}
1186#endif /* MBEDTLS_RSA_C */
1187
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001188/** Retrieve all the publicly-accessible attributes of a key.
1189 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1191 psa_key_attributes_t *attributes )
1192{
1193 psa_key_slot_t *slot;
1194 psa_status_t status;
1195
1196 psa_reset_key_attributes( attributes );
1197
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001198 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001199 if( status != PSA_SUCCESS )
1200 return( status );
1201
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001202 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001203 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1204 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1205
1206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1207 if( psa_key_slot_is_external( slot ) )
1208 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 {
1213#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001214 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001215 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001216#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001217 /* TODO: reporting the public exponent for opaque keys
1218 * is not yet implemented.
1219 * https://github.com/ARMmbed/mbed-crypto/issues/216
1220 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001221 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001222 break;
1223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1225 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001226#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 default:
1228 /* Nothing else to do. */
1229 break;
1230 }
1231
1232 if( status != PSA_SUCCESS )
1233 psa_reset_key_attributes( attributes );
1234 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001235}
1236
Gilles Peskinec8000c02019-08-02 20:15:51 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1238psa_status_t psa_get_key_slot_number(
1239 const psa_key_attributes_t *attributes,
1240 psa_key_slot_number_t *slot_number )
1241{
1242 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1243 {
1244 *slot_number = attributes->slot_number;
1245 return( PSA_SUCCESS );
1246 }
1247 else
1248 return( PSA_ERROR_INVALID_ARGUMENT );
1249}
1250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1251
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001252#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001253static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1254 unsigned char *buf, size_t size )
1255{
1256 int ret;
1257 unsigned char *c;
1258 size_t len = 0;
1259
1260 c = buf + size;
1261
1262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1263
1264 return( (int) len );
1265}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001266#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001267
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001268static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1269 uint8_t *data,
1270 size_t data_size,
1271 size_t *data_length,
1272 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001273{
Gilles Peskine5d309672019-07-12 23:47:28 +02001274#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1275 const psa_drv_se_t *drv;
1276 psa_drv_se_context_t *drv_context;
1277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1278
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001279 *data_length = 0;
1280
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001282 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001283
Gilles Peskine5d309672019-07-12 23:47:28 +02001284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001285 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001286 {
1287 psa_drv_se_export_key_t method;
1288 if( drv->key_management == NULL )
1289 return( PSA_ERROR_NOT_SUPPORTED );
1290 method = ( export_public_key ?
1291 drv->key_management->p_export_public :
1292 drv->key_management->p_export );
1293 if( method == NULL )
1294 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001295 return( method( drv_context,
1296 slot->data.se.slot_number,
1297 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001298 }
1299#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1300
Gilles Peskine8e338702019-07-30 20:06:31 +02001301 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302 {
1303 if( slot->data.raw.bytes > data_size )
1304 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001305 if( data_size != 0 )
1306 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001307 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001308 memset( data + slot->data.raw.bytes, 0,
1309 data_size - slot->data.raw.bytes );
1310 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311 *data_length = slot->data.raw.bytes;
1312 return( PSA_SUCCESS );
1313 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001314#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001315 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001317 psa_status_t status;
1318
Gilles Peskineb46bef22019-07-30 21:32:04 +02001319 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001320 if( bytes > data_size )
1321 return( PSA_ERROR_BUFFER_TOO_SMALL );
1322 status = mbedtls_to_psa_error(
1323 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1324 if( status != PSA_SUCCESS )
1325 return( status );
1326 memset( data + bytes, 0, data_size - bytes );
1327 *data_length = bytes;
1328 return( PSA_SUCCESS );
1329 }
1330#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331 else
Moran Peker17e36e12018-05-02 12:55:20 +03001332 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001333#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001334 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1335 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001336 {
Moran Pekera998bc62018-04-16 18:16:20 +03001337 mbedtls_pk_context pk;
1338 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001339 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001340 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#if defined(MBEDTLS_RSA_C)
1342 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001343 pk.pk_info = &mbedtls_rsa_info;
1344 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001345#else
1346 return( PSA_ERROR_NOT_SUPPORTED );
1347#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001348 }
1349 else
1350 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#if defined(MBEDTLS_ECP_C)
1352 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001353 pk.pk_info = &mbedtls_eckey_info;
1354 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001358 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001359 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001360 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001361 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001362 }
Moran Peker17e36e12018-05-02 12:55:20 +03001363 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001364 {
Moran Peker17e36e12018-05-02 12:55:20 +03001365 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001366 }
Moran Peker60364322018-04-29 11:34:58 +03001367 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001368 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001369 /* If data_size is 0 then data may be NULL and then the
1370 * call to memset would have undefined behavior. */
1371 if( data_size != 0 )
1372 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001373 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001374 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001375 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1376 * Move the data to the beginning and erase remaining data
1377 * at the original location. */
1378 if( 2 * (size_t) ret <= data_size )
1379 {
1380 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 }
1383 else if( (size_t) ret < data_size )
1384 {
1385 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001386 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001387 }
Moran Pekera998bc62018-04-16 18:16:20 +03001388 *data_length = ret;
1389 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001390 }
1391 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001392#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 {
1394 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001395 it is valid for a special-purpose implementation to omit
1396 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001397 return( PSA_ERROR_NOT_SUPPORTED );
1398 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001399 }
1400}
1401
Gilles Peskinec5487a82018-12-03 18:08:14 +01001402psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001403 uint8_t *data,
1404 size_t data_size,
1405 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001406{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001407 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001408 psa_status_t status;
1409
1410 /* Set the key to empty now, so that even when there are errors, we always
1411 * set data_length to a value between 0 and data_size. On error, setting
1412 * the key to empty is a good choice because an empty key representation is
1413 * unlikely to be accepted anywhere. */
1414 *data_length = 0;
1415
1416 /* Export requires the EXPORT flag. There is an exception for public keys,
1417 * which don't require any flag, but psa_get_key_from_slot takes
1418 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001419 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001420 if( status != PSA_SUCCESS )
1421 return( status );
1422 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001423 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001427 uint8_t *data,
1428 size_t data_size,
1429 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001430{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001431 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001432 psa_status_t status;
1433
1434 /* Set the key to empty now, so that even when there are errors, we always
1435 * set data_length to a value between 0 and data_size. On error, setting
1436 * the key to empty is a good choice because an empty key representation is
1437 * unlikely to be accepted anywhere. */
1438 *data_length = 0;
1439
1440 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001441 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001442 if( status != PSA_SUCCESS )
1443 return( status );
1444 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001445 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001446}
1447
Gilles Peskine91e8c332019-08-02 19:19:39 +02001448#if defined(static_assert)
1449static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1450 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001451static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001452 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001453static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001454 "One or more key attribute flag is listed as both internal-only and external-only" );
1455#endif
1456
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001457/** Validate that a key policy is internally well-formed.
1458 *
1459 * This function only rejects invalid policies. It does not validate the
1460 * consistency of the policy with respect to other attributes of the key
1461 * such as the key type.
1462 */
1463static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001464{
1465 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001466 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 PSA_KEY_USAGE_ENCRYPT |
1468 PSA_KEY_USAGE_DECRYPT |
1469 PSA_KEY_USAGE_SIGN |
1470 PSA_KEY_USAGE_VERIFY |
1471 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1472 return( PSA_ERROR_INVALID_ARGUMENT );
1473
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 return( PSA_SUCCESS );
1475}
1476
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001477/** Validate the internal consistency of key attributes.
1478 *
1479 * This function only rejects invalid attribute values. If does not
1480 * validate the consistency of the attributes with any key data that may
1481 * be involved in the creation of the key.
1482 *
1483 * Call this function early in the key creation process.
1484 *
1485 * \param[in] attributes Key attributes for the new key.
1486 * \param[out] p_drv On any return, the driver for the key, if any.
1487 * NULL for a transparent key.
1488 *
1489 */
1490static psa_status_t psa_validate_key_attributes(
1491 const psa_key_attributes_t *attributes,
1492 psa_se_drv_table_entry_t **p_drv )
1493{
1494 psa_status_t status;
1495
1496 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1497 {
1498 status = psa_validate_persistent_key_parameters(
1499 attributes->core.lifetime, attributes->core.id,
1500 p_drv, 1 );
1501 if( status != PSA_SUCCESS )
1502 return( status );
1503 }
1504
1505 status = psa_validate_key_policy( &attributes->core.policy );
1506 if( status != PSA_SUCCESS )
1507 return( status );
1508
1509 /* Refuse to create overly large keys.
1510 * Note that this doesn't trigger on import if the attributes don't
1511 * explicitly specify a size (so psa_get_key_bits returns 0), so
1512 * psa_import_key() needs its own checks. */
1513 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1514 return( PSA_ERROR_NOT_SUPPORTED );
1515
Gilles Peskine91e8c332019-08-02 19:19:39 +02001516 /* Reject invalid flags. These should not be reachable through the API. */
1517 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1518 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1519 return( PSA_ERROR_INVALID_ARGUMENT );
1520
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001521 return( PSA_SUCCESS );
1522}
1523
Gilles Peskine4747d192019-04-17 15:05:45 +02001524/** Prepare a key slot to receive key material.
1525 *
1526 * This function allocates a key slot and sets its metadata.
1527 *
1528 * If this function fails, call psa_fail_key_creation().
1529 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001530 * This function is intended to be used as follows:
1531 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1532 * it with the specified attributes, and assign it a handle.
1533 * -# Populate the slot with the key material.
1534 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1535 * In case of failure at any step, stop the sequence and call
1536 * psa_fail_key_creation().
1537 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001538 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001539 * \param[in] attributes Key attributes for the new key.
1540 * \param[out] handle On success, a handle for the allocated slot.
1541 * \param[out] p_slot On success, a pointer to the prepared slot.
1542 * \param[out] p_drv On any return, the driver for the key, if any.
1543 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001544 *
1545 * \retval #PSA_SUCCESS
1546 * The key slot is ready to receive key material.
1547 * \return If this function fails, the key slot is an invalid state.
1548 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001549 */
1550static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001551 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001552 const psa_key_attributes_t *attributes,
1553 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001555 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001556{
1557 psa_status_t status;
1558 psa_key_slot_t *slot;
1559
Gilles Peskinedf179142019-07-15 22:02:14 +02001560 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001561 *p_drv = NULL;
1562
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001563 status = psa_validate_key_attributes( attributes, p_drv );
1564 if( status != PSA_SUCCESS )
1565 return( status );
1566
Gilles Peskineedbed562019-08-07 18:19:59 +02001567 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 if( status != PSA_SUCCESS )
1569 return( status );
1570 slot = *p_slot;
1571
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001572 /* We're storing the declared bit-size of the key. It's up to each
1573 * creation mechanism to verify that this information is correct.
1574 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001575 * an input (generate, device) but not for those where the bit-size
1576 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001577
1578 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001579
Gilles Peskine91e8c332019-08-02 19:19:39 +02001580 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001581 * external-only flags, query `attributes`. Thanks to the check
1582 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001583 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001584 * may have set. */
1585 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001586
Gilles Peskinecbaff462019-07-12 23:46:04 +02001587#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001588 /* For a key in a secure element, we need to do three things
1589 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001590 * create the key file in internal storage, create the
1591 * key inside the secure element, and update the driver's
1592 * persistent data. Start a transaction that will encompass these
1593 * three actions. */
1594 /* The first thing to do is to find a slot number for the new key.
1595 * We save the slot number in persistent storage as part of the
1596 * transaction data. It will be needed to recover if the power
1597 * fails during the key creation process, to clean up on the secure
1598 * element side after restarting. Obtaining a slot number from the
1599 * secure element driver updates its persistent state, but we do not yet
1600 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001601 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001602 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001603 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001604 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001605 &slot->data.se.slot_number );
1606 if( status != PSA_SUCCESS )
1607 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001609 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001610 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001611 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001612 status = psa_crypto_save_transaction( );
1613 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001614 {
1615 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001616 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001617 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001618 }
1619#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1620
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 return( status );
1622}
1623
1624/** Finalize the creation of a key once its key material has been set.
1625 *
1626 * This entails writing the key to persistent storage.
1627 *
1628 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001629 * See the documentation of psa_start_key_creation() for the intended use
1630 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001632 * \param[in,out] slot Pointer to the slot with key material.
1633 * \param[in] driver The secure element driver for the key,
1634 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001635 *
1636 * \retval #PSA_SUCCESS
1637 * The key was successfully created. The handle is now valid.
1638 * \return If this function fails, the key slot is an invalid state.
1639 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001640 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001641static psa_status_t psa_finish_key_creation(
1642 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001643 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001644{
1645 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001646 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001648
1649#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001650 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001652#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1653 if( driver != NULL )
1654 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001655 psa_se_key_data_storage_t data;
1656#if defined(static_assert)
1657 static_assert( sizeof( slot->data.se.slot_number ) ==
1658 sizeof( data.slot_number ),
1659 "Slot number size does not match psa_se_key_data_storage_t" );
1660 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1661 "Bit-size size does not match psa_se_key_data_storage_t" );
1662#endif
1663 memcpy( &data.slot_number, &slot->data.se.slot_number,
1664 sizeof( slot->data.se.slot_number ) );
1665 memcpy( &data.bits, &slot->attr.bits,
1666 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001667 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001668 (uint8_t*) &data,
1669 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001670 }
1671 else
1672#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1673 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001674 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001675 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001676 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001677 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1678 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001679 if( buffer == NULL && buffer_size != 0 )
1680 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1681 status = psa_internal_export_key( slot,
1682 buffer, buffer_size, &length,
1683 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001684 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001685 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001686 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001687
Gilles Peskine1df83d42019-07-23 16:13:14 +02001688 if( buffer_size != 0 )
1689 mbedtls_platform_zeroize( buffer, buffer_size );
1690 mbedtls_free( buffer );
1691 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 }
1693#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1694
Gilles Peskinecbaff462019-07-12 23:46:04 +02001695#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001696 /* Finish the transaction for a key creation. This does not
1697 * happen when registering an existing key. Detect this case
1698 * by checking whether a transaction is in progress (actual
1699 * creation of a key in a secure element requires a transaction,
1700 * but registration doesn't use one). */
1701 if( driver != NULL &&
1702 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703 {
1704 status = psa_save_se_persistent_data( driver );
1705 if( status != PSA_SUCCESS )
1706 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001707 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001708 return( status );
1709 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001710 status = psa_crypto_stop_transaction( );
1711 if( status != PSA_SUCCESS )
1712 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713 }
1714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1715
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 return( status );
1717}
1718
1719/** Abort the creation of a key.
1720 *
1721 * You may call this function after calling psa_start_key_creation(),
1722 * or after psa_finish_key_creation() fails. In other circumstances, this
1723 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001724 * See the documentation of psa_start_key_creation() for the intended use
1725 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in,out] slot Pointer to the slot with key material.
1728 * \param[in] driver The secure element driver for the key,
1729 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001731static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001732 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001733{
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 (void) driver;
1735
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 if( slot == NULL )
1737 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001738
1739#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001740 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * element, and the failure happened later (when saving metadata
1742 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001743 * element.
1744 * https://github.com/ARMmbed/mbed-crypto/issues/217
1745 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001746
Gilles Peskined7729582019-08-05 15:55:54 +02001747 /* Abort the ongoing transaction if any (there may not be one if
1748 * the creation process failed before starting one, or if the
1749 * key creation is a registration of a key in a secure element).
1750 * Earlier functions must already have done what it takes to undo any
1751 * partial creation. All that's left is to update the transaction data
1752 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001753 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1755
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 psa_wipe_key_slot( slot );
1757}
1758
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001759/** Validate optional attributes during key creation.
1760 *
1761 * Some key attributes are optional during key creation. If they are
1762 * specified in the attributes structure, check that they are consistent
1763 * with the data in the slot.
1764 *
1765 * This function should be called near the end of key creation, after
1766 * the slot in memory is fully populated but before saving persistent data.
1767 */
1768static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001769 const psa_key_slot_t *slot,
1770 const psa_key_attributes_t *attributes )
1771{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001772 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001774 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001775 return( PSA_ERROR_INVALID_ARGUMENT );
1776 }
1777
1778 if( attributes->domain_parameters_size != 0 )
1779 {
1780#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001782 {
1783 mbedtls_mpi actual, required;
1784 int ret;
1785 mbedtls_mpi_init( &actual );
1786 mbedtls_mpi_init( &required );
1787 ret = mbedtls_rsa_export( slot->data.rsa,
1788 NULL, NULL, NULL, NULL, &actual );
1789 if( ret != 0 )
1790 goto rsa_exit;
1791 ret = mbedtls_mpi_read_binary( &required,
1792 attributes->domain_parameters,
1793 attributes->domain_parameters_size );
1794 if( ret != 0 )
1795 goto rsa_exit;
1796 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1797 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1798 rsa_exit:
1799 mbedtls_mpi_free( &actual );
1800 mbedtls_mpi_free( &required );
1801 if( ret != 0)
1802 return( mbedtls_to_psa_error( ret ) );
1803 }
1804 else
1805#endif
1806 {
1807 return( PSA_ERROR_INVALID_ARGUMENT );
1808 }
1809 }
1810
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001811 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001813 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001814 return( PSA_ERROR_INVALID_ARGUMENT );
1815 }
1816
1817 return( PSA_SUCCESS );
1818}
1819
Gilles Peskine4747d192019-04-17 15:05:45 +02001820psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001822 size_t data_length,
1823 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001824{
1825 psa_status_t status;
1826 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001827 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828
Gilles Peskinedf179142019-07-15 22:02:14 +02001829 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1830 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 if( status != PSA_SUCCESS )
1832 goto exit;
1833
Gilles Peskine5d309672019-07-12 23:47:28 +02001834#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1835 if( driver != NULL )
1836 {
1837 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001838 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001839 if( drv->key_management == NULL ||
1840 drv->key_management->p_import == NULL )
1841 {
1842 status = PSA_ERROR_NOT_SUPPORTED;
1843 goto exit;
1844 }
1845 status = drv->key_management->p_import(
1846 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001847 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001848 &bits );
1849 if( status != PSA_SUCCESS )
1850 goto exit;
1851 if( bits > PSA_MAX_KEY_BITS )
1852 {
1853 status = PSA_ERROR_NOT_SUPPORTED;
1854 goto exit;
1855 }
1856 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001857 }
1858 else
1859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860 {
1861 status = psa_import_key_into_slot( slot, data, data_length );
1862 if( status != PSA_SUCCESS )
1863 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001864 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001865 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001866 if( status != PSA_SUCCESS )
1867 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 if( status != PSA_SUCCESS )
1872 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001873 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 *handle = 0;
1875 }
1876 return( status );
1877}
1878
Gilles Peskined7729582019-08-05 15:55:54 +02001879#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1880psa_status_t mbedtls_psa_register_se_key(
1881 const psa_key_attributes_t *attributes )
1882{
1883 psa_status_t status;
1884 psa_key_slot_t *slot = NULL;
1885 psa_se_drv_table_entry_t *driver = NULL;
1886 const psa_drv_se_t *drv;
1887 psa_key_handle_t handle = 0;
1888
1889 /* Leaving attributes unspecified is not currently supported.
1890 * It could make sense to query the key type and size from the
1891 * secure element, but not all secure elements support this
1892 * and the driver HAL doesn't currently support it. */
1893 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1894 return( PSA_ERROR_NOT_SUPPORTED );
1895 if( psa_get_key_bits( attributes ) == 0 )
1896 return( PSA_ERROR_NOT_SUPPORTED );
1897
1898 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1899 &handle, &slot, &driver );
1900 if( status != PSA_SUCCESS )
1901 goto exit;
1902
1903 if( driver == NULL )
1904 {
1905 status = PSA_ERROR_INVALID_ARGUMENT;
1906 goto exit;
1907 }
1908 drv = psa_get_se_driver_methods( driver );
1909
1910 if ( psa_get_key_slot_number( attributes,
1911 &slot->data.se.slot_number ) != PSA_SUCCESS )
1912 {
1913 /* The application didn't specify a slot number. This doesn't
1914 * make sense when registering a slot. */
1915 status = PSA_ERROR_INVALID_ARGUMENT;
1916 goto exit;
1917 }
1918
Gilles Peskinea5f87492019-08-05 16:46:18 +02001919 /* If the driver has a slot number validation method, call it.
1920 * If it doesn't, it means the secure element is unable to validate
1921 * anything and so we have to trust the application. */
1922 if( drv->key_management != NULL &&
1923 drv->key_management->p_validate_slot_number != NULL )
1924 {
1925 status = drv->key_management->p_validate_slot_number(
1926 psa_get_se_driver_context( driver ),
1927 attributes,
1928 PSA_KEY_CREATION_REGISTER,
1929 slot->data.se.slot_number );
1930 if( status != PSA_SUCCESS )
1931 goto exit;
1932 }
1933
Gilles Peskined7729582019-08-05 15:55:54 +02001934 status = psa_finish_key_creation( slot, driver );
1935
1936exit:
1937 if( status != PSA_SUCCESS )
1938 {
1939 psa_fail_key_creation( slot, driver );
1940 }
1941 /* Registration doesn't keep the key in RAM. */
1942 psa_close_key( handle );
1943 return( status );
1944}
1945#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1946
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001947static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001948 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001949{
1950 psa_status_t status;
1951 uint8_t *buffer = NULL;
1952 size_t buffer_size = 0;
1953 size_t length;
1954
Gilles Peskine8e338702019-07-30 20:06:31 +02001955 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001956 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001957 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001958 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001959 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001960 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1961 if( status != PSA_SUCCESS )
1962 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001963 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001964 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001965
1966exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001967 if( buffer_size != 0 )
1968 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001969 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001970 return( status );
1971}
1972
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1974 const psa_key_attributes_t *specified_attributes,
1975 psa_key_handle_t *target_handle )
1976{
1977 psa_status_t status;
1978 psa_key_slot_t *source_slot = NULL;
1979 psa_key_slot_t *target_slot = NULL;
1980 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001981 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982
Gilles Peskine28f8f302019-07-24 13:30:31 +02001983 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001984 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 if( status != PSA_SUCCESS )
1986 goto exit;
1987
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001988 status = psa_validate_optional_attributes( source_slot,
1989 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001993 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001994 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
Gilles Peskinedf179142019-07-15 22:02:14 +02001998 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1999 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002000 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 if( status != PSA_SUCCESS )
2002 goto exit;
2003
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002004#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2005 if( driver != NULL )
2006 {
2007 /* Copying to a secure element is not implemented yet. */
2008 status = PSA_ERROR_NOT_SUPPORTED;
2009 goto exit;
2010 }
2011#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2012
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 if( status != PSA_SUCCESS )
2015 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016
Gilles Peskine011e4282019-06-26 18:34:38 +02002017 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002019 if( status != PSA_SUCCESS )
2020 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002021 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002022 *target_handle = 0;
2023 }
2024 return( status );
2025}
2026
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002027
2028
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002029/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002030/* Message digests */
2031/****************************************************************/
2032
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002033static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002034{
2035 switch( alg )
2036 {
2037#if defined(MBEDTLS_MD2_C)
2038 case PSA_ALG_MD2:
2039 return( &mbedtls_md2_info );
2040#endif
2041#if defined(MBEDTLS_MD4_C)
2042 case PSA_ALG_MD4:
2043 return( &mbedtls_md4_info );
2044#endif
2045#if defined(MBEDTLS_MD5_C)
2046 case PSA_ALG_MD5:
2047 return( &mbedtls_md5_info );
2048#endif
2049#if defined(MBEDTLS_RIPEMD160_C)
2050 case PSA_ALG_RIPEMD160:
2051 return( &mbedtls_ripemd160_info );
2052#endif
2053#if defined(MBEDTLS_SHA1_C)
2054 case PSA_ALG_SHA_1:
2055 return( &mbedtls_sha1_info );
2056#endif
2057#if defined(MBEDTLS_SHA256_C)
2058 case PSA_ALG_SHA_224:
2059 return( &mbedtls_sha224_info );
2060 case PSA_ALG_SHA_256:
2061 return( &mbedtls_sha256_info );
2062#endif
2063#if defined(MBEDTLS_SHA512_C)
2064 case PSA_ALG_SHA_384:
2065 return( &mbedtls_sha384_info );
2066 case PSA_ALG_SHA_512:
2067 return( &mbedtls_sha512_info );
2068#endif
2069 default:
2070 return( NULL );
2071 }
2072}
2073
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002074psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2075{
2076 switch( operation->alg )
2077 {
Gilles Peskine81736312018-06-26 15:04:31 +02002078 case 0:
2079 /* The object has (apparently) been initialized but it is not
2080 * in use. It's ok to call abort on such an object, and there's
2081 * nothing to do. */
2082 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002083#if defined(MBEDTLS_MD2_C)
2084 case PSA_ALG_MD2:
2085 mbedtls_md2_free( &operation->ctx.md2 );
2086 break;
2087#endif
2088#if defined(MBEDTLS_MD4_C)
2089 case PSA_ALG_MD4:
2090 mbedtls_md4_free( &operation->ctx.md4 );
2091 break;
2092#endif
2093#if defined(MBEDTLS_MD5_C)
2094 case PSA_ALG_MD5:
2095 mbedtls_md5_free( &operation->ctx.md5 );
2096 break;
2097#endif
2098#if defined(MBEDTLS_RIPEMD160_C)
2099 case PSA_ALG_RIPEMD160:
2100 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2101 break;
2102#endif
2103#if defined(MBEDTLS_SHA1_C)
2104 case PSA_ALG_SHA_1:
2105 mbedtls_sha1_free( &operation->ctx.sha1 );
2106 break;
2107#endif
2108#if defined(MBEDTLS_SHA256_C)
2109 case PSA_ALG_SHA_224:
2110 case PSA_ALG_SHA_256:
2111 mbedtls_sha256_free( &operation->ctx.sha256 );
2112 break;
2113#endif
2114#if defined(MBEDTLS_SHA512_C)
2115 case PSA_ALG_SHA_384:
2116 case PSA_ALG_SHA_512:
2117 mbedtls_sha512_free( &operation->ctx.sha512 );
2118 break;
2119#endif
2120 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002121 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 }
2123 operation->alg = 0;
2124 return( PSA_SUCCESS );
2125}
2126
Gilles Peskineda8191d2018-07-08 19:46:38 +02002127psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128 psa_algorithm_t alg )
2129{
2130 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002131
2132 /* A context must be freshly initialized before it can be set up. */
2133 if( operation->alg != 0 )
2134 {
2135 return( PSA_ERROR_BAD_STATE );
2136 }
2137
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002138 switch( alg )
2139 {
2140#if defined(MBEDTLS_MD2_C)
2141 case PSA_ALG_MD2:
2142 mbedtls_md2_init( &operation->ctx.md2 );
2143 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2144 break;
2145#endif
2146#if defined(MBEDTLS_MD4_C)
2147 case PSA_ALG_MD4:
2148 mbedtls_md4_init( &operation->ctx.md4 );
2149 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2150 break;
2151#endif
2152#if defined(MBEDTLS_MD5_C)
2153 case PSA_ALG_MD5:
2154 mbedtls_md5_init( &operation->ctx.md5 );
2155 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2156 break;
2157#endif
2158#if defined(MBEDTLS_RIPEMD160_C)
2159 case PSA_ALG_RIPEMD160:
2160 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2161 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_SHA1_C)
2165 case PSA_ALG_SHA_1:
2166 mbedtls_sha1_init( &operation->ctx.sha1 );
2167 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_SHA256_C)
2171 case PSA_ALG_SHA_224:
2172 mbedtls_sha256_init( &operation->ctx.sha256 );
2173 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2174 break;
2175 case PSA_ALG_SHA_256:
2176 mbedtls_sha256_init( &operation->ctx.sha256 );
2177 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2178 break;
2179#endif
2180#if defined(MBEDTLS_SHA512_C)
2181 case PSA_ALG_SHA_384:
2182 mbedtls_sha512_init( &operation->ctx.sha512 );
2183 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2184 break;
2185 case PSA_ALG_SHA_512:
2186 mbedtls_sha512_init( &operation->ctx.sha512 );
2187 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2188 break;
2189#endif
2190 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002191 return( PSA_ALG_IS_HASH( alg ) ?
2192 PSA_ERROR_NOT_SUPPORTED :
2193 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 }
2195 if( ret == 0 )
2196 operation->alg = alg;
2197 else
2198 psa_hash_abort( operation );
2199 return( mbedtls_to_psa_error( ret ) );
2200}
2201
2202psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2203 const uint8_t *input,
2204 size_t input_length )
2205{
2206 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002207
2208 /* Don't require hash implementations to behave correctly on a
2209 * zero-length input, which may have an invalid pointer. */
2210 if( input_length == 0 )
2211 return( PSA_SUCCESS );
2212
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213 switch( operation->alg )
2214 {
2215#if defined(MBEDTLS_MD2_C)
2216 case PSA_ALG_MD2:
2217 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2218 input, input_length );
2219 break;
2220#endif
2221#if defined(MBEDTLS_MD4_C)
2222 case PSA_ALG_MD4:
2223 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2224 input, input_length );
2225 break;
2226#endif
2227#if defined(MBEDTLS_MD5_C)
2228 case PSA_ALG_MD5:
2229 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2230 input, input_length );
2231 break;
2232#endif
2233#if defined(MBEDTLS_RIPEMD160_C)
2234 case PSA_ALG_RIPEMD160:
2235 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2236 input, input_length );
2237 break;
2238#endif
2239#if defined(MBEDTLS_SHA1_C)
2240 case PSA_ALG_SHA_1:
2241 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2242 input, input_length );
2243 break;
2244#endif
2245#if defined(MBEDTLS_SHA256_C)
2246 case PSA_ALG_SHA_224:
2247 case PSA_ALG_SHA_256:
2248 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2249 input, input_length );
2250 break;
2251#endif
2252#if defined(MBEDTLS_SHA512_C)
2253 case PSA_ALG_SHA_384:
2254 case PSA_ALG_SHA_512:
2255 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2256 input, input_length );
2257 break;
2258#endif
2259 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002260 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002262
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 if( ret != 0 )
2264 psa_hash_abort( operation );
2265 return( mbedtls_to_psa_error( ret ) );
2266}
2267
2268psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2269 uint8_t *hash,
2270 size_t hash_size,
2271 size_t *hash_length )
2272{
itayzafrir40835d42018-08-02 13:14:17 +03002273 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002275 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276
2277 /* Fill the output buffer with something that isn't a valid hash
2278 * (barring an attack on the hash and deliberately-crafted input),
2279 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002280 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002281 /* If hash_size is 0 then hash may be NULL and then the
2282 * call to memset would have undefined behavior. */
2283 if( hash_size != 0 )
2284 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285
2286 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002287 {
2288 status = PSA_ERROR_BUFFER_TOO_SMALL;
2289 goto exit;
2290 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291
2292 switch( operation->alg )
2293 {
2294#if defined(MBEDTLS_MD2_C)
2295 case PSA_ALG_MD2:
2296 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2297 break;
2298#endif
2299#if defined(MBEDTLS_MD4_C)
2300 case PSA_ALG_MD4:
2301 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2302 break;
2303#endif
2304#if defined(MBEDTLS_MD5_C)
2305 case PSA_ALG_MD5:
2306 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2307 break;
2308#endif
2309#if defined(MBEDTLS_RIPEMD160_C)
2310 case PSA_ALG_RIPEMD160:
2311 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2312 break;
2313#endif
2314#if defined(MBEDTLS_SHA1_C)
2315 case PSA_ALG_SHA_1:
2316 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2317 break;
2318#endif
2319#if defined(MBEDTLS_SHA256_C)
2320 case PSA_ALG_SHA_224:
2321 case PSA_ALG_SHA_256:
2322 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2323 break;
2324#endif
2325#if defined(MBEDTLS_SHA512_C)
2326 case PSA_ALG_SHA_384:
2327 case PSA_ALG_SHA_512:
2328 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2329 break;
2330#endif
2331 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002332 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 }
itayzafrir40835d42018-08-02 13:14:17 +03002334 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335
itayzafrir40835d42018-08-02 13:14:17 +03002336exit:
2337 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002339 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 return( psa_hash_abort( operation ) );
2341 }
2342 else
2343 {
2344 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002345 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 }
2347}
2348
Gilles Peskine2d277862018-06-18 15:41:12 +02002349psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2350 const uint8_t *hash,
2351 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352{
2353 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2354 size_t actual_hash_length;
2355 psa_status_t status = psa_hash_finish( operation,
2356 actual_hash, sizeof( actual_hash ),
2357 &actual_hash_length );
2358 if( status != PSA_SUCCESS )
2359 return( status );
2360 if( actual_hash_length != hash_length )
2361 return( PSA_ERROR_INVALID_SIGNATURE );
2362 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2363 return( PSA_ERROR_INVALID_SIGNATURE );
2364 return( PSA_SUCCESS );
2365}
2366
Gilles Peskineeb35d782019-01-22 17:56:16 +01002367psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2368 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002369{
2370 if( target_operation->alg != 0 )
2371 return( PSA_ERROR_BAD_STATE );
2372
2373 switch( source_operation->alg )
2374 {
2375 case 0:
2376 return( PSA_ERROR_BAD_STATE );
2377#if defined(MBEDTLS_MD2_C)
2378 case PSA_ALG_MD2:
2379 mbedtls_md2_clone( &target_operation->ctx.md2,
2380 &source_operation->ctx.md2 );
2381 break;
2382#endif
2383#if defined(MBEDTLS_MD4_C)
2384 case PSA_ALG_MD4:
2385 mbedtls_md4_clone( &target_operation->ctx.md4,
2386 &source_operation->ctx.md4 );
2387 break;
2388#endif
2389#if defined(MBEDTLS_MD5_C)
2390 case PSA_ALG_MD5:
2391 mbedtls_md5_clone( &target_operation->ctx.md5,
2392 &source_operation->ctx.md5 );
2393 break;
2394#endif
2395#if defined(MBEDTLS_RIPEMD160_C)
2396 case PSA_ALG_RIPEMD160:
2397 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2398 &source_operation->ctx.ripemd160 );
2399 break;
2400#endif
2401#if defined(MBEDTLS_SHA1_C)
2402 case PSA_ALG_SHA_1:
2403 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2404 &source_operation->ctx.sha1 );
2405 break;
2406#endif
2407#if defined(MBEDTLS_SHA256_C)
2408 case PSA_ALG_SHA_224:
2409 case PSA_ALG_SHA_256:
2410 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2411 &source_operation->ctx.sha256 );
2412 break;
2413#endif
2414#if defined(MBEDTLS_SHA512_C)
2415 case PSA_ALG_SHA_384:
2416 case PSA_ALG_SHA_512:
2417 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2418 &source_operation->ctx.sha512 );
2419 break;
2420#endif
2421 default:
2422 return( PSA_ERROR_NOT_SUPPORTED );
2423 }
2424
2425 target_operation->alg = source_operation->alg;
2426 return( PSA_SUCCESS );
2427}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428
2429
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002430/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431/* MAC */
2432/****************************************************************/
2433
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002434static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 psa_algorithm_t alg,
2436 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002437 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002438 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002441 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002442
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002443 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002444 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002445
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2447 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002448 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002450 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002451 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452 mode = MBEDTLS_MODE_STREAM;
2453 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 case PSA_ALG_CTR:
2455 mode = MBEDTLS_MODE_CTR;
2456 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002457 case PSA_ALG_CFB:
2458 mode = MBEDTLS_MODE_CFB;
2459 break;
2460 case PSA_ALG_OFB:
2461 mode = MBEDTLS_MODE_OFB;
2462 break;
2463 case PSA_ALG_CBC_NO_PADDING:
2464 mode = MBEDTLS_MODE_CBC;
2465 break;
2466 case PSA_ALG_CBC_PKCS7:
2467 mode = MBEDTLS_MODE_CBC;
2468 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002469 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470 mode = MBEDTLS_MODE_CCM;
2471 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002472 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473 mode = MBEDTLS_MODE_GCM;
2474 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002475 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2476 mode = MBEDTLS_MODE_CHACHAPOLY;
2477 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478 default:
2479 return( NULL );
2480 }
2481 }
2482 else if( alg == PSA_ALG_CMAC )
2483 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484 else
2485 return( NULL );
2486
2487 switch( key_type )
2488 {
2489 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002490 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 break;
2492 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002493 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2494 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002496 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002498 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002499 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2500 * but two-key Triple-DES is functionally three-key Triple-DES
2501 * with K1=K3, so that's how we present it to mbedtls. */
2502 if( key_bits == 128 )
2503 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 break;
2505 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002506 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 break;
2508 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002509 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002511 case PSA_KEY_TYPE_CHACHA20:
2512 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2513 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 default:
2515 return( NULL );
2516 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002517 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002518 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519
Jaeden Amero23bbb752018-06-26 14:16:54 +01002520 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2521 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522}
2523
Gilles Peskinea05219c2018-11-16 16:02:56 +01002524#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002525static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002526{
Gilles Peskine2d277862018-06-18 15:41:12 +02002527 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002528 {
2529 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002530 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002531 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002532 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002533 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002534 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002535 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002536 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002537 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002538 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002539 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002540 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002541 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002542 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002543 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002544 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002545 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002546 return( 128 );
2547 default:
2548 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002549 }
2550}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002551#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002552
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553/* Initialize the MAC operation structure. Once this function has been
2554 * called, psa_mac_abort can run and will do the right thing. */
2555static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2556 psa_algorithm_t alg )
2557{
2558 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2559
2560 operation->alg = alg;
2561 operation->key_set = 0;
2562 operation->iv_set = 0;
2563 operation->iv_required = 0;
2564 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002565 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566
2567#if defined(MBEDTLS_CMAC_C)
2568 if( alg == PSA_ALG_CMAC )
2569 {
2570 operation->iv_required = 0;
2571 mbedtls_cipher_init( &operation->ctx.cmac );
2572 status = PSA_SUCCESS;
2573 }
2574 else
2575#endif /* MBEDTLS_CMAC_C */
2576#if defined(MBEDTLS_MD_C)
2577 if( PSA_ALG_IS_HMAC( operation->alg ) )
2578 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002579 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2580 operation->ctx.hmac.hash_ctx.alg = 0;
2581 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002582 }
2583 else
2584#endif /* MBEDTLS_MD_C */
2585 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002586 if( ! PSA_ALG_IS_MAC( alg ) )
2587 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588 }
2589
2590 if( status != PSA_SUCCESS )
2591 memset( operation, 0, sizeof( *operation ) );
2592 return( status );
2593}
2594
Gilles Peskine01126fa2018-07-12 17:04:55 +02002595#if defined(MBEDTLS_MD_C)
2596static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2597{
Gilles Peskine3f108122018-12-07 18:14:53 +01002598 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002599 return( psa_hash_abort( &hmac->hash_ctx ) );
2600}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002601
Janos Follath999f6482019-06-11 12:04:10 +01002602#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002603static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2604{
2605 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2606 memset( hmac, 0, sizeof( *hmac ) );
2607}
Janos Follath999f6482019-06-11 12:04:10 +01002608#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002609#endif /* MBEDTLS_MD_C */
2610
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2612{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002615 /* The object has (apparently) been initialized but it is not
2616 * in use. It's ok to call abort on such an object, and there's
2617 * nothing to do. */
2618 return( PSA_SUCCESS );
2619 }
2620 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002622 if( operation->alg == PSA_ALG_CMAC )
2623 {
2624 mbedtls_cipher_free( &operation->ctx.cmac );
2625 }
2626 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002628#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002629 if( PSA_ALG_IS_HMAC( operation->alg ) )
2630 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002631 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002632 }
2633 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002635 {
2636 /* Sanity check (shouldn't happen: operation->alg should
2637 * always have been initialized to a valid value). */
2638 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639 }
Moran Peker41deec42018-04-04 15:43:05 +03002640
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641 operation->alg = 0;
2642 operation->key_set = 0;
2643 operation->iv_set = 0;
2644 operation->iv_required = 0;
2645 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002647
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002649
2650bad_state:
2651 /* If abort is called on an uninitialized object, we can't trust
2652 * anything. Wipe the object in case it contains confidential data.
2653 * This may result in a memory leak if a pointer gets overwritten,
2654 * but it's too late to do anything about this. */
2655 memset( operation, 0, sizeof( *operation ) );
2656 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657}
2658
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002659#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002660static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002661 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002662 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002663 const mbedtls_cipher_info_t *cipher_info )
2664{
2665 int ret;
2666
2667 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002668
2669 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2670 if( ret != 0 )
2671 return( ret );
2672
2673 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2674 slot->data.raw.data,
2675 key_bits );
2676 return( ret );
2677}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002678#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002679
Gilles Peskine248051a2018-06-20 16:09:38 +02002680#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002681static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2682 const uint8_t *key,
2683 size_t key_length,
2684 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002685{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002686 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002687 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002688 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002689 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002690 psa_status_t status;
2691
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002692 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2693 * overflow below. This should never trigger if the hash algorithm
2694 * is implemented correctly. */
2695 /* The size checks against the ipad and opad buffers cannot be written
2696 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2697 * because that triggers -Wlogical-op on GCC 7.3. */
2698 if( block_size > sizeof( ipad ) )
2699 return( PSA_ERROR_NOT_SUPPORTED );
2700 if( block_size > sizeof( hmac->opad ) )
2701 return( PSA_ERROR_NOT_SUPPORTED );
2702 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002703 return( PSA_ERROR_NOT_SUPPORTED );
2704
Gilles Peskined223b522018-06-11 18:12:58 +02002705 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002707 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002709 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002710 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002712 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002713 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002714 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002716 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717 }
Gilles Peskine96889972018-07-12 17:07:03 +02002718 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2719 * but it is permitted. It is common when HMAC is used in HKDF, for
2720 * example. Don't call `memcpy` in the 0-length because `key` could be
2721 * an invalid pointer which would make the behavior undefined. */
2722 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002723 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002724
Gilles Peskined223b522018-06-11 18:12:58 +02002725 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2726 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002728 ipad[i] ^= 0x36;
2729 memset( ipad + key_length, 0x36, block_size - key_length );
2730
2731 /* Copy the key material from ipad to opad, flipping the requisite bits,
2732 * and filling the rest of opad with the requisite constant. */
2733 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002734 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2735 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002736
Gilles Peskine01126fa2018-07-12 17:04:55 +02002737 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002738 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002739 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740
Gilles Peskine01126fa2018-07-12 17:04:55 +02002741 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002742
2743cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002744 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002745
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746 return( status );
2747}
Gilles Peskine248051a2018-06-20 16:09:38 +02002748#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002749
Gilles Peskine89167cb2018-07-08 20:12:23 +02002750static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002751 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002752 psa_algorithm_t alg,
2753 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002756 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002758 psa_key_usage_t usage =
2759 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002760 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002761 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002763 /* A context must be freshly initialized before it can be set up. */
2764 if( operation->alg != 0 )
2765 {
2766 return( PSA_ERROR_BAD_STATE );
2767 }
2768
Gilles Peskined911eb72018-08-14 15:18:45 +02002769 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002770 if( status != PSA_SUCCESS )
2771 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002772 if( is_sign )
2773 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774
Gilles Peskine28f8f302019-07-24 13:30:31 +02002775 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002776 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002777 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002778 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002779
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002781 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002782 {
2783 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002784 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002785 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002786 int ret;
2787 if( cipher_info == NULL )
2788 {
2789 status = PSA_ERROR_NOT_SUPPORTED;
2790 goto exit;
2791 }
2792 operation->mac_size = cipher_info->block_size;
2793 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2794 status = mbedtls_to_psa_error( ret );
2795 }
2796 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002799 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002800 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002801 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002802 if( hash_alg == 0 )
2803 {
2804 status = PSA_ERROR_NOT_SUPPORTED;
2805 goto exit;
2806 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002807
2808 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2809 /* Sanity check. This shouldn't fail on a valid configuration. */
2810 if( operation->mac_size == 0 ||
2811 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2812 {
2813 status = PSA_ERROR_NOT_SUPPORTED;
2814 goto exit;
2815 }
2816
Gilles Peskine8e338702019-07-30 20:06:31 +02002817 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002818 {
2819 status = PSA_ERROR_INVALID_ARGUMENT;
2820 goto exit;
2821 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002822
Gilles Peskine01126fa2018-07-12 17:04:55 +02002823 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2824 slot->data.raw.data,
2825 slot->data.raw.bytes,
2826 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 }
2828 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002830 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002831 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002832 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834
Gilles Peskined911eb72018-08-14 15:18:45 +02002835 if( truncated == 0 )
2836 {
2837 /* The "normal" case: untruncated algorithm. Nothing to do. */
2838 }
2839 else if( truncated < 4 )
2840 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002841 /* A very short MAC is too short for security since it can be
2842 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2843 * so we make this our minimum, even though 32 bits is still
2844 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002845 status = PSA_ERROR_NOT_SUPPORTED;
2846 }
2847 else if( truncated > operation->mac_size )
2848 {
2849 /* It's impossible to "truncate" to a larger length. */
2850 status = PSA_ERROR_INVALID_ARGUMENT;
2851 }
2852 else
2853 operation->mac_size = truncated;
2854
Gilles Peskinefbfac682018-07-08 20:51:54 +02002855exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002856 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002858 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002860 else
2861 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002862 operation->key_set = 1;
2863 }
2864 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865}
2866
Gilles Peskine89167cb2018-07-08 20:12:23 +02002867psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002868 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002869 psa_algorithm_t alg )
2870{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002871 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002872}
2873
2874psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002875 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002876 psa_algorithm_t alg )
2877{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002878 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002879}
2880
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2882 const uint8_t *input,
2883 size_t input_length )
2884{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002885 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002887 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002889 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890 operation->has_input = 1;
2891
Gilles Peskine8c9def32018-02-08 10:02:12 +01002892#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002893 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002894 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2896 input, input_length );
2897 status = mbedtls_to_psa_error( ret );
2898 }
2899 else
2900#endif /* MBEDTLS_CMAC_C */
2901#if defined(MBEDTLS_MD_C)
2902 if( PSA_ALG_IS_HMAC( operation->alg ) )
2903 {
2904 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2905 input_length );
2906 }
2907 else
2908#endif /* MBEDTLS_MD_C */
2909 {
2910 /* This shouldn't happen if `operation` was initialized by
2911 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002912 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002913 }
2914
Gilles Peskinefbfac682018-07-08 20:51:54 +02002915 if( status != PSA_SUCCESS )
2916 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002917 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918}
2919
Gilles Peskine01126fa2018-07-12 17:04:55 +02002920#if defined(MBEDTLS_MD_C)
2921static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2922 uint8_t *mac,
2923 size_t mac_size )
2924{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002925 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2927 size_t hash_size = 0;
2928 size_t block_size = psa_get_hash_block_size( hash_alg );
2929 psa_status_t status;
2930
Gilles Peskine01126fa2018-07-12 17:04:55 +02002931 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2932 if( status != PSA_SUCCESS )
2933 return( status );
2934 /* From here on, tmp needs to be wiped. */
2935
2936 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2937 if( status != PSA_SUCCESS )
2938 goto exit;
2939
2940 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2941 if( status != PSA_SUCCESS )
2942 goto exit;
2943
2944 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2945 if( status != PSA_SUCCESS )
2946 goto exit;
2947
Gilles Peskined911eb72018-08-14 15:18:45 +02002948 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2949 if( status != PSA_SUCCESS )
2950 goto exit;
2951
2952 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002953
2954exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002955 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002956 return( status );
2957}
2958#endif /* MBEDTLS_MD_C */
2959
mohammad16036df908f2018-04-02 08:34:15 -07002960static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002961 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002962 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002964 if( ! operation->key_set )
2965 return( PSA_ERROR_BAD_STATE );
2966 if( operation->iv_required && ! operation->iv_set )
2967 return( PSA_ERROR_BAD_STATE );
2968
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969 if( mac_size < operation->mac_size )
2970 return( PSA_ERROR_BUFFER_TOO_SMALL );
2971
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002973 if( operation->alg == PSA_ALG_CMAC )
2974 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002975 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2976 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2977 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002978 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002979 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002980 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002982 else
2983#endif /* MBEDTLS_CMAC_C */
2984#if defined(MBEDTLS_MD_C)
2985 if( PSA_ALG_IS_HMAC( operation->alg ) )
2986 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002987 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002988 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002989 }
2990 else
2991#endif /* MBEDTLS_MD_C */
2992 {
2993 /* This shouldn't happen if `operation` was initialized by
2994 * a setup function. */
2995 return( PSA_ERROR_BAD_STATE );
2996 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997}
2998
Gilles Peskineacd4be32018-07-08 19:56:25 +02002999psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3000 uint8_t *mac,
3001 size_t mac_size,
3002 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003003{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003004 psa_status_t status;
3005
Jaeden Amero252ef282019-02-15 14:05:35 +00003006 if( operation->alg == 0 )
3007 {
3008 return( PSA_ERROR_BAD_STATE );
3009 }
3010
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003011 /* Fill the output buffer with something that isn't a valid mac
3012 * (barring an attack on the mac and deliberately-crafted input),
3013 * in case the caller doesn't check the return status properly. */
3014 *mac_length = mac_size;
3015 /* If mac_size is 0 then mac may be NULL and then the
3016 * call to memset would have undefined behavior. */
3017 if( mac_size != 0 )
3018 memset( mac, '!', mac_size );
3019
Gilles Peskine89167cb2018-07-08 20:12:23 +02003020 if( ! operation->is_sign )
3021 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003022 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003023 }
mohammad16036df908f2018-04-02 08:34:15 -07003024
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003025 status = psa_mac_finish_internal( operation, mac, mac_size );
3026
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003027 if( status == PSA_SUCCESS )
3028 {
3029 status = psa_mac_abort( operation );
3030 if( status == PSA_SUCCESS )
3031 *mac_length = operation->mac_size;
3032 else
3033 memset( mac, '!', mac_size );
3034 }
3035 else
3036 psa_mac_abort( operation );
3037 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003038}
3039
Gilles Peskineacd4be32018-07-08 19:56:25 +02003040psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3041 const uint8_t *mac,
3042 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043{
Gilles Peskine828ed142018-06-18 23:25:51 +02003044 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003045 psa_status_t status;
3046
Jaeden Amero252ef282019-02-15 14:05:35 +00003047 if( operation->alg == 0 )
3048 {
3049 return( PSA_ERROR_BAD_STATE );
3050 }
3051
Gilles Peskine89167cb2018-07-08 20:12:23 +02003052 if( operation->is_sign )
3053 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003054 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003055 }
3056 if( operation->mac_size != mac_length )
3057 {
3058 status = PSA_ERROR_INVALID_SIGNATURE;
3059 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003060 }
mohammad16036df908f2018-04-02 08:34:15 -07003061
3062 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003063 actual_mac, sizeof( actual_mac ) );
3064
3065 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3066 status = PSA_ERROR_INVALID_SIGNATURE;
3067
3068cleanup:
3069 if( status == PSA_SUCCESS )
3070 status = psa_mac_abort( operation );
3071 else
3072 psa_mac_abort( operation );
3073
Gilles Peskine3f108122018-12-07 18:14:53 +01003074 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003075
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003076 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003077}
3078
3079
Gilles Peskine20035e32018-02-03 22:44:14 +01003080
Gilles Peskine20035e32018-02-03 22:44:14 +01003081/****************************************************************/
3082/* Asymmetric cryptography */
3083/****************************************************************/
3084
Gilles Peskine2b450e32018-06-27 15:42:46 +02003085#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003086/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003087 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003088static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3089 size_t hash_length,
3090 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003091{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003092 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003093 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003094 *md_alg = mbedtls_md_get_type( md_info );
3095
3096 /* The Mbed TLS RSA module uses an unsigned int for hash length
3097 * parameters. Validate that it fits so that we don't risk an
3098 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003099#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003100 if( hash_length > UINT_MAX )
3101 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003102#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003103
3104#if defined(MBEDTLS_PKCS1_V15)
3105 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3106 * must be correct. */
3107 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3108 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003109 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003110 if( md_info == NULL )
3111 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003112 if( mbedtls_md_get_size( md_info ) != hash_length )
3113 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003114 }
3115#endif /* MBEDTLS_PKCS1_V15 */
3116
3117#if defined(MBEDTLS_PKCS1_V21)
3118 /* PSS requires a hash internally. */
3119 if( PSA_ALG_IS_RSA_PSS( alg ) )
3120 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003121 if( md_info == NULL )
3122 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003123 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003124#endif /* MBEDTLS_PKCS1_V21 */
3125
Gilles Peskine61b91d42018-06-08 16:09:36 +02003126 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003127}
3128
Gilles Peskine2b450e32018-06-27 15:42:46 +02003129static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3130 psa_algorithm_t alg,
3131 const uint8_t *hash,
3132 size_t hash_length,
3133 uint8_t *signature,
3134 size_t signature_size,
3135 size_t *signature_length )
3136{
3137 psa_status_t status;
3138 int ret;
3139 mbedtls_md_type_t md_alg;
3140
3141 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3142 if( status != PSA_SUCCESS )
3143 return( status );
3144
Gilles Peskine630a18a2018-06-29 17:49:35 +02003145 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003146 return( PSA_ERROR_BUFFER_TOO_SMALL );
3147
3148#if defined(MBEDTLS_PKCS1_V15)
3149 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3150 {
3151 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3152 MBEDTLS_MD_NONE );
3153 ret = mbedtls_rsa_pkcs1_sign( rsa,
3154 mbedtls_ctr_drbg_random,
3155 &global_data.ctr_drbg,
3156 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003157 md_alg,
3158 (unsigned int) hash_length,
3159 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003160 signature );
3161 }
3162 else
3163#endif /* MBEDTLS_PKCS1_V15 */
3164#if defined(MBEDTLS_PKCS1_V21)
3165 if( PSA_ALG_IS_RSA_PSS( alg ) )
3166 {
3167 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3168 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3169 mbedtls_ctr_drbg_random,
3170 &global_data.ctr_drbg,
3171 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003172 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003173 (unsigned int) hash_length,
3174 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003175 signature );
3176 }
3177 else
3178#endif /* MBEDTLS_PKCS1_V21 */
3179 {
3180 return( PSA_ERROR_INVALID_ARGUMENT );
3181 }
3182
3183 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003184 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003185 return( mbedtls_to_psa_error( ret ) );
3186}
3187
3188static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3189 psa_algorithm_t alg,
3190 const uint8_t *hash,
3191 size_t hash_length,
3192 const uint8_t *signature,
3193 size_t signature_length )
3194{
3195 psa_status_t status;
3196 int ret;
3197 mbedtls_md_type_t md_alg;
3198
3199 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3200 if( status != PSA_SUCCESS )
3201 return( status );
3202
Gilles Peskine630a18a2018-06-29 17:49:35 +02003203 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003204 return( PSA_ERROR_BUFFER_TOO_SMALL );
3205
3206#if defined(MBEDTLS_PKCS1_V15)
3207 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3208 {
3209 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3210 MBEDTLS_MD_NONE );
3211 ret = mbedtls_rsa_pkcs1_verify( rsa,
3212 mbedtls_ctr_drbg_random,
3213 &global_data.ctr_drbg,
3214 MBEDTLS_RSA_PUBLIC,
3215 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003216 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003217 hash,
3218 signature );
3219 }
3220 else
3221#endif /* MBEDTLS_PKCS1_V15 */
3222#if defined(MBEDTLS_PKCS1_V21)
3223 if( PSA_ALG_IS_RSA_PSS( alg ) )
3224 {
3225 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3226 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3227 mbedtls_ctr_drbg_random,
3228 &global_data.ctr_drbg,
3229 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003230 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003231 (unsigned int) hash_length,
3232 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003233 signature );
3234 }
3235 else
3236#endif /* MBEDTLS_PKCS1_V21 */
3237 {
3238 return( PSA_ERROR_INVALID_ARGUMENT );
3239 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003240
3241 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3242 * the rest of the signature is invalid". This has little use in
3243 * practice and PSA doesn't report this distinction. */
3244 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3245 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003246 return( mbedtls_to_psa_error( ret ) );
3247}
3248#endif /* MBEDTLS_RSA_C */
3249
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003250#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003251/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3252 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3253 * (even though these functions don't modify it). */
3254static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3255 psa_algorithm_t alg,
3256 const uint8_t *hash,
3257 size_t hash_length,
3258 uint8_t *signature,
3259 size_t signature_size,
3260 size_t *signature_length )
3261{
3262 int ret;
3263 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003264 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003265 mbedtls_mpi_init( &r );
3266 mbedtls_mpi_init( &s );
3267
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003268 if( signature_size < 2 * curve_bytes )
3269 {
3270 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3271 goto cleanup;
3272 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003273
Gilles Peskinea05219c2018-11-16 16:02:56 +01003274#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003275 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3276 {
3277 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3278 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3279 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3280 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3281 hash, hash_length,
3282 md_alg ) );
3283 }
3284 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003285#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003286 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003287 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003288 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3289 hash, hash_length,
3290 mbedtls_ctr_drbg_random,
3291 &global_data.ctr_drbg ) );
3292 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003293
3294 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3295 signature,
3296 curve_bytes ) );
3297 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3298 signature + curve_bytes,
3299 curve_bytes ) );
3300
3301cleanup:
3302 mbedtls_mpi_free( &r );
3303 mbedtls_mpi_free( &s );
3304 if( ret == 0 )
3305 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003306 return( mbedtls_to_psa_error( ret ) );
3307}
3308
3309static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3310 const uint8_t *hash,
3311 size_t hash_length,
3312 const uint8_t *signature,
3313 size_t signature_length )
3314{
3315 int ret;
3316 mbedtls_mpi r, s;
3317 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3318 mbedtls_mpi_init( &r );
3319 mbedtls_mpi_init( &s );
3320
3321 if( signature_length != 2 * curve_bytes )
3322 return( PSA_ERROR_INVALID_SIGNATURE );
3323
3324 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3325 signature,
3326 curve_bytes ) );
3327 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3328 signature + curve_bytes,
3329 curve_bytes ) );
3330
3331 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3332 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003333
3334cleanup:
3335 mbedtls_mpi_free( &r );
3336 mbedtls_mpi_free( &s );
3337 return( mbedtls_to_psa_error( ret ) );
3338}
3339#endif /* MBEDTLS_ECDSA_C */
3340
Gilles Peskinec5487a82018-12-03 18:08:14 +01003341psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003342 psa_algorithm_t alg,
3343 const uint8_t *hash,
3344 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003345 uint8_t *signature,
3346 size_t signature_size,
3347 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003348{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003349 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003350 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3352 const psa_drv_se_t *drv;
3353 psa_drv_se_context_t *drv_context;
3354#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003355
3356 *signature_length = signature_size;
3357
Gilles Peskineedc64242019-08-07 21:05:07 +02003358 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003359 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003360 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003361 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003362 {
3363 status = PSA_ERROR_INVALID_ARGUMENT;
3364 goto exit;
3365 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003366
Gilles Peskineedc64242019-08-07 21:05:07 +02003367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3368 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3369 {
3370 if( drv->asymmetric == NULL ||
3371 drv->asymmetric->p_sign == NULL )
3372 {
3373 status = PSA_ERROR_NOT_SUPPORTED;
3374 goto exit;
3375 }
3376 status = drv->asymmetric->p_sign( drv_context,
3377 slot->data.se.slot_number,
3378 alg,
3379 hash, hash_length,
3380 signature, signature_size,
3381 signature_length );
3382 }
3383 else
3384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003385#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003386 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003387 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003388 status = psa_rsa_sign( slot->data.rsa,
3389 alg,
3390 hash, hash_length,
3391 signature, signature_size,
3392 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003393 }
3394 else
3395#endif /* defined(MBEDTLS_RSA_C) */
3396#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003397 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003398 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003399#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003400 if(
3401#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3402 PSA_ALG_IS_ECDSA( alg )
3403#else
3404 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3405#endif
3406 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003407 status = psa_ecdsa_sign( slot->data.ecp,
3408 alg,
3409 hash, hash_length,
3410 signature, signature_size,
3411 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003412 else
3413#endif /* defined(MBEDTLS_ECDSA_C) */
3414 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003415 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003416 }
itayzafrir5c753392018-05-08 11:18:38 +03003417 }
3418 else
3419#endif /* defined(MBEDTLS_ECP_C) */
3420 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003421 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003422 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003423
3424exit:
3425 /* Fill the unused part of the output buffer (the whole buffer on error,
3426 * the trailing part on success) with something that isn't a valid mac
3427 * (barring an attack on the mac and deliberately-crafted input),
3428 * in case the caller doesn't check the return status properly. */
3429 if( status == PSA_SUCCESS )
3430 memset( signature + *signature_length, '!',
3431 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003432 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003433 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003434 /* If signature_size is 0 then we have nothing to do. We must not call
3435 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003436 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003437}
3438
Gilles Peskinec5487a82018-12-03 18:08:14 +01003439psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003440 psa_algorithm_t alg,
3441 const uint8_t *hash,
3442 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003443 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003444 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003445{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003446 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003447 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003448#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3449 const psa_drv_se_t *drv;
3450 psa_drv_se_context_t *drv_context;
3451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003452
Gilles Peskineedc64242019-08-07 21:05:07 +02003453 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003454 if( status != PSA_SUCCESS )
3455 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003456
Gilles Peskineedc64242019-08-07 21:05:07 +02003457#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3458 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3459 {
3460 if( drv->asymmetric == NULL ||
3461 drv->asymmetric->p_verify == NULL )
3462 return( PSA_ERROR_NOT_SUPPORTED );
3463 return( drv->asymmetric->p_verify( drv_context,
3464 slot->data.se.slot_number,
3465 alg,
3466 hash, hash_length,
3467 signature, signature_length ) );
3468 }
3469 else
3470#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003471#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003472 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003473 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003474 return( psa_rsa_verify( slot->data.rsa,
3475 alg,
3476 hash, hash_length,
3477 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003478 }
3479 else
3480#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003481#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003482 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003483 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003484#if defined(MBEDTLS_ECDSA_C)
3485 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003486 return( psa_ecdsa_verify( slot->data.ecp,
3487 hash, hash_length,
3488 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003489 else
3490#endif /* defined(MBEDTLS_ECDSA_C) */
3491 {
3492 return( PSA_ERROR_INVALID_ARGUMENT );
3493 }
itayzafrir5c753392018-05-08 11:18:38 +03003494 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003495 else
3496#endif /* defined(MBEDTLS_ECP_C) */
3497 {
3498 return( PSA_ERROR_NOT_SUPPORTED );
3499 }
3500}
3501
Gilles Peskine072ac562018-06-30 00:21:29 +02003502#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3503static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3504 mbedtls_rsa_context *rsa )
3505{
3506 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3507 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3508 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3509 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3510}
3511#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3512
Gilles Peskinec5487a82018-12-03 18:08:14 +01003513psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003514 psa_algorithm_t alg,
3515 const uint8_t *input,
3516 size_t input_length,
3517 const uint8_t *salt,
3518 size_t salt_length,
3519 uint8_t *output,
3520 size_t output_size,
3521 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003523 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003524 psa_status_t status;
3525
Darryl Green5cc689a2018-07-24 15:34:10 +01003526 (void) input;
3527 (void) input_length;
3528 (void) salt;
3529 (void) output;
3530 (void) output_size;
3531
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003532 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003533
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003534 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3535 return( PSA_ERROR_INVALID_ARGUMENT );
3536
Gilles Peskine28f8f302019-07-24 13:30:31 +02003537 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003538 if( status != PSA_SUCCESS )
3539 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003540 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3541 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003543
3544#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003545 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546 {
3547 mbedtls_rsa_context *rsa = slot->data.rsa;
3548 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003549 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003550 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003552 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003554 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3555 mbedtls_ctr_drbg_random,
3556 &global_data.ctr_drbg,
3557 MBEDTLS_RSA_PUBLIC,
3558 input_length,
3559 input,
3560 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003561 }
3562 else
3563#endif /* MBEDTLS_PKCS1_V15 */
3564#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003565 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003566 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003567 psa_rsa_oaep_set_padding_mode( alg, rsa );
3568 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3569 mbedtls_ctr_drbg_random,
3570 &global_data.ctr_drbg,
3571 MBEDTLS_RSA_PUBLIC,
3572 salt, salt_length,
3573 input_length,
3574 input,
3575 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576 }
3577 else
3578#endif /* MBEDTLS_PKCS1_V21 */
3579 {
3580 return( PSA_ERROR_INVALID_ARGUMENT );
3581 }
3582 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003583 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584 return( mbedtls_to_psa_error( ret ) );
3585 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003587#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588 {
3589 return( PSA_ERROR_NOT_SUPPORTED );
3590 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003591}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592
Gilles Peskinec5487a82018-12-03 18:08:14 +01003593psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003594 psa_algorithm_t alg,
3595 const uint8_t *input,
3596 size_t input_length,
3597 const uint8_t *salt,
3598 size_t salt_length,
3599 uint8_t *output,
3600 size_t output_size,
3601 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003603 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003604 psa_status_t status;
3605
Darryl Green5cc689a2018-07-24 15:34:10 +01003606 (void) input;
3607 (void) input_length;
3608 (void) salt;
3609 (void) output;
3610 (void) output_size;
3611
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003612 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003614 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3615 return( PSA_ERROR_INVALID_ARGUMENT );
3616
Gilles Peskine28f8f302019-07-24 13:30:31 +02003617 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003618 if( status != PSA_SUCCESS )
3619 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003620 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621 return( PSA_ERROR_INVALID_ARGUMENT );
3622
3623#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003624 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003625 {
3626 mbedtls_rsa_context *rsa = slot->data.rsa;
3627 int ret;
3628
Gilles Peskine630a18a2018-06-29 17:49:35 +02003629 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630 return( PSA_ERROR_INVALID_ARGUMENT );
3631
3632#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003633 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003635 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3636 mbedtls_ctr_drbg_random,
3637 &global_data.ctr_drbg,
3638 MBEDTLS_RSA_PRIVATE,
3639 output_length,
3640 input,
3641 output,
3642 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 }
3644 else
3645#endif /* MBEDTLS_PKCS1_V15 */
3646#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003647 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003649 psa_rsa_oaep_set_padding_mode( alg, rsa );
3650 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3651 mbedtls_ctr_drbg_random,
3652 &global_data.ctr_drbg,
3653 MBEDTLS_RSA_PRIVATE,
3654 salt, salt_length,
3655 output_length,
3656 input,
3657 output,
3658 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659 }
3660 else
3661#endif /* MBEDTLS_PKCS1_V21 */
3662 {
3663 return( PSA_ERROR_INVALID_ARGUMENT );
3664 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003665
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003666 return( mbedtls_to_psa_error( ret ) );
3667 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003669#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 {
3671 return( PSA_ERROR_NOT_SUPPORTED );
3672 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003673}
Gilles Peskine20035e32018-02-03 22:44:14 +01003674
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003675
3676
mohammad1603503973b2018-03-12 15:59:30 +02003677/****************************************************************/
3678/* Symmetric cryptography */
3679/****************************************************************/
3680
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003681/* Initialize the cipher operation structure. Once this function has been
3682 * called, psa_cipher_abort can run and will do the right thing. */
3683static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3684 psa_algorithm_t alg )
3685{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003686 if( ! PSA_ALG_IS_CIPHER( alg ) )
3687 {
3688 memset( operation, 0, sizeof( *operation ) );
3689 return( PSA_ERROR_INVALID_ARGUMENT );
3690 }
3691
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003692 operation->alg = alg;
3693 operation->key_set = 0;
3694 operation->iv_set = 0;
3695 operation->iv_required = 1;
3696 operation->iv_size = 0;
3697 operation->block_size = 0;
3698 mbedtls_cipher_init( &operation->ctx.cipher );
3699 return( PSA_SUCCESS );
3700}
3701
Gilles Peskinee553c652018-06-04 16:22:46 +02003702static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003703 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003704 psa_algorithm_t alg,
3705 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003706{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003707 int ret = 0;
3708 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003709 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003710 size_t key_bits;
3711 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003712 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3713 PSA_KEY_USAGE_ENCRYPT :
3714 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003715
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003716 /* A context must be freshly initialized before it can be set up. */
3717 if( operation->alg != 0 )
3718 {
3719 return( PSA_ERROR_BAD_STATE );
3720 }
3721
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003722 status = psa_cipher_init( operation, alg );
3723 if( status != PSA_SUCCESS )
3724 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003725
Gilles Peskine28f8f302019-07-24 13:30:31 +02003726 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003727 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003728 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003729 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003730
Gilles Peskine8e338702019-07-30 20:06:31 +02003731 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003732 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003733 {
3734 status = PSA_ERROR_NOT_SUPPORTED;
3735 goto exit;
3736 }
mohammad1603503973b2018-03-12 15:59:30 +02003737
mohammad1603503973b2018-03-12 15:59:30 +02003738 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003739 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003740 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003741
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003742#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003743 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003744 {
3745 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003746 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003747 memcpy( keys, slot->data.raw.data, 16 );
3748 memcpy( keys + 16, slot->data.raw.data, 8 );
3749 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3750 keys,
3751 192, cipher_operation );
3752 }
3753 else
3754#endif
3755 {
3756 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3757 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003758 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003759 }
Moran Peker41deec42018-04-04 15:43:05 +03003760 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003761 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003762
mohammad16038481e742018-03-18 13:57:31 +02003763#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003764 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003765 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003766 case PSA_ALG_CBC_NO_PADDING:
3767 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3768 MBEDTLS_PADDING_NONE );
3769 break;
3770 case PSA_ALG_CBC_PKCS7:
3771 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3772 MBEDTLS_PADDING_PKCS7 );
3773 break;
3774 default:
3775 /* The algorithm doesn't involve padding. */
3776 ret = 0;
3777 break;
3778 }
3779 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003780 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003781#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3782
mohammad1603503973b2018-03-12 15:59:30 +02003783 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003784 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003785 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003786 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003787 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003788 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003789 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003790#if defined(MBEDTLS_CHACHA20_C)
3791 else
3792 if( alg == PSA_ALG_CHACHA20 )
3793 operation->iv_size = 12;
3794#endif
mohammad1603503973b2018-03-12 15:59:30 +02003795
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003796exit:
3797 if( status == 0 )
3798 status = mbedtls_to_psa_error( ret );
3799 if( status != 0 )
3800 psa_cipher_abort( operation );
3801 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003802}
3803
Gilles Peskinefe119512018-07-08 21:39:34 +02003804psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003805 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003806 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003807{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003808 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003809}
3810
Gilles Peskinefe119512018-07-08 21:39:34 +02003811psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003812 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003813 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003814{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003815 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003816}
3817
Gilles Peskinefe119512018-07-08 21:39:34 +02003818psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003819 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003820 size_t iv_size,
3821 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003822{
itayzafrir534bd7c2018-08-02 13:56:32 +03003823 psa_status_t status;
3824 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003825 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003826 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003827 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003828 }
Moran Peker41deec42018-04-04 15:43:05 +03003829 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003830 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003831 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003832 goto exit;
3833 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003834 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3835 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003836 if( ret != 0 )
3837 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003838 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003839 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003840 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003841
mohammad16038481e742018-03-18 13:57:31 +02003842 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003843 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003844
Moran Peker395db872018-05-31 14:07:14 +03003845exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003846 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003847 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003848 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003849}
3850
Gilles Peskinefe119512018-07-08 21:39:34 +02003851psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003852 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003853 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003854{
itayzafrir534bd7c2018-08-02 13:56:32 +03003855 psa_status_t status;
3856 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003857 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003858 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003859 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003860 }
Moran Pekera28258c2018-05-29 16:25:04 +03003861 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003862 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003863 status = PSA_ERROR_INVALID_ARGUMENT;
3864 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003865 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003866 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3867 status = mbedtls_to_psa_error( ret );
3868exit:
3869 if( status == PSA_SUCCESS )
3870 operation->iv_set = 1;
3871 else
mohammad1603503973b2018-03-12 15:59:30 +02003872 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003873 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003874}
3875
Gilles Peskinee553c652018-06-04 16:22:46 +02003876psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3877 const uint8_t *input,
3878 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003879 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003880 size_t output_size,
3881 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003882{
itayzafrir534bd7c2018-08-02 13:56:32 +03003883 psa_status_t status;
3884 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003885 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003886
3887 if( operation->alg == 0 )
3888 {
3889 return( PSA_ERROR_BAD_STATE );
3890 }
3891
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003892 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003893 {
3894 /* Take the unprocessed partial block left over from previous
3895 * update calls, if any, plus the input to this call. Remove
3896 * the last partial block, if any. You get the data that will be
3897 * output in this call. */
3898 expected_output_size =
3899 ( operation->ctx.cipher.unprocessed_len + input_length )
3900 / operation->block_size * operation->block_size;
3901 }
3902 else
3903 {
3904 expected_output_size = input_length;
3905 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003906
Gilles Peskine89d789c2018-06-04 17:17:16 +02003907 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003908 {
3909 status = PSA_ERROR_BUFFER_TOO_SMALL;
3910 goto exit;
3911 }
mohammad160382759612018-03-12 18:16:40 +02003912
mohammad1603503973b2018-03-12 15:59:30 +02003913 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003914 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 status = mbedtls_to_psa_error( ret );
3916exit:
3917 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003918 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003919 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003920}
3921
Gilles Peskinee553c652018-06-04 16:22:46 +02003922psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3923 uint8_t *output,
3924 size_t output_size,
3925 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003926{
David Saadab4ecc272019-02-14 13:48:10 +02003927 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003928 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003929 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003930
mohammad1603503973b2018-03-12 15:59:30 +02003931 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003932 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003933 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003934 }
3935 if( operation->iv_required && ! operation->iv_set )
3936 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003937 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003938 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003939
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003940 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003941 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3942 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003943 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003944 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003945 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003946 }
3947
Janos Follath315b51c2018-07-09 16:04:51 +01003948 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3949 temp_output_buffer,
3950 output_length );
3951 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003952 {
Janos Follath315b51c2018-07-09 16:04:51 +01003953 status = mbedtls_to_psa_error( cipher_ret );
3954 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003955 }
Janos Follath315b51c2018-07-09 16:04:51 +01003956
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003957 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003958 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003959 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003960 memcpy( output, temp_output_buffer, *output_length );
3961 else
3962 {
Janos Follath315b51c2018-07-09 16:04:51 +01003963 status = PSA_ERROR_BUFFER_TOO_SMALL;
3964 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003965 }
mohammad1603503973b2018-03-12 15:59:30 +02003966
Gilles Peskine3f108122018-12-07 18:14:53 +01003967 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003968 status = psa_cipher_abort( operation );
3969
3970 return( status );
3971
3972error:
3973
3974 *output_length = 0;
3975
Gilles Peskine3f108122018-12-07 18:14:53 +01003976 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003977 (void) psa_cipher_abort( operation );
3978
3979 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003980}
3981
Gilles Peskinee553c652018-06-04 16:22:46 +02003982psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3983{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003984 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003985 {
3986 /* The object has (apparently) been initialized but it is not
3987 * in use. It's ok to call abort on such an object, and there's
3988 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003989 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003990 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003991
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003992 /* Sanity check (shouldn't happen: operation->alg should
3993 * always have been initialized to a valid value). */
3994 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3995 return( PSA_ERROR_BAD_STATE );
3996
mohammad1603503973b2018-03-12 15:59:30 +02003997 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003998
Moran Peker41deec42018-04-04 15:43:05 +03003999 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004000 operation->key_set = 0;
4001 operation->iv_set = 0;
4002 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004003 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004004 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004005
Moran Peker395db872018-05-31 14:07:14 +03004006 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004007}
4008
Gilles Peskinea0655c32018-04-30 17:06:50 +02004009
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004010
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004011
mohammad16035955c982018-04-26 00:53:03 +03004012/****************************************************************/
4013/* AEAD */
4014/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004015
Gilles Peskineedf9a652018-08-17 18:11:56 +02004016typedef struct
4017{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004018 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004019 const mbedtls_cipher_info_t *cipher_info;
4020 union
4021 {
4022#if defined(MBEDTLS_CCM_C)
4023 mbedtls_ccm_context ccm;
4024#endif /* MBEDTLS_CCM_C */
4025#if defined(MBEDTLS_GCM_C)
4026 mbedtls_gcm_context gcm;
4027#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004028#if defined(MBEDTLS_CHACHAPOLY_C)
4029 mbedtls_chachapoly_context chachapoly;
4030#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004031 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004032 psa_algorithm_t core_alg;
4033 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004034 uint8_t tag_length;
4035} aead_operation_t;
4036
Gilles Peskine30a9e412019-01-14 18:36:12 +01004037static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004038{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004039 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004040 {
4041#if defined(MBEDTLS_CCM_C)
4042 case PSA_ALG_CCM:
4043 mbedtls_ccm_free( &operation->ctx.ccm );
4044 break;
4045#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004046#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004047 case PSA_ALG_GCM:
4048 mbedtls_gcm_free( &operation->ctx.gcm );
4049 break;
4050#endif /* MBEDTLS_GCM_C */
4051 }
4052}
4053
4054static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004055 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004056 psa_key_usage_t usage,
4057 psa_algorithm_t alg )
4058{
4059 psa_status_t status;
4060 size_t key_bits;
4061 mbedtls_cipher_id_t cipher_id;
4062
Gilles Peskine28f8f302019-07-24 13:30:31 +02004063 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004064 if( status != PSA_SUCCESS )
4065 return( status );
4066
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004067 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004068
4069 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004070 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004071 &cipher_id );
4072 if( operation->cipher_info == NULL )
4073 return( PSA_ERROR_NOT_SUPPORTED );
4074
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004075 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004076 {
4077#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004078 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4079 operation->core_alg = PSA_ALG_CCM;
4080 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004081 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4082 * The call to mbedtls_ccm_encrypt_and_tag or
4083 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004084 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085 return( PSA_ERROR_INVALID_ARGUMENT );
4086 mbedtls_ccm_init( &operation->ctx.ccm );
4087 status = mbedtls_to_psa_error(
4088 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4089 operation->slot->data.raw.data,
4090 (unsigned int) key_bits ) );
4091 if( status != 0 )
4092 goto cleanup;
4093 break;
4094#endif /* MBEDTLS_CCM_C */
4095
4096#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004097 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4098 operation->core_alg = PSA_ALG_GCM;
4099 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004100 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4101 * The call to mbedtls_gcm_crypt_and_tag or
4102 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004103 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004104 return( PSA_ERROR_INVALID_ARGUMENT );
4105 mbedtls_gcm_init( &operation->ctx.gcm );
4106 status = mbedtls_to_psa_error(
4107 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4108 operation->slot->data.raw.data,
4109 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004110 if( status != 0 )
4111 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112 break;
4113#endif /* MBEDTLS_GCM_C */
4114
Gilles Peskine26869f22019-05-06 15:25:00 +02004115#if defined(MBEDTLS_CHACHAPOLY_C)
4116 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4117 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4118 operation->full_tag_length = 16;
4119 /* We only support the default tag length. */
4120 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4121 return( PSA_ERROR_NOT_SUPPORTED );
4122 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4123 status = mbedtls_to_psa_error(
4124 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4125 operation->slot->data.raw.data ) );
4126 if( status != 0 )
4127 goto cleanup;
4128 break;
4129#endif /* MBEDTLS_CHACHAPOLY_C */
4130
Gilles Peskineedf9a652018-08-17 18:11:56 +02004131 default:
4132 return( PSA_ERROR_NOT_SUPPORTED );
4133 }
4134
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004135 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4136 {
4137 status = PSA_ERROR_INVALID_ARGUMENT;
4138 goto cleanup;
4139 }
4140 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004141
Gilles Peskineedf9a652018-08-17 18:11:56 +02004142 return( PSA_SUCCESS );
4143
4144cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004145 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004146 return( status );
4147}
4148
Gilles Peskinec5487a82018-12-03 18:08:14 +01004149psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004150 psa_algorithm_t alg,
4151 const uint8_t *nonce,
4152 size_t nonce_length,
4153 const uint8_t *additional_data,
4154 size_t additional_data_length,
4155 const uint8_t *plaintext,
4156 size_t plaintext_length,
4157 uint8_t *ciphertext,
4158 size_t ciphertext_size,
4159 size_t *ciphertext_length )
4160{
mohammad16035955c982018-04-26 00:53:03 +03004161 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004162 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004163 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004164
mohammad1603f08a5502018-06-03 15:05:47 +03004165 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004166
Gilles Peskinec5487a82018-12-03 18:08:14 +01004167 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004168 if( status != PSA_SUCCESS )
4169 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004170
Gilles Peskineedf9a652018-08-17 18:11:56 +02004171 /* For all currently supported modes, the tag is at the end of the
4172 * ciphertext. */
4173 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4174 {
4175 status = PSA_ERROR_BUFFER_TOO_SMALL;
4176 goto exit;
4177 }
4178 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004179
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004180#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004181 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004182 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004183 status = mbedtls_to_psa_error(
4184 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4185 MBEDTLS_GCM_ENCRYPT,
4186 plaintext_length,
4187 nonce, nonce_length,
4188 additional_data, additional_data_length,
4189 plaintext, ciphertext,
4190 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004191 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004192 else
4193#endif /* MBEDTLS_GCM_C */
4194#if defined(MBEDTLS_CCM_C)
4195 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004196 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004197 status = mbedtls_to_psa_error(
4198 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4199 plaintext_length,
4200 nonce, nonce_length,
4201 additional_data,
4202 additional_data_length,
4203 plaintext, ciphertext,
4204 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004205 }
mohammad16035c8845f2018-05-09 05:40:09 -07004206 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004207#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004208#if defined(MBEDTLS_CHACHAPOLY_C)
4209 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4210 {
4211 if( nonce_length != 12 || operation.tag_length != 16 )
4212 {
4213 status = PSA_ERROR_NOT_SUPPORTED;
4214 goto exit;
4215 }
4216 status = mbedtls_to_psa_error(
4217 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4218 plaintext_length,
4219 nonce,
4220 additional_data,
4221 additional_data_length,
4222 plaintext,
4223 ciphertext,
4224 tag ) );
4225 }
4226 else
4227#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004228 {
mohammad1603554faad2018-06-03 15:07:38 +03004229 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004230 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004231
Gilles Peskineedf9a652018-08-17 18:11:56 +02004232 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4233 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004234
Gilles Peskineedf9a652018-08-17 18:11:56 +02004235exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004236 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004237 if( status == PSA_SUCCESS )
4238 *ciphertext_length = plaintext_length + operation.tag_length;
4239 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004240}
4241
Gilles Peskineee652a32018-06-01 19:23:52 +02004242/* Locate the tag in a ciphertext buffer containing the encrypted data
4243 * followed by the tag. Return the length of the part preceding the tag in
4244 * *plaintext_length. This is the size of the plaintext in modes where
4245 * the encrypted data has the same size as the plaintext, such as
4246 * CCM and GCM. */
4247static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4248 const uint8_t *ciphertext,
4249 size_t ciphertext_length,
4250 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004251 const uint8_t **p_tag )
4252{
4253 size_t payload_length;
4254 if( tag_length > ciphertext_length )
4255 return( PSA_ERROR_INVALID_ARGUMENT );
4256 payload_length = ciphertext_length - tag_length;
4257 if( payload_length > plaintext_size )
4258 return( PSA_ERROR_BUFFER_TOO_SMALL );
4259 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004260 return( PSA_SUCCESS );
4261}
4262
Gilles Peskinec5487a82018-12-03 18:08:14 +01004263psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004264 psa_algorithm_t alg,
4265 const uint8_t *nonce,
4266 size_t nonce_length,
4267 const uint8_t *additional_data,
4268 size_t additional_data_length,
4269 const uint8_t *ciphertext,
4270 size_t ciphertext_length,
4271 uint8_t *plaintext,
4272 size_t plaintext_size,
4273 size_t *plaintext_length )
4274{
mohammad16035955c982018-04-26 00:53:03 +03004275 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004276 aead_operation_t operation;
4277 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004278
Gilles Peskineee652a32018-06-01 19:23:52 +02004279 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004280
Gilles Peskinec5487a82018-12-03 18:08:14 +01004281 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004282 if( status != PSA_SUCCESS )
4283 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004284
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004285 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4286 ciphertext, ciphertext_length,
4287 plaintext_size, &tag );
4288 if( status != PSA_SUCCESS )
4289 goto exit;
4290
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004291#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004292 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004293 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004294 status = mbedtls_to_psa_error(
4295 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4296 ciphertext_length - operation.tag_length,
4297 nonce, nonce_length,
4298 additional_data,
4299 additional_data_length,
4300 tag, operation.tag_length,
4301 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004302 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004303 else
4304#endif /* MBEDTLS_GCM_C */
4305#if defined(MBEDTLS_CCM_C)
4306 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004307 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004308 status = mbedtls_to_psa_error(
4309 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4310 ciphertext_length - operation.tag_length,
4311 nonce, nonce_length,
4312 additional_data,
4313 additional_data_length,
4314 ciphertext, plaintext,
4315 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004316 }
mohammad160339574652018-06-01 04:39:53 -07004317 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004318#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004319#if defined(MBEDTLS_CHACHAPOLY_C)
4320 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4321 {
4322 if( nonce_length != 12 || operation.tag_length != 16 )
4323 {
4324 status = PSA_ERROR_NOT_SUPPORTED;
4325 goto exit;
4326 }
4327 status = mbedtls_to_psa_error(
4328 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4329 ciphertext_length - operation.tag_length,
4330 nonce,
4331 additional_data,
4332 additional_data_length,
4333 tag,
4334 ciphertext,
4335 plaintext ) );
4336 }
4337 else
4338#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004339 {
mohammad1603554faad2018-06-03 15:07:38 +03004340 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004341 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004342
Gilles Peskineedf9a652018-08-17 18:11:56 +02004343 if( status != PSA_SUCCESS && plaintext_size != 0 )
4344 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004345
Gilles Peskineedf9a652018-08-17 18:11:56 +02004346exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004347 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004348 if( status == PSA_SUCCESS )
4349 *plaintext_length = ciphertext_length - operation.tag_length;
4350 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004351}
4352
Gilles Peskinea0655c32018-04-30 17:06:50 +02004353
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004354
Gilles Peskine20035e32018-02-03 22:44:14 +01004355/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004356/* Generators */
4357/****************************************************************/
4358
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004359#define HKDF_STATE_INIT 0 /* no input yet */
4360#define HKDF_STATE_STARTED 1 /* got salt */
4361#define HKDF_STATE_KEYED 2 /* got key */
4362#define HKDF_STATE_OUTPUT 3 /* output started */
4363
Gilles Peskinecbe66502019-05-16 16:59:18 +02004364static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004366{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4368 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004369 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004371}
4372
4373
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004374psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004375{
4376 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004378 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004379 {
4380 /* The object has (apparently) been initialized but it is not
4381 * in use. It's ok to call abort on such an object, and there's
4382 * nothing to do. */
4383 }
4384 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004385#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004386 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004387 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 mbedtls_free( operation->ctx.hkdf.info );
4389 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004390 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004391 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004393 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004394 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004395#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004397 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4399 operation->ctx.tls12_prf.key_len );
4400 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004401 }
Hanno Becker580fba12018-11-13 20:50:45 +00004402
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004404 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4406 operation->ctx.tls12_prf.Ai_with_seed_len );
4407 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004408 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004409#else
4410 if( operation->ctx.tls12_prf.seed != NULL )
4411 {
4412 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4413 operation->ctx.tls12_prf.seed_length );
4414 mbedtls_free( operation->ctx.tls12_prf.seed );
4415 }
4416
4417 if( operation->ctx.tls12_prf.label != NULL )
4418 {
4419 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4420 operation->ctx.tls12_prf.label_length );
4421 mbedtls_free( operation->ctx.tls12_prf.label );
4422 }
4423
4424 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4425
4426 /* We leave the fields Ai and output_block to be erased safely by the
4427 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004428#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004429 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430 else
4431#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004432 {
4433 status = PSA_ERROR_BAD_STATE;
4434 }
Janos Follath083036a2019-06-11 10:22:26 +01004435 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004436 return( status );
4437}
4438
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004440 size_t *capacity)
4441{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004443 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004445 return PSA_ERROR_BAD_STATE;
4446 }
4447
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004449 return( PSA_SUCCESS );
4450}
4451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004452psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004453 size_t capacity )
4454{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004456 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004457 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004458 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004460 return( PSA_SUCCESS );
4461}
4462
Gilles Peskinebef7f142018-07-12 17:22:21 +02004463#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004465 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004466static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004467 psa_algorithm_t hash_alg,
4468 uint8_t *output,
4469 size_t output_length )
4470{
4471 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4472 psa_status_t status;
4473
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004474 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4475 return( PSA_ERROR_BAD_STATE );
4476 hkdf->state = HKDF_STATE_OUTPUT;
4477
Gilles Peskinebef7f142018-07-12 17:22:21 +02004478 while( output_length != 0 )
4479 {
4480 /* Copy what remains of the current block */
4481 uint8_t n = hash_length - hkdf->offset_in_block;
4482 if( n > output_length )
4483 n = (uint8_t) output_length;
4484 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4485 output += n;
4486 output_length -= n;
4487 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004488 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004489 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004490 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004491 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004493 * object was corrupted or if this function is called directly
4494 * inside the library. */
4495 if( hkdf->block_number == 0xff )
4496 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004497
4498 /* We need a new block */
4499 ++hkdf->block_number;
4500 hkdf->offset_in_block = 0;
4501 status = psa_hmac_setup_internal( &hkdf->hmac,
4502 hkdf->prk, hash_length,
4503 hash_alg );
4504 if( status != PSA_SUCCESS )
4505 return( status );
4506 if( hkdf->block_number != 1 )
4507 {
4508 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4509 hkdf->output_block,
4510 hash_length );
4511 if( status != PSA_SUCCESS )
4512 return( status );
4513 }
4514 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4515 hkdf->info,
4516 hkdf->info_length );
4517 if( status != PSA_SUCCESS )
4518 return( status );
4519 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4520 &hkdf->block_number, 1 );
4521 if( status != PSA_SUCCESS )
4522 return( status );
4523 status = psa_hmac_finish_internal( &hkdf->hmac,
4524 hkdf->output_block,
4525 sizeof( hkdf->output_block ) );
4526 if( status != PSA_SUCCESS )
4527 return( status );
4528 }
4529
4530 return( PSA_SUCCESS );
4531}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004532
Janos Follath999f6482019-06-11 12:04:10 +01004533#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004534static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4535 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004536 psa_algorithm_t alg )
4537{
4538 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4539 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4540 psa_hmac_internal_data hmac;
4541 psa_status_t status, cleanup_status;
4542
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004543 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004544 size_t Ai_len;
4545
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004546 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004547 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004549 * object was corrupted or if this function is called directly
4550 * inside the library. */
4551 if( tls12_prf->block_number == 0xff )
4552 return( PSA_ERROR_BAD_STATE );
4553
4554 /* We need a new block */
4555 ++tls12_prf->block_number;
4556 tls12_prf->offset_in_block = 0;
4557
4558 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4559 *
4560 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4561 *
4562 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4563 * HMAC_hash(secret, A(2) + seed) +
4564 * HMAC_hash(secret, A(3) + seed) + ...
4565 *
4566 * A(0) = seed
4567 * A(i) = HMAC_hash( secret, A(i-1) )
4568 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004569 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004570 * `HMAC_hash(secret, A(i) + seed)` from which the output
4571 * is currently extracted as `output_block`, while
4572 * `A(i) + seed` is stored in `Ai_with_seed`.
4573 *
4574 * Generating a new block means recalculating `Ai_with_seed`
4575 * from the A(i)-part of it, and afterwards recalculating
4576 * `output_block`.
4577 *
4578 * A(0) is computed at setup time.
4579 *
4580 */
4581
4582 psa_hmac_init_internal( &hmac );
4583
4584 /* We must distinguish the calculation of A(1) from those
4585 * of A(2) and higher, because A(0)=seed has a different
4586 * length than the other A(i). */
4587 if( tls12_prf->block_number == 1 )
4588 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004589 Ai = tls12_prf->Ai_with_seed + hash_length;
4590 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004591 }
4592 else
4593 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004594 Ai = tls12_prf->Ai_with_seed;
4595 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004596 }
4597
Hanno Becker3b339e22018-11-13 20:56:14 +00004598 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4599 status = psa_hmac_setup_internal( &hmac,
4600 tls12_prf->key,
4601 tls12_prf->key_len,
4602 hash_alg );
4603 if( status != PSA_SUCCESS )
4604 goto cleanup;
4605
4606 status = psa_hash_update( &hmac.hash_ctx,
4607 Ai, Ai_len );
4608 if( status != PSA_SUCCESS )
4609 goto cleanup;
4610
4611 status = psa_hmac_finish_internal( &hmac,
4612 tls12_prf->Ai_with_seed,
4613 hash_length );
4614 if( status != PSA_SUCCESS )
4615 goto cleanup;
4616
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004617 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4618 status = psa_hmac_setup_internal( &hmac,
4619 tls12_prf->key,
4620 tls12_prf->key_len,
4621 hash_alg );
4622 if( status != PSA_SUCCESS )
4623 goto cleanup;
4624
4625 status = psa_hash_update( &hmac.hash_ctx,
4626 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004627 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004628 if( status != PSA_SUCCESS )
4629 goto cleanup;
4630
4631 status = psa_hmac_finish_internal( &hmac,
4632 tls12_prf->output_block,
4633 hash_length );
4634 if( status != PSA_SUCCESS )
4635 goto cleanup;
4636
4637cleanup:
4638
4639 cleanup_status = psa_hmac_abort_internal( &hmac );
4640 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4641 status = cleanup_status;
4642
4643 return( status );
4644}
Janos Follath7742fee2019-06-17 12:58:10 +01004645#else
4646static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4647 psa_tls12_prf_key_derivation_t *tls12_prf,
4648 psa_algorithm_t alg )
4649{
4650 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4651 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004652 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4653 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004654
Janos Follath7742fee2019-06-17 12:58:10 +01004655 /* We can't be wanting more output after block 0xff, otherwise
4656 * the capacity check in psa_key_derivation_output_bytes() would have
4657 * prevented this call. It could happen only if the operation
4658 * object was corrupted or if this function is called directly
4659 * inside the library. */
4660 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004661 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004662
4663 /* We need a new block */
4664 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004665 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004666
4667 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4668 *
4669 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4670 *
4671 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4672 * HMAC_hash(secret, A(2) + seed) +
4673 * HMAC_hash(secret, A(3) + seed) + ...
4674 *
4675 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004676 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004677 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004678 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004679 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004680 * is currently extracted as `output_block` and where i is
4681 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004682 */
4683
Janos Follathea29bfb2019-06-19 12:21:20 +01004684 /* Save the hash context before using it, to preserve the hash state with
4685 * only the inner padding in it. We need this, because inner padding depends
4686 * on the key (secret in the RFC's terminology). */
4687 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4688 if( status != PSA_SUCCESS )
4689 goto cleanup;
4690
4691 /* Calculate A(i) where i = tls12_prf->block_number. */
4692 if( tls12_prf->block_number == 1 )
4693 {
4694 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4695 * the variable seed and in this instance means it in the context of the
4696 * P_hash function, where seed = label + seed.) */
4697 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4698 tls12_prf->label, tls12_prf->label_length );
4699 if( status != PSA_SUCCESS )
4700 goto cleanup;
4701 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4702 tls12_prf->seed, tls12_prf->seed_length );
4703 if( status != PSA_SUCCESS )
4704 goto cleanup;
4705 }
4706 else
4707 {
4708 /* A(i) = HMAC_hash(secret, A(i-1)) */
4709 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4710 tls12_prf->Ai, hash_length );
4711 if( status != PSA_SUCCESS )
4712 goto cleanup;
4713 }
4714
4715 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4716 tls12_prf->Ai, hash_length );
4717 if( status != PSA_SUCCESS )
4718 goto cleanup;
4719 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4720 if( status != PSA_SUCCESS )
4721 goto cleanup;
4722
4723 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4724 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4725 tls12_prf->Ai, hash_length );
4726 if( status != PSA_SUCCESS )
4727 goto cleanup;
4728 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4729 tls12_prf->label, tls12_prf->label_length );
4730 if( status != PSA_SUCCESS )
4731 goto cleanup;
4732 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4733 tls12_prf->seed, tls12_prf->seed_length );
4734 if( status != PSA_SUCCESS )
4735 goto cleanup;
4736 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4737 tls12_prf->output_block, hash_length );
4738 if( status != PSA_SUCCESS )
4739 goto cleanup;
4740 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4741 if( status != PSA_SUCCESS )
4742 goto cleanup;
4743
Janos Follath7742fee2019-06-17 12:58:10 +01004744
4745cleanup:
4746
Janos Follathea29bfb2019-06-19 12:21:20 +01004747 cleanup_status = psa_hash_abort( &backup );
4748 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4749 status = cleanup_status;
4750
4751 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004752}
Janos Follath999f6482019-06-11 12:04:10 +01004753#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004754
Janos Follath999f6482019-06-11 12:04:10 +01004755#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004757 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004758static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004759 psa_tls12_prf_key_derivation_t *tls12_prf,
4760 psa_algorithm_t alg,
4761 uint8_t *output,
4762 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004763{
4764 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4765 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4766 psa_status_t status;
4767
4768 while( output_length != 0 )
4769 {
4770 /* Copy what remains of the current block */
4771 uint8_t n = hash_length - tls12_prf->offset_in_block;
4772
4773 /* Check if we have fully processed the current block. */
4774 if( n == 0 )
4775 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004776 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004777 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004778 if( status != PSA_SUCCESS )
4779 return( status );
4780
4781 continue;
4782 }
4783
4784 if( n > output_length )
4785 n = (uint8_t) output_length;
4786 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4787 n );
4788 output += n;
4789 output_length -= n;
4790 tls12_prf->offset_in_block += n;
4791 }
4792
4793 return( PSA_SUCCESS );
4794}
Janos Follath844eb0e2019-06-19 12:10:49 +01004795#else
4796static psa_status_t psa_key_derivation_tls12_prf_read(
4797 psa_tls12_prf_key_derivation_t *tls12_prf,
4798 psa_algorithm_t alg,
4799 uint8_t *output,
4800 size_t output_length )
4801{
4802 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4803 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4804 psa_status_t status;
4805 uint8_t offset, length;
4806
4807 while( output_length != 0 )
4808 {
4809 /* Check if we have fully processed the current block. */
4810 if( tls12_prf->left_in_block == 0 )
4811 {
4812 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4813 alg );
4814 if( status != PSA_SUCCESS )
4815 return( status );
4816
4817 continue;
4818 }
4819
4820 if( tls12_prf->left_in_block > output_length )
4821 length = (uint8_t) output_length;
4822 else
4823 length = tls12_prf->left_in_block;
4824
4825 offset = hash_length - tls12_prf->left_in_block;
4826 memcpy( output, tls12_prf->output_block + offset, length );
4827 output += length;
4828 output_length -= length;
4829 tls12_prf->left_in_block -= length;
4830 }
4831
4832 return( PSA_SUCCESS );
4833}
Janos Follath999f6482019-06-11 12:04:10 +01004834#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004835#endif /* MBEDTLS_MD_C */
4836
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004837psa_status_t psa_key_derivation_output_bytes(
4838 psa_key_derivation_operation_t *operation,
4839 uint8_t *output,
4840 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004841{
4842 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004844
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004846 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004848 return PSA_ERROR_BAD_STATE;
4849 }
4850
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004852 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004854 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004856 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004857 goto exit;
4858 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004860 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004862 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004863 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4864 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004866 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004867 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004868 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004870
Gilles Peskinebef7f142018-07-12 17:22:21 +02004871#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004872 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004873 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004876 output, output_length );
4877 }
Janos Follathadbec812019-06-14 11:05:39 +01004878 else
Janos Follathadbec812019-06-14 11:05:39 +01004879 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004880 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004881 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004883 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004884 output_length );
4885 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004886 else
4887#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004888 {
4889 return( PSA_ERROR_BAD_STATE );
4890 }
4891
4892exit:
4893 if( status != PSA_SUCCESS )
4894 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004895 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 * This allows us to differentiate between exhausted operations and
4897 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4898 * operations. */
4899 psa_algorithm_t alg = operation->alg;
4900 psa_key_derivation_abort( operation );
4901 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004902 memset( output, '!', output_length );
4903 }
4904 return( status );
4905}
4906
Gilles Peskine08542d82018-07-19 17:05:42 +02004907#if defined(MBEDTLS_DES_C)
4908static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4909{
4910 if( data_size >= 8 )
4911 mbedtls_des_key_set_parity( data );
4912 if( data_size >= 16 )
4913 mbedtls_des_key_set_parity( data + 8 );
4914 if( data_size >= 24 )
4915 mbedtls_des_key_set_parity( data + 16 );
4916}
4917#endif /* MBEDTLS_DES_C */
4918
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004919static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004920 psa_key_slot_t *slot,
4921 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004922 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004923{
4924 uint8_t *data = NULL;
4925 size_t bytes = PSA_BITS_TO_BYTES( bits );
4926 psa_status_t status;
4927
Gilles Peskine8e338702019-07-30 20:06:31 +02004928 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004929 return( PSA_ERROR_INVALID_ARGUMENT );
4930 if( bits % 8 != 0 )
4931 return( PSA_ERROR_INVALID_ARGUMENT );
4932 data = mbedtls_calloc( 1, bytes );
4933 if( data == NULL )
4934 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004937 if( status != PSA_SUCCESS )
4938 goto exit;
4939#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004940 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004941 psa_des_set_key_parity( data, bytes );
4942#endif /* MBEDTLS_DES_C */
4943 status = psa_import_key_into_slot( slot, data, bytes );
4944
4945exit:
4946 mbedtls_free( data );
4947 return( status );
4948}
4949
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004950psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004952 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004953{
4954 psa_status_t status;
4955 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004956 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004957 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4958 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004959#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4960 if( driver != NULL )
4961 {
4962 /* Deriving a key in a secure element is not implemented yet. */
4963 status = PSA_ERROR_NOT_SUPPORTED;
4964 }
4965#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004966 if( status == PSA_SUCCESS )
4967 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004968 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004969 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004971 }
4972 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004973 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004974 if( status != PSA_SUCCESS )
4975 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004976 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004977 *handle = 0;
4978 }
4979 return( status );
4980}
4981
Gilles Peskineeab56e42018-07-12 17:12:33 +02004982
4983
4984/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004985/* Key derivation */
4986/****************************************************************/
4987
Gilles Peskinea05219c2018-11-16 16:02:56 +01004988#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004989#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004991 * of the HKDF algorithm.
4992 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004993 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004994 * to potentially free embedded data structures and wipe confidential data.
4995 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004996static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004997 const uint8_t *secret,
4998 size_t secret_length,
4999 psa_algorithm_t hash_alg,
5000 const uint8_t *salt,
5001 size_t salt_length,
5002 const uint8_t *label,
5003 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005004{
5005 psa_status_t status;
5006 status = psa_hmac_setup_internal( &hkdf->hmac,
5007 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005008 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005009 if( status != PSA_SUCCESS )
5010 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005011 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005012 if( status != PSA_SUCCESS )
5013 return( status );
5014 status = psa_hmac_finish_internal( &hkdf->hmac,
5015 hkdf->prk,
5016 sizeof( hkdf->prk ) );
5017 if( status != PSA_SUCCESS )
5018 return( status );
5019 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5020 hkdf->block_number = 0;
5021 hkdf->info_length = label_length;
5022 if( label_length != 0 )
5023 {
5024 hkdf->info = mbedtls_calloc( 1, label_length );
5025 if( hkdf->info == NULL )
5026 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5027 memcpy( hkdf->info, label, label_length );
5028 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 hkdf->state = HKDF_STATE_KEYED;
5030 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005031 return( PSA_SUCCESS );
5032}
Janos Follath71a4c912019-06-11 09:14:47 +01005033#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005034#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005035
Gilles Peskinea05219c2018-11-16 16:02:56 +01005036#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01005037#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01005039 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005040 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005041 * to potentially free embedded data structures and wipe confidential data.
5042 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005043static psa_status_t psa_key_derivation_tls12_prf_setup(
5044 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005045 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005046 size_t key_len,
5047 psa_algorithm_t hash_alg,
5048 const uint8_t *salt,
5049 size_t salt_length,
5050 const uint8_t *label,
5051 size_t label_length )
5052{
5053 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00005054 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
5055 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005056
5057 tls12_prf->key = mbedtls_calloc( 1, key_len );
5058 if( tls12_prf->key == NULL )
5059 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5060 tls12_prf->key_len = key_len;
5061 memcpy( tls12_prf->key, key, key_len );
5062
Hanno Becker580fba12018-11-13 20:50:45 +00005063 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005064 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00005065 if( overflow )
5066 return( PSA_ERROR_INVALID_ARGUMENT );
5067
5068 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
5069 if( tls12_prf->Ai_with_seed == NULL )
5070 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5071 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
5072
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005073 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
5074 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00005075 if( label_length != 0 )
5076 {
5077 memcpy( tls12_prf->Ai_with_seed + hash_length,
5078 label, label_length );
5079 }
5080
5081 if( salt_length != 0 )
5082 {
5083 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
5084 salt, salt_length );
5085 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005086
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005087 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005088 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005089 tls12_prf->block_number = 0;
5090 tls12_prf->offset_in_block = hash_length;
5091
5092 return( PSA_SUCCESS );
5093}
Janos Follath71a4c912019-06-11 09:14:47 +01005094#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00005095
Janos Follath71a4c912019-06-11 09:14:47 +01005096#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005097/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005098static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
5099 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005100 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005101 size_t psk_len,
5102 psa_algorithm_t hash_alg,
5103 const uint8_t *salt,
5104 size_t salt_length,
5105 const uint8_t *label,
5106 size_t label_length )
5107{
5108 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005109 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00005110
5111 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5112 return( PSA_ERROR_INVALID_ARGUMENT );
5113
5114 /* Quoting RFC 4279, Section 2:
5115 *
5116 * The premaster secret is formed as follows: if the PSK is N octets
5117 * long, concatenate a uint16 with the value N, N zero octets, a second
5118 * uint16 with the value N, and the PSK itself.
5119 */
5120
5121 pms[0] = ( psk_len >> 8 ) & 0xff;
5122 pms[1] = ( psk_len >> 0 ) & 0xff;
5123 memset( pms + 2, 0, psk_len );
5124 pms[2 + psk_len + 0] = pms[0];
5125 pms[2 + psk_len + 1] = pms[1];
5126 memcpy( pms + 4 + psk_len, psk, psk_len );
5127
Gilles Peskinecbe66502019-05-16 16:59:18 +02005128 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005129 pms, 4 + 2 * psk_len,
5130 hash_alg,
5131 salt, salt_length,
5132 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005133
Gilles Peskine3f108122018-12-07 18:14:53 +01005134 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005135 return( status );
5136}
Janos Follath71a4c912019-06-11 09:14:47 +01005137#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005138#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005139
Janos Follath71a4c912019-06-11 09:14:47 +01005140#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005141/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005142 * to potentially free embedded data structures and wipe confidential data.
5143 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005144static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005146 const uint8_t *secret, size_t secret_length,
5147 psa_algorithm_t alg,
5148 const uint8_t *salt, size_t salt_length,
5149 const uint8_t *label, size_t label_length,
5150 size_t capacity )
5151{
5152 psa_status_t status;
5153 size_t max_capacity;
5154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 /* Set operation->alg even on failure so that abort knows what to do. */
5156 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005157
5158#if defined(MBEDTLS_MD_C)
5159 if( PSA_ALG_IS_HKDF( alg ) )
5160 {
5161 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5162 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5163 if( hash_size == 0 )
5164 return( PSA_ERROR_NOT_SUPPORTED );
5165 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005166 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005167 secret, secret_length,
5168 hash_alg,
5169 salt, salt_length,
5170 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005171 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005172 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5173 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5174 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005175 {
5176 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5177 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5178
5179 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5180 if( hash_alg != PSA_ALG_SHA_256 &&
5181 hash_alg != PSA_ALG_SHA_384 )
5182 {
5183 return( PSA_ERROR_NOT_SUPPORTED );
5184 }
5185
5186 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005187
5188 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5189 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005191 secret, secret_length,
5192 hash_alg, salt, salt_length,
5193 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005194 }
5195 else
5196 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005197 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005198 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005199 secret, secret_length,
5200 hash_alg, salt, salt_length,
5201 label, label_length );
5202 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005203 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005204 else
5205#endif
5206 {
5207 return( PSA_ERROR_NOT_SUPPORTED );
5208 }
5209
5210 if( status != PSA_SUCCESS )
5211 return( status );
5212
5213 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005215 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005217 else
5218 return( PSA_ERROR_INVALID_ARGUMENT );
5219
5220 return( PSA_SUCCESS );
5221}
Janos Follath71a4c912019-06-11 09:14:47 +01005222#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005223
Janos Follath71a4c912019-06-11 09:14:47 +01005224#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005226 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005227 psa_algorithm_t alg,
5228 const uint8_t *salt,
5229 size_t salt_length,
5230 const uint8_t *label,
5231 size_t label_length,
5232 size_t capacity )
5233{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005234 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005235 psa_status_t status;
5236
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005237 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005238 return( PSA_ERROR_BAD_STATE );
5239
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005240 /* Make sure that alg is a key derivation algorithm. This prevents
5241 * key selection algorithms, which psa_key_derivation_internal
5242 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005243 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5244 return( PSA_ERROR_INVALID_ARGUMENT );
5245
Gilles Peskine28f8f302019-07-24 13:30:31 +02005246 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005247 if( status != PSA_SUCCESS )
5248 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005249
Gilles Peskine8e338702019-07-30 20:06:31 +02005250 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005251 return( PSA_ERROR_INVALID_ARGUMENT );
5252
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005254 slot->data.raw.data,
5255 slot->data.raw.bytes,
5256 alg,
5257 salt, salt_length,
5258 label, label_length,
5259 capacity );
5260 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005262 return( status );
5263}
Janos Follath71a4c912019-06-11 09:14:47 +01005264#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005265
Gilles Peskine969c5d62019-01-16 15:53:06 +01005266static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005269{
Janos Follath5fe19732019-06-20 15:09:30 +01005270 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5271 * initialisers for this union leave some bytes unspecified.) */
5272 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5273
Gilles Peskine969c5d62019-01-16 15:53:06 +01005274 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005276 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5277 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5278 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005280 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5282 if( hash_size == 0 )
5283 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005284 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5285 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005286 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005287 {
5288 return( PSA_ERROR_NOT_SUPPORTED );
5289 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005291 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005292 }
5293#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005294 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005296}
5297
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005299 psa_algorithm_t alg )
5300{
5301 psa_status_t status;
5302
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005304 return( PSA_ERROR_BAD_STATE );
5305
Gilles Peskine6843c292019-01-18 16:44:49 +01005306 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5307 return( PSA_ERROR_INVALID_ARGUMENT );
5308 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005309 {
5310 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005312 }
5313 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5314 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005316 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005317 else
5318 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005319
5320 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005322 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323}
5324
5325#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005326static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327 psa_algorithm_t hash_alg,
5328 psa_key_derivation_step_t step,
5329 const uint8_t *data,
5330 size_t data_length )
5331{
5332 psa_status_t status;
5333 switch( step )
5334 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005335 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005336 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005338 status = psa_hmac_setup_internal( &hkdf->hmac,
5339 data, data_length,
5340 hash_alg );
5341 if( status != PSA_SUCCESS )
5342 return( status );
5343 hkdf->state = HKDF_STATE_STARTED;
5344 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005345 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005346 /* If no salt was provided, use an empty salt. */
5347 if( hkdf->state == HKDF_STATE_INIT )
5348 {
5349 status = psa_hmac_setup_internal( &hkdf->hmac,
5350 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005351 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005352 if( status != PSA_SUCCESS )
5353 return( status );
5354 hkdf->state = HKDF_STATE_STARTED;
5355 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005356 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005357 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005358 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5359 data, data_length );
5360 if( status != PSA_SUCCESS )
5361 return( status );
5362 status = psa_hmac_finish_internal( &hkdf->hmac,
5363 hkdf->prk,
5364 sizeof( hkdf->prk ) );
5365 if( status != PSA_SUCCESS )
5366 return( status );
5367 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5368 hkdf->block_number = 0;
5369 hkdf->state = HKDF_STATE_KEYED;
5370 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005371 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005372 if( hkdf->state == HKDF_STATE_OUTPUT )
5373 return( PSA_ERROR_BAD_STATE );
5374 if( hkdf->info_set )
5375 return( PSA_ERROR_BAD_STATE );
5376 hkdf->info_length = data_length;
5377 if( data_length != 0 )
5378 {
5379 hkdf->info = mbedtls_calloc( 1, data_length );
5380 if( hkdf->info == NULL )
5381 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5382 memcpy( hkdf->info, data, data_length );
5383 }
5384 hkdf->info_set = 1;
5385 return( PSA_SUCCESS );
5386 default:
5387 return( PSA_ERROR_INVALID_ARGUMENT );
5388 }
5389}
Janos Follathb03233e2019-06-11 15:30:30 +01005390
5391#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5392static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5393 psa_algorithm_t hash_alg,
5394 psa_key_derivation_step_t step,
5395 const uint8_t *data,
5396 size_t data_length )
5397{
5398 (void) prf;
5399 (void) hash_alg;
5400 (void) step;
5401 (void) data;
5402 (void) data_length;
5403
5404 return( PSA_ERROR_INVALID_ARGUMENT );
5405}
Janos Follath6660f0e2019-06-17 08:44:03 +01005406
5407static psa_status_t psa_tls12_prf_psk_to_ms_input(
5408 psa_tls12_prf_key_derivation_t *prf,
5409 psa_algorithm_t hash_alg,
5410 psa_key_derivation_step_t step,
5411 const uint8_t *data,
5412 size_t data_length )
5413{
5414 (void) prf;
5415 (void) hash_alg;
5416 (void) step;
5417 (void) data;
5418 (void) data_length;
5419
5420 return( PSA_ERROR_INVALID_ARGUMENT );
5421}
Janos Follathb03233e2019-06-11 15:30:30 +01005422#else
Janos Follathf08e2652019-06-13 09:05:41 +01005423static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5424 const uint8_t *data,
5425 size_t data_length )
5426{
5427 if( prf->state != TLS12_PRF_STATE_INIT )
5428 return( PSA_ERROR_BAD_STATE );
5429
Janos Follathd6dce9f2019-07-04 09:11:38 +01005430 if( data_length != 0 )
5431 {
5432 prf->seed = mbedtls_calloc( 1, data_length );
5433 if( prf->seed == NULL )
5434 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005435
Janos Follathd6dce9f2019-07-04 09:11:38 +01005436 memcpy( prf->seed, data, data_length );
5437 prf->seed_length = data_length;
5438 }
Janos Follathf08e2652019-06-13 09:05:41 +01005439
5440 prf->state = TLS12_PRF_STATE_SEED_SET;
5441
5442 return( PSA_SUCCESS );
5443}
5444
Janos Follath81550542019-06-13 14:26:34 +01005445static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5446 psa_algorithm_t hash_alg,
5447 const uint8_t *data,
5448 size_t data_length )
5449{
5450 psa_status_t status;
5451 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5452 return( PSA_ERROR_BAD_STATE );
5453
5454 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5455 if( status != PSA_SUCCESS )
5456 return( status );
5457
5458 prf->state = TLS12_PRF_STATE_KEY_SET;
5459
5460 return( PSA_SUCCESS );
5461}
5462
Janos Follath6660f0e2019-06-17 08:44:03 +01005463static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5464 psa_tls12_prf_key_derivation_t *prf,
5465 psa_algorithm_t hash_alg,
5466 const uint8_t *data,
5467 size_t data_length )
5468{
5469 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005470 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5471 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005472
5473 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5474 return( PSA_ERROR_INVALID_ARGUMENT );
5475
5476 /* Quoting RFC 4279, Section 2:
5477 *
5478 * The premaster secret is formed as follows: if the PSK is N octets
5479 * long, concatenate a uint16 with the value N, N zero octets, a second
5480 * uint16 with the value N, and the PSK itself.
5481 */
5482
Janos Follath40e13932019-06-26 13:22:29 +01005483 *cur++ = ( data_length >> 8 ) & 0xff;
5484 *cur++ = ( data_length >> 0 ) & 0xff;
5485 memset( cur, 0, data_length );
5486 cur += data_length;
5487 *cur++ = pms[0];
5488 *cur++ = pms[1];
5489 memcpy( cur, data, data_length );
5490 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005491
Janos Follath40e13932019-06-26 13:22:29 +01005492 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005493
5494 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5495 return( status );
5496}
5497
Janos Follath63028dd2019-06-13 09:15:47 +01005498static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5499 const uint8_t *data,
5500 size_t data_length )
5501{
5502 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5503 return( PSA_ERROR_BAD_STATE );
5504
Janos Follathd6dce9f2019-07-04 09:11:38 +01005505 if( data_length != 0 )
5506 {
5507 prf->label = mbedtls_calloc( 1, data_length );
5508 if( prf->label == NULL )
5509 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005510
Janos Follathd6dce9f2019-07-04 09:11:38 +01005511 memcpy( prf->label, data, data_length );
5512 prf->label_length = data_length;
5513 }
Janos Follath63028dd2019-06-13 09:15:47 +01005514
5515 prf->state = TLS12_PRF_STATE_LABEL_SET;
5516
5517 return( PSA_SUCCESS );
5518}
5519
Janos Follathb03233e2019-06-11 15:30:30 +01005520static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5521 psa_algorithm_t hash_alg,
5522 psa_key_derivation_step_t step,
5523 const uint8_t *data,
5524 size_t data_length )
5525{
Janos Follathb03233e2019-06-11 15:30:30 +01005526 switch( step )
5527 {
Janos Follathf08e2652019-06-13 09:05:41 +01005528 case PSA_KEY_DERIVATION_INPUT_SEED:
5529 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005530 case PSA_KEY_DERIVATION_INPUT_SECRET:
5531 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005532 case PSA_KEY_DERIVATION_INPUT_LABEL:
5533 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005534 default:
5535 return( PSA_ERROR_INVALID_ARGUMENT );
5536 }
5537}
Janos Follath6660f0e2019-06-17 08:44:03 +01005538
5539static psa_status_t psa_tls12_prf_psk_to_ms_input(
5540 psa_tls12_prf_key_derivation_t *prf,
5541 psa_algorithm_t hash_alg,
5542 psa_key_derivation_step_t step,
5543 const uint8_t *data,
5544 size_t data_length )
5545{
5546 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005547 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005548 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5549 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005550 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005551
5552 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5553}
Janos Follathb03233e2019-06-11 15:30:30 +01005554#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005555#endif /* MBEDTLS_MD_C */
5556
Janos Follathb80a94e2019-06-12 15:54:46 +01005557static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005559 psa_key_derivation_step_t step,
5560 const uint8_t *data,
5561 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005562{
5563 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005564 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005565
5566#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005567 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005568 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005569 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005570 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571 step, data, data_length );
5572 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005573 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005575#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005576 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005577 {
Janos Follathb03233e2019-06-11 15:30:30 +01005578 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5579 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5580 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005581 }
5582 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5583 {
5584 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5585 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5586 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005587 }
5588 else
5589#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005590 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005592 return( PSA_ERROR_BAD_STATE );
5593 }
5594
5595 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005597 return( status );
5598}
5599
Janos Follath51f4a0f2019-06-14 11:35:55 +01005600psa_status_t psa_key_derivation_input_bytes(
5601 psa_key_derivation_operation_t *operation,
5602 psa_key_derivation_step_t step,
5603 const uint8_t *data,
5604 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005605{
Janos Follathc5621512019-06-14 11:27:57 +01005606 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5607 return( PSA_ERROR_INVALID_ARGUMENT );
5608
5609 return( psa_key_derivation_input_internal( operation, step,
5610 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005611}
5612
Janos Follath51f4a0f2019-06-14 11:35:55 +01005613psa_status_t psa_key_derivation_input_key(
5614 psa_key_derivation_operation_t *operation,
5615 psa_key_derivation_step_t step,
5616 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617{
5618 psa_key_slot_t *slot;
5619 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005620 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005621 PSA_KEY_USAGE_DERIVE,
5622 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005623 if( status != PSA_SUCCESS )
5624 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005625 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005626 return( PSA_ERROR_INVALID_ARGUMENT );
5627 /* Don't allow a key to be used as an input that is usually public.
5628 * This is debatable. It's ok from a cryptographic perspective to
5629 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005630 * the material should be dedicated to a particular input step,
5631 * otherwise this may allow the key to be used in an unintended way
5632 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005633 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005634 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005635 return( psa_key_derivation_input_internal( operation,
5636 step,
5637 slot->data.raw.data,
5638 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005639}
5640
Gilles Peskineea0fb492018-07-12 17:17:20 +02005641
5642
5643/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005644/* Key agreement */
5645/****************************************************************/
5646
Gilles Peskinea05219c2018-11-16 16:02:56 +01005647#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005648static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5649 size_t peer_key_length,
5650 const mbedtls_ecp_keypair *our_key,
5651 uint8_t *shared_secret,
5652 size_t shared_secret_size,
5653 size_t *shared_secret_length )
5654{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005655 mbedtls_ecp_keypair *their_key = NULL;
5656 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005657 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005658 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005659
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005660 status = psa_import_ec_public_key(
5661 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5662 peer_key, peer_key_length,
5663 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005664 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005665 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005666
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005667 status = mbedtls_to_psa_error(
5668 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5669 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005670 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005671 status = mbedtls_to_psa_error(
5672 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5673 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005674 goto exit;
5675
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005676 status = mbedtls_to_psa_error(
5677 mbedtls_ecdh_calc_secret( &ecdh,
5678 shared_secret_length,
5679 shared_secret, shared_secret_size,
5680 mbedtls_ctr_drbg_random,
5681 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005682
5683exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005684 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005685 mbedtls_ecp_keypair_free( their_key );
5686 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005687 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005688}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005689#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005690
Gilles Peskine01d718c2018-09-18 12:01:02 +02005691#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5692
Gilles Peskine0216fe12019-04-11 21:23:21 +02005693static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5694 psa_key_slot_t *private_key,
5695 const uint8_t *peer_key,
5696 size_t peer_key_length,
5697 uint8_t *shared_secret,
5698 size_t shared_secret_size,
5699 size_t *shared_secret_length )
5700{
5701 switch( alg )
5702 {
5703#if defined(MBEDTLS_ECDH_C)
5704 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005705 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005706 return( PSA_ERROR_INVALID_ARGUMENT );
5707 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5708 private_key->data.ecp,
5709 shared_secret, shared_secret_size,
5710 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005711#endif /* MBEDTLS_ECDH_C */
5712 default:
5713 (void) private_key;
5714 (void) peer_key;
5715 (void) peer_key_length;
5716 (void) shared_secret;
5717 (void) shared_secret_size;
5718 (void) shared_secret_length;
5719 return( PSA_ERROR_NOT_SUPPORTED );
5720 }
5721}
5722
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005723/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005724 * to potentially free embedded data structures and wipe confidential data.
5725 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005727 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005728 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005729 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005730 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005731{
5732 psa_status_t status;
5733 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5734 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005736
5737 /* Step 1: run the secret agreement algorithm to generate the shared
5738 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005739 status = psa_key_agreement_raw_internal( ka_alg,
5740 private_key,
5741 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005742 shared_secret,
5743 sizeof( shared_secret ),
5744 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005745 if( status != PSA_SUCCESS )
5746 goto exit;
5747
5748 /* Step 2: set up the key derivation to generate key material from
5749 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005750 status = psa_key_derivation_input_internal( operation, step,
5751 shared_secret,
5752 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005753
Gilles Peskine01d718c2018-09-18 12:01:02 +02005754exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005755 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005756 return( status );
5757}
5758
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005760 psa_key_derivation_step_t step,
5761 psa_key_handle_t private_key,
5762 const uint8_t *peer_key,
5763 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005764{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005765 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005766 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005767 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005768 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005769 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005770 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005771 if( status != PSA_SUCCESS )
5772 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005773 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005774 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005775 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005776 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005777 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005778 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005779}
5780
Gilles Peskinebe697d82019-05-16 18:00:41 +02005781psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5782 psa_key_handle_t private_key,
5783 const uint8_t *peer_key,
5784 size_t peer_key_length,
5785 uint8_t *output,
5786 size_t output_size,
5787 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005788{
5789 psa_key_slot_t *slot;
5790 psa_status_t status;
5791
5792 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5793 {
5794 status = PSA_ERROR_INVALID_ARGUMENT;
5795 goto exit;
5796 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005797 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005798 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005799 if( status != PSA_SUCCESS )
5800 goto exit;
5801
5802 status = psa_key_agreement_raw_internal( alg, slot,
5803 peer_key, peer_key_length,
5804 output, output_size,
5805 output_length );
5806
5807exit:
5808 if( status != PSA_SUCCESS )
5809 {
5810 /* If an error happens and is not handled properly, the output
5811 * may be used as a key to protect sensitive data. Arrange for such
5812 * a key to be random, which is likely to result in decryption or
5813 * verification errors. This is better than filling the buffer with
5814 * some constant data such as zeros, which would result in the data
5815 * being protected with a reproducible, easily knowable key.
5816 */
5817 psa_generate_random( output, output_size );
5818 *output_length = output_size;
5819 }
5820 return( status );
5821}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005822
5823
5824/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005825/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005826/****************************************************************/
5827
5828psa_status_t psa_generate_random( uint8_t *output,
5829 size_t output_size )
5830{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005831 int ret;
5832 GUARD_MODULE_INITIALIZED;
5833
Gilles Peskinef181eca2019-08-07 13:49:00 +02005834 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5835 {
5836 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5837 output,
5838 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5839 if( ret != 0 )
5840 return( mbedtls_to_psa_error( ret ) );
5841 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5842 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5843 }
5844
itayzafrir0adf0fc2018-09-06 16:24:41 +03005845 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005846 return( mbedtls_to_psa_error( ret ) );
5847}
5848
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005849#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5850#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005851
Gilles Peskine7228da22019-07-15 11:06:15 +02005852psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005853 size_t seed_size )
5854{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005855 if( global_data.initialized )
5856 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005857
5858 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5859 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5860 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5861 return( PSA_ERROR_INVALID_ARGUMENT );
5862
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005863 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005864}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005865#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005866
Gilles Peskinee56e8782019-04-26 17:34:02 +02005867#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5868static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5869 size_t domain_parameters_size,
5870 int *exponent )
5871{
5872 size_t i;
5873 uint32_t acc = 0;
5874
5875 if( domain_parameters_size == 0 )
5876 {
5877 *exponent = 65537;
5878 return( PSA_SUCCESS );
5879 }
5880
5881 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5882 * support values that fit in a 32-bit integer, which is larger than
5883 * int on just about every platform anyway. */
5884 if( domain_parameters_size > sizeof( acc ) )
5885 return( PSA_ERROR_NOT_SUPPORTED );
5886 for( i = 0; i < domain_parameters_size; i++ )
5887 acc = ( acc << 8 ) | domain_parameters[i];
5888 if( acc > INT_MAX )
5889 return( PSA_ERROR_NOT_SUPPORTED );
5890 *exponent = acc;
5891 return( PSA_SUCCESS );
5892}
5893#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5894
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005895static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005896 psa_key_slot_t *slot, size_t bits,
5897 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005898{
Gilles Peskine8e338702019-07-30 20:06:31 +02005899 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005900
Gilles Peskinee56e8782019-04-26 17:34:02 +02005901 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005902 return( PSA_ERROR_INVALID_ARGUMENT );
5903
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005904 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005905 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005906 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005907 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005908 if( status != PSA_SUCCESS )
5909 return( status );
5910 status = psa_generate_random( slot->data.raw.data,
5911 slot->data.raw.bytes );
5912 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005913 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005914#if defined(MBEDTLS_DES_C)
5915 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005916 psa_des_set_key_parity( slot->data.raw.data,
5917 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005918#endif /* MBEDTLS_DES_C */
5919 }
5920 else
5921
5922#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005923 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005924 {
5925 mbedtls_rsa_context *rsa;
5926 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005927 int exponent;
5928 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005929 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5930 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005931 /* Accept only byte-aligned keys, for the same reasons as
5932 * in psa_import_rsa_key(). */
5933 if( bits % 8 != 0 )
5934 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005935 status = psa_read_rsa_exponent( domain_parameters,
5936 domain_parameters_size,
5937 &exponent );
5938 if( status != PSA_SUCCESS )
5939 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005940 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5941 if( rsa == NULL )
5942 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5943 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5944 ret = mbedtls_rsa_gen_key( rsa,
5945 mbedtls_ctr_drbg_random,
5946 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005947 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005948 exponent );
5949 if( ret != 0 )
5950 {
5951 mbedtls_rsa_free( rsa );
5952 mbedtls_free( rsa );
5953 return( mbedtls_to_psa_error( ret ) );
5954 }
5955 slot->data.rsa = rsa;
5956 }
5957 else
5958#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5959
5960#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005961 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005962 {
5963 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5964 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5965 const mbedtls_ecp_curve_info *curve_info =
5966 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5967 mbedtls_ecp_keypair *ecp;
5968 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005969 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005970 return( PSA_ERROR_NOT_SUPPORTED );
5971 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5972 return( PSA_ERROR_NOT_SUPPORTED );
5973 if( curve_info->bit_size != bits )
5974 return( PSA_ERROR_INVALID_ARGUMENT );
5975 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5976 if( ecp == NULL )
5977 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5978 mbedtls_ecp_keypair_init( ecp );
5979 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5980 mbedtls_ctr_drbg_random,
5981 &global_data.ctr_drbg );
5982 if( ret != 0 )
5983 {
5984 mbedtls_ecp_keypair_free( ecp );
5985 mbedtls_free( ecp );
5986 return( mbedtls_to_psa_error( ret ) );
5987 }
5988 slot->data.ecp = ecp;
5989 }
5990 else
5991#endif /* MBEDTLS_ECP_C */
5992
5993 return( PSA_ERROR_NOT_SUPPORTED );
5994
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005995 return( PSA_SUCCESS );
5996}
5997
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005998psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005999 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006000{
6001 psa_status_t status;
6002 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006003 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006004
Gilles Peskinedf179142019-07-15 22:02:14 +02006005 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6006 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006007 if( status != PSA_SUCCESS )
6008 goto exit;
6009
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006010#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6011 if( driver != NULL )
6012 {
Gilles Peskine11792082019-08-06 18:36:36 +02006013 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6014 size_t pubkey_length = 0; /* We don't support this feature yet */
6015 if( drv->key_management == NULL ||
6016 drv->key_management->p_generate == NULL )
6017 {
6018 status = PSA_ERROR_NOT_SUPPORTED;
6019 goto exit;
6020 }
6021 status = drv->key_management->p_generate(
6022 psa_get_se_driver_context( driver ),
6023 slot->data.se.slot_number, attributes,
6024 NULL, 0, &pubkey_length );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006025 }
Gilles Peskine11792082019-08-06 18:36:36 +02006026 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006028 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006029 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006030 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006031 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006032 }
Gilles Peskine11792082019-08-06 18:36:36 +02006033
6034exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006035 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006036 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006037 if( status != PSA_SUCCESS )
6038 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006039 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006040 *handle = 0;
6041 }
6042 return( status );
6043}
6044
6045
Gilles Peskine05d69892018-06-19 22:00:52 +02006046
6047/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01006048/* Module setup */
6049/****************************************************************/
6050
Gilles Peskine5e769522018-11-20 21:59:56 +01006051psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6052 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6053 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6054{
6055 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6056 return( PSA_ERROR_BAD_STATE );
6057 global_data.entropy_init = entropy_init;
6058 global_data.entropy_free = entropy_free;
6059 return( PSA_SUCCESS );
6060}
6061
Gilles Peskinee59236f2018-01-27 23:32:46 +01006062void mbedtls_psa_crypto_free( void )
6063{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006064 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006065 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6066 {
6067 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006068 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006069 }
6070 /* Wipe all remaining data, including configuration.
6071 * In particular, this sets all state indicator to the value
6072 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006073 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006074#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006075 /* Unregister all secure element drivers, so that we restart from
6076 * a pristine state. */
6077 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079}
6080
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006081#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6082/** Recover a transaction that was interrupted by a power failure.
6083 *
6084 * This function is called during initialization, before psa_crypto_init()
6085 * returns. If this function returns a failure status, the initialization
6086 * fails.
6087 */
6088static psa_status_t psa_crypto_recover_transaction(
6089 const psa_crypto_transaction_t *transaction )
6090{
6091 switch( transaction->unknown.type )
6092 {
6093 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6094 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006095 /* TODO - fall through to the failure case until this
6096 * is implemented.
6097 * https://github.com/ARMmbed/mbed-crypto/issues/218
6098 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006099 default:
6100 /* We found an unsupported transaction in the storage.
6101 * We don't know what state the storage is in. Give up. */
6102 return( PSA_ERROR_STORAGE_FAILURE );
6103 }
6104}
6105#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6106
Gilles Peskinee59236f2018-01-27 23:32:46 +01006107psa_status_t psa_crypto_init( void )
6108{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006109 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006110 const unsigned char drbg_seed[] = "PSA";
6111
Gilles Peskinec6b69072018-11-20 21:42:52 +01006112 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113 if( global_data.initialized != 0 )
6114 return( PSA_SUCCESS );
6115
Gilles Peskine5e769522018-11-20 21:59:56 +01006116 /* Set default configuration if
6117 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6118 if( global_data.entropy_init == NULL )
6119 global_data.entropy_init = mbedtls_entropy_init;
6120 if( global_data.entropy_free == NULL )
6121 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006122
Gilles Peskinec6b69072018-11-20 21:42:52 +01006123 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006124 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006125#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6126 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6127 /* The PSA entropy injection feature depends on using NV seed as an entropy
6128 * source. Add NV seed as an entropy source for PSA entropy injection. */
6129 mbedtls_entropy_add_source( &global_data.entropy,
6130 mbedtls_nv_seed_poll, NULL,
6131 MBEDTLS_ENTROPY_BLOCK_SIZE,
6132 MBEDTLS_ENTROPY_SOURCE_STRONG );
6133#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006135 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006136 status = mbedtls_to_psa_error(
6137 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6138 mbedtls_entropy_func,
6139 &global_data.entropy,
6140 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6141 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006142 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006143 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006144
Gilles Peskine66fb1262018-12-10 16:29:04 +01006145 status = psa_initialize_key_slots( );
6146 if( status != PSA_SUCCESS )
6147 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006148
Gilles Peskine4b734222019-07-24 15:56:31 +02006149#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006150 status = psa_crypto_load_transaction( );
6151 if( status == PSA_SUCCESS )
6152 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006153 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6154 if( status != PSA_SUCCESS )
6155 goto exit;
6156 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006157 }
6158 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6159 {
6160 /* There's no transaction to complete. It's all good. */
6161 status = PSA_SUCCESS;
6162 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006163#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006164
Gilles Peskinec6b69072018-11-20 21:42:52 +01006165 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006166 global_data.initialized = 1;
6167
Gilles Peskinee59236f2018-01-27 23:32:46 +01006168exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006169 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006170 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006171 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006172}
6173
6174#endif /* MBEDTLS_PSA_CRYPTO_C */