blob: b98a4629d8126eb434e7a44137aaf99d3407f925 [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 );
Janos Follatha13b9052019-11-22 12:48:59 +0000354 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
355 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300356
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 default:
David Saadab4ecc272019-02-14 13:48:10 +0200358 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100359 }
360}
361
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200362
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200363
364
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365/****************************************************************/
366/* Key management */
367/****************************************************************/
368
Gilles Peskine73167e12019-07-12 23:44:37 +0200369#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
370static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
371{
Gilles Peskine8e338702019-07-30 20:06:31 +0200372 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200373}
374#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
375
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100376#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200377static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
378{
379 switch( grpid )
380 {
381 case MBEDTLS_ECP_DP_SECP192R1:
382 return( PSA_ECC_CURVE_SECP192R1 );
383 case MBEDTLS_ECP_DP_SECP224R1:
384 return( PSA_ECC_CURVE_SECP224R1 );
385 case MBEDTLS_ECP_DP_SECP256R1:
386 return( PSA_ECC_CURVE_SECP256R1 );
387 case MBEDTLS_ECP_DP_SECP384R1:
388 return( PSA_ECC_CURVE_SECP384R1 );
389 case MBEDTLS_ECP_DP_SECP521R1:
390 return( PSA_ECC_CURVE_SECP521R1 );
391 case MBEDTLS_ECP_DP_BP256R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
393 case MBEDTLS_ECP_DP_BP384R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
395 case MBEDTLS_ECP_DP_BP512R1:
396 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
397 case MBEDTLS_ECP_DP_CURVE25519:
398 return( PSA_ECC_CURVE_CURVE25519 );
399 case MBEDTLS_ECP_DP_SECP192K1:
400 return( PSA_ECC_CURVE_SECP192K1 );
401 case MBEDTLS_ECP_DP_SECP224K1:
402 return( PSA_ECC_CURVE_SECP224K1 );
403 case MBEDTLS_ECP_DP_SECP256K1:
404 return( PSA_ECC_CURVE_SECP256K1 );
405 case MBEDTLS_ECP_DP_CURVE448:
406 return( PSA_ECC_CURVE_CURVE448 );
407 default:
408 return( 0 );
409 }
410}
411
Gilles Peskine12313cd2018-06-20 00:20:32 +0200412static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
413{
414 switch( curve )
415 {
416 case PSA_ECC_CURVE_SECP192R1:
417 return( MBEDTLS_ECP_DP_SECP192R1 );
418 case PSA_ECC_CURVE_SECP224R1:
419 return( MBEDTLS_ECP_DP_SECP224R1 );
420 case PSA_ECC_CURVE_SECP256R1:
421 return( MBEDTLS_ECP_DP_SECP256R1 );
422 case PSA_ECC_CURVE_SECP384R1:
423 return( MBEDTLS_ECP_DP_SECP384R1 );
424 case PSA_ECC_CURVE_SECP521R1:
425 return( MBEDTLS_ECP_DP_SECP521R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
427 return( MBEDTLS_ECP_DP_BP256R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
429 return( MBEDTLS_ECP_DP_BP384R1 );
430 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
431 return( MBEDTLS_ECP_DP_BP512R1 );
432 case PSA_ECC_CURVE_CURVE25519:
433 return( MBEDTLS_ECP_DP_CURVE25519 );
434 case PSA_ECC_CURVE_SECP192K1:
435 return( MBEDTLS_ECP_DP_SECP192K1 );
436 case PSA_ECC_CURVE_SECP224K1:
437 return( MBEDTLS_ECP_DP_SECP224K1 );
438 case PSA_ECC_CURVE_SECP256K1:
439 return( MBEDTLS_ECP_DP_SECP256K1 );
440 case PSA_ECC_CURVE_CURVE448:
441 return( MBEDTLS_ECP_DP_CURVE448 );
442 default:
443 return( MBEDTLS_ECP_DP_NONE );
444 }
445}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100446#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200447
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200448static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
449 size_t bits,
450 struct raw_data *raw )
451{
452 /* Check that the bit size is acceptable for the key type */
453 switch( type )
454 {
455 case PSA_KEY_TYPE_RAW_DATA:
456#if defined(MBEDTLS_MD_C)
457 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200458#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200459 case PSA_KEY_TYPE_DERIVE:
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_AES_C)
462 case PSA_KEY_TYPE_AES:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_CAMELLIA_C)
468 case PSA_KEY_TYPE_CAMELLIA:
469 if( bits != 128 && bits != 192 && bits != 256 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_DES_C)
474 case PSA_KEY_TYPE_DES:
475 if( bits != 64 && bits != 128 && bits != 192 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
479#if defined(MBEDTLS_ARC4_C)
480 case PSA_KEY_TYPE_ARC4:
481 if( bits < 8 || bits > 2048 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200485#if defined(MBEDTLS_CHACHA20_C)
486 case PSA_KEY_TYPE_CHACHA20:
487 if( bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 default:
492 return( PSA_ERROR_NOT_SUPPORTED );
493 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200494 if( bits % 8 != 0 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496
497 /* Allocate memory for the key */
498 raw->bytes = PSA_BITS_TO_BYTES( bits );
499 raw->data = mbedtls_calloc( 1, raw->bytes );
500 if( raw->data == NULL )
501 {
502 raw->bytes = 0;
503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
504 }
505 return( PSA_SUCCESS );
506}
507
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100509/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
510 * that are not a multiple of 8) well. For example, there is only
511 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
512 * way to return the exact bit size of a key.
513 * To keep things simple, reject non-byte-aligned key sizes. */
514static psa_status_t psa_check_rsa_key_byte_aligned(
515 const mbedtls_rsa_context *rsa )
516{
517 mbedtls_mpi n;
518 psa_status_t status;
519 mbedtls_mpi_init( &n );
520 status = mbedtls_to_psa_error(
521 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
522 if( status == PSA_SUCCESS )
523 {
524 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
525 status = PSA_ERROR_NOT_SUPPORTED;
526 }
527 mbedtls_mpi_free( &n );
528 return( status );
529}
530
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000531static psa_status_t psa_import_rsa_key( psa_key_type_t type,
532 const uint8_t *data,
533 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 mbedtls_rsa_context **p_rsa )
535{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
537 mbedtls_pk_context pk;
538 mbedtls_rsa_context *rsa;
539 size_t bits;
540
541 mbedtls_pk_init( &pk );
542
543 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200544 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 status = mbedtls_to_psa_error(
546 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200547 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 status = mbedtls_to_psa_error(
549 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
550 if( status != PSA_SUCCESS )
551 goto exit;
552
553 /* We have something that the pkparse module recognizes. If it is a
554 * valid RSA key, store it. */
555 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000557 status = PSA_ERROR_INVALID_ARGUMENT;
558 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560
561 rsa = mbedtls_pk_rsa( pk );
562 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
563 * supports non-byte-aligned key sizes, but not well. For example,
564 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
565 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
567 {
568 status = PSA_ERROR_NOT_SUPPORTED;
569 goto exit;
570 }
571 status = psa_check_rsa_key_byte_aligned( rsa );
572
573exit:
574 /* Free the content of the pk object only on error. */
575 if( status != PSA_SUCCESS )
576 {
577 mbedtls_pk_free( &pk );
578 return( status );
579 }
580
581 /* On success, store the content of the object in the RSA context. */
582 *p_rsa = rsa;
583
584 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200585}
586#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
587
Jaeden Ameroccdce902019-01-10 11:42:27 +0000588#if defined(MBEDTLS_ECP_C)
589
590/* Import a public key given as the uncompressed representation defined by SEC1
591 * 2.3.3 as the content of an ECPoint. */
592static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
593 const uint8_t *data,
594 size_t data_length,
595 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200596{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000598 mbedtls_ecp_keypair *ecp = NULL;
599 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
600
601 *p_ecp = NULL;
602 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
603 if( ecp == NULL )
604 return( PSA_ERROR_INSUFFICIENT_MEMORY );
605 mbedtls_ecp_keypair_init( ecp );
606
607 /* Load the group. */
608 status = mbedtls_to_psa_error(
609 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
610 if( status != PSA_SUCCESS )
611 goto exit;
612 /* Load the public value. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
615 data, data_length ) );
616 if( status != PSA_SUCCESS )
617 goto exit;
618
619 /* Check that the point is on the curve. */
620 status = mbedtls_to_psa_error(
621 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
622 if( status != PSA_SUCCESS )
623 goto exit;
624
625 *p_ecp = ecp;
626 return( PSA_SUCCESS );
627
628exit:
629 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200630 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 mbedtls_ecp_keypair_free( ecp );
632 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000636#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637
Gilles Peskinef76aa772018-10-29 19:24:33 +0100638#if defined(MBEDTLS_ECP_C)
639/* Import a private key given as a byte string which is the private value
640 * in big-endian order. */
641static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
642 const uint8_t *data,
643 size_t data_length,
644 mbedtls_ecp_keypair **p_ecp )
645{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100647 mbedtls_ecp_keypair *ecp = NULL;
648 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
649
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200650 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
651 return( PSA_ERROR_INVALID_ARGUMENT );
652
Gilles Peskinef76aa772018-10-29 19:24:33 +0100653 *p_ecp = NULL;
654 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
655 if( ecp == NULL )
656 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000657 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658
659 /* Load the group. */
660 status = mbedtls_to_psa_error(
661 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
662 if( status != PSA_SUCCESS )
663 goto exit;
664 /* Load the secret value. */
665 status = mbedtls_to_psa_error(
666 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Validate the private key. */
670 status = mbedtls_to_psa_error(
671 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Calculate the public key from the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
677 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
678 if( status != PSA_SUCCESS )
679 goto exit;
680
681 *p_ecp = ecp;
682 return( PSA_SUCCESS );
683
684exit:
685 if( ecp != NULL )
686 {
687 mbedtls_ecp_keypair_free( ecp );
688 mbedtls_free( ecp );
689 }
690 return( status );
691}
692#endif /* defined(MBEDTLS_ECP_C) */
693
Gilles Peskineb46bef22019-07-30 21:32:04 +0200694
695/** Return the size of the key in the given slot, in bits.
696 *
697 * \param[in] slot A key slot.
698 *
699 * \return The key size in bits, read from the metadata in the slot.
700 */
701static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
702{
703 return( slot->attr.bits );
704}
705
706/** Calculate the size of the key in the given slot, in bits.
707 *
708 * \param[in] slot A key slot containing a transparent key.
709 *
710 * \return The key size in bits, calculated from the key data.
711 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200713{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200714 size_t bits = 0; /* return 0 on an empty slot */
715
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
720 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721#endif /* defined(MBEDTLS_RSA_C) */
722#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
724 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200725#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200726
727 /* We know that the size fits in psa_key_bits_t thanks to checks
728 * when the key was created. */
729 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730}
731
Gilles Peskine8e338702019-07-30 20:06:31 +0200732/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100733 * previously. This function assumes that the slot does not contain
734 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100735psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
736 const uint8_t *data,
737 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200739 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740
Gilles Peskine8e338702019-07-30 20:06:31 +0200741 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200744 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745 if( data_length > SIZE_MAX / 8 )
746 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200747 /* Enforce a size limit, and in particular ensure that the bit
748 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 if( bit_size > PSA_MAX_KEY_BITS )
750 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200751 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200752 &slot->data.raw );
753 if( status != PSA_SUCCESS )
754 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200755 if( data_length != 0 )
756 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100757 }
758 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200762 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 data, data_length,
764 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100765 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200766 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100767 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200769 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000770 data, data_length,
771 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 }
773 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100774#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000775#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000779 data, data_length,
780 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 }
782 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000783#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784 {
785 return( PSA_ERROR_NOT_SUPPORTED );
786 }
Darryl Green940d72c2018-07-13 13:18:51 +0100787
Gilles Peskineb46bef22019-07-30 21:32:04 +0200788 if( status == PSA_SUCCESS )
789 {
790 /* Write the actual key size to the slot.
791 * psa_start_key_creation() wrote the size declared by the
792 * caller, which may be 0 (meaning unspecified) or wrong. */
793 slot->attr.bits = psa_calculate_key_bits( slot );
794 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100795 return( status );
796}
797
Gilles Peskinef603c712019-01-19 13:40:11 +0100798/** Calculate the intersection of two algorithm usage policies.
799 *
800 * Return 0 (which allows no operation) on incompatibility.
801 */
802static psa_algorithm_t psa_key_policy_algorithm_intersection(
803 psa_algorithm_t alg1,
804 psa_algorithm_t alg2 )
805{
Gilles Peskine549ea862019-05-22 11:45:59 +0200806 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100807 if( alg1 == alg2 )
808 return( alg1 );
809 /* If the policies are from the same hash-and-sign family, check
810 * if one is a wildcard. If so the other has the specific algorithm. */
811 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
812 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
813 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
814 {
815 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
816 return( alg2 );
817 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
818 return( alg1 );
819 }
820 /* If the policies are incompatible, allow nothing. */
821 return( 0 );
822}
823
Gilles Peskined6f371b2019-05-10 19:33:38 +0200824static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
825 psa_algorithm_t requested_alg )
826{
Gilles Peskine549ea862019-05-22 11:45:59 +0200827 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200828 if( requested_alg == policy_alg )
829 return( 1 );
830 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200831 * and requested_alg is the same hash-and-sign family with any hash,
832 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
834 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
835 {
836 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
837 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
838 }
839 /* If it isn't permitted, it's forbidden. */
840 return( 0 );
841}
842
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100843/** Test whether a policy permits an algorithm.
844 *
845 * The caller must test usage flags separately.
846 */
847static int psa_key_policy_permits( const psa_key_policy_t *policy,
848 psa_algorithm_t alg )
849{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200850 return( psa_key_algorithm_permits( policy->alg, alg ) ||
851 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852}
853
Gilles Peskinef603c712019-01-19 13:40:11 +0100854/** Restrict a key policy based on a constraint.
855 *
856 * \param[in,out] policy The policy to restrict.
857 * \param[in] constraint The policy constraint to apply.
858 *
859 * \retval #PSA_SUCCESS
860 * \c *policy contains the intersection of the original value of
861 * \c *policy and \c *constraint.
862 * \retval #PSA_ERROR_INVALID_ARGUMENT
863 * \c *policy and \c *constraint are incompatible.
864 * \c *policy is unchanged.
865 */
866static psa_status_t psa_restrict_key_policy(
867 psa_key_policy_t *policy,
868 const psa_key_policy_t *constraint )
869{
870 psa_algorithm_t intersection_alg =
871 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200872 psa_algorithm_t intersection_alg2 =
873 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100874 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200876 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
877 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100878 policy->usage &= constraint->usage;
879 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200880 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100881 return( PSA_SUCCESS );
882}
883
Darryl Green06fd18d2018-07-16 11:21:11 +0100884/** Retrieve a slot which must contain a key. The key must have allow all the
885 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
886 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100887static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100888 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100889 psa_key_usage_t usage,
890 psa_algorithm_t alg )
891{
892 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100894
895 *p_slot = NULL;
896
Gilles Peskinec5487a82018-12-03 18:08:14 +0100897 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898 if( status != PSA_SUCCESS )
899 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100900
901 /* Enforce that usage policy for the key slot contains all the flags
902 * required by the usage parameter. There is one exception: public
903 * keys can always be exported, so we treat public key objects as
904 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200907 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100908 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100909
910 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200911 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100912 return( PSA_ERROR_NOT_PERMITTED );
913
914 *p_slot = slot;
915 return( PSA_SUCCESS );
916}
Darryl Green940d72c2018-07-13 13:18:51 +0100917
Gilles Peskine28f8f302019-07-24 13:30:31 +0200918/** Retrieve a slot which must contain a transparent key.
919 *
920 * A transparent key is a key for which the key material is directly
921 * available, as opposed to a key in a secure element.
922 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200923 * This is a temporary function to use instead of psa_get_key_from_slot()
924 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200925 */
926#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
927static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
928 psa_key_slot_t **p_slot,
929 psa_key_usage_t usage,
930 psa_algorithm_t alg )
931{
932 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
933 if( status != PSA_SUCCESS )
934 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200935 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200936 {
937 *p_slot = NULL;
938 return( PSA_ERROR_NOT_SUPPORTED );
939 }
940 return( PSA_SUCCESS );
941}
942#else /* MBEDTLS_PSA_CRYPTO_SE_C */
943/* With no secure element support, all keys are transparent. */
944#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
945 psa_get_key_from_slot( handle, p_slot, usage, alg )
946#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
947
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100948/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100949static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000950{
Gilles Peskine73167e12019-07-12 23:44:37 +0200951#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
952 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000953 {
954 /* No key material to clean. */
955 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200956 else
957#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200958 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000959 {
960 /* No key material to clean. */
961 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200962 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000963 {
964 mbedtls_free( slot->data.raw.data );
965 }
966 else
967#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200968 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000969 {
970 mbedtls_rsa_free( slot->data.rsa );
971 mbedtls_free( slot->data.rsa );
972 }
973 else
974#endif /* defined(MBEDTLS_RSA_C) */
975#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200976 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000977 {
978 mbedtls_ecp_keypair_free( slot->data.ecp );
979 mbedtls_free( slot->data.ecp );
980 }
981 else
982#endif /* defined(MBEDTLS_ECP_C) */
983 {
984 /* Shouldn't happen: the key type is not any type that we
985 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200986 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000987 }
988
989 return( PSA_SUCCESS );
990}
991
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100992/** Completely wipe a slot in memory, including its policy.
993 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100994psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100995{
996 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200997 /* Multipart operations may still be using the key. This is safe
998 * because all multipart operation objects are independent from
999 * the key slot: if they need to access the key after the setup
1000 * phase, they have a copy of the key. Note that this means that
1001 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001002 /* At this point, key material and other type-specific content has
1003 * been wiped. Clear remaining metadata. We can call memset and not
1004 * zeroize because the metadata is not particularly sensitive. */
1005 memset( slot, 0, sizeof( *slot ) );
1006 return( status );
1007}
1008
Gilles Peskinec5487a82018-12-03 18:08:14 +01001009psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001011 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001012 psa_status_t status; /* status of the last operation */
1013 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1015 psa_se_drv_table_entry_t *driver;
1016#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017
Gilles Peskine1841cf42019-10-08 15:48:25 +02001018 if( handle == 0 )
1019 return( PSA_SUCCESS );
1020
Gilles Peskinec5487a82018-12-03 18:08:14 +01001021 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 if( status != PSA_SUCCESS )
1023 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001024
1025#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001026 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001027 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001028 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001029 /* For a key in a secure element, we need to do three things:
1030 * remove the key file in internal storage, destroy the
1031 * key inside the secure element, and update the driver's
1032 * persistent data. Start a transaction that will encompass these
1033 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001034 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 status = psa_crypto_save_transaction( );
1039 if( status != PSA_SUCCESS )
1040 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001041 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001042 /* We should still try to destroy the key in the secure
1043 * element and the key metadata in storage. This is especially
1044 * important if the error is that the storage is full.
1045 * But how to do it exactly without risking an inconsistent
1046 * state after a reset?
1047 * https://github.com/ARMmbed/mbed-crypto/issues/215
1048 */
1049 overall_status = status;
1050 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
1052
Gilles Peskine354f7672019-07-12 23:46:38 +02001053 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001054 if( overall_status == PSA_SUCCESS )
1055 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001056 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001057#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1058
Darryl Greend49a4992018-06-18 17:27:26 +01001059#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001060 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001061 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001062 status = psa_destroy_persistent_key( slot->attr.id );
1063 if( overall_status == PSA_SUCCESS )
1064 overall_status = status;
1065
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001066 /* TODO: other slots may have a copy of the same key. We should
1067 * invalidate them.
1068 * https://github.com/ARMmbed/mbed-crypto/issues/214
1069 */
Darryl Greend49a4992018-06-18 17:27:26 +01001070 }
1071#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001072
Gilles Peskinefc762652019-07-22 19:30:34 +02001073#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1074 if( driver != NULL )
1075 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001076 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001077 if( overall_status == PSA_SUCCESS )
1078 overall_status = status;
1079 status = psa_crypto_stop_transaction( );
1080 if( overall_status == PSA_SUCCESS )
1081 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001082 }
1083#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1084
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001085#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1086exit:
1087#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001088 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001089 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1090 if( overall_status == PSA_SUCCESS )
1091 overall_status = status;
1092 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001093}
1094
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001095void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001096{
Gilles Peskineb699f072019-04-26 16:06:02 +02001097 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001098 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001099}
1100
Gilles Peskineb699f072019-04-26 16:06:02 +02001101psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1102 psa_key_type_t type,
1103 const uint8_t *data,
1104 size_t data_length )
1105{
1106 uint8_t *copy = NULL;
1107
1108 if( data_length != 0 )
1109 {
1110 copy = mbedtls_calloc( 1, data_length );
1111 if( copy == NULL )
1112 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1113 memcpy( copy, data, data_length );
1114 }
1115 /* After this point, this function is guaranteed to succeed, so it
1116 * can start modifying `*attributes`. */
1117
1118 if( attributes->domain_parameters != NULL )
1119 {
1120 mbedtls_free( attributes->domain_parameters );
1121 attributes->domain_parameters = NULL;
1122 attributes->domain_parameters_size = 0;
1123 }
1124
1125 attributes->domain_parameters = copy;
1126 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001127 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001128 return( PSA_SUCCESS );
1129}
1130
1131psa_status_t psa_get_key_domain_parameters(
1132 const psa_key_attributes_t *attributes,
1133 uint8_t *data, size_t data_size, size_t *data_length )
1134{
1135 if( attributes->domain_parameters_size > data_size )
1136 return( PSA_ERROR_BUFFER_TOO_SMALL );
1137 *data_length = attributes->domain_parameters_size;
1138 if( attributes->domain_parameters_size != 0 )
1139 memcpy( data, attributes->domain_parameters,
1140 attributes->domain_parameters_size );
1141 return( PSA_SUCCESS );
1142}
1143
1144#if defined(MBEDTLS_RSA_C)
1145static psa_status_t psa_get_rsa_public_exponent(
1146 const mbedtls_rsa_context *rsa,
1147 psa_key_attributes_t *attributes )
1148{
1149 mbedtls_mpi mpi;
1150 int ret;
1151 uint8_t *buffer = NULL;
1152 size_t buflen;
1153 mbedtls_mpi_init( &mpi );
1154
1155 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1156 if( ret != 0 )
1157 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001158 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1159 {
1160 /* It's the default value, which is reported as an empty string,
1161 * so there's nothing to do. */
1162 goto exit;
1163 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001164
1165 buflen = mbedtls_mpi_size( &mpi );
1166 buffer = mbedtls_calloc( 1, buflen );
1167 if( buffer == NULL )
1168 {
1169 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1170 goto exit;
1171 }
1172 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1173 if( ret != 0 )
1174 goto exit;
1175 attributes->domain_parameters = buffer;
1176 attributes->domain_parameters_size = buflen;
1177
1178exit:
1179 mbedtls_mpi_free( &mpi );
1180 if( ret != 0 )
1181 mbedtls_free( buffer );
1182 return( mbedtls_to_psa_error( ret ) );
1183}
1184#endif /* MBEDTLS_RSA_C */
1185
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001186/** Retrieve all the publicly-accessible attributes of a key.
1187 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001188psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1189 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001190{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001191 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001192 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001193
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194 psa_reset_key_attributes( attributes );
1195
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001196 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001197 if( status != PSA_SUCCESS )
1198 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001199
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001200 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001201 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1202 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1203
1204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205 if( psa_key_slot_is_external( slot ) )
1206 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001208
Gilles Peskine8e338702019-07-30 20:06:31 +02001209 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 {
1211#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001212 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001214#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001215 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001216 * is not yet implemented.
1217 * https://github.com/ARMmbed/mbed-crypto/issues/216
1218 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001219 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001220 break;
1221#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1223 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001224#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001225 default:
1226 /* Nothing else to do. */
1227 break;
1228 }
1229
1230 if( status != PSA_SUCCESS )
1231 psa_reset_key_attributes( attributes );
1232 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233}
1234
Gilles Peskinec8000c02019-08-02 20:15:51 +02001235#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1236psa_status_t psa_get_key_slot_number(
1237 const psa_key_attributes_t *attributes,
1238 psa_key_slot_number_t *slot_number )
1239{
1240 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1241 {
1242 *slot_number = attributes->slot_number;
1243 return( PSA_SUCCESS );
1244 }
1245 else
1246 return( PSA_ERROR_INVALID_ARGUMENT );
1247}
1248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1249
Jaeden Ameroccdce902019-01-10 11:42:27 +00001250#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001251static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1252 unsigned char *buf, size_t size )
1253{
1254 int ret;
1255 unsigned char *c;
1256 size_t len = 0;
1257
1258 c = buf + size;
1259
1260 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1261
1262 return( (int) len );
1263}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001264#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001265
Gilles Peskinef603c712019-01-19 13:40:11 +01001266static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1267 uint8_t *data,
1268 size_t data_size,
1269 size_t *data_length,
1270 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001271{
Gilles Peskine5d309672019-07-12 23:47:28 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1273 const psa_drv_se_t *drv;
1274 psa_drv_se_context_t *drv_context;
1275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1276
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001277 *data_length = 0;
1278
Gilles Peskine8e338702019-07-30 20:06:31 +02001279 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001280 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001281
Gilles Peskinef9168942019-09-12 19:20:29 +02001282 /* Reject a zero-length output buffer now, since this can never be a
1283 * valid key representation. This way we know that data must be a valid
1284 * pointer and we can do things like memset(data, ..., data_size). */
1285 if( data_size == 0 )
1286 return( PSA_ERROR_BUFFER_TOO_SMALL );
1287
Gilles Peskine5d309672019-07-12 23:47:28 +02001288#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001289 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001290 {
1291 psa_drv_se_export_key_t method;
1292 if( drv->key_management == NULL )
1293 return( PSA_ERROR_NOT_SUPPORTED );
1294 method = ( export_public_key ?
1295 drv->key_management->p_export_public :
1296 drv->key_management->p_export );
1297 if( method == NULL )
1298 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001299 return( method( drv_context,
1300 slot->data.se.slot_number,
1301 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001302 }
1303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1304
Gilles Peskine8e338702019-07-30 20:06:31 +02001305 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306 {
1307 if( slot->data.raw.bytes > data_size )
1308 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001309 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1310 memset( data + slot->data.raw.bytes, 0,
1311 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312 *data_length = slot->data.raw.bytes;
1313 return( PSA_SUCCESS );
1314 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001315#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001316 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001317 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001318 psa_status_t status;
1319
Gilles Peskineb46bef22019-07-30 21:32:04 +02001320 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001321 if( bytes > data_size )
1322 return( PSA_ERROR_BUFFER_TOO_SMALL );
1323 status = mbedtls_to_psa_error(
1324 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1325 if( status != PSA_SUCCESS )
1326 return( status );
1327 memset( data + bytes, 0, data_size - bytes );
1328 *data_length = bytes;
1329 return( PSA_SUCCESS );
1330 }
1331#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332 else
Moran Peker17e36e12018-05-02 12:55:20 +03001333 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001334#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001335 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1336 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001337 {
Moran Pekera998bc62018-04-16 18:16:20 +03001338 mbedtls_pk_context pk;
1339 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001340 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001341 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001342#if defined(MBEDTLS_RSA_C)
1343 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001344 pk.pk_info = &mbedtls_rsa_info;
1345 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001346#else
1347 return( PSA_ERROR_NOT_SUPPORTED );
1348#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001349 }
1350 else
1351 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001352#if defined(MBEDTLS_ECP_C)
1353 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001354 pk.pk_info = &mbedtls_eckey_info;
1355 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001356#else
1357 return( PSA_ERROR_NOT_SUPPORTED );
1358#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001359 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001360 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001361 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001362 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001363 }
Moran Peker17e36e12018-05-02 12:55:20 +03001364 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001365 {
Moran Peker17e36e12018-05-02 12:55:20 +03001366 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001367 }
Moran Peker60364322018-04-29 11:34:58 +03001368 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001369 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001370 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001371 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001372 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001373 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1374 * Move the data to the beginning and erase remaining data
1375 * at the original location. */
1376 if( 2 * (size_t) ret <= data_size )
1377 {
1378 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 }
1381 else if( (size_t) ret < data_size )
1382 {
1383 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001384 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001385 }
Moran Pekera998bc62018-04-16 18:16:20 +03001386 *data_length = ret;
1387 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001388 }
1389 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001390#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001391 {
1392 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001393 it is valid for a special-purpose implementation to omit
1394 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001395 return( PSA_ERROR_NOT_SUPPORTED );
1396 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397 }
1398}
1399
Gilles Peskinec5487a82018-12-03 18:08:14 +01001400psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001401 uint8_t *data,
1402 size_t data_size,
1403 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001404{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001405 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001406 psa_status_t status;
1407
1408 /* Set the key to empty now, so that even when there are errors, we always
1409 * set data_length to a value between 0 and data_size. On error, setting
1410 * the key to empty is a good choice because an empty key representation is
1411 * unlikely to be accepted anywhere. */
1412 *data_length = 0;
1413
1414 /* Export requires the EXPORT flag. There is an exception for public keys,
1415 * which don't require any flag, but psa_get_key_from_slot takes
1416 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001417 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001418 if( status != PSA_SUCCESS )
1419 return( status );
1420 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001421 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001422}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423
Gilles Peskinec5487a82018-12-03 18:08:14 +01001424psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001425 uint8_t *data,
1426 size_t data_size,
1427 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001428{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001429 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001430 psa_status_t status;
1431
1432 /* Set the key to empty now, so that even when there are errors, we always
1433 * set data_length to a value between 0 and data_size. On error, setting
1434 * the key to empty is a good choice because an empty key representation is
1435 * unlikely to be accepted anywhere. */
1436 *data_length = 0;
1437
1438 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001439 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001440 if( status != PSA_SUCCESS )
1441 return( status );
1442 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001443 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001444}
1445
Gilles Peskine91e8c332019-08-02 19:19:39 +02001446#if defined(static_assert)
1447static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1448 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001449static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001450 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001451static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452 "One or more key attribute flag is listed as both internal-only and external-only" );
1453#endif
1454
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001455/** Validate that a key policy is internally well-formed.
1456 *
1457 * This function only rejects invalid policies. It does not validate the
1458 * consistency of the policy with respect to other attributes of the key
1459 * such as the key type.
1460 */
1461static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001462{
1463 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001464 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001465 PSA_KEY_USAGE_ENCRYPT |
1466 PSA_KEY_USAGE_DECRYPT |
1467 PSA_KEY_USAGE_SIGN |
1468 PSA_KEY_USAGE_VERIFY |
1469 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1470 return( PSA_ERROR_INVALID_ARGUMENT );
1471
Gilles Peskine4747d192019-04-17 15:05:45 +02001472 return( PSA_SUCCESS );
1473}
1474
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001475/** Validate the internal consistency of key attributes.
1476 *
1477 * This function only rejects invalid attribute values. If does not
1478 * validate the consistency of the attributes with any key data that may
1479 * be involved in the creation of the key.
1480 *
1481 * Call this function early in the key creation process.
1482 *
1483 * \param[in] attributes Key attributes for the new key.
1484 * \param[out] p_drv On any return, the driver for the key, if any.
1485 * NULL for a transparent key.
1486 *
1487 */
1488static psa_status_t psa_validate_key_attributes(
1489 const psa_key_attributes_t *attributes,
1490 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001491{
1492 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001493
1494 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001495 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001496 status = psa_validate_persistent_key_parameters(
1497 attributes->core.lifetime, attributes->core.id,
1498 p_drv, 1 );
1499 if( status != PSA_SUCCESS )
1500 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001501 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502
1503 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001504 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001505 return( status );
1506
1507 /* Refuse to create overly large keys.
1508 * Note that this doesn't trigger on import if the attributes don't
1509 * explicitly specify a size (so psa_get_key_bits returns 0), so
1510 * psa_import_key() needs its own checks. */
1511 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1512 return( PSA_ERROR_NOT_SUPPORTED );
1513
Gilles Peskine91e8c332019-08-02 19:19:39 +02001514 /* Reject invalid flags. These should not be reachable through the API. */
1515 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1516 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1517 return( PSA_ERROR_INVALID_ARGUMENT );
1518
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001519 return( PSA_SUCCESS );
1520}
1521
Gilles Peskine4747d192019-04-17 15:05:45 +02001522/** Prepare a key slot to receive key material.
1523 *
1524 * This function allocates a key slot and sets its metadata.
1525 *
1526 * If this function fails, call psa_fail_key_creation().
1527 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001528 * This function is intended to be used as follows:
1529 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1530 * it with the specified attributes, and assign it a handle.
1531 * -# Populate the slot with the key material.
1532 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1533 * In case of failure at any step, stop the sequence and call
1534 * psa_fail_key_creation().
1535 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001536 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 * \param[in] attributes Key attributes for the new key.
1538 * \param[out] handle On success, a handle for the allocated slot.
1539 * \param[out] p_slot On success, a pointer to the prepared slot.
1540 * \param[out] p_drv On any return, the driver for the key, if any.
1541 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * The key slot is ready to receive key material.
1545 * \return If this function fails, the key slot is an invalid state.
1546 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001547 */
1548static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001549 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 const psa_key_attributes_t *attributes,
1551 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001552 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001553 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001554{
1555 psa_status_t status;
1556 psa_key_slot_t *slot;
1557
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001559 *p_drv = NULL;
1560
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561 status = psa_validate_key_attributes( attributes, p_drv );
1562 if( status != PSA_SUCCESS )
1563 return( status );
1564
Gilles Peskineedbed562019-08-07 18:19:59 +02001565 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001566 if( status != PSA_SUCCESS )
1567 return( status );
1568 slot = *p_slot;
1569
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001570 /* We're storing the declared bit-size of the key. It's up to each
1571 * creation mechanism to verify that this information is correct.
1572 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001573 * an input (generate, device) but not for those where the bit-size
1574 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001575
1576 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001577
Gilles Peskine91e8c332019-08-02 19:19:39 +02001578 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001579 * external-only flags, query `attributes`. Thanks to the check
1580 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001581 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001582 * may have set. */
1583 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001584
Gilles Peskinecbaff462019-07-12 23:46:04 +02001585#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001586 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001587 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001588 * create the key file in internal storage, create the
1589 * key inside the secure element, and update the driver's
1590 * persistent data. Start a transaction that will encompass these
1591 * three actions. */
1592 /* The first thing to do is to find a slot number for the new key.
1593 * We save the slot number in persistent storage as part of the
1594 * transaction data. It will be needed to recover if the power
1595 * fails during the key creation process, to clean up on the secure
1596 * element side after restarting. Obtaining a slot number from the
1597 * secure element driver updates its persistent state, but we do not yet
1598 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001599 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001600 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001601 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001602 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001603 &slot->data.se.slot_number );
1604 if( status != PSA_SUCCESS )
1605 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001606 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001607 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001609 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001610 status = psa_crypto_save_transaction( );
1611 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001612 {
1613 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001614 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001615 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001616 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001617
1618 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1619 {
1620 /* Key registration only makes sense with a secure element. */
1621 return( PSA_ERROR_INVALID_ARGUMENT );
1622 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001623#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1624
Darryl Green0c6575a2018-11-07 16:05:30 +00001625 return( status );
1626}
Gilles Peskine4747d192019-04-17 15:05:45 +02001627
1628/** Finalize the creation of a key once its key material has been set.
1629 *
1630 * This entails writing the key to persistent storage.
1631 *
1632 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001633 * See the documentation of psa_start_key_creation() for the intended use
1634 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001636 * \param[in,out] slot Pointer to the slot with key material.
1637 * \param[in] driver The secure element driver for the key,
1638 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001639 *
1640 * \retval #PSA_SUCCESS
1641 * The key was successfully created. The handle is now valid.
1642 * \return If this function fails, the key slot is an invalid state.
1643 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001645static psa_status_t psa_finish_key_creation(
1646 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001647 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001648{
1649 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001650 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001651 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001652
1653#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001654 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001656#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1657 if( driver != NULL )
1658 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001659 psa_se_key_data_storage_t data;
1660#if defined(static_assert)
1661 static_assert( sizeof( slot->data.se.slot_number ) ==
1662 sizeof( data.slot_number ),
1663 "Slot number size does not match psa_se_key_data_storage_t" );
1664 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1665 "Bit-size size does not match psa_se_key_data_storage_t" );
1666#endif
1667 memcpy( &data.slot_number, &slot->data.se.slot_number,
1668 sizeof( slot->data.se.slot_number ) );
1669 memcpy( &data.bits, &slot->attr.bits,
1670 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001671 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001672 (uint8_t*) &data,
1673 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001674 }
1675 else
1676#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1677 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001678 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001679 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001680 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001681 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1682 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001683 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001684 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1685 status = psa_internal_export_key( slot,
1686 buffer, buffer_size, &length,
1687 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001688 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001689 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001690 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001691
Gilles Peskinef9168942019-09-12 19:20:29 +02001692 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001693 mbedtls_free( buffer );
1694 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001695 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001696#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1697
Gilles Peskinecbaff462019-07-12 23:46:04 +02001698#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001699 /* Finish the transaction for a key creation. This does not
1700 * happen when registering an existing key. Detect this case
1701 * by checking whether a transaction is in progress (actual
1702 * creation of a key in a secure element requires a transaction,
1703 * but registration doesn't use one). */
1704 if( driver != NULL &&
1705 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706 {
1707 status = psa_save_se_persistent_data( driver );
1708 if( status != PSA_SUCCESS )
1709 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001710 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001711 return( status );
1712 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001713 status = psa_crypto_stop_transaction( );
1714 if( status != PSA_SUCCESS )
1715 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001716 }
1717#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1718
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 return( status );
1720}
1721
1722/** Abort the creation of a key.
1723 *
1724 * You may call this function after calling psa_start_key_creation(),
1725 * or after psa_finish_key_creation() fails. In other circumstances, this
1726 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001727 * See the documentation of psa_start_key_creation() for the intended use
1728 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001729 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001730 * \param[in,out] slot Pointer to the slot with key material.
1731 * \param[in] driver The secure element driver for the key,
1732 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001734static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001735 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001736{
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 (void) driver;
1738
Gilles Peskine4747d192019-04-17 15:05:45 +02001739 if( slot == NULL )
1740 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001741
1742#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001743 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001744 * element, and the failure happened later (when saving metadata
1745 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001746 * element.
1747 * https://github.com/ARMmbed/mbed-crypto/issues/217
1748 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001749
Gilles Peskined7729582019-08-05 15:55:54 +02001750 /* Abort the ongoing transaction if any (there may not be one if
1751 * the creation process failed before starting one, or if the
1752 * key creation is a registration of a key in a secure element).
1753 * Earlier functions must already have done what it takes to undo any
1754 * partial creation. All that's left is to update the transaction data
1755 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001756 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001757#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1758
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 psa_wipe_key_slot( slot );
1760}
1761
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001762/** Validate optional attributes during key creation.
1763 *
1764 * Some key attributes are optional during key creation. If they are
1765 * specified in the attributes structure, check that they are consistent
1766 * with the data in the slot.
1767 *
1768 * This function should be called near the end of key creation, after
1769 * the slot in memory is fully populated but before saving persistent data.
1770 */
1771static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001772 const psa_key_slot_t *slot,
1773 const psa_key_attributes_t *attributes )
1774{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001775 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001776 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001777 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001778 return( PSA_ERROR_INVALID_ARGUMENT );
1779 }
1780
1781 if( attributes->domain_parameters_size != 0 )
1782 {
1783#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001784 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001785 {
1786 mbedtls_mpi actual, required;
1787 int ret;
1788 mbedtls_mpi_init( &actual );
1789 mbedtls_mpi_init( &required );
1790 ret = mbedtls_rsa_export( slot->data.rsa,
1791 NULL, NULL, NULL, NULL, &actual );
1792 if( ret != 0 )
1793 goto rsa_exit;
1794 ret = mbedtls_mpi_read_binary( &required,
1795 attributes->domain_parameters,
1796 attributes->domain_parameters_size );
1797 if( ret != 0 )
1798 goto rsa_exit;
1799 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1800 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1801 rsa_exit:
1802 mbedtls_mpi_free( &actual );
1803 mbedtls_mpi_free( &required );
1804 if( ret != 0)
1805 return( mbedtls_to_psa_error( ret ) );
1806 }
1807 else
1808#endif
1809 {
1810 return( PSA_ERROR_INVALID_ARGUMENT );
1811 }
1812 }
1813
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001814 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001816 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001817 return( PSA_ERROR_INVALID_ARGUMENT );
1818 }
1819
1820 return( PSA_SUCCESS );
1821}
1822
Gilles Peskine4747d192019-04-17 15:05:45 +02001823psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001825 size_t data_length,
1826 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001827{
1828 psa_status_t status;
1829 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001830 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831
Gilles Peskine0f84d622019-09-12 19:03:13 +02001832 /* Reject zero-length symmetric keys (including raw data key objects).
1833 * This also rejects any key which might be encoded as an empty string,
1834 * which is never valid. */
1835 if( data_length == 0 )
1836 return( PSA_ERROR_INVALID_ARGUMENT );
1837
Gilles Peskinedf179142019-07-15 22:02:14 +02001838 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1839 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001840 if( status != PSA_SUCCESS )
1841 goto exit;
1842
Gilles Peskine5d309672019-07-12 23:47:28 +02001843#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1844 if( driver != NULL )
1845 {
1846 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001847 /* The driver should set the number of key bits, however in
1848 * case it doesn't, we initialize bits to an invalid value. */
1849 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001850 if( drv->key_management == NULL ||
1851 drv->key_management->p_import == NULL )
1852 {
1853 status = PSA_ERROR_NOT_SUPPORTED;
1854 goto exit;
1855 }
1856 status = drv->key_management->p_import(
1857 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001858 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001859 &bits );
1860 if( status != PSA_SUCCESS )
1861 goto exit;
1862 if( bits > PSA_MAX_KEY_BITS )
1863 {
1864 status = PSA_ERROR_NOT_SUPPORTED;
1865 goto exit;
1866 }
1867 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001868 }
1869 else
1870#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1871 {
1872 status = psa_import_key_into_slot( slot, data, data_length );
1873 if( status != PSA_SUCCESS )
1874 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001875 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001876 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001877 if( status != PSA_SUCCESS )
1878 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879
Gilles Peskine011e4282019-06-26 18:34:38 +02001880 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 if( status != PSA_SUCCESS )
1883 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001884 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001885 *handle = 0;
1886 }
1887 return( status );
1888}
1889
Gilles Peskined7729582019-08-05 15:55:54 +02001890#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1891psa_status_t mbedtls_psa_register_se_key(
1892 const psa_key_attributes_t *attributes )
1893{
1894 psa_status_t status;
1895 psa_key_slot_t *slot = NULL;
1896 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001897 psa_key_handle_t handle = 0;
1898
1899 /* Leaving attributes unspecified is not currently supported.
1900 * It could make sense to query the key type and size from the
1901 * secure element, but not all secure elements support this
1902 * and the driver HAL doesn't currently support it. */
1903 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1904 return( PSA_ERROR_NOT_SUPPORTED );
1905 if( psa_get_key_bits( attributes ) == 0 )
1906 return( PSA_ERROR_NOT_SUPPORTED );
1907
1908 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1909 &handle, &slot, &driver );
1910 if( status != PSA_SUCCESS )
1911 goto exit;
1912
Gilles Peskined7729582019-08-05 15:55:54 +02001913 status = psa_finish_key_creation( slot, driver );
1914
1915exit:
1916 if( status != PSA_SUCCESS )
1917 {
1918 psa_fail_key_creation( slot, driver );
1919 }
1920 /* Registration doesn't keep the key in RAM. */
1921 psa_close_key( handle );
1922 return( status );
1923}
1924#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1925
Gilles Peskinef603c712019-01-19 13:40:11 +01001926static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001927 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001928{
1929 psa_status_t status;
1930 uint8_t *buffer = NULL;
1931 size_t buffer_size = 0;
1932 size_t length;
1933
Gilles Peskine8e338702019-07-30 20:06:31 +02001934 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001935 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001936 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001937 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001938 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001939 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1940 if( status != PSA_SUCCESS )
1941 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001942 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001943 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001944
1945exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001946 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001947 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001948 return( status );
1949}
1950
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001951psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1952 const psa_key_attributes_t *specified_attributes,
1953 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001954{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001955 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001956 psa_key_slot_t *source_slot = NULL;
1957 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001958 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001959 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001960
Gilles Peskine28f8f302019-07-24 13:30:31 +02001961 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001962 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001963 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001964 goto exit;
1965
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001966 status = psa_validate_optional_attributes( source_slot,
1967 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001968 if( status != PSA_SUCCESS )
1969 goto exit;
1970
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001971 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001972 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 if( status != PSA_SUCCESS )
1974 goto exit;
1975
Gilles Peskinedf179142019-07-15 22:02:14 +02001976 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1977 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001978 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 if( status != PSA_SUCCESS )
1980 goto exit;
1981
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001982#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1983 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001984 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001985 /* Copying to a secure element is not implemented yet. */
1986 status = PSA_ERROR_NOT_SUPPORTED;
1987 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001988 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001989#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001990
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001991 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001992 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001993 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001994
Gilles Peskine011e4282019-06-26 18:34:38 +02001995 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001996exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 if( status != PSA_SUCCESS )
1998 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001999 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 *target_handle = 0;
2001 }
2002 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002003}
2004
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002005
2006
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002007/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002008/* Message digests */
2009/****************************************************************/
2010
Gilles Peskineb16841e2019-10-10 20:36:12 +02002011#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002012static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002013{
2014 switch( alg )
2015 {
2016#if defined(MBEDTLS_MD2_C)
2017 case PSA_ALG_MD2:
2018 return( &mbedtls_md2_info );
2019#endif
2020#if defined(MBEDTLS_MD4_C)
2021 case PSA_ALG_MD4:
2022 return( &mbedtls_md4_info );
2023#endif
2024#if defined(MBEDTLS_MD5_C)
2025 case PSA_ALG_MD5:
2026 return( &mbedtls_md5_info );
2027#endif
2028#if defined(MBEDTLS_RIPEMD160_C)
2029 case PSA_ALG_RIPEMD160:
2030 return( &mbedtls_ripemd160_info );
2031#endif
2032#if defined(MBEDTLS_SHA1_C)
2033 case PSA_ALG_SHA_1:
2034 return( &mbedtls_sha1_info );
2035#endif
2036#if defined(MBEDTLS_SHA256_C)
2037 case PSA_ALG_SHA_224:
2038 return( &mbedtls_sha224_info );
2039 case PSA_ALG_SHA_256:
2040 return( &mbedtls_sha256_info );
2041#endif
2042#if defined(MBEDTLS_SHA512_C)
2043 case PSA_ALG_SHA_384:
2044 return( &mbedtls_sha384_info );
2045 case PSA_ALG_SHA_512:
2046 return( &mbedtls_sha512_info );
2047#endif
2048 default:
2049 return( NULL );
2050 }
2051}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002052#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002053
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002054psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2055{
2056 switch( operation->alg )
2057 {
Gilles Peskine81736312018-06-26 15:04:31 +02002058 case 0:
2059 /* The object has (apparently) been initialized but it is not
2060 * in use. It's ok to call abort on such an object, and there's
2061 * nothing to do. */
2062 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002063#if defined(MBEDTLS_MD2_C)
2064 case PSA_ALG_MD2:
2065 mbedtls_md2_free( &operation->ctx.md2 );
2066 break;
2067#endif
2068#if defined(MBEDTLS_MD4_C)
2069 case PSA_ALG_MD4:
2070 mbedtls_md4_free( &operation->ctx.md4 );
2071 break;
2072#endif
2073#if defined(MBEDTLS_MD5_C)
2074 case PSA_ALG_MD5:
2075 mbedtls_md5_free( &operation->ctx.md5 );
2076 break;
2077#endif
2078#if defined(MBEDTLS_RIPEMD160_C)
2079 case PSA_ALG_RIPEMD160:
2080 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2081 break;
2082#endif
2083#if defined(MBEDTLS_SHA1_C)
2084 case PSA_ALG_SHA_1:
2085 mbedtls_sha1_free( &operation->ctx.sha1 );
2086 break;
2087#endif
2088#if defined(MBEDTLS_SHA256_C)
2089 case PSA_ALG_SHA_224:
2090 case PSA_ALG_SHA_256:
2091 mbedtls_sha256_free( &operation->ctx.sha256 );
2092 break;
2093#endif
2094#if defined(MBEDTLS_SHA512_C)
2095 case PSA_ALG_SHA_384:
2096 case PSA_ALG_SHA_512:
2097 mbedtls_sha512_free( &operation->ctx.sha512 );
2098 break;
2099#endif
2100 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002101 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002102 }
2103 operation->alg = 0;
2104 return( PSA_SUCCESS );
2105}
2106
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002107psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002108 psa_algorithm_t alg )
2109{
2110 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002111
2112 /* A context must be freshly initialized before it can be set up. */
2113 if( operation->alg != 0 )
2114 {
2115 return( PSA_ERROR_BAD_STATE );
2116 }
2117
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002118 switch( alg )
2119 {
2120#if defined(MBEDTLS_MD2_C)
2121 case PSA_ALG_MD2:
2122 mbedtls_md2_init( &operation->ctx.md2 );
2123 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2124 break;
2125#endif
2126#if defined(MBEDTLS_MD4_C)
2127 case PSA_ALG_MD4:
2128 mbedtls_md4_init( &operation->ctx.md4 );
2129 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2130 break;
2131#endif
2132#if defined(MBEDTLS_MD5_C)
2133 case PSA_ALG_MD5:
2134 mbedtls_md5_init( &operation->ctx.md5 );
2135 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2136 break;
2137#endif
2138#if defined(MBEDTLS_RIPEMD160_C)
2139 case PSA_ALG_RIPEMD160:
2140 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2141 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2142 break;
2143#endif
2144#if defined(MBEDTLS_SHA1_C)
2145 case PSA_ALG_SHA_1:
2146 mbedtls_sha1_init( &operation->ctx.sha1 );
2147 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2148 break;
2149#endif
2150#if defined(MBEDTLS_SHA256_C)
2151 case PSA_ALG_SHA_224:
2152 mbedtls_sha256_init( &operation->ctx.sha256 );
2153 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2154 break;
2155 case PSA_ALG_SHA_256:
2156 mbedtls_sha256_init( &operation->ctx.sha256 );
2157 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_SHA512_C)
2161 case PSA_ALG_SHA_384:
2162 mbedtls_sha512_init( &operation->ctx.sha512 );
2163 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2164 break;
2165 case PSA_ALG_SHA_512:
2166 mbedtls_sha512_init( &operation->ctx.sha512 );
2167 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2168 break;
2169#endif
2170 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002171 return( PSA_ALG_IS_HASH( alg ) ?
2172 PSA_ERROR_NOT_SUPPORTED :
2173 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174 }
2175 if( ret == 0 )
2176 operation->alg = alg;
2177 else
2178 psa_hash_abort( operation );
2179 return( mbedtls_to_psa_error( ret ) );
2180}
2181
2182psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2183 const uint8_t *input,
2184 size_t input_length )
2185{
2186 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002187
2188 /* Don't require hash implementations to behave correctly on a
2189 * zero-length input, which may have an invalid pointer. */
2190 if( input_length == 0 )
2191 return( PSA_SUCCESS );
2192
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 switch( operation->alg )
2194 {
2195#if defined(MBEDTLS_MD2_C)
2196 case PSA_ALG_MD2:
2197 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2198 input, input_length );
2199 break;
2200#endif
2201#if defined(MBEDTLS_MD4_C)
2202 case PSA_ALG_MD4:
2203 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2204 input, input_length );
2205 break;
2206#endif
2207#if defined(MBEDTLS_MD5_C)
2208 case PSA_ALG_MD5:
2209 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2210 input, input_length );
2211 break;
2212#endif
2213#if defined(MBEDTLS_RIPEMD160_C)
2214 case PSA_ALG_RIPEMD160:
2215 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2216 input, input_length );
2217 break;
2218#endif
2219#if defined(MBEDTLS_SHA1_C)
2220 case PSA_ALG_SHA_1:
2221 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2222 input, input_length );
2223 break;
2224#endif
2225#if defined(MBEDTLS_SHA256_C)
2226 case PSA_ALG_SHA_224:
2227 case PSA_ALG_SHA_256:
2228 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2229 input, input_length );
2230 break;
2231#endif
2232#if defined(MBEDTLS_SHA512_C)
2233 case PSA_ALG_SHA_384:
2234 case PSA_ALG_SHA_512:
2235 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2236 input, input_length );
2237 break;
2238#endif
2239 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002240 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002242
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243 if( ret != 0 )
2244 psa_hash_abort( operation );
2245 return( mbedtls_to_psa_error( ret ) );
2246}
2247
2248psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2249 uint8_t *hash,
2250 size_t hash_size,
2251 size_t *hash_length )
2252{
itayzafrir40835d42018-08-02 13:14:17 +03002253 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002255 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256
2257 /* Fill the output buffer with something that isn't a valid hash
2258 * (barring an attack on the hash and deliberately-crafted input),
2259 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002260 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002261 /* If hash_size is 0 then hash may be NULL and then the
2262 * call to memset would have undefined behavior. */
2263 if( hash_size != 0 )
2264 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265
2266 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002267 {
2268 status = PSA_ERROR_BUFFER_TOO_SMALL;
2269 goto exit;
2270 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271
2272 switch( operation->alg )
2273 {
2274#if defined(MBEDTLS_MD2_C)
2275 case PSA_ALG_MD2:
2276 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2277 break;
2278#endif
2279#if defined(MBEDTLS_MD4_C)
2280 case PSA_ALG_MD4:
2281 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2282 break;
2283#endif
2284#if defined(MBEDTLS_MD5_C)
2285 case PSA_ALG_MD5:
2286 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2287 break;
2288#endif
2289#if defined(MBEDTLS_RIPEMD160_C)
2290 case PSA_ALG_RIPEMD160:
2291 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2292 break;
2293#endif
2294#if defined(MBEDTLS_SHA1_C)
2295 case PSA_ALG_SHA_1:
2296 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2297 break;
2298#endif
2299#if defined(MBEDTLS_SHA256_C)
2300 case PSA_ALG_SHA_224:
2301 case PSA_ALG_SHA_256:
2302 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2303 break;
2304#endif
2305#if defined(MBEDTLS_SHA512_C)
2306 case PSA_ALG_SHA_384:
2307 case PSA_ALG_SHA_512:
2308 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2309 break;
2310#endif
2311 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002312 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313 }
itayzafrir40835d42018-08-02 13:14:17 +03002314 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315
itayzafrir40835d42018-08-02 13:14:17 +03002316exit:
2317 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002319 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320 return( psa_hash_abort( operation ) );
2321 }
2322 else
2323 {
2324 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002325 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 }
2327}
2328
Gilles Peskine2d277862018-06-18 15:41:12 +02002329psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2330 const uint8_t *hash,
2331 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332{
2333 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2334 size_t actual_hash_length;
2335 psa_status_t status = psa_hash_finish( operation,
2336 actual_hash, sizeof( actual_hash ),
2337 &actual_hash_length );
2338 if( status != PSA_SUCCESS )
2339 return( status );
2340 if( actual_hash_length != hash_length )
2341 return( PSA_ERROR_INVALID_SIGNATURE );
2342 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2343 return( PSA_ERROR_INVALID_SIGNATURE );
2344 return( PSA_SUCCESS );
2345}
2346
Gilles Peskineeb35d782019-01-22 17:56:16 +01002347psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2348 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002349{
2350 if( target_operation->alg != 0 )
2351 return( PSA_ERROR_BAD_STATE );
2352
2353 switch( source_operation->alg )
2354 {
2355 case 0:
2356 return( PSA_ERROR_BAD_STATE );
2357#if defined(MBEDTLS_MD2_C)
2358 case PSA_ALG_MD2:
2359 mbedtls_md2_clone( &target_operation->ctx.md2,
2360 &source_operation->ctx.md2 );
2361 break;
2362#endif
2363#if defined(MBEDTLS_MD4_C)
2364 case PSA_ALG_MD4:
2365 mbedtls_md4_clone( &target_operation->ctx.md4,
2366 &source_operation->ctx.md4 );
2367 break;
2368#endif
2369#if defined(MBEDTLS_MD5_C)
2370 case PSA_ALG_MD5:
2371 mbedtls_md5_clone( &target_operation->ctx.md5,
2372 &source_operation->ctx.md5 );
2373 break;
2374#endif
2375#if defined(MBEDTLS_RIPEMD160_C)
2376 case PSA_ALG_RIPEMD160:
2377 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2378 &source_operation->ctx.ripemd160 );
2379 break;
2380#endif
2381#if defined(MBEDTLS_SHA1_C)
2382 case PSA_ALG_SHA_1:
2383 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2384 &source_operation->ctx.sha1 );
2385 break;
2386#endif
2387#if defined(MBEDTLS_SHA256_C)
2388 case PSA_ALG_SHA_224:
2389 case PSA_ALG_SHA_256:
2390 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2391 &source_operation->ctx.sha256 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_SHA512_C)
2395 case PSA_ALG_SHA_384:
2396 case PSA_ALG_SHA_512:
2397 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2398 &source_operation->ctx.sha512 );
2399 break;
2400#endif
2401 default:
2402 return( PSA_ERROR_NOT_SUPPORTED );
2403 }
2404
2405 target_operation->alg = source_operation->alg;
2406 return( PSA_SUCCESS );
2407}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408
2409
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411/* MAC */
2412/****************************************************************/
2413
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002414static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415 psa_algorithm_t alg,
2416 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002417 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002418 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002421 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002423 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002424 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002425
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2427 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002428 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002430 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002431 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 mode = MBEDTLS_MODE_STREAM;
2433 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434 case PSA_ALG_CTR:
2435 mode = MBEDTLS_MODE_CTR;
2436 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002437 case PSA_ALG_CFB:
2438 mode = MBEDTLS_MODE_CFB;
2439 break;
2440 case PSA_ALG_OFB:
2441 mode = MBEDTLS_MODE_OFB;
2442 break;
2443 case PSA_ALG_CBC_NO_PADDING:
2444 mode = MBEDTLS_MODE_CBC;
2445 break;
2446 case PSA_ALG_CBC_PKCS7:
2447 mode = MBEDTLS_MODE_CBC;
2448 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002449 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450 mode = MBEDTLS_MODE_CCM;
2451 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002452 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 mode = MBEDTLS_MODE_GCM;
2454 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002455 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2456 mode = MBEDTLS_MODE_CHACHAPOLY;
2457 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458 default:
2459 return( NULL );
2460 }
2461 }
2462 else if( alg == PSA_ALG_CMAC )
2463 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464 else
2465 return( NULL );
2466
2467 switch( key_type )
2468 {
2469 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002470 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002471 break;
2472 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002473 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2474 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002476 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002478 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002479 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2480 * but two-key Triple-DES is functionally three-key Triple-DES
2481 * with K1=K3, so that's how we present it to mbedtls. */
2482 if( key_bits == 128 )
2483 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484 break;
2485 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002486 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487 break;
2488 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002489 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002491 case PSA_KEY_TYPE_CHACHA20:
2492 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2493 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 default:
2495 return( NULL );
2496 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002497 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002498 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499
Jaeden Amero23bbb752018-06-26 14:16:54 +01002500 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2501 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502}
2503
Gilles Peskinea05219c2018-11-16 16:02:56 +01002504#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002505static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002506{
Gilles Peskine2d277862018-06-18 15:41:12 +02002507 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002508 {
2509 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002510 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002511 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002512 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002513 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002514 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002515 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002516 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002517 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002518 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002519 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002520 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002521 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002522 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002523 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002524 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002525 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 return( 128 );
2527 default:
2528 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002529 }
2530}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002531#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002532
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002533/* Initialize the MAC operation structure. Once this function has been
2534 * called, psa_mac_abort can run and will do the right thing. */
2535static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2536 psa_algorithm_t alg )
2537{
2538 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2539
2540 operation->alg = alg;
2541 operation->key_set = 0;
2542 operation->iv_set = 0;
2543 operation->iv_required = 0;
2544 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002545 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002546
2547#if defined(MBEDTLS_CMAC_C)
2548 if( alg == PSA_ALG_CMAC )
2549 {
2550 operation->iv_required = 0;
2551 mbedtls_cipher_init( &operation->ctx.cmac );
2552 status = PSA_SUCCESS;
2553 }
2554 else
2555#endif /* MBEDTLS_CMAC_C */
2556#if defined(MBEDTLS_MD_C)
2557 if( PSA_ALG_IS_HMAC( operation->alg ) )
2558 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002559 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2560 operation->ctx.hmac.hash_ctx.alg = 0;
2561 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562 }
2563 else
2564#endif /* MBEDTLS_MD_C */
2565 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002566 if( ! PSA_ALG_IS_MAC( alg ) )
2567 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568 }
2569
2570 if( status != PSA_SUCCESS )
2571 memset( operation, 0, sizeof( *operation ) );
2572 return( status );
2573}
2574
Gilles Peskine01126fa2018-07-12 17:04:55 +02002575#if defined(MBEDTLS_MD_C)
2576static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2577{
Gilles Peskine3f108122018-12-07 18:14:53 +01002578 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002579 return( psa_hash_abort( &hmac->hash_ctx ) );
2580}
2581#endif /* MBEDTLS_MD_C */
2582
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2584{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002585 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002587 /* The object has (apparently) been initialized but it is not
2588 * in use. It's ok to call abort on such an object, and there's
2589 * nothing to do. */
2590 return( PSA_SUCCESS );
2591 }
2592 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002594 if( operation->alg == PSA_ALG_CMAC )
2595 {
2596 mbedtls_cipher_free( &operation->ctx.cmac );
2597 }
2598 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002601 if( PSA_ALG_IS_HMAC( operation->alg ) )
2602 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002603 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002604 }
2605 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002607 {
2608 /* Sanity check (shouldn't happen: operation->alg should
2609 * always have been initialized to a valid value). */
2610 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 }
Moran Peker41deec42018-04-04 15:43:05 +03002612
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613 operation->alg = 0;
2614 operation->key_set = 0;
2615 operation->iv_set = 0;
2616 operation->iv_required = 0;
2617 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002618 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002619
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002621
2622bad_state:
2623 /* If abort is called on an uninitialized object, we can't trust
2624 * anything. Wipe the object in case it contains confidential data.
2625 * This may result in a memory leak if a pointer gets overwritten,
2626 * but it's too late to do anything about this. */
2627 memset( operation, 0, sizeof( *operation ) );
2628 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629}
2630
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002631#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002633 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002634 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002635 const mbedtls_cipher_info_t *cipher_info )
2636{
2637 int ret;
2638
2639 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002640
2641 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2642 if( ret != 0 )
2643 return( ret );
2644
2645 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2646 slot->data.raw.data,
2647 key_bits );
2648 return( ret );
2649}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002650#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002651
Gilles Peskine248051a2018-06-20 16:09:38 +02002652#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002653static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2654 const uint8_t *key,
2655 size_t key_length,
2656 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002657{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002658 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002659 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002660 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002661 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002662 psa_status_t status;
2663
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002664 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2665 * overflow below. This should never trigger if the hash algorithm
2666 * is implemented correctly. */
2667 /* The size checks against the ipad and opad buffers cannot be written
2668 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2669 * because that triggers -Wlogical-op on GCC 7.3. */
2670 if( block_size > sizeof( ipad ) )
2671 return( PSA_ERROR_NOT_SUPPORTED );
2672 if( block_size > sizeof( hmac->opad ) )
2673 return( PSA_ERROR_NOT_SUPPORTED );
2674 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002675 return( PSA_ERROR_NOT_SUPPORTED );
2676
Gilles Peskined223b522018-06-11 18:12:58 +02002677 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002678 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002679 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002680 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002681 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002682 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002683 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002684 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002685 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002686 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002687 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002688 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689 }
Gilles Peskine96889972018-07-12 17:07:03 +02002690 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2691 * but it is permitted. It is common when HMAC is used in HKDF, for
2692 * example. Don't call `memcpy` in the 0-length because `key` could be
2693 * an invalid pointer which would make the behavior undefined. */
2694 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002695 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002696
Gilles Peskined223b522018-06-11 18:12:58 +02002697 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2698 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002699 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002700 ipad[i] ^= 0x36;
2701 memset( ipad + key_length, 0x36, block_size - key_length );
2702
2703 /* Copy the key material from ipad to opad, flipping the requisite bits,
2704 * and filling the rest of opad with the requisite constant. */
2705 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002706 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2707 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002711 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002712
Gilles Peskine01126fa2018-07-12 17:04:55 +02002713 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002714
2715cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002716 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002717
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718 return( status );
2719}
Gilles Peskine248051a2018-06-20 16:09:38 +02002720#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721
Gilles Peskine89167cb2018-07-08 20:12:23 +02002722static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002723 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002724 psa_algorithm_t alg,
2725 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002728 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002730 psa_key_usage_t usage =
2731 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002732 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002733 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002735 /* A context must be freshly initialized before it can be set up. */
2736 if( operation->alg != 0 )
2737 {
2738 return( PSA_ERROR_BAD_STATE );
2739 }
2740
Gilles Peskined911eb72018-08-14 15:18:45 +02002741 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002742 if( status != PSA_SUCCESS )
2743 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002744 if( is_sign )
2745 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746
Gilles Peskine28f8f302019-07-24 13:30:31 +02002747 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002748 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002749 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002750 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002751
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002753 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002754 {
2755 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002756 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002757 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002758 int ret;
2759 if( cipher_info == NULL )
2760 {
2761 status = PSA_ERROR_NOT_SUPPORTED;
2762 goto exit;
2763 }
2764 operation->mac_size = cipher_info->block_size;
2765 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2766 status = mbedtls_to_psa_error( ret );
2767 }
2768 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002771 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002772 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002773 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002774 if( hash_alg == 0 )
2775 {
2776 status = PSA_ERROR_NOT_SUPPORTED;
2777 goto exit;
2778 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002779
2780 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2781 /* Sanity check. This shouldn't fail on a valid configuration. */
2782 if( operation->mac_size == 0 ||
2783 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2784 {
2785 status = PSA_ERROR_NOT_SUPPORTED;
2786 goto exit;
2787 }
2788
Gilles Peskine8e338702019-07-30 20:06:31 +02002789 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002790 {
2791 status = PSA_ERROR_INVALID_ARGUMENT;
2792 goto exit;
2793 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002794
Gilles Peskine01126fa2018-07-12 17:04:55 +02002795 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2796 slot->data.raw.data,
2797 slot->data.raw.bytes,
2798 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002799 }
2800 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002802 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002803 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002804 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002805 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002806
Gilles Peskined911eb72018-08-14 15:18:45 +02002807 if( truncated == 0 )
2808 {
2809 /* The "normal" case: untruncated algorithm. Nothing to do. */
2810 }
2811 else if( truncated < 4 )
2812 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002813 /* A very short MAC is too short for security since it can be
2814 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2815 * so we make this our minimum, even though 32 bits is still
2816 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002817 status = PSA_ERROR_NOT_SUPPORTED;
2818 }
2819 else if( truncated > operation->mac_size )
2820 {
2821 /* It's impossible to "truncate" to a larger length. */
2822 status = PSA_ERROR_INVALID_ARGUMENT;
2823 }
2824 else
2825 operation->mac_size = truncated;
2826
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002828 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002830 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002832 else
2833 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834 operation->key_set = 1;
2835 }
2836 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837}
2838
Gilles Peskine89167cb2018-07-08 20:12:23 +02002839psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002840 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002841 psa_algorithm_t alg )
2842{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002843 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002844}
2845
2846psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002847 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002848 psa_algorithm_t alg )
2849{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002850 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002851}
2852
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2854 const uint8_t *input,
2855 size_t input_length )
2856{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002857 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002859 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002861 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862 operation->has_input = 1;
2863
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002866 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002867 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2868 input, input_length );
2869 status = mbedtls_to_psa_error( ret );
2870 }
2871 else
2872#endif /* MBEDTLS_CMAC_C */
2873#if defined(MBEDTLS_MD_C)
2874 if( PSA_ALG_IS_HMAC( operation->alg ) )
2875 {
2876 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2877 input_length );
2878 }
2879 else
2880#endif /* MBEDTLS_MD_C */
2881 {
2882 /* This shouldn't happen if `operation` was initialized by
2883 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002884 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002885 }
2886
Gilles Peskinefbfac682018-07-08 20:51:54 +02002887 if( status != PSA_SUCCESS )
2888 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002889 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890}
2891
Gilles Peskine01126fa2018-07-12 17:04:55 +02002892#if defined(MBEDTLS_MD_C)
2893static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2894 uint8_t *mac,
2895 size_t mac_size )
2896{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002897 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002898 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2899 size_t hash_size = 0;
2900 size_t block_size = psa_get_hash_block_size( hash_alg );
2901 psa_status_t status;
2902
Gilles Peskine01126fa2018-07-12 17:04:55 +02002903 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2904 if( status != PSA_SUCCESS )
2905 return( status );
2906 /* From here on, tmp needs to be wiped. */
2907
2908 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2909 if( status != PSA_SUCCESS )
2910 goto exit;
2911
2912 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2913 if( status != PSA_SUCCESS )
2914 goto exit;
2915
2916 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2917 if( status != PSA_SUCCESS )
2918 goto exit;
2919
Gilles Peskined911eb72018-08-14 15:18:45 +02002920 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2921 if( status != PSA_SUCCESS )
2922 goto exit;
2923
2924 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002925
2926exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002927 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002928 return( status );
2929}
2930#endif /* MBEDTLS_MD_C */
2931
mohammad16036df908f2018-04-02 08:34:15 -07002932static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002933 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002934 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002936 if( ! operation->key_set )
2937 return( PSA_ERROR_BAD_STATE );
2938 if( operation->iv_required && ! operation->iv_set )
2939 return( PSA_ERROR_BAD_STATE );
2940
Gilles Peskine8c9def32018-02-08 10:02:12 +01002941 if( mac_size < operation->mac_size )
2942 return( PSA_ERROR_BUFFER_TOO_SMALL );
2943
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002945 if( operation->alg == PSA_ALG_CMAC )
2946 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002947 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2948 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2949 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002950 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002951 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002952 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 else
2955#endif /* MBEDTLS_CMAC_C */
2956#if defined(MBEDTLS_MD_C)
2957 if( PSA_ALG_IS_HMAC( operation->alg ) )
2958 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002959 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002960 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002961 }
2962 else
2963#endif /* MBEDTLS_MD_C */
2964 {
2965 /* This shouldn't happen if `operation` was initialized by
2966 * a setup function. */
2967 return( PSA_ERROR_BAD_STATE );
2968 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969}
2970
Gilles Peskineacd4be32018-07-08 19:56:25 +02002971psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2972 uint8_t *mac,
2973 size_t mac_size,
2974 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002975{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002976 psa_status_t status;
2977
Jaeden Amero252ef282019-02-15 14:05:35 +00002978 if( operation->alg == 0 )
2979 {
2980 return( PSA_ERROR_BAD_STATE );
2981 }
2982
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002983 /* Fill the output buffer with something that isn't a valid mac
2984 * (barring an attack on the mac and deliberately-crafted input),
2985 * in case the caller doesn't check the return status properly. */
2986 *mac_length = mac_size;
2987 /* If mac_size is 0 then mac may be NULL and then the
2988 * call to memset would have undefined behavior. */
2989 if( mac_size != 0 )
2990 memset( mac, '!', mac_size );
2991
Gilles Peskine89167cb2018-07-08 20:12:23 +02002992 if( ! operation->is_sign )
2993 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002994 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002995 }
mohammad16036df908f2018-04-02 08:34:15 -07002996
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002997 status = psa_mac_finish_internal( operation, mac, mac_size );
2998
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002999 if( status == PSA_SUCCESS )
3000 {
3001 status = psa_mac_abort( operation );
3002 if( status == PSA_SUCCESS )
3003 *mac_length = operation->mac_size;
3004 else
3005 memset( mac, '!', mac_size );
3006 }
3007 else
3008 psa_mac_abort( operation );
3009 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003010}
3011
Gilles Peskineacd4be32018-07-08 19:56:25 +02003012psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3013 const uint8_t *mac,
3014 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015{
Gilles Peskine828ed142018-06-18 23:25:51 +02003016 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003017 psa_status_t status;
3018
Jaeden Amero252ef282019-02-15 14:05:35 +00003019 if( operation->alg == 0 )
3020 {
3021 return( PSA_ERROR_BAD_STATE );
3022 }
3023
Gilles Peskine89167cb2018-07-08 20:12:23 +02003024 if( operation->is_sign )
3025 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003026 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003027 }
3028 if( operation->mac_size != mac_length )
3029 {
3030 status = PSA_ERROR_INVALID_SIGNATURE;
3031 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003032 }
mohammad16036df908f2018-04-02 08:34:15 -07003033
3034 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003035 actual_mac, sizeof( actual_mac ) );
3036
3037 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3038 status = PSA_ERROR_INVALID_SIGNATURE;
3039
3040cleanup:
3041 if( status == PSA_SUCCESS )
3042 status = psa_mac_abort( operation );
3043 else
3044 psa_mac_abort( operation );
3045
Gilles Peskine3f108122018-12-07 18:14:53 +01003046 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003047
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003048 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049}
3050
3051
Gilles Peskine20035e32018-02-03 22:44:14 +01003052
Gilles Peskine20035e32018-02-03 22:44:14 +01003053/****************************************************************/
3054/* Asymmetric cryptography */
3055/****************************************************************/
3056
Gilles Peskine2b450e32018-06-27 15:42:46 +02003057#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003058/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003059 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003060static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3061 size_t hash_length,
3062 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003063{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003064 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003065 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003066 *md_alg = mbedtls_md_get_type( md_info );
3067
3068 /* The Mbed TLS RSA module uses an unsigned int for hash length
3069 * parameters. Validate that it fits so that we don't risk an
3070 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003071#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003072 if( hash_length > UINT_MAX )
3073 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003074#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003075
3076#if defined(MBEDTLS_PKCS1_V15)
3077 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3078 * must be correct. */
3079 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3080 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003081 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003082 if( md_info == NULL )
3083 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003084 if( mbedtls_md_get_size( md_info ) != hash_length )
3085 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003086 }
3087#endif /* MBEDTLS_PKCS1_V15 */
3088
3089#if defined(MBEDTLS_PKCS1_V21)
3090 /* PSS requires a hash internally. */
3091 if( PSA_ALG_IS_RSA_PSS( alg ) )
3092 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003093 if( md_info == NULL )
3094 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003095 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003096#endif /* MBEDTLS_PKCS1_V21 */
3097
Gilles Peskine61b91d42018-06-08 16:09:36 +02003098 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003099}
3100
Gilles Peskine2b450e32018-06-27 15:42:46 +02003101static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3102 psa_algorithm_t alg,
3103 const uint8_t *hash,
3104 size_t hash_length,
3105 uint8_t *signature,
3106 size_t signature_size,
3107 size_t *signature_length )
3108{
3109 psa_status_t status;
3110 int ret;
3111 mbedtls_md_type_t md_alg;
3112
3113 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3114 if( status != PSA_SUCCESS )
3115 return( status );
3116
Gilles Peskine630a18a2018-06-29 17:49:35 +02003117 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003118 return( PSA_ERROR_BUFFER_TOO_SMALL );
3119
3120#if defined(MBEDTLS_PKCS1_V15)
3121 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3122 {
3123 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3124 MBEDTLS_MD_NONE );
3125 ret = mbedtls_rsa_pkcs1_sign( rsa,
3126 mbedtls_ctr_drbg_random,
3127 &global_data.ctr_drbg,
3128 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003129 md_alg,
3130 (unsigned int) hash_length,
3131 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003132 signature );
3133 }
3134 else
3135#endif /* MBEDTLS_PKCS1_V15 */
3136#if defined(MBEDTLS_PKCS1_V21)
3137 if( PSA_ALG_IS_RSA_PSS( alg ) )
3138 {
3139 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3140 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3141 mbedtls_ctr_drbg_random,
3142 &global_data.ctr_drbg,
3143 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003144 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003145 (unsigned int) hash_length,
3146 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003147 signature );
3148 }
3149 else
3150#endif /* MBEDTLS_PKCS1_V21 */
3151 {
3152 return( PSA_ERROR_INVALID_ARGUMENT );
3153 }
3154
3155 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003156 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003157 return( mbedtls_to_psa_error( ret ) );
3158}
3159
3160static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3161 psa_algorithm_t alg,
3162 const uint8_t *hash,
3163 size_t hash_length,
3164 const uint8_t *signature,
3165 size_t signature_length )
3166{
3167 psa_status_t status;
3168 int ret;
3169 mbedtls_md_type_t md_alg;
3170
3171 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3172 if( status != PSA_SUCCESS )
3173 return( status );
3174
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003175 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3176 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003177
3178#if defined(MBEDTLS_PKCS1_V15)
3179 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3180 {
3181 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3182 MBEDTLS_MD_NONE );
3183 ret = mbedtls_rsa_pkcs1_verify( rsa,
3184 mbedtls_ctr_drbg_random,
3185 &global_data.ctr_drbg,
3186 MBEDTLS_RSA_PUBLIC,
3187 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003188 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003189 hash,
3190 signature );
3191 }
3192 else
3193#endif /* MBEDTLS_PKCS1_V15 */
3194#if defined(MBEDTLS_PKCS1_V21)
3195 if( PSA_ALG_IS_RSA_PSS( alg ) )
3196 {
3197 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3198 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3199 mbedtls_ctr_drbg_random,
3200 &global_data.ctr_drbg,
3201 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003202 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003203 (unsigned int) hash_length,
3204 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003205 signature );
3206 }
3207 else
3208#endif /* MBEDTLS_PKCS1_V21 */
3209 {
3210 return( PSA_ERROR_INVALID_ARGUMENT );
3211 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003212
3213 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3214 * the rest of the signature is invalid". This has little use in
3215 * practice and PSA doesn't report this distinction. */
3216 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3217 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003218 return( mbedtls_to_psa_error( ret ) );
3219}
3220#endif /* MBEDTLS_RSA_C */
3221
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003222#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003223/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3224 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3225 * (even though these functions don't modify it). */
3226static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3227 psa_algorithm_t alg,
3228 const uint8_t *hash,
3229 size_t hash_length,
3230 uint8_t *signature,
3231 size_t signature_size,
3232 size_t *signature_length )
3233{
3234 int ret;
3235 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003236 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003237 mbedtls_mpi_init( &r );
3238 mbedtls_mpi_init( &s );
3239
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003240 if( signature_size < 2 * curve_bytes )
3241 {
3242 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3243 goto cleanup;
3244 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003245
Gilles Peskinea05219c2018-11-16 16:02:56 +01003246#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003247 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3248 {
3249 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3250 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3251 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003252 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3253 &ecp->d, hash,
3254 hash_length, md_alg,
3255 mbedtls_ctr_drbg_random,
3256 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003257 }
3258 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003259#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003260 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003261 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003262 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3263 hash, hash_length,
3264 mbedtls_ctr_drbg_random,
3265 &global_data.ctr_drbg ) );
3266 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003267
3268 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3269 signature,
3270 curve_bytes ) );
3271 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3272 signature + curve_bytes,
3273 curve_bytes ) );
3274
3275cleanup:
3276 mbedtls_mpi_free( &r );
3277 mbedtls_mpi_free( &s );
3278 if( ret == 0 )
3279 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003280 return( mbedtls_to_psa_error( ret ) );
3281}
3282
3283static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3284 const uint8_t *hash,
3285 size_t hash_length,
3286 const uint8_t *signature,
3287 size_t signature_length )
3288{
3289 int ret;
3290 mbedtls_mpi r, s;
3291 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3292 mbedtls_mpi_init( &r );
3293 mbedtls_mpi_init( &s );
3294
3295 if( signature_length != 2 * curve_bytes )
3296 return( PSA_ERROR_INVALID_SIGNATURE );
3297
3298 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3299 signature,
3300 curve_bytes ) );
3301 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3302 signature + curve_bytes,
3303 curve_bytes ) );
3304
3305 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3306 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003307
3308cleanup:
3309 mbedtls_mpi_free( &r );
3310 mbedtls_mpi_free( &s );
3311 return( mbedtls_to_psa_error( ret ) );
3312}
3313#endif /* MBEDTLS_ECDSA_C */
3314
Gilles Peskinec5487a82018-12-03 18:08:14 +01003315psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003316 psa_algorithm_t alg,
3317 const uint8_t *hash,
3318 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003319 uint8_t *signature,
3320 size_t signature_size,
3321 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003322{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003323 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003324 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003325#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3326 const psa_drv_se_t *drv;
3327 psa_drv_se_context_t *drv_context;
3328#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003329
3330 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003331 /* Immediately reject a zero-length signature buffer. This guarantees
3332 * that signature must be a valid pointer. (On the other hand, the hash
3333 * buffer can in principle be empty since it doesn't actually have
3334 * to be a hash.) */
3335 if( signature_size == 0 )
3336 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003337
Gilles Peskineedc64242019-08-07 21:05:07 +02003338 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003339 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003340 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003341 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003342 {
3343 status = PSA_ERROR_INVALID_ARGUMENT;
3344 goto exit;
3345 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003346
Gilles Peskineedc64242019-08-07 21:05:07 +02003347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3348 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3349 {
3350 if( drv->asymmetric == NULL ||
3351 drv->asymmetric->p_sign == NULL )
3352 {
3353 status = PSA_ERROR_NOT_SUPPORTED;
3354 goto exit;
3355 }
3356 status = drv->asymmetric->p_sign( drv_context,
3357 slot->data.se.slot_number,
3358 alg,
3359 hash, hash_length,
3360 signature, signature_size,
3361 signature_length );
3362 }
3363 else
3364#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003365#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003366 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003367 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003368 status = psa_rsa_sign( slot->data.rsa,
3369 alg,
3370 hash, hash_length,
3371 signature, signature_size,
3372 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003373 }
3374 else
3375#endif /* defined(MBEDTLS_RSA_C) */
3376#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003377 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003378 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003379#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003380 if(
3381#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3382 PSA_ALG_IS_ECDSA( alg )
3383#else
3384 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3385#endif
3386 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003387 status = psa_ecdsa_sign( slot->data.ecp,
3388 alg,
3389 hash, hash_length,
3390 signature, signature_size,
3391 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003392 else
3393#endif /* defined(MBEDTLS_ECDSA_C) */
3394 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003395 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003396 }
itayzafrir5c753392018-05-08 11:18:38 +03003397 }
3398 else
3399#endif /* defined(MBEDTLS_ECP_C) */
3400 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003401 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003402 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003403
3404exit:
3405 /* Fill the unused part of the output buffer (the whole buffer on error,
3406 * the trailing part on success) with something that isn't a valid mac
3407 * (barring an attack on the mac and deliberately-crafted input),
3408 * in case the caller doesn't check the return status properly. */
3409 if( status == PSA_SUCCESS )
3410 memset( signature + *signature_length, '!',
3411 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003412 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003413 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003414 /* If signature_size is 0 then we have nothing to do. We must not call
3415 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003416 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003417}
3418
Gilles Peskinec5487a82018-12-03 18:08:14 +01003419psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003420 psa_algorithm_t alg,
3421 const uint8_t *hash,
3422 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003423 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003424 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003425{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003426 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003427 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003428#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3429 const psa_drv_se_t *drv;
3430 psa_drv_se_context_t *drv_context;
3431#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003432
Gilles Peskineedc64242019-08-07 21:05:07 +02003433 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003434 if( status != PSA_SUCCESS )
3435 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003436
Gilles Peskineedc64242019-08-07 21:05:07 +02003437#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3438 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3439 {
3440 if( drv->asymmetric == NULL ||
3441 drv->asymmetric->p_verify == NULL )
3442 return( PSA_ERROR_NOT_SUPPORTED );
3443 return( drv->asymmetric->p_verify( drv_context,
3444 slot->data.se.slot_number,
3445 alg,
3446 hash, hash_length,
3447 signature, signature_length ) );
3448 }
3449 else
3450#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003451#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003452 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003454 return( psa_rsa_verify( slot->data.rsa,
3455 alg,
3456 hash, hash_length,
3457 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003458 }
3459 else
3460#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003461#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003462 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003463 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003464#if defined(MBEDTLS_ECDSA_C)
3465 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003466 return( psa_ecdsa_verify( slot->data.ecp,
3467 hash, hash_length,
3468 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003469 else
3470#endif /* defined(MBEDTLS_ECDSA_C) */
3471 {
3472 return( PSA_ERROR_INVALID_ARGUMENT );
3473 }
itayzafrir5c753392018-05-08 11:18:38 +03003474 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003475 else
3476#endif /* defined(MBEDTLS_ECP_C) */
3477 {
3478 return( PSA_ERROR_NOT_SUPPORTED );
3479 }
3480}
3481
Gilles Peskine072ac562018-06-30 00:21:29 +02003482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3483static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3484 mbedtls_rsa_context *rsa )
3485{
3486 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3487 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3488 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3489 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3490}
3491#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3492
Gilles Peskinec5487a82018-12-03 18:08:14 +01003493psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003494 psa_algorithm_t alg,
3495 const uint8_t *input,
3496 size_t input_length,
3497 const uint8_t *salt,
3498 size_t salt_length,
3499 uint8_t *output,
3500 size_t output_size,
3501 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003502{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003503 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003504 psa_status_t status;
3505
Darryl Green5cc689a2018-07-24 15:34:10 +01003506 (void) input;
3507 (void) input_length;
3508 (void) salt;
3509 (void) output;
3510 (void) output_size;
3511
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003512 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003513
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003514 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3515 return( PSA_ERROR_INVALID_ARGUMENT );
3516
Gilles Peskine28f8f302019-07-24 13:30:31 +02003517 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003518 if( status != PSA_SUCCESS )
3519 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003520 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3521 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003523
3524#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003525 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526 {
3527 mbedtls_rsa_context *rsa = slot->data.rsa;
3528 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003529 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003530 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003531#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003532 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003533 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003534 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3535 mbedtls_ctr_drbg_random,
3536 &global_data.ctr_drbg,
3537 MBEDTLS_RSA_PUBLIC,
3538 input_length,
3539 input,
3540 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541 }
3542 else
3543#endif /* MBEDTLS_PKCS1_V15 */
3544#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003545 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003547 psa_rsa_oaep_set_padding_mode( alg, rsa );
3548 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3549 mbedtls_ctr_drbg_random,
3550 &global_data.ctr_drbg,
3551 MBEDTLS_RSA_PUBLIC,
3552 salt, salt_length,
3553 input_length,
3554 input,
3555 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003556 }
3557 else
3558#endif /* MBEDTLS_PKCS1_V21 */
3559 {
3560 return( PSA_ERROR_INVALID_ARGUMENT );
3561 }
3562 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003563 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564 return( mbedtls_to_psa_error( ret ) );
3565 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003566 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003567#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568 {
3569 return( PSA_ERROR_NOT_SUPPORTED );
3570 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003571}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572
Gilles Peskinec5487a82018-12-03 18:08:14 +01003573psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003574 psa_algorithm_t alg,
3575 const uint8_t *input,
3576 size_t input_length,
3577 const uint8_t *salt,
3578 size_t salt_length,
3579 uint8_t *output,
3580 size_t output_size,
3581 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003583 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003584 psa_status_t status;
3585
Darryl Green5cc689a2018-07-24 15:34:10 +01003586 (void) input;
3587 (void) input_length;
3588 (void) salt;
3589 (void) output;
3590 (void) output_size;
3591
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003592 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003593
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003594 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3595 return( PSA_ERROR_INVALID_ARGUMENT );
3596
Gilles Peskine28f8f302019-07-24 13:30:31 +02003597 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003598 if( status != PSA_SUCCESS )
3599 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003600 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601 return( PSA_ERROR_INVALID_ARGUMENT );
3602
3603#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003604 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605 {
3606 mbedtls_rsa_context *rsa = slot->data.rsa;
3607 int ret;
3608
Gilles Peskine630a18a2018-06-29 17:49:35 +02003609 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 return( PSA_ERROR_INVALID_ARGUMENT );
3611
3612#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003613 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003614 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003615 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3616 mbedtls_ctr_drbg_random,
3617 &global_data.ctr_drbg,
3618 MBEDTLS_RSA_PRIVATE,
3619 output_length,
3620 input,
3621 output,
3622 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 }
3624 else
3625#endif /* MBEDTLS_PKCS1_V15 */
3626#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003627 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003628 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003629 psa_rsa_oaep_set_padding_mode( alg, rsa );
3630 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3631 mbedtls_ctr_drbg_random,
3632 &global_data.ctr_drbg,
3633 MBEDTLS_RSA_PRIVATE,
3634 salt, salt_length,
3635 output_length,
3636 input,
3637 output,
3638 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 }
3640 else
3641#endif /* MBEDTLS_PKCS1_V21 */
3642 {
3643 return( PSA_ERROR_INVALID_ARGUMENT );
3644 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003645
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646 return( mbedtls_to_psa_error( ret ) );
3647 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003649#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650 {
3651 return( PSA_ERROR_NOT_SUPPORTED );
3652 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003653}
Gilles Peskine20035e32018-02-03 22:44:14 +01003654
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003655
3656
mohammad1603503973b2018-03-12 15:59:30 +02003657/****************************************************************/
3658/* Symmetric cryptography */
3659/****************************************************************/
3660
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003661/* Initialize the cipher operation structure. Once this function has been
3662 * called, psa_cipher_abort can run and will do the right thing. */
3663static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3664 psa_algorithm_t alg )
3665{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003666 if( ! PSA_ALG_IS_CIPHER( alg ) )
3667 {
3668 memset( operation, 0, sizeof( *operation ) );
3669 return( PSA_ERROR_INVALID_ARGUMENT );
3670 }
3671
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003672 operation->alg = alg;
3673 operation->key_set = 0;
3674 operation->iv_set = 0;
3675 operation->iv_required = 1;
3676 operation->iv_size = 0;
3677 operation->block_size = 0;
3678 mbedtls_cipher_init( &operation->ctx.cipher );
3679 return( PSA_SUCCESS );
3680}
3681
Gilles Peskinee553c652018-06-04 16:22:46 +02003682static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003683 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003684 psa_algorithm_t alg,
3685 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003686{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003687 int ret = 0;
3688 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003689 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003690 size_t key_bits;
3691 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003692 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3693 PSA_KEY_USAGE_ENCRYPT :
3694 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003695
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003696 /* A context must be freshly initialized before it can be set up. */
3697 if( operation->alg != 0 )
3698 {
3699 return( PSA_ERROR_BAD_STATE );
3700 }
3701
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003702 status = psa_cipher_init( operation, alg );
3703 if( status != PSA_SUCCESS )
3704 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003705
Gilles Peskine28f8f302019-07-24 13:30:31 +02003706 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003707 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003708 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003709 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003710
Gilles Peskine8e338702019-07-30 20:06:31 +02003711 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003712 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003713 {
3714 status = PSA_ERROR_NOT_SUPPORTED;
3715 goto exit;
3716 }
mohammad1603503973b2018-03-12 15:59:30 +02003717
mohammad1603503973b2018-03-12 15:59:30 +02003718 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003719 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003720 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003721
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003722#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003723 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003724 {
3725 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003726 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003727 memcpy( keys, slot->data.raw.data, 16 );
3728 memcpy( keys + 16, slot->data.raw.data, 8 );
3729 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3730 keys,
3731 192, cipher_operation );
3732 }
3733 else
3734#endif
3735 {
3736 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3737 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003738 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003739 }
Moran Peker41deec42018-04-04 15:43:05 +03003740 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003741 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003742
mohammad16038481e742018-03-18 13:57:31 +02003743#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003744 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003745 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003746 case PSA_ALG_CBC_NO_PADDING:
3747 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3748 MBEDTLS_PADDING_NONE );
3749 break;
3750 case PSA_ALG_CBC_PKCS7:
3751 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3752 MBEDTLS_PADDING_PKCS7 );
3753 break;
3754 default:
3755 /* The algorithm doesn't involve padding. */
3756 ret = 0;
3757 break;
3758 }
3759 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003760 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003761#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3762
mohammad1603503973b2018-03-12 15:59:30 +02003763 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003764 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003765 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003766 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003767 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003768 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003769 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003770#if defined(MBEDTLS_CHACHA20_C)
3771 else
3772 if( alg == PSA_ALG_CHACHA20 )
3773 operation->iv_size = 12;
3774#endif
mohammad1603503973b2018-03-12 15:59:30 +02003775
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003776exit:
3777 if( status == 0 )
3778 status = mbedtls_to_psa_error( ret );
3779 if( status != 0 )
3780 psa_cipher_abort( operation );
3781 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003782}
3783
Gilles Peskinefe119512018-07-08 21:39:34 +02003784psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003785 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003786 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003787{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003788 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003789}
3790
Gilles Peskinefe119512018-07-08 21:39:34 +02003791psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003792 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003793 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003794{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003795 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003796}
3797
Gilles Peskinefe119512018-07-08 21:39:34 +02003798psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003799 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003800 size_t iv_size,
3801 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003802{
itayzafrir534bd7c2018-08-02 13:56:32 +03003803 psa_status_t status;
3804 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003805 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003806 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003807 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003808 }
Moran Peker41deec42018-04-04 15:43:05 +03003809 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003810 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003811 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003812 goto exit;
3813 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003814 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3815 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003816 if( ret != 0 )
3817 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003818 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003819 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003820 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003821
mohammad16038481e742018-03-18 13:57:31 +02003822 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003823 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003824
Moran Peker395db872018-05-31 14:07:14 +03003825exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003826 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003827 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003828 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003829}
3830
Gilles Peskinefe119512018-07-08 21:39:34 +02003831psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003832 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003833 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003834{
itayzafrir534bd7c2018-08-02 13:56:32 +03003835 psa_status_t status;
3836 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003837 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003838 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003839 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003840 }
Moran Pekera28258c2018-05-29 16:25:04 +03003841 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003842 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003843 status = PSA_ERROR_INVALID_ARGUMENT;
3844 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003845 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003846 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3847 status = mbedtls_to_psa_error( ret );
3848exit:
3849 if( status == PSA_SUCCESS )
3850 operation->iv_set = 1;
3851 else
mohammad1603503973b2018-03-12 15:59:30 +02003852 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003853 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003854}
3855
Gilles Peskinee553c652018-06-04 16:22:46 +02003856psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3857 const uint8_t *input,
3858 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003859 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003860 size_t output_size,
3861 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003862{
itayzafrir534bd7c2018-08-02 13:56:32 +03003863 psa_status_t status;
3864 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003865 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003866
3867 if( operation->alg == 0 )
3868 {
3869 return( PSA_ERROR_BAD_STATE );
3870 }
3871
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003872 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003873 {
3874 /* Take the unprocessed partial block left over from previous
3875 * update calls, if any, plus the input to this call. Remove
3876 * the last partial block, if any. You get the data that will be
3877 * output in this call. */
3878 expected_output_size =
3879 ( operation->ctx.cipher.unprocessed_len + input_length )
3880 / operation->block_size * operation->block_size;
3881 }
3882 else
3883 {
3884 expected_output_size = input_length;
3885 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003886
Gilles Peskine89d789c2018-06-04 17:17:16 +02003887 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003888 {
3889 status = PSA_ERROR_BUFFER_TOO_SMALL;
3890 goto exit;
3891 }
mohammad160382759612018-03-12 18:16:40 +02003892
mohammad1603503973b2018-03-12 15:59:30 +02003893 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003894 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 status = mbedtls_to_psa_error( ret );
3896exit:
3897 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003898 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003900}
3901
Gilles Peskinee553c652018-06-04 16:22:46 +02003902psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3903 uint8_t *output,
3904 size_t output_size,
3905 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003906{
David Saadab4ecc272019-02-14 13:48:10 +02003907 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003908 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003909 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003910
mohammad1603503973b2018-03-12 15:59:30 +02003911 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003912 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003913 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003914 }
3915 if( operation->iv_required && ! operation->iv_set )
3916 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003917 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003918 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003919
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003920 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003921 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3922 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003923 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003924 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003925 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003926 }
3927
Janos Follath315b51c2018-07-09 16:04:51 +01003928 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3929 temp_output_buffer,
3930 output_length );
3931 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003932 {
Janos Follath315b51c2018-07-09 16:04:51 +01003933 status = mbedtls_to_psa_error( cipher_ret );
3934 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003935 }
Janos Follath315b51c2018-07-09 16:04:51 +01003936
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003937 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003938 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003939 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003940 memcpy( output, temp_output_buffer, *output_length );
3941 else
3942 {
Janos Follath315b51c2018-07-09 16:04:51 +01003943 status = PSA_ERROR_BUFFER_TOO_SMALL;
3944 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003945 }
mohammad1603503973b2018-03-12 15:59:30 +02003946
Gilles Peskine3f108122018-12-07 18:14:53 +01003947 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003948 status = psa_cipher_abort( operation );
3949
3950 return( status );
3951
3952error:
3953
3954 *output_length = 0;
3955
Gilles Peskine3f108122018-12-07 18:14:53 +01003956 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003957 (void) psa_cipher_abort( operation );
3958
3959 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003960}
3961
Gilles Peskinee553c652018-06-04 16:22:46 +02003962psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3963{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003964 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003965 {
3966 /* The object has (apparently) been initialized but it is not
3967 * in use. It's ok to call abort on such an object, and there's
3968 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003969 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003970 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003971
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003972 /* Sanity check (shouldn't happen: operation->alg should
3973 * always have been initialized to a valid value). */
3974 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3975 return( PSA_ERROR_BAD_STATE );
3976
mohammad1603503973b2018-03-12 15:59:30 +02003977 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003978
Moran Peker41deec42018-04-04 15:43:05 +03003979 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003980 operation->key_set = 0;
3981 operation->iv_set = 0;
3982 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003983 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003984 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003985
Moran Peker395db872018-05-31 14:07:14 +03003986 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003987}
3988
Gilles Peskinea0655c32018-04-30 17:06:50 +02003989
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003990
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003991
mohammad16035955c982018-04-26 00:53:03 +03003992/****************************************************************/
3993/* AEAD */
3994/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003995
Gilles Peskineedf9a652018-08-17 18:11:56 +02003996typedef struct
3997{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003998 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003999 const mbedtls_cipher_info_t *cipher_info;
4000 union
4001 {
4002#if defined(MBEDTLS_CCM_C)
4003 mbedtls_ccm_context ccm;
4004#endif /* MBEDTLS_CCM_C */
4005#if defined(MBEDTLS_GCM_C)
4006 mbedtls_gcm_context gcm;
4007#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004008#if defined(MBEDTLS_CHACHAPOLY_C)
4009 mbedtls_chachapoly_context chachapoly;
4010#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004011 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004012 psa_algorithm_t core_alg;
4013 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004014 uint8_t tag_length;
4015} aead_operation_t;
4016
Gilles Peskine30a9e412019-01-14 18:36:12 +01004017static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004018{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004019 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004020 {
4021#if defined(MBEDTLS_CCM_C)
4022 case PSA_ALG_CCM:
4023 mbedtls_ccm_free( &operation->ctx.ccm );
4024 break;
4025#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004026#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004027 case PSA_ALG_GCM:
4028 mbedtls_gcm_free( &operation->ctx.gcm );
4029 break;
4030#endif /* MBEDTLS_GCM_C */
4031 }
4032}
4033
4034static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004035 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004036 psa_key_usage_t usage,
4037 psa_algorithm_t alg )
4038{
4039 psa_status_t status;
4040 size_t key_bits;
4041 mbedtls_cipher_id_t cipher_id;
4042
Gilles Peskine28f8f302019-07-24 13:30:31 +02004043 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004044 if( status != PSA_SUCCESS )
4045 return( status );
4046
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004047 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004048
4049 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004050 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004051 &cipher_id );
4052 if( operation->cipher_info == NULL )
4053 return( PSA_ERROR_NOT_SUPPORTED );
4054
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004055 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004056 {
4057#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004058 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4059 operation->core_alg = PSA_ALG_CCM;
4060 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004061 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4062 * The call to mbedtls_ccm_encrypt_and_tag or
4063 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004064 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004065 return( PSA_ERROR_INVALID_ARGUMENT );
4066 mbedtls_ccm_init( &operation->ctx.ccm );
4067 status = mbedtls_to_psa_error(
4068 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4069 operation->slot->data.raw.data,
4070 (unsigned int) key_bits ) );
4071 if( status != 0 )
4072 goto cleanup;
4073 break;
4074#endif /* MBEDTLS_CCM_C */
4075
4076#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004077 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4078 operation->core_alg = PSA_ALG_GCM;
4079 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004080 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4081 * The call to mbedtls_gcm_crypt_and_tag or
4082 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004083 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004084 return( PSA_ERROR_INVALID_ARGUMENT );
4085 mbedtls_gcm_init( &operation->ctx.gcm );
4086 status = mbedtls_to_psa_error(
4087 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4088 operation->slot->data.raw.data,
4089 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004090 if( status != 0 )
4091 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004092 break;
4093#endif /* MBEDTLS_GCM_C */
4094
Gilles Peskine26869f22019-05-06 15:25:00 +02004095#if defined(MBEDTLS_CHACHAPOLY_C)
4096 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4097 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4098 operation->full_tag_length = 16;
4099 /* We only support the default tag length. */
4100 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4101 return( PSA_ERROR_NOT_SUPPORTED );
4102 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4103 status = mbedtls_to_psa_error(
4104 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4105 operation->slot->data.raw.data ) );
4106 if( status != 0 )
4107 goto cleanup;
4108 break;
4109#endif /* MBEDTLS_CHACHAPOLY_C */
4110
Gilles Peskineedf9a652018-08-17 18:11:56 +02004111 default:
4112 return( PSA_ERROR_NOT_SUPPORTED );
4113 }
4114
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004115 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4116 {
4117 status = PSA_ERROR_INVALID_ARGUMENT;
4118 goto cleanup;
4119 }
4120 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004121
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 return( PSA_SUCCESS );
4123
4124cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004125 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 return( status );
4127}
4128
Gilles Peskinec5487a82018-12-03 18:08:14 +01004129psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004130 psa_algorithm_t alg,
4131 const uint8_t *nonce,
4132 size_t nonce_length,
4133 const uint8_t *additional_data,
4134 size_t additional_data_length,
4135 const uint8_t *plaintext,
4136 size_t plaintext_length,
4137 uint8_t *ciphertext,
4138 size_t ciphertext_size,
4139 size_t *ciphertext_length )
4140{
mohammad16035955c982018-04-26 00:53:03 +03004141 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004142 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004143 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004144
mohammad1603f08a5502018-06-03 15:05:47 +03004145 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004146
Gilles Peskinec5487a82018-12-03 18:08:14 +01004147 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004148 if( status != PSA_SUCCESS )
4149 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004150
Gilles Peskineedf9a652018-08-17 18:11:56 +02004151 /* For all currently supported modes, the tag is at the end of the
4152 * ciphertext. */
4153 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4154 {
4155 status = PSA_ERROR_BUFFER_TOO_SMALL;
4156 goto exit;
4157 }
4158 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004159
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004160#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004161 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004162 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004163 status = mbedtls_to_psa_error(
4164 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4165 MBEDTLS_GCM_ENCRYPT,
4166 plaintext_length,
4167 nonce, nonce_length,
4168 additional_data, additional_data_length,
4169 plaintext, ciphertext,
4170 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004171 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004172 else
4173#endif /* MBEDTLS_GCM_C */
4174#if defined(MBEDTLS_CCM_C)
4175 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004176 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004177 status = mbedtls_to_psa_error(
4178 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4179 plaintext_length,
4180 nonce, nonce_length,
4181 additional_data,
4182 additional_data_length,
4183 plaintext, ciphertext,
4184 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004185 }
mohammad16035c8845f2018-05-09 05:40:09 -07004186 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004187#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004188#if defined(MBEDTLS_CHACHAPOLY_C)
4189 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4190 {
4191 if( nonce_length != 12 || operation.tag_length != 16 )
4192 {
4193 status = PSA_ERROR_NOT_SUPPORTED;
4194 goto exit;
4195 }
4196 status = mbedtls_to_psa_error(
4197 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4198 plaintext_length,
4199 nonce,
4200 additional_data,
4201 additional_data_length,
4202 plaintext,
4203 ciphertext,
4204 tag ) );
4205 }
4206 else
4207#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004208 {
mohammad1603554faad2018-06-03 15:07:38 +03004209 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004210 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004211
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4213 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004214
Gilles Peskineedf9a652018-08-17 18:11:56 +02004215exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004216 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004217 if( status == PSA_SUCCESS )
4218 *ciphertext_length = plaintext_length + operation.tag_length;
4219 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004220}
4221
Gilles Peskineee652a32018-06-01 19:23:52 +02004222/* Locate the tag in a ciphertext buffer containing the encrypted data
4223 * followed by the tag. Return the length of the part preceding the tag in
4224 * *plaintext_length. This is the size of the plaintext in modes where
4225 * the encrypted data has the same size as the plaintext, such as
4226 * CCM and GCM. */
4227static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4228 const uint8_t *ciphertext,
4229 size_t ciphertext_length,
4230 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004231 const uint8_t **p_tag )
4232{
4233 size_t payload_length;
4234 if( tag_length > ciphertext_length )
4235 return( PSA_ERROR_INVALID_ARGUMENT );
4236 payload_length = ciphertext_length - tag_length;
4237 if( payload_length > plaintext_size )
4238 return( PSA_ERROR_BUFFER_TOO_SMALL );
4239 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004240 return( PSA_SUCCESS );
4241}
4242
Gilles Peskinec5487a82018-12-03 18:08:14 +01004243psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004244 psa_algorithm_t alg,
4245 const uint8_t *nonce,
4246 size_t nonce_length,
4247 const uint8_t *additional_data,
4248 size_t additional_data_length,
4249 const uint8_t *ciphertext,
4250 size_t ciphertext_length,
4251 uint8_t *plaintext,
4252 size_t plaintext_size,
4253 size_t *plaintext_length )
4254{
mohammad16035955c982018-04-26 00:53:03 +03004255 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004256 aead_operation_t operation;
4257 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004258
Gilles Peskineee652a32018-06-01 19:23:52 +02004259 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004260
Gilles Peskinec5487a82018-12-03 18:08:14 +01004261 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004262 if( status != PSA_SUCCESS )
4263 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004264
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004265 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4266 ciphertext, ciphertext_length,
4267 plaintext_size, &tag );
4268 if( status != PSA_SUCCESS )
4269 goto exit;
4270
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004271#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004272 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004273 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004274 status = mbedtls_to_psa_error(
4275 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4276 ciphertext_length - operation.tag_length,
4277 nonce, nonce_length,
4278 additional_data,
4279 additional_data_length,
4280 tag, operation.tag_length,
4281 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004282 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004283 else
4284#endif /* MBEDTLS_GCM_C */
4285#if defined(MBEDTLS_CCM_C)
4286 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004287 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004288 status = mbedtls_to_psa_error(
4289 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4290 ciphertext_length - operation.tag_length,
4291 nonce, nonce_length,
4292 additional_data,
4293 additional_data_length,
4294 ciphertext, plaintext,
4295 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004296 }
mohammad160339574652018-06-01 04:39:53 -07004297 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004298#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004299#if defined(MBEDTLS_CHACHAPOLY_C)
4300 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4301 {
4302 if( nonce_length != 12 || operation.tag_length != 16 )
4303 {
4304 status = PSA_ERROR_NOT_SUPPORTED;
4305 goto exit;
4306 }
4307 status = mbedtls_to_psa_error(
4308 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4309 ciphertext_length - operation.tag_length,
4310 nonce,
4311 additional_data,
4312 additional_data_length,
4313 tag,
4314 ciphertext,
4315 plaintext ) );
4316 }
4317 else
4318#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004319 {
mohammad1603554faad2018-06-03 15:07:38 +03004320 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004321 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004322
Gilles Peskineedf9a652018-08-17 18:11:56 +02004323 if( status != PSA_SUCCESS && plaintext_size != 0 )
4324 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004325
Gilles Peskineedf9a652018-08-17 18:11:56 +02004326exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004327 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004328 if( status == PSA_SUCCESS )
4329 *plaintext_length = ciphertext_length - operation.tag_length;
4330 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004331}
4332
Gilles Peskinea0655c32018-04-30 17:06:50 +02004333
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004334
Gilles Peskine20035e32018-02-03 22:44:14 +01004335/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004336/* Generators */
4337/****************************************************************/
4338
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004339#define HKDF_STATE_INIT 0 /* no input yet */
4340#define HKDF_STATE_STARTED 1 /* got salt */
4341#define HKDF_STATE_KEYED 2 /* got key */
4342#define HKDF_STATE_OUTPUT 3 /* output started */
4343
Gilles Peskinecbe66502019-05-16 16:59:18 +02004344static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004346{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4348 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004349 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004351}
4352
4353
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004355{
4356 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004357 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004358 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004359 {
4360 /* The object has (apparently) been initialized but it is not
4361 * in use. It's ok to call abort on such an object, and there's
4362 * nothing to do. */
4363 }
4364 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004365#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004366 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004367 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 mbedtls_free( operation->ctx.hkdf.info );
4369 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004370 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004371 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004373 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004374 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004375 if( operation->ctx.tls12_prf.seed != NULL )
4376 {
4377 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4378 operation->ctx.tls12_prf.seed_length );
4379 mbedtls_free( operation->ctx.tls12_prf.seed );
4380 }
4381
4382 if( operation->ctx.tls12_prf.label != NULL )
4383 {
4384 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4385 operation->ctx.tls12_prf.label_length );
4386 mbedtls_free( operation->ctx.tls12_prf.label );
4387 }
4388
4389 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4390
4391 /* We leave the fields Ai and output_block to be erased safely by the
4392 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004393 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004394 else
4395#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004396 {
4397 status = PSA_ERROR_BAD_STATE;
4398 }
Janos Follath083036a2019-06-11 10:22:26 +01004399 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004400 return( status );
4401}
4402
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004404 size_t *capacity)
4405{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004407 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004409 return PSA_ERROR_BAD_STATE;
4410 }
4411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413 return( PSA_SUCCESS );
4414}
4415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004417 size_t capacity )
4418{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004420 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004422 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004424 return( PSA_SUCCESS );
4425}
4426
Gilles Peskinebef7f142018-07-12 17:22:21 +02004427#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004429 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004430static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 psa_algorithm_t hash_alg,
4432 uint8_t *output,
4433 size_t output_length )
4434{
4435 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4436 psa_status_t status;
4437
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004438 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4439 return( PSA_ERROR_BAD_STATE );
4440 hkdf->state = HKDF_STATE_OUTPUT;
4441
Gilles Peskinebef7f142018-07-12 17:22:21 +02004442 while( output_length != 0 )
4443 {
4444 /* Copy what remains of the current block */
4445 uint8_t n = hash_length - hkdf->offset_in_block;
4446 if( n > output_length )
4447 n = (uint8_t) output_length;
4448 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4449 output += n;
4450 output_length -= n;
4451 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004452 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004453 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004454 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004455 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004456 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004457 * object was corrupted or if this function is called directly
4458 * inside the library. */
4459 if( hkdf->block_number == 0xff )
4460 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004461
4462 /* We need a new block */
4463 ++hkdf->block_number;
4464 hkdf->offset_in_block = 0;
4465 status = psa_hmac_setup_internal( &hkdf->hmac,
4466 hkdf->prk, hash_length,
4467 hash_alg );
4468 if( status != PSA_SUCCESS )
4469 return( status );
4470 if( hkdf->block_number != 1 )
4471 {
4472 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4473 hkdf->output_block,
4474 hash_length );
4475 if( status != PSA_SUCCESS )
4476 return( status );
4477 }
4478 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4479 hkdf->info,
4480 hkdf->info_length );
4481 if( status != PSA_SUCCESS )
4482 return( status );
4483 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4484 &hkdf->block_number, 1 );
4485 if( status != PSA_SUCCESS )
4486 return( status );
4487 status = psa_hmac_finish_internal( &hkdf->hmac,
4488 hkdf->output_block,
4489 sizeof( hkdf->output_block ) );
4490 if( status != PSA_SUCCESS )
4491 return( status );
4492 }
4493
4494 return( PSA_SUCCESS );
4495}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004496
Janos Follath7742fee2019-06-17 12:58:10 +01004497static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4498 psa_tls12_prf_key_derivation_t *tls12_prf,
4499 psa_algorithm_t alg )
4500{
4501 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4502 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004503 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4504 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004505
Janos Follath7742fee2019-06-17 12:58:10 +01004506 /* We can't be wanting more output after block 0xff, otherwise
4507 * the capacity check in psa_key_derivation_output_bytes() would have
4508 * prevented this call. It could happen only if the operation
4509 * object was corrupted or if this function is called directly
4510 * inside the library. */
4511 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004512 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004513
4514 /* We need a new block */
4515 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004516 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004517
4518 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4519 *
4520 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4521 *
4522 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4523 * HMAC_hash(secret, A(2) + seed) +
4524 * HMAC_hash(secret, A(3) + seed) + ...
4525 *
4526 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004527 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004528 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004529 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004530 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004531 * is currently extracted as `output_block` and where i is
4532 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004533 */
4534
Janos Follathea29bfb2019-06-19 12:21:20 +01004535 /* Save the hash context before using it, to preserve the hash state with
4536 * only the inner padding in it. We need this, because inner padding depends
4537 * on the key (secret in the RFC's terminology). */
4538 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4539 if( status != PSA_SUCCESS )
4540 goto cleanup;
4541
4542 /* Calculate A(i) where i = tls12_prf->block_number. */
4543 if( tls12_prf->block_number == 1 )
4544 {
4545 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4546 * the variable seed and in this instance means it in the context of the
4547 * P_hash function, where seed = label + seed.) */
4548 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4549 tls12_prf->label, tls12_prf->label_length );
4550 if( status != PSA_SUCCESS )
4551 goto cleanup;
4552 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4553 tls12_prf->seed, tls12_prf->seed_length );
4554 if( status != PSA_SUCCESS )
4555 goto cleanup;
4556 }
4557 else
4558 {
4559 /* A(i) = HMAC_hash(secret, A(i-1)) */
4560 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4561 tls12_prf->Ai, hash_length );
4562 if( status != PSA_SUCCESS )
4563 goto cleanup;
4564 }
4565
4566 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4567 tls12_prf->Ai, hash_length );
4568 if( status != PSA_SUCCESS )
4569 goto cleanup;
4570 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4571 if( status != PSA_SUCCESS )
4572 goto cleanup;
4573
4574 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4575 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4576 tls12_prf->Ai, hash_length );
4577 if( status != PSA_SUCCESS )
4578 goto cleanup;
4579 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4580 tls12_prf->label, tls12_prf->label_length );
4581 if( status != PSA_SUCCESS )
4582 goto cleanup;
4583 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4584 tls12_prf->seed, tls12_prf->seed_length );
4585 if( status != PSA_SUCCESS )
4586 goto cleanup;
4587 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4588 tls12_prf->output_block, hash_length );
4589 if( status != PSA_SUCCESS )
4590 goto cleanup;
4591 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4592 if( status != PSA_SUCCESS )
4593 goto cleanup;
4594
Janos Follath7742fee2019-06-17 12:58:10 +01004595
4596cleanup:
4597
Janos Follathea29bfb2019-06-19 12:21:20 +01004598 cleanup_status = psa_hash_abort( &backup );
4599 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4600 status = cleanup_status;
4601
4602 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004603}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004604
Janos Follath844eb0e2019-06-19 12:10:49 +01004605static psa_status_t psa_key_derivation_tls12_prf_read(
4606 psa_tls12_prf_key_derivation_t *tls12_prf,
4607 psa_algorithm_t alg,
4608 uint8_t *output,
4609 size_t output_length )
4610{
4611 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4612 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4613 psa_status_t status;
4614 uint8_t offset, length;
4615
4616 while( output_length != 0 )
4617 {
4618 /* Check if we have fully processed the current block. */
4619 if( tls12_prf->left_in_block == 0 )
4620 {
4621 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4622 alg );
4623 if( status != PSA_SUCCESS )
4624 return( status );
4625
4626 continue;
4627 }
4628
4629 if( tls12_prf->left_in_block > output_length )
4630 length = (uint8_t) output_length;
4631 else
4632 length = tls12_prf->left_in_block;
4633
4634 offset = hash_length - tls12_prf->left_in_block;
4635 memcpy( output, tls12_prf->output_block + offset, length );
4636 output += length;
4637 output_length -= length;
4638 tls12_prf->left_in_block -= length;
4639 }
4640
4641 return( PSA_SUCCESS );
4642}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004643#endif /* MBEDTLS_MD_C */
4644
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004645psa_status_t psa_key_derivation_output_bytes(
4646 psa_key_derivation_operation_t *operation,
4647 uint8_t *output,
4648 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004649{
4650 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004652
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004654 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004656 return PSA_ERROR_BAD_STATE;
4657 }
4658
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004661 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004662 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004664 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004665 goto exit;
4666 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004668 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004670 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4672 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004675 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004676 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004678
Gilles Peskinebef7f142018-07-12 17:22:21 +02004679#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004680 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004681 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004682 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004684 output, output_length );
4685 }
Janos Follathadbec812019-06-14 11:05:39 +01004686 else
Janos Follathadbec812019-06-14 11:05:39 +01004687 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004688 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004689 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004691 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004692 output_length );
4693 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004694 else
4695#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004696 {
4697 return( PSA_ERROR_BAD_STATE );
4698 }
4699
4700exit:
4701 if( status != PSA_SUCCESS )
4702 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004703 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004704 * This allows us to differentiate between exhausted operations and
4705 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4706 * operations. */
4707 psa_algorithm_t alg = operation->alg;
4708 psa_key_derivation_abort( operation );
4709 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004710 memset( output, '!', output_length );
4711 }
4712 return( status );
4713}
4714
Gilles Peskine08542d82018-07-19 17:05:42 +02004715#if defined(MBEDTLS_DES_C)
4716static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4717{
4718 if( data_size >= 8 )
4719 mbedtls_des_key_set_parity( data );
4720 if( data_size >= 16 )
4721 mbedtls_des_key_set_parity( data + 8 );
4722 if( data_size >= 24 )
4723 mbedtls_des_key_set_parity( data + 16 );
4724}
4725#endif /* MBEDTLS_DES_C */
4726
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004727static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004728 psa_key_slot_t *slot,
4729 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731{
4732 uint8_t *data = NULL;
4733 size_t bytes = PSA_BITS_TO_BYTES( bits );
4734 psa_status_t status;
4735
Gilles Peskine8e338702019-07-30 20:06:31 +02004736 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 return( PSA_ERROR_INVALID_ARGUMENT );
4738 if( bits % 8 != 0 )
4739 return( PSA_ERROR_INVALID_ARGUMENT );
4740 data = mbedtls_calloc( 1, bytes );
4741 if( data == NULL )
4742 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4743
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 if( status != PSA_SUCCESS )
4746 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004747#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004748 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004749 psa_des_set_key_parity( data, bytes );
4750#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004751 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004752
4753exit:
4754 mbedtls_free( data );
4755 return( status );
4756}
4757
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004758psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004760 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004761{
4762 psa_status_t status;
4763 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004764 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004765
4766 /* Reject any attempt to create a zero-length key so that we don't
4767 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4768 if( psa_get_key_bits( attributes ) == 0 )
4769 return( PSA_ERROR_INVALID_ARGUMENT );
4770
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004771 if( ! operation->can_output_key )
4772 return( PSA_ERROR_NOT_PERMITTED );
4773
Gilles Peskinedf179142019-07-15 22:02:14 +02004774 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4775 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004776#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4777 if( driver != NULL )
4778 {
4779 /* Deriving a key in a secure element is not implemented yet. */
4780 status = PSA_ERROR_NOT_SUPPORTED;
4781 }
4782#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004783 if( status == PSA_SUCCESS )
4784 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004785 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004786 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004788 }
4789 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004790 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004791 if( status != PSA_SUCCESS )
4792 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004793 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004794 *handle = 0;
4795 }
4796 return( status );
4797}
4798
Gilles Peskineeab56e42018-07-12 17:12:33 +02004799
4800
4801/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004802/* Key derivation */
4803/****************************************************************/
4804
Gilles Peskine969c5d62019-01-16 15:53:06 +01004805static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004807 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004808{
Janos Follath5fe19732019-06-20 15:09:30 +01004809 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4810 * initialisers for this union leave some bytes unspecified.) */
4811 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4812
Gilles Peskine969c5d62019-01-16 15:53:06 +01004813 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004814#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4816 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4817 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004818 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004819 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004820 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4821 if( hash_size == 0 )
4822 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4824 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004825 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004826 {
4827 return( PSA_ERROR_NOT_SUPPORTED );
4828 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004830 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004831 }
4832#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004833 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004834 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004835}
4836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004838 psa_algorithm_t alg )
4839{
4840 psa_status_t status;
4841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004843 return( PSA_ERROR_BAD_STATE );
4844
Gilles Peskine6843c292019-01-18 16:44:49 +01004845 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4846 return( PSA_ERROR_INVALID_ARGUMENT );
4847 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004848 {
4849 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004851 }
4852 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4853 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004854 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004855 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004856 else
4857 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004858
4859 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004861 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004862}
4863
4864#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004865static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004866 psa_algorithm_t hash_alg,
4867 psa_key_derivation_step_t step,
4868 const uint8_t *data,
4869 size_t data_length )
4870{
4871 psa_status_t status;
4872 switch( step )
4873 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004874 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004875 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004876 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004877 status = psa_hmac_setup_internal( &hkdf->hmac,
4878 data, data_length,
4879 hash_alg );
4880 if( status != PSA_SUCCESS )
4881 return( status );
4882 hkdf->state = HKDF_STATE_STARTED;
4883 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004884 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885 /* If no salt was provided, use an empty salt. */
4886 if( hkdf->state == HKDF_STATE_INIT )
4887 {
4888 status = psa_hmac_setup_internal( &hkdf->hmac,
4889 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004890 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004891 if( status != PSA_SUCCESS )
4892 return( status );
4893 hkdf->state = HKDF_STATE_STARTED;
4894 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004895 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004897 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4898 data, data_length );
4899 if( status != PSA_SUCCESS )
4900 return( status );
4901 status = psa_hmac_finish_internal( &hkdf->hmac,
4902 hkdf->prk,
4903 sizeof( hkdf->prk ) );
4904 if( status != PSA_SUCCESS )
4905 return( status );
4906 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4907 hkdf->block_number = 0;
4908 hkdf->state = HKDF_STATE_KEYED;
4909 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004910 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004911 if( hkdf->state == HKDF_STATE_OUTPUT )
4912 return( PSA_ERROR_BAD_STATE );
4913 if( hkdf->info_set )
4914 return( PSA_ERROR_BAD_STATE );
4915 hkdf->info_length = data_length;
4916 if( data_length != 0 )
4917 {
4918 hkdf->info = mbedtls_calloc( 1, data_length );
4919 if( hkdf->info == NULL )
4920 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4921 memcpy( hkdf->info, data, data_length );
4922 }
4923 hkdf->info_set = 1;
4924 return( PSA_SUCCESS );
4925 default:
4926 return( PSA_ERROR_INVALID_ARGUMENT );
4927 }
4928}
Janos Follathb03233e2019-06-11 15:30:30 +01004929
Janos Follathf08e2652019-06-13 09:05:41 +01004930static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4931 const uint8_t *data,
4932 size_t data_length )
4933{
4934 if( prf->state != TLS12_PRF_STATE_INIT )
4935 return( PSA_ERROR_BAD_STATE );
4936
Janos Follathd6dce9f2019-07-04 09:11:38 +01004937 if( data_length != 0 )
4938 {
4939 prf->seed = mbedtls_calloc( 1, data_length );
4940 if( prf->seed == NULL )
4941 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004942
Janos Follathd6dce9f2019-07-04 09:11:38 +01004943 memcpy( prf->seed, data, data_length );
4944 prf->seed_length = data_length;
4945 }
Janos Follathf08e2652019-06-13 09:05:41 +01004946
4947 prf->state = TLS12_PRF_STATE_SEED_SET;
4948
4949 return( PSA_SUCCESS );
4950}
4951
Janos Follath81550542019-06-13 14:26:34 +01004952static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4953 psa_algorithm_t hash_alg,
4954 const uint8_t *data,
4955 size_t data_length )
4956{
4957 psa_status_t status;
4958 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4959 return( PSA_ERROR_BAD_STATE );
4960
4961 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4962 if( status != PSA_SUCCESS )
4963 return( status );
4964
4965 prf->state = TLS12_PRF_STATE_KEY_SET;
4966
4967 return( PSA_SUCCESS );
4968}
4969
Janos Follath6660f0e2019-06-17 08:44:03 +01004970static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4971 psa_tls12_prf_key_derivation_t *prf,
4972 psa_algorithm_t hash_alg,
4973 const uint8_t *data,
4974 size_t data_length )
4975{
4976 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004977 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4978 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004979
4980 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4981 return( PSA_ERROR_INVALID_ARGUMENT );
4982
4983 /* Quoting RFC 4279, Section 2:
4984 *
4985 * The premaster secret is formed as follows: if the PSK is N octets
4986 * long, concatenate a uint16 with the value N, N zero octets, a second
4987 * uint16 with the value N, and the PSK itself.
4988 */
4989
Janos Follath40e13932019-06-26 13:22:29 +01004990 *cur++ = ( data_length >> 8 ) & 0xff;
4991 *cur++ = ( data_length >> 0 ) & 0xff;
4992 memset( cur, 0, data_length );
4993 cur += data_length;
4994 *cur++ = pms[0];
4995 *cur++ = pms[1];
4996 memcpy( cur, data, data_length );
4997 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004998
Janos Follath40e13932019-06-26 13:22:29 +01004999 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005000
5001 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5002 return( status );
5003}
5004
Janos Follath63028dd2019-06-13 09:15:47 +01005005static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5006 const uint8_t *data,
5007 size_t data_length )
5008{
5009 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5010 return( PSA_ERROR_BAD_STATE );
5011
Janos Follathd6dce9f2019-07-04 09:11:38 +01005012 if( data_length != 0 )
5013 {
5014 prf->label = mbedtls_calloc( 1, data_length );
5015 if( prf->label == NULL )
5016 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005017
Janos Follathd6dce9f2019-07-04 09:11:38 +01005018 memcpy( prf->label, data, data_length );
5019 prf->label_length = data_length;
5020 }
Janos Follath63028dd2019-06-13 09:15:47 +01005021
5022 prf->state = TLS12_PRF_STATE_LABEL_SET;
5023
5024 return( PSA_SUCCESS );
5025}
5026
Janos Follathb03233e2019-06-11 15:30:30 +01005027static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5028 psa_algorithm_t hash_alg,
5029 psa_key_derivation_step_t step,
5030 const uint8_t *data,
5031 size_t data_length )
5032{
Janos Follathb03233e2019-06-11 15:30:30 +01005033 switch( step )
5034 {
Janos Follathf08e2652019-06-13 09:05:41 +01005035 case PSA_KEY_DERIVATION_INPUT_SEED:
5036 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005037 case PSA_KEY_DERIVATION_INPUT_SECRET:
5038 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005039 case PSA_KEY_DERIVATION_INPUT_LABEL:
5040 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005041 default:
5042 return( PSA_ERROR_INVALID_ARGUMENT );
5043 }
5044}
Janos Follath6660f0e2019-06-17 08:44:03 +01005045
5046static psa_status_t psa_tls12_prf_psk_to_ms_input(
5047 psa_tls12_prf_key_derivation_t *prf,
5048 psa_algorithm_t hash_alg,
5049 psa_key_derivation_step_t step,
5050 const uint8_t *data,
5051 size_t data_length )
5052{
5053 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005054 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005055 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5056 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005057 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005058
5059 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5060}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005061#endif /* MBEDTLS_MD_C */
5062
Gilles Peskineb8965192019-09-24 16:21:10 +02005063/** Check whether the given key type is acceptable for the given
5064 * input step of a key derivation.
5065 *
5066 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5067 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5068 * Both secret and non-secret inputs can alternatively have the type
5069 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5070 * that the input was passed as a buffer rather than via a key object.
5071 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005072static int psa_key_derivation_check_input_type(
5073 psa_key_derivation_step_t step,
5074 psa_key_type_t key_type )
5075{
5076 switch( step )
5077 {
5078 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005079 if( key_type == PSA_KEY_TYPE_DERIVE )
5080 return( PSA_SUCCESS );
5081 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005082 return( PSA_SUCCESS );
5083 break;
5084 case PSA_KEY_DERIVATION_INPUT_LABEL:
5085 case PSA_KEY_DERIVATION_INPUT_SALT:
5086 case PSA_KEY_DERIVATION_INPUT_INFO:
5087 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005088 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5089 return( PSA_SUCCESS );
5090 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005091 return( PSA_SUCCESS );
5092 break;
5093 }
5094 return( PSA_ERROR_INVALID_ARGUMENT );
5095}
5096
Janos Follathb80a94e2019-06-12 15:54:46 +01005097static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005099 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005100 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005101 const uint8_t *data,
5102 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005103{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005104 psa_status_t status;
5105 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5106
5107 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005108 if( status != PSA_SUCCESS )
5109 goto exit;
5110
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005111#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005112 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005113 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005115 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116 step, data, data_length );
5117 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005118 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005119 {
Janos Follathb03233e2019-06-11 15:30:30 +01005120 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5121 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5122 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005123 }
5124 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5125 {
5126 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5127 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5128 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005129 }
5130 else
5131#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005134 return( PSA_ERROR_BAD_STATE );
5135 }
5136
Gilles Peskine224b0d62019-09-23 18:13:17 +02005137exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005140 return( status );
5141}
5142
Janos Follath51f4a0f2019-06-14 11:35:55 +01005143psa_status_t psa_key_derivation_input_bytes(
5144 psa_key_derivation_operation_t *operation,
5145 psa_key_derivation_step_t step,
5146 const uint8_t *data,
5147 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005148{
Gilles Peskineb8965192019-09-24 16:21:10 +02005149 return( psa_key_derivation_input_internal( operation, step,
5150 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005151 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005152}
5153
Janos Follath51f4a0f2019-06-14 11:35:55 +01005154psa_status_t psa_key_derivation_input_key(
5155 psa_key_derivation_operation_t *operation,
5156 psa_key_derivation_step_t step,
5157 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158{
5159 psa_key_slot_t *slot;
5160 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005161
Gilles Peskine28f8f302019-07-24 13:30:31 +02005162 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005163 PSA_KEY_USAGE_DERIVE,
5164 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005165 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005166 {
5167 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005168 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005169 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005170
5171 /* Passing a key object as a SECRET input unlocks the permission
5172 * to output to a key object. */
5173 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5174 operation->can_output_key = 1;
5175
Janos Follathb80a94e2019-06-12 15:54:46 +01005176 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005177 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005178 slot->data.raw.data,
5179 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005181
5182
5183
5184/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005185/* Key agreement */
5186/****************************************************************/
5187
Gilles Peskinea05219c2018-11-16 16:02:56 +01005188#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005189static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5190 size_t peer_key_length,
5191 const mbedtls_ecp_keypair *our_key,
5192 uint8_t *shared_secret,
5193 size_t shared_secret_size,
5194 size_t *shared_secret_length )
5195{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005196 mbedtls_ecp_keypair *their_key = NULL;
5197 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005198 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005199 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005200
Jaeden Ameroccdce902019-01-10 11:42:27 +00005201 status = psa_import_ec_public_key(
5202 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5203 peer_key, peer_key_length,
5204 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005205 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005206 goto exit;
5207
Jaeden Amero97271b32019-01-10 19:38:51 +00005208 status = mbedtls_to_psa_error(
5209 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5210 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005211 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005212 status = mbedtls_to_psa_error(
5213 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5214 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005215 goto exit;
5216
Jaeden Amero97271b32019-01-10 19:38:51 +00005217 status = mbedtls_to_psa_error(
5218 mbedtls_ecdh_calc_secret( &ecdh,
5219 shared_secret_length,
5220 shared_secret, shared_secret_size,
5221 mbedtls_ctr_drbg_random,
5222 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005223
5224exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005226 mbedtls_ecp_keypair_free( their_key );
5227 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005228 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005229}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005230#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005231
Gilles Peskine01d718c2018-09-18 12:01:02 +02005232#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5233
Gilles Peskine0216fe12019-04-11 21:23:21 +02005234static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5235 psa_key_slot_t *private_key,
5236 const uint8_t *peer_key,
5237 size_t peer_key_length,
5238 uint8_t *shared_secret,
5239 size_t shared_secret_size,
5240 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005241{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005242 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005243 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005244#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005245 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005246 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005247 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005248 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5249 private_key->data.ecp,
5250 shared_secret, shared_secret_size,
5251 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005252#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005254 (void) private_key;
5255 (void) peer_key;
5256 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005257 (void) shared_secret;
5258 (void) shared_secret_size;
5259 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260 return( PSA_ERROR_NOT_SUPPORTED );
5261 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005262}
5263
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005264/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265 * to potentially free embedded data structures and wipe confidential data.
5266 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005269 psa_key_slot_t *private_key,
5270 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005271 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005272{
5273 psa_status_t status;
5274 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5275 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005277
5278 /* Step 1: run the secret agreement algorithm to generate the shared
5279 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005280 status = psa_key_agreement_raw_internal( ka_alg,
5281 private_key,
5282 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005283 shared_secret,
5284 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005285 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005286 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005287 goto exit;
5288
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005289 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005290 * the shared secret. A shared secret is permitted wherever a key
5291 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005292 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005293 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005294 shared_secret,
5295 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005296
Gilles Peskine12313cd2018-06-20 00:20:32 +02005297exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005298 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005299 return( status );
5300}
5301
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005303 psa_key_derivation_step_t step,
5304 psa_key_handle_t private_key,
5305 const uint8_t *peer_key,
5306 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005307{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005308 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005309 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005311 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005312 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005313 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005314 if( status != PSA_SUCCESS )
5315 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005317 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005319 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005321 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005322}
5323
Gilles Peskinebe697d82019-05-16 18:00:41 +02005324psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5325 psa_key_handle_t private_key,
5326 const uint8_t *peer_key,
5327 size_t peer_key_length,
5328 uint8_t *output,
5329 size_t output_size,
5330 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005331{
5332 psa_key_slot_t *slot;
5333 psa_status_t status;
5334
5335 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5336 {
5337 status = PSA_ERROR_INVALID_ARGUMENT;
5338 goto exit;
5339 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005340 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005341 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005342 if( status != PSA_SUCCESS )
5343 goto exit;
5344
5345 status = psa_key_agreement_raw_internal( alg, slot,
5346 peer_key, peer_key_length,
5347 output, output_size,
5348 output_length );
5349
5350exit:
5351 if( status != PSA_SUCCESS )
5352 {
5353 /* If an error happens and is not handled properly, the output
5354 * may be used as a key to protect sensitive data. Arrange for such
5355 * a key to be random, which is likely to result in decryption or
5356 * verification errors. This is better than filling the buffer with
5357 * some constant data such as zeros, which would result in the data
5358 * being protected with a reproducible, easily knowable key.
5359 */
5360 psa_generate_random( output, output_size );
5361 *output_length = output_size;
5362 }
5363 return( status );
5364}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005365
5366
5367/****************************************************************/
5368/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005369/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005370
Gilles Peskine4c317f42018-07-12 01:24:09 +02005371psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005372 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005373{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005374 int ret;
5375 GUARD_MODULE_INITIALIZED;
5376
Gilles Peskinef181eca2019-08-07 13:49:00 +02005377 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5378 {
5379 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5380 output,
5381 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5382 if( ret != 0 )
5383 return( mbedtls_to_psa_error( ret ) );
5384 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5385 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5386 }
5387
Gilles Peskinee59236f2018-01-27 23:32:46 +01005388 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5389 return( mbedtls_to_psa_error( ret ) );
5390}
5391
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005392#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5393#include "mbedtls/entropy_poll.h"
5394
Gilles Peskine7228da22019-07-15 11:06:15 +02005395psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005396 size_t seed_size )
5397{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005398 if( global_data.initialized )
5399 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005400
5401 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5402 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5403 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5404 return( PSA_ERROR_INVALID_ARGUMENT );
5405
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005406 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005407}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005408#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005409
Gilles Peskinee56e8782019-04-26 17:34:02 +02005410#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5411static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5412 size_t domain_parameters_size,
5413 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005414{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005415 size_t i;
5416 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005417
Gilles Peskinee56e8782019-04-26 17:34:02 +02005418 if( domain_parameters_size == 0 )
5419 {
5420 *exponent = 65537;
5421 return( PSA_SUCCESS );
5422 }
5423
5424 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5425 * support values that fit in a 32-bit integer, which is larger than
5426 * int on just about every platform anyway. */
5427 if( domain_parameters_size > sizeof( acc ) )
5428 return( PSA_ERROR_NOT_SUPPORTED );
5429 for( i = 0; i < domain_parameters_size; i++ )
5430 acc = ( acc << 8 ) | domain_parameters[i];
5431 if( acc > INT_MAX )
5432 return( PSA_ERROR_NOT_SUPPORTED );
5433 *exponent = acc;
5434 return( PSA_SUCCESS );
5435}
5436#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5437
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005438static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005439 psa_key_slot_t *slot, size_t bits,
5440 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005441{
Gilles Peskine8e338702019-07-30 20:06:31 +02005442 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005443
Gilles Peskinee56e8782019-04-26 17:34:02 +02005444 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005445 return( PSA_ERROR_INVALID_ARGUMENT );
5446
Gilles Peskinee59236f2018-01-27 23:32:46 +01005447 if( key_type_is_raw_bytes( type ) )
5448 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005449 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005450 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5451 if( status != PSA_SUCCESS )
5452 return( status );
5453 status = psa_generate_random( slot->data.raw.data,
5454 slot->data.raw.bytes );
5455 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005456 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005457#if defined(MBEDTLS_DES_C)
5458 if( type == PSA_KEY_TYPE_DES )
5459 psa_des_set_key_parity( slot->data.raw.data,
5460 slot->data.raw.bytes );
5461#endif /* MBEDTLS_DES_C */
5462 }
5463 else
5464
5465#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005466 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005467 {
5468 mbedtls_rsa_context *rsa;
5469 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005470 int exponent;
5471 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005472 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5473 return( PSA_ERROR_NOT_SUPPORTED );
5474 /* Accept only byte-aligned keys, for the same reasons as
5475 * in psa_import_rsa_key(). */
5476 if( bits % 8 != 0 )
5477 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005478 status = psa_read_rsa_exponent( domain_parameters,
5479 domain_parameters_size,
5480 &exponent );
5481 if( status != PSA_SUCCESS )
5482 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005483 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5484 if( rsa == NULL )
5485 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5486 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5487 ret = mbedtls_rsa_gen_key( rsa,
5488 mbedtls_ctr_drbg_random,
5489 &global_data.ctr_drbg,
5490 (unsigned int) bits,
5491 exponent );
5492 if( ret != 0 )
5493 {
5494 mbedtls_rsa_free( rsa );
5495 mbedtls_free( rsa );
5496 return( mbedtls_to_psa_error( ret ) );
5497 }
5498 slot->data.rsa = rsa;
5499 }
5500 else
5501#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5502
5503#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005504 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005505 {
5506 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5507 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5508 const mbedtls_ecp_curve_info *curve_info =
5509 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5510 mbedtls_ecp_keypair *ecp;
5511 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005512 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005513 return( PSA_ERROR_NOT_SUPPORTED );
5514 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5515 return( PSA_ERROR_NOT_SUPPORTED );
5516 if( curve_info->bit_size != bits )
5517 return( PSA_ERROR_INVALID_ARGUMENT );
5518 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5519 if( ecp == NULL )
5520 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5521 mbedtls_ecp_keypair_init( ecp );
5522 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5523 mbedtls_ctr_drbg_random,
5524 &global_data.ctr_drbg );
5525 if( ret != 0 )
5526 {
5527 mbedtls_ecp_keypair_free( ecp );
5528 mbedtls_free( ecp );
5529 return( mbedtls_to_psa_error( ret ) );
5530 }
5531 slot->data.ecp = ecp;
5532 }
5533 else
5534#endif /* MBEDTLS_ECP_C */
5535
5536 return( PSA_ERROR_NOT_SUPPORTED );
5537
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538 return( PSA_SUCCESS );
5539}
Darryl Green0c6575a2018-11-07 16:05:30 +00005540
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005541psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005542 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005543{
5544 psa_status_t status;
5545 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005546 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005547
Gilles Peskine0f84d622019-09-12 19:03:13 +02005548 /* Reject any attempt to create a zero-length key so that we don't
5549 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5550 if( psa_get_key_bits( attributes ) == 0 )
5551 return( PSA_ERROR_INVALID_ARGUMENT );
5552
Gilles Peskinedf179142019-07-15 22:02:14 +02005553 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5554 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005555 if( status != PSA_SUCCESS )
5556 goto exit;
5557
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005558#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5559 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005560 {
Gilles Peskine11792082019-08-06 18:36:36 +02005561 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5562 size_t pubkey_length = 0; /* We don't support this feature yet */
5563 if( drv->key_management == NULL ||
5564 drv->key_management->p_generate == NULL )
5565 {
5566 status = PSA_ERROR_NOT_SUPPORTED;
5567 goto exit;
5568 }
5569 status = drv->key_management->p_generate(
5570 psa_get_se_driver_context( driver ),
5571 slot->data.se.slot_number, attributes,
5572 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005573 }
Gilles Peskine11792082019-08-06 18:36:36 +02005574 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005575#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005576 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005577 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005578 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005579 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005580 }
Gilles Peskine11792082019-08-06 18:36:36 +02005581
5582exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005583 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005584 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005585 if( status != PSA_SUCCESS )
5586 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005587 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005588 *handle = 0;
5589 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005590 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005591}
5592
5593
Gilles Peskinee59236f2018-01-27 23:32:46 +01005594
5595/****************************************************************/
5596/* Module setup */
5597/****************************************************************/
5598
Gilles Peskine5e769522018-11-20 21:59:56 +01005599psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5600 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5601 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5602{
5603 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5604 return( PSA_ERROR_BAD_STATE );
5605 global_data.entropy_init = entropy_init;
5606 global_data.entropy_free = entropy_free;
5607 return( PSA_SUCCESS );
5608}
5609
Gilles Peskinee59236f2018-01-27 23:32:46 +01005610void mbedtls_psa_crypto_free( void )
5611{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005612 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005613 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5614 {
5615 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005616 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005617 }
5618 /* Wipe all remaining data, including configuration.
5619 * In particular, this sets all state indicator to the value
5620 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005621 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005623 /* Unregister all secure element drivers, so that we restart from
5624 * a pristine state. */
5625 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005626#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005627}
5628
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005629#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5630/** Recover a transaction that was interrupted by a power failure.
5631 *
5632 * This function is called during initialization, before psa_crypto_init()
5633 * returns. If this function returns a failure status, the initialization
5634 * fails.
5635 */
5636static psa_status_t psa_crypto_recover_transaction(
5637 const psa_crypto_transaction_t *transaction )
5638{
5639 switch( transaction->unknown.type )
5640 {
5641 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5642 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005643 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005644 * is implemented.
5645 * https://github.com/ARMmbed/mbed-crypto/issues/218
5646 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005647 default:
5648 /* We found an unsupported transaction in the storage.
5649 * We don't know what state the storage is in. Give up. */
5650 return( PSA_ERROR_STORAGE_FAILURE );
5651 }
5652}
5653#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5654
Gilles Peskinee59236f2018-01-27 23:32:46 +01005655psa_status_t psa_crypto_init( void )
5656{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005657 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005658 const unsigned char drbg_seed[] = "PSA";
5659
Gilles Peskinec6b69072018-11-20 21:42:52 +01005660 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005661 if( global_data.initialized != 0 )
5662 return( PSA_SUCCESS );
5663
Gilles Peskine5e769522018-11-20 21:59:56 +01005664 /* Set default configuration if
5665 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5666 if( global_data.entropy_init == NULL )
5667 global_data.entropy_init = mbedtls_entropy_init;
5668 if( global_data.entropy_free == NULL )
5669 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005670
Gilles Peskinec6b69072018-11-20 21:42:52 +01005671 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005672 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005673#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5674 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5675 /* The PSA entropy injection feature depends on using NV seed as an entropy
5676 * source. Add NV seed as an entropy source for PSA entropy injection. */
5677 mbedtls_entropy_add_source( &global_data.entropy,
5678 mbedtls_nv_seed_poll, NULL,
5679 MBEDTLS_ENTROPY_BLOCK_SIZE,
5680 MBEDTLS_ENTROPY_SOURCE_STRONG );
5681#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005682 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005683 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005684 status = mbedtls_to_psa_error(
5685 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5686 mbedtls_entropy_func,
5687 &global_data.entropy,
5688 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5689 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005690 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005691 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005692
Gilles Peskine66fb1262018-12-10 16:29:04 +01005693 status = psa_initialize_key_slots( );
5694 if( status != PSA_SUCCESS )
5695 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005696
Gilles Peskined9348f22019-10-01 15:22:29 +02005697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5698 status = psa_init_all_se_drivers( );
5699 if( status != PSA_SUCCESS )
5700 goto exit;
5701#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5702
Gilles Peskine4b734222019-07-24 15:56:31 +02005703#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005704 status = psa_crypto_load_transaction( );
5705 if( status == PSA_SUCCESS )
5706 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005707 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5708 if( status != PSA_SUCCESS )
5709 goto exit;
5710 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005711 }
5712 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5713 {
5714 /* There's no transaction to complete. It's all good. */
5715 status = PSA_SUCCESS;
5716 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005717#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005718
Gilles Peskinec6b69072018-11-20 21:42:52 +01005719 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005720 global_data.initialized = 1;
5721
5722exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005723 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005724 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005725 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005726}
5727
5728#endif /* MBEDTLS_PSA_CRYPTO_C */