blob: e6ef7f747465afd80e5e620b96dfbb1ffc1cac68 [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 Amero25384a22019-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)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#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)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#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:
454#if defined(MBEDTLS_MD_C)
455 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200456#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
495 /* Allocate memory for the key */
496 raw->bytes = PSA_BITS_TO_BYTES( bits );
497 raw->data = mbedtls_calloc( 1, raw->bytes );
498 if( raw->data == NULL )
499 {
500 raw->bytes = 0;
501 return( PSA_ERROR_INSUFFICIENT_MEMORY );
502 }
503 return( PSA_SUCCESS );
504}
505
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100507/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
508 * that are not a multiple of 8) well. For example, there is only
509 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
510 * way to return the exact bit size of a key.
511 * To keep things simple, reject non-byte-aligned key sizes. */
512static psa_status_t psa_check_rsa_key_byte_aligned(
513 const mbedtls_rsa_context *rsa )
514{
515 mbedtls_mpi n;
516 psa_status_t status;
517 mbedtls_mpi_init( &n );
518 status = mbedtls_to_psa_error(
519 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
520 if( status == PSA_SUCCESS )
521 {
522 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
523 status = PSA_ERROR_NOT_SUPPORTED;
524 }
525 mbedtls_mpi_free( &n );
526 return( status );
527}
528
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000529static psa_status_t psa_import_rsa_key( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 mbedtls_rsa_context **p_rsa )
533{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534 psa_status_t status;
535 mbedtls_pk_context pk;
536 mbedtls_rsa_context *rsa;
537 size_t bits;
538
539 mbedtls_pk_init( &pk );
540
541 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
553 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
559 rsa = mbedtls_pk_rsa( pk );
560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
569 status = psa_check_rsa_key_byte_aligned( rsa );
570
571exit:
572 /* Free the content of the pk object only on error. */
573 if( status != PSA_SUCCESS )
574 {
575 mbedtls_pk_free( &pk );
576 return( status );
577 }
578
579 /* On success, store the content of the object in the RSA context. */
580 *p_rsa = rsa;
581
582 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583}
584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586#if defined(MBEDTLS_ECP_C)
587
588/* Import a public key given as the uncompressed representation defined by SEC1
589 * 2.3.3 as the content of an ECPoint. */
590static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
591 const uint8_t *data,
592 size_t data_length,
593 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000596 mbedtls_ecp_keypair *ecp = NULL;
597 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598
599 *p_ecp = NULL;
600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
601 if( ecp == NULL )
602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
603 mbedtls_ecp_keypair_init( ecp );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the public value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
613 data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616
617 /* Check that the point is on the curve. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 *p_ecp = ecp;
624 return( PSA_SUCCESS );
625
626exit:
627 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair_free( ecp );
630 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
637/* Import a private key given as a byte string which is the private value
638 * in big-endian order. */
639static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
640 const uint8_t *data,
641 size_t data_length,
642 mbedtls_ecp_keypair **p_ecp )
643{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 mbedtls_ecp_keypair *ecp = NULL;
646 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200648 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649 return( PSA_ERROR_INVALID_ARGUMENT );
650
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 *p_ecp = NULL;
652 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
653 if( ecp == NULL )
654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000655 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
657 /* Load the group. */
658 status = mbedtls_to_psa_error(
659 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662 /* Load the secret value. */
663 status = mbedtls_to_psa_error(
664 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
665 if( status != PSA_SUCCESS )
666 goto exit;
667 /* Validate the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
670 if( status != PSA_SUCCESS )
671 goto exit;
672 /* Calculate the public key from the private key. */
673 status = mbedtls_to_psa_error(
674 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
675 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678
679 *p_ecp = ecp;
680 return( PSA_SUCCESS );
681
682exit:
683 if( ecp != NULL )
684 {
685 mbedtls_ecp_keypair_free( ecp );
686 mbedtls_free( ecp );
687 }
688 return( status );
689}
690#endif /* defined(MBEDTLS_ECP_C) */
691
Gilles Peskineb46bef22019-07-30 21:32:04 +0200692
693/** Return the size of the key in the given slot, in bits.
694 *
695 * \param[in] slot A key slot.
696 *
697 * \return The key size in bits, read from the metadata in the slot.
698 */
699static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
700{
701 return( slot->attr.bits );
702}
703
704/** Calculate the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot containing a transparent key.
707 *
708 * \return The key size in bits, calculated from the key data.
709 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200710static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 size_t bits = 0; /* return 0 on an empty slot */
713
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
718 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#endif /* defined(MBEDTLS_RSA_C) */
720#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
722 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724
725 /* We know that the size fits in psa_key_bits_t thanks to checks
726 * when the key was created. */
727 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728}
729
Gilles Peskine8e338702019-07-30 20:06:31 +0200730/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100731 * previously. This function assumes that the slot does not contain
732 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100733psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
734 const uint8_t *data,
735 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Gilles Peskine8e338702019-07-30 20:06:31 +0200739 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200742 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 if( data_length > SIZE_MAX / 8 )
744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750 &slot->data.raw );
751 if( status != PSA_SUCCESS )
752 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200753 if( data_length != 0 )
754 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 }
756 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100757#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200758 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 data, data_length,
762 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000766 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 data, data_length,
769 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000777 data, data_length,
778 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 }
780 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
783 return( PSA_ERROR_NOT_SUPPORTED );
784 }
Darryl Green940d72c2018-07-13 13:18:51 +0100785
Gilles Peskineb46bef22019-07-30 21:32:04 +0200786 if( status == PSA_SUCCESS )
787 {
788 /* Write the actual key size to the slot.
789 * psa_start_key_creation() wrote the size declared by the
790 * caller, which may be 0 (meaning unspecified) or wrong. */
791 slot->attr.bits = psa_calculate_key_bits( slot );
792 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 return( status );
794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
801 psa_algorithm_t alg1,
802 psa_algorithm_t alg2 )
803{
Gilles Peskine549ea862019-05-22 11:45:59 +0200804 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 if( alg1 == alg2 )
806 return( alg1 );
807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
809 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
810 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
811 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
812 {
813 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
814 return( alg2 );
815 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
816 return( alg1 );
817 }
818 /* If the policies are incompatible, allow nothing. */
819 return( 0 );
820}
821
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
823 psa_algorithm_t requested_alg )
824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826 if( requested_alg == policy_alg )
827 return( 1 );
828 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 * and requested_alg is the same hash-and-sign family with any hash,
830 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
832 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
833 {
834 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
835 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
836 }
837 /* If it isn't permitted, it's forbidden. */
838 return( 0 );
839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
844 */
845static int psa_key_policy_permits( const psa_key_policy_t *policy,
846 psa_algorithm_t alg )
847{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200848 return( psa_key_algorithm_permits( policy->alg, alg ) ||
849 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850}
851
Gilles Peskinef603c712019-01-19 13:40:11 +0100852/** Restrict a key policy based on a constraint.
853 *
854 * \param[in,out] policy The policy to restrict.
855 * \param[in] constraint The policy constraint to apply.
856 *
857 * \retval #PSA_SUCCESS
858 * \c *policy contains the intersection of the original value of
859 * \c *policy and \c *constraint.
860 * \retval #PSA_ERROR_INVALID_ARGUMENT
861 * \c *policy and \c *constraint are incompatible.
862 * \c *policy is unchanged.
863 */
864static psa_status_t psa_restrict_key_policy(
865 psa_key_policy_t *policy,
866 const psa_key_policy_t *constraint )
867{
868 psa_algorithm_t intersection_alg =
869 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 psa_algorithm_t intersection_alg2 =
871 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Darryl Green06fd18d2018-07-16 11:21:11 +0100882/** Retrieve a slot which must contain a key. The key must have allow all the
883 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
884 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100885static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100887 psa_key_usage_t usage,
888 psa_algorithm_t alg )
889{
890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100891 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100892
893 *p_slot = NULL;
894
Gilles Peskinec5487a82018-12-03 18:08:14 +0100895 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100896 if( status != PSA_SUCCESS )
897 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 /* Enforce that usage policy for the key slot contains all the flags
900 * required by the usage parameter. There is one exception: public
901 * keys can always be exported, so we treat public key objects as
902 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200903 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907
908 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
911
912 *p_slot = slot;
913 return( PSA_SUCCESS );
914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916/** Retrieve a slot which must contain a transparent key.
917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200921 * This is a temporary function to use instead of psa_get_key_from_slot()
922 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923 */
924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
925static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
926 psa_key_slot_t **p_slot,
927 psa_key_usage_t usage,
928 psa_algorithm_t alg )
929{
930 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200933 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 {
935 *p_slot = NULL;
936 return( PSA_ERROR_NOT_SUPPORTED );
937 }
938 return( PSA_SUCCESS );
939}
940#else /* MBEDTLS_PSA_CRYPTO_SE_C */
941/* With no secure element support, all keys are transparent. */
942#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
943 psa_get_key_from_slot( handle, p_slot, usage, alg )
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
945
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100946/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000948{
Gilles Peskine73167e12019-07-12 23:44:37 +0200949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
950 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000951 {
952 /* No key material to clean. */
953 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200954 else
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200956 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000957 {
958 /* No key material to clean. */
959 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 mbedtls_free( slot->data.raw.data );
963 }
964 else
965#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_rsa_free( slot->data.rsa );
969 mbedtls_free( slot->data.rsa );
970 }
971 else
972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_ecp_keypair_free( slot->data.ecp );
977 mbedtls_free( slot->data.ecp );
978 }
979 else
980#endif /* defined(MBEDTLS_ECP_C) */
981 {
982 /* Shouldn't happen: the key type is not any type that we
983 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200984 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000985 }
986
987 return( PSA_SUCCESS );
988}
989
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990/** Completely wipe a slot in memory, including its policy.
991 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100992psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993{
994 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200995 /* Multipart operations may still be using the key. This is safe
996 * because all multipart operation objects are independent from
997 * the key slot: if they need to access the key after the setup
998 * phase, they have a copy of the key. Note that this means that
999 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000 /* At this point, key material and other type-specific content has
1001 * been wiped. Clear remaining metadata. We can call memset and not
1002 * zeroize because the metadata is not particularly sensitive. */
1003 memset( slot, 0, sizeof( *slot ) );
1004 return( status );
1005}
1006
Gilles Peskinec5487a82018-12-03 18:08:14 +01001007psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001009 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001010 psa_status_t status; /* status of the last operation */
1011 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 psa_se_drv_table_entry_t *driver;
1014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 if( status != PSA_SUCCESS )
1018 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001019
1020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001023 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001024 /* For a key in a secure element, we need to do three things:
1025 * remove the key file in internal storage, destroy the
1026 * key inside the secure element, and update the driver's
1027 * persistent data. Start a transaction that will encompass these
1028 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 status = psa_crypto_save_transaction( );
1034 if( status != PSA_SUCCESS )
1035 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001036 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001037 /* We should still try to destroy the key in the secure
1038 * element and the key metadata in storage. This is especially
1039 * important if the error is that the storage is full.
1040 * But how to do it exactly without risking an inconsistent
1041 * state after a reset?
1042 * https://github.com/ARMmbed/mbed-crypto/issues/215
1043 */
1044 overall_status = status;
1045 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 }
1047
Gilles Peskine354f7672019-07-12 23:46:38 +02001048 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001049 if( overall_status == PSA_SUCCESS )
1050 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001055 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001057 status = psa_destroy_persistent_key( slot->attr.id );
1058 if( overall_status == PSA_SUCCESS )
1059 overall_status = status;
1060
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001061 /* TODO: other slots may have a copy of the same key. We should
1062 * invalidate them.
1063 * https://github.com/ARMmbed/mbed-crypto/issues/214
1064 */
Darryl Greend49a4992018-06-18 17:27:26 +01001065 }
1066#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001067
Gilles Peskinefc762652019-07-22 19:30:34 +02001068#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1069 if( driver != NULL )
1070 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001071 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074 status = psa_crypto_stop_transaction( );
1075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001077 }
1078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1079
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1081exit:
1082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001084 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
1087 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088}
1089
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001091{
Gilles Peskineb699f072019-04-26 16:06:02 +02001092 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001094}
1095
Gilles Peskineb699f072019-04-26 16:06:02 +02001096psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1097 psa_key_type_t type,
1098 const uint8_t *data,
1099 size_t data_length )
1100{
1101 uint8_t *copy = NULL;
1102
1103 if( data_length != 0 )
1104 {
1105 copy = mbedtls_calloc( 1, data_length );
1106 if( copy == NULL )
1107 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1108 memcpy( copy, data, data_length );
1109 }
1110 /* After this point, this function is guaranteed to succeed, so it
1111 * can start modifying `*attributes`. */
1112
1113 if( attributes->domain_parameters != NULL )
1114 {
1115 mbedtls_free( attributes->domain_parameters );
1116 attributes->domain_parameters = NULL;
1117 attributes->domain_parameters_size = 0;
1118 }
1119
1120 attributes->domain_parameters = copy;
1121 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001122 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001123 return( PSA_SUCCESS );
1124}
1125
1126psa_status_t psa_get_key_domain_parameters(
1127 const psa_key_attributes_t *attributes,
1128 uint8_t *data, size_t data_size, size_t *data_length )
1129{
1130 if( attributes->domain_parameters_size > data_size )
1131 return( PSA_ERROR_BUFFER_TOO_SMALL );
1132 *data_length = attributes->domain_parameters_size;
1133 if( attributes->domain_parameters_size != 0 )
1134 memcpy( data, attributes->domain_parameters,
1135 attributes->domain_parameters_size );
1136 return( PSA_SUCCESS );
1137}
1138
1139#if defined(MBEDTLS_RSA_C)
1140static psa_status_t psa_get_rsa_public_exponent(
1141 const mbedtls_rsa_context *rsa,
1142 psa_key_attributes_t *attributes )
1143{
1144 mbedtls_mpi mpi;
1145 int ret;
1146 uint8_t *buffer = NULL;
1147 size_t buflen;
1148 mbedtls_mpi_init( &mpi );
1149
1150 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1151 if( ret != 0 )
1152 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001153 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1154 {
1155 /* It's the default value, which is reported as an empty string,
1156 * so there's nothing to do. */
1157 goto exit;
1158 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001159
1160 buflen = mbedtls_mpi_size( &mpi );
1161 buffer = mbedtls_calloc( 1, buflen );
1162 if( buffer == NULL )
1163 {
1164 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1165 goto exit;
1166 }
1167 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1168 if( ret != 0 )
1169 goto exit;
1170 attributes->domain_parameters = buffer;
1171 attributes->domain_parameters_size = buflen;
1172
1173exit:
1174 mbedtls_mpi_free( &mpi );
1175 if( ret != 0 )
1176 mbedtls_free( buffer );
1177 return( mbedtls_to_psa_error( ret ) );
1178}
1179#endif /* MBEDTLS_RSA_C */
1180
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001181/** Retrieve all the publicly-accessible attributes of a key.
1182 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1184 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001187 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189 psa_reset_key_attributes( attributes );
1190
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001192 if( status != PSA_SUCCESS )
1193 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001194
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001195 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001196 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1197 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1198
1199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1200 if( psa_key_slot_is_external( slot ) )
1201 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
Gilles Peskine8e338702019-07-30 20:06:31 +02001204 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 {
1206#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001207 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001210 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001211 * is not yet implemented.
1212 * https://github.com/ARMmbed/mbed-crypto/issues/216
1213 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001214 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001215 break;
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1218 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001219#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 default:
1221 /* Nothing else to do. */
1222 break;
1223 }
1224
1225 if( status != PSA_SUCCESS )
1226 psa_reset_key_attributes( attributes );
1227 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228}
1229
Gilles Peskinec8000c02019-08-02 20:15:51 +02001230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1231psa_status_t psa_get_key_slot_number(
1232 const psa_key_attributes_t *attributes,
1233 psa_key_slot_number_t *slot_number )
1234{
1235 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1236 {
1237 *slot_number = attributes->slot_number;
1238 return( PSA_SUCCESS );
1239 }
1240 else
1241 return( PSA_ERROR_INVALID_ARGUMENT );
1242}
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1244
Jaeden Ameroccdce902019-01-10 11:42:27 +00001245#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001246static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1247 unsigned char *buf, size_t size )
1248{
1249 int ret;
1250 unsigned char *c;
1251 size_t len = 0;
1252
1253 c = buf + size;
1254
1255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1256
1257 return( (int) len );
1258}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001260
Gilles Peskinef603c712019-01-19 13:40:11 +01001261static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1262 uint8_t *data,
1263 size_t data_size,
1264 size_t *data_length,
1265 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266{
Gilles Peskine5d309672019-07-12 23:47:28 +02001267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1268 const psa_drv_se_t *drv;
1269 psa_drv_se_context_t *drv_context;
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1271
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001272 *data_length = 0;
1273
Gilles Peskine8e338702019-07-30 20:06:31 +02001274 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001275 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001276
Gilles Peskinef9168942019-09-12 19:20:29 +02001277 /* Reject a zero-length output buffer now, since this can never be a
1278 * valid key representation. This way we know that data must be a valid
1279 * pointer and we can do things like memset(data, ..., data_size). */
1280 if( data_size == 0 )
1281 return( PSA_ERROR_BUFFER_TOO_SMALL );
1282
Gilles Peskine5d309672019-07-12 23:47:28 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001285 {
1286 psa_drv_se_export_key_t method;
1287 if( drv->key_management == NULL )
1288 return( PSA_ERROR_NOT_SUPPORTED );
1289 method = ( export_public_key ?
1290 drv->key_management->p_export_public :
1291 drv->key_management->p_export );
1292 if( method == NULL )
1293 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001294 return( method( drv_context,
1295 slot->data.se.slot_number,
1296 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001297 }
1298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1299
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301 {
1302 if( slot->data.raw.bytes > data_size )
1303 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001304 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1305 memset( data + slot->data.raw.bytes, 0,
1306 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 *data_length = slot->data.raw.bytes;
1308 return( PSA_SUCCESS );
1309 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001310#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001312 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001313 psa_status_t status;
1314
Gilles Peskineb46bef22019-07-30 21:32:04 +02001315 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316 if( bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
1318 status = mbedtls_to_psa_error(
1319 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1320 if( status != PSA_SUCCESS )
1321 return( status );
1322 memset( data + bytes, 0, data_size - bytes );
1323 *data_length = bytes;
1324 return( PSA_SUCCESS );
1325 }
1326#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 else
Moran Peker17e36e12018-05-02 12:55:20 +03001328 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001329#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001330 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1331 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001332 {
Moran Pekera998bc62018-04-16 18:16:20 +03001333 mbedtls_pk_context pk;
1334 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001335 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001336 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001337#if defined(MBEDTLS_RSA_C)
1338 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001339 pk.pk_info = &mbedtls_rsa_info;
1340 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#else
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001344 }
1345 else
1346 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001347#if defined(MBEDTLS_ECP_C)
1348 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001349 pk.pk_info = &mbedtls_eckey_info;
1350 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#else
1352 return( PSA_ERROR_NOT_SUPPORTED );
1353#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001354 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001355 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001356 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001357 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001358 }
Moran Peker17e36e12018-05-02 12:55:20 +03001359 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001360 {
Moran Peker17e36e12018-05-02 12:55:20 +03001361 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 }
Moran Peker60364322018-04-29 11:34:58 +03001363 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001364 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001365 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001366 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001368 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1369 * Move the data to the beginning and erase remaining data
1370 * at the original location. */
1371 if( 2 * (size_t) ret <= data_size )
1372 {
1373 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001374 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001375 }
1376 else if( (size_t) ret < data_size )
1377 {
1378 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 }
Moran Pekera998bc62018-04-16 18:16:20 +03001381 *data_length = ret;
1382 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001383 }
1384 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001385#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001386 {
1387 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001388 it is valid for a special-purpose implementation to omit
1389 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001390 return( PSA_ERROR_NOT_SUPPORTED );
1391 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001392 }
1393}
1394
Gilles Peskinec5487a82018-12-03 18:08:14 +01001395psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001396 uint8_t *data,
1397 size_t data_size,
1398 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001400 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001401 psa_status_t status;
1402
1403 /* Set the key to empty now, so that even when there are errors, we always
1404 * set data_length to a value between 0 and data_size. On error, setting
1405 * the key to empty is a good choice because an empty key representation is
1406 * unlikely to be accepted anywhere. */
1407 *data_length = 0;
1408
1409 /* Export requires the EXPORT flag. There is an exception for public keys,
1410 * which don't require any flag, but psa_get_key_from_slot takes
1411 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001412 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001413 if( status != PSA_SUCCESS )
1414 return( status );
1415 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001416 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001417}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418
Gilles Peskinec5487a82018-12-03 18:08:14 +01001419psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001420 uint8_t *data,
1421 size_t data_size,
1422 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001423{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001424 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001425 psa_status_t status;
1426
1427 /* Set the key to empty now, so that even when there are errors, we always
1428 * set data_length to a value between 0 and data_size. On error, setting
1429 * the key to empty is a good choice because an empty key representation is
1430 * unlikely to be accepted anywhere. */
1431 *data_length = 0;
1432
1433 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001434 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001435 if( status != PSA_SUCCESS )
1436 return( status );
1437 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001438 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001439}
1440
Gilles Peskine91e8c332019-08-02 19:19:39 +02001441#if defined(static_assert)
1442static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1443 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001444static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001445 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001446static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001447 "One or more key attribute flag is listed as both internal-only and external-only" );
1448#endif
1449
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001450/** Validate that a key policy is internally well-formed.
1451 *
1452 * This function only rejects invalid policies. It does not validate the
1453 * consistency of the policy with respect to other attributes of the key
1454 * such as the key type.
1455 */
1456static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001457{
1458 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001459 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 PSA_KEY_USAGE_ENCRYPT |
1461 PSA_KEY_USAGE_DECRYPT |
1462 PSA_KEY_USAGE_SIGN |
1463 PSA_KEY_USAGE_VERIFY |
1464 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1465 return( PSA_ERROR_INVALID_ARGUMENT );
1466
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 return( PSA_SUCCESS );
1468}
1469
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001470/** Validate the internal consistency of key attributes.
1471 *
1472 * This function only rejects invalid attribute values. If does not
1473 * validate the consistency of the attributes with any key data that may
1474 * be involved in the creation of the key.
1475 *
1476 * Call this function early in the key creation process.
1477 *
1478 * \param[in] attributes Key attributes for the new key.
1479 * \param[out] p_drv On any return, the driver for the key, if any.
1480 * NULL for a transparent key.
1481 *
1482 */
1483static psa_status_t psa_validate_key_attributes(
1484 const psa_key_attributes_t *attributes,
1485 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001486{
1487 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001488
1489 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001490 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491 status = psa_validate_persistent_key_parameters(
1492 attributes->core.lifetime, attributes->core.id,
1493 p_drv, 1 );
1494 if( status != PSA_SUCCESS )
1495 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001496 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001497
1498 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500 return( status );
1501
1502 /* Refuse to create overly large keys.
1503 * Note that this doesn't trigger on import if the attributes don't
1504 * explicitly specify a size (so psa_get_key_bits returns 0), so
1505 * psa_import_key() needs its own checks. */
1506 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1507 return( PSA_ERROR_NOT_SUPPORTED );
1508
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509 /* Reject invalid flags. These should not be reachable through the API. */
1510 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1511 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1512 return( PSA_ERROR_INVALID_ARGUMENT );
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( PSA_SUCCESS );
1515}
1516
Gilles Peskine4747d192019-04-17 15:05:45 +02001517/** Prepare a key slot to receive key material.
1518 *
1519 * This function allocates a key slot and sets its metadata.
1520 *
1521 * If this function fails, call psa_fail_key_creation().
1522 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001523 * This function is intended to be used as follows:
1524 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1525 * it with the specified attributes, and assign it a handle.
1526 * -# Populate the slot with the key material.
1527 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1528 * In case of failure at any step, stop the sequence and call
1529 * psa_fail_key_creation().
1530 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001531 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] handle On success, a handle for the allocated slot.
1534 * \param[out] p_slot On success, a pointer to the prepared slot.
1535 * \param[out] p_drv On any return, the driver for the key, if any.
1536 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The key slot is ready to receive key material.
1540 * \return If this function fails, the key slot is an invalid state.
1541 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 */
1543static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001544 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 const psa_key_attributes_t *attributes,
1546 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001548 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001549{
1550 psa_status_t status;
1551 psa_key_slot_t *slot;
1552
Gilles Peskinedf179142019-07-15 22:02:14 +02001553 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 *p_drv = NULL;
1555
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001556 status = psa_validate_key_attributes( attributes, p_drv );
1557 if( status != PSA_SUCCESS )
1558 return( status );
1559
Gilles Peskineedbed562019-08-07 18:19:59 +02001560 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 if( status != PSA_SUCCESS )
1562 return( status );
1563 slot = *p_slot;
1564
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001565 /* We're storing the declared bit-size of the key. It's up to each
1566 * creation mechanism to verify that this information is correct.
1567 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001568 * an input (generate, device) but not for those where the bit-size
1569 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001570
1571 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001572
Gilles Peskine91e8c332019-08-02 19:19:39 +02001573 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001574 * external-only flags, query `attributes`. Thanks to the check
1575 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001576 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * may have set. */
1578 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskinecbaff462019-07-12 23:46:04 +02001580#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001581 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001582 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001583 * create the key file in internal storage, create the
1584 * key inside the secure element, and update the driver's
1585 * persistent data. Start a transaction that will encompass these
1586 * three actions. */
1587 /* The first thing to do is to find a slot number for the new key.
1588 * We save the slot number in persistent storage as part of the
1589 * transaction data. It will be needed to recover if the power
1590 * fails during the key creation process, to clean up on the secure
1591 * element side after restarting. Obtaining a slot number from the
1592 * secure element driver updates its persistent state, but we do not yet
1593 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001594 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001595 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001596 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001597 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001598 &slot->data.se.slot_number );
1599 if( status != PSA_SUCCESS )
1600 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001601 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001602 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001603 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001604 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001605 status = psa_crypto_save_transaction( );
1606 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001607 {
1608 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001609 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001612
1613 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1614 {
1615 /* Key registration only makes sense with a secure element. */
1616 return( PSA_ERROR_INVALID_ARGUMENT );
1617 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001618#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1619
Darryl Green0c6575a2018-11-07 16:05:30 +00001620 return( status );
1621}
Gilles Peskine4747d192019-04-17 15:05:45 +02001622
1623/** Finalize the creation of a key once its key material has been set.
1624 *
1625 * This entails writing the key to persistent storage.
1626 *
1627 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001628 * See the documentation of psa_start_key_creation() for the intended use
1629 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001630 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001631 * \param[in,out] slot Pointer to the slot with key material.
1632 * \param[in] driver The secure element driver for the key,
1633 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 *
1635 * \retval #PSA_SUCCESS
1636 * The key was successfully created. The handle is now valid.
1637 * \return If this function fails, the key slot is an invalid state.
1638 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001640static psa_status_t psa_finish_key_creation(
1641 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001642 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001643{
1644 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001645 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001647
1648#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001649 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001650 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001651#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1652 if( driver != NULL )
1653 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001654 psa_se_key_data_storage_t data;
1655#if defined(static_assert)
1656 static_assert( sizeof( slot->data.se.slot_number ) ==
1657 sizeof( data.slot_number ),
1658 "Slot number size does not match psa_se_key_data_storage_t" );
1659 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1660 "Bit-size size does not match psa_se_key_data_storage_t" );
1661#endif
1662 memcpy( &data.slot_number, &slot->data.se.slot_number,
1663 sizeof( slot->data.se.slot_number ) );
1664 memcpy( &data.bits, &slot->attr.bits,
1665 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001666 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001667 (uint8_t*) &data,
1668 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001669 }
1670 else
1671#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1672 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001673 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001674 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001676 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1677 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001678 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001679 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1680 status = psa_internal_export_key( slot,
1681 buffer, buffer_size, &length,
1682 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001683 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001684 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001685 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001686
Gilles Peskinef9168942019-09-12 19:20:29 +02001687 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001688 mbedtls_free( buffer );
1689 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001691#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1692
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001694 /* Finish the transaction for a key creation. This does not
1695 * happen when registering an existing key. Detect this case
1696 * by checking whether a transaction is in progress (actual
1697 * creation of a key in a secure element requires a transaction,
1698 * but registration doesn't use one). */
1699 if( driver != NULL &&
1700 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001701 {
1702 status = psa_save_se_persistent_data( driver );
1703 if( status != PSA_SUCCESS )
1704 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001705 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706 return( status );
1707 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001708 status = psa_crypto_stop_transaction( );
1709 if( status != PSA_SUCCESS )
1710 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001711 }
1712#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1713
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 return( status );
1715}
1716
1717/** Abort the creation of a key.
1718 *
1719 * You may call this function after calling psa_start_key_creation(),
1720 * or after psa_finish_key_creation() fails. In other circumstances, this
1721 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001722 * See the documentation of psa_start_key_creation() for the intended use
1723 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001725 * \param[in,out] slot Pointer to the slot with key material.
1726 * \param[in] driver The secure element driver for the key,
1727 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001729static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001730 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001731{
Gilles Peskine011e4282019-06-26 18:34:38 +02001732 (void) driver;
1733
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 if( slot == NULL )
1735 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001736
1737#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001738 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 * element, and the failure happened later (when saving metadata
1740 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001741 * element.
1742 * https://github.com/ARMmbed/mbed-crypto/issues/217
1743 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001744
Gilles Peskined7729582019-08-05 15:55:54 +02001745 /* Abort the ongoing transaction if any (there may not be one if
1746 * the creation process failed before starting one, or if the
1747 * key creation is a registration of a key in a secure element).
1748 * Earlier functions must already have done what it takes to undo any
1749 * partial creation. All that's left is to update the transaction data
1750 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001751 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001752#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1753
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 psa_wipe_key_slot( slot );
1755}
1756
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001757/** Validate optional attributes during key creation.
1758 *
1759 * Some key attributes are optional during key creation. If they are
1760 * specified in the attributes structure, check that they are consistent
1761 * with the data in the slot.
1762 *
1763 * This function should be called near the end of key creation, after
1764 * the slot in memory is fully populated but before saving persistent data.
1765 */
1766static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001767 const psa_key_slot_t *slot,
1768 const psa_key_attributes_t *attributes )
1769{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001770 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001771 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001772 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 return( PSA_ERROR_INVALID_ARGUMENT );
1774 }
1775
1776 if( attributes->domain_parameters_size != 0 )
1777 {
1778#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001779 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001780 {
1781 mbedtls_mpi actual, required;
1782 int ret;
1783 mbedtls_mpi_init( &actual );
1784 mbedtls_mpi_init( &required );
1785 ret = mbedtls_rsa_export( slot->data.rsa,
1786 NULL, NULL, NULL, NULL, &actual );
1787 if( ret != 0 )
1788 goto rsa_exit;
1789 ret = mbedtls_mpi_read_binary( &required,
1790 attributes->domain_parameters,
1791 attributes->domain_parameters_size );
1792 if( ret != 0 )
1793 goto rsa_exit;
1794 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1795 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1796 rsa_exit:
1797 mbedtls_mpi_free( &actual );
1798 mbedtls_mpi_free( &required );
1799 if( ret != 0)
1800 return( mbedtls_to_psa_error( ret ) );
1801 }
1802 else
1803#endif
1804 {
1805 return( PSA_ERROR_INVALID_ARGUMENT );
1806 }
1807 }
1808
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001809 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001811 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 return( PSA_ERROR_INVALID_ARGUMENT );
1813 }
1814
1815 return( PSA_SUCCESS );
1816}
1817
Gilles Peskine4747d192019-04-17 15:05:45 +02001818psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001820 size_t data_length,
1821 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001822{
1823 psa_status_t status;
1824 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001825 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826
Gilles Peskine0f84d622019-09-12 19:03:13 +02001827 /* Reject zero-length symmetric keys (including raw data key objects).
1828 * This also rejects any key which might be encoded as an empty string,
1829 * which is never valid. */
1830 if( data_length == 0 )
1831 return( PSA_ERROR_INVALID_ARGUMENT );
1832
Gilles Peskinedf179142019-07-15 22:02:14 +02001833 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1834 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 if( status != PSA_SUCCESS )
1836 goto exit;
1837
Gilles Peskine5d309672019-07-12 23:47:28 +02001838#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1839 if( driver != NULL )
1840 {
1841 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001842 /* The driver should set the number of key bits, however in
1843 * case it doesn't, we initialize bits to an invalid value. */
1844 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001845 if( drv->key_management == NULL ||
1846 drv->key_management->p_import == NULL )
1847 {
1848 status = PSA_ERROR_NOT_SUPPORTED;
1849 goto exit;
1850 }
1851 status = drv->key_management->p_import(
1852 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001853 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001854 &bits );
1855 if( status != PSA_SUCCESS )
1856 goto exit;
1857 if( bits > PSA_MAX_KEY_BITS )
1858 {
1859 status = PSA_ERROR_NOT_SUPPORTED;
1860 goto exit;
1861 }
1862 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001863 }
1864 else
1865#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1866 {
1867 status = psa_import_key_into_slot( slot, data, data_length );
1868 if( status != PSA_SUCCESS )
1869 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001870 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001871 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001872 if( status != PSA_SUCCESS )
1873 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001874
Gilles Peskine011e4282019-06-26 18:34:38 +02001875 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001876exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 if( status != PSA_SUCCESS )
1878 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 *handle = 0;
1881 }
1882 return( status );
1883}
1884
Gilles Peskined7729582019-08-05 15:55:54 +02001885#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1886psa_status_t mbedtls_psa_register_se_key(
1887 const psa_key_attributes_t *attributes )
1888{
1889 psa_status_t status;
1890 psa_key_slot_t *slot = NULL;
1891 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001892 psa_key_handle_t handle = 0;
1893
1894 /* Leaving attributes unspecified is not currently supported.
1895 * It could make sense to query the key type and size from the
1896 * secure element, but not all secure elements support this
1897 * and the driver HAL doesn't currently support it. */
1898 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1899 return( PSA_ERROR_NOT_SUPPORTED );
1900 if( psa_get_key_bits( attributes ) == 0 )
1901 return( PSA_ERROR_NOT_SUPPORTED );
1902
1903 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1904 &handle, &slot, &driver );
1905 if( status != PSA_SUCCESS )
1906 goto exit;
1907
Gilles Peskined7729582019-08-05 15:55:54 +02001908 status = psa_finish_key_creation( slot, driver );
1909
1910exit:
1911 if( status != PSA_SUCCESS )
1912 {
1913 psa_fail_key_creation( slot, driver );
1914 }
1915 /* Registration doesn't keep the key in RAM. */
1916 psa_close_key( handle );
1917 return( status );
1918}
1919#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1920
Gilles Peskinef603c712019-01-19 13:40:11 +01001921static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001922 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001923{
1924 psa_status_t status;
1925 uint8_t *buffer = NULL;
1926 size_t buffer_size = 0;
1927 size_t length;
1928
Gilles Peskine8e338702019-07-30 20:06:31 +02001929 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001930 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001931 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001932 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001933 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001934 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1935 if( status != PSA_SUCCESS )
1936 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001937 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001938 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001939
1940exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001941 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001942 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001943 return( status );
1944}
1945
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001946psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1947 const psa_key_attributes_t *specified_attributes,
1948 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001949{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001950 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001951 psa_key_slot_t *source_slot = NULL;
1952 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001953 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001954 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001955
Gilles Peskine28f8f302019-07-24 13:30:31 +02001956 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001957 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001958 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001959 goto exit;
1960
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001961 status = psa_validate_optional_attributes( source_slot,
1962 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 if( status != PSA_SUCCESS )
1964 goto exit;
1965
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001966 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001967 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968 if( status != PSA_SUCCESS )
1969 goto exit;
1970
Gilles Peskinedf179142019-07-15 22:02:14 +02001971 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1972 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 if( status != PSA_SUCCESS )
1975 goto exit;
1976
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1978 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001979 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001980 /* Copying to a secure element is not implemented yet. */
1981 status = PSA_ERROR_NOT_SUPPORTED;
1982 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001983 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001984#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001985
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001987 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001989
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001991exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001992 if( status != PSA_SUCCESS )
1993 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001994 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 *target_handle = 0;
1996 }
1997 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998}
1999
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002000
2001
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002002/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002003/* Message digests */
2004/****************************************************************/
2005
Gilles Peskineb16841e2019-10-10 20:36:12 +02002006#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002007static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002008{
2009 switch( alg )
2010 {
2011#if defined(MBEDTLS_MD2_C)
2012 case PSA_ALG_MD2:
2013 return( &mbedtls_md2_info );
2014#endif
2015#if defined(MBEDTLS_MD4_C)
2016 case PSA_ALG_MD4:
2017 return( &mbedtls_md4_info );
2018#endif
2019#if defined(MBEDTLS_MD5_C)
2020 case PSA_ALG_MD5:
2021 return( &mbedtls_md5_info );
2022#endif
2023#if defined(MBEDTLS_RIPEMD160_C)
2024 case PSA_ALG_RIPEMD160:
2025 return( &mbedtls_ripemd160_info );
2026#endif
2027#if defined(MBEDTLS_SHA1_C)
2028 case PSA_ALG_SHA_1:
2029 return( &mbedtls_sha1_info );
2030#endif
2031#if defined(MBEDTLS_SHA256_C)
2032 case PSA_ALG_SHA_224:
2033 return( &mbedtls_sha224_info );
2034 case PSA_ALG_SHA_256:
2035 return( &mbedtls_sha256_info );
2036#endif
2037#if defined(MBEDTLS_SHA512_C)
2038 case PSA_ALG_SHA_384:
2039 return( &mbedtls_sha384_info );
2040 case PSA_ALG_SHA_512:
2041 return( &mbedtls_sha512_info );
2042#endif
2043 default:
2044 return( NULL );
2045 }
2046}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002047#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002048
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002049psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2050{
2051 switch( operation->alg )
2052 {
Gilles Peskine81736312018-06-26 15:04:31 +02002053 case 0:
2054 /* The object has (apparently) been initialized but it is not
2055 * in use. It's ok to call abort on such an object, and there's
2056 * nothing to do. */
2057 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002058#if defined(MBEDTLS_MD2_C)
2059 case PSA_ALG_MD2:
2060 mbedtls_md2_free( &operation->ctx.md2 );
2061 break;
2062#endif
2063#if defined(MBEDTLS_MD4_C)
2064 case PSA_ALG_MD4:
2065 mbedtls_md4_free( &operation->ctx.md4 );
2066 break;
2067#endif
2068#if defined(MBEDTLS_MD5_C)
2069 case PSA_ALG_MD5:
2070 mbedtls_md5_free( &operation->ctx.md5 );
2071 break;
2072#endif
2073#if defined(MBEDTLS_RIPEMD160_C)
2074 case PSA_ALG_RIPEMD160:
2075 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2076 break;
2077#endif
2078#if defined(MBEDTLS_SHA1_C)
2079 case PSA_ALG_SHA_1:
2080 mbedtls_sha1_free( &operation->ctx.sha1 );
2081 break;
2082#endif
2083#if defined(MBEDTLS_SHA256_C)
2084 case PSA_ALG_SHA_224:
2085 case PSA_ALG_SHA_256:
2086 mbedtls_sha256_free( &operation->ctx.sha256 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_SHA512_C)
2090 case PSA_ALG_SHA_384:
2091 case PSA_ALG_SHA_512:
2092 mbedtls_sha512_free( &operation->ctx.sha512 );
2093 break;
2094#endif
2095 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002096 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002097 }
2098 operation->alg = 0;
2099 return( PSA_SUCCESS );
2100}
2101
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002102psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002103 psa_algorithm_t alg )
2104{
2105 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002106
2107 /* A context must be freshly initialized before it can be set up. */
2108 if( operation->alg != 0 )
2109 {
2110 return( PSA_ERROR_BAD_STATE );
2111 }
2112
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002113 switch( alg )
2114 {
2115#if defined(MBEDTLS_MD2_C)
2116 case PSA_ALG_MD2:
2117 mbedtls_md2_init( &operation->ctx.md2 );
2118 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2119 break;
2120#endif
2121#if defined(MBEDTLS_MD4_C)
2122 case PSA_ALG_MD4:
2123 mbedtls_md4_init( &operation->ctx.md4 );
2124 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2125 break;
2126#endif
2127#if defined(MBEDTLS_MD5_C)
2128 case PSA_ALG_MD5:
2129 mbedtls_md5_init( &operation->ctx.md5 );
2130 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2131 break;
2132#endif
2133#if defined(MBEDTLS_RIPEMD160_C)
2134 case PSA_ALG_RIPEMD160:
2135 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2136 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_SHA1_C)
2140 case PSA_ALG_SHA_1:
2141 mbedtls_sha1_init( &operation->ctx.sha1 );
2142 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_SHA256_C)
2146 case PSA_ALG_SHA_224:
2147 mbedtls_sha256_init( &operation->ctx.sha256 );
2148 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2149 break;
2150 case PSA_ALG_SHA_256:
2151 mbedtls_sha256_init( &operation->ctx.sha256 );
2152 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2153 break;
2154#endif
2155#if defined(MBEDTLS_SHA512_C)
2156 case PSA_ALG_SHA_384:
2157 mbedtls_sha512_init( &operation->ctx.sha512 );
2158 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2159 break;
2160 case PSA_ALG_SHA_512:
2161 mbedtls_sha512_init( &operation->ctx.sha512 );
2162 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2163 break;
2164#endif
2165 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002166 return( PSA_ALG_IS_HASH( alg ) ?
2167 PSA_ERROR_NOT_SUPPORTED :
2168 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002169 }
2170 if( ret == 0 )
2171 operation->alg = alg;
2172 else
2173 psa_hash_abort( operation );
2174 return( mbedtls_to_psa_error( ret ) );
2175}
2176
2177psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2178 const uint8_t *input,
2179 size_t input_length )
2180{
2181 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002182
2183 /* Don't require hash implementations to behave correctly on a
2184 * zero-length input, which may have an invalid pointer. */
2185 if( input_length == 0 )
2186 return( PSA_SUCCESS );
2187
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188 switch( operation->alg )
2189 {
2190#if defined(MBEDTLS_MD2_C)
2191 case PSA_ALG_MD2:
2192 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2193 input, input_length );
2194 break;
2195#endif
2196#if defined(MBEDTLS_MD4_C)
2197 case PSA_ALG_MD4:
2198 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2199 input, input_length );
2200 break;
2201#endif
2202#if defined(MBEDTLS_MD5_C)
2203 case PSA_ALG_MD5:
2204 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2205 input, input_length );
2206 break;
2207#endif
2208#if defined(MBEDTLS_RIPEMD160_C)
2209 case PSA_ALG_RIPEMD160:
2210 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2211 input, input_length );
2212 break;
2213#endif
2214#if defined(MBEDTLS_SHA1_C)
2215 case PSA_ALG_SHA_1:
2216 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2217 input, input_length );
2218 break;
2219#endif
2220#if defined(MBEDTLS_SHA256_C)
2221 case PSA_ALG_SHA_224:
2222 case PSA_ALG_SHA_256:
2223 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2224 input, input_length );
2225 break;
2226#endif
2227#if defined(MBEDTLS_SHA512_C)
2228 case PSA_ALG_SHA_384:
2229 case PSA_ALG_SHA_512:
2230 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2231 input, input_length );
2232 break;
2233#endif
2234 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002235 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002237
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 if( ret != 0 )
2239 psa_hash_abort( operation );
2240 return( mbedtls_to_psa_error( ret ) );
2241}
2242
2243psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2244 uint8_t *hash,
2245 size_t hash_size,
2246 size_t *hash_length )
2247{
itayzafrir40835d42018-08-02 13:14:17 +03002248 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002250 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251
2252 /* Fill the output buffer with something that isn't a valid hash
2253 * (barring an attack on the hash and deliberately-crafted input),
2254 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002255 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002256 /* If hash_size is 0 then hash may be NULL and then the
2257 * call to memset would have undefined behavior. */
2258 if( hash_size != 0 )
2259 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260
2261 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002262 {
2263 status = PSA_ERROR_BUFFER_TOO_SMALL;
2264 goto exit;
2265 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266
2267 switch( operation->alg )
2268 {
2269#if defined(MBEDTLS_MD2_C)
2270 case PSA_ALG_MD2:
2271 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2272 break;
2273#endif
2274#if defined(MBEDTLS_MD4_C)
2275 case PSA_ALG_MD4:
2276 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2277 break;
2278#endif
2279#if defined(MBEDTLS_MD5_C)
2280 case PSA_ALG_MD5:
2281 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2282 break;
2283#endif
2284#if defined(MBEDTLS_RIPEMD160_C)
2285 case PSA_ALG_RIPEMD160:
2286 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2287 break;
2288#endif
2289#if defined(MBEDTLS_SHA1_C)
2290 case PSA_ALG_SHA_1:
2291 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2292 break;
2293#endif
2294#if defined(MBEDTLS_SHA256_C)
2295 case PSA_ALG_SHA_224:
2296 case PSA_ALG_SHA_256:
2297 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2298 break;
2299#endif
2300#if defined(MBEDTLS_SHA512_C)
2301 case PSA_ALG_SHA_384:
2302 case PSA_ALG_SHA_512:
2303 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2304 break;
2305#endif
2306 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002307 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308 }
itayzafrir40835d42018-08-02 13:14:17 +03002309 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310
itayzafrir40835d42018-08-02 13:14:17 +03002311exit:
2312 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002314 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315 return( psa_hash_abort( operation ) );
2316 }
2317 else
2318 {
2319 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002320 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 }
2322}
2323
Gilles Peskine2d277862018-06-18 15:41:12 +02002324psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2325 const uint8_t *hash,
2326 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327{
2328 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2329 size_t actual_hash_length;
2330 psa_status_t status = psa_hash_finish( operation,
2331 actual_hash, sizeof( actual_hash ),
2332 &actual_hash_length );
2333 if( status != PSA_SUCCESS )
2334 return( status );
2335 if( actual_hash_length != hash_length )
2336 return( PSA_ERROR_INVALID_SIGNATURE );
2337 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2338 return( PSA_ERROR_INVALID_SIGNATURE );
2339 return( PSA_SUCCESS );
2340}
2341
Gilles Peskineeb35d782019-01-22 17:56:16 +01002342psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2343 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002344{
2345 if( target_operation->alg != 0 )
2346 return( PSA_ERROR_BAD_STATE );
2347
2348 switch( source_operation->alg )
2349 {
2350 case 0:
2351 return( PSA_ERROR_BAD_STATE );
2352#if defined(MBEDTLS_MD2_C)
2353 case PSA_ALG_MD2:
2354 mbedtls_md2_clone( &target_operation->ctx.md2,
2355 &source_operation->ctx.md2 );
2356 break;
2357#endif
2358#if defined(MBEDTLS_MD4_C)
2359 case PSA_ALG_MD4:
2360 mbedtls_md4_clone( &target_operation->ctx.md4,
2361 &source_operation->ctx.md4 );
2362 break;
2363#endif
2364#if defined(MBEDTLS_MD5_C)
2365 case PSA_ALG_MD5:
2366 mbedtls_md5_clone( &target_operation->ctx.md5,
2367 &source_operation->ctx.md5 );
2368 break;
2369#endif
2370#if defined(MBEDTLS_RIPEMD160_C)
2371 case PSA_ALG_RIPEMD160:
2372 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2373 &source_operation->ctx.ripemd160 );
2374 break;
2375#endif
2376#if defined(MBEDTLS_SHA1_C)
2377 case PSA_ALG_SHA_1:
2378 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2379 &source_operation->ctx.sha1 );
2380 break;
2381#endif
2382#if defined(MBEDTLS_SHA256_C)
2383 case PSA_ALG_SHA_224:
2384 case PSA_ALG_SHA_256:
2385 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2386 &source_operation->ctx.sha256 );
2387 break;
2388#endif
2389#if defined(MBEDTLS_SHA512_C)
2390 case PSA_ALG_SHA_384:
2391 case PSA_ALG_SHA_512:
2392 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2393 &source_operation->ctx.sha512 );
2394 break;
2395#endif
2396 default:
2397 return( PSA_ERROR_NOT_SUPPORTED );
2398 }
2399
2400 target_operation->alg = source_operation->alg;
2401 return( PSA_SUCCESS );
2402}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403
2404
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002406/* MAC */
2407/****************************************************************/
2408
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002409static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410 psa_algorithm_t alg,
2411 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002412 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002413 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002416 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002418 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002419 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002420
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2422 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002423 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002425 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002426 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 mode = MBEDTLS_MODE_STREAM;
2428 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429 case PSA_ALG_CTR:
2430 mode = MBEDTLS_MODE_CTR;
2431 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002432 case PSA_ALG_CFB:
2433 mode = MBEDTLS_MODE_CFB;
2434 break;
2435 case PSA_ALG_OFB:
2436 mode = MBEDTLS_MODE_OFB;
2437 break;
2438 case PSA_ALG_CBC_NO_PADDING:
2439 mode = MBEDTLS_MODE_CBC;
2440 break;
2441 case PSA_ALG_CBC_PKCS7:
2442 mode = MBEDTLS_MODE_CBC;
2443 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002444 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445 mode = MBEDTLS_MODE_CCM;
2446 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002447 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 mode = MBEDTLS_MODE_GCM;
2449 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002450 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2451 mode = MBEDTLS_MODE_CHACHAPOLY;
2452 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 default:
2454 return( NULL );
2455 }
2456 }
2457 else if( alg == PSA_ALG_CMAC )
2458 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 else
2460 return( NULL );
2461
2462 switch( key_type )
2463 {
2464 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002465 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002466 break;
2467 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002468 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2469 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002471 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002473 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002474 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2475 * but two-key Triple-DES is functionally three-key Triple-DES
2476 * with K1=K3, so that's how we present it to mbedtls. */
2477 if( key_bits == 128 )
2478 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479 break;
2480 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002481 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482 break;
2483 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002484 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002486 case PSA_KEY_TYPE_CHACHA20:
2487 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2488 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489 default:
2490 return( NULL );
2491 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002493 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494
Jaeden Amero23bbb752018-06-26 14:16:54 +01002495 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2496 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497}
2498
Gilles Peskinea05219c2018-11-16 16:02:56 +01002499#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002500static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002501{
Gilles Peskine2d277862018-06-18 15:41:12 +02002502 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002503 {
2504 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002505 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002506 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002507 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002508 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002509 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002510 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002511 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002512 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002513 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002514 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002515 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002516 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002517 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002518 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002519 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002520 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002521 return( 128 );
2522 default:
2523 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002524 }
2525}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002526#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002527
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002528/* Initialize the MAC operation structure. Once this function has been
2529 * called, psa_mac_abort can run and will do the right thing. */
2530static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2531 psa_algorithm_t alg )
2532{
2533 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2534
2535 operation->alg = alg;
2536 operation->key_set = 0;
2537 operation->iv_set = 0;
2538 operation->iv_required = 0;
2539 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002540 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002541
2542#if defined(MBEDTLS_CMAC_C)
2543 if( alg == PSA_ALG_CMAC )
2544 {
2545 operation->iv_required = 0;
2546 mbedtls_cipher_init( &operation->ctx.cmac );
2547 status = PSA_SUCCESS;
2548 }
2549 else
2550#endif /* MBEDTLS_CMAC_C */
2551#if defined(MBEDTLS_MD_C)
2552 if( PSA_ALG_IS_HMAC( operation->alg ) )
2553 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002554 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2555 operation->ctx.hmac.hash_ctx.alg = 0;
2556 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002557 }
2558 else
2559#endif /* MBEDTLS_MD_C */
2560 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002561 if( ! PSA_ALG_IS_MAC( alg ) )
2562 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563 }
2564
2565 if( status != PSA_SUCCESS )
2566 memset( operation, 0, sizeof( *operation ) );
2567 return( status );
2568}
2569
Gilles Peskine01126fa2018-07-12 17:04:55 +02002570#if defined(MBEDTLS_MD_C)
2571static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2572{
Gilles Peskine3f108122018-12-07 18:14:53 +01002573 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002574 return( psa_hash_abort( &hmac->hash_ctx ) );
2575}
2576#endif /* MBEDTLS_MD_C */
2577
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2579{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002580 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002582 /* The object has (apparently) been initialized but it is not
2583 * in use. It's ok to call abort on such an object, and there's
2584 * nothing to do. */
2585 return( PSA_SUCCESS );
2586 }
2587 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002589 if( operation->alg == PSA_ALG_CMAC )
2590 {
2591 mbedtls_cipher_free( &operation->ctx.cmac );
2592 }
2593 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002596 if( PSA_ALG_IS_HMAC( operation->alg ) )
2597 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002598 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002599 }
2600 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002602 {
2603 /* Sanity check (shouldn't happen: operation->alg should
2604 * always have been initialized to a valid value). */
2605 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 }
Moran Peker41deec42018-04-04 15:43:05 +03002607
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608 operation->alg = 0;
2609 operation->key_set = 0;
2610 operation->iv_set = 0;
2611 operation->iv_required = 0;
2612 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002613 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002614
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002616
2617bad_state:
2618 /* If abort is called on an uninitialized object, we can't trust
2619 * anything. Wipe the object in case it contains confidential data.
2620 * This may result in a memory leak if a pointer gets overwritten,
2621 * but it's too late to do anything about this. */
2622 memset( operation, 0, sizeof( *operation ) );
2623 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624}
2625
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002626#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002628 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002629 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002630 const mbedtls_cipher_info_t *cipher_info )
2631{
2632 int ret;
2633
2634 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002635
2636 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2637 if( ret != 0 )
2638 return( ret );
2639
2640 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2641 slot->data.raw.data,
2642 key_bits );
2643 return( ret );
2644}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002645#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002646
Gilles Peskine248051a2018-06-20 16:09:38 +02002647#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002648static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2649 const uint8_t *key,
2650 size_t key_length,
2651 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002653 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002654 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002655 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002656 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002657 psa_status_t status;
2658
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002659 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2660 * overflow below. This should never trigger if the hash algorithm
2661 * is implemented correctly. */
2662 /* The size checks against the ipad and opad buffers cannot be written
2663 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2664 * because that triggers -Wlogical-op on GCC 7.3. */
2665 if( block_size > sizeof( ipad ) )
2666 return( PSA_ERROR_NOT_SUPPORTED );
2667 if( block_size > sizeof( hmac->opad ) )
2668 return( PSA_ERROR_NOT_SUPPORTED );
2669 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002670 return( PSA_ERROR_NOT_SUPPORTED );
2671
Gilles Peskined223b522018-06-11 18:12:58 +02002672 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002674 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002675 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002676 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002677 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002678 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002679 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002680 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002681 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002682 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002683 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002684 }
Gilles Peskine96889972018-07-12 17:07:03 +02002685 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2686 * but it is permitted. It is common when HMAC is used in HKDF, for
2687 * example. Don't call `memcpy` in the 0-length because `key` could be
2688 * an invalid pointer which would make the behavior undefined. */
2689 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002690 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002691
Gilles Peskined223b522018-06-11 18:12:58 +02002692 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2693 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002695 ipad[i] ^= 0x36;
2696 memset( ipad + key_length, 0x36, block_size - key_length );
2697
2698 /* Copy the key material from ipad to opad, flipping the requisite bits,
2699 * and filling the rest of opad with the requisite constant. */
2700 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2702 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002703
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002705 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002706 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002707
Gilles Peskine01126fa2018-07-12 17:04:55 +02002708 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002709
2710cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002711 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002712
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 return( status );
2714}
Gilles Peskine248051a2018-06-20 16:09:38 +02002715#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716
Gilles Peskine89167cb2018-07-08 20:12:23 +02002717static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002718 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002719 psa_algorithm_t alg,
2720 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002723 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002725 psa_key_usage_t usage =
2726 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002727 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002728 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002730 /* A context must be freshly initialized before it can be set up. */
2731 if( operation->alg != 0 )
2732 {
2733 return( PSA_ERROR_BAD_STATE );
2734 }
2735
Gilles Peskined911eb72018-08-14 15:18:45 +02002736 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002737 if( status != PSA_SUCCESS )
2738 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002739 if( is_sign )
2740 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741
Gilles Peskine28f8f302019-07-24 13:30:31 +02002742 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002743 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002744 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002745 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002746
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002748 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002749 {
2750 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002751 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002752 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002753 int ret;
2754 if( cipher_info == NULL )
2755 {
2756 status = PSA_ERROR_NOT_SUPPORTED;
2757 goto exit;
2758 }
2759 operation->mac_size = cipher_info->block_size;
2760 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2761 status = mbedtls_to_psa_error( ret );
2762 }
2763 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002766 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002767 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002768 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002769 if( hash_alg == 0 )
2770 {
2771 status = PSA_ERROR_NOT_SUPPORTED;
2772 goto exit;
2773 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002774
2775 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2776 /* Sanity check. This shouldn't fail on a valid configuration. */
2777 if( operation->mac_size == 0 ||
2778 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2779 {
2780 status = PSA_ERROR_NOT_SUPPORTED;
2781 goto exit;
2782 }
2783
Gilles Peskine8e338702019-07-30 20:06:31 +02002784 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002785 {
2786 status = PSA_ERROR_INVALID_ARGUMENT;
2787 goto exit;
2788 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002789
Gilles Peskine01126fa2018-07-12 17:04:55 +02002790 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2791 slot->data.raw.data,
2792 slot->data.raw.bytes,
2793 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002794 }
2795 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002797 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002798 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002799 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801
Gilles Peskined911eb72018-08-14 15:18:45 +02002802 if( truncated == 0 )
2803 {
2804 /* The "normal" case: untruncated algorithm. Nothing to do. */
2805 }
2806 else if( truncated < 4 )
2807 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002808 /* A very short MAC is too short for security since it can be
2809 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2810 * so we make this our minimum, even though 32 bits is still
2811 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002812 status = PSA_ERROR_NOT_SUPPORTED;
2813 }
2814 else if( truncated > operation->mac_size )
2815 {
2816 /* It's impossible to "truncate" to a larger length. */
2817 status = PSA_ERROR_INVALID_ARGUMENT;
2818 }
2819 else
2820 operation->mac_size = truncated;
2821
Gilles Peskinefbfac682018-07-08 20:51:54 +02002822exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002823 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002825 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002827 else
2828 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002829 operation->key_set = 1;
2830 }
2831 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832}
2833
Gilles Peskine89167cb2018-07-08 20:12:23 +02002834psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002835 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002836 psa_algorithm_t alg )
2837{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002838 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002839}
2840
2841psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002842 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002843 psa_algorithm_t alg )
2844{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002845 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002846}
2847
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2849 const uint8_t *input,
2850 size_t input_length )
2851{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002852 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002854 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002856 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857 operation->has_input = 1;
2858
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002860 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002861 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002862 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2863 input, input_length );
2864 status = mbedtls_to_psa_error( ret );
2865 }
2866 else
2867#endif /* MBEDTLS_CMAC_C */
2868#if defined(MBEDTLS_MD_C)
2869 if( PSA_ALG_IS_HMAC( operation->alg ) )
2870 {
2871 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2872 input_length );
2873 }
2874 else
2875#endif /* MBEDTLS_MD_C */
2876 {
2877 /* This shouldn't happen if `operation` was initialized by
2878 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002879 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002880 }
2881
Gilles Peskinefbfac682018-07-08 20:51:54 +02002882 if( status != PSA_SUCCESS )
2883 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002884 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885}
2886
Gilles Peskine01126fa2018-07-12 17:04:55 +02002887#if defined(MBEDTLS_MD_C)
2888static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2889 uint8_t *mac,
2890 size_t mac_size )
2891{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002892 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002893 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2894 size_t hash_size = 0;
2895 size_t block_size = psa_get_hash_block_size( hash_alg );
2896 psa_status_t status;
2897
Gilles Peskine01126fa2018-07-12 17:04:55 +02002898 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2899 if( status != PSA_SUCCESS )
2900 return( status );
2901 /* From here on, tmp needs to be wiped. */
2902
2903 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2904 if( status != PSA_SUCCESS )
2905 goto exit;
2906
2907 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2908 if( status != PSA_SUCCESS )
2909 goto exit;
2910
2911 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2912 if( status != PSA_SUCCESS )
2913 goto exit;
2914
Gilles Peskined911eb72018-08-14 15:18:45 +02002915 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2916 if( status != PSA_SUCCESS )
2917 goto exit;
2918
2919 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002920
2921exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002922 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002923 return( status );
2924}
2925#endif /* MBEDTLS_MD_C */
2926
mohammad16036df908f2018-04-02 08:34:15 -07002927static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002928 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002929 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002931 if( ! operation->key_set )
2932 return( PSA_ERROR_BAD_STATE );
2933 if( operation->iv_required && ! operation->iv_set )
2934 return( PSA_ERROR_BAD_STATE );
2935
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 if( mac_size < operation->mac_size )
2937 return( PSA_ERROR_BUFFER_TOO_SMALL );
2938
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002940 if( operation->alg == PSA_ALG_CMAC )
2941 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002942 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2943 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2944 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002945 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002946 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002947 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002949 else
2950#endif /* MBEDTLS_CMAC_C */
2951#if defined(MBEDTLS_MD_C)
2952 if( PSA_ALG_IS_HMAC( operation->alg ) )
2953 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002954 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002955 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 }
2957 else
2958#endif /* MBEDTLS_MD_C */
2959 {
2960 /* This shouldn't happen if `operation` was initialized by
2961 * a setup function. */
2962 return( PSA_ERROR_BAD_STATE );
2963 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964}
2965
Gilles Peskineacd4be32018-07-08 19:56:25 +02002966psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2967 uint8_t *mac,
2968 size_t mac_size,
2969 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002970{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002971 psa_status_t status;
2972
Jaeden Amero252ef282019-02-15 14:05:35 +00002973 if( operation->alg == 0 )
2974 {
2975 return( PSA_ERROR_BAD_STATE );
2976 }
2977
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002978 /* Fill the output buffer with something that isn't a valid mac
2979 * (barring an attack on the mac and deliberately-crafted input),
2980 * in case the caller doesn't check the return status properly. */
2981 *mac_length = mac_size;
2982 /* If mac_size is 0 then mac may be NULL and then the
2983 * call to memset would have undefined behavior. */
2984 if( mac_size != 0 )
2985 memset( mac, '!', mac_size );
2986
Gilles Peskine89167cb2018-07-08 20:12:23 +02002987 if( ! operation->is_sign )
2988 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002989 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002990 }
mohammad16036df908f2018-04-02 08:34:15 -07002991
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002992 status = psa_mac_finish_internal( operation, mac, mac_size );
2993
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002994 if( status == PSA_SUCCESS )
2995 {
2996 status = psa_mac_abort( operation );
2997 if( status == PSA_SUCCESS )
2998 *mac_length = operation->mac_size;
2999 else
3000 memset( mac, '!', mac_size );
3001 }
3002 else
3003 psa_mac_abort( operation );
3004 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003005}
3006
Gilles Peskineacd4be32018-07-08 19:56:25 +02003007psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3008 const uint8_t *mac,
3009 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010{
Gilles Peskine828ed142018-06-18 23:25:51 +02003011 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003012 psa_status_t status;
3013
Jaeden Amero252ef282019-02-15 14:05:35 +00003014 if( operation->alg == 0 )
3015 {
3016 return( PSA_ERROR_BAD_STATE );
3017 }
3018
Gilles Peskine89167cb2018-07-08 20:12:23 +02003019 if( operation->is_sign )
3020 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003021 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003022 }
3023 if( operation->mac_size != mac_length )
3024 {
3025 status = PSA_ERROR_INVALID_SIGNATURE;
3026 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003027 }
mohammad16036df908f2018-04-02 08:34:15 -07003028
3029 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003030 actual_mac, sizeof( actual_mac ) );
3031
3032 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3033 status = PSA_ERROR_INVALID_SIGNATURE;
3034
3035cleanup:
3036 if( status == PSA_SUCCESS )
3037 status = psa_mac_abort( operation );
3038 else
3039 psa_mac_abort( operation );
3040
Gilles Peskine3f108122018-12-07 18:14:53 +01003041 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003042
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003043 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003044}
3045
3046
Gilles Peskine20035e32018-02-03 22:44:14 +01003047
Gilles Peskine20035e32018-02-03 22:44:14 +01003048/****************************************************************/
3049/* Asymmetric cryptography */
3050/****************************************************************/
3051
Gilles Peskine2b450e32018-06-27 15:42:46 +02003052#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003053/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003054 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003055static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3056 size_t hash_length,
3057 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003058{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003059 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003060 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003061 *md_alg = mbedtls_md_get_type( md_info );
3062
3063 /* The Mbed TLS RSA module uses an unsigned int for hash length
3064 * parameters. Validate that it fits so that we don't risk an
3065 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003066#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003067 if( hash_length > UINT_MAX )
3068 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003069#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003070
3071#if defined(MBEDTLS_PKCS1_V15)
3072 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3073 * must be correct. */
3074 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3075 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003076 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003077 if( md_info == NULL )
3078 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003079 if( mbedtls_md_get_size( md_info ) != hash_length )
3080 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003081 }
3082#endif /* MBEDTLS_PKCS1_V15 */
3083
3084#if defined(MBEDTLS_PKCS1_V21)
3085 /* PSS requires a hash internally. */
3086 if( PSA_ALG_IS_RSA_PSS( alg ) )
3087 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003088 if( md_info == NULL )
3089 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003090 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003091#endif /* MBEDTLS_PKCS1_V21 */
3092
Gilles Peskine61b91d42018-06-08 16:09:36 +02003093 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003094}
3095
Gilles Peskine2b450e32018-06-27 15:42:46 +02003096static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3097 psa_algorithm_t alg,
3098 const uint8_t *hash,
3099 size_t hash_length,
3100 uint8_t *signature,
3101 size_t signature_size,
3102 size_t *signature_length )
3103{
3104 psa_status_t status;
3105 int ret;
3106 mbedtls_md_type_t md_alg;
3107
3108 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3109 if( status != PSA_SUCCESS )
3110 return( status );
3111
Gilles Peskine630a18a2018-06-29 17:49:35 +02003112 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003113 return( PSA_ERROR_BUFFER_TOO_SMALL );
3114
3115#if defined(MBEDTLS_PKCS1_V15)
3116 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3117 {
3118 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3119 MBEDTLS_MD_NONE );
3120 ret = mbedtls_rsa_pkcs1_sign( rsa,
3121 mbedtls_ctr_drbg_random,
3122 &global_data.ctr_drbg,
3123 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003124 md_alg,
3125 (unsigned int) hash_length,
3126 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003127 signature );
3128 }
3129 else
3130#endif /* MBEDTLS_PKCS1_V15 */
3131#if defined(MBEDTLS_PKCS1_V21)
3132 if( PSA_ALG_IS_RSA_PSS( alg ) )
3133 {
3134 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3135 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3136 mbedtls_ctr_drbg_random,
3137 &global_data.ctr_drbg,
3138 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003139 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003140 (unsigned int) hash_length,
3141 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003142 signature );
3143 }
3144 else
3145#endif /* MBEDTLS_PKCS1_V21 */
3146 {
3147 return( PSA_ERROR_INVALID_ARGUMENT );
3148 }
3149
3150 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003151 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003152 return( mbedtls_to_psa_error( ret ) );
3153}
3154
3155static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3156 psa_algorithm_t alg,
3157 const uint8_t *hash,
3158 size_t hash_length,
3159 const uint8_t *signature,
3160 size_t signature_length )
3161{
3162 psa_status_t status;
3163 int ret;
3164 mbedtls_md_type_t md_alg;
3165
3166 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3167 if( status != PSA_SUCCESS )
3168 return( status );
3169
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003170 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3171 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003172
3173#if defined(MBEDTLS_PKCS1_V15)
3174 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3175 {
3176 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3177 MBEDTLS_MD_NONE );
3178 ret = mbedtls_rsa_pkcs1_verify( rsa,
3179 mbedtls_ctr_drbg_random,
3180 &global_data.ctr_drbg,
3181 MBEDTLS_RSA_PUBLIC,
3182 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003183 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003184 hash,
3185 signature );
3186 }
3187 else
3188#endif /* MBEDTLS_PKCS1_V15 */
3189#if defined(MBEDTLS_PKCS1_V21)
3190 if( PSA_ALG_IS_RSA_PSS( alg ) )
3191 {
3192 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3193 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3194 mbedtls_ctr_drbg_random,
3195 &global_data.ctr_drbg,
3196 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003197 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003198 (unsigned int) hash_length,
3199 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003200 signature );
3201 }
3202 else
3203#endif /* MBEDTLS_PKCS1_V21 */
3204 {
3205 return( PSA_ERROR_INVALID_ARGUMENT );
3206 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003207
3208 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3209 * the rest of the signature is invalid". This has little use in
3210 * practice and PSA doesn't report this distinction. */
3211 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3212 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003213 return( mbedtls_to_psa_error( ret ) );
3214}
3215#endif /* MBEDTLS_RSA_C */
3216
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003217#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003218/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3219 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3220 * (even though these functions don't modify it). */
3221static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3222 psa_algorithm_t alg,
3223 const uint8_t *hash,
3224 size_t hash_length,
3225 uint8_t *signature,
3226 size_t signature_size,
3227 size_t *signature_length )
3228{
3229 int ret;
3230 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003231 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003232 mbedtls_mpi_init( &r );
3233 mbedtls_mpi_init( &s );
3234
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003235 if( signature_size < 2 * curve_bytes )
3236 {
3237 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3238 goto cleanup;
3239 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003240
Gilles Peskinea05219c2018-11-16 16:02:56 +01003241#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003242 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3243 {
3244 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3245 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3246 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003247 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3248 &ecp->d, hash,
3249 hash_length, md_alg,
3250 mbedtls_ctr_drbg_random,
3251 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003252 }
3253 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003254#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003255 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003256 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003257 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3258 hash, hash_length,
3259 mbedtls_ctr_drbg_random,
3260 &global_data.ctr_drbg ) );
3261 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003262
3263 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3264 signature,
3265 curve_bytes ) );
3266 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3267 signature + curve_bytes,
3268 curve_bytes ) );
3269
3270cleanup:
3271 mbedtls_mpi_free( &r );
3272 mbedtls_mpi_free( &s );
3273 if( ret == 0 )
3274 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003275 return( mbedtls_to_psa_error( ret ) );
3276}
3277
3278static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3279 const uint8_t *hash,
3280 size_t hash_length,
3281 const uint8_t *signature,
3282 size_t signature_length )
3283{
3284 int ret;
3285 mbedtls_mpi r, s;
3286 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3287 mbedtls_mpi_init( &r );
3288 mbedtls_mpi_init( &s );
3289
3290 if( signature_length != 2 * curve_bytes )
3291 return( PSA_ERROR_INVALID_SIGNATURE );
3292
3293 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3294 signature,
3295 curve_bytes ) );
3296 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3297 signature + curve_bytes,
3298 curve_bytes ) );
3299
3300 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3301 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003302
3303cleanup:
3304 mbedtls_mpi_free( &r );
3305 mbedtls_mpi_free( &s );
3306 return( mbedtls_to_psa_error( ret ) );
3307}
3308#endif /* MBEDTLS_ECDSA_C */
3309
Gilles Peskinec5487a82018-12-03 18:08:14 +01003310psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003311 psa_algorithm_t alg,
3312 const uint8_t *hash,
3313 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003314 uint8_t *signature,
3315 size_t signature_size,
3316 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003317{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003318 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003319 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003320#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3321 const psa_drv_se_t *drv;
3322 psa_drv_se_context_t *drv_context;
3323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003324
3325 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003326 /* Immediately reject a zero-length signature buffer. This guarantees
3327 * that signature must be a valid pointer. (On the other hand, the hash
3328 * buffer can in principle be empty since it doesn't actually have
3329 * to be a hash.) */
3330 if( signature_size == 0 )
3331 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003332
Gilles Peskineedc64242019-08-07 21:05:07 +02003333 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003334 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003335 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003336 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003337 {
3338 status = PSA_ERROR_INVALID_ARGUMENT;
3339 goto exit;
3340 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003341
Gilles Peskineedc64242019-08-07 21:05:07 +02003342#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3343 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3344 {
3345 if( drv->asymmetric == NULL ||
3346 drv->asymmetric->p_sign == NULL )
3347 {
3348 status = PSA_ERROR_NOT_SUPPORTED;
3349 goto exit;
3350 }
3351 status = drv->asymmetric->p_sign( drv_context,
3352 slot->data.se.slot_number,
3353 alg,
3354 hash, hash_length,
3355 signature, signature_size,
3356 signature_length );
3357 }
3358 else
3359#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003360#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003361 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003362 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003363 status = psa_rsa_sign( slot->data.rsa,
3364 alg,
3365 hash, hash_length,
3366 signature, signature_size,
3367 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003368 }
3369 else
3370#endif /* defined(MBEDTLS_RSA_C) */
3371#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003372 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003373 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003374#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003375 if(
3376#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3377 PSA_ALG_IS_ECDSA( alg )
3378#else
3379 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3380#endif
3381 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003382 status = psa_ecdsa_sign( slot->data.ecp,
3383 alg,
3384 hash, hash_length,
3385 signature, signature_size,
3386 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003387 else
3388#endif /* defined(MBEDTLS_ECDSA_C) */
3389 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003390 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003391 }
itayzafrir5c753392018-05-08 11:18:38 +03003392 }
3393 else
3394#endif /* defined(MBEDTLS_ECP_C) */
3395 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003396 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003397 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003398
3399exit:
3400 /* Fill the unused part of the output buffer (the whole buffer on error,
3401 * the trailing part on success) with something that isn't a valid mac
3402 * (barring an attack on the mac and deliberately-crafted input),
3403 * in case the caller doesn't check the return status properly. */
3404 if( status == PSA_SUCCESS )
3405 memset( signature + *signature_length, '!',
3406 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003407 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003408 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003409 /* If signature_size is 0 then we have nothing to do. We must not call
3410 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003412}
3413
Gilles Peskinec5487a82018-12-03 18:08:14 +01003414psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003415 psa_algorithm_t alg,
3416 const uint8_t *hash,
3417 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003418 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003419 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003420{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003421 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003422 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003423#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3424 const psa_drv_se_t *drv;
3425 psa_drv_se_context_t *drv_context;
3426#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003427
Gilles Peskineedc64242019-08-07 21:05:07 +02003428 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003429 if( status != PSA_SUCCESS )
3430 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003431
Gilles Peskineedc64242019-08-07 21:05:07 +02003432#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3433 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3434 {
3435 if( drv->asymmetric == NULL ||
3436 drv->asymmetric->p_verify == NULL )
3437 return( PSA_ERROR_NOT_SUPPORTED );
3438 return( drv->asymmetric->p_verify( drv_context,
3439 slot->data.se.slot_number,
3440 alg,
3441 hash, hash_length,
3442 signature, signature_length ) );
3443 }
3444 else
3445#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003446#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003447 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003448 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003449 return( psa_rsa_verify( slot->data.rsa,
3450 alg,
3451 hash, hash_length,
3452 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453 }
3454 else
3455#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003456#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003457 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003458 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003459#if defined(MBEDTLS_ECDSA_C)
3460 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003461 return( psa_ecdsa_verify( slot->data.ecp,
3462 hash, hash_length,
3463 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003464 else
3465#endif /* defined(MBEDTLS_ECDSA_C) */
3466 {
3467 return( PSA_ERROR_INVALID_ARGUMENT );
3468 }
itayzafrir5c753392018-05-08 11:18:38 +03003469 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003470 else
3471#endif /* defined(MBEDTLS_ECP_C) */
3472 {
3473 return( PSA_ERROR_NOT_SUPPORTED );
3474 }
3475}
3476
Gilles Peskine072ac562018-06-30 00:21:29 +02003477#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3478static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3479 mbedtls_rsa_context *rsa )
3480{
3481 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3482 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3483 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3484 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3485}
3486#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3487
Gilles Peskinec5487a82018-12-03 18:08:14 +01003488psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003489 psa_algorithm_t alg,
3490 const uint8_t *input,
3491 size_t input_length,
3492 const uint8_t *salt,
3493 size_t salt_length,
3494 uint8_t *output,
3495 size_t output_size,
3496 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003497{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003498 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003499 psa_status_t status;
3500
Darryl Green5cc689a2018-07-24 15:34:10 +01003501 (void) input;
3502 (void) input_length;
3503 (void) salt;
3504 (void) output;
3505 (void) output_size;
3506
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003507 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003508
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003509 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3510 return( PSA_ERROR_INVALID_ARGUMENT );
3511
Gilles Peskine28f8f302019-07-24 13:30:31 +02003512 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003513 if( status != PSA_SUCCESS )
3514 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003515 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3516 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003517 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003518
3519#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003520 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003521 {
3522 mbedtls_rsa_context *rsa = slot->data.rsa;
3523 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003524 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003525 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003527 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003528 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003529 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3530 mbedtls_ctr_drbg_random,
3531 &global_data.ctr_drbg,
3532 MBEDTLS_RSA_PUBLIC,
3533 input_length,
3534 input,
3535 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536 }
3537 else
3538#endif /* MBEDTLS_PKCS1_V15 */
3539#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003540 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003542 psa_rsa_oaep_set_padding_mode( alg, rsa );
3543 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3544 mbedtls_ctr_drbg_random,
3545 &global_data.ctr_drbg,
3546 MBEDTLS_RSA_PUBLIC,
3547 salt, salt_length,
3548 input_length,
3549 input,
3550 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551 }
3552 else
3553#endif /* MBEDTLS_PKCS1_V21 */
3554 {
3555 return( PSA_ERROR_INVALID_ARGUMENT );
3556 }
3557 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003558 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559 return( mbedtls_to_psa_error( ret ) );
3560 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003561 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003562#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003563 {
3564 return( PSA_ERROR_NOT_SUPPORTED );
3565 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003566}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567
Gilles Peskinec5487a82018-12-03 18:08:14 +01003568psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003569 psa_algorithm_t alg,
3570 const uint8_t *input,
3571 size_t input_length,
3572 const uint8_t *salt,
3573 size_t salt_length,
3574 uint8_t *output,
3575 size_t output_size,
3576 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003578 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 psa_status_t status;
3580
Darryl Green5cc689a2018-07-24 15:34:10 +01003581 (void) input;
3582 (void) input_length;
3583 (void) salt;
3584 (void) output;
3585 (void) output_size;
3586
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003587 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003589 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3590 return( PSA_ERROR_INVALID_ARGUMENT );
3591
Gilles Peskine28f8f302019-07-24 13:30:31 +02003592 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003593 if( status != PSA_SUCCESS )
3594 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003595 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596 return( PSA_ERROR_INVALID_ARGUMENT );
3597
3598#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003599 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600 {
3601 mbedtls_rsa_context *rsa = slot->data.rsa;
3602 int ret;
3603
Gilles Peskine630a18a2018-06-29 17:49:35 +02003604 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605 return( PSA_ERROR_INVALID_ARGUMENT );
3606
3607#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003608 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003610 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3611 mbedtls_ctr_drbg_random,
3612 &global_data.ctr_drbg,
3613 MBEDTLS_RSA_PRIVATE,
3614 output_length,
3615 input,
3616 output,
3617 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618 }
3619 else
3620#endif /* MBEDTLS_PKCS1_V15 */
3621#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003622 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003624 psa_rsa_oaep_set_padding_mode( alg, rsa );
3625 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3626 mbedtls_ctr_drbg_random,
3627 &global_data.ctr_drbg,
3628 MBEDTLS_RSA_PRIVATE,
3629 salt, salt_length,
3630 output_length,
3631 input,
3632 output,
3633 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 }
3635 else
3636#endif /* MBEDTLS_PKCS1_V21 */
3637 {
3638 return( PSA_ERROR_INVALID_ARGUMENT );
3639 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003640
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 return( mbedtls_to_psa_error( ret ) );
3642 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003644#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645 {
3646 return( PSA_ERROR_NOT_SUPPORTED );
3647 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003648}
Gilles Peskine20035e32018-02-03 22:44:14 +01003649
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003650
3651
mohammad1603503973b2018-03-12 15:59:30 +02003652/****************************************************************/
3653/* Symmetric cryptography */
3654/****************************************************************/
3655
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003656/* Initialize the cipher operation structure. Once this function has been
3657 * called, psa_cipher_abort can run and will do the right thing. */
3658static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3659 psa_algorithm_t alg )
3660{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003661 if( ! PSA_ALG_IS_CIPHER( alg ) )
3662 {
3663 memset( operation, 0, sizeof( *operation ) );
3664 return( PSA_ERROR_INVALID_ARGUMENT );
3665 }
3666
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003667 operation->alg = alg;
3668 operation->key_set = 0;
3669 operation->iv_set = 0;
3670 operation->iv_required = 1;
3671 operation->iv_size = 0;
3672 operation->block_size = 0;
3673 mbedtls_cipher_init( &operation->ctx.cipher );
3674 return( PSA_SUCCESS );
3675}
3676
Gilles Peskinee553c652018-06-04 16:22:46 +02003677static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003678 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003679 psa_algorithm_t alg,
3680 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003681{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003682 int ret = 0;
3683 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003684 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003685 size_t key_bits;
3686 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003687 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3688 PSA_KEY_USAGE_ENCRYPT :
3689 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003690
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003691 /* A context must be freshly initialized before it can be set up. */
3692 if( operation->alg != 0 )
3693 {
3694 return( PSA_ERROR_BAD_STATE );
3695 }
3696
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003697 status = psa_cipher_init( operation, alg );
3698 if( status != PSA_SUCCESS )
3699 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003700
Gilles Peskine28f8f302019-07-24 13:30:31 +02003701 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003702 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003703 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003704 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003705
Gilles Peskine8e338702019-07-30 20:06:31 +02003706 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003707 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003708 {
3709 status = PSA_ERROR_NOT_SUPPORTED;
3710 goto exit;
3711 }
mohammad1603503973b2018-03-12 15:59:30 +02003712
mohammad1603503973b2018-03-12 15:59:30 +02003713 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003714 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003715 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003716
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003717#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003718 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003719 {
3720 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003721 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003722 memcpy( keys, slot->data.raw.data, 16 );
3723 memcpy( keys + 16, slot->data.raw.data, 8 );
3724 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3725 keys,
3726 192, cipher_operation );
3727 }
3728 else
3729#endif
3730 {
3731 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3732 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003733 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003734 }
Moran Peker41deec42018-04-04 15:43:05 +03003735 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003736 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003737
mohammad16038481e742018-03-18 13:57:31 +02003738#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003739 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003740 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003741 case PSA_ALG_CBC_NO_PADDING:
3742 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3743 MBEDTLS_PADDING_NONE );
3744 break;
3745 case PSA_ALG_CBC_PKCS7:
3746 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3747 MBEDTLS_PADDING_PKCS7 );
3748 break;
3749 default:
3750 /* The algorithm doesn't involve padding. */
3751 ret = 0;
3752 break;
3753 }
3754 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003755 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003756#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3757
mohammad1603503973b2018-03-12 15:59:30 +02003758 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003759 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003760 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003761 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003762 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003763 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003764 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003765#if defined(MBEDTLS_CHACHA20_C)
3766 else
3767 if( alg == PSA_ALG_CHACHA20 )
3768 operation->iv_size = 12;
3769#endif
mohammad1603503973b2018-03-12 15:59:30 +02003770
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003771exit:
3772 if( status == 0 )
3773 status = mbedtls_to_psa_error( ret );
3774 if( status != 0 )
3775 psa_cipher_abort( operation );
3776 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003777}
3778
Gilles Peskinefe119512018-07-08 21:39:34 +02003779psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003780 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003781 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003782{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003783 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003784}
3785
Gilles Peskinefe119512018-07-08 21:39:34 +02003786psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003787 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003788 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003789{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003790 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003791}
3792
Gilles Peskinefe119512018-07-08 21:39:34 +02003793psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003794 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003795 size_t iv_size,
3796 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003797{
itayzafrir534bd7c2018-08-02 13:56:32 +03003798 psa_status_t status;
3799 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003800 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003801 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003802 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003803 }
Moran Peker41deec42018-04-04 15:43:05 +03003804 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003805 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003806 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003807 goto exit;
3808 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003809 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3810 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003811 if( ret != 0 )
3812 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003813 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003814 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003815 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003816
mohammad16038481e742018-03-18 13:57:31 +02003817 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003818 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003819
Moran Peker395db872018-05-31 14:07:14 +03003820exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003821 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003822 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003823 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003824}
3825
Gilles Peskinefe119512018-07-08 21:39:34 +02003826psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003827 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003828 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003829{
itayzafrir534bd7c2018-08-02 13:56:32 +03003830 psa_status_t status;
3831 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003832 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003833 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003834 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003835 }
Moran Pekera28258c2018-05-29 16:25:04 +03003836 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003837 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003838 status = PSA_ERROR_INVALID_ARGUMENT;
3839 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003840 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003841 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3842 status = mbedtls_to_psa_error( ret );
3843exit:
3844 if( status == PSA_SUCCESS )
3845 operation->iv_set = 1;
3846 else
mohammad1603503973b2018-03-12 15:59:30 +02003847 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003848 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003849}
3850
Gilles Peskinee553c652018-06-04 16:22:46 +02003851psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3852 const uint8_t *input,
3853 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003854 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003855 size_t output_size,
3856 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003857{
itayzafrir534bd7c2018-08-02 13:56:32 +03003858 psa_status_t status;
3859 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003860 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003861
3862 if( operation->alg == 0 )
3863 {
3864 return( PSA_ERROR_BAD_STATE );
3865 }
3866
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003867 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003868 {
3869 /* Take the unprocessed partial block left over from previous
3870 * update calls, if any, plus the input to this call. Remove
3871 * the last partial block, if any. You get the data that will be
3872 * output in this call. */
3873 expected_output_size =
3874 ( operation->ctx.cipher.unprocessed_len + input_length )
3875 / operation->block_size * operation->block_size;
3876 }
3877 else
3878 {
3879 expected_output_size = input_length;
3880 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003881
Gilles Peskine89d789c2018-06-04 17:17:16 +02003882 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003883 {
3884 status = PSA_ERROR_BUFFER_TOO_SMALL;
3885 goto exit;
3886 }
mohammad160382759612018-03-12 18:16:40 +02003887
mohammad1603503973b2018-03-12 15:59:30 +02003888 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003889 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003890 status = mbedtls_to_psa_error( ret );
3891exit:
3892 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003893 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003894 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003895}
3896
Gilles Peskinee553c652018-06-04 16:22:46 +02003897psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3898 uint8_t *output,
3899 size_t output_size,
3900 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003901{
David Saadab4ecc272019-02-14 13:48:10 +02003902 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003903 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003904 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003905
mohammad1603503973b2018-03-12 15:59:30 +02003906 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003907 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003908 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003909 }
3910 if( operation->iv_required && ! operation->iv_set )
3911 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003912 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003913 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003914
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003915 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003916 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3917 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003918 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003919 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003920 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003921 }
3922
Janos Follath315b51c2018-07-09 16:04:51 +01003923 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3924 temp_output_buffer,
3925 output_length );
3926 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003927 {
Janos Follath315b51c2018-07-09 16:04:51 +01003928 status = mbedtls_to_psa_error( cipher_ret );
3929 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003930 }
Janos Follath315b51c2018-07-09 16:04:51 +01003931
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003932 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003933 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003934 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003935 memcpy( output, temp_output_buffer, *output_length );
3936 else
3937 {
Janos Follath315b51c2018-07-09 16:04:51 +01003938 status = PSA_ERROR_BUFFER_TOO_SMALL;
3939 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003940 }
mohammad1603503973b2018-03-12 15:59:30 +02003941
Gilles Peskine3f108122018-12-07 18:14:53 +01003942 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003943 status = psa_cipher_abort( operation );
3944
3945 return( status );
3946
3947error:
3948
3949 *output_length = 0;
3950
Gilles Peskine3f108122018-12-07 18:14:53 +01003951 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003952 (void) psa_cipher_abort( operation );
3953
3954 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003955}
3956
Gilles Peskinee553c652018-06-04 16:22:46 +02003957psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3958{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003959 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003960 {
3961 /* The object has (apparently) been initialized but it is not
3962 * in use. It's ok to call abort on such an object, and there's
3963 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003964 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003965 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003966
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003967 /* Sanity check (shouldn't happen: operation->alg should
3968 * always have been initialized to a valid value). */
3969 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3970 return( PSA_ERROR_BAD_STATE );
3971
mohammad1603503973b2018-03-12 15:59:30 +02003972 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003973
Moran Peker41deec42018-04-04 15:43:05 +03003974 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003975 operation->key_set = 0;
3976 operation->iv_set = 0;
3977 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003978 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003979 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003980
Moran Peker395db872018-05-31 14:07:14 +03003981 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003982}
3983
Gilles Peskinea0655c32018-04-30 17:06:50 +02003984
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003985
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003986
mohammad16035955c982018-04-26 00:53:03 +03003987/****************************************************************/
3988/* AEAD */
3989/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003990
Gilles Peskineedf9a652018-08-17 18:11:56 +02003991typedef struct
3992{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003993 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003994 const mbedtls_cipher_info_t *cipher_info;
3995 union
3996 {
3997#if defined(MBEDTLS_CCM_C)
3998 mbedtls_ccm_context ccm;
3999#endif /* MBEDTLS_CCM_C */
4000#if defined(MBEDTLS_GCM_C)
4001 mbedtls_gcm_context gcm;
4002#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004003#if defined(MBEDTLS_CHACHAPOLY_C)
4004 mbedtls_chachapoly_context chachapoly;
4005#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004006 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004007 psa_algorithm_t core_alg;
4008 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004009 uint8_t tag_length;
4010} aead_operation_t;
4011
Gilles Peskine30a9e412019-01-14 18:36:12 +01004012static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004013{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004014 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004015 {
4016#if defined(MBEDTLS_CCM_C)
4017 case PSA_ALG_CCM:
4018 mbedtls_ccm_free( &operation->ctx.ccm );
4019 break;
4020#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004021#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004022 case PSA_ALG_GCM:
4023 mbedtls_gcm_free( &operation->ctx.gcm );
4024 break;
4025#endif /* MBEDTLS_GCM_C */
4026 }
4027}
4028
4029static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004030 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004031 psa_key_usage_t usage,
4032 psa_algorithm_t alg )
4033{
4034 psa_status_t status;
4035 size_t key_bits;
4036 mbedtls_cipher_id_t cipher_id;
4037
Gilles Peskine28f8f302019-07-24 13:30:31 +02004038 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004039 if( status != PSA_SUCCESS )
4040 return( status );
4041
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004042 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004043
4044 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004045 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004046 &cipher_id );
4047 if( operation->cipher_info == NULL )
4048 return( PSA_ERROR_NOT_SUPPORTED );
4049
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004050 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004051 {
4052#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004053 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4054 operation->core_alg = PSA_ALG_CCM;
4055 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004056 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4057 * The call to mbedtls_ccm_encrypt_and_tag or
4058 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004059 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 return( PSA_ERROR_INVALID_ARGUMENT );
4061 mbedtls_ccm_init( &operation->ctx.ccm );
4062 status = mbedtls_to_psa_error(
4063 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4064 operation->slot->data.raw.data,
4065 (unsigned int) key_bits ) );
4066 if( status != 0 )
4067 goto cleanup;
4068 break;
4069#endif /* MBEDTLS_CCM_C */
4070
4071#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004072 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4073 operation->core_alg = PSA_ALG_GCM;
4074 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004075 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4076 * The call to mbedtls_gcm_crypt_and_tag or
4077 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004078 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004079 return( PSA_ERROR_INVALID_ARGUMENT );
4080 mbedtls_gcm_init( &operation->ctx.gcm );
4081 status = mbedtls_to_psa_error(
4082 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4083 operation->slot->data.raw.data,
4084 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004085 if( status != 0 )
4086 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 break;
4088#endif /* MBEDTLS_GCM_C */
4089
Gilles Peskine26869f22019-05-06 15:25:00 +02004090#if defined(MBEDTLS_CHACHAPOLY_C)
4091 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4092 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4093 operation->full_tag_length = 16;
4094 /* We only support the default tag length. */
4095 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4096 return( PSA_ERROR_NOT_SUPPORTED );
4097 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4098 status = mbedtls_to_psa_error(
4099 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4100 operation->slot->data.raw.data ) );
4101 if( status != 0 )
4102 goto cleanup;
4103 break;
4104#endif /* MBEDTLS_CHACHAPOLY_C */
4105
Gilles Peskineedf9a652018-08-17 18:11:56 +02004106 default:
4107 return( PSA_ERROR_NOT_SUPPORTED );
4108 }
4109
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004110 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4111 {
4112 status = PSA_ERROR_INVALID_ARGUMENT;
4113 goto cleanup;
4114 }
4115 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004116
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 return( PSA_SUCCESS );
4118
4119cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004120 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004121 return( status );
4122}
4123
Gilles Peskinec5487a82018-12-03 18:08:14 +01004124psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004125 psa_algorithm_t alg,
4126 const uint8_t *nonce,
4127 size_t nonce_length,
4128 const uint8_t *additional_data,
4129 size_t additional_data_length,
4130 const uint8_t *plaintext,
4131 size_t plaintext_length,
4132 uint8_t *ciphertext,
4133 size_t ciphertext_size,
4134 size_t *ciphertext_length )
4135{
mohammad16035955c982018-04-26 00:53:03 +03004136 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004137 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004138 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004139
mohammad1603f08a5502018-06-03 15:05:47 +03004140 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004141
Gilles Peskinec5487a82018-12-03 18:08:14 +01004142 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004143 if( status != PSA_SUCCESS )
4144 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004145
Gilles Peskineedf9a652018-08-17 18:11:56 +02004146 /* For all currently supported modes, the tag is at the end of the
4147 * ciphertext. */
4148 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4149 {
4150 status = PSA_ERROR_BUFFER_TOO_SMALL;
4151 goto exit;
4152 }
4153 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004154
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004155#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004156 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004157 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004158 status = mbedtls_to_psa_error(
4159 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4160 MBEDTLS_GCM_ENCRYPT,
4161 plaintext_length,
4162 nonce, nonce_length,
4163 additional_data, additional_data_length,
4164 plaintext, ciphertext,
4165 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004166 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004167 else
4168#endif /* MBEDTLS_GCM_C */
4169#if defined(MBEDTLS_CCM_C)
4170 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004171 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004172 status = mbedtls_to_psa_error(
4173 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4174 plaintext_length,
4175 nonce, nonce_length,
4176 additional_data,
4177 additional_data_length,
4178 plaintext, ciphertext,
4179 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004180 }
mohammad16035c8845f2018-05-09 05:40:09 -07004181 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004182#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004183#if defined(MBEDTLS_CHACHAPOLY_C)
4184 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4185 {
4186 if( nonce_length != 12 || operation.tag_length != 16 )
4187 {
4188 status = PSA_ERROR_NOT_SUPPORTED;
4189 goto exit;
4190 }
4191 status = mbedtls_to_psa_error(
4192 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4193 plaintext_length,
4194 nonce,
4195 additional_data,
4196 additional_data_length,
4197 plaintext,
4198 ciphertext,
4199 tag ) );
4200 }
4201 else
4202#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004203 {
mohammad1603554faad2018-06-03 15:07:38 +03004204 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004205 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004206
Gilles Peskineedf9a652018-08-17 18:11:56 +02004207 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4208 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004209
Gilles Peskineedf9a652018-08-17 18:11:56 +02004210exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004211 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212 if( status == PSA_SUCCESS )
4213 *ciphertext_length = plaintext_length + operation.tag_length;
4214 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004215}
4216
Gilles Peskineee652a32018-06-01 19:23:52 +02004217/* Locate the tag in a ciphertext buffer containing the encrypted data
4218 * followed by the tag. Return the length of the part preceding the tag in
4219 * *plaintext_length. This is the size of the plaintext in modes where
4220 * the encrypted data has the same size as the plaintext, such as
4221 * CCM and GCM. */
4222static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4223 const uint8_t *ciphertext,
4224 size_t ciphertext_length,
4225 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004226 const uint8_t **p_tag )
4227{
4228 size_t payload_length;
4229 if( tag_length > ciphertext_length )
4230 return( PSA_ERROR_INVALID_ARGUMENT );
4231 payload_length = ciphertext_length - tag_length;
4232 if( payload_length > plaintext_size )
4233 return( PSA_ERROR_BUFFER_TOO_SMALL );
4234 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004235 return( PSA_SUCCESS );
4236}
4237
Gilles Peskinec5487a82018-12-03 18:08:14 +01004238psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004239 psa_algorithm_t alg,
4240 const uint8_t *nonce,
4241 size_t nonce_length,
4242 const uint8_t *additional_data,
4243 size_t additional_data_length,
4244 const uint8_t *ciphertext,
4245 size_t ciphertext_length,
4246 uint8_t *plaintext,
4247 size_t plaintext_size,
4248 size_t *plaintext_length )
4249{
mohammad16035955c982018-04-26 00:53:03 +03004250 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004251 aead_operation_t operation;
4252 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004253
Gilles Peskineee652a32018-06-01 19:23:52 +02004254 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004255
Gilles Peskinec5487a82018-12-03 18:08:14 +01004256 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004257 if( status != PSA_SUCCESS )
4258 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004259
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004260 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4261 ciphertext, ciphertext_length,
4262 plaintext_size, &tag );
4263 if( status != PSA_SUCCESS )
4264 goto exit;
4265
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004266#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004267 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004268 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004269 status = mbedtls_to_psa_error(
4270 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4271 ciphertext_length - operation.tag_length,
4272 nonce, nonce_length,
4273 additional_data,
4274 additional_data_length,
4275 tag, operation.tag_length,
4276 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004277 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004278 else
4279#endif /* MBEDTLS_GCM_C */
4280#if defined(MBEDTLS_CCM_C)
4281 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004282 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004283 status = mbedtls_to_psa_error(
4284 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4285 ciphertext_length - operation.tag_length,
4286 nonce, nonce_length,
4287 additional_data,
4288 additional_data_length,
4289 ciphertext, plaintext,
4290 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004291 }
mohammad160339574652018-06-01 04:39:53 -07004292 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004293#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004294#if defined(MBEDTLS_CHACHAPOLY_C)
4295 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4296 {
4297 if( nonce_length != 12 || operation.tag_length != 16 )
4298 {
4299 status = PSA_ERROR_NOT_SUPPORTED;
4300 goto exit;
4301 }
4302 status = mbedtls_to_psa_error(
4303 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4304 ciphertext_length - operation.tag_length,
4305 nonce,
4306 additional_data,
4307 additional_data_length,
4308 tag,
4309 ciphertext,
4310 plaintext ) );
4311 }
4312 else
4313#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004314 {
mohammad1603554faad2018-06-03 15:07:38 +03004315 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004316 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004317
Gilles Peskineedf9a652018-08-17 18:11:56 +02004318 if( status != PSA_SUCCESS && plaintext_size != 0 )
4319 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004320
Gilles Peskineedf9a652018-08-17 18:11:56 +02004321exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004322 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004323 if( status == PSA_SUCCESS )
4324 *plaintext_length = ciphertext_length - operation.tag_length;
4325 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004326}
4327
Gilles Peskinea0655c32018-04-30 17:06:50 +02004328
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004329
Gilles Peskine20035e32018-02-03 22:44:14 +01004330/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004331/* Generators */
4332/****************************************************************/
4333
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004334#define HKDF_STATE_INIT 0 /* no input yet */
4335#define HKDF_STATE_STARTED 1 /* got salt */
4336#define HKDF_STATE_KEYED 2 /* got key */
4337#define HKDF_STATE_OUTPUT 3 /* output started */
4338
Gilles Peskinecbe66502019-05-16 16:59:18 +02004339static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004340 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004341{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004342 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4343 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004344 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004346}
4347
4348
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004349psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004350{
4351 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004352 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004353 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004354 {
4355 /* The object has (apparently) been initialized but it is not
4356 * in use. It's ok to call abort on such an object, and there's
4357 * nothing to do. */
4358 }
4359 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004360#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004361 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004362 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 mbedtls_free( operation->ctx.hkdf.info );
4364 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004365 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004366 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004368 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004369 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004370 if( operation->ctx.tls12_prf.seed != NULL )
4371 {
4372 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4373 operation->ctx.tls12_prf.seed_length );
4374 mbedtls_free( operation->ctx.tls12_prf.seed );
4375 }
4376
4377 if( operation->ctx.tls12_prf.label != NULL )
4378 {
4379 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4380 operation->ctx.tls12_prf.label_length );
4381 mbedtls_free( operation->ctx.tls12_prf.label );
4382 }
4383
4384 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4385
4386 /* We leave the fields Ai and output_block to be erased safely by the
4387 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004388 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004389 else
4390#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004391 {
4392 status = PSA_ERROR_BAD_STATE;
4393 }
Janos Follath083036a2019-06-11 10:22:26 +01004394 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004395 return( status );
4396}
4397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004399 size_t *capacity)
4400{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004402 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004404 return PSA_ERROR_BAD_STATE;
4405 }
4406
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004407 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004408 return( PSA_SUCCESS );
4409}
4410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004412 size_t capacity )
4413{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004417 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419 return( PSA_SUCCESS );
4420}
4421
Gilles Peskinebef7f142018-07-12 17:22:21 +02004422#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004425static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004426 psa_algorithm_t hash_alg,
4427 uint8_t *output,
4428 size_t output_length )
4429{
4430 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4431 psa_status_t status;
4432
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004433 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4434 return( PSA_ERROR_BAD_STATE );
4435 hkdf->state = HKDF_STATE_OUTPUT;
4436
Gilles Peskinebef7f142018-07-12 17:22:21 +02004437 while( output_length != 0 )
4438 {
4439 /* Copy what remains of the current block */
4440 uint8_t n = hash_length - hkdf->offset_in_block;
4441 if( n > output_length )
4442 n = (uint8_t) output_length;
4443 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4444 output += n;
4445 output_length -= n;
4446 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004447 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004448 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004449 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004450 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004452 * object was corrupted or if this function is called directly
4453 * inside the library. */
4454 if( hkdf->block_number == 0xff )
4455 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004456
4457 /* We need a new block */
4458 ++hkdf->block_number;
4459 hkdf->offset_in_block = 0;
4460 status = psa_hmac_setup_internal( &hkdf->hmac,
4461 hkdf->prk, hash_length,
4462 hash_alg );
4463 if( status != PSA_SUCCESS )
4464 return( status );
4465 if( hkdf->block_number != 1 )
4466 {
4467 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4468 hkdf->output_block,
4469 hash_length );
4470 if( status != PSA_SUCCESS )
4471 return( status );
4472 }
4473 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4474 hkdf->info,
4475 hkdf->info_length );
4476 if( status != PSA_SUCCESS )
4477 return( status );
4478 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4479 &hkdf->block_number, 1 );
4480 if( status != PSA_SUCCESS )
4481 return( status );
4482 status = psa_hmac_finish_internal( &hkdf->hmac,
4483 hkdf->output_block,
4484 sizeof( hkdf->output_block ) );
4485 if( status != PSA_SUCCESS )
4486 return( status );
4487 }
4488
4489 return( PSA_SUCCESS );
4490}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004491
Janos Follath7742fee2019-06-17 12:58:10 +01004492static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4493 psa_tls12_prf_key_derivation_t *tls12_prf,
4494 psa_algorithm_t alg )
4495{
4496 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4497 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004498 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4499 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004500
Janos Follath7742fee2019-06-17 12:58:10 +01004501 /* We can't be wanting more output after block 0xff, otherwise
4502 * the capacity check in psa_key_derivation_output_bytes() would have
4503 * prevented this call. It could happen only if the operation
4504 * object was corrupted or if this function is called directly
4505 * inside the library. */
4506 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004507 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004508
4509 /* We need a new block */
4510 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004511 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004512
4513 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4514 *
4515 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4516 *
4517 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4518 * HMAC_hash(secret, A(2) + seed) +
4519 * HMAC_hash(secret, A(3) + seed) + ...
4520 *
4521 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004523 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004524 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004525 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004526 * is currently extracted as `output_block` and where i is
4527 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004528 */
4529
Janos Follathea29bfb2019-06-19 12:21:20 +01004530 /* Save the hash context before using it, to preserve the hash state with
4531 * only the inner padding in it. We need this, because inner padding depends
4532 * on the key (secret in the RFC's terminology). */
4533 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4534 if( status != PSA_SUCCESS )
4535 goto cleanup;
4536
4537 /* Calculate A(i) where i = tls12_prf->block_number. */
4538 if( tls12_prf->block_number == 1 )
4539 {
4540 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4541 * the variable seed and in this instance means it in the context of the
4542 * P_hash function, where seed = label + seed.) */
4543 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4544 tls12_prf->label, tls12_prf->label_length );
4545 if( status != PSA_SUCCESS )
4546 goto cleanup;
4547 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4548 tls12_prf->seed, tls12_prf->seed_length );
4549 if( status != PSA_SUCCESS )
4550 goto cleanup;
4551 }
4552 else
4553 {
4554 /* A(i) = HMAC_hash(secret, A(i-1)) */
4555 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4556 tls12_prf->Ai, hash_length );
4557 if( status != PSA_SUCCESS )
4558 goto cleanup;
4559 }
4560
4561 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4562 tls12_prf->Ai, hash_length );
4563 if( status != PSA_SUCCESS )
4564 goto cleanup;
4565 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4566 if( status != PSA_SUCCESS )
4567 goto cleanup;
4568
4569 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4570 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4571 tls12_prf->Ai, hash_length );
4572 if( status != PSA_SUCCESS )
4573 goto cleanup;
4574 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4575 tls12_prf->label, tls12_prf->label_length );
4576 if( status != PSA_SUCCESS )
4577 goto cleanup;
4578 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4579 tls12_prf->seed, tls12_prf->seed_length );
4580 if( status != PSA_SUCCESS )
4581 goto cleanup;
4582 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4583 tls12_prf->output_block, hash_length );
4584 if( status != PSA_SUCCESS )
4585 goto cleanup;
4586 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4587 if( status != PSA_SUCCESS )
4588 goto cleanup;
4589
Janos Follath7742fee2019-06-17 12:58:10 +01004590
4591cleanup:
4592
Janos Follathea29bfb2019-06-19 12:21:20 +01004593 cleanup_status = psa_hash_abort( &backup );
4594 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4595 status = cleanup_status;
4596
4597 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004598}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004599
Janos Follath844eb0e2019-06-19 12:10:49 +01004600static psa_status_t psa_key_derivation_tls12_prf_read(
4601 psa_tls12_prf_key_derivation_t *tls12_prf,
4602 psa_algorithm_t alg,
4603 uint8_t *output,
4604 size_t output_length )
4605{
4606 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4607 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4608 psa_status_t status;
4609 uint8_t offset, length;
4610
4611 while( output_length != 0 )
4612 {
4613 /* Check if we have fully processed the current block. */
4614 if( tls12_prf->left_in_block == 0 )
4615 {
4616 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4617 alg );
4618 if( status != PSA_SUCCESS )
4619 return( status );
4620
4621 continue;
4622 }
4623
4624 if( tls12_prf->left_in_block > output_length )
4625 length = (uint8_t) output_length;
4626 else
4627 length = tls12_prf->left_in_block;
4628
4629 offset = hash_length - tls12_prf->left_in_block;
4630 memcpy( output, tls12_prf->output_block + offset, length );
4631 output += length;
4632 output_length -= length;
4633 tls12_prf->left_in_block -= length;
4634 }
4635
4636 return( PSA_SUCCESS );
4637}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004638#endif /* MBEDTLS_MD_C */
4639
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004640psa_status_t psa_key_derivation_output_bytes(
4641 psa_key_derivation_operation_t *operation,
4642 uint8_t *output,
4643 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004644{
4645 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004649 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004651 return PSA_ERROR_BAD_STATE;
4652 }
4653
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004655 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004657 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004659 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660 goto exit;
4661 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004665 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004666 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4667 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004670 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004673
Gilles Peskinebef7f142018-07-12 17:22:21 +02004674#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004675 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004676 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004677 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004679 output, output_length );
4680 }
Janos Follathadbec812019-06-14 11:05:39 +01004681 else
Janos Follathadbec812019-06-14 11:05:39 +01004682 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004683 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004684 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004686 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004687 output_length );
4688 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004689 else
4690#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004691 {
4692 return( PSA_ERROR_BAD_STATE );
4693 }
4694
4695exit:
4696 if( status != PSA_SUCCESS )
4697 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004698 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 * This allows us to differentiate between exhausted operations and
4700 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4701 * operations. */
4702 psa_algorithm_t alg = operation->alg;
4703 psa_key_derivation_abort( operation );
4704 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004705 memset( output, '!', output_length );
4706 }
4707 return( status );
4708}
4709
Gilles Peskine08542d82018-07-19 17:05:42 +02004710#if defined(MBEDTLS_DES_C)
4711static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4712{
4713 if( data_size >= 8 )
4714 mbedtls_des_key_set_parity( data );
4715 if( data_size >= 16 )
4716 mbedtls_des_key_set_parity( data + 8 );
4717 if( data_size >= 24 )
4718 mbedtls_des_key_set_parity( data + 16 );
4719}
4720#endif /* MBEDTLS_DES_C */
4721
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004722static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004723 psa_key_slot_t *slot,
4724 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726{
4727 uint8_t *data = NULL;
4728 size_t bytes = PSA_BITS_TO_BYTES( bits );
4729 psa_status_t status;
4730
Gilles Peskine8e338702019-07-30 20:06:31 +02004731 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 return( PSA_ERROR_INVALID_ARGUMENT );
4733 if( bits % 8 != 0 )
4734 return( PSA_ERROR_INVALID_ARGUMENT );
4735 data = mbedtls_calloc( 1, bytes );
4736 if( data == NULL )
4737 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4738
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 if( status != PSA_SUCCESS )
4741 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004742#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004743 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004744 psa_des_set_key_parity( data, bytes );
4745#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004746 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004747
4748exit:
4749 mbedtls_free( data );
4750 return( status );
4751}
4752
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004753psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004755 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004756{
4757 psa_status_t status;
4758 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004759 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004760
4761 /* Reject any attempt to create a zero-length key so that we don't
4762 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4763 if( psa_get_key_bits( attributes ) == 0 )
4764 return( PSA_ERROR_INVALID_ARGUMENT );
4765
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004766 if( ! operation->can_output_key )
4767 return( PSA_ERROR_NOT_PERMITTED );
4768
Gilles Peskinedf179142019-07-15 22:02:14 +02004769 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4770 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004771#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4772 if( driver != NULL )
4773 {
4774 /* Deriving a key in a secure element is not implemented yet. */
4775 status = PSA_ERROR_NOT_SUPPORTED;
4776 }
4777#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004778 if( status == PSA_SUCCESS )
4779 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004780 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004781 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004783 }
4784 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004785 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004786 if( status != PSA_SUCCESS )
4787 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004788 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 *handle = 0;
4790 }
4791 return( status );
4792}
4793
Gilles Peskineeab56e42018-07-12 17:12:33 +02004794
4795
4796/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004797/* Key derivation */
4798/****************************************************************/
4799
Gilles Peskine969c5d62019-01-16 15:53:06 +01004800static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004802 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004803{
Janos Follath5fe19732019-06-20 15:09:30 +01004804 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4805 * initialisers for this union leave some bytes unspecified.) */
4806 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4807
Gilles Peskine969c5d62019-01-16 15:53:06 +01004808 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004809#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004810 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4811 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4812 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004813 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004814 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004815 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4816 if( hash_size == 0 )
4817 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004818 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4819 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004820 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004821 {
4822 return( PSA_ERROR_NOT_SUPPORTED );
4823 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004826 }
4827#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004828 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004830}
4831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004833 psa_algorithm_t alg )
4834{
4835 psa_status_t status;
4836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004838 return( PSA_ERROR_BAD_STATE );
4839
Gilles Peskine6843c292019-01-18 16:44:49 +01004840 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4841 return( PSA_ERROR_INVALID_ARGUMENT );
4842 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004843 {
4844 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004846 }
4847 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4848 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004850 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004851 else
4852 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853
4854 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857}
4858
4859#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004860static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004861 psa_algorithm_t hash_alg,
4862 psa_key_derivation_step_t step,
4863 const uint8_t *data,
4864 size_t data_length )
4865{
4866 psa_status_t status;
4867 switch( step )
4868 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004869 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004870 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004871 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004872 status = psa_hmac_setup_internal( &hkdf->hmac,
4873 data, data_length,
4874 hash_alg );
4875 if( status != PSA_SUCCESS )
4876 return( status );
4877 hkdf->state = HKDF_STATE_STARTED;
4878 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004879 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004880 /* If no salt was provided, use an empty salt. */
4881 if( hkdf->state == HKDF_STATE_INIT )
4882 {
4883 status = psa_hmac_setup_internal( &hkdf->hmac,
4884 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004885 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886 if( status != PSA_SUCCESS )
4887 return( status );
4888 hkdf->state = HKDF_STATE_STARTED;
4889 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004890 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004891 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004892 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4893 data, data_length );
4894 if( status != PSA_SUCCESS )
4895 return( status );
4896 status = psa_hmac_finish_internal( &hkdf->hmac,
4897 hkdf->prk,
4898 sizeof( hkdf->prk ) );
4899 if( status != PSA_SUCCESS )
4900 return( status );
4901 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4902 hkdf->block_number = 0;
4903 hkdf->state = HKDF_STATE_KEYED;
4904 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004905 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004906 if( hkdf->state == HKDF_STATE_OUTPUT )
4907 return( PSA_ERROR_BAD_STATE );
4908 if( hkdf->info_set )
4909 return( PSA_ERROR_BAD_STATE );
4910 hkdf->info_length = data_length;
4911 if( data_length != 0 )
4912 {
4913 hkdf->info = mbedtls_calloc( 1, data_length );
4914 if( hkdf->info == NULL )
4915 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4916 memcpy( hkdf->info, data, data_length );
4917 }
4918 hkdf->info_set = 1;
4919 return( PSA_SUCCESS );
4920 default:
4921 return( PSA_ERROR_INVALID_ARGUMENT );
4922 }
4923}
Janos Follathb03233e2019-06-11 15:30:30 +01004924
Janos Follathf08e2652019-06-13 09:05:41 +01004925static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4926 const uint8_t *data,
4927 size_t data_length )
4928{
4929 if( prf->state != TLS12_PRF_STATE_INIT )
4930 return( PSA_ERROR_BAD_STATE );
4931
Janos Follathd6dce9f2019-07-04 09:11:38 +01004932 if( data_length != 0 )
4933 {
4934 prf->seed = mbedtls_calloc( 1, data_length );
4935 if( prf->seed == NULL )
4936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004937
Janos Follathd6dce9f2019-07-04 09:11:38 +01004938 memcpy( prf->seed, data, data_length );
4939 prf->seed_length = data_length;
4940 }
Janos Follathf08e2652019-06-13 09:05:41 +01004941
4942 prf->state = TLS12_PRF_STATE_SEED_SET;
4943
4944 return( PSA_SUCCESS );
4945}
4946
Janos Follath81550542019-06-13 14:26:34 +01004947static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4948 psa_algorithm_t hash_alg,
4949 const uint8_t *data,
4950 size_t data_length )
4951{
4952 psa_status_t status;
4953 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4954 return( PSA_ERROR_BAD_STATE );
4955
4956 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4957 if( status != PSA_SUCCESS )
4958 return( status );
4959
4960 prf->state = TLS12_PRF_STATE_KEY_SET;
4961
4962 return( PSA_SUCCESS );
4963}
4964
Janos Follath6660f0e2019-06-17 08:44:03 +01004965static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4966 psa_tls12_prf_key_derivation_t *prf,
4967 psa_algorithm_t hash_alg,
4968 const uint8_t *data,
4969 size_t data_length )
4970{
4971 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004972 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4973 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004974
4975 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4976 return( PSA_ERROR_INVALID_ARGUMENT );
4977
4978 /* Quoting RFC 4279, Section 2:
4979 *
4980 * The premaster secret is formed as follows: if the PSK is N octets
4981 * long, concatenate a uint16 with the value N, N zero octets, a second
4982 * uint16 with the value N, and the PSK itself.
4983 */
4984
Janos Follath40e13932019-06-26 13:22:29 +01004985 *cur++ = ( data_length >> 8 ) & 0xff;
4986 *cur++ = ( data_length >> 0 ) & 0xff;
4987 memset( cur, 0, data_length );
4988 cur += data_length;
4989 *cur++ = pms[0];
4990 *cur++ = pms[1];
4991 memcpy( cur, data, data_length );
4992 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004993
Janos Follath40e13932019-06-26 13:22:29 +01004994 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01004995
4996 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4997 return( status );
4998}
4999
Janos Follath63028dd2019-06-13 09:15:47 +01005000static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5001 const uint8_t *data,
5002 size_t data_length )
5003{
5004 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5005 return( PSA_ERROR_BAD_STATE );
5006
Janos Follathd6dce9f2019-07-04 09:11:38 +01005007 if( data_length != 0 )
5008 {
5009 prf->label = mbedtls_calloc( 1, data_length );
5010 if( prf->label == NULL )
5011 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005012
Janos Follathd6dce9f2019-07-04 09:11:38 +01005013 memcpy( prf->label, data, data_length );
5014 prf->label_length = data_length;
5015 }
Janos Follath63028dd2019-06-13 09:15:47 +01005016
5017 prf->state = TLS12_PRF_STATE_LABEL_SET;
5018
5019 return( PSA_SUCCESS );
5020}
5021
Janos Follathb03233e2019-06-11 15:30:30 +01005022static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5023 psa_algorithm_t hash_alg,
5024 psa_key_derivation_step_t step,
5025 const uint8_t *data,
5026 size_t data_length )
5027{
Janos Follathb03233e2019-06-11 15:30:30 +01005028 switch( step )
5029 {
Janos Follathf08e2652019-06-13 09:05:41 +01005030 case PSA_KEY_DERIVATION_INPUT_SEED:
5031 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005032 case PSA_KEY_DERIVATION_INPUT_SECRET:
5033 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005034 case PSA_KEY_DERIVATION_INPUT_LABEL:
5035 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005036 default:
5037 return( PSA_ERROR_INVALID_ARGUMENT );
5038 }
5039}
Janos Follath6660f0e2019-06-17 08:44:03 +01005040
5041static psa_status_t psa_tls12_prf_psk_to_ms_input(
5042 psa_tls12_prf_key_derivation_t *prf,
5043 psa_algorithm_t hash_alg,
5044 psa_key_derivation_step_t step,
5045 const uint8_t *data,
5046 size_t data_length )
5047{
5048 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005049 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005050 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5051 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005052 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005053
5054 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5055}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056#endif /* MBEDTLS_MD_C */
5057
Gilles Peskineb8965192019-09-24 16:21:10 +02005058/** Check whether the given key type is acceptable for the given
5059 * input step of a key derivation.
5060 *
5061 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5062 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5063 * Both secret and non-secret inputs can alternatively have the type
5064 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5065 * that the input was passed as a buffer rather than via a key object.
5066 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005067static int psa_key_derivation_check_input_type(
5068 psa_key_derivation_step_t step,
5069 psa_key_type_t key_type )
5070{
5071 switch( step )
5072 {
5073 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005074 if( key_type == PSA_KEY_TYPE_DERIVE )
5075 return( PSA_SUCCESS );
5076 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005077 return( PSA_SUCCESS );
5078 break;
5079 case PSA_KEY_DERIVATION_INPUT_LABEL:
5080 case PSA_KEY_DERIVATION_INPUT_SALT:
5081 case PSA_KEY_DERIVATION_INPUT_INFO:
5082 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005083 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5084 return( PSA_SUCCESS );
5085 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005086 return( PSA_SUCCESS );
5087 break;
5088 }
5089 return( PSA_ERROR_INVALID_ARGUMENT );
5090}
5091
Janos Follathb80a94e2019-06-12 15:54:46 +01005092static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005094 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005095 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005096 const uint8_t *data,
5097 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005098{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005099 psa_status_t status;
5100 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5101
5102 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005103 if( status != PSA_SUCCESS )
5104 goto exit;
5105
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005106#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005107 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005108 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005110 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005111 step, data, data_length );
5112 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005113 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114 {
Janos Follathb03233e2019-06-11 15:30:30 +01005115 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5116 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5117 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005118 }
5119 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5120 {
5121 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5122 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5123 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005124 }
5125 else
5126#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005127 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005129 return( PSA_ERROR_BAD_STATE );
5130 }
5131
Gilles Peskine224b0d62019-09-23 18:13:17 +02005132exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135 return( status );
5136}
5137
Janos Follath51f4a0f2019-06-14 11:35:55 +01005138psa_status_t psa_key_derivation_input_bytes(
5139 psa_key_derivation_operation_t *operation,
5140 psa_key_derivation_step_t step,
5141 const uint8_t *data,
5142 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005143{
Gilles Peskineb8965192019-09-24 16:21:10 +02005144 return( psa_key_derivation_input_internal( operation, step,
5145 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005146 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005147}
5148
Janos Follath51f4a0f2019-06-14 11:35:55 +01005149psa_status_t psa_key_derivation_input_key(
5150 psa_key_derivation_operation_t *operation,
5151 psa_key_derivation_step_t step,
5152 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005153{
5154 psa_key_slot_t *slot;
5155 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005156
Gilles Peskine28f8f302019-07-24 13:30:31 +02005157 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005158 PSA_KEY_USAGE_DERIVE,
5159 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005160 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005161 {
5162 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005163 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005164 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005165
5166 /* Passing a key object as a SECRET input unlocks the permission
5167 * to output to a key object. */
5168 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5169 operation->can_output_key = 1;
5170
Janos Follathb80a94e2019-06-12 15:54:46 +01005171 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005172 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005173 slot->data.raw.data,
5174 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005176
5177
5178
5179/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005180/* Key agreement */
5181/****************************************************************/
5182
Gilles Peskinea05219c2018-11-16 16:02:56 +01005183#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005184static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5185 size_t peer_key_length,
5186 const mbedtls_ecp_keypair *our_key,
5187 uint8_t *shared_secret,
5188 size_t shared_secret_size,
5189 size_t *shared_secret_length )
5190{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005191 mbedtls_ecp_keypair *their_key = NULL;
5192 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005193 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005194 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005195
Jaeden Ameroccdce902019-01-10 11:42:27 +00005196 status = psa_import_ec_public_key(
5197 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5198 peer_key, peer_key_length,
5199 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005200 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005201 goto exit;
5202
Jaeden Amero97271b32019-01-10 19:38:51 +00005203 status = mbedtls_to_psa_error(
5204 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5205 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005206 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005207 status = mbedtls_to_psa_error(
5208 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5209 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005210 goto exit;
5211
Jaeden Amero97271b32019-01-10 19:38:51 +00005212 status = mbedtls_to_psa_error(
5213 mbedtls_ecdh_calc_secret( &ecdh,
5214 shared_secret_length,
5215 shared_secret, shared_secret_size,
5216 mbedtls_ctr_drbg_random,
5217 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005218
5219exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005220 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005221 mbedtls_ecp_keypair_free( their_key );
5222 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005223 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005224}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005225#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005226
Gilles Peskine01d718c2018-09-18 12:01:02 +02005227#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5228
Gilles Peskine0216fe12019-04-11 21:23:21 +02005229static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5230 psa_key_slot_t *private_key,
5231 const uint8_t *peer_key,
5232 size_t peer_key_length,
5233 uint8_t *shared_secret,
5234 size_t shared_secret_size,
5235 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005236{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005237 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005238 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005239#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005240 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005241 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005242 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005243 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5244 private_key->data.ecp,
5245 shared_secret, shared_secret_size,
5246 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005247#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005248 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005249 (void) private_key;
5250 (void) peer_key;
5251 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005252 (void) shared_secret;
5253 (void) shared_secret_size;
5254 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005255 return( PSA_ERROR_NOT_SUPPORTED );
5256 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005257}
5258
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005259/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260 * to potentially free embedded data structures and wipe confidential data.
5261 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005263 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005264 psa_key_slot_t *private_key,
5265 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005266 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005267{
5268 psa_status_t status;
5269 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5270 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005272
5273 /* Step 1: run the secret agreement algorithm to generate the shared
5274 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005275 status = psa_key_agreement_raw_internal( ka_alg,
5276 private_key,
5277 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005278 shared_secret,
5279 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005280 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005281 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005282 goto exit;
5283
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005284 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005285 * the shared secret. A shared secret is permitted wherever a key
5286 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005287 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005288 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005289 shared_secret,
5290 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005291
Gilles Peskine12313cd2018-06-20 00:20:32 +02005292exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005293 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005294 return( status );
5295}
5296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005297psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005298 psa_key_derivation_step_t step,
5299 psa_key_handle_t private_key,
5300 const uint8_t *peer_key,
5301 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005302{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005303 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005304 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005305 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005306 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005307 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005308 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005309 if( status != PSA_SUCCESS )
5310 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005312 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005313 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005314 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005316 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005317}
5318
Gilles Peskinebe697d82019-05-16 18:00:41 +02005319psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5320 psa_key_handle_t private_key,
5321 const uint8_t *peer_key,
5322 size_t peer_key_length,
5323 uint8_t *output,
5324 size_t output_size,
5325 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005326{
5327 psa_key_slot_t *slot;
5328 psa_status_t status;
5329
5330 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5331 {
5332 status = PSA_ERROR_INVALID_ARGUMENT;
5333 goto exit;
5334 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005335 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005336 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005337 if( status != PSA_SUCCESS )
5338 goto exit;
5339
5340 status = psa_key_agreement_raw_internal( alg, slot,
5341 peer_key, peer_key_length,
5342 output, output_size,
5343 output_length );
5344
5345exit:
5346 if( status != PSA_SUCCESS )
5347 {
5348 /* If an error happens and is not handled properly, the output
5349 * may be used as a key to protect sensitive data. Arrange for such
5350 * a key to be random, which is likely to result in decryption or
5351 * verification errors. This is better than filling the buffer with
5352 * some constant data such as zeros, which would result in the data
5353 * being protected with a reproducible, easily knowable key.
5354 */
5355 psa_generate_random( output, output_size );
5356 *output_length = output_size;
5357 }
5358 return( status );
5359}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005360
5361
5362/****************************************************************/
5363/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005364/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005365
Gilles Peskine4c317f42018-07-12 01:24:09 +02005366psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005367 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005369 int ret;
5370 GUARD_MODULE_INITIALIZED;
5371
Gilles Peskinef181eca2019-08-07 13:49:00 +02005372 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5373 {
5374 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5375 output,
5376 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5377 if( ret != 0 )
5378 return( mbedtls_to_psa_error( ret ) );
5379 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5380 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5381 }
5382
Gilles Peskinee59236f2018-01-27 23:32:46 +01005383 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5384 return( mbedtls_to_psa_error( ret ) );
5385}
5386
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005387#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5388#include "mbedtls/entropy_poll.h"
5389
Gilles Peskine7228da22019-07-15 11:06:15 +02005390psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005391 size_t seed_size )
5392{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005393 if( global_data.initialized )
5394 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005395
5396 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5397 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5398 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5399 return( PSA_ERROR_INVALID_ARGUMENT );
5400
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005401 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005402}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005403#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005404
Gilles Peskinee56e8782019-04-26 17:34:02 +02005405#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5406static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5407 size_t domain_parameters_size,
5408 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005409{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005410 size_t i;
5411 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005412
Gilles Peskinee56e8782019-04-26 17:34:02 +02005413 if( domain_parameters_size == 0 )
5414 {
5415 *exponent = 65537;
5416 return( PSA_SUCCESS );
5417 }
5418
5419 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5420 * support values that fit in a 32-bit integer, which is larger than
5421 * int on just about every platform anyway. */
5422 if( domain_parameters_size > sizeof( acc ) )
5423 return( PSA_ERROR_NOT_SUPPORTED );
5424 for( i = 0; i < domain_parameters_size; i++ )
5425 acc = ( acc << 8 ) | domain_parameters[i];
5426 if( acc > INT_MAX )
5427 return( PSA_ERROR_NOT_SUPPORTED );
5428 *exponent = acc;
5429 return( PSA_SUCCESS );
5430}
5431#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5432
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005433static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005434 psa_key_slot_t *slot, size_t bits,
5435 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005436{
Gilles Peskine8e338702019-07-30 20:06:31 +02005437 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005438
Gilles Peskinee56e8782019-04-26 17:34:02 +02005439 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005440 return( PSA_ERROR_INVALID_ARGUMENT );
5441
Gilles Peskinee59236f2018-01-27 23:32:46 +01005442 if( key_type_is_raw_bytes( type ) )
5443 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005444 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005445 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5446 if( status != PSA_SUCCESS )
5447 return( status );
5448 status = psa_generate_random( slot->data.raw.data,
5449 slot->data.raw.bytes );
5450 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005451 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452#if defined(MBEDTLS_DES_C)
5453 if( type == PSA_KEY_TYPE_DES )
5454 psa_des_set_key_parity( slot->data.raw.data,
5455 slot->data.raw.bytes );
5456#endif /* MBEDTLS_DES_C */
5457 }
5458 else
5459
5460#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005461 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005462 {
5463 mbedtls_rsa_context *rsa;
5464 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005465 int exponent;
5466 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005467 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5468 return( PSA_ERROR_NOT_SUPPORTED );
5469 /* Accept only byte-aligned keys, for the same reasons as
5470 * in psa_import_rsa_key(). */
5471 if( bits % 8 != 0 )
5472 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005473 status = psa_read_rsa_exponent( domain_parameters,
5474 domain_parameters_size,
5475 &exponent );
5476 if( status != PSA_SUCCESS )
5477 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005478 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5479 if( rsa == NULL )
5480 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5481 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5482 ret = mbedtls_rsa_gen_key( rsa,
5483 mbedtls_ctr_drbg_random,
5484 &global_data.ctr_drbg,
5485 (unsigned int) bits,
5486 exponent );
5487 if( ret != 0 )
5488 {
5489 mbedtls_rsa_free( rsa );
5490 mbedtls_free( rsa );
5491 return( mbedtls_to_psa_error( ret ) );
5492 }
5493 slot->data.rsa = rsa;
5494 }
5495 else
5496#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5497
5498#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005499 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005500 {
5501 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5502 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5503 const mbedtls_ecp_curve_info *curve_info =
5504 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5505 mbedtls_ecp_keypair *ecp;
5506 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005507 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005508 return( PSA_ERROR_NOT_SUPPORTED );
5509 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5510 return( PSA_ERROR_NOT_SUPPORTED );
5511 if( curve_info->bit_size != bits )
5512 return( PSA_ERROR_INVALID_ARGUMENT );
5513 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5514 if( ecp == NULL )
5515 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5516 mbedtls_ecp_keypair_init( ecp );
5517 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5518 mbedtls_ctr_drbg_random,
5519 &global_data.ctr_drbg );
5520 if( ret != 0 )
5521 {
5522 mbedtls_ecp_keypair_free( ecp );
5523 mbedtls_free( ecp );
5524 return( mbedtls_to_psa_error( ret ) );
5525 }
5526 slot->data.ecp = ecp;
5527 }
5528 else
5529#endif /* MBEDTLS_ECP_C */
5530
5531 return( PSA_ERROR_NOT_SUPPORTED );
5532
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005533 return( PSA_SUCCESS );
5534}
Darryl Green0c6575a2018-11-07 16:05:30 +00005535
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005536psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005537 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538{
5539 psa_status_t status;
5540 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005541 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005542
Gilles Peskine0f84d622019-09-12 19:03:13 +02005543 /* Reject any attempt to create a zero-length key so that we don't
5544 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5545 if( psa_get_key_bits( attributes ) == 0 )
5546 return( PSA_ERROR_INVALID_ARGUMENT );
5547
Gilles Peskinedf179142019-07-15 22:02:14 +02005548 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5549 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005550 if( status != PSA_SUCCESS )
5551 goto exit;
5552
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005553#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5554 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005555 {
Gilles Peskine11792082019-08-06 18:36:36 +02005556 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5557 size_t pubkey_length = 0; /* We don't support this feature yet */
5558 if( drv->key_management == NULL ||
5559 drv->key_management->p_generate == NULL )
5560 {
5561 status = PSA_ERROR_NOT_SUPPORTED;
5562 goto exit;
5563 }
5564 status = drv->key_management->p_generate(
5565 psa_get_se_driver_context( driver ),
5566 slot->data.se.slot_number, attributes,
5567 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005568 }
Gilles Peskine11792082019-08-06 18:36:36 +02005569 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005570#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005571 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005572 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005573 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005574 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005575 }
Gilles Peskine11792082019-08-06 18:36:36 +02005576
5577exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005578 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005579 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005580 if( status != PSA_SUCCESS )
5581 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005582 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005583 *handle = 0;
5584 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005585 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005586}
5587
5588
Gilles Peskinee59236f2018-01-27 23:32:46 +01005589
5590/****************************************************************/
5591/* Module setup */
5592/****************************************************************/
5593
Gilles Peskine5e769522018-11-20 21:59:56 +01005594psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5595 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5596 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5597{
5598 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5599 return( PSA_ERROR_BAD_STATE );
5600 global_data.entropy_init = entropy_init;
5601 global_data.entropy_free = entropy_free;
5602 return( PSA_SUCCESS );
5603}
5604
Gilles Peskinee59236f2018-01-27 23:32:46 +01005605void mbedtls_psa_crypto_free( void )
5606{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005607 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005608 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5609 {
5610 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005611 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005612 }
5613 /* Wipe all remaining data, including configuration.
5614 * In particular, this sets all state indicator to the value
5615 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005616 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005617#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005618 /* Unregister all secure element drivers, so that we restart from
5619 * a pristine state. */
5620 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005621#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005622}
5623
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005624#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5625/** Recover a transaction that was interrupted by a power failure.
5626 *
5627 * This function is called during initialization, before psa_crypto_init()
5628 * returns. If this function returns a failure status, the initialization
5629 * fails.
5630 */
5631static psa_status_t psa_crypto_recover_transaction(
5632 const psa_crypto_transaction_t *transaction )
5633{
5634 switch( transaction->unknown.type )
5635 {
5636 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5637 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005638 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005639 * is implemented.
5640 * https://github.com/ARMmbed/mbed-crypto/issues/218
5641 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005642 default:
5643 /* We found an unsupported transaction in the storage.
5644 * We don't know what state the storage is in. Give up. */
5645 return( PSA_ERROR_STORAGE_FAILURE );
5646 }
5647}
5648#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5649
Gilles Peskinee59236f2018-01-27 23:32:46 +01005650psa_status_t psa_crypto_init( void )
5651{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005652 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005653 const unsigned char drbg_seed[] = "PSA";
5654
Gilles Peskinec6b69072018-11-20 21:42:52 +01005655 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005656 if( global_data.initialized != 0 )
5657 return( PSA_SUCCESS );
5658
Gilles Peskine5e769522018-11-20 21:59:56 +01005659 /* Set default configuration if
5660 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5661 if( global_data.entropy_init == NULL )
5662 global_data.entropy_init = mbedtls_entropy_init;
5663 if( global_data.entropy_free == NULL )
5664 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005665
Gilles Peskinec6b69072018-11-20 21:42:52 +01005666 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005667 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005668#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5669 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5670 /* The PSA entropy injection feature depends on using NV seed as an entropy
5671 * source. Add NV seed as an entropy source for PSA entropy injection. */
5672 mbedtls_entropy_add_source( &global_data.entropy,
5673 mbedtls_nv_seed_poll, NULL,
5674 MBEDTLS_ENTROPY_BLOCK_SIZE,
5675 MBEDTLS_ENTROPY_SOURCE_STRONG );
5676#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005677 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005678 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005679 status = mbedtls_to_psa_error(
5680 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5681 mbedtls_entropy_func,
5682 &global_data.entropy,
5683 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5684 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005685 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005686 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005687
Gilles Peskine66fb1262018-12-10 16:29:04 +01005688 status = psa_initialize_key_slots( );
5689 if( status != PSA_SUCCESS )
5690 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005691
Gilles Peskined9348f22019-10-01 15:22:29 +02005692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5693 status = psa_init_all_se_drivers( );
5694 if( status != PSA_SUCCESS )
5695 goto exit;
5696#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5697
Gilles Peskine4b734222019-07-24 15:56:31 +02005698#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005699 status = psa_crypto_load_transaction( );
5700 if( status == PSA_SUCCESS )
5701 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005702 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5703 if( status != PSA_SUCCESS )
5704 goto exit;
5705 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005706 }
5707 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5708 {
5709 /* There's no transaction to complete. It's all good. */
5710 status = PSA_SUCCESS;
5711 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005712#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005713
Gilles Peskinec6b69072018-11-20 21:42:52 +01005714 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005715 global_data.initialized = 1;
5716
5717exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005718 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005719 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005720 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005721}
5722
5723#endif /* MBEDTLS_PSA_CRYPTO_C */