blob: 25aff019ab5d9f48836539483555107bdd1b9c8f [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 Peskine1841cf42019-10-08 15:48:25 +02001016 if( handle == 0 )
1017 return( PSA_SUCCESS );
1018
Gilles Peskinec5487a82018-12-03 18:08:14 +01001019 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 if( status != PSA_SUCCESS )
1021 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022
1023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001024 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001025 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001026 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001027 /* For a key in a secure element, we need to do three things:
1028 * remove the key file in internal storage, destroy the
1029 * key inside the secure element, and update the driver's
1030 * persistent data. Start a transaction that will encompass these
1031 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001033 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001034 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 status = psa_crypto_save_transaction( );
1037 if( status != PSA_SUCCESS )
1038 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001039 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001040 /* We should still try to destroy the key in the secure
1041 * element and the key metadata in storage. This is especially
1042 * important if the error is that the storage is full.
1043 * But how to do it exactly without risking an inconsistent
1044 * state after a reset?
1045 * https://github.com/ARMmbed/mbed-crypto/issues/215
1046 */
1047 overall_status = status;
1048 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 }
1050
Gilles Peskine354f7672019-07-12 23:46:38 +02001051 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 if( overall_status == PSA_SUCCESS )
1053 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001054 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001055#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1056
Darryl Greend49a4992018-06-18 17:27:26 +01001057#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001058 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001059 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001060 status = psa_destroy_persistent_key( slot->attr.id );
1061 if( overall_status == PSA_SUCCESS )
1062 overall_status = status;
1063
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001064 /* TODO: other slots may have a copy of the same key. We should
1065 * invalidate them.
1066 * https://github.com/ARMmbed/mbed-crypto/issues/214
1067 */
Darryl Greend49a4992018-06-18 17:27:26 +01001068 }
1069#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001070
Gilles Peskinefc762652019-07-22 19:30:34 +02001071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1072 if( driver != NULL )
1073 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001074 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
1077 status = psa_crypto_stop_transaction( );
1078 if( overall_status == PSA_SUCCESS )
1079 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001080 }
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1082
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1084exit:
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001086 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1088 if( overall_status == PSA_SUCCESS )
1089 overall_status = status;
1090 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091}
1092
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001094{
Gilles Peskineb699f072019-04-26 16:06:02 +02001095 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001096 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001097}
1098
Gilles Peskineb699f072019-04-26 16:06:02 +02001099psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1100 psa_key_type_t type,
1101 const uint8_t *data,
1102 size_t data_length )
1103{
1104 uint8_t *copy = NULL;
1105
1106 if( data_length != 0 )
1107 {
1108 copy = mbedtls_calloc( 1, data_length );
1109 if( copy == NULL )
1110 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1111 memcpy( copy, data, data_length );
1112 }
1113 /* After this point, this function is guaranteed to succeed, so it
1114 * can start modifying `*attributes`. */
1115
1116 if( attributes->domain_parameters != NULL )
1117 {
1118 mbedtls_free( attributes->domain_parameters );
1119 attributes->domain_parameters = NULL;
1120 attributes->domain_parameters_size = 0;
1121 }
1122
1123 attributes->domain_parameters = copy;
1124 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001125 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001126 return( PSA_SUCCESS );
1127}
1128
1129psa_status_t psa_get_key_domain_parameters(
1130 const psa_key_attributes_t *attributes,
1131 uint8_t *data, size_t data_size, size_t *data_length )
1132{
1133 if( attributes->domain_parameters_size > data_size )
1134 return( PSA_ERROR_BUFFER_TOO_SMALL );
1135 *data_length = attributes->domain_parameters_size;
1136 if( attributes->domain_parameters_size != 0 )
1137 memcpy( data, attributes->domain_parameters,
1138 attributes->domain_parameters_size );
1139 return( PSA_SUCCESS );
1140}
1141
1142#if defined(MBEDTLS_RSA_C)
1143static psa_status_t psa_get_rsa_public_exponent(
1144 const mbedtls_rsa_context *rsa,
1145 psa_key_attributes_t *attributes )
1146{
1147 mbedtls_mpi mpi;
1148 int ret;
1149 uint8_t *buffer = NULL;
1150 size_t buflen;
1151 mbedtls_mpi_init( &mpi );
1152
1153 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1154 if( ret != 0 )
1155 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001156 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1157 {
1158 /* It's the default value, which is reported as an empty string,
1159 * so there's nothing to do. */
1160 goto exit;
1161 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001162
1163 buflen = mbedtls_mpi_size( &mpi );
1164 buffer = mbedtls_calloc( 1, buflen );
1165 if( buffer == NULL )
1166 {
1167 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1168 goto exit;
1169 }
1170 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1171 if( ret != 0 )
1172 goto exit;
1173 attributes->domain_parameters = buffer;
1174 attributes->domain_parameters_size = buflen;
1175
1176exit:
1177 mbedtls_mpi_free( &mpi );
1178 if( ret != 0 )
1179 mbedtls_free( buffer );
1180 return( mbedtls_to_psa_error( ret ) );
1181}
1182#endif /* MBEDTLS_RSA_C */
1183
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001184/** Retrieve all the publicly-accessible attributes of a key.
1185 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001186psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1187 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001189 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001190 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001192 psa_reset_key_attributes( attributes );
1193
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001194 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001195 if( status != PSA_SUCCESS )
1196 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001197
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001198 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001199 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1200 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1201
1202#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1203 if( psa_key_slot_is_external( slot ) )
1204 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001206
Gilles Peskine8e338702019-07-30 20:06:31 +02001207 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 {
1209#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001210 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001212#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001213 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001214 * is not yet implemented.
1215 * https://github.com/ARMmbed/mbed-crypto/issues/216
1216 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001217 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001218 break;
1219#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1221 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001222#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 default:
1224 /* Nothing else to do. */
1225 break;
1226 }
1227
1228 if( status != PSA_SUCCESS )
1229 psa_reset_key_attributes( attributes );
1230 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231}
1232
Gilles Peskinec8000c02019-08-02 20:15:51 +02001233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1234psa_status_t psa_get_key_slot_number(
1235 const psa_key_attributes_t *attributes,
1236 psa_key_slot_number_t *slot_number )
1237{
1238 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1239 {
1240 *slot_number = attributes->slot_number;
1241 return( PSA_SUCCESS );
1242 }
1243 else
1244 return( PSA_ERROR_INVALID_ARGUMENT );
1245}
1246#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1247
Jaeden Ameroccdce902019-01-10 11:42:27 +00001248#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001249static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1250 unsigned char *buf, size_t size )
1251{
1252 int ret;
1253 unsigned char *c;
1254 size_t len = 0;
1255
1256 c = buf + size;
1257
1258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1259
1260 return( (int) len );
1261}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001262#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001263
Gilles Peskinef603c712019-01-19 13:40:11 +01001264static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1265 uint8_t *data,
1266 size_t data_size,
1267 size_t *data_length,
1268 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269{
Gilles Peskine5d309672019-07-12 23:47:28 +02001270#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1271 const psa_drv_se_t *drv;
1272 psa_drv_se_context_t *drv_context;
1273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1274
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001275 *data_length = 0;
1276
Gilles Peskine8e338702019-07-30 20:06:31 +02001277 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001278 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001279
Gilles Peskinef9168942019-09-12 19:20:29 +02001280 /* Reject a zero-length output buffer now, since this can never be a
1281 * valid key representation. This way we know that data must be a valid
1282 * pointer and we can do things like memset(data, ..., data_size). */
1283 if( data_size == 0 )
1284 return( PSA_ERROR_BUFFER_TOO_SMALL );
1285
Gilles Peskine5d309672019-07-12 23:47:28 +02001286#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001287 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001288 {
1289 psa_drv_se_export_key_t method;
1290 if( drv->key_management == NULL )
1291 return( PSA_ERROR_NOT_SUPPORTED );
1292 method = ( export_public_key ?
1293 drv->key_management->p_export_public :
1294 drv->key_management->p_export );
1295 if( method == NULL )
1296 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001297 return( method( drv_context,
1298 slot->data.se.slot_number,
1299 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001300 }
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1302
Gilles Peskine8e338702019-07-30 20:06:31 +02001303 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304 {
1305 if( slot->data.raw.bytes > data_size )
1306 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001307 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1308 memset( data + slot->data.raw.bytes, 0,
1309 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310 *data_length = slot->data.raw.bytes;
1311 return( PSA_SUCCESS );
1312 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001313#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001315 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001316 psa_status_t status;
1317
Gilles Peskineb46bef22019-07-30 21:32:04 +02001318 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001319 if( bytes > data_size )
1320 return( PSA_ERROR_BUFFER_TOO_SMALL );
1321 status = mbedtls_to_psa_error(
1322 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1323 if( status != PSA_SUCCESS )
1324 return( status );
1325 memset( data + bytes, 0, data_size - bytes );
1326 *data_length = bytes;
1327 return( PSA_SUCCESS );
1328 }
1329#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330 else
Moran Peker17e36e12018-05-02 12:55:20 +03001331 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001332#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001333 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1334 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001335 {
Moran Pekera998bc62018-04-16 18:16:20 +03001336 mbedtls_pk_context pk;
1337 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001338 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001339 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001340#if defined(MBEDTLS_RSA_C)
1341 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001342 pk.pk_info = &mbedtls_rsa_info;
1343 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001344#else
1345 return( PSA_ERROR_NOT_SUPPORTED );
1346#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001347 }
1348 else
1349 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001350#if defined(MBEDTLS_ECP_C)
1351 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001352 pk.pk_info = &mbedtls_eckey_info;
1353 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001354#else
1355 return( PSA_ERROR_NOT_SUPPORTED );
1356#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001357 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001358 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001359 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001360 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001361 }
Moran Peker17e36e12018-05-02 12:55:20 +03001362 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001363 {
Moran Peker17e36e12018-05-02 12:55:20 +03001364 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001365 }
Moran Peker60364322018-04-29 11:34:58 +03001366 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001368 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001369 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001370 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001371 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1372 * Move the data to the beginning and erase remaining data
1373 * at the original location. */
1374 if( 2 * (size_t) ret <= data_size )
1375 {
1376 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001377 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001378 }
1379 else if( (size_t) ret < data_size )
1380 {
1381 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001382 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001383 }
Moran Pekera998bc62018-04-16 18:16:20 +03001384 *data_length = ret;
1385 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001386 }
1387 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001388#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001389 {
1390 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001391 it is valid for a special-purpose implementation to omit
1392 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 return( PSA_ERROR_NOT_SUPPORTED );
1394 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395 }
1396}
1397
Gilles Peskinec5487a82018-12-03 18:08:14 +01001398psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001399 uint8_t *data,
1400 size_t data_size,
1401 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001402{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001403 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001404 psa_status_t status;
1405
1406 /* Set the key to empty now, so that even when there are errors, we always
1407 * set data_length to a value between 0 and data_size. On error, setting
1408 * the key to empty is a good choice because an empty key representation is
1409 * unlikely to be accepted anywhere. */
1410 *data_length = 0;
1411
1412 /* Export requires the EXPORT flag. There is an exception for public keys,
1413 * which don't require any flag, but psa_get_key_from_slot takes
1414 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001415 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001416 if( status != PSA_SUCCESS )
1417 return( status );
1418 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001419 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001420}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421
Gilles Peskinec5487a82018-12-03 18:08:14 +01001422psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001423 uint8_t *data,
1424 size_t data_size,
1425 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001427 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 psa_status_t status;
1429
1430 /* Set the key to empty now, so that even when there are errors, we always
1431 * set data_length to a value between 0 and data_size. On error, setting
1432 * the key to empty is a good choice because an empty key representation is
1433 * unlikely to be accepted anywhere. */
1434 *data_length = 0;
1435
1436 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001437 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001438 if( status != PSA_SUCCESS )
1439 return( status );
1440 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001441 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001442}
1443
Gilles Peskine91e8c332019-08-02 19:19:39 +02001444#if defined(static_assert)
1445static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1446 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001447static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001448 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001449static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001450 "One or more key attribute flag is listed as both internal-only and external-only" );
1451#endif
1452
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001453/** Validate that a key policy is internally well-formed.
1454 *
1455 * This function only rejects invalid policies. It does not validate the
1456 * consistency of the policy with respect to other attributes of the key
1457 * such as the key type.
1458 */
1459static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001460{
1461 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001462 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 PSA_KEY_USAGE_ENCRYPT |
1464 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001465 PSA_KEY_USAGE_SIGN_HASH |
1466 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1468 return( PSA_ERROR_INVALID_ARGUMENT );
1469
Gilles Peskine4747d192019-04-17 15:05:45 +02001470 return( PSA_SUCCESS );
1471}
1472
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001473/** Validate the internal consistency of key attributes.
1474 *
1475 * This function only rejects invalid attribute values. If does not
1476 * validate the consistency of the attributes with any key data that may
1477 * be involved in the creation of the key.
1478 *
1479 * Call this function early in the key creation process.
1480 *
1481 * \param[in] attributes Key attributes for the new key.
1482 * \param[out] p_drv On any return, the driver for the key, if any.
1483 * NULL for a transparent key.
1484 *
1485 */
1486static psa_status_t psa_validate_key_attributes(
1487 const psa_key_attributes_t *attributes,
1488 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001489{
1490 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491
1492 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001493 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001494 status = psa_validate_persistent_key_parameters(
1495 attributes->core.lifetime, attributes->core.id,
1496 p_drv, 1 );
1497 if( status != PSA_SUCCESS )
1498 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500
1501 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001502 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503 return( status );
1504
1505 /* Refuse to create overly large keys.
1506 * Note that this doesn't trigger on import if the attributes don't
1507 * explicitly specify a size (so psa_get_key_bits returns 0), so
1508 * psa_import_key() needs its own checks. */
1509 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1510 return( PSA_ERROR_NOT_SUPPORTED );
1511
Gilles Peskine91e8c332019-08-02 19:19:39 +02001512 /* Reject invalid flags. These should not be reachable through the API. */
1513 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1514 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1515 return( PSA_ERROR_INVALID_ARGUMENT );
1516
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001517 return( PSA_SUCCESS );
1518}
1519
Gilles Peskine4747d192019-04-17 15:05:45 +02001520/** Prepare a key slot to receive key material.
1521 *
1522 * This function allocates a key slot and sets its metadata.
1523 *
1524 * If this function fails, call psa_fail_key_creation().
1525 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001526 * This function is intended to be used as follows:
1527 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1528 * it with the specified attributes, and assign it a handle.
1529 * -# Populate the slot with the key material.
1530 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1531 * In case of failure at any step, stop the sequence and call
1532 * psa_fail_key_creation().
1533 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001534 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001535 * \param[in] attributes Key attributes for the new key.
1536 * \param[out] handle On success, a handle for the allocated slot.
1537 * \param[out] p_slot On success, a pointer to the prepared slot.
1538 * \param[out] p_drv On any return, the driver for the key, if any.
1539 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * The key slot is ready to receive key material.
1543 * \return If this function fails, the key slot is an invalid state.
1544 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 */
1546static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001547 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001548 const psa_key_attributes_t *attributes,
1549 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001550 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001551 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001552{
1553 psa_status_t status;
1554 psa_key_slot_t *slot;
1555
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001557 *p_drv = NULL;
1558
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559 status = psa_validate_key_attributes( attributes, p_drv );
1560 if( status != PSA_SUCCESS )
1561 return( status );
1562
Gilles Peskineedbed562019-08-07 18:19:59 +02001563 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001564 if( status != PSA_SUCCESS )
1565 return( status );
1566 slot = *p_slot;
1567
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001568 /* We're storing the declared bit-size of the key. It's up to each
1569 * creation mechanism to verify that this information is correct.
1570 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001571 * an input (generate, device) but not for those where the bit-size
1572 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001573
1574 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001575
Gilles Peskine91e8c332019-08-02 19:19:39 +02001576 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * external-only flags, query `attributes`. Thanks to the check
1578 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001579 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001580 * may have set. */
1581 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001582
Gilles Peskinecbaff462019-07-12 23:46:04 +02001583#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001584 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001585 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001586 * create the key file in internal storage, create the
1587 * key inside the secure element, and update the driver's
1588 * persistent data. Start a transaction that will encompass these
1589 * three actions. */
1590 /* The first thing to do is to find a slot number for the new key.
1591 * We save the slot number in persistent storage as part of the
1592 * transaction data. It will be needed to recover if the power
1593 * fails during the key creation process, to clean up on the secure
1594 * element side after restarting. Obtaining a slot number from the
1595 * secure element driver updates its persistent state, but we do not yet
1596 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001597 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001598 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001599 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001600 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001601 &slot->data.se.slot_number );
1602 if( status != PSA_SUCCESS )
1603 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001604 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001605 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001606 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001607 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 status = psa_crypto_save_transaction( );
1609 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 {
1611 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001612 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001613 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001615
1616 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1617 {
1618 /* Key registration only makes sense with a secure element. */
1619 return( PSA_ERROR_INVALID_ARGUMENT );
1620 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001621#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1622
Darryl Green0c6575a2018-11-07 16:05:30 +00001623 return( status );
1624}
Gilles Peskine4747d192019-04-17 15:05:45 +02001625
1626/** Finalize the creation of a key once its key material has been set.
1627 *
1628 * This entails writing the key to persistent storage.
1629 *
1630 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 * See the documentation of psa_start_key_creation() for the intended use
1632 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001634 * \param[in,out] slot Pointer to the slot with key material.
1635 * \param[in] driver The secure element driver for the key,
1636 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001637 *
1638 * \retval #PSA_SUCCESS
1639 * The key was successfully created. The handle is now valid.
1640 * \return If this function fails, the key slot is an invalid state.
1641 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001643static psa_status_t psa_finish_key_creation(
1644 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001645 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001646{
1647 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001648 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001649 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001650
1651#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001652 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001654#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1655 if( driver != NULL )
1656 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001657 psa_se_key_data_storage_t data;
1658#if defined(static_assert)
1659 static_assert( sizeof( slot->data.se.slot_number ) ==
1660 sizeof( data.slot_number ),
1661 "Slot number size does not match psa_se_key_data_storage_t" );
1662 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1663 "Bit-size size does not match psa_se_key_data_storage_t" );
1664#endif
1665 memcpy( &data.slot_number, &slot->data.se.slot_number,
1666 sizeof( slot->data.se.slot_number ) );
1667 memcpy( &data.bits, &slot->attr.bits,
1668 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001669 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001670 (uint8_t*) &data,
1671 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001672 }
1673 else
1674#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1675 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001676 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001677 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001678 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001679 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1680 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001681 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001682 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1683 status = psa_internal_export_key( slot,
1684 buffer, buffer_size, &length,
1685 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001686 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001687 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001688 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001689
Gilles Peskinef9168942019-09-12 19:20:29 +02001690 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001691 mbedtls_free( buffer );
1692 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001694#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1695
Gilles Peskinecbaff462019-07-12 23:46:04 +02001696#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001697 /* Finish the transaction for a key creation. This does not
1698 * happen when registering an existing key. Detect this case
1699 * by checking whether a transaction is in progress (actual
1700 * creation of a key in a secure element requires a transaction,
1701 * but registration doesn't use one). */
1702 if( driver != NULL &&
1703 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001704 {
1705 status = psa_save_se_persistent_data( driver );
1706 if( status != PSA_SUCCESS )
1707 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001708 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001709 return( status );
1710 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001711 status = psa_crypto_stop_transaction( );
1712 if( status != PSA_SUCCESS )
1713 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001714 }
1715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 return( status );
1718}
1719
1720/** Abort the creation of a key.
1721 *
1722 * You may call this function after calling psa_start_key_creation(),
1723 * or after psa_finish_key_creation() fails. In other circumstances, this
1724 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001725 * See the documentation of psa_start_key_creation() for the intended use
1726 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001727 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001728 * \param[in,out] slot Pointer to the slot with key material.
1729 * \param[in] driver The secure element driver for the key,
1730 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001731 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001732static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001733 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001734{
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 (void) driver;
1736
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 if( slot == NULL )
1738 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001739
1740#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001741 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 * element, and the failure happened later (when saving metadata
1743 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001744 * element.
1745 * https://github.com/ARMmbed/mbed-crypto/issues/217
1746 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001747
Gilles Peskined7729582019-08-05 15:55:54 +02001748 /* Abort the ongoing transaction if any (there may not be one if
1749 * the creation process failed before starting one, or if the
1750 * key creation is a registration of a key in a secure element).
1751 * Earlier functions must already have done what it takes to undo any
1752 * partial creation. All that's left is to update the transaction data
1753 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001754 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001755#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1756
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 psa_wipe_key_slot( slot );
1758}
1759
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001760/** Validate optional attributes during key creation.
1761 *
1762 * Some key attributes are optional during key creation. If they are
1763 * specified in the attributes structure, check that they are consistent
1764 * with the data in the slot.
1765 *
1766 * This function should be called near the end of key creation, after
1767 * the slot in memory is fully populated but before saving persistent data.
1768 */
1769static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001770 const psa_key_slot_t *slot,
1771 const psa_key_attributes_t *attributes )
1772{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001773 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001774 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001775 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001776 return( PSA_ERROR_INVALID_ARGUMENT );
1777 }
1778
1779 if( attributes->domain_parameters_size != 0 )
1780 {
1781#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001782 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 {
1784 mbedtls_mpi actual, required;
1785 int ret;
1786 mbedtls_mpi_init( &actual );
1787 mbedtls_mpi_init( &required );
1788 ret = mbedtls_rsa_export( slot->data.rsa,
1789 NULL, NULL, NULL, NULL, &actual );
1790 if( ret != 0 )
1791 goto rsa_exit;
1792 ret = mbedtls_mpi_read_binary( &required,
1793 attributes->domain_parameters,
1794 attributes->domain_parameters_size );
1795 if( ret != 0 )
1796 goto rsa_exit;
1797 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1798 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1799 rsa_exit:
1800 mbedtls_mpi_free( &actual );
1801 mbedtls_mpi_free( &required );
1802 if( ret != 0)
1803 return( mbedtls_to_psa_error( ret ) );
1804 }
1805 else
1806#endif
1807 {
1808 return( PSA_ERROR_INVALID_ARGUMENT );
1809 }
1810 }
1811
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001812 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001813 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001814 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 return( PSA_ERROR_INVALID_ARGUMENT );
1816 }
1817
1818 return( PSA_SUCCESS );
1819}
1820
Gilles Peskine4747d192019-04-17 15:05:45 +02001821psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001823 size_t data_length,
1824 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001825{
1826 psa_status_t status;
1827 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001828 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829
Gilles Peskine0f84d622019-09-12 19:03:13 +02001830 /* Reject zero-length symmetric keys (including raw data key objects).
1831 * This also rejects any key which might be encoded as an empty string,
1832 * which is never valid. */
1833 if( data_length == 0 )
1834 return( PSA_ERROR_INVALID_ARGUMENT );
1835
Gilles Peskinedf179142019-07-15 22:02:14 +02001836 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1837 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001838 if( status != PSA_SUCCESS )
1839 goto exit;
1840
Gilles Peskine5d309672019-07-12 23:47:28 +02001841#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1842 if( driver != NULL )
1843 {
1844 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001845 /* The driver should set the number of key bits, however in
1846 * case it doesn't, we initialize bits to an invalid value. */
1847 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001848 if( drv->key_management == NULL ||
1849 drv->key_management->p_import == NULL )
1850 {
1851 status = PSA_ERROR_NOT_SUPPORTED;
1852 goto exit;
1853 }
1854 status = drv->key_management->p_import(
1855 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001856 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001857 &bits );
1858 if( status != PSA_SUCCESS )
1859 goto exit;
1860 if( bits > PSA_MAX_KEY_BITS )
1861 {
1862 status = PSA_ERROR_NOT_SUPPORTED;
1863 goto exit;
1864 }
1865 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001866 }
1867 else
1868#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1869 {
1870 status = psa_import_key_into_slot( slot, data, data_length );
1871 if( status != PSA_SUCCESS )
1872 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001873 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001874 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001875 if( status != PSA_SUCCESS )
1876 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001877
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 if( status != PSA_SUCCESS )
1881 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001883 *handle = 0;
1884 }
1885 return( status );
1886}
1887
Gilles Peskined7729582019-08-05 15:55:54 +02001888#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1889psa_status_t mbedtls_psa_register_se_key(
1890 const psa_key_attributes_t *attributes )
1891{
1892 psa_status_t status;
1893 psa_key_slot_t *slot = NULL;
1894 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001895 psa_key_handle_t handle = 0;
1896
1897 /* Leaving attributes unspecified is not currently supported.
1898 * It could make sense to query the key type and size from the
1899 * secure element, but not all secure elements support this
1900 * and the driver HAL doesn't currently support it. */
1901 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1902 return( PSA_ERROR_NOT_SUPPORTED );
1903 if( psa_get_key_bits( attributes ) == 0 )
1904 return( PSA_ERROR_NOT_SUPPORTED );
1905
1906 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1907 &handle, &slot, &driver );
1908 if( status != PSA_SUCCESS )
1909 goto exit;
1910
Gilles Peskined7729582019-08-05 15:55:54 +02001911 status = psa_finish_key_creation( slot, driver );
1912
1913exit:
1914 if( status != PSA_SUCCESS )
1915 {
1916 psa_fail_key_creation( slot, driver );
1917 }
1918 /* Registration doesn't keep the key in RAM. */
1919 psa_close_key( handle );
1920 return( status );
1921}
1922#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1923
Gilles Peskinef603c712019-01-19 13:40:11 +01001924static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001925 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001926{
1927 psa_status_t status;
1928 uint8_t *buffer = NULL;
1929 size_t buffer_size = 0;
1930 size_t length;
1931
Gilles Peskine8e338702019-07-30 20:06:31 +02001932 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001933 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001934 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001935 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001937 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1938 if( status != PSA_SUCCESS )
1939 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001940 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001941 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001942
1943exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001944 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001945 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001946 return( status );
1947}
1948
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001949psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1950 const psa_key_attributes_t *specified_attributes,
1951 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001952{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001953 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001954 psa_key_slot_t *source_slot = NULL;
1955 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001956 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001957 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001958
Gilles Peskine28f8f302019-07-24 13:30:31 +02001959 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001960 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001961 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001962 goto exit;
1963
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001964 status = psa_validate_optional_attributes( source_slot,
1965 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001966 if( status != PSA_SUCCESS )
1967 goto exit;
1968
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001969 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001970 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 if( status != PSA_SUCCESS )
1972 goto exit;
1973
Gilles Peskinedf179142019-07-15 22:02:14 +02001974 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1975 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1981 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001982 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001983 /* Copying to a secure element is not implemented yet. */
1984 status = PSA_ERROR_NOT_SUPPORTED;
1985 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001986 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001988
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001989 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001990 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001992
Gilles Peskine011e4282019-06-26 18:34:38 +02001993 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 if( status != PSA_SUCCESS )
1996 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001997 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001998 *target_handle = 0;
1999 }
2000 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002001}
2002
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002003
2004
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002005/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002006/* Message digests */
2007/****************************************************************/
2008
Gilles Peskineb16841e2019-10-10 20:36:12 +02002009#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002010static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002011{
2012 switch( alg )
2013 {
2014#if defined(MBEDTLS_MD2_C)
2015 case PSA_ALG_MD2:
2016 return( &mbedtls_md2_info );
2017#endif
2018#if defined(MBEDTLS_MD4_C)
2019 case PSA_ALG_MD4:
2020 return( &mbedtls_md4_info );
2021#endif
2022#if defined(MBEDTLS_MD5_C)
2023 case PSA_ALG_MD5:
2024 return( &mbedtls_md5_info );
2025#endif
2026#if defined(MBEDTLS_RIPEMD160_C)
2027 case PSA_ALG_RIPEMD160:
2028 return( &mbedtls_ripemd160_info );
2029#endif
2030#if defined(MBEDTLS_SHA1_C)
2031 case PSA_ALG_SHA_1:
2032 return( &mbedtls_sha1_info );
2033#endif
2034#if defined(MBEDTLS_SHA256_C)
2035 case PSA_ALG_SHA_224:
2036 return( &mbedtls_sha224_info );
2037 case PSA_ALG_SHA_256:
2038 return( &mbedtls_sha256_info );
2039#endif
2040#if defined(MBEDTLS_SHA512_C)
2041 case PSA_ALG_SHA_384:
2042 return( &mbedtls_sha384_info );
2043 case PSA_ALG_SHA_512:
2044 return( &mbedtls_sha512_info );
2045#endif
2046 default:
2047 return( NULL );
2048 }
2049}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002050#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002051
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002052psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2053{
2054 switch( operation->alg )
2055 {
Gilles Peskine81736312018-06-26 15:04:31 +02002056 case 0:
2057 /* The object has (apparently) been initialized but it is not
2058 * in use. It's ok to call abort on such an object, and there's
2059 * nothing to do. */
2060 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002061#if defined(MBEDTLS_MD2_C)
2062 case PSA_ALG_MD2:
2063 mbedtls_md2_free( &operation->ctx.md2 );
2064 break;
2065#endif
2066#if defined(MBEDTLS_MD4_C)
2067 case PSA_ALG_MD4:
2068 mbedtls_md4_free( &operation->ctx.md4 );
2069 break;
2070#endif
2071#if defined(MBEDTLS_MD5_C)
2072 case PSA_ALG_MD5:
2073 mbedtls_md5_free( &operation->ctx.md5 );
2074 break;
2075#endif
2076#if defined(MBEDTLS_RIPEMD160_C)
2077 case PSA_ALG_RIPEMD160:
2078 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_SHA1_C)
2082 case PSA_ALG_SHA_1:
2083 mbedtls_sha1_free( &operation->ctx.sha1 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_SHA256_C)
2087 case PSA_ALG_SHA_224:
2088 case PSA_ALG_SHA_256:
2089 mbedtls_sha256_free( &operation->ctx.sha256 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_SHA512_C)
2093 case PSA_ALG_SHA_384:
2094 case PSA_ALG_SHA_512:
2095 mbedtls_sha512_free( &operation->ctx.sha512 );
2096 break;
2097#endif
2098 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002099 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002100 }
2101 operation->alg = 0;
2102 return( PSA_SUCCESS );
2103}
2104
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002105psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002106 psa_algorithm_t alg )
2107{
2108 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002109
2110 /* A context must be freshly initialized before it can be set up. */
2111 if( operation->alg != 0 )
2112 {
2113 return( PSA_ERROR_BAD_STATE );
2114 }
2115
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 switch( alg )
2117 {
2118#if defined(MBEDTLS_MD2_C)
2119 case PSA_ALG_MD2:
2120 mbedtls_md2_init( &operation->ctx.md2 );
2121 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2122 break;
2123#endif
2124#if defined(MBEDTLS_MD4_C)
2125 case PSA_ALG_MD4:
2126 mbedtls_md4_init( &operation->ctx.md4 );
2127 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2128 break;
2129#endif
2130#if defined(MBEDTLS_MD5_C)
2131 case PSA_ALG_MD5:
2132 mbedtls_md5_init( &operation->ctx.md5 );
2133 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2134 break;
2135#endif
2136#if defined(MBEDTLS_RIPEMD160_C)
2137 case PSA_ALG_RIPEMD160:
2138 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2139 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2140 break;
2141#endif
2142#if defined(MBEDTLS_SHA1_C)
2143 case PSA_ALG_SHA_1:
2144 mbedtls_sha1_init( &operation->ctx.sha1 );
2145 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_SHA256_C)
2149 case PSA_ALG_SHA_224:
2150 mbedtls_sha256_init( &operation->ctx.sha256 );
2151 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2152 break;
2153 case PSA_ALG_SHA_256:
2154 mbedtls_sha256_init( &operation->ctx.sha256 );
2155 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2156 break;
2157#endif
2158#if defined(MBEDTLS_SHA512_C)
2159 case PSA_ALG_SHA_384:
2160 mbedtls_sha512_init( &operation->ctx.sha512 );
2161 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2162 break;
2163 case PSA_ALG_SHA_512:
2164 mbedtls_sha512_init( &operation->ctx.sha512 );
2165 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2166 break;
2167#endif
2168 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002169 return( PSA_ALG_IS_HASH( alg ) ?
2170 PSA_ERROR_NOT_SUPPORTED :
2171 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172 }
2173 if( ret == 0 )
2174 operation->alg = alg;
2175 else
2176 psa_hash_abort( operation );
2177 return( mbedtls_to_psa_error( ret ) );
2178}
2179
2180psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2181 const uint8_t *input,
2182 size_t input_length )
2183{
2184 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002185
2186 /* Don't require hash implementations to behave correctly on a
2187 * zero-length input, which may have an invalid pointer. */
2188 if( input_length == 0 )
2189 return( PSA_SUCCESS );
2190
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002191 switch( operation->alg )
2192 {
2193#if defined(MBEDTLS_MD2_C)
2194 case PSA_ALG_MD2:
2195 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2196 input, input_length );
2197 break;
2198#endif
2199#if defined(MBEDTLS_MD4_C)
2200 case PSA_ALG_MD4:
2201 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2202 input, input_length );
2203 break;
2204#endif
2205#if defined(MBEDTLS_MD5_C)
2206 case PSA_ALG_MD5:
2207 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2208 input, input_length );
2209 break;
2210#endif
2211#if defined(MBEDTLS_RIPEMD160_C)
2212 case PSA_ALG_RIPEMD160:
2213 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2214 input, input_length );
2215 break;
2216#endif
2217#if defined(MBEDTLS_SHA1_C)
2218 case PSA_ALG_SHA_1:
2219 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2220 input, input_length );
2221 break;
2222#endif
2223#if defined(MBEDTLS_SHA256_C)
2224 case PSA_ALG_SHA_224:
2225 case PSA_ALG_SHA_256:
2226 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2227 input, input_length );
2228 break;
2229#endif
2230#if defined(MBEDTLS_SHA512_C)
2231 case PSA_ALG_SHA_384:
2232 case PSA_ALG_SHA_512:
2233 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2234 input, input_length );
2235 break;
2236#endif
2237 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002238 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002240
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241 if( ret != 0 )
2242 psa_hash_abort( operation );
2243 return( mbedtls_to_psa_error( ret ) );
2244}
2245
2246psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2247 uint8_t *hash,
2248 size_t hash_size,
2249 size_t *hash_length )
2250{
itayzafrir40835d42018-08-02 13:14:17 +03002251 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002253 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254
2255 /* Fill the output buffer with something that isn't a valid hash
2256 * (barring an attack on the hash and deliberately-crafted input),
2257 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002258 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002259 /* If hash_size is 0 then hash may be NULL and then the
2260 * call to memset would have undefined behavior. */
2261 if( hash_size != 0 )
2262 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263
2264 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002265 {
2266 status = PSA_ERROR_BUFFER_TOO_SMALL;
2267 goto exit;
2268 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269
2270 switch( operation->alg )
2271 {
2272#if defined(MBEDTLS_MD2_C)
2273 case PSA_ALG_MD2:
2274 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2275 break;
2276#endif
2277#if defined(MBEDTLS_MD4_C)
2278 case PSA_ALG_MD4:
2279 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2280 break;
2281#endif
2282#if defined(MBEDTLS_MD5_C)
2283 case PSA_ALG_MD5:
2284 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2285 break;
2286#endif
2287#if defined(MBEDTLS_RIPEMD160_C)
2288 case PSA_ALG_RIPEMD160:
2289 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2290 break;
2291#endif
2292#if defined(MBEDTLS_SHA1_C)
2293 case PSA_ALG_SHA_1:
2294 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2295 break;
2296#endif
2297#if defined(MBEDTLS_SHA256_C)
2298 case PSA_ALG_SHA_224:
2299 case PSA_ALG_SHA_256:
2300 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_SHA512_C)
2304 case PSA_ALG_SHA_384:
2305 case PSA_ALG_SHA_512:
2306 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2307 break;
2308#endif
2309 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002310 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 }
itayzafrir40835d42018-08-02 13:14:17 +03002312 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313
itayzafrir40835d42018-08-02 13:14:17 +03002314exit:
2315 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002317 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318 return( psa_hash_abort( operation ) );
2319 }
2320 else
2321 {
2322 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002323 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 }
2325}
2326
Gilles Peskine2d277862018-06-18 15:41:12 +02002327psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2328 const uint8_t *hash,
2329 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330{
2331 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2332 size_t actual_hash_length;
2333 psa_status_t status = psa_hash_finish( operation,
2334 actual_hash, sizeof( actual_hash ),
2335 &actual_hash_length );
2336 if( status != PSA_SUCCESS )
2337 return( status );
2338 if( actual_hash_length != hash_length )
2339 return( PSA_ERROR_INVALID_SIGNATURE );
2340 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2341 return( PSA_ERROR_INVALID_SIGNATURE );
2342 return( PSA_SUCCESS );
2343}
2344
Gilles Peskineeb35d782019-01-22 17:56:16 +01002345psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2346 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002347{
2348 if( target_operation->alg != 0 )
2349 return( PSA_ERROR_BAD_STATE );
2350
2351 switch( source_operation->alg )
2352 {
2353 case 0:
2354 return( PSA_ERROR_BAD_STATE );
2355#if defined(MBEDTLS_MD2_C)
2356 case PSA_ALG_MD2:
2357 mbedtls_md2_clone( &target_operation->ctx.md2,
2358 &source_operation->ctx.md2 );
2359 break;
2360#endif
2361#if defined(MBEDTLS_MD4_C)
2362 case PSA_ALG_MD4:
2363 mbedtls_md4_clone( &target_operation->ctx.md4,
2364 &source_operation->ctx.md4 );
2365 break;
2366#endif
2367#if defined(MBEDTLS_MD5_C)
2368 case PSA_ALG_MD5:
2369 mbedtls_md5_clone( &target_operation->ctx.md5,
2370 &source_operation->ctx.md5 );
2371 break;
2372#endif
2373#if defined(MBEDTLS_RIPEMD160_C)
2374 case PSA_ALG_RIPEMD160:
2375 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2376 &source_operation->ctx.ripemd160 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_SHA1_C)
2380 case PSA_ALG_SHA_1:
2381 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2382 &source_operation->ctx.sha1 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_SHA256_C)
2386 case PSA_ALG_SHA_224:
2387 case PSA_ALG_SHA_256:
2388 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2389 &source_operation->ctx.sha256 );
2390 break;
2391#endif
2392#if defined(MBEDTLS_SHA512_C)
2393 case PSA_ALG_SHA_384:
2394 case PSA_ALG_SHA_512:
2395 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2396 &source_operation->ctx.sha512 );
2397 break;
2398#endif
2399 default:
2400 return( PSA_ERROR_NOT_SUPPORTED );
2401 }
2402
2403 target_operation->alg = source_operation->alg;
2404 return( PSA_SUCCESS );
2405}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406
2407
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409/* MAC */
2410/****************************************************************/
2411
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002412static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413 psa_algorithm_t alg,
2414 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002415 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002416 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002419 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002421 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002422 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002423
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2425 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002426 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002428 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002429 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 mode = MBEDTLS_MODE_STREAM;
2431 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 case PSA_ALG_CTR:
2433 mode = MBEDTLS_MODE_CTR;
2434 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002435 case PSA_ALG_CFB:
2436 mode = MBEDTLS_MODE_CFB;
2437 break;
2438 case PSA_ALG_OFB:
2439 mode = MBEDTLS_MODE_OFB;
2440 break;
2441 case PSA_ALG_CBC_NO_PADDING:
2442 mode = MBEDTLS_MODE_CBC;
2443 break;
2444 case PSA_ALG_CBC_PKCS7:
2445 mode = MBEDTLS_MODE_CBC;
2446 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002447 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 mode = MBEDTLS_MODE_CCM;
2449 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002450 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 mode = MBEDTLS_MODE_GCM;
2452 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002453 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2454 mode = MBEDTLS_MODE_CHACHAPOLY;
2455 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 default:
2457 return( NULL );
2458 }
2459 }
2460 else if( alg == PSA_ALG_CMAC )
2461 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462 else
2463 return( NULL );
2464
2465 switch( key_type )
2466 {
2467 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002468 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469 break;
2470 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002471 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2472 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002474 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002476 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002477 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2478 * but two-key Triple-DES is functionally three-key Triple-DES
2479 * with K1=K3, so that's how we present it to mbedtls. */
2480 if( key_bits == 128 )
2481 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482 break;
2483 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002484 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 break;
2486 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002487 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002489 case PSA_KEY_TYPE_CHACHA20:
2490 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2491 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492 default:
2493 return( NULL );
2494 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002495 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002496 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497
Jaeden Amero23bbb752018-06-26 14:16:54 +01002498 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2499 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500}
2501
Gilles Peskinea05219c2018-11-16 16:02:56 +01002502#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002503static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002504{
Gilles Peskine2d277862018-06-18 15:41:12 +02002505 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002506 {
2507 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002508 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002509 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002510 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002511 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002512 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002513 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002514 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002515 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002516 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002517 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002518 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002519 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002520 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002521 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002522 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002523 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002524 return( 128 );
2525 default:
2526 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002527 }
2528}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002529#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002530
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002531/* Initialize the MAC operation structure. Once this function has been
2532 * called, psa_mac_abort can run and will do the right thing. */
2533static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2534 psa_algorithm_t alg )
2535{
2536 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2537
2538 operation->alg = alg;
2539 operation->key_set = 0;
2540 operation->iv_set = 0;
2541 operation->iv_required = 0;
2542 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002543 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002544
2545#if defined(MBEDTLS_CMAC_C)
2546 if( alg == PSA_ALG_CMAC )
2547 {
2548 operation->iv_required = 0;
2549 mbedtls_cipher_init( &operation->ctx.cmac );
2550 status = PSA_SUCCESS;
2551 }
2552 else
2553#endif /* MBEDTLS_CMAC_C */
2554#if defined(MBEDTLS_MD_C)
2555 if( PSA_ALG_IS_HMAC( operation->alg ) )
2556 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002557 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2558 operation->ctx.hmac.hash_ctx.alg = 0;
2559 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 }
2561 else
2562#endif /* MBEDTLS_MD_C */
2563 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002564 if( ! PSA_ALG_IS_MAC( alg ) )
2565 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566 }
2567
2568 if( status != PSA_SUCCESS )
2569 memset( operation, 0, sizeof( *operation ) );
2570 return( status );
2571}
2572
Gilles Peskine01126fa2018-07-12 17:04:55 +02002573#if defined(MBEDTLS_MD_C)
2574static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2575{
Gilles Peskine3f108122018-12-07 18:14:53 +01002576 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002577 return( psa_hash_abort( &hmac->hash_ctx ) );
2578}
2579#endif /* MBEDTLS_MD_C */
2580
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2582{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002583 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002585 /* The object has (apparently) been initialized but it is not
2586 * in use. It's ok to call abort on such an object, and there's
2587 * nothing to do. */
2588 return( PSA_SUCCESS );
2589 }
2590 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002591#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002592 if( operation->alg == PSA_ALG_CMAC )
2593 {
2594 mbedtls_cipher_free( &operation->ctx.cmac );
2595 }
2596 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002599 if( PSA_ALG_IS_HMAC( operation->alg ) )
2600 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002601 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002602 }
2603 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002605 {
2606 /* Sanity check (shouldn't happen: operation->alg should
2607 * always have been initialized to a valid value). */
2608 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 }
Moran Peker41deec42018-04-04 15:43:05 +03002610
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 operation->alg = 0;
2612 operation->key_set = 0;
2613 operation->iv_set = 0;
2614 operation->iv_required = 0;
2615 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002616 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002617
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002619
2620bad_state:
2621 /* If abort is called on an uninitialized object, we can't trust
2622 * anything. Wipe the object in case it contains confidential data.
2623 * This may result in a memory leak if a pointer gets overwritten,
2624 * but it's too late to do anything about this. */
2625 memset( operation, 0, sizeof( *operation ) );
2626 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627}
2628
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002629#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002630static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002631 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002632 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002633 const mbedtls_cipher_info_t *cipher_info )
2634{
2635 int ret;
2636
2637 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002638
2639 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2640 if( ret != 0 )
2641 return( ret );
2642
2643 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2644 slot->data.raw.data,
2645 key_bits );
2646 return( ret );
2647}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002648#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002649
Gilles Peskine248051a2018-06-20 16:09:38 +02002650#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002651static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2652 const uint8_t *key,
2653 size_t key_length,
2654 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002655{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002656 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002657 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002658 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002659 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002660 psa_status_t status;
2661
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002662 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2663 * overflow below. This should never trigger if the hash algorithm
2664 * is implemented correctly. */
2665 /* The size checks against the ipad and opad buffers cannot be written
2666 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2667 * because that triggers -Wlogical-op on GCC 7.3. */
2668 if( block_size > sizeof( ipad ) )
2669 return( PSA_ERROR_NOT_SUPPORTED );
2670 if( block_size > sizeof( hmac->opad ) )
2671 return( PSA_ERROR_NOT_SUPPORTED );
2672 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673 return( PSA_ERROR_NOT_SUPPORTED );
2674
Gilles Peskined223b522018-06-11 18:12:58 +02002675 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002677 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002678 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002679 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002680 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002682 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002684 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002685 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002686 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002687 }
Gilles Peskine96889972018-07-12 17:07:03 +02002688 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2689 * but it is permitted. It is common when HMAC is used in HKDF, for
2690 * example. Don't call `memcpy` in the 0-length because `key` could be
2691 * an invalid pointer which would make the behavior undefined. */
2692 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002693 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694
Gilles Peskined223b522018-06-11 18:12:58 +02002695 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2696 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002698 ipad[i] ^= 0x36;
2699 memset( ipad + key_length, 0x36, block_size - key_length );
2700
2701 /* Copy the key material from ipad to opad, flipping the requisite bits,
2702 * and filling the rest of opad with the requisite constant. */
2703 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2705 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706
Gilles Peskine01126fa2018-07-12 17:04:55 +02002707 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002709 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710
Gilles Peskine01126fa2018-07-12 17:04:55 +02002711 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002712
2713cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002714 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002715
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716 return( status );
2717}
Gilles Peskine248051a2018-06-20 16:09:38 +02002718#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719
Gilles Peskine89167cb2018-07-08 20:12:23 +02002720static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002721 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002722 psa_algorithm_t alg,
2723 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002726 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002728 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002729 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002730 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002731 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002733 /* A context must be freshly initialized before it can be set up. */
2734 if( operation->alg != 0 )
2735 {
2736 return( PSA_ERROR_BAD_STATE );
2737 }
2738
Gilles Peskined911eb72018-08-14 15:18:45 +02002739 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002740 if( status != PSA_SUCCESS )
2741 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002742 if( is_sign )
2743 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744
Gilles Peskine28f8f302019-07-24 13:30:31 +02002745 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002746 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002747 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002748 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002749
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002751 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002752 {
2753 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002754 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002755 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002756 int ret;
2757 if( cipher_info == NULL )
2758 {
2759 status = PSA_ERROR_NOT_SUPPORTED;
2760 goto exit;
2761 }
2762 operation->mac_size = cipher_info->block_size;
2763 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2764 status = mbedtls_to_psa_error( ret );
2765 }
2766 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002769 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002770 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002771 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002772 if( hash_alg == 0 )
2773 {
2774 status = PSA_ERROR_NOT_SUPPORTED;
2775 goto exit;
2776 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002777
2778 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2779 /* Sanity check. This shouldn't fail on a valid configuration. */
2780 if( operation->mac_size == 0 ||
2781 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2782 {
2783 status = PSA_ERROR_NOT_SUPPORTED;
2784 goto exit;
2785 }
2786
Gilles Peskine8e338702019-07-30 20:06:31 +02002787 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002788 {
2789 status = PSA_ERROR_INVALID_ARGUMENT;
2790 goto exit;
2791 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002792
Gilles Peskine01126fa2018-07-12 17:04:55 +02002793 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2794 slot->data.raw.data,
2795 slot->data.raw.bytes,
2796 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002797 }
2798 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002800 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002801 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002802 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002804
Gilles Peskined911eb72018-08-14 15:18:45 +02002805 if( truncated == 0 )
2806 {
2807 /* The "normal" case: untruncated algorithm. Nothing to do. */
2808 }
2809 else if( truncated < 4 )
2810 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002811 /* A very short MAC is too short for security since it can be
2812 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2813 * so we make this our minimum, even though 32 bits is still
2814 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002815 status = PSA_ERROR_NOT_SUPPORTED;
2816 }
2817 else if( truncated > operation->mac_size )
2818 {
2819 /* It's impossible to "truncate" to a larger length. */
2820 status = PSA_ERROR_INVALID_ARGUMENT;
2821 }
2822 else
2823 operation->mac_size = truncated;
2824
Gilles Peskinefbfac682018-07-08 20:51:54 +02002825exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002826 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002828 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002830 else
2831 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002832 operation->key_set = 1;
2833 }
2834 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835}
2836
Gilles Peskine89167cb2018-07-08 20:12:23 +02002837psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002838 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002839 psa_algorithm_t alg )
2840{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002841 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002842}
2843
2844psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002845 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002846 psa_algorithm_t alg )
2847{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002848 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002849}
2850
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2852 const uint8_t *input,
2853 size_t input_length )
2854{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002855 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002857 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002859 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 operation->has_input = 1;
2861
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002864 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2866 input, input_length );
2867 status = mbedtls_to_psa_error( ret );
2868 }
2869 else
2870#endif /* MBEDTLS_CMAC_C */
2871#if defined(MBEDTLS_MD_C)
2872 if( PSA_ALG_IS_HMAC( operation->alg ) )
2873 {
2874 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2875 input_length );
2876 }
2877 else
2878#endif /* MBEDTLS_MD_C */
2879 {
2880 /* This shouldn't happen if `operation` was initialized by
2881 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002882 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002883 }
2884
Gilles Peskinefbfac682018-07-08 20:51:54 +02002885 if( status != PSA_SUCCESS )
2886 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002887 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888}
2889
Gilles Peskine01126fa2018-07-12 17:04:55 +02002890#if defined(MBEDTLS_MD_C)
2891static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2892 uint8_t *mac,
2893 size_t mac_size )
2894{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002895 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002896 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2897 size_t hash_size = 0;
2898 size_t block_size = psa_get_hash_block_size( hash_alg );
2899 psa_status_t status;
2900
Gilles Peskine01126fa2018-07-12 17:04:55 +02002901 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2902 if( status != PSA_SUCCESS )
2903 return( status );
2904 /* From here on, tmp needs to be wiped. */
2905
2906 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2907 if( status != PSA_SUCCESS )
2908 goto exit;
2909
2910 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2911 if( status != PSA_SUCCESS )
2912 goto exit;
2913
2914 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2915 if( status != PSA_SUCCESS )
2916 goto exit;
2917
Gilles Peskined911eb72018-08-14 15:18:45 +02002918 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2919 if( status != PSA_SUCCESS )
2920 goto exit;
2921
2922 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002923
2924exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002925 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 return( status );
2927}
2928#endif /* MBEDTLS_MD_C */
2929
mohammad16036df908f2018-04-02 08:34:15 -07002930static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002931 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002932 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002934 if( ! operation->key_set )
2935 return( PSA_ERROR_BAD_STATE );
2936 if( operation->iv_required && ! operation->iv_set )
2937 return( PSA_ERROR_BAD_STATE );
2938
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939 if( mac_size < operation->mac_size )
2940 return( PSA_ERROR_BUFFER_TOO_SMALL );
2941
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002943 if( operation->alg == PSA_ALG_CMAC )
2944 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002945 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2946 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2947 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002948 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002949 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002950 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002952 else
2953#endif /* MBEDTLS_CMAC_C */
2954#if defined(MBEDTLS_MD_C)
2955 if( PSA_ALG_IS_HMAC( operation->alg ) )
2956 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002957 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002958 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002959 }
2960 else
2961#endif /* MBEDTLS_MD_C */
2962 {
2963 /* This shouldn't happen if `operation` was initialized by
2964 * a setup function. */
2965 return( PSA_ERROR_BAD_STATE );
2966 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967}
2968
Gilles Peskineacd4be32018-07-08 19:56:25 +02002969psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2970 uint8_t *mac,
2971 size_t mac_size,
2972 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002973{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002974 psa_status_t status;
2975
Jaeden Amero252ef282019-02-15 14:05:35 +00002976 if( operation->alg == 0 )
2977 {
2978 return( PSA_ERROR_BAD_STATE );
2979 }
2980
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002981 /* Fill the output buffer with something that isn't a valid mac
2982 * (barring an attack on the mac and deliberately-crafted input),
2983 * in case the caller doesn't check the return status properly. */
2984 *mac_length = mac_size;
2985 /* If mac_size is 0 then mac may be NULL and then the
2986 * call to memset would have undefined behavior. */
2987 if( mac_size != 0 )
2988 memset( mac, '!', mac_size );
2989
Gilles Peskine89167cb2018-07-08 20:12:23 +02002990 if( ! operation->is_sign )
2991 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002992 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002993 }
mohammad16036df908f2018-04-02 08:34:15 -07002994
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002995 status = psa_mac_finish_internal( operation, mac, mac_size );
2996
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002997 if( status == PSA_SUCCESS )
2998 {
2999 status = psa_mac_abort( operation );
3000 if( status == PSA_SUCCESS )
3001 *mac_length = operation->mac_size;
3002 else
3003 memset( mac, '!', mac_size );
3004 }
3005 else
3006 psa_mac_abort( operation );
3007 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003008}
3009
Gilles Peskineacd4be32018-07-08 19:56:25 +02003010psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3011 const uint8_t *mac,
3012 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013{
Gilles Peskine828ed142018-06-18 23:25:51 +02003014 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003015 psa_status_t status;
3016
Jaeden Amero252ef282019-02-15 14:05:35 +00003017 if( operation->alg == 0 )
3018 {
3019 return( PSA_ERROR_BAD_STATE );
3020 }
3021
Gilles Peskine89167cb2018-07-08 20:12:23 +02003022 if( operation->is_sign )
3023 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003024 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003025 }
3026 if( operation->mac_size != mac_length )
3027 {
3028 status = PSA_ERROR_INVALID_SIGNATURE;
3029 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003030 }
mohammad16036df908f2018-04-02 08:34:15 -07003031
3032 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003033 actual_mac, sizeof( actual_mac ) );
3034
3035 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3036 status = PSA_ERROR_INVALID_SIGNATURE;
3037
3038cleanup:
3039 if( status == PSA_SUCCESS )
3040 status = psa_mac_abort( operation );
3041 else
3042 psa_mac_abort( operation );
3043
Gilles Peskine3f108122018-12-07 18:14:53 +01003044 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003045
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003046 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003047}
3048
3049
Gilles Peskine20035e32018-02-03 22:44:14 +01003050
Gilles Peskine20035e32018-02-03 22:44:14 +01003051/****************************************************************/
3052/* Asymmetric cryptography */
3053/****************************************************************/
3054
Gilles Peskine2b450e32018-06-27 15:42:46 +02003055#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003056/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003057 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003058static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3059 size_t hash_length,
3060 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003061{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003062 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003063 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003064 *md_alg = mbedtls_md_get_type( md_info );
3065
3066 /* The Mbed TLS RSA module uses an unsigned int for hash length
3067 * parameters. Validate that it fits so that we don't risk an
3068 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003069#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003070 if( hash_length > UINT_MAX )
3071 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003072#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003073
3074#if defined(MBEDTLS_PKCS1_V15)
3075 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3076 * must be correct. */
3077 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3078 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003079 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003080 if( md_info == NULL )
3081 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003082 if( mbedtls_md_get_size( md_info ) != hash_length )
3083 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003084 }
3085#endif /* MBEDTLS_PKCS1_V15 */
3086
3087#if defined(MBEDTLS_PKCS1_V21)
3088 /* PSS requires a hash internally. */
3089 if( PSA_ALG_IS_RSA_PSS( alg ) )
3090 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003091 if( md_info == NULL )
3092 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003093 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003094#endif /* MBEDTLS_PKCS1_V21 */
3095
Gilles Peskine61b91d42018-06-08 16:09:36 +02003096 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003097}
3098
Gilles Peskine2b450e32018-06-27 15:42:46 +02003099static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3100 psa_algorithm_t alg,
3101 const uint8_t *hash,
3102 size_t hash_length,
3103 uint8_t *signature,
3104 size_t signature_size,
3105 size_t *signature_length )
3106{
3107 psa_status_t status;
3108 int ret;
3109 mbedtls_md_type_t md_alg;
3110
3111 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3112 if( status != PSA_SUCCESS )
3113 return( status );
3114
Gilles Peskine630a18a2018-06-29 17:49:35 +02003115 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003116 return( PSA_ERROR_BUFFER_TOO_SMALL );
3117
3118#if defined(MBEDTLS_PKCS1_V15)
3119 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3120 {
3121 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3122 MBEDTLS_MD_NONE );
3123 ret = mbedtls_rsa_pkcs1_sign( rsa,
3124 mbedtls_ctr_drbg_random,
3125 &global_data.ctr_drbg,
3126 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003127 md_alg,
3128 (unsigned int) hash_length,
3129 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003130 signature );
3131 }
3132 else
3133#endif /* MBEDTLS_PKCS1_V15 */
3134#if defined(MBEDTLS_PKCS1_V21)
3135 if( PSA_ALG_IS_RSA_PSS( alg ) )
3136 {
3137 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3138 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3139 mbedtls_ctr_drbg_random,
3140 &global_data.ctr_drbg,
3141 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003142 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003143 (unsigned int) hash_length,
3144 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003145 signature );
3146 }
3147 else
3148#endif /* MBEDTLS_PKCS1_V21 */
3149 {
3150 return( PSA_ERROR_INVALID_ARGUMENT );
3151 }
3152
3153 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003154 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003155 return( mbedtls_to_psa_error( ret ) );
3156}
3157
3158static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3159 psa_algorithm_t alg,
3160 const uint8_t *hash,
3161 size_t hash_length,
3162 const uint8_t *signature,
3163 size_t signature_length )
3164{
3165 psa_status_t status;
3166 int ret;
3167 mbedtls_md_type_t md_alg;
3168
3169 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3170 if( status != PSA_SUCCESS )
3171 return( status );
3172
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003173 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3174 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003175
3176#if defined(MBEDTLS_PKCS1_V15)
3177 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3178 {
3179 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3180 MBEDTLS_MD_NONE );
3181 ret = mbedtls_rsa_pkcs1_verify( rsa,
3182 mbedtls_ctr_drbg_random,
3183 &global_data.ctr_drbg,
3184 MBEDTLS_RSA_PUBLIC,
3185 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003186 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003187 hash,
3188 signature );
3189 }
3190 else
3191#endif /* MBEDTLS_PKCS1_V15 */
3192#if defined(MBEDTLS_PKCS1_V21)
3193 if( PSA_ALG_IS_RSA_PSS( alg ) )
3194 {
3195 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3196 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3197 mbedtls_ctr_drbg_random,
3198 &global_data.ctr_drbg,
3199 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003200 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003201 (unsigned int) hash_length,
3202 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003203 signature );
3204 }
3205 else
3206#endif /* MBEDTLS_PKCS1_V21 */
3207 {
3208 return( PSA_ERROR_INVALID_ARGUMENT );
3209 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003210
3211 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3212 * the rest of the signature is invalid". This has little use in
3213 * practice and PSA doesn't report this distinction. */
3214 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3215 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003216 return( mbedtls_to_psa_error( ret ) );
3217}
3218#endif /* MBEDTLS_RSA_C */
3219
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003220#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003221/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3222 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3223 * (even though these functions don't modify it). */
3224static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3225 psa_algorithm_t alg,
3226 const uint8_t *hash,
3227 size_t hash_length,
3228 uint8_t *signature,
3229 size_t signature_size,
3230 size_t *signature_length )
3231{
3232 int ret;
3233 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003234 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003235 mbedtls_mpi_init( &r );
3236 mbedtls_mpi_init( &s );
3237
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003238 if( signature_size < 2 * curve_bytes )
3239 {
3240 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3241 goto cleanup;
3242 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003243
Gilles Peskinea05219c2018-11-16 16:02:56 +01003244#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003245 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3246 {
3247 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3248 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3249 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003250 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3251 &ecp->d, hash,
3252 hash_length, md_alg,
3253 mbedtls_ctr_drbg_random,
3254 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003255 }
3256 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003257#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003258 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003259 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003260 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3261 hash, hash_length,
3262 mbedtls_ctr_drbg_random,
3263 &global_data.ctr_drbg ) );
3264 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003265
3266 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3267 signature,
3268 curve_bytes ) );
3269 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3270 signature + curve_bytes,
3271 curve_bytes ) );
3272
3273cleanup:
3274 mbedtls_mpi_free( &r );
3275 mbedtls_mpi_free( &s );
3276 if( ret == 0 )
3277 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003278 return( mbedtls_to_psa_error( ret ) );
3279}
3280
3281static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3282 const uint8_t *hash,
3283 size_t hash_length,
3284 const uint8_t *signature,
3285 size_t signature_length )
3286{
3287 int ret;
3288 mbedtls_mpi r, s;
3289 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3290 mbedtls_mpi_init( &r );
3291 mbedtls_mpi_init( &s );
3292
3293 if( signature_length != 2 * curve_bytes )
3294 return( PSA_ERROR_INVALID_SIGNATURE );
3295
3296 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3297 signature,
3298 curve_bytes ) );
3299 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3300 signature + curve_bytes,
3301 curve_bytes ) );
3302
3303 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3304 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003305
3306cleanup:
3307 mbedtls_mpi_free( &r );
3308 mbedtls_mpi_free( &s );
3309 return( mbedtls_to_psa_error( ret ) );
3310}
3311#endif /* MBEDTLS_ECDSA_C */
3312
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003313psa_status_t psa_sign_hash( psa_key_handle_t handle,
3314 psa_algorithm_t alg,
3315 const uint8_t *hash,
3316 size_t hash_length,
3317 uint8_t *signature,
3318 size_t signature_size,
3319 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003320{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003321 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003322 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003323#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3324 const psa_drv_se_t *drv;
3325 psa_drv_se_context_t *drv_context;
3326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003327
3328 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003329 /* Immediately reject a zero-length signature buffer. This guarantees
3330 * that signature must be a valid pointer. (On the other hand, the hash
3331 * buffer can in principle be empty since it doesn't actually have
3332 * to be a hash.) */
3333 if( signature_size == 0 )
3334 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003335
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003336 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003337 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003338 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003339 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003340 {
3341 status = PSA_ERROR_INVALID_ARGUMENT;
3342 goto exit;
3343 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003344
Gilles Peskineedc64242019-08-07 21:05:07 +02003345#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3346 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3347 {
3348 if( drv->asymmetric == NULL ||
3349 drv->asymmetric->p_sign == NULL )
3350 {
3351 status = PSA_ERROR_NOT_SUPPORTED;
3352 goto exit;
3353 }
3354 status = drv->asymmetric->p_sign( drv_context,
3355 slot->data.se.slot_number,
3356 alg,
3357 hash, hash_length,
3358 signature, signature_size,
3359 signature_length );
3360 }
3361 else
3362#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003363#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003364 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003365 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003366 status = psa_rsa_sign( slot->data.rsa,
3367 alg,
3368 hash, hash_length,
3369 signature, signature_size,
3370 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003371 }
3372 else
3373#endif /* defined(MBEDTLS_RSA_C) */
3374#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003375 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003376 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003377#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003378 if(
3379#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3380 PSA_ALG_IS_ECDSA( alg )
3381#else
3382 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3383#endif
3384 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003385 status = psa_ecdsa_sign( slot->data.ecp,
3386 alg,
3387 hash, hash_length,
3388 signature, signature_size,
3389 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003390 else
3391#endif /* defined(MBEDTLS_ECDSA_C) */
3392 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003393 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003394 }
itayzafrir5c753392018-05-08 11:18:38 +03003395 }
3396 else
3397#endif /* defined(MBEDTLS_ECP_C) */
3398 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003399 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003400 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003401
3402exit:
3403 /* Fill the unused part of the output buffer (the whole buffer on error,
3404 * the trailing part on success) with something that isn't a valid mac
3405 * (barring an attack on the mac and deliberately-crafted input),
3406 * in case the caller doesn't check the return status properly. */
3407 if( status == PSA_SUCCESS )
3408 memset( signature + *signature_length, '!',
3409 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003410 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003412 /* If signature_size is 0 then we have nothing to do. We must not call
3413 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003415}
3416
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003417psa_status_t psa_verify_hash( psa_key_handle_t handle,
3418 psa_algorithm_t alg,
3419 const uint8_t *hash,
3420 size_t hash_length,
3421 const uint8_t *signature,
3422 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003423{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003424 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003425 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3427 const psa_drv_se_t *drv;
3428 psa_drv_se_context_t *drv_context;
3429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003430
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003431 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003432 if( status != PSA_SUCCESS )
3433 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003434
Gilles Peskineedc64242019-08-07 21:05:07 +02003435#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3436 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3437 {
3438 if( drv->asymmetric == NULL ||
3439 drv->asymmetric->p_verify == NULL )
3440 return( PSA_ERROR_NOT_SUPPORTED );
3441 return( drv->asymmetric->p_verify( drv_context,
3442 slot->data.se.slot_number,
3443 alg,
3444 hash, hash_length,
3445 signature, signature_length ) );
3446 }
3447 else
3448#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003449#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003450 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003452 return( psa_rsa_verify( slot->data.rsa,
3453 alg,
3454 hash, hash_length,
3455 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003456 }
3457 else
3458#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003459#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003460 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003461 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003462#if defined(MBEDTLS_ECDSA_C)
3463 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003464 return( psa_ecdsa_verify( slot->data.ecp,
3465 hash, hash_length,
3466 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467 else
3468#endif /* defined(MBEDTLS_ECDSA_C) */
3469 {
3470 return( PSA_ERROR_INVALID_ARGUMENT );
3471 }
itayzafrir5c753392018-05-08 11:18:38 +03003472 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003473 else
3474#endif /* defined(MBEDTLS_ECP_C) */
3475 {
3476 return( PSA_ERROR_NOT_SUPPORTED );
3477 }
3478}
3479
Gilles Peskine072ac562018-06-30 00:21:29 +02003480#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3481static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3482 mbedtls_rsa_context *rsa )
3483{
3484 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3485 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3486 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3487 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3488}
3489#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3490
Gilles Peskinec5487a82018-12-03 18:08:14 +01003491psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003492 psa_algorithm_t alg,
3493 const uint8_t *input,
3494 size_t input_length,
3495 const uint8_t *salt,
3496 size_t salt_length,
3497 uint8_t *output,
3498 size_t output_size,
3499 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003500{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003501 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003502 psa_status_t status;
3503
Darryl Green5cc689a2018-07-24 15:34:10 +01003504 (void) input;
3505 (void) input_length;
3506 (void) salt;
3507 (void) output;
3508 (void) output_size;
3509
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003510 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003511
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003512 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3513 return( PSA_ERROR_INVALID_ARGUMENT );
3514
Gilles Peskine28f8f302019-07-24 13:30:31 +02003515 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003516 if( status != PSA_SUCCESS )
3517 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003518 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3519 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003520 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003521
3522#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003523 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524 {
3525 mbedtls_rsa_context *rsa = slot->data.rsa;
3526 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003527 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003528 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003530 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003531 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003532 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3533 mbedtls_ctr_drbg_random,
3534 &global_data.ctr_drbg,
3535 MBEDTLS_RSA_PUBLIC,
3536 input_length,
3537 input,
3538 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539 }
3540 else
3541#endif /* MBEDTLS_PKCS1_V15 */
3542#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003543 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003544 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003545 psa_rsa_oaep_set_padding_mode( alg, rsa );
3546 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3547 mbedtls_ctr_drbg_random,
3548 &global_data.ctr_drbg,
3549 MBEDTLS_RSA_PUBLIC,
3550 salt, salt_length,
3551 input_length,
3552 input,
3553 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554 }
3555 else
3556#endif /* MBEDTLS_PKCS1_V21 */
3557 {
3558 return( PSA_ERROR_INVALID_ARGUMENT );
3559 }
3560 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003561 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562 return( mbedtls_to_psa_error( ret ) );
3563 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003565#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003566 {
3567 return( PSA_ERROR_NOT_SUPPORTED );
3568 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003569}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003570
Gilles Peskinec5487a82018-12-03 18:08:14 +01003571psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003572 psa_algorithm_t alg,
3573 const uint8_t *input,
3574 size_t input_length,
3575 const uint8_t *salt,
3576 size_t salt_length,
3577 uint8_t *output,
3578 size_t output_size,
3579 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003581 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003582 psa_status_t status;
3583
Darryl Green5cc689a2018-07-24 15:34:10 +01003584 (void) input;
3585 (void) input_length;
3586 (void) salt;
3587 (void) output;
3588 (void) output_size;
3589
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003590 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003592 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3593 return( PSA_ERROR_INVALID_ARGUMENT );
3594
Gilles Peskine28f8f302019-07-24 13:30:31 +02003595 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003596 if( status != PSA_SUCCESS )
3597 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003598 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599 return( PSA_ERROR_INVALID_ARGUMENT );
3600
3601#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003602 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603 {
3604 mbedtls_rsa_context *rsa = slot->data.rsa;
3605 int ret;
3606
Gilles Peskine630a18a2018-06-29 17:49:35 +02003607 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608 return( PSA_ERROR_INVALID_ARGUMENT );
3609
3610#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003611 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003613 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3614 mbedtls_ctr_drbg_random,
3615 &global_data.ctr_drbg,
3616 MBEDTLS_RSA_PRIVATE,
3617 output_length,
3618 input,
3619 output,
3620 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621 }
3622 else
3623#endif /* MBEDTLS_PKCS1_V15 */
3624#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003625 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003626 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003627 psa_rsa_oaep_set_padding_mode( alg, rsa );
3628 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3629 mbedtls_ctr_drbg_random,
3630 &global_data.ctr_drbg,
3631 MBEDTLS_RSA_PRIVATE,
3632 salt, salt_length,
3633 output_length,
3634 input,
3635 output,
3636 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637 }
3638 else
3639#endif /* MBEDTLS_PKCS1_V21 */
3640 {
3641 return( PSA_ERROR_INVALID_ARGUMENT );
3642 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003643
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 return( mbedtls_to_psa_error( ret ) );
3645 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003647#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648 {
3649 return( PSA_ERROR_NOT_SUPPORTED );
3650 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003651}
Gilles Peskine20035e32018-02-03 22:44:14 +01003652
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003653
3654
mohammad1603503973b2018-03-12 15:59:30 +02003655/****************************************************************/
3656/* Symmetric cryptography */
3657/****************************************************************/
3658
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003659/* Initialize the cipher operation structure. Once this function has been
3660 * called, psa_cipher_abort can run and will do the right thing. */
3661static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3662 psa_algorithm_t alg )
3663{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003664 if( ! PSA_ALG_IS_CIPHER( alg ) )
3665 {
3666 memset( operation, 0, sizeof( *operation ) );
3667 return( PSA_ERROR_INVALID_ARGUMENT );
3668 }
3669
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003670 operation->alg = alg;
3671 operation->key_set = 0;
3672 operation->iv_set = 0;
3673 operation->iv_required = 1;
3674 operation->iv_size = 0;
3675 operation->block_size = 0;
3676 mbedtls_cipher_init( &operation->ctx.cipher );
3677 return( PSA_SUCCESS );
3678}
3679
Gilles Peskinee553c652018-06-04 16:22:46 +02003680static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003681 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003682 psa_algorithm_t alg,
3683 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003684{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003685 int ret = 0;
3686 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003687 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003688 size_t key_bits;
3689 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003690 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3691 PSA_KEY_USAGE_ENCRYPT :
3692 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003693
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003694 /* A context must be freshly initialized before it can be set up. */
3695 if( operation->alg != 0 )
3696 {
3697 return( PSA_ERROR_BAD_STATE );
3698 }
3699
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003700 status = psa_cipher_init( operation, alg );
3701 if( status != PSA_SUCCESS )
3702 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003703
Gilles Peskine28f8f302019-07-24 13:30:31 +02003704 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003705 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003706 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003707 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003708
Gilles Peskine8e338702019-07-30 20:06:31 +02003709 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003710 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003711 {
3712 status = PSA_ERROR_NOT_SUPPORTED;
3713 goto exit;
3714 }
mohammad1603503973b2018-03-12 15:59:30 +02003715
mohammad1603503973b2018-03-12 15:59:30 +02003716 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003717 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003718 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003719
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003720#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003721 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003722 {
3723 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003724 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003725 memcpy( keys, slot->data.raw.data, 16 );
3726 memcpy( keys + 16, slot->data.raw.data, 8 );
3727 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3728 keys,
3729 192, cipher_operation );
3730 }
3731 else
3732#endif
3733 {
3734 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3735 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003736 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003737 }
Moran Peker41deec42018-04-04 15:43:05 +03003738 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003739 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003740
mohammad16038481e742018-03-18 13:57:31 +02003741#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003742 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003743 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003744 case PSA_ALG_CBC_NO_PADDING:
3745 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3746 MBEDTLS_PADDING_NONE );
3747 break;
3748 case PSA_ALG_CBC_PKCS7:
3749 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3750 MBEDTLS_PADDING_PKCS7 );
3751 break;
3752 default:
3753 /* The algorithm doesn't involve padding. */
3754 ret = 0;
3755 break;
3756 }
3757 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003758 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003759#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3760
mohammad1603503973b2018-03-12 15:59:30 +02003761 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003762 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003763 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003764 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003765 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003766 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003767 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003768#if defined(MBEDTLS_CHACHA20_C)
3769 else
3770 if( alg == PSA_ALG_CHACHA20 )
3771 operation->iv_size = 12;
3772#endif
mohammad1603503973b2018-03-12 15:59:30 +02003773
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003774exit:
3775 if( status == 0 )
3776 status = mbedtls_to_psa_error( ret );
3777 if( status != 0 )
3778 psa_cipher_abort( operation );
3779 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003780}
3781
Gilles Peskinefe119512018-07-08 21:39:34 +02003782psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003783 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003784 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003785{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003786 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003787}
3788
Gilles Peskinefe119512018-07-08 21:39:34 +02003789psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003790 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003791 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003792{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003793 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003794}
3795
Gilles Peskinefe119512018-07-08 21:39:34 +02003796psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003797 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003798 size_t iv_size,
3799 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003800{
itayzafrir534bd7c2018-08-02 13:56:32 +03003801 psa_status_t status;
3802 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003803 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003804 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003805 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003806 }
Moran Peker41deec42018-04-04 15:43:05 +03003807 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003808 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003809 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003810 goto exit;
3811 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003812 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3813 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003814 if( ret != 0 )
3815 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003816 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003817 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003818 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003819
mohammad16038481e742018-03-18 13:57:31 +02003820 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003821 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003822
Moran Peker395db872018-05-31 14:07:14 +03003823exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003824 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003825 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003826 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003827}
3828
Gilles Peskinefe119512018-07-08 21:39:34 +02003829psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003830 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003831 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003832{
itayzafrir534bd7c2018-08-02 13:56:32 +03003833 psa_status_t status;
3834 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003835 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003836 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003837 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003838 }
Moran Pekera28258c2018-05-29 16:25:04 +03003839 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003840 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003841 status = PSA_ERROR_INVALID_ARGUMENT;
3842 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003843 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003844 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3845 status = mbedtls_to_psa_error( ret );
3846exit:
3847 if( status == PSA_SUCCESS )
3848 operation->iv_set = 1;
3849 else
mohammad1603503973b2018-03-12 15:59:30 +02003850 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003851 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003852}
3853
Gilles Peskinee553c652018-06-04 16:22:46 +02003854psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3855 const uint8_t *input,
3856 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003857 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003858 size_t output_size,
3859 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003860{
itayzafrir534bd7c2018-08-02 13:56:32 +03003861 psa_status_t status;
3862 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003863 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003864
3865 if( operation->alg == 0 )
3866 {
3867 return( PSA_ERROR_BAD_STATE );
3868 }
3869
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003870 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003871 {
3872 /* Take the unprocessed partial block left over from previous
3873 * update calls, if any, plus the input to this call. Remove
3874 * the last partial block, if any. You get the data that will be
3875 * output in this call. */
3876 expected_output_size =
3877 ( operation->ctx.cipher.unprocessed_len + input_length )
3878 / operation->block_size * operation->block_size;
3879 }
3880 else
3881 {
3882 expected_output_size = input_length;
3883 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003884
Gilles Peskine89d789c2018-06-04 17:17:16 +02003885 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003886 {
3887 status = PSA_ERROR_BUFFER_TOO_SMALL;
3888 goto exit;
3889 }
mohammad160382759612018-03-12 18:16:40 +02003890
mohammad1603503973b2018-03-12 15:59:30 +02003891 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003892 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003893 status = mbedtls_to_psa_error( ret );
3894exit:
3895 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003896 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003898}
3899
Gilles Peskinee553c652018-06-04 16:22:46 +02003900psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3901 uint8_t *output,
3902 size_t output_size,
3903 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003904{
David Saadab4ecc272019-02-14 13:48:10 +02003905 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003906 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003907 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003908
mohammad1603503973b2018-03-12 15:59:30 +02003909 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003910 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003911 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003912 }
3913 if( operation->iv_required && ! operation->iv_set )
3914 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003915 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003916 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003917
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003918 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003919 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3920 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003921 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003922 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003923 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003924 }
3925
Janos Follath315b51c2018-07-09 16:04:51 +01003926 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3927 temp_output_buffer,
3928 output_length );
3929 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003930 {
Janos Follath315b51c2018-07-09 16:04:51 +01003931 status = mbedtls_to_psa_error( cipher_ret );
3932 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003933 }
Janos Follath315b51c2018-07-09 16:04:51 +01003934
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003935 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003936 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003937 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003938 memcpy( output, temp_output_buffer, *output_length );
3939 else
3940 {
Janos Follath315b51c2018-07-09 16:04:51 +01003941 status = PSA_ERROR_BUFFER_TOO_SMALL;
3942 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003943 }
mohammad1603503973b2018-03-12 15:59:30 +02003944
Gilles Peskine3f108122018-12-07 18:14:53 +01003945 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003946 status = psa_cipher_abort( operation );
3947
3948 return( status );
3949
3950error:
3951
3952 *output_length = 0;
3953
Gilles Peskine3f108122018-12-07 18:14:53 +01003954 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003955 (void) psa_cipher_abort( operation );
3956
3957 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003958}
3959
Gilles Peskinee553c652018-06-04 16:22:46 +02003960psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3961{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003962 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003963 {
3964 /* The object has (apparently) been initialized but it is not
3965 * in use. It's ok to call abort on such an object, and there's
3966 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003967 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003968 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003969
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003970 /* Sanity check (shouldn't happen: operation->alg should
3971 * always have been initialized to a valid value). */
3972 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3973 return( PSA_ERROR_BAD_STATE );
3974
mohammad1603503973b2018-03-12 15:59:30 +02003975 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003976
Moran Peker41deec42018-04-04 15:43:05 +03003977 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003978 operation->key_set = 0;
3979 operation->iv_set = 0;
3980 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003981 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003982 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003983
Moran Peker395db872018-05-31 14:07:14 +03003984 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003985}
3986
Gilles Peskinea0655c32018-04-30 17:06:50 +02003987
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003988
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003989
mohammad16035955c982018-04-26 00:53:03 +03003990/****************************************************************/
3991/* AEAD */
3992/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003993
Gilles Peskineedf9a652018-08-17 18:11:56 +02003994typedef struct
3995{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003996 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003997 const mbedtls_cipher_info_t *cipher_info;
3998 union
3999 {
4000#if defined(MBEDTLS_CCM_C)
4001 mbedtls_ccm_context ccm;
4002#endif /* MBEDTLS_CCM_C */
4003#if defined(MBEDTLS_GCM_C)
4004 mbedtls_gcm_context gcm;
4005#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004006#if defined(MBEDTLS_CHACHAPOLY_C)
4007 mbedtls_chachapoly_context chachapoly;
4008#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004009 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004010 psa_algorithm_t core_alg;
4011 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004012 uint8_t tag_length;
4013} aead_operation_t;
4014
Gilles Peskine30a9e412019-01-14 18:36:12 +01004015static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004016{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004017 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004018 {
4019#if defined(MBEDTLS_CCM_C)
4020 case PSA_ALG_CCM:
4021 mbedtls_ccm_free( &operation->ctx.ccm );
4022 break;
4023#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004024#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004025 case PSA_ALG_GCM:
4026 mbedtls_gcm_free( &operation->ctx.gcm );
4027 break;
4028#endif /* MBEDTLS_GCM_C */
4029 }
4030}
4031
4032static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004033 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004034 psa_key_usage_t usage,
4035 psa_algorithm_t alg )
4036{
4037 psa_status_t status;
4038 size_t key_bits;
4039 mbedtls_cipher_id_t cipher_id;
4040
Gilles Peskine28f8f302019-07-24 13:30:31 +02004041 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004042 if( status != PSA_SUCCESS )
4043 return( status );
4044
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004045 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004046
4047 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004048 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049 &cipher_id );
4050 if( operation->cipher_info == NULL )
4051 return( PSA_ERROR_NOT_SUPPORTED );
4052
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004053 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004054 {
4055#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004056 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4057 operation->core_alg = PSA_ALG_CCM;
4058 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004059 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4060 * The call to mbedtls_ccm_encrypt_and_tag or
4061 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004062 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004063 return( PSA_ERROR_INVALID_ARGUMENT );
4064 mbedtls_ccm_init( &operation->ctx.ccm );
4065 status = mbedtls_to_psa_error(
4066 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4067 operation->slot->data.raw.data,
4068 (unsigned int) key_bits ) );
4069 if( status != 0 )
4070 goto cleanup;
4071 break;
4072#endif /* MBEDTLS_CCM_C */
4073
4074#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004075 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4076 operation->core_alg = PSA_ALG_GCM;
4077 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004078 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4079 * The call to mbedtls_gcm_crypt_and_tag or
4080 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004081 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004082 return( PSA_ERROR_INVALID_ARGUMENT );
4083 mbedtls_gcm_init( &operation->ctx.gcm );
4084 status = mbedtls_to_psa_error(
4085 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4086 operation->slot->data.raw.data,
4087 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004088 if( status != 0 )
4089 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004090 break;
4091#endif /* MBEDTLS_GCM_C */
4092
Gilles Peskine26869f22019-05-06 15:25:00 +02004093#if defined(MBEDTLS_CHACHAPOLY_C)
4094 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4095 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4096 operation->full_tag_length = 16;
4097 /* We only support the default tag length. */
4098 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4099 return( PSA_ERROR_NOT_SUPPORTED );
4100 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4101 status = mbedtls_to_psa_error(
4102 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4103 operation->slot->data.raw.data ) );
4104 if( status != 0 )
4105 goto cleanup;
4106 break;
4107#endif /* MBEDTLS_CHACHAPOLY_C */
4108
Gilles Peskineedf9a652018-08-17 18:11:56 +02004109 default:
4110 return( PSA_ERROR_NOT_SUPPORTED );
4111 }
4112
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004113 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4114 {
4115 status = PSA_ERROR_INVALID_ARGUMENT;
4116 goto cleanup;
4117 }
4118 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004119
Gilles Peskineedf9a652018-08-17 18:11:56 +02004120 return( PSA_SUCCESS );
4121
4122cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004123 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004124 return( status );
4125}
4126
Gilles Peskinec5487a82018-12-03 18:08:14 +01004127psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004128 psa_algorithm_t alg,
4129 const uint8_t *nonce,
4130 size_t nonce_length,
4131 const uint8_t *additional_data,
4132 size_t additional_data_length,
4133 const uint8_t *plaintext,
4134 size_t plaintext_length,
4135 uint8_t *ciphertext,
4136 size_t ciphertext_size,
4137 size_t *ciphertext_length )
4138{
mohammad16035955c982018-04-26 00:53:03 +03004139 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004140 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004141 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004142
mohammad1603f08a5502018-06-03 15:05:47 +03004143 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004144
Gilles Peskinec5487a82018-12-03 18:08:14 +01004145 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004146 if( status != PSA_SUCCESS )
4147 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004148
Gilles Peskineedf9a652018-08-17 18:11:56 +02004149 /* For all currently supported modes, the tag is at the end of the
4150 * ciphertext. */
4151 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4152 {
4153 status = PSA_ERROR_BUFFER_TOO_SMALL;
4154 goto exit;
4155 }
4156 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004157
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004158#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004159 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004160 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 status = mbedtls_to_psa_error(
4162 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4163 MBEDTLS_GCM_ENCRYPT,
4164 plaintext_length,
4165 nonce, nonce_length,
4166 additional_data, additional_data_length,
4167 plaintext, ciphertext,
4168 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004169 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004170 else
4171#endif /* MBEDTLS_GCM_C */
4172#if defined(MBEDTLS_CCM_C)
4173 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004174 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004175 status = mbedtls_to_psa_error(
4176 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4177 plaintext_length,
4178 nonce, nonce_length,
4179 additional_data,
4180 additional_data_length,
4181 plaintext, ciphertext,
4182 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004183 }
mohammad16035c8845f2018-05-09 05:40:09 -07004184 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004185#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004186#if defined(MBEDTLS_CHACHAPOLY_C)
4187 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4188 {
4189 if( nonce_length != 12 || operation.tag_length != 16 )
4190 {
4191 status = PSA_ERROR_NOT_SUPPORTED;
4192 goto exit;
4193 }
4194 status = mbedtls_to_psa_error(
4195 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4196 plaintext_length,
4197 nonce,
4198 additional_data,
4199 additional_data_length,
4200 plaintext,
4201 ciphertext,
4202 tag ) );
4203 }
4204 else
4205#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004206 {
mohammad1603554faad2018-06-03 15:07:38 +03004207 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004208 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004209
Gilles Peskineedf9a652018-08-17 18:11:56 +02004210 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4211 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004212
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004214 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004215 if( status == PSA_SUCCESS )
4216 *ciphertext_length = plaintext_length + operation.tag_length;
4217 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004218}
4219
Gilles Peskineee652a32018-06-01 19:23:52 +02004220/* Locate the tag in a ciphertext buffer containing the encrypted data
4221 * followed by the tag. Return the length of the part preceding the tag in
4222 * *plaintext_length. This is the size of the plaintext in modes where
4223 * the encrypted data has the same size as the plaintext, such as
4224 * CCM and GCM. */
4225static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4226 const uint8_t *ciphertext,
4227 size_t ciphertext_length,
4228 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004229 const uint8_t **p_tag )
4230{
4231 size_t payload_length;
4232 if( tag_length > ciphertext_length )
4233 return( PSA_ERROR_INVALID_ARGUMENT );
4234 payload_length = ciphertext_length - tag_length;
4235 if( payload_length > plaintext_size )
4236 return( PSA_ERROR_BUFFER_TOO_SMALL );
4237 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004238 return( PSA_SUCCESS );
4239}
4240
Gilles Peskinec5487a82018-12-03 18:08:14 +01004241psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004242 psa_algorithm_t alg,
4243 const uint8_t *nonce,
4244 size_t nonce_length,
4245 const uint8_t *additional_data,
4246 size_t additional_data_length,
4247 const uint8_t *ciphertext,
4248 size_t ciphertext_length,
4249 uint8_t *plaintext,
4250 size_t plaintext_size,
4251 size_t *plaintext_length )
4252{
mohammad16035955c982018-04-26 00:53:03 +03004253 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004254 aead_operation_t operation;
4255 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004256
Gilles Peskineee652a32018-06-01 19:23:52 +02004257 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004258
Gilles Peskinec5487a82018-12-03 18:08:14 +01004259 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004260 if( status != PSA_SUCCESS )
4261 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004262
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004263 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4264 ciphertext, ciphertext_length,
4265 plaintext_size, &tag );
4266 if( status != PSA_SUCCESS )
4267 goto exit;
4268
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004269#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004270 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004271 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004272 status = mbedtls_to_psa_error(
4273 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4274 ciphertext_length - operation.tag_length,
4275 nonce, nonce_length,
4276 additional_data,
4277 additional_data_length,
4278 tag, operation.tag_length,
4279 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004280 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004281 else
4282#endif /* MBEDTLS_GCM_C */
4283#if defined(MBEDTLS_CCM_C)
4284 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004285 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004286 status = mbedtls_to_psa_error(
4287 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4288 ciphertext_length - operation.tag_length,
4289 nonce, nonce_length,
4290 additional_data,
4291 additional_data_length,
4292 ciphertext, plaintext,
4293 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004294 }
mohammad160339574652018-06-01 04:39:53 -07004295 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004296#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004297#if defined(MBEDTLS_CHACHAPOLY_C)
4298 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4299 {
4300 if( nonce_length != 12 || operation.tag_length != 16 )
4301 {
4302 status = PSA_ERROR_NOT_SUPPORTED;
4303 goto exit;
4304 }
4305 status = mbedtls_to_psa_error(
4306 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4307 ciphertext_length - operation.tag_length,
4308 nonce,
4309 additional_data,
4310 additional_data_length,
4311 tag,
4312 ciphertext,
4313 plaintext ) );
4314 }
4315 else
4316#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004317 {
mohammad1603554faad2018-06-03 15:07:38 +03004318 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004319 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004320
Gilles Peskineedf9a652018-08-17 18:11:56 +02004321 if( status != PSA_SUCCESS && plaintext_size != 0 )
4322 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004323
Gilles Peskineedf9a652018-08-17 18:11:56 +02004324exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004325 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004326 if( status == PSA_SUCCESS )
4327 *plaintext_length = ciphertext_length - operation.tag_length;
4328 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004329}
4330
Gilles Peskinea0655c32018-04-30 17:06:50 +02004331
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004332
Gilles Peskine20035e32018-02-03 22:44:14 +01004333/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004334/* Generators */
4335/****************************************************************/
4336
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004337#define HKDF_STATE_INIT 0 /* no input yet */
4338#define HKDF_STATE_STARTED 1 /* got salt */
4339#define HKDF_STATE_KEYED 2 /* got key */
4340#define HKDF_STATE_OUTPUT 3 /* output started */
4341
Gilles Peskinecbe66502019-05-16 16:59:18 +02004342static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004344{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4346 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004348 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004349}
4350
4351
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004352psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004353{
4354 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004356 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004357 {
4358 /* The object has (apparently) been initialized but it is not
4359 * in use. It's ok to call abort on such an object, and there's
4360 * nothing to do. */
4361 }
4362 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004363#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004364 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004365 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 mbedtls_free( operation->ctx.hkdf.info );
4367 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004368 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004369 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004371 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004372 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004373 if( operation->ctx.tls12_prf.seed != NULL )
4374 {
4375 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4376 operation->ctx.tls12_prf.seed_length );
4377 mbedtls_free( operation->ctx.tls12_prf.seed );
4378 }
4379
4380 if( operation->ctx.tls12_prf.label != NULL )
4381 {
4382 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4383 operation->ctx.tls12_prf.label_length );
4384 mbedtls_free( operation->ctx.tls12_prf.label );
4385 }
4386
4387 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4388
4389 /* We leave the fields Ai and output_block to be erased safely by the
4390 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004391 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004392 else
4393#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004394 {
4395 status = PSA_ERROR_BAD_STATE;
4396 }
Janos Follath083036a2019-06-11 10:22:26 +01004397 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004398 return( status );
4399}
4400
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004402 size_t *capacity)
4403{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004405 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004407 return PSA_ERROR_BAD_STATE;
4408 }
4409
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004411 return( PSA_SUCCESS );
4412}
4413
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 size_t capacity )
4416{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004417 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004418 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004420 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004422 return( PSA_SUCCESS );
4423}
4424
Gilles Peskinebef7f142018-07-12 17:22:21 +02004425#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004427 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004428static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004429 psa_algorithm_t hash_alg,
4430 uint8_t *output,
4431 size_t output_length )
4432{
4433 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4434 psa_status_t status;
4435
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004436 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4437 return( PSA_ERROR_BAD_STATE );
4438 hkdf->state = HKDF_STATE_OUTPUT;
4439
Gilles Peskinebef7f142018-07-12 17:22:21 +02004440 while( output_length != 0 )
4441 {
4442 /* Copy what remains of the current block */
4443 uint8_t n = hash_length - hkdf->offset_in_block;
4444 if( n > output_length )
4445 n = (uint8_t) output_length;
4446 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4447 output += n;
4448 output_length -= n;
4449 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004450 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004451 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004452 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004453 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004454 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004455 * object was corrupted or if this function is called directly
4456 * inside the library. */
4457 if( hkdf->block_number == 0xff )
4458 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004459
4460 /* We need a new block */
4461 ++hkdf->block_number;
4462 hkdf->offset_in_block = 0;
4463 status = psa_hmac_setup_internal( &hkdf->hmac,
4464 hkdf->prk, hash_length,
4465 hash_alg );
4466 if( status != PSA_SUCCESS )
4467 return( status );
4468 if( hkdf->block_number != 1 )
4469 {
4470 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4471 hkdf->output_block,
4472 hash_length );
4473 if( status != PSA_SUCCESS )
4474 return( status );
4475 }
4476 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4477 hkdf->info,
4478 hkdf->info_length );
4479 if( status != PSA_SUCCESS )
4480 return( status );
4481 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4482 &hkdf->block_number, 1 );
4483 if( status != PSA_SUCCESS )
4484 return( status );
4485 status = psa_hmac_finish_internal( &hkdf->hmac,
4486 hkdf->output_block,
4487 sizeof( hkdf->output_block ) );
4488 if( status != PSA_SUCCESS )
4489 return( status );
4490 }
4491
4492 return( PSA_SUCCESS );
4493}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004494
Janos Follath7742fee2019-06-17 12:58:10 +01004495static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4496 psa_tls12_prf_key_derivation_t *tls12_prf,
4497 psa_algorithm_t alg )
4498{
4499 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4500 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004501 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4502 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004503
Janos Follath7742fee2019-06-17 12:58:10 +01004504 /* We can't be wanting more output after block 0xff, otherwise
4505 * the capacity check in psa_key_derivation_output_bytes() would have
4506 * prevented this call. It could happen only if the operation
4507 * object was corrupted or if this function is called directly
4508 * inside the library. */
4509 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004510 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004511
4512 /* We need a new block */
4513 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004514 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004515
4516 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4517 *
4518 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4519 *
4520 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4521 * HMAC_hash(secret, A(2) + seed) +
4522 * HMAC_hash(secret, A(3) + seed) + ...
4523 *
4524 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004525 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004526 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004527 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004528 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004529 * is currently extracted as `output_block` and where i is
4530 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004531 */
4532
Janos Follathea29bfb2019-06-19 12:21:20 +01004533 /* Save the hash context before using it, to preserve the hash state with
4534 * only the inner padding in it. We need this, because inner padding depends
4535 * on the key (secret in the RFC's terminology). */
4536 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4537 if( status != PSA_SUCCESS )
4538 goto cleanup;
4539
4540 /* Calculate A(i) where i = tls12_prf->block_number. */
4541 if( tls12_prf->block_number == 1 )
4542 {
4543 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4544 * the variable seed and in this instance means it in the context of the
4545 * P_hash function, where seed = label + seed.) */
4546 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4547 tls12_prf->label, tls12_prf->label_length );
4548 if( status != PSA_SUCCESS )
4549 goto cleanup;
4550 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4551 tls12_prf->seed, tls12_prf->seed_length );
4552 if( status != PSA_SUCCESS )
4553 goto cleanup;
4554 }
4555 else
4556 {
4557 /* A(i) = HMAC_hash(secret, A(i-1)) */
4558 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4559 tls12_prf->Ai, hash_length );
4560 if( status != PSA_SUCCESS )
4561 goto cleanup;
4562 }
4563
4564 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4565 tls12_prf->Ai, hash_length );
4566 if( status != PSA_SUCCESS )
4567 goto cleanup;
4568 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4569 if( status != PSA_SUCCESS )
4570 goto cleanup;
4571
4572 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4573 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4574 tls12_prf->Ai, hash_length );
4575 if( status != PSA_SUCCESS )
4576 goto cleanup;
4577 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4578 tls12_prf->label, tls12_prf->label_length );
4579 if( status != PSA_SUCCESS )
4580 goto cleanup;
4581 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4582 tls12_prf->seed, tls12_prf->seed_length );
4583 if( status != PSA_SUCCESS )
4584 goto cleanup;
4585 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4586 tls12_prf->output_block, hash_length );
4587 if( status != PSA_SUCCESS )
4588 goto cleanup;
4589 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4590 if( status != PSA_SUCCESS )
4591 goto cleanup;
4592
Janos Follath7742fee2019-06-17 12:58:10 +01004593
4594cleanup:
4595
Janos Follathea29bfb2019-06-19 12:21:20 +01004596 cleanup_status = psa_hash_abort( &backup );
4597 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4598 status = cleanup_status;
4599
4600 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004601}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004602
Janos Follath844eb0e2019-06-19 12:10:49 +01004603static psa_status_t psa_key_derivation_tls12_prf_read(
4604 psa_tls12_prf_key_derivation_t *tls12_prf,
4605 psa_algorithm_t alg,
4606 uint8_t *output,
4607 size_t output_length )
4608{
4609 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4610 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4611 psa_status_t status;
4612 uint8_t offset, length;
4613
4614 while( output_length != 0 )
4615 {
4616 /* Check if we have fully processed the current block. */
4617 if( tls12_prf->left_in_block == 0 )
4618 {
4619 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4620 alg );
4621 if( status != PSA_SUCCESS )
4622 return( status );
4623
4624 continue;
4625 }
4626
4627 if( tls12_prf->left_in_block > output_length )
4628 length = (uint8_t) output_length;
4629 else
4630 length = tls12_prf->left_in_block;
4631
4632 offset = hash_length - tls12_prf->left_in_block;
4633 memcpy( output, tls12_prf->output_block + offset, length );
4634 output += length;
4635 output_length -= length;
4636 tls12_prf->left_in_block -= length;
4637 }
4638
4639 return( PSA_SUCCESS );
4640}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004641#endif /* MBEDTLS_MD_C */
4642
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004643psa_status_t psa_key_derivation_output_bytes(
4644 psa_key_derivation_operation_t *operation,
4645 uint8_t *output,
4646 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004647{
4648 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004650
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004652 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004654 return PSA_ERROR_BAD_STATE;
4655 }
4656
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004658 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004661 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004662 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663 goto exit;
4664 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004666 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004668 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4670 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004672 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004673 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004676
Gilles Peskinebef7f142018-07-12 17:22:21 +02004677#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004678 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004679 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004680 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004682 output, output_length );
4683 }
Janos Follathadbec812019-06-14 11:05:39 +01004684 else
Janos Follathadbec812019-06-14 11:05:39 +01004685 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004686 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004687 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004689 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004690 output_length );
4691 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004692 else
4693#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004694 {
4695 return( PSA_ERROR_BAD_STATE );
4696 }
4697
4698exit:
4699 if( status != PSA_SUCCESS )
4700 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004701 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702 * This allows us to differentiate between exhausted operations and
4703 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4704 * operations. */
4705 psa_algorithm_t alg = operation->alg;
4706 psa_key_derivation_abort( operation );
4707 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708 memset( output, '!', output_length );
4709 }
4710 return( status );
4711}
4712
Gilles Peskine08542d82018-07-19 17:05:42 +02004713#if defined(MBEDTLS_DES_C)
4714static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4715{
4716 if( data_size >= 8 )
4717 mbedtls_des_key_set_parity( data );
4718 if( data_size >= 16 )
4719 mbedtls_des_key_set_parity( data + 8 );
4720 if( data_size >= 24 )
4721 mbedtls_des_key_set_parity( data + 16 );
4722}
4723#endif /* MBEDTLS_DES_C */
4724
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004725static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004726 psa_key_slot_t *slot,
4727 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729{
4730 uint8_t *data = NULL;
4731 size_t bytes = PSA_BITS_TO_BYTES( bits );
4732 psa_status_t status;
4733
Gilles Peskine8e338702019-07-30 20:06:31 +02004734 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 return( PSA_ERROR_INVALID_ARGUMENT );
4736 if( bits % 8 != 0 )
4737 return( PSA_ERROR_INVALID_ARGUMENT );
4738 data = mbedtls_calloc( 1, bytes );
4739 if( data == NULL )
4740 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4741
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 if( status != PSA_SUCCESS )
4744 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004745#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004746 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004747 psa_des_set_key_parity( data, bytes );
4748#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004749 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750
4751exit:
4752 mbedtls_free( data );
4753 return( status );
4754}
4755
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004756psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004758 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004759{
4760 psa_status_t status;
4761 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004762 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004763
4764 /* Reject any attempt to create a zero-length key so that we don't
4765 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4766 if( psa_get_key_bits( attributes ) == 0 )
4767 return( PSA_ERROR_INVALID_ARGUMENT );
4768
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004769 if( ! operation->can_output_key )
4770 return( PSA_ERROR_NOT_PERMITTED );
4771
Gilles Peskinedf179142019-07-15 22:02:14 +02004772 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4773 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004774#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4775 if( driver != NULL )
4776 {
4777 /* Deriving a key in a secure element is not implemented yet. */
4778 status = PSA_ERROR_NOT_SUPPORTED;
4779 }
4780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004781 if( status == PSA_SUCCESS )
4782 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004783 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004784 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004785 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004786 }
4787 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004788 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 if( status != PSA_SUCCESS )
4790 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004791 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004792 *handle = 0;
4793 }
4794 return( status );
4795}
4796
Gilles Peskineeab56e42018-07-12 17:12:33 +02004797
4798
4799/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004800/* Key derivation */
4801/****************************************************************/
4802
Gilles Peskine969c5d62019-01-16 15:53:06 +01004803static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004805 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004806{
Janos Follath5fe19732019-06-20 15:09:30 +01004807 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4808 * initialisers for this union leave some bytes unspecified.) */
4809 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4810
Gilles Peskine969c5d62019-01-16 15:53:06 +01004811 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004813 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4814 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4815 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004816 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004817 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004818 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4819 if( hash_size == 0 )
4820 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4822 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004823 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824 {
4825 return( PSA_ERROR_NOT_SUPPORTED );
4826 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004828 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 }
4830#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004832 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004833}
4834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004836 psa_algorithm_t alg )
4837{
4838 psa_status_t status;
4839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004841 return( PSA_ERROR_BAD_STATE );
4842
Gilles Peskine6843c292019-01-18 16:44:49 +01004843 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4844 return( PSA_ERROR_INVALID_ARGUMENT );
4845 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004846 {
4847 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004849 }
4850 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4851 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004854 else
4855 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856
4857 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004859 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004860}
4861
4862#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004863static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004864 psa_algorithm_t hash_alg,
4865 psa_key_derivation_step_t step,
4866 const uint8_t *data,
4867 size_t data_length )
4868{
4869 psa_status_t status;
4870 switch( step )
4871 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004872 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004873 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004874 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004875 status = psa_hmac_setup_internal( &hkdf->hmac,
4876 data, data_length,
4877 hash_alg );
4878 if( status != PSA_SUCCESS )
4879 return( status );
4880 hkdf->state = HKDF_STATE_STARTED;
4881 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004882 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883 /* If no salt was provided, use an empty salt. */
4884 if( hkdf->state == HKDF_STATE_INIT )
4885 {
4886 status = psa_hmac_setup_internal( &hkdf->hmac,
4887 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004888 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 if( status != PSA_SUCCESS )
4890 return( status );
4891 hkdf->state = HKDF_STATE_STARTED;
4892 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004893 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004894 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004895 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4896 data, data_length );
4897 if( status != PSA_SUCCESS )
4898 return( status );
4899 status = psa_hmac_finish_internal( &hkdf->hmac,
4900 hkdf->prk,
4901 sizeof( hkdf->prk ) );
4902 if( status != PSA_SUCCESS )
4903 return( status );
4904 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4905 hkdf->block_number = 0;
4906 hkdf->state = HKDF_STATE_KEYED;
4907 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004908 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 if( hkdf->state == HKDF_STATE_OUTPUT )
4910 return( PSA_ERROR_BAD_STATE );
4911 if( hkdf->info_set )
4912 return( PSA_ERROR_BAD_STATE );
4913 hkdf->info_length = data_length;
4914 if( data_length != 0 )
4915 {
4916 hkdf->info = mbedtls_calloc( 1, data_length );
4917 if( hkdf->info == NULL )
4918 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4919 memcpy( hkdf->info, data, data_length );
4920 }
4921 hkdf->info_set = 1;
4922 return( PSA_SUCCESS );
4923 default:
4924 return( PSA_ERROR_INVALID_ARGUMENT );
4925 }
4926}
Janos Follathb03233e2019-06-11 15:30:30 +01004927
Janos Follathf08e2652019-06-13 09:05:41 +01004928static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4929 const uint8_t *data,
4930 size_t data_length )
4931{
4932 if( prf->state != TLS12_PRF_STATE_INIT )
4933 return( PSA_ERROR_BAD_STATE );
4934
Janos Follathd6dce9f2019-07-04 09:11:38 +01004935 if( data_length != 0 )
4936 {
4937 prf->seed = mbedtls_calloc( 1, data_length );
4938 if( prf->seed == NULL )
4939 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004940
Janos Follathd6dce9f2019-07-04 09:11:38 +01004941 memcpy( prf->seed, data, data_length );
4942 prf->seed_length = data_length;
4943 }
Janos Follathf08e2652019-06-13 09:05:41 +01004944
4945 prf->state = TLS12_PRF_STATE_SEED_SET;
4946
4947 return( PSA_SUCCESS );
4948}
4949
Janos Follath81550542019-06-13 14:26:34 +01004950static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4951 psa_algorithm_t hash_alg,
4952 const uint8_t *data,
4953 size_t data_length )
4954{
4955 psa_status_t status;
4956 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4957 return( PSA_ERROR_BAD_STATE );
4958
4959 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4960 if( status != PSA_SUCCESS )
4961 return( status );
4962
4963 prf->state = TLS12_PRF_STATE_KEY_SET;
4964
4965 return( PSA_SUCCESS );
4966}
4967
Janos Follath6660f0e2019-06-17 08:44:03 +01004968static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4969 psa_tls12_prf_key_derivation_t *prf,
4970 psa_algorithm_t hash_alg,
4971 const uint8_t *data,
4972 size_t data_length )
4973{
4974 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004975 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4976 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004977
4978 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4979 return( PSA_ERROR_INVALID_ARGUMENT );
4980
4981 /* Quoting RFC 4279, Section 2:
4982 *
4983 * The premaster secret is formed as follows: if the PSK is N octets
4984 * long, concatenate a uint16 with the value N, N zero octets, a second
4985 * uint16 with the value N, and the PSK itself.
4986 */
4987
Janos Follath40e13932019-06-26 13:22:29 +01004988 *cur++ = ( data_length >> 8 ) & 0xff;
4989 *cur++ = ( data_length >> 0 ) & 0xff;
4990 memset( cur, 0, data_length );
4991 cur += data_length;
4992 *cur++ = pms[0];
4993 *cur++ = pms[1];
4994 memcpy( cur, data, data_length );
4995 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004996
Janos Follath40e13932019-06-26 13:22:29 +01004997 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01004998
4999 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5000 return( status );
5001}
5002
Janos Follath63028dd2019-06-13 09:15:47 +01005003static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5004 const uint8_t *data,
5005 size_t data_length )
5006{
5007 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5008 return( PSA_ERROR_BAD_STATE );
5009
Janos Follathd6dce9f2019-07-04 09:11:38 +01005010 if( data_length != 0 )
5011 {
5012 prf->label = mbedtls_calloc( 1, data_length );
5013 if( prf->label == NULL )
5014 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005015
Janos Follathd6dce9f2019-07-04 09:11:38 +01005016 memcpy( prf->label, data, data_length );
5017 prf->label_length = data_length;
5018 }
Janos Follath63028dd2019-06-13 09:15:47 +01005019
5020 prf->state = TLS12_PRF_STATE_LABEL_SET;
5021
5022 return( PSA_SUCCESS );
5023}
5024
Janos Follathb03233e2019-06-11 15:30:30 +01005025static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5026 psa_algorithm_t hash_alg,
5027 psa_key_derivation_step_t step,
5028 const uint8_t *data,
5029 size_t data_length )
5030{
Janos Follathb03233e2019-06-11 15:30:30 +01005031 switch( step )
5032 {
Janos Follathf08e2652019-06-13 09:05:41 +01005033 case PSA_KEY_DERIVATION_INPUT_SEED:
5034 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005035 case PSA_KEY_DERIVATION_INPUT_SECRET:
5036 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005037 case PSA_KEY_DERIVATION_INPUT_LABEL:
5038 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005039 default:
5040 return( PSA_ERROR_INVALID_ARGUMENT );
5041 }
5042}
Janos Follath6660f0e2019-06-17 08:44:03 +01005043
5044static psa_status_t psa_tls12_prf_psk_to_ms_input(
5045 psa_tls12_prf_key_derivation_t *prf,
5046 psa_algorithm_t hash_alg,
5047 psa_key_derivation_step_t step,
5048 const uint8_t *data,
5049 size_t data_length )
5050{
5051 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005052 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005053 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5054 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005055 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005056
5057 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5058}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005059#endif /* MBEDTLS_MD_C */
5060
Gilles Peskineb8965192019-09-24 16:21:10 +02005061/** Check whether the given key type is acceptable for the given
5062 * input step of a key derivation.
5063 *
5064 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5065 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5066 * Both secret and non-secret inputs can alternatively have the type
5067 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5068 * that the input was passed as a buffer rather than via a key object.
5069 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005070static int psa_key_derivation_check_input_type(
5071 psa_key_derivation_step_t step,
5072 psa_key_type_t key_type )
5073{
5074 switch( step )
5075 {
5076 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005077 if( key_type == PSA_KEY_TYPE_DERIVE )
5078 return( PSA_SUCCESS );
5079 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005080 return( PSA_SUCCESS );
5081 break;
5082 case PSA_KEY_DERIVATION_INPUT_LABEL:
5083 case PSA_KEY_DERIVATION_INPUT_SALT:
5084 case PSA_KEY_DERIVATION_INPUT_INFO:
5085 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005086 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5087 return( PSA_SUCCESS );
5088 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005089 return( PSA_SUCCESS );
5090 break;
5091 }
5092 return( PSA_ERROR_INVALID_ARGUMENT );
5093}
5094
Janos Follathb80a94e2019-06-12 15:54:46 +01005095static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005097 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005098 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005099 const uint8_t *data,
5100 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005101{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005102 psa_status_t status;
5103 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5104
5105 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005106 if( status != PSA_SUCCESS )
5107 goto exit;
5108
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005110 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005111 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005113 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114 step, data, data_length );
5115 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005116 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117 {
Janos Follathb03233e2019-06-11 15:30:30 +01005118 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5119 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5120 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005121 }
5122 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5123 {
5124 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5125 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5126 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005127 }
5128 else
5129#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132 return( PSA_ERROR_BAD_STATE );
5133 }
5134
Gilles Peskine224b0d62019-09-23 18:13:17 +02005135exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138 return( status );
5139}
5140
Janos Follath51f4a0f2019-06-14 11:35:55 +01005141psa_status_t psa_key_derivation_input_bytes(
5142 psa_key_derivation_operation_t *operation,
5143 psa_key_derivation_step_t step,
5144 const uint8_t *data,
5145 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005146{
Gilles Peskineb8965192019-09-24 16:21:10 +02005147 return( psa_key_derivation_input_internal( operation, step,
5148 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005149 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005150}
5151
Janos Follath51f4a0f2019-06-14 11:35:55 +01005152psa_status_t psa_key_derivation_input_key(
5153 psa_key_derivation_operation_t *operation,
5154 psa_key_derivation_step_t step,
5155 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156{
5157 psa_key_slot_t *slot;
5158 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005159
Gilles Peskine28f8f302019-07-24 13:30:31 +02005160 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005161 PSA_KEY_USAGE_DERIVE,
5162 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005163 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005164 {
5165 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005166 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005167 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005168
5169 /* Passing a key object as a SECRET input unlocks the permission
5170 * to output to a key object. */
5171 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5172 operation->can_output_key = 1;
5173
Janos Follathb80a94e2019-06-12 15:54:46 +01005174 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005175 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005176 slot->data.raw.data,
5177 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005178}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005179
5180
5181
5182/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005183/* Key agreement */
5184/****************************************************************/
5185
Gilles Peskinea05219c2018-11-16 16:02:56 +01005186#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005187static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5188 size_t peer_key_length,
5189 const mbedtls_ecp_keypair *our_key,
5190 uint8_t *shared_secret,
5191 size_t shared_secret_size,
5192 size_t *shared_secret_length )
5193{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005194 mbedtls_ecp_keypair *their_key = NULL;
5195 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005196 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005197 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005198
Jaeden Ameroccdce902019-01-10 11:42:27 +00005199 status = psa_import_ec_public_key(
5200 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5201 peer_key, peer_key_length,
5202 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005203 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005204 goto exit;
5205
Jaeden Amero97271b32019-01-10 19:38:51 +00005206 status = mbedtls_to_psa_error(
5207 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5208 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005209 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005210 status = mbedtls_to_psa_error(
5211 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5212 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005213 goto exit;
5214
Jaeden Amero97271b32019-01-10 19:38:51 +00005215 status = mbedtls_to_psa_error(
5216 mbedtls_ecdh_calc_secret( &ecdh,
5217 shared_secret_length,
5218 shared_secret, shared_secret_size,
5219 mbedtls_ctr_drbg_random,
5220 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005221
5222exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005223 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005224 mbedtls_ecp_keypair_free( their_key );
5225 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005226 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005227}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005228#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005229
Gilles Peskine01d718c2018-09-18 12:01:02 +02005230#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5231
Gilles Peskine0216fe12019-04-11 21:23:21 +02005232static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5233 psa_key_slot_t *private_key,
5234 const uint8_t *peer_key,
5235 size_t peer_key_length,
5236 uint8_t *shared_secret,
5237 size_t shared_secret_size,
5238 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005239{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005240 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005241 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005242#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005243 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005244 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005245 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005246 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5247 private_key->data.ecp,
5248 shared_secret, shared_secret_size,
5249 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005250#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005251 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005252 (void) private_key;
5253 (void) peer_key;
5254 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005255 (void) shared_secret;
5256 (void) shared_secret_size;
5257 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005258 return( PSA_ERROR_NOT_SUPPORTED );
5259 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005260}
5261
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005262/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005263 * to potentially free embedded data structures and wipe confidential data.
5264 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005266 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005267 psa_key_slot_t *private_key,
5268 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005269 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005270{
5271 psa_status_t status;
5272 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5273 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005275
5276 /* Step 1: run the secret agreement algorithm to generate the shared
5277 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005278 status = psa_key_agreement_raw_internal( ka_alg,
5279 private_key,
5280 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005281 shared_secret,
5282 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005283 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005284 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005285 goto exit;
5286
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005287 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005288 * the shared secret. A shared secret is permitted wherever a key
5289 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005290 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005291 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005292 shared_secret,
5293 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005294
Gilles Peskine12313cd2018-06-20 00:20:32 +02005295exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005296 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005297 return( status );
5298}
5299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005301 psa_key_derivation_step_t step,
5302 psa_key_handle_t private_key,
5303 const uint8_t *peer_key,
5304 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005305{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005306 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005307 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005309 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005310 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005311 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005312 if( status != PSA_SUCCESS )
5313 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005315 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005316 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005317 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005319 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005320}
5321
Gilles Peskinebe697d82019-05-16 18:00:41 +02005322psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5323 psa_key_handle_t private_key,
5324 const uint8_t *peer_key,
5325 size_t peer_key_length,
5326 uint8_t *output,
5327 size_t output_size,
5328 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005329{
5330 psa_key_slot_t *slot;
5331 psa_status_t status;
5332
5333 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5334 {
5335 status = PSA_ERROR_INVALID_ARGUMENT;
5336 goto exit;
5337 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005338 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005339 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005340 if( status != PSA_SUCCESS )
5341 goto exit;
5342
5343 status = psa_key_agreement_raw_internal( alg, slot,
5344 peer_key, peer_key_length,
5345 output, output_size,
5346 output_length );
5347
5348exit:
5349 if( status != PSA_SUCCESS )
5350 {
5351 /* If an error happens and is not handled properly, the output
5352 * may be used as a key to protect sensitive data. Arrange for such
5353 * a key to be random, which is likely to result in decryption or
5354 * verification errors. This is better than filling the buffer with
5355 * some constant data such as zeros, which would result in the data
5356 * being protected with a reproducible, easily knowable key.
5357 */
5358 psa_generate_random( output, output_size );
5359 *output_length = output_size;
5360 }
5361 return( status );
5362}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005363
5364
5365/****************************************************************/
5366/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005367/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368
Gilles Peskine4c317f42018-07-12 01:24:09 +02005369psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005370 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005372 int ret;
5373 GUARD_MODULE_INITIALIZED;
5374
Gilles Peskinef181eca2019-08-07 13:49:00 +02005375 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5376 {
5377 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5378 output,
5379 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5380 if( ret != 0 )
5381 return( mbedtls_to_psa_error( ret ) );
5382 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5383 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5384 }
5385
Gilles Peskinee59236f2018-01-27 23:32:46 +01005386 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5387 return( mbedtls_to_psa_error( ret ) );
5388}
5389
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005390#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5391#include "mbedtls/entropy_poll.h"
5392
Gilles Peskine7228da22019-07-15 11:06:15 +02005393psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005394 size_t seed_size )
5395{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005396 if( global_data.initialized )
5397 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005398
5399 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5400 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5401 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5402 return( PSA_ERROR_INVALID_ARGUMENT );
5403
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005404 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005405}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005406#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005407
Gilles Peskinee56e8782019-04-26 17:34:02 +02005408#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5409static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5410 size_t domain_parameters_size,
5411 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005412{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005413 size_t i;
5414 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005415
Gilles Peskinee56e8782019-04-26 17:34:02 +02005416 if( domain_parameters_size == 0 )
5417 {
5418 *exponent = 65537;
5419 return( PSA_SUCCESS );
5420 }
5421
5422 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5423 * support values that fit in a 32-bit integer, which is larger than
5424 * int on just about every platform anyway. */
5425 if( domain_parameters_size > sizeof( acc ) )
5426 return( PSA_ERROR_NOT_SUPPORTED );
5427 for( i = 0; i < domain_parameters_size; i++ )
5428 acc = ( acc << 8 ) | domain_parameters[i];
5429 if( acc > INT_MAX )
5430 return( PSA_ERROR_NOT_SUPPORTED );
5431 *exponent = acc;
5432 return( PSA_SUCCESS );
5433}
5434#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5435
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005436static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 psa_key_slot_t *slot, size_t bits,
5438 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005439{
Gilles Peskine8e338702019-07-30 20:06:31 +02005440 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005441
Gilles Peskinee56e8782019-04-26 17:34:02 +02005442 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005443 return( PSA_ERROR_INVALID_ARGUMENT );
5444
Gilles Peskinee59236f2018-01-27 23:32:46 +01005445 if( key_type_is_raw_bytes( type ) )
5446 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005447 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5449 if( status != PSA_SUCCESS )
5450 return( status );
5451 status = psa_generate_random( slot->data.raw.data,
5452 slot->data.raw.bytes );
5453 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005454 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005455#if defined(MBEDTLS_DES_C)
5456 if( type == PSA_KEY_TYPE_DES )
5457 psa_des_set_key_parity( slot->data.raw.data,
5458 slot->data.raw.bytes );
5459#endif /* MBEDTLS_DES_C */
5460 }
5461 else
5462
5463#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005464 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 {
5466 mbedtls_rsa_context *rsa;
5467 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005468 int exponent;
5469 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005470 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5471 return( PSA_ERROR_NOT_SUPPORTED );
5472 /* Accept only byte-aligned keys, for the same reasons as
5473 * in psa_import_rsa_key(). */
5474 if( bits % 8 != 0 )
5475 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005476 status = psa_read_rsa_exponent( domain_parameters,
5477 domain_parameters_size,
5478 &exponent );
5479 if( status != PSA_SUCCESS )
5480 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5482 if( rsa == NULL )
5483 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5484 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5485 ret = mbedtls_rsa_gen_key( rsa,
5486 mbedtls_ctr_drbg_random,
5487 &global_data.ctr_drbg,
5488 (unsigned int) bits,
5489 exponent );
5490 if( ret != 0 )
5491 {
5492 mbedtls_rsa_free( rsa );
5493 mbedtls_free( rsa );
5494 return( mbedtls_to_psa_error( ret ) );
5495 }
5496 slot->data.rsa = rsa;
5497 }
5498 else
5499#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5500
5501#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005502 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005503 {
5504 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5505 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5506 const mbedtls_ecp_curve_info *curve_info =
5507 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5508 mbedtls_ecp_keypair *ecp;
5509 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005510 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511 return( PSA_ERROR_NOT_SUPPORTED );
5512 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5513 return( PSA_ERROR_NOT_SUPPORTED );
5514 if( curve_info->bit_size != bits )
5515 return( PSA_ERROR_INVALID_ARGUMENT );
5516 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5517 if( ecp == NULL )
5518 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5519 mbedtls_ecp_keypair_init( ecp );
5520 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5521 mbedtls_ctr_drbg_random,
5522 &global_data.ctr_drbg );
5523 if( ret != 0 )
5524 {
5525 mbedtls_ecp_keypair_free( ecp );
5526 mbedtls_free( ecp );
5527 return( mbedtls_to_psa_error( ret ) );
5528 }
5529 slot->data.ecp = ecp;
5530 }
5531 else
5532#endif /* MBEDTLS_ECP_C */
5533
5534 return( PSA_ERROR_NOT_SUPPORTED );
5535
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005536 return( PSA_SUCCESS );
5537}
Darryl Green0c6575a2018-11-07 16:05:30 +00005538
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005539psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005540 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005541{
5542 psa_status_t status;
5543 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005544 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005545
Gilles Peskine0f84d622019-09-12 19:03:13 +02005546 /* Reject any attempt to create a zero-length key so that we don't
5547 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5548 if( psa_get_key_bits( attributes ) == 0 )
5549 return( PSA_ERROR_INVALID_ARGUMENT );
5550
Gilles Peskinedf179142019-07-15 22:02:14 +02005551 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5552 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005553 if( status != PSA_SUCCESS )
5554 goto exit;
5555
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005556#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5557 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005558 {
Gilles Peskine11792082019-08-06 18:36:36 +02005559 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5560 size_t pubkey_length = 0; /* We don't support this feature yet */
5561 if( drv->key_management == NULL ||
5562 drv->key_management->p_generate == NULL )
5563 {
5564 status = PSA_ERROR_NOT_SUPPORTED;
5565 goto exit;
5566 }
5567 status = drv->key_management->p_generate(
5568 psa_get_se_driver_context( driver ),
5569 slot->data.se.slot_number, attributes,
5570 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005571 }
Gilles Peskine11792082019-08-06 18:36:36 +02005572 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005573#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005574 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005575 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005576 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005577 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005578 }
Gilles Peskine11792082019-08-06 18:36:36 +02005579
5580exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005581 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005582 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005583 if( status != PSA_SUCCESS )
5584 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005585 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005586 *handle = 0;
5587 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005588 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005589}
5590
5591
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592
5593/****************************************************************/
5594/* Module setup */
5595/****************************************************************/
5596
Gilles Peskine5e769522018-11-20 21:59:56 +01005597psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5598 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5599 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5600{
5601 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5602 return( PSA_ERROR_BAD_STATE );
5603 global_data.entropy_init = entropy_init;
5604 global_data.entropy_free = entropy_free;
5605 return( PSA_SUCCESS );
5606}
5607
Gilles Peskinee59236f2018-01-27 23:32:46 +01005608void mbedtls_psa_crypto_free( void )
5609{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005610 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005611 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5612 {
5613 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005614 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005615 }
5616 /* Wipe all remaining data, including configuration.
5617 * In particular, this sets all state indicator to the value
5618 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005619 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005620#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005621 /* Unregister all secure element drivers, so that we restart from
5622 * a pristine state. */
5623 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005624#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005625}
5626
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005627#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5628/** Recover a transaction that was interrupted by a power failure.
5629 *
5630 * This function is called during initialization, before psa_crypto_init()
5631 * returns. If this function returns a failure status, the initialization
5632 * fails.
5633 */
5634static psa_status_t psa_crypto_recover_transaction(
5635 const psa_crypto_transaction_t *transaction )
5636{
5637 switch( transaction->unknown.type )
5638 {
5639 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5640 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005641 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005642 * is implemented.
5643 * https://github.com/ARMmbed/mbed-crypto/issues/218
5644 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005645 default:
5646 /* We found an unsupported transaction in the storage.
5647 * We don't know what state the storage is in. Give up. */
5648 return( PSA_ERROR_STORAGE_FAILURE );
5649 }
5650}
5651#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5652
Gilles Peskinee59236f2018-01-27 23:32:46 +01005653psa_status_t psa_crypto_init( void )
5654{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005655 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005656 const unsigned char drbg_seed[] = "PSA";
5657
Gilles Peskinec6b69072018-11-20 21:42:52 +01005658 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005659 if( global_data.initialized != 0 )
5660 return( PSA_SUCCESS );
5661
Gilles Peskine5e769522018-11-20 21:59:56 +01005662 /* Set default configuration if
5663 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5664 if( global_data.entropy_init == NULL )
5665 global_data.entropy_init = mbedtls_entropy_init;
5666 if( global_data.entropy_free == NULL )
5667 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005668
Gilles Peskinec6b69072018-11-20 21:42:52 +01005669 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005670 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005671#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5672 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5673 /* The PSA entropy injection feature depends on using NV seed as an entropy
5674 * source. Add NV seed as an entropy source for PSA entropy injection. */
5675 mbedtls_entropy_add_source( &global_data.entropy,
5676 mbedtls_nv_seed_poll, NULL,
5677 MBEDTLS_ENTROPY_BLOCK_SIZE,
5678 MBEDTLS_ENTROPY_SOURCE_STRONG );
5679#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005680 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005681 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005682 status = mbedtls_to_psa_error(
5683 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5684 mbedtls_entropy_func,
5685 &global_data.entropy,
5686 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5687 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005689 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005690
Gilles Peskine66fb1262018-12-10 16:29:04 +01005691 status = psa_initialize_key_slots( );
5692 if( status != PSA_SUCCESS )
5693 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005694
Gilles Peskined9348f22019-10-01 15:22:29 +02005695#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5696 status = psa_init_all_se_drivers( );
5697 if( status != PSA_SUCCESS )
5698 goto exit;
5699#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5700
Gilles Peskine4b734222019-07-24 15:56:31 +02005701#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005702 status = psa_crypto_load_transaction( );
5703 if( status == PSA_SUCCESS )
5704 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005705 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5706 if( status != PSA_SUCCESS )
5707 goto exit;
5708 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005709 }
5710 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5711 {
5712 /* There's no transaction to complete. It's all good. */
5713 status = PSA_SUCCESS;
5714 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005715#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005716
Gilles Peskinec6b69072018-11-20 21:42:52 +01005717 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005718 global_data.initialized = 1;
5719
5720exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005721 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005722 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005723 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005724}
5725
5726#endif /* MBEDTLS_PSA_CRYPTO_C */