blob: ac2eae6670cb53a5561f623eedc920e26cf8077f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
454#if defined(MBEDTLS_MD_C)
455 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200456#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
495 /* Allocate memory for the key */
496 raw->bytes = PSA_BITS_TO_BYTES( bits );
497 raw->data = mbedtls_calloc( 1, raw->bytes );
498 if( raw->data == NULL )
499 {
500 raw->bytes = 0;
501 return( PSA_ERROR_INSUFFICIENT_MEMORY );
502 }
503 return( PSA_SUCCESS );
504}
505
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100507/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
508 * that are not a multiple of 8) well. For example, there is only
509 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
510 * way to return the exact bit size of a key.
511 * To keep things simple, reject non-byte-aligned key sizes. */
512static psa_status_t psa_check_rsa_key_byte_aligned(
513 const mbedtls_rsa_context *rsa )
514{
515 mbedtls_mpi n;
516 psa_status_t status;
517 mbedtls_mpi_init( &n );
518 status = mbedtls_to_psa_error(
519 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
520 if( status == PSA_SUCCESS )
521 {
522 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
523 status = PSA_ERROR_NOT_SUPPORTED;
524 }
525 mbedtls_mpi_free( &n );
526 return( status );
527}
528
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000529static psa_status_t psa_import_rsa_key( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 mbedtls_rsa_context **p_rsa )
533{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534 psa_status_t status;
535 mbedtls_pk_context pk;
536 mbedtls_rsa_context *rsa;
537 size_t bits;
538
539 mbedtls_pk_init( &pk );
540
541 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
553 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
559 rsa = mbedtls_pk_rsa( pk );
560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
569 status = psa_check_rsa_key_byte_aligned( rsa );
570
571exit:
572 /* Free the content of the pk object only on error. */
573 if( status != PSA_SUCCESS )
574 {
575 mbedtls_pk_free( &pk );
576 return( status );
577 }
578
579 /* On success, store the content of the object in the RSA context. */
580 *p_rsa = rsa;
581
582 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583}
584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586#if defined(MBEDTLS_ECP_C)
587
588/* Import a public key given as the uncompressed representation defined by SEC1
589 * 2.3.3 as the content of an ECPoint. */
590static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
591 const uint8_t *data,
592 size_t data_length,
593 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000596 mbedtls_ecp_keypair *ecp = NULL;
597 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598
599 *p_ecp = NULL;
600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
601 if( ecp == NULL )
602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
603 mbedtls_ecp_keypair_init( ecp );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the public value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
613 data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616
617 /* Check that the point is on the curve. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 *p_ecp = ecp;
624 return( PSA_SUCCESS );
625
626exit:
627 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair_free( ecp );
630 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
637/* Import a private key given as a byte string which is the private value
638 * in big-endian order. */
639static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
640 const uint8_t *data,
641 size_t data_length,
642 mbedtls_ecp_keypair **p_ecp )
643{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 mbedtls_ecp_keypair *ecp = NULL;
646 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200648 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649 return( PSA_ERROR_INVALID_ARGUMENT );
650
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 *p_ecp = NULL;
652 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
653 if( ecp == NULL )
654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000655 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
657 /* Load the group. */
658 status = mbedtls_to_psa_error(
659 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662 /* Load the secret value. */
663 status = mbedtls_to_psa_error(
664 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
665 if( status != PSA_SUCCESS )
666 goto exit;
667 /* Validate the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
670 if( status != PSA_SUCCESS )
671 goto exit;
672 /* Calculate the public key from the private key. */
673 status = mbedtls_to_psa_error(
674 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
675 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678
679 *p_ecp = ecp;
680 return( PSA_SUCCESS );
681
682exit:
683 if( ecp != NULL )
684 {
685 mbedtls_ecp_keypair_free( ecp );
686 mbedtls_free( ecp );
687 }
688 return( status );
689}
690#endif /* defined(MBEDTLS_ECP_C) */
691
Gilles Peskineb46bef22019-07-30 21:32:04 +0200692
693/** Return the size of the key in the given slot, in bits.
694 *
695 * \param[in] slot A key slot.
696 *
697 * \return The key size in bits, read from the metadata in the slot.
698 */
699static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
700{
701 return( slot->attr.bits );
702}
703
704/** Calculate the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot containing a transparent key.
707 *
708 * \return The key size in bits, calculated from the key data.
709 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200710static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 size_t bits = 0; /* return 0 on an empty slot */
713
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
718 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#endif /* defined(MBEDTLS_RSA_C) */
720#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
722 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724
725 /* We know that the size fits in psa_key_bits_t thanks to checks
726 * when the key was created. */
727 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728}
729
Gilles Peskine8e338702019-07-30 20:06:31 +0200730/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100731 * previously. This function assumes that the slot does not contain
732 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100733psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
734 const uint8_t *data,
735 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Gilles Peskine8e338702019-07-30 20:06:31 +0200739 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200742 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 if( data_length > SIZE_MAX / 8 )
744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750 &slot->data.raw );
751 if( status != PSA_SUCCESS )
752 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200753 if( data_length != 0 )
754 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 }
756 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100757#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200758 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 data, data_length,
762 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000766 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 data, data_length,
769 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000777 data, data_length,
778 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 }
780 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
783 return( PSA_ERROR_NOT_SUPPORTED );
784 }
Darryl Green940d72c2018-07-13 13:18:51 +0100785
Gilles Peskineb46bef22019-07-30 21:32:04 +0200786 if( status == PSA_SUCCESS )
787 {
788 /* Write the actual key size to the slot.
789 * psa_start_key_creation() wrote the size declared by the
790 * caller, which may be 0 (meaning unspecified) or wrong. */
791 slot->attr.bits = psa_calculate_key_bits( slot );
792 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 return( status );
794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
801 psa_algorithm_t alg1,
802 psa_algorithm_t alg2 )
803{
Gilles Peskine549ea862019-05-22 11:45:59 +0200804 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 if( alg1 == alg2 )
806 return( alg1 );
807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
809 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
810 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
811 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
812 {
813 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
814 return( alg2 );
815 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
816 return( alg1 );
817 }
818 /* If the policies are incompatible, allow nothing. */
819 return( 0 );
820}
821
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
823 psa_algorithm_t requested_alg )
824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826 if( requested_alg == policy_alg )
827 return( 1 );
828 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 * and requested_alg is the same hash-and-sign family with any hash,
830 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
832 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
833 {
834 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
835 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
836 }
837 /* If it isn't permitted, it's forbidden. */
838 return( 0 );
839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
844 */
845static int psa_key_policy_permits( const psa_key_policy_t *policy,
846 psa_algorithm_t alg )
847{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200848 return( psa_key_algorithm_permits( policy->alg, alg ) ||
849 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850}
851
Gilles Peskinef603c712019-01-19 13:40:11 +0100852/** Restrict a key policy based on a constraint.
853 *
854 * \param[in,out] policy The policy to restrict.
855 * \param[in] constraint The policy constraint to apply.
856 *
857 * \retval #PSA_SUCCESS
858 * \c *policy contains the intersection of the original value of
859 * \c *policy and \c *constraint.
860 * \retval #PSA_ERROR_INVALID_ARGUMENT
861 * \c *policy and \c *constraint are incompatible.
862 * \c *policy is unchanged.
863 */
864static psa_status_t psa_restrict_key_policy(
865 psa_key_policy_t *policy,
866 const psa_key_policy_t *constraint )
867{
868 psa_algorithm_t intersection_alg =
869 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 psa_algorithm_t intersection_alg2 =
871 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Darryl Green06fd18d2018-07-16 11:21:11 +0100882/** Retrieve a slot which must contain a key. The key must have allow all the
883 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
884 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100885static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100887 psa_key_usage_t usage,
888 psa_algorithm_t alg )
889{
890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100891 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100892
893 *p_slot = NULL;
894
Gilles Peskinec5487a82018-12-03 18:08:14 +0100895 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100896 if( status != PSA_SUCCESS )
897 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 /* Enforce that usage policy for the key slot contains all the flags
900 * required by the usage parameter. There is one exception: public
901 * keys can always be exported, so we treat public key objects as
902 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200903 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907
908 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
911
912 *p_slot = slot;
913 return( PSA_SUCCESS );
914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916/** Retrieve a slot which must contain a transparent key.
917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200921 * This is a temporary function to use instead of psa_get_key_from_slot()
922 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923 */
924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
925static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
926 psa_key_slot_t **p_slot,
927 psa_key_usage_t usage,
928 psa_algorithm_t alg )
929{
930 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200933 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 {
935 *p_slot = NULL;
936 return( PSA_ERROR_NOT_SUPPORTED );
937 }
938 return( PSA_SUCCESS );
939}
940#else /* MBEDTLS_PSA_CRYPTO_SE_C */
941/* With no secure element support, all keys are transparent. */
942#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
943 psa_get_key_from_slot( handle, p_slot, usage, alg )
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
945
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100946/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000948{
Gilles Peskine73167e12019-07-12 23:44:37 +0200949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
950 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000951 {
952 /* No key material to clean. */
953 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200954 else
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200956 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000957 {
958 /* No key material to clean. */
959 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 mbedtls_free( slot->data.raw.data );
963 }
964 else
965#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_rsa_free( slot->data.rsa );
969 mbedtls_free( slot->data.rsa );
970 }
971 else
972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_ecp_keypair_free( slot->data.ecp );
977 mbedtls_free( slot->data.ecp );
978 }
979 else
980#endif /* defined(MBEDTLS_ECP_C) */
981 {
982 /* Shouldn't happen: the key type is not any type that we
983 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200984 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000985 }
986
987 return( PSA_SUCCESS );
988}
989
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990/** Completely wipe a slot in memory, including its policy.
991 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100992psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993{
994 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200995 /* Multipart operations may still be using the key. This is safe
996 * because all multipart operation objects are independent from
997 * the key slot: if they need to access the key after the setup
998 * phase, they have a copy of the key. Note that this means that
999 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000 /* At this point, key material and other type-specific content has
1001 * been wiped. Clear remaining metadata. We can call memset and not
1002 * zeroize because the metadata is not particularly sensitive. */
1003 memset( slot, 0, sizeof( *slot ) );
1004 return( status );
1005}
1006
Gilles Peskinec5487a82018-12-03 18:08:14 +01001007psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001009 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001010 psa_status_t status; /* status of the last operation */
1011 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 psa_se_drv_table_entry_t *driver;
1014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 if( status != PSA_SUCCESS )
1018 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001019
1020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001023 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001024 /* For a key in a secure element, we need to do three things:
1025 * remove the key file in internal storage, destroy the
1026 * key inside the secure element, and update the driver's
1027 * persistent data. Start a transaction that will encompass these
1028 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 status = psa_crypto_save_transaction( );
1034 if( status != PSA_SUCCESS )
1035 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001036 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001037 /* We should still try to destroy the key in the secure
1038 * element and the key metadata in storage. This is especially
1039 * important if the error is that the storage is full.
1040 * But how to do it exactly without risking an inconsistent
1041 * state after a reset?
1042 * https://github.com/ARMmbed/mbed-crypto/issues/215
1043 */
1044 overall_status = status;
1045 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 }
1047
Gilles Peskine354f7672019-07-12 23:46:38 +02001048 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001049 if( overall_status == PSA_SUCCESS )
1050 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001055 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001057 status = psa_destroy_persistent_key( slot->attr.id );
1058 if( overall_status == PSA_SUCCESS )
1059 overall_status = status;
1060
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001061 /* TODO: other slots may have a copy of the same key. We should
1062 * invalidate them.
1063 * https://github.com/ARMmbed/mbed-crypto/issues/214
1064 */
Darryl Greend49a4992018-06-18 17:27:26 +01001065 }
1066#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001067
Gilles Peskinefc762652019-07-22 19:30:34 +02001068#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1069 if( driver != NULL )
1070 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001071 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074 status = psa_crypto_stop_transaction( );
1075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001077 }
1078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1079
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1081exit:
1082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001084 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
1087 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088}
1089
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001091{
Gilles Peskineb699f072019-04-26 16:06:02 +02001092 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001094}
1095
Gilles Peskineb699f072019-04-26 16:06:02 +02001096psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1097 psa_key_type_t type,
1098 const uint8_t *data,
1099 size_t data_length )
1100{
1101 uint8_t *copy = NULL;
1102
1103 if( data_length != 0 )
1104 {
1105 copy = mbedtls_calloc( 1, data_length );
1106 if( copy == NULL )
1107 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1108 memcpy( copy, data, data_length );
1109 }
1110 /* After this point, this function is guaranteed to succeed, so it
1111 * can start modifying `*attributes`. */
1112
1113 if( attributes->domain_parameters != NULL )
1114 {
1115 mbedtls_free( attributes->domain_parameters );
1116 attributes->domain_parameters = NULL;
1117 attributes->domain_parameters_size = 0;
1118 }
1119
1120 attributes->domain_parameters = copy;
1121 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001122 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001123 return( PSA_SUCCESS );
1124}
1125
1126psa_status_t psa_get_key_domain_parameters(
1127 const psa_key_attributes_t *attributes,
1128 uint8_t *data, size_t data_size, size_t *data_length )
1129{
1130 if( attributes->domain_parameters_size > data_size )
1131 return( PSA_ERROR_BUFFER_TOO_SMALL );
1132 *data_length = attributes->domain_parameters_size;
1133 if( attributes->domain_parameters_size != 0 )
1134 memcpy( data, attributes->domain_parameters,
1135 attributes->domain_parameters_size );
1136 return( PSA_SUCCESS );
1137}
1138
1139#if defined(MBEDTLS_RSA_C)
1140static psa_status_t psa_get_rsa_public_exponent(
1141 const mbedtls_rsa_context *rsa,
1142 psa_key_attributes_t *attributes )
1143{
1144 mbedtls_mpi mpi;
1145 int ret;
1146 uint8_t *buffer = NULL;
1147 size_t buflen;
1148 mbedtls_mpi_init( &mpi );
1149
1150 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1151 if( ret != 0 )
1152 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001153 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1154 {
1155 /* It's the default value, which is reported as an empty string,
1156 * so there's nothing to do. */
1157 goto exit;
1158 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001159
1160 buflen = mbedtls_mpi_size( &mpi );
1161 buffer = mbedtls_calloc( 1, buflen );
1162 if( buffer == NULL )
1163 {
1164 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1165 goto exit;
1166 }
1167 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1168 if( ret != 0 )
1169 goto exit;
1170 attributes->domain_parameters = buffer;
1171 attributes->domain_parameters_size = buflen;
1172
1173exit:
1174 mbedtls_mpi_free( &mpi );
1175 if( ret != 0 )
1176 mbedtls_free( buffer );
1177 return( mbedtls_to_psa_error( ret ) );
1178}
1179#endif /* MBEDTLS_RSA_C */
1180
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001181/** Retrieve all the publicly-accessible attributes of a key.
1182 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1184 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001187 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189 psa_reset_key_attributes( attributes );
1190
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001192 if( status != PSA_SUCCESS )
1193 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001194
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001195 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001196 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1197 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1198
1199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1200 if( psa_key_slot_is_external( slot ) )
1201 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
Gilles Peskine8e338702019-07-30 20:06:31 +02001204 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 {
1206#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001207 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001210 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001211 * is not yet implemented.
1212 * https://github.com/ARMmbed/mbed-crypto/issues/216
1213 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001214 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001215 break;
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1218 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001219#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 default:
1221 /* Nothing else to do. */
1222 break;
1223 }
1224
1225 if( status != PSA_SUCCESS )
1226 psa_reset_key_attributes( attributes );
1227 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228}
1229
Gilles Peskinec8000c02019-08-02 20:15:51 +02001230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1231psa_status_t psa_get_key_slot_number(
1232 const psa_key_attributes_t *attributes,
1233 psa_key_slot_number_t *slot_number )
1234{
1235 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1236 {
1237 *slot_number = attributes->slot_number;
1238 return( PSA_SUCCESS );
1239 }
1240 else
1241 return( PSA_ERROR_INVALID_ARGUMENT );
1242}
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1244
Jaeden Ameroccdce902019-01-10 11:42:27 +00001245#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001246static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1247 unsigned char *buf, size_t size )
1248{
1249 int ret;
1250 unsigned char *c;
1251 size_t len = 0;
1252
1253 c = buf + size;
1254
1255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1256
1257 return( (int) len );
1258}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001260
Gilles Peskinef603c712019-01-19 13:40:11 +01001261static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1262 uint8_t *data,
1263 size_t data_size,
1264 size_t *data_length,
1265 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266{
Gilles Peskine5d309672019-07-12 23:47:28 +02001267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1268 const psa_drv_se_t *drv;
1269 psa_drv_se_context_t *drv_context;
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1271
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001272 *data_length = 0;
1273
Gilles Peskine8e338702019-07-30 20:06:31 +02001274 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001275 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001276
Gilles Peskinef9168942019-09-12 19:20:29 +02001277 /* Reject a zero-length output buffer now, since this can never be a
1278 * valid key representation. This way we know that data must be a valid
1279 * pointer and we can do things like memset(data, ..., data_size). */
1280 if( data_size == 0 )
1281 return( PSA_ERROR_BUFFER_TOO_SMALL );
1282
Gilles Peskine5d309672019-07-12 23:47:28 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001285 {
1286 psa_drv_se_export_key_t method;
1287 if( drv->key_management == NULL )
1288 return( PSA_ERROR_NOT_SUPPORTED );
1289 method = ( export_public_key ?
1290 drv->key_management->p_export_public :
1291 drv->key_management->p_export );
1292 if( method == NULL )
1293 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001294 return( method( drv_context,
1295 slot->data.se.slot_number,
1296 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001297 }
1298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1299
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301 {
1302 if( slot->data.raw.bytes > data_size )
1303 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001304 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1305 memset( data + slot->data.raw.bytes, 0,
1306 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 *data_length = slot->data.raw.bytes;
1308 return( PSA_SUCCESS );
1309 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001310#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001312 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001313 psa_status_t status;
1314
Gilles Peskineb46bef22019-07-30 21:32:04 +02001315 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316 if( bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
1318 status = mbedtls_to_psa_error(
1319 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1320 if( status != PSA_SUCCESS )
1321 return( status );
1322 memset( data + bytes, 0, data_size - bytes );
1323 *data_length = bytes;
1324 return( PSA_SUCCESS );
1325 }
1326#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 else
Moran Peker17e36e12018-05-02 12:55:20 +03001328 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001329#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001330 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1331 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001332 {
Moran Pekera998bc62018-04-16 18:16:20 +03001333 mbedtls_pk_context pk;
1334 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001335 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001336 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001337#if defined(MBEDTLS_RSA_C)
1338 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001339 pk.pk_info = &mbedtls_rsa_info;
1340 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#else
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001344 }
1345 else
1346 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001347#if defined(MBEDTLS_ECP_C)
1348 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001349 pk.pk_info = &mbedtls_eckey_info;
1350 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#else
1352 return( PSA_ERROR_NOT_SUPPORTED );
1353#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001354 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001355 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001356 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001357 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001358 }
Moran Peker17e36e12018-05-02 12:55:20 +03001359 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001360 {
Moran Peker17e36e12018-05-02 12:55:20 +03001361 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 }
Moran Peker60364322018-04-29 11:34:58 +03001363 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001364 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001365 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001366 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001368 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1369 * Move the data to the beginning and erase remaining data
1370 * at the original location. */
1371 if( 2 * (size_t) ret <= data_size )
1372 {
1373 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001374 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001375 }
1376 else if( (size_t) ret < data_size )
1377 {
1378 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 }
Moran Pekera998bc62018-04-16 18:16:20 +03001381 *data_length = ret;
1382 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001383 }
1384 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001385#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001386 {
1387 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001388 it is valid for a special-purpose implementation to omit
1389 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001390 return( PSA_ERROR_NOT_SUPPORTED );
1391 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001392 }
1393}
1394
Gilles Peskinec5487a82018-12-03 18:08:14 +01001395psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001396 uint8_t *data,
1397 size_t data_size,
1398 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001400 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001401 psa_status_t status;
1402
1403 /* Set the key to empty now, so that even when there are errors, we always
1404 * set data_length to a value between 0 and data_size. On error, setting
1405 * the key to empty is a good choice because an empty key representation is
1406 * unlikely to be accepted anywhere. */
1407 *data_length = 0;
1408
1409 /* Export requires the EXPORT flag. There is an exception for public keys,
1410 * which don't require any flag, but psa_get_key_from_slot takes
1411 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001412 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001413 if( status != PSA_SUCCESS )
1414 return( status );
1415 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001416 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001417}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418
Gilles Peskinec5487a82018-12-03 18:08:14 +01001419psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001420 uint8_t *data,
1421 size_t data_size,
1422 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001423{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001424 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001425 psa_status_t status;
1426
1427 /* Set the key to empty now, so that even when there are errors, we always
1428 * set data_length to a value between 0 and data_size. On error, setting
1429 * the key to empty is a good choice because an empty key representation is
1430 * unlikely to be accepted anywhere. */
1431 *data_length = 0;
1432
1433 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001434 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001435 if( status != PSA_SUCCESS )
1436 return( status );
1437 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001438 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001439}
1440
Gilles Peskine91e8c332019-08-02 19:19:39 +02001441#if defined(static_assert)
1442static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1443 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001444static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001445 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001446static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001447 "One or more key attribute flag is listed as both internal-only and external-only" );
1448#endif
1449
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001450/** Validate that a key policy is internally well-formed.
1451 *
1452 * This function only rejects invalid policies. It does not validate the
1453 * consistency of the policy with respect to other attributes of the key
1454 * such as the key type.
1455 */
1456static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001457{
1458 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001459 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 PSA_KEY_USAGE_ENCRYPT |
1461 PSA_KEY_USAGE_DECRYPT |
1462 PSA_KEY_USAGE_SIGN |
1463 PSA_KEY_USAGE_VERIFY |
1464 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1465 return( PSA_ERROR_INVALID_ARGUMENT );
1466
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 return( PSA_SUCCESS );
1468}
1469
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001470/** Validate the internal consistency of key attributes.
1471 *
1472 * This function only rejects invalid attribute values. If does not
1473 * validate the consistency of the attributes with any key data that may
1474 * be involved in the creation of the key.
1475 *
1476 * Call this function early in the key creation process.
1477 *
1478 * \param[in] attributes Key attributes for the new key.
1479 * \param[out] p_drv On any return, the driver for the key, if any.
1480 * NULL for a transparent key.
1481 *
1482 */
1483static psa_status_t psa_validate_key_attributes(
1484 const psa_key_attributes_t *attributes,
1485 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001486{
1487 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001488
1489 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001490 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491 status = psa_validate_persistent_key_parameters(
1492 attributes->core.lifetime, attributes->core.id,
1493 p_drv, 1 );
1494 if( status != PSA_SUCCESS )
1495 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001496 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001497
1498 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500 return( status );
1501
1502 /* Refuse to create overly large keys.
1503 * Note that this doesn't trigger on import if the attributes don't
1504 * explicitly specify a size (so psa_get_key_bits returns 0), so
1505 * psa_import_key() needs its own checks. */
1506 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1507 return( PSA_ERROR_NOT_SUPPORTED );
1508
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509 /* Reject invalid flags. These should not be reachable through the API. */
1510 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1511 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1512 return( PSA_ERROR_INVALID_ARGUMENT );
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( PSA_SUCCESS );
1515}
1516
Gilles Peskine4747d192019-04-17 15:05:45 +02001517/** Prepare a key slot to receive key material.
1518 *
1519 * This function allocates a key slot and sets its metadata.
1520 *
1521 * If this function fails, call psa_fail_key_creation().
1522 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001523 * This function is intended to be used as follows:
1524 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1525 * it with the specified attributes, and assign it a handle.
1526 * -# Populate the slot with the key material.
1527 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1528 * In case of failure at any step, stop the sequence and call
1529 * psa_fail_key_creation().
1530 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001531 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] handle On success, a handle for the allocated slot.
1534 * \param[out] p_slot On success, a pointer to the prepared slot.
1535 * \param[out] p_drv On any return, the driver for the key, if any.
1536 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The key slot is ready to receive key material.
1540 * \return If this function fails, the key slot is an invalid state.
1541 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 */
1543static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001544 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 const psa_key_attributes_t *attributes,
1546 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001548 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001549{
1550 psa_status_t status;
1551 psa_key_slot_t *slot;
1552
Gilles Peskinedf179142019-07-15 22:02:14 +02001553 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 *p_drv = NULL;
1555
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001556 status = psa_validate_key_attributes( attributes, p_drv );
1557 if( status != PSA_SUCCESS )
1558 return( status );
1559
Gilles Peskineedbed562019-08-07 18:19:59 +02001560 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 if( status != PSA_SUCCESS )
1562 return( status );
1563 slot = *p_slot;
1564
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001565 /* We're storing the declared bit-size of the key. It's up to each
1566 * creation mechanism to verify that this information is correct.
1567 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001568 * an input (generate, device) but not for those where the bit-size
1569 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001570
1571 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001572
Gilles Peskine91e8c332019-08-02 19:19:39 +02001573 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001574 * external-only flags, query `attributes`. Thanks to the check
1575 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001576 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * may have set. */
1578 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskinecbaff462019-07-12 23:46:04 +02001580#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001581 /* For a key in a secure element, we need to do three things
1582 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001583 * create the key file in internal storage, create the
1584 * key inside the secure element, and update the driver's
1585 * persistent data. Start a transaction that will encompass these
1586 * three actions. */
1587 /* The first thing to do is to find a slot number for the new key.
1588 * We save the slot number in persistent storage as part of the
1589 * transaction data. It will be needed to recover if the power
1590 * fails during the key creation process, to clean up on the secure
1591 * element side after restarting. Obtaining a slot number from the
1592 * secure element driver updates its persistent state, but we do not yet
1593 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001594 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001595 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Darryl Green0c6575a2018-11-07 16:05:30 +00001596 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001597 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001598 &slot->data.se.slot_number );
1599 if( status != PSA_SUCCESS )
1600 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001601 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001602 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001603 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001604 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001605 status = psa_crypto_save_transaction( );
1606 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001607 {
1608 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001609 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1613
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 return( status );
1615}
Gilles Peskine4747d192019-04-17 15:05:45 +02001616
1617/** Finalize the creation of a key once its key material has been set.
1618 *
1619 * This entails writing the key to persistent storage.
1620 *
1621 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001622 * See the documentation of psa_start_key_creation() for the intended use
1623 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001625 * \param[in,out] slot Pointer to the slot with key material.
1626 * \param[in] driver The secure element driver for the key,
1627 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001628 *
1629 * \retval #PSA_SUCCESS
1630 * The key was successfully created. The handle is now valid.
1631 * \return If this function fails, the key slot is an invalid state.
1632 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001634static psa_status_t psa_finish_key_creation(
1635 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001636 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001637{
1638 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001639 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001641
1642#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001643 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1646 if( driver != NULL )
1647 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001648 psa_se_key_data_storage_t data;
1649#if defined(static_assert)
1650 static_assert( sizeof( slot->data.se.slot_number ) ==
1651 sizeof( data.slot_number ),
1652 "Slot number size does not match psa_se_key_data_storage_t" );
1653 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1654 "Bit-size size does not match psa_se_key_data_storage_t" );
1655#endif
1656 memcpy( &data.slot_number, &slot->data.se.slot_number,
1657 sizeof( slot->data.se.slot_number ) );
1658 memcpy( &data.bits, &slot->attr.bits,
1659 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001660 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001661 (uint8_t*) &data,
1662 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001663 }
1664 else
1665#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1666 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001667 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001668 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001669 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001670 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1671 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001672 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001673 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1674 status = psa_internal_export_key( slot,
1675 buffer, buffer_size, &length,
1676 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001677 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001678 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001679 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001680
Gilles Peskinef9168942019-09-12 19:20:29 +02001681 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001682 mbedtls_free( buffer );
1683 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001685#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1686
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001688 /* Finish the transaction for a key creation. This does not
1689 * happen when registering an existing key. Detect this case
1690 * by checking whether a transaction is in progress (actual
1691 * creation of a key in a secure element requires a transaction,
1692 * but registration doesn't use one). */
1693 if( driver != NULL &&
1694 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001695 {
1696 status = psa_save_se_persistent_data( driver );
1697 if( status != PSA_SUCCESS )
1698 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001699 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001700 return( status );
1701 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001702 status = psa_crypto_stop_transaction( );
1703 if( status != PSA_SUCCESS )
1704 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001705 }
1706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1707
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 return( status );
1709}
1710
1711/** Abort the creation of a key.
1712 *
1713 * You may call this function after calling psa_start_key_creation(),
1714 * or after psa_finish_key_creation() fails. In other circumstances, this
1715 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001716 * See the documentation of psa_start_key_creation() for the intended use
1717 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001719 * \param[in,out] slot Pointer to the slot with key material.
1720 * \param[in] driver The secure element driver for the key,
1721 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001723static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001724 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001725{
Gilles Peskine011e4282019-06-26 18:34:38 +02001726 (void) driver;
1727
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 if( slot == NULL )
1729 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001730
1731#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001732 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001733 * element, and the failure happened later (when saving metadata
1734 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001735 * element.
1736 * https://github.com/ARMmbed/mbed-crypto/issues/217
1737 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001738
Gilles Peskined7729582019-08-05 15:55:54 +02001739 /* Abort the ongoing transaction if any (there may not be one if
1740 * the creation process failed before starting one, or if the
1741 * key creation is a registration of a key in a secure element).
1742 * Earlier functions must already have done what it takes to undo any
1743 * partial creation. All that's left is to update the transaction data
1744 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001745 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 psa_wipe_key_slot( slot );
1749}
1750
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001751/** Validate optional attributes during key creation.
1752 *
1753 * Some key attributes are optional during key creation. If they are
1754 * specified in the attributes structure, check that they are consistent
1755 * with the data in the slot.
1756 *
1757 * This function should be called near the end of key creation, after
1758 * the slot in memory is fully populated but before saving persistent data.
1759 */
1760static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001761 const psa_key_slot_t *slot,
1762 const psa_key_attributes_t *attributes )
1763{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001764 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001765 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001766 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001767 return( PSA_ERROR_INVALID_ARGUMENT );
1768 }
1769
1770 if( attributes->domain_parameters_size != 0 )
1771 {
1772#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001773 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001774 {
1775 mbedtls_mpi actual, required;
1776 int ret;
1777 mbedtls_mpi_init( &actual );
1778 mbedtls_mpi_init( &required );
1779 ret = mbedtls_rsa_export( slot->data.rsa,
1780 NULL, NULL, NULL, NULL, &actual );
1781 if( ret != 0 )
1782 goto rsa_exit;
1783 ret = mbedtls_mpi_read_binary( &required,
1784 attributes->domain_parameters,
1785 attributes->domain_parameters_size );
1786 if( ret != 0 )
1787 goto rsa_exit;
1788 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1789 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1790 rsa_exit:
1791 mbedtls_mpi_free( &actual );
1792 mbedtls_mpi_free( &required );
1793 if( ret != 0)
1794 return( mbedtls_to_psa_error( ret ) );
1795 }
1796 else
1797#endif
1798 {
1799 return( PSA_ERROR_INVALID_ARGUMENT );
1800 }
1801 }
1802
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001803 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001805 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001806 return( PSA_ERROR_INVALID_ARGUMENT );
1807 }
1808
1809 return( PSA_SUCCESS );
1810}
1811
Gilles Peskine4747d192019-04-17 15:05:45 +02001812psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001813 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001814 size_t data_length,
1815 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001816{
1817 psa_status_t status;
1818 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001819 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001820
Gilles Peskine0f84d622019-09-12 19:03:13 +02001821 /* Reject zero-length symmetric keys (including raw data key objects).
1822 * This also rejects any key which might be encoded as an empty string,
1823 * which is never valid. */
1824 if( data_length == 0 )
1825 return( PSA_ERROR_INVALID_ARGUMENT );
1826
Gilles Peskinedf179142019-07-15 22:02:14 +02001827 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1828 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829 if( status != PSA_SUCCESS )
1830 goto exit;
1831
Gilles Peskine5d309672019-07-12 23:47:28 +02001832#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1833 if( driver != NULL )
1834 {
1835 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001836 /* The driver should set the number of key bits, however in
1837 * case it doesn't, we initialize bits to an invalid value. */
1838 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001839 if( drv->key_management == NULL ||
1840 drv->key_management->p_import == NULL )
1841 {
1842 status = PSA_ERROR_NOT_SUPPORTED;
1843 goto exit;
1844 }
1845 status = drv->key_management->p_import(
1846 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001847 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001848 &bits );
1849 if( status != PSA_SUCCESS )
1850 goto exit;
1851 if( bits > PSA_MAX_KEY_BITS )
1852 {
1853 status = PSA_ERROR_NOT_SUPPORTED;
1854 goto exit;
1855 }
1856 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001857 }
1858 else
1859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860 {
1861 status = psa_import_key_into_slot( slot, data, data_length );
1862 if( status != PSA_SUCCESS )
1863 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001864 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001865 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001866 if( status != PSA_SUCCESS )
1867 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 if( status != PSA_SUCCESS )
1872 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001873 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 *handle = 0;
1875 }
1876 return( status );
1877}
1878
Gilles Peskined7729582019-08-05 15:55:54 +02001879#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1880psa_status_t mbedtls_psa_register_se_key(
1881 const psa_key_attributes_t *attributes )
1882{
1883 psa_status_t status;
1884 psa_key_slot_t *slot = NULL;
1885 psa_se_drv_table_entry_t *driver = NULL;
1886 const psa_drv_se_t *drv;
1887 psa_key_handle_t handle = 0;
1888
1889 /* Leaving attributes unspecified is not currently supported.
1890 * It could make sense to query the key type and size from the
1891 * secure element, but not all secure elements support this
1892 * and the driver HAL doesn't currently support it. */
1893 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1894 return( PSA_ERROR_NOT_SUPPORTED );
1895 if( psa_get_key_bits( attributes ) == 0 )
1896 return( PSA_ERROR_NOT_SUPPORTED );
1897
1898 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1899 &handle, &slot, &driver );
1900 if( status != PSA_SUCCESS )
1901 goto exit;
1902
1903 if( driver == NULL )
1904 {
1905 status = PSA_ERROR_INVALID_ARGUMENT;
1906 goto exit;
1907 }
1908 drv = psa_get_se_driver_methods( driver );
1909
1910 if ( psa_get_key_slot_number( attributes,
1911 &slot->data.se.slot_number ) != PSA_SUCCESS )
1912 {
1913 /* The application didn't specify a slot number. This doesn't
1914 * make sense when registering a slot. */
1915 status = PSA_ERROR_INVALID_ARGUMENT;
1916 goto exit;
1917 }
1918
Gilles Peskinea5f87492019-08-05 16:46:18 +02001919 /* If the driver has a slot number validation method, call it.
1920 * If it doesn't, it means the secure element is unable to validate
1921 * anything and so we have to trust the application. */
1922 if( drv->key_management != NULL &&
1923 drv->key_management->p_validate_slot_number != NULL )
1924 {
1925 status = drv->key_management->p_validate_slot_number(
1926 psa_get_se_driver_context( driver ),
1927 attributes,
1928 PSA_KEY_CREATION_REGISTER,
1929 slot->data.se.slot_number );
1930 if( status != PSA_SUCCESS )
1931 goto exit;
1932 }
1933
Gilles Peskined7729582019-08-05 15:55:54 +02001934 status = psa_finish_key_creation( slot, driver );
1935
1936exit:
1937 if( status != PSA_SUCCESS )
1938 {
1939 psa_fail_key_creation( slot, driver );
1940 }
1941 /* Registration doesn't keep the key in RAM. */
1942 psa_close_key( handle );
1943 return( status );
1944}
1945#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1946
Gilles Peskinef603c712019-01-19 13:40:11 +01001947static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001948 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001949{
1950 psa_status_t status;
1951 uint8_t *buffer = NULL;
1952 size_t buffer_size = 0;
1953 size_t length;
1954
Gilles Peskine8e338702019-07-30 20:06:31 +02001955 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001956 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001957 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001958 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001959 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001960 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1961 if( status != PSA_SUCCESS )
1962 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001963 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001964 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001965
1966exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001967 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001968 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001969 return( status );
1970}
1971
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001972psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1973 const psa_key_attributes_t *specified_attributes,
1974 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001975{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001976 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001977 psa_key_slot_t *source_slot = NULL;
1978 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001980 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001981
Gilles Peskine28f8f302019-07-24 13:30:31 +02001982 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001983 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001984 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 goto exit;
1986
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001987 status = psa_validate_optional_attributes( source_slot,
1988 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 if( status != PSA_SUCCESS )
1990 goto exit;
1991
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001992 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001993 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 if( status != PSA_SUCCESS )
1995 goto exit;
1996
Gilles Peskinedf179142019-07-15 22:02:14 +02001997 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1998 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001999 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 if( status != PSA_SUCCESS )
2001 goto exit;
2002
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002003#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2004 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002005 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002006 /* Copying to a secure element is not implemented yet. */
2007 status = PSA_ERROR_NOT_SUPPORTED;
2008 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002009 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002010#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002011
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002013 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002015
Gilles Peskine011e4282019-06-26 18:34:38 +02002016 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002017exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 if( status != PSA_SUCCESS )
2019 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002020 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 *target_handle = 0;
2022 }
2023 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002024}
2025
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002026
2027
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002028/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002029/* Message digests */
2030/****************************************************************/
2031
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002032static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002033{
2034 switch( alg )
2035 {
2036#if defined(MBEDTLS_MD2_C)
2037 case PSA_ALG_MD2:
2038 return( &mbedtls_md2_info );
2039#endif
2040#if defined(MBEDTLS_MD4_C)
2041 case PSA_ALG_MD4:
2042 return( &mbedtls_md4_info );
2043#endif
2044#if defined(MBEDTLS_MD5_C)
2045 case PSA_ALG_MD5:
2046 return( &mbedtls_md5_info );
2047#endif
2048#if defined(MBEDTLS_RIPEMD160_C)
2049 case PSA_ALG_RIPEMD160:
2050 return( &mbedtls_ripemd160_info );
2051#endif
2052#if defined(MBEDTLS_SHA1_C)
2053 case PSA_ALG_SHA_1:
2054 return( &mbedtls_sha1_info );
2055#endif
2056#if defined(MBEDTLS_SHA256_C)
2057 case PSA_ALG_SHA_224:
2058 return( &mbedtls_sha224_info );
2059 case PSA_ALG_SHA_256:
2060 return( &mbedtls_sha256_info );
2061#endif
2062#if defined(MBEDTLS_SHA512_C)
2063 case PSA_ALG_SHA_384:
2064 return( &mbedtls_sha384_info );
2065 case PSA_ALG_SHA_512:
2066 return( &mbedtls_sha512_info );
2067#endif
2068 default:
2069 return( NULL );
2070 }
2071}
2072
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002073psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2074{
2075 switch( operation->alg )
2076 {
Gilles Peskine81736312018-06-26 15:04:31 +02002077 case 0:
2078 /* The object has (apparently) been initialized but it is not
2079 * in use. It's ok to call abort on such an object, and there's
2080 * nothing to do. */
2081 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082#if defined(MBEDTLS_MD2_C)
2083 case PSA_ALG_MD2:
2084 mbedtls_md2_free( &operation->ctx.md2 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_MD4_C)
2088 case PSA_ALG_MD4:
2089 mbedtls_md4_free( &operation->ctx.md4 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_MD5_C)
2093 case PSA_ALG_MD5:
2094 mbedtls_md5_free( &operation->ctx.md5 );
2095 break;
2096#endif
2097#if defined(MBEDTLS_RIPEMD160_C)
2098 case PSA_ALG_RIPEMD160:
2099 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2100 break;
2101#endif
2102#if defined(MBEDTLS_SHA1_C)
2103 case PSA_ALG_SHA_1:
2104 mbedtls_sha1_free( &operation->ctx.sha1 );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA256_C)
2108 case PSA_ALG_SHA_224:
2109 case PSA_ALG_SHA_256:
2110 mbedtls_sha256_free( &operation->ctx.sha256 );
2111 break;
2112#endif
2113#if defined(MBEDTLS_SHA512_C)
2114 case PSA_ALG_SHA_384:
2115 case PSA_ALG_SHA_512:
2116 mbedtls_sha512_free( &operation->ctx.sha512 );
2117 break;
2118#endif
2119 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002120 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 }
2122 operation->alg = 0;
2123 return( PSA_SUCCESS );
2124}
2125
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002126psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127 psa_algorithm_t alg )
2128{
2129 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002130
2131 /* A context must be freshly initialized before it can be set up. */
2132 if( operation->alg != 0 )
2133 {
2134 return( PSA_ERROR_BAD_STATE );
2135 }
2136
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002137 switch( alg )
2138 {
2139#if defined(MBEDTLS_MD2_C)
2140 case PSA_ALG_MD2:
2141 mbedtls_md2_init( &operation->ctx.md2 );
2142 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_MD4_C)
2146 case PSA_ALG_MD4:
2147 mbedtls_md4_init( &operation->ctx.md4 );
2148 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_MD5_C)
2152 case PSA_ALG_MD5:
2153 mbedtls_md5_init( &operation->ctx.md5 );
2154 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2155 break;
2156#endif
2157#if defined(MBEDTLS_RIPEMD160_C)
2158 case PSA_ALG_RIPEMD160:
2159 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2160 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA1_C)
2164 case PSA_ALG_SHA_1:
2165 mbedtls_sha1_init( &operation->ctx.sha1 );
2166 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2167 break;
2168#endif
2169#if defined(MBEDTLS_SHA256_C)
2170 case PSA_ALG_SHA_224:
2171 mbedtls_sha256_init( &operation->ctx.sha256 );
2172 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2173 break;
2174 case PSA_ALG_SHA_256:
2175 mbedtls_sha256_init( &operation->ctx.sha256 );
2176 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2177 break;
2178#endif
2179#if defined(MBEDTLS_SHA512_C)
2180 case PSA_ALG_SHA_384:
2181 mbedtls_sha512_init( &operation->ctx.sha512 );
2182 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2183 break;
2184 case PSA_ALG_SHA_512:
2185 mbedtls_sha512_init( &operation->ctx.sha512 );
2186 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2187 break;
2188#endif
2189 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002190 return( PSA_ALG_IS_HASH( alg ) ?
2191 PSA_ERROR_NOT_SUPPORTED :
2192 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 }
2194 if( ret == 0 )
2195 operation->alg = alg;
2196 else
2197 psa_hash_abort( operation );
2198 return( mbedtls_to_psa_error( ret ) );
2199}
2200
2201psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2202 const uint8_t *input,
2203 size_t input_length )
2204{
2205 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002206
2207 /* Don't require hash implementations to behave correctly on a
2208 * zero-length input, which may have an invalid pointer. */
2209 if( input_length == 0 )
2210 return( PSA_SUCCESS );
2211
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212 switch( operation->alg )
2213 {
2214#if defined(MBEDTLS_MD2_C)
2215 case PSA_ALG_MD2:
2216 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2217 input, input_length );
2218 break;
2219#endif
2220#if defined(MBEDTLS_MD4_C)
2221 case PSA_ALG_MD4:
2222 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2223 input, input_length );
2224 break;
2225#endif
2226#if defined(MBEDTLS_MD5_C)
2227 case PSA_ALG_MD5:
2228 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2229 input, input_length );
2230 break;
2231#endif
2232#if defined(MBEDTLS_RIPEMD160_C)
2233 case PSA_ALG_RIPEMD160:
2234 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2235 input, input_length );
2236 break;
2237#endif
2238#if defined(MBEDTLS_SHA1_C)
2239 case PSA_ALG_SHA_1:
2240 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2241 input, input_length );
2242 break;
2243#endif
2244#if defined(MBEDTLS_SHA256_C)
2245 case PSA_ALG_SHA_224:
2246 case PSA_ALG_SHA_256:
2247 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2248 input, input_length );
2249 break;
2250#endif
2251#if defined(MBEDTLS_SHA512_C)
2252 case PSA_ALG_SHA_384:
2253 case PSA_ALG_SHA_512:
2254 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2255 input, input_length );
2256 break;
2257#endif
2258 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002259 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002261
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 if( ret != 0 )
2263 psa_hash_abort( operation );
2264 return( mbedtls_to_psa_error( ret ) );
2265}
2266
2267psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2268 uint8_t *hash,
2269 size_t hash_size,
2270 size_t *hash_length )
2271{
itayzafrir40835d42018-08-02 13:14:17 +03002272 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002274 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275
2276 /* Fill the output buffer with something that isn't a valid hash
2277 * (barring an attack on the hash and deliberately-crafted input),
2278 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002279 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002280 /* If hash_size is 0 then hash may be NULL and then the
2281 * call to memset would have undefined behavior. */
2282 if( hash_size != 0 )
2283 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284
2285 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002286 {
2287 status = PSA_ERROR_BUFFER_TOO_SMALL;
2288 goto exit;
2289 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290
2291 switch( operation->alg )
2292 {
2293#if defined(MBEDTLS_MD2_C)
2294 case PSA_ALG_MD2:
2295 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_MD4_C)
2299 case PSA_ALG_MD4:
2300 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_MD5_C)
2304 case PSA_ALG_MD5:
2305 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_RIPEMD160_C)
2309 case PSA_ALG_RIPEMD160:
2310 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2311 break;
2312#endif
2313#if defined(MBEDTLS_SHA1_C)
2314 case PSA_ALG_SHA_1:
2315 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2316 break;
2317#endif
2318#if defined(MBEDTLS_SHA256_C)
2319 case PSA_ALG_SHA_224:
2320 case PSA_ALG_SHA_256:
2321 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA512_C)
2325 case PSA_ALG_SHA_384:
2326 case PSA_ALG_SHA_512:
2327 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2328 break;
2329#endif
2330 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002331 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 }
itayzafrir40835d42018-08-02 13:14:17 +03002333 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334
itayzafrir40835d42018-08-02 13:14:17 +03002335exit:
2336 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002338 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 return( psa_hash_abort( operation ) );
2340 }
2341 else
2342 {
2343 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002344 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 }
2346}
2347
Gilles Peskine2d277862018-06-18 15:41:12 +02002348psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2349 const uint8_t *hash,
2350 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351{
2352 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2353 size_t actual_hash_length;
2354 psa_status_t status = psa_hash_finish( operation,
2355 actual_hash, sizeof( actual_hash ),
2356 &actual_hash_length );
2357 if( status != PSA_SUCCESS )
2358 return( status );
2359 if( actual_hash_length != hash_length )
2360 return( PSA_ERROR_INVALID_SIGNATURE );
2361 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2362 return( PSA_ERROR_INVALID_SIGNATURE );
2363 return( PSA_SUCCESS );
2364}
2365
Gilles Peskineeb35d782019-01-22 17:56:16 +01002366psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2367 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002368{
2369 if( target_operation->alg != 0 )
2370 return( PSA_ERROR_BAD_STATE );
2371
2372 switch( source_operation->alg )
2373 {
2374 case 0:
2375 return( PSA_ERROR_BAD_STATE );
2376#if defined(MBEDTLS_MD2_C)
2377 case PSA_ALG_MD2:
2378 mbedtls_md2_clone( &target_operation->ctx.md2,
2379 &source_operation->ctx.md2 );
2380 break;
2381#endif
2382#if defined(MBEDTLS_MD4_C)
2383 case PSA_ALG_MD4:
2384 mbedtls_md4_clone( &target_operation->ctx.md4,
2385 &source_operation->ctx.md4 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_MD5_C)
2389 case PSA_ALG_MD5:
2390 mbedtls_md5_clone( &target_operation->ctx.md5,
2391 &source_operation->ctx.md5 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_RIPEMD160_C)
2395 case PSA_ALG_RIPEMD160:
2396 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2397 &source_operation->ctx.ripemd160 );
2398 break;
2399#endif
2400#if defined(MBEDTLS_SHA1_C)
2401 case PSA_ALG_SHA_1:
2402 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2403 &source_operation->ctx.sha1 );
2404 break;
2405#endif
2406#if defined(MBEDTLS_SHA256_C)
2407 case PSA_ALG_SHA_224:
2408 case PSA_ALG_SHA_256:
2409 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2410 &source_operation->ctx.sha256 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_SHA512_C)
2414 case PSA_ALG_SHA_384:
2415 case PSA_ALG_SHA_512:
2416 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2417 &source_operation->ctx.sha512 );
2418 break;
2419#endif
2420 default:
2421 return( PSA_ERROR_NOT_SUPPORTED );
2422 }
2423
2424 target_operation->alg = source_operation->alg;
2425 return( PSA_SUCCESS );
2426}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002427
2428
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430/* MAC */
2431/****************************************************************/
2432
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002433static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434 psa_algorithm_t alg,
2435 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002436 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002437 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002440 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002442 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002443 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002444
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2446 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002447 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002449 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002450 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 mode = MBEDTLS_MODE_STREAM;
2452 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 case PSA_ALG_CTR:
2454 mode = MBEDTLS_MODE_CTR;
2455 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002456 case PSA_ALG_CFB:
2457 mode = MBEDTLS_MODE_CFB;
2458 break;
2459 case PSA_ALG_OFB:
2460 mode = MBEDTLS_MODE_OFB;
2461 break;
2462 case PSA_ALG_CBC_NO_PADDING:
2463 mode = MBEDTLS_MODE_CBC;
2464 break;
2465 case PSA_ALG_CBC_PKCS7:
2466 mode = MBEDTLS_MODE_CBC;
2467 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002468 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469 mode = MBEDTLS_MODE_CCM;
2470 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002471 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 mode = MBEDTLS_MODE_GCM;
2473 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002474 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2475 mode = MBEDTLS_MODE_CHACHAPOLY;
2476 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 default:
2478 return( NULL );
2479 }
2480 }
2481 else if( alg == PSA_ALG_CMAC )
2482 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 else
2484 return( NULL );
2485
2486 switch( key_type )
2487 {
2488 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002489 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 break;
2491 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002492 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2493 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002495 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002497 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002498 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2499 * but two-key Triple-DES is functionally three-key Triple-DES
2500 * with K1=K3, so that's how we present it to mbedtls. */
2501 if( key_bits == 128 )
2502 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 break;
2504 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002505 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 break;
2507 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002508 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002510 case PSA_KEY_TYPE_CHACHA20:
2511 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2512 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 default:
2514 return( NULL );
2515 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002516 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002517 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518
Jaeden Amero23bbb752018-06-26 14:16:54 +01002519 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2520 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521}
2522
Gilles Peskinea05219c2018-11-16 16:02:56 +01002523#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002524static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002525{
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002527 {
2528 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002529 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002530 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002531 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002532 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002533 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002534 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002535 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002536 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002537 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002538 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002539 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002540 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002541 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002542 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002543 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002544 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002545 return( 128 );
2546 default:
2547 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002548 }
2549}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002550#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002551
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552/* Initialize the MAC operation structure. Once this function has been
2553 * called, psa_mac_abort can run and will do the right thing. */
2554static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2555 psa_algorithm_t alg )
2556{
2557 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2558
2559 operation->alg = alg;
2560 operation->key_set = 0;
2561 operation->iv_set = 0;
2562 operation->iv_required = 0;
2563 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002564 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002565
2566#if defined(MBEDTLS_CMAC_C)
2567 if( alg == PSA_ALG_CMAC )
2568 {
2569 operation->iv_required = 0;
2570 mbedtls_cipher_init( &operation->ctx.cmac );
2571 status = PSA_SUCCESS;
2572 }
2573 else
2574#endif /* MBEDTLS_CMAC_C */
2575#if defined(MBEDTLS_MD_C)
2576 if( PSA_ALG_IS_HMAC( operation->alg ) )
2577 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002578 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2579 operation->ctx.hmac.hash_ctx.alg = 0;
2580 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581 }
2582 else
2583#endif /* MBEDTLS_MD_C */
2584 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002585 if( ! PSA_ALG_IS_MAC( alg ) )
2586 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002587 }
2588
2589 if( status != PSA_SUCCESS )
2590 memset( operation, 0, sizeof( *operation ) );
2591 return( status );
2592}
2593
Gilles Peskine01126fa2018-07-12 17:04:55 +02002594#if defined(MBEDTLS_MD_C)
2595static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2596{
Gilles Peskine3f108122018-12-07 18:14:53 +01002597 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002598 return( psa_hash_abort( &hmac->hash_ctx ) );
2599}
2600#endif /* MBEDTLS_MD_C */
2601
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2603{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002604 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002606 /* The object has (apparently) been initialized but it is not
2607 * in use. It's ok to call abort on such an object, and there's
2608 * nothing to do. */
2609 return( PSA_SUCCESS );
2610 }
2611 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 if( operation->alg == PSA_ALG_CMAC )
2614 {
2615 mbedtls_cipher_free( &operation->ctx.cmac );
2616 }
2617 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002620 if( PSA_ALG_IS_HMAC( operation->alg ) )
2621 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002622 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 }
2624 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002626 {
2627 /* Sanity check (shouldn't happen: operation->alg should
2628 * always have been initialized to a valid value). */
2629 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630 }
Moran Peker41deec42018-04-04 15:43:05 +03002631
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632 operation->alg = 0;
2633 operation->key_set = 0;
2634 operation->iv_set = 0;
2635 operation->iv_required = 0;
2636 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002637 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002638
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002640
2641bad_state:
2642 /* If abort is called on an uninitialized object, we can't trust
2643 * anything. Wipe the object in case it contains confidential data.
2644 * This may result in a memory leak if a pointer gets overwritten,
2645 * but it's too late to do anything about this. */
2646 memset( operation, 0, sizeof( *operation ) );
2647 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648}
2649
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002650#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002651static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002653 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002654 const mbedtls_cipher_info_t *cipher_info )
2655{
2656 int ret;
2657
2658 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002659
2660 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2661 if( ret != 0 )
2662 return( ret );
2663
2664 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2665 slot->data.raw.data,
2666 key_bits );
2667 return( ret );
2668}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002669#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002670
Gilles Peskine248051a2018-06-20 16:09:38 +02002671#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002672static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2673 const uint8_t *key,
2674 size_t key_length,
2675 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002677 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002678 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002679 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002680 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681 psa_status_t status;
2682
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002683 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2684 * overflow below. This should never trigger if the hash algorithm
2685 * is implemented correctly. */
2686 /* The size checks against the ipad and opad buffers cannot be written
2687 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2688 * because that triggers -Wlogical-op on GCC 7.3. */
2689 if( block_size > sizeof( ipad ) )
2690 return( PSA_ERROR_NOT_SUPPORTED );
2691 if( block_size > sizeof( hmac->opad ) )
2692 return( PSA_ERROR_NOT_SUPPORTED );
2693 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694 return( PSA_ERROR_NOT_SUPPORTED );
2695
Gilles Peskined223b522018-06-11 18:12:58 +02002696 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002698 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002699 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002700 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002702 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002703 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002705 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002707 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 }
Gilles Peskine96889972018-07-12 17:07:03 +02002709 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2710 * but it is permitted. It is common when HMAC is used in HKDF, for
2711 * example. Don't call `memcpy` in the 0-length because `key` could be
2712 * an invalid pointer which would make the behavior undefined. */
2713 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715
Gilles Peskined223b522018-06-11 18:12:58 +02002716 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2717 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002719 ipad[i] ^= 0x36;
2720 memset( ipad + key_length, 0x36, block_size - key_length );
2721
2722 /* Copy the key material from ipad to opad, flipping the requisite bits,
2723 * and filling the rest of opad with the requisite constant. */
2724 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002725 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2726 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727
Gilles Peskine01126fa2018-07-12 17:04:55 +02002728 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002730 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002731
Gilles Peskine01126fa2018-07-12 17:04:55 +02002732 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002733
2734cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002735 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002736
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737 return( status );
2738}
Gilles Peskine248051a2018-06-20 16:09:38 +02002739#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740
Gilles Peskine89167cb2018-07-08 20:12:23 +02002741static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002742 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002743 psa_algorithm_t alg,
2744 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002747 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002749 psa_key_usage_t usage =
2750 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002751 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002752 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002754 /* A context must be freshly initialized before it can be set up. */
2755 if( operation->alg != 0 )
2756 {
2757 return( PSA_ERROR_BAD_STATE );
2758 }
2759
Gilles Peskined911eb72018-08-14 15:18:45 +02002760 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002761 if( status != PSA_SUCCESS )
2762 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002763 if( is_sign )
2764 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765
Gilles Peskine28f8f302019-07-24 13:30:31 +02002766 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002767 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002768 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002769 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002770
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002772 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 {
2774 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002775 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002776 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002777 int ret;
2778 if( cipher_info == NULL )
2779 {
2780 status = PSA_ERROR_NOT_SUPPORTED;
2781 goto exit;
2782 }
2783 operation->mac_size = cipher_info->block_size;
2784 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2785 status = mbedtls_to_psa_error( ret );
2786 }
2787 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002790 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002791 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002792 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002793 if( hash_alg == 0 )
2794 {
2795 status = PSA_ERROR_NOT_SUPPORTED;
2796 goto exit;
2797 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002798
2799 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2800 /* Sanity check. This shouldn't fail on a valid configuration. */
2801 if( operation->mac_size == 0 ||
2802 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2803 {
2804 status = PSA_ERROR_NOT_SUPPORTED;
2805 goto exit;
2806 }
2807
Gilles Peskine8e338702019-07-30 20:06:31 +02002808 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002809 {
2810 status = PSA_ERROR_INVALID_ARGUMENT;
2811 goto exit;
2812 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002813
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2815 slot->data.raw.data,
2816 slot->data.raw.bytes,
2817 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 }
2819 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002820#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002821 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002822 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002825
Gilles Peskined911eb72018-08-14 15:18:45 +02002826 if( truncated == 0 )
2827 {
2828 /* The "normal" case: untruncated algorithm. Nothing to do. */
2829 }
2830 else if( truncated < 4 )
2831 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002832 /* A very short MAC is too short for security since it can be
2833 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2834 * so we make this our minimum, even though 32 bits is still
2835 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002836 status = PSA_ERROR_NOT_SUPPORTED;
2837 }
2838 else if( truncated > operation->mac_size )
2839 {
2840 /* It's impossible to "truncate" to a larger length. */
2841 status = PSA_ERROR_INVALID_ARGUMENT;
2842 }
2843 else
2844 operation->mac_size = truncated;
2845
Gilles Peskinefbfac682018-07-08 20:51:54 +02002846exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002847 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002849 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002851 else
2852 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002853 operation->key_set = 1;
2854 }
2855 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856}
2857
Gilles Peskine89167cb2018-07-08 20:12:23 +02002858psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002859 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002860 psa_algorithm_t alg )
2861{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002862 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002863}
2864
2865psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002866 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002867 psa_algorithm_t alg )
2868{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002869 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002870}
2871
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2873 const uint8_t *input,
2874 size_t input_length )
2875{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002878 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002880 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881 operation->has_input = 1;
2882
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002884 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002885 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002886 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2887 input, input_length );
2888 status = mbedtls_to_psa_error( ret );
2889 }
2890 else
2891#endif /* MBEDTLS_CMAC_C */
2892#if defined(MBEDTLS_MD_C)
2893 if( PSA_ALG_IS_HMAC( operation->alg ) )
2894 {
2895 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2896 input_length );
2897 }
2898 else
2899#endif /* MBEDTLS_MD_C */
2900 {
2901 /* This shouldn't happen if `operation` was initialized by
2902 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002903 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002904 }
2905
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 if( status != PSA_SUCCESS )
2907 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002908 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909}
2910
Gilles Peskine01126fa2018-07-12 17:04:55 +02002911#if defined(MBEDTLS_MD_C)
2912static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2913 uint8_t *mac,
2914 size_t mac_size )
2915{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002916 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002917 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2918 size_t hash_size = 0;
2919 size_t block_size = psa_get_hash_block_size( hash_alg );
2920 psa_status_t status;
2921
Gilles Peskine01126fa2018-07-12 17:04:55 +02002922 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2923 if( status != PSA_SUCCESS )
2924 return( status );
2925 /* From here on, tmp needs to be wiped. */
2926
2927 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2928 if( status != PSA_SUCCESS )
2929 goto exit;
2930
2931 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2932 if( status != PSA_SUCCESS )
2933 goto exit;
2934
2935 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2936 if( status != PSA_SUCCESS )
2937 goto exit;
2938
Gilles Peskined911eb72018-08-14 15:18:45 +02002939 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2940 if( status != PSA_SUCCESS )
2941 goto exit;
2942
2943 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002944
2945exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002946 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947 return( status );
2948}
2949#endif /* MBEDTLS_MD_C */
2950
mohammad16036df908f2018-04-02 08:34:15 -07002951static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002952 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002953 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002955 if( ! operation->key_set )
2956 return( PSA_ERROR_BAD_STATE );
2957 if( operation->iv_required && ! operation->iv_set )
2958 return( PSA_ERROR_BAD_STATE );
2959
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960 if( mac_size < operation->mac_size )
2961 return( PSA_ERROR_BUFFER_TOO_SMALL );
2962
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002964 if( operation->alg == PSA_ALG_CMAC )
2965 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002966 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2967 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2968 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002969 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002970 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002971 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002973 else
2974#endif /* MBEDTLS_CMAC_C */
2975#if defined(MBEDTLS_MD_C)
2976 if( PSA_ALG_IS_HMAC( operation->alg ) )
2977 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002978 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002979 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002980 }
2981 else
2982#endif /* MBEDTLS_MD_C */
2983 {
2984 /* This shouldn't happen if `operation` was initialized by
2985 * a setup function. */
2986 return( PSA_ERROR_BAD_STATE );
2987 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988}
2989
Gilles Peskineacd4be32018-07-08 19:56:25 +02002990psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2991 uint8_t *mac,
2992 size_t mac_size,
2993 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002994{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002995 psa_status_t status;
2996
Jaeden Amero252ef282019-02-15 14:05:35 +00002997 if( operation->alg == 0 )
2998 {
2999 return( PSA_ERROR_BAD_STATE );
3000 }
3001
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003002 /* Fill the output buffer with something that isn't a valid mac
3003 * (barring an attack on the mac and deliberately-crafted input),
3004 * in case the caller doesn't check the return status properly. */
3005 *mac_length = mac_size;
3006 /* If mac_size is 0 then mac may be NULL and then the
3007 * call to memset would have undefined behavior. */
3008 if( mac_size != 0 )
3009 memset( mac, '!', mac_size );
3010
Gilles Peskine89167cb2018-07-08 20:12:23 +02003011 if( ! operation->is_sign )
3012 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003013 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003014 }
mohammad16036df908f2018-04-02 08:34:15 -07003015
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003016 status = psa_mac_finish_internal( operation, mac, mac_size );
3017
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003018 if( status == PSA_SUCCESS )
3019 {
3020 status = psa_mac_abort( operation );
3021 if( status == PSA_SUCCESS )
3022 *mac_length = operation->mac_size;
3023 else
3024 memset( mac, '!', mac_size );
3025 }
3026 else
3027 psa_mac_abort( operation );
3028 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003029}
3030
Gilles Peskineacd4be32018-07-08 19:56:25 +02003031psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3032 const uint8_t *mac,
3033 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034{
Gilles Peskine828ed142018-06-18 23:25:51 +02003035 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003036 psa_status_t status;
3037
Jaeden Amero252ef282019-02-15 14:05:35 +00003038 if( operation->alg == 0 )
3039 {
3040 return( PSA_ERROR_BAD_STATE );
3041 }
3042
Gilles Peskine89167cb2018-07-08 20:12:23 +02003043 if( operation->is_sign )
3044 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003045 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003046 }
3047 if( operation->mac_size != mac_length )
3048 {
3049 status = PSA_ERROR_INVALID_SIGNATURE;
3050 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003051 }
mohammad16036df908f2018-04-02 08:34:15 -07003052
3053 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003054 actual_mac, sizeof( actual_mac ) );
3055
3056 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3057 status = PSA_ERROR_INVALID_SIGNATURE;
3058
3059cleanup:
3060 if( status == PSA_SUCCESS )
3061 status = psa_mac_abort( operation );
3062 else
3063 psa_mac_abort( operation );
3064
Gilles Peskine3f108122018-12-07 18:14:53 +01003065 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003066
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003067 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068}
3069
3070
Gilles Peskine20035e32018-02-03 22:44:14 +01003071
Gilles Peskine20035e32018-02-03 22:44:14 +01003072/****************************************************************/
3073/* Asymmetric cryptography */
3074/****************************************************************/
3075
Gilles Peskine2b450e32018-06-27 15:42:46 +02003076#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003077/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003078 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003079static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3080 size_t hash_length,
3081 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003082{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003083 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003084 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003085 *md_alg = mbedtls_md_get_type( md_info );
3086
3087 /* The Mbed TLS RSA module uses an unsigned int for hash length
3088 * parameters. Validate that it fits so that we don't risk an
3089 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003090#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003091 if( hash_length > UINT_MAX )
3092 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003093#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003094
3095#if defined(MBEDTLS_PKCS1_V15)
3096 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3097 * must be correct. */
3098 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3099 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003100 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003101 if( md_info == NULL )
3102 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003103 if( mbedtls_md_get_size( md_info ) != hash_length )
3104 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003105 }
3106#endif /* MBEDTLS_PKCS1_V15 */
3107
3108#if defined(MBEDTLS_PKCS1_V21)
3109 /* PSS requires a hash internally. */
3110 if( PSA_ALG_IS_RSA_PSS( alg ) )
3111 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003112 if( md_info == NULL )
3113 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003114 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003115#endif /* MBEDTLS_PKCS1_V21 */
3116
Gilles Peskine61b91d42018-06-08 16:09:36 +02003117 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003118}
3119
Gilles Peskine2b450e32018-06-27 15:42:46 +02003120static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3121 psa_algorithm_t alg,
3122 const uint8_t *hash,
3123 size_t hash_length,
3124 uint8_t *signature,
3125 size_t signature_size,
3126 size_t *signature_length )
3127{
3128 psa_status_t status;
3129 int ret;
3130 mbedtls_md_type_t md_alg;
3131
3132 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3133 if( status != PSA_SUCCESS )
3134 return( status );
3135
Gilles Peskine630a18a2018-06-29 17:49:35 +02003136 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003137 return( PSA_ERROR_BUFFER_TOO_SMALL );
3138
3139#if defined(MBEDTLS_PKCS1_V15)
3140 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3141 {
3142 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3143 MBEDTLS_MD_NONE );
3144 ret = mbedtls_rsa_pkcs1_sign( rsa,
3145 mbedtls_ctr_drbg_random,
3146 &global_data.ctr_drbg,
3147 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003148 md_alg,
3149 (unsigned int) hash_length,
3150 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003151 signature );
3152 }
3153 else
3154#endif /* MBEDTLS_PKCS1_V15 */
3155#if defined(MBEDTLS_PKCS1_V21)
3156 if( PSA_ALG_IS_RSA_PSS( alg ) )
3157 {
3158 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3159 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3160 mbedtls_ctr_drbg_random,
3161 &global_data.ctr_drbg,
3162 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003163 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003164 (unsigned int) hash_length,
3165 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003166 signature );
3167 }
3168 else
3169#endif /* MBEDTLS_PKCS1_V21 */
3170 {
3171 return( PSA_ERROR_INVALID_ARGUMENT );
3172 }
3173
3174 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003175 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003176 return( mbedtls_to_psa_error( ret ) );
3177}
3178
3179static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3180 psa_algorithm_t alg,
3181 const uint8_t *hash,
3182 size_t hash_length,
3183 const uint8_t *signature,
3184 size_t signature_length )
3185{
3186 psa_status_t status;
3187 int ret;
3188 mbedtls_md_type_t md_alg;
3189
3190 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3191 if( status != PSA_SUCCESS )
3192 return( status );
3193
Gilles Peskine630a18a2018-06-29 17:49:35 +02003194 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003195 return( PSA_ERROR_BUFFER_TOO_SMALL );
3196
3197#if defined(MBEDTLS_PKCS1_V15)
3198 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3199 {
3200 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3201 MBEDTLS_MD_NONE );
3202 ret = mbedtls_rsa_pkcs1_verify( rsa,
3203 mbedtls_ctr_drbg_random,
3204 &global_data.ctr_drbg,
3205 MBEDTLS_RSA_PUBLIC,
3206 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003207 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003208 hash,
3209 signature );
3210 }
3211 else
3212#endif /* MBEDTLS_PKCS1_V15 */
3213#if defined(MBEDTLS_PKCS1_V21)
3214 if( PSA_ALG_IS_RSA_PSS( alg ) )
3215 {
3216 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3217 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3218 mbedtls_ctr_drbg_random,
3219 &global_data.ctr_drbg,
3220 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003221 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003222 (unsigned int) hash_length,
3223 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003224 signature );
3225 }
3226 else
3227#endif /* MBEDTLS_PKCS1_V21 */
3228 {
3229 return( PSA_ERROR_INVALID_ARGUMENT );
3230 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003231
3232 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3233 * the rest of the signature is invalid". This has little use in
3234 * practice and PSA doesn't report this distinction. */
3235 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3236 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003237 return( mbedtls_to_psa_error( ret ) );
3238}
3239#endif /* MBEDTLS_RSA_C */
3240
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003241#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003242/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3243 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3244 * (even though these functions don't modify it). */
3245static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3246 psa_algorithm_t alg,
3247 const uint8_t *hash,
3248 size_t hash_length,
3249 uint8_t *signature,
3250 size_t signature_size,
3251 size_t *signature_length )
3252{
3253 int ret;
3254 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003255 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003256 mbedtls_mpi_init( &r );
3257 mbedtls_mpi_init( &s );
3258
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003259 if( signature_size < 2 * curve_bytes )
3260 {
3261 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3262 goto cleanup;
3263 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003264
Gilles Peskinea05219c2018-11-16 16:02:56 +01003265#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003266 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3267 {
3268 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3269 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3270 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003271 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3272 &ecp->d, hash,
3273 hash_length, md_alg,
3274 mbedtls_ctr_drbg_random,
3275 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003276 }
3277 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003278#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003279 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003280 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003281 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3282 hash, hash_length,
3283 mbedtls_ctr_drbg_random,
3284 &global_data.ctr_drbg ) );
3285 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003286
3287 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3288 signature,
3289 curve_bytes ) );
3290 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3291 signature + curve_bytes,
3292 curve_bytes ) );
3293
3294cleanup:
3295 mbedtls_mpi_free( &r );
3296 mbedtls_mpi_free( &s );
3297 if( ret == 0 )
3298 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003299 return( mbedtls_to_psa_error( ret ) );
3300}
3301
3302static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3303 const uint8_t *hash,
3304 size_t hash_length,
3305 const uint8_t *signature,
3306 size_t signature_length )
3307{
3308 int ret;
3309 mbedtls_mpi r, s;
3310 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3311 mbedtls_mpi_init( &r );
3312 mbedtls_mpi_init( &s );
3313
3314 if( signature_length != 2 * curve_bytes )
3315 return( PSA_ERROR_INVALID_SIGNATURE );
3316
3317 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3318 signature,
3319 curve_bytes ) );
3320 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3321 signature + curve_bytes,
3322 curve_bytes ) );
3323
3324 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3325 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003326
3327cleanup:
3328 mbedtls_mpi_free( &r );
3329 mbedtls_mpi_free( &s );
3330 return( mbedtls_to_psa_error( ret ) );
3331}
3332#endif /* MBEDTLS_ECDSA_C */
3333
Gilles Peskinec5487a82018-12-03 18:08:14 +01003334psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003335 psa_algorithm_t alg,
3336 const uint8_t *hash,
3337 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003338 uint8_t *signature,
3339 size_t signature_size,
3340 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003341{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003342 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003343 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3345 const psa_drv_se_t *drv;
3346 psa_drv_se_context_t *drv_context;
3347#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003348
3349 *signature_length = signature_size;
3350
Gilles Peskineedc64242019-08-07 21:05:07 +02003351 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003352 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003353 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003354 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003355 {
3356 status = PSA_ERROR_INVALID_ARGUMENT;
3357 goto exit;
3358 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003359
Gilles Peskineedc64242019-08-07 21:05:07 +02003360#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3361 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3362 {
3363 if( drv->asymmetric == NULL ||
3364 drv->asymmetric->p_sign == NULL )
3365 {
3366 status = PSA_ERROR_NOT_SUPPORTED;
3367 goto exit;
3368 }
3369 status = drv->asymmetric->p_sign( drv_context,
3370 slot->data.se.slot_number,
3371 alg,
3372 hash, hash_length,
3373 signature, signature_size,
3374 signature_length );
3375 }
3376 else
3377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003378#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003379 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003380 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003381 status = psa_rsa_sign( slot->data.rsa,
3382 alg,
3383 hash, hash_length,
3384 signature, signature_size,
3385 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003386 }
3387 else
3388#endif /* defined(MBEDTLS_RSA_C) */
3389#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003390 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003391 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003392#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003393 if(
3394#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3395 PSA_ALG_IS_ECDSA( alg )
3396#else
3397 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3398#endif
3399 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003400 status = psa_ecdsa_sign( slot->data.ecp,
3401 alg,
3402 hash, hash_length,
3403 signature, signature_size,
3404 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003405 else
3406#endif /* defined(MBEDTLS_ECDSA_C) */
3407 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003408 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003409 }
itayzafrir5c753392018-05-08 11:18:38 +03003410 }
3411 else
3412#endif /* defined(MBEDTLS_ECP_C) */
3413 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003415 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003416
3417exit:
3418 /* Fill the unused part of the output buffer (the whole buffer on error,
3419 * the trailing part on success) with something that isn't a valid mac
3420 * (barring an attack on the mac and deliberately-crafted input),
3421 * in case the caller doesn't check the return status properly. */
3422 if( status == PSA_SUCCESS )
3423 memset( signature + *signature_length, '!',
3424 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003425 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003426 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003427 /* If signature_size is 0 then we have nothing to do. We must not call
3428 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003429 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003430}
3431
Gilles Peskinec5487a82018-12-03 18:08:14 +01003432psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003433 psa_algorithm_t alg,
3434 const uint8_t *hash,
3435 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003436 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003437 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003438{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003439 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003440 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3442 const psa_drv_se_t *drv;
3443 psa_drv_se_context_t *drv_context;
3444#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003445
Gilles Peskineedc64242019-08-07 21:05:07 +02003446 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003447 if( status != PSA_SUCCESS )
3448 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003449
Gilles Peskineedc64242019-08-07 21:05:07 +02003450#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3451 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3452 {
3453 if( drv->asymmetric == NULL ||
3454 drv->asymmetric->p_verify == NULL )
3455 return( PSA_ERROR_NOT_SUPPORTED );
3456 return( drv->asymmetric->p_verify( drv_context,
3457 slot->data.se.slot_number,
3458 alg,
3459 hash, hash_length,
3460 signature, signature_length ) );
3461 }
3462 else
3463#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003464#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003465 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003466 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003467 return( psa_rsa_verify( slot->data.rsa,
3468 alg,
3469 hash, hash_length,
3470 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003471 }
3472 else
3473#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003474#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003475 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003476 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003477#if defined(MBEDTLS_ECDSA_C)
3478 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003479 return( psa_ecdsa_verify( slot->data.ecp,
3480 hash, hash_length,
3481 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003482 else
3483#endif /* defined(MBEDTLS_ECDSA_C) */
3484 {
3485 return( PSA_ERROR_INVALID_ARGUMENT );
3486 }
itayzafrir5c753392018-05-08 11:18:38 +03003487 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003488 else
3489#endif /* defined(MBEDTLS_ECP_C) */
3490 {
3491 return( PSA_ERROR_NOT_SUPPORTED );
3492 }
3493}
3494
Gilles Peskine072ac562018-06-30 00:21:29 +02003495#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3496static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3497 mbedtls_rsa_context *rsa )
3498{
3499 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3500 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3501 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3502 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3503}
3504#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3505
Gilles Peskinec5487a82018-12-03 18:08:14 +01003506psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003507 psa_algorithm_t alg,
3508 const uint8_t *input,
3509 size_t input_length,
3510 const uint8_t *salt,
3511 size_t salt_length,
3512 uint8_t *output,
3513 size_t output_size,
3514 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003515{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003516 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003517 psa_status_t status;
3518
Darryl Green5cc689a2018-07-24 15:34:10 +01003519 (void) input;
3520 (void) input_length;
3521 (void) salt;
3522 (void) output;
3523 (void) output_size;
3524
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003525 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003527 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3528 return( PSA_ERROR_INVALID_ARGUMENT );
3529
Gilles Peskine28f8f302019-07-24 13:30:31 +02003530 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003531 if( status != PSA_SUCCESS )
3532 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003533 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3534 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536
3537#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003538 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539 {
3540 mbedtls_rsa_context *rsa = slot->data.rsa;
3541 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003542 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003543 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003544#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003545 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003547 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3548 mbedtls_ctr_drbg_random,
3549 &global_data.ctr_drbg,
3550 MBEDTLS_RSA_PUBLIC,
3551 input_length,
3552 input,
3553 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554 }
3555 else
3556#endif /* MBEDTLS_PKCS1_V15 */
3557#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003558 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003560 psa_rsa_oaep_set_padding_mode( alg, rsa );
3561 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3562 mbedtls_ctr_drbg_random,
3563 &global_data.ctr_drbg,
3564 MBEDTLS_RSA_PUBLIC,
3565 salt, salt_length,
3566 input_length,
3567 input,
3568 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 }
3570 else
3571#endif /* MBEDTLS_PKCS1_V21 */
3572 {
3573 return( PSA_ERROR_INVALID_ARGUMENT );
3574 }
3575 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003576 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577 return( mbedtls_to_psa_error( ret ) );
3578 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003580#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003581 {
3582 return( PSA_ERROR_NOT_SUPPORTED );
3583 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003584}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003585
Gilles Peskinec5487a82018-12-03 18:08:14 +01003586psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003587 psa_algorithm_t alg,
3588 const uint8_t *input,
3589 size_t input_length,
3590 const uint8_t *salt,
3591 size_t salt_length,
3592 uint8_t *output,
3593 size_t output_size,
3594 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003595{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003596 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003597 psa_status_t status;
3598
Darryl Green5cc689a2018-07-24 15:34:10 +01003599 (void) input;
3600 (void) input_length;
3601 (void) salt;
3602 (void) output;
3603 (void) output_size;
3604
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003605 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003607 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3608 return( PSA_ERROR_INVALID_ARGUMENT );
3609
Gilles Peskine28f8f302019-07-24 13:30:31 +02003610 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003611 if( status != PSA_SUCCESS )
3612 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003613 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003614 return( PSA_ERROR_INVALID_ARGUMENT );
3615
3616#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003617 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618 {
3619 mbedtls_rsa_context *rsa = slot->data.rsa;
3620 int ret;
3621
Gilles Peskine630a18a2018-06-29 17:49:35 +02003622 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 return( PSA_ERROR_INVALID_ARGUMENT );
3624
3625#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003626 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003628 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3629 mbedtls_ctr_drbg_random,
3630 &global_data.ctr_drbg,
3631 MBEDTLS_RSA_PRIVATE,
3632 output_length,
3633 input,
3634 output,
3635 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003636 }
3637 else
3638#endif /* MBEDTLS_PKCS1_V15 */
3639#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003640 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003642 psa_rsa_oaep_set_padding_mode( alg, rsa );
3643 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3644 mbedtls_ctr_drbg_random,
3645 &global_data.ctr_drbg,
3646 MBEDTLS_RSA_PRIVATE,
3647 salt, salt_length,
3648 output_length,
3649 input,
3650 output,
3651 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 }
3653 else
3654#endif /* MBEDTLS_PKCS1_V21 */
3655 {
3656 return( PSA_ERROR_INVALID_ARGUMENT );
3657 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003658
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659 return( mbedtls_to_psa_error( ret ) );
3660 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003661 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003662#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003663 {
3664 return( PSA_ERROR_NOT_SUPPORTED );
3665 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003666}
Gilles Peskine20035e32018-02-03 22:44:14 +01003667
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003668
3669
mohammad1603503973b2018-03-12 15:59:30 +02003670/****************************************************************/
3671/* Symmetric cryptography */
3672/****************************************************************/
3673
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003674/* Initialize the cipher operation structure. Once this function has been
3675 * called, psa_cipher_abort can run and will do the right thing. */
3676static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3677 psa_algorithm_t alg )
3678{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003679 if( ! PSA_ALG_IS_CIPHER( alg ) )
3680 {
3681 memset( operation, 0, sizeof( *operation ) );
3682 return( PSA_ERROR_INVALID_ARGUMENT );
3683 }
3684
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003685 operation->alg = alg;
3686 operation->key_set = 0;
3687 operation->iv_set = 0;
3688 operation->iv_required = 1;
3689 operation->iv_size = 0;
3690 operation->block_size = 0;
3691 mbedtls_cipher_init( &operation->ctx.cipher );
3692 return( PSA_SUCCESS );
3693}
3694
Gilles Peskinee553c652018-06-04 16:22:46 +02003695static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003696 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003697 psa_algorithm_t alg,
3698 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003699{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003700 int ret = 0;
3701 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003702 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003703 size_t key_bits;
3704 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003705 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3706 PSA_KEY_USAGE_ENCRYPT :
3707 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003708
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003709 /* A context must be freshly initialized before it can be set up. */
3710 if( operation->alg != 0 )
3711 {
3712 return( PSA_ERROR_BAD_STATE );
3713 }
3714
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003715 status = psa_cipher_init( operation, alg );
3716 if( status != PSA_SUCCESS )
3717 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003718
Gilles Peskine28f8f302019-07-24 13:30:31 +02003719 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003720 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003721 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003722 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003723
Gilles Peskine8e338702019-07-30 20:06:31 +02003724 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003725 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003726 {
3727 status = PSA_ERROR_NOT_SUPPORTED;
3728 goto exit;
3729 }
mohammad1603503973b2018-03-12 15:59:30 +02003730
mohammad1603503973b2018-03-12 15:59:30 +02003731 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003732 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003733 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003734
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003735#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003736 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003737 {
3738 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003739 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003740 memcpy( keys, slot->data.raw.data, 16 );
3741 memcpy( keys + 16, slot->data.raw.data, 8 );
3742 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3743 keys,
3744 192, cipher_operation );
3745 }
3746 else
3747#endif
3748 {
3749 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3750 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003751 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003752 }
Moran Peker41deec42018-04-04 15:43:05 +03003753 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003754 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003755
mohammad16038481e742018-03-18 13:57:31 +02003756#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003757 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003758 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003759 case PSA_ALG_CBC_NO_PADDING:
3760 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3761 MBEDTLS_PADDING_NONE );
3762 break;
3763 case PSA_ALG_CBC_PKCS7:
3764 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3765 MBEDTLS_PADDING_PKCS7 );
3766 break;
3767 default:
3768 /* The algorithm doesn't involve padding. */
3769 ret = 0;
3770 break;
3771 }
3772 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003773 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003774#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3775
mohammad1603503973b2018-03-12 15:59:30 +02003776 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003777 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003778 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003779 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003780 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003781 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003782 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003783#if defined(MBEDTLS_CHACHA20_C)
3784 else
3785 if( alg == PSA_ALG_CHACHA20 )
3786 operation->iv_size = 12;
3787#endif
mohammad1603503973b2018-03-12 15:59:30 +02003788
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003789exit:
3790 if( status == 0 )
3791 status = mbedtls_to_psa_error( ret );
3792 if( status != 0 )
3793 psa_cipher_abort( operation );
3794 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003795}
3796
Gilles Peskinefe119512018-07-08 21:39:34 +02003797psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003798 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003799 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003800{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003801 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003802}
3803
Gilles Peskinefe119512018-07-08 21:39:34 +02003804psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003805 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003806 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003807{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003808 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003809}
3810
Gilles Peskinefe119512018-07-08 21:39:34 +02003811psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003812 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003813 size_t iv_size,
3814 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003815{
itayzafrir534bd7c2018-08-02 13:56:32 +03003816 psa_status_t status;
3817 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003818 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003820 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003821 }
Moran Peker41deec42018-04-04 15:43:05 +03003822 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003823 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003824 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003825 goto exit;
3826 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003827 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3828 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003829 if( ret != 0 )
3830 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003831 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003832 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003833 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003834
mohammad16038481e742018-03-18 13:57:31 +02003835 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003836 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003837
Moran Peker395db872018-05-31 14:07:14 +03003838exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003840 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003841 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003842}
3843
Gilles Peskinefe119512018-07-08 21:39:34 +02003844psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003845 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003846 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003847{
itayzafrir534bd7c2018-08-02 13:56:32 +03003848 psa_status_t status;
3849 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003850 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003851 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003852 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003853 }
Moran Pekera28258c2018-05-29 16:25:04 +03003854 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003855 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003856 status = PSA_ERROR_INVALID_ARGUMENT;
3857 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003858 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003859 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3860 status = mbedtls_to_psa_error( ret );
3861exit:
3862 if( status == PSA_SUCCESS )
3863 operation->iv_set = 1;
3864 else
mohammad1603503973b2018-03-12 15:59:30 +02003865 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003866 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003867}
3868
Gilles Peskinee553c652018-06-04 16:22:46 +02003869psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3870 const uint8_t *input,
3871 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003872 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003873 size_t output_size,
3874 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003875{
itayzafrir534bd7c2018-08-02 13:56:32 +03003876 psa_status_t status;
3877 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003878 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003879
3880 if( operation->alg == 0 )
3881 {
3882 return( PSA_ERROR_BAD_STATE );
3883 }
3884
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003885 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003886 {
3887 /* Take the unprocessed partial block left over from previous
3888 * update calls, if any, plus the input to this call. Remove
3889 * the last partial block, if any. You get the data that will be
3890 * output in this call. */
3891 expected_output_size =
3892 ( operation->ctx.cipher.unprocessed_len + input_length )
3893 / operation->block_size * operation->block_size;
3894 }
3895 else
3896 {
3897 expected_output_size = input_length;
3898 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003899
Gilles Peskine89d789c2018-06-04 17:17:16 +02003900 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003901 {
3902 status = PSA_ERROR_BUFFER_TOO_SMALL;
3903 goto exit;
3904 }
mohammad160382759612018-03-12 18:16:40 +02003905
mohammad1603503973b2018-03-12 15:59:30 +02003906 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003907 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003908 status = mbedtls_to_psa_error( ret );
3909exit:
3910 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003911 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003913}
3914
Gilles Peskinee553c652018-06-04 16:22:46 +02003915psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3916 uint8_t *output,
3917 size_t output_size,
3918 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003919{
David Saadab4ecc272019-02-14 13:48:10 +02003920 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003921 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003922 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003923
mohammad1603503973b2018-03-12 15:59:30 +02003924 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003925 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003926 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003927 }
3928 if( operation->iv_required && ! operation->iv_set )
3929 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003930 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003931 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003932
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003933 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003934 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3935 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003936 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003937 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003938 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003939 }
3940
Janos Follath315b51c2018-07-09 16:04:51 +01003941 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3942 temp_output_buffer,
3943 output_length );
3944 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003945 {
Janos Follath315b51c2018-07-09 16:04:51 +01003946 status = mbedtls_to_psa_error( cipher_ret );
3947 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003948 }
Janos Follath315b51c2018-07-09 16:04:51 +01003949
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003950 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003951 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003952 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003953 memcpy( output, temp_output_buffer, *output_length );
3954 else
3955 {
Janos Follath315b51c2018-07-09 16:04:51 +01003956 status = PSA_ERROR_BUFFER_TOO_SMALL;
3957 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003958 }
mohammad1603503973b2018-03-12 15:59:30 +02003959
Gilles Peskine3f108122018-12-07 18:14:53 +01003960 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003961 status = psa_cipher_abort( operation );
3962
3963 return( status );
3964
3965error:
3966
3967 *output_length = 0;
3968
Gilles Peskine3f108122018-12-07 18:14:53 +01003969 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003970 (void) psa_cipher_abort( operation );
3971
3972 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003973}
3974
Gilles Peskinee553c652018-06-04 16:22:46 +02003975psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3976{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003977 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003978 {
3979 /* The object has (apparently) been initialized but it is not
3980 * in use. It's ok to call abort on such an object, and there's
3981 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003982 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003983 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003984
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003985 /* Sanity check (shouldn't happen: operation->alg should
3986 * always have been initialized to a valid value). */
3987 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3988 return( PSA_ERROR_BAD_STATE );
3989
mohammad1603503973b2018-03-12 15:59:30 +02003990 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003991
Moran Peker41deec42018-04-04 15:43:05 +03003992 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003993 operation->key_set = 0;
3994 operation->iv_set = 0;
3995 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003996 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003997 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003998
Moran Peker395db872018-05-31 14:07:14 +03003999 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004000}
4001
Gilles Peskinea0655c32018-04-30 17:06:50 +02004002
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004003
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004004
mohammad16035955c982018-04-26 00:53:03 +03004005/****************************************************************/
4006/* AEAD */
4007/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004008
Gilles Peskineedf9a652018-08-17 18:11:56 +02004009typedef struct
4010{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004011 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004012 const mbedtls_cipher_info_t *cipher_info;
4013 union
4014 {
4015#if defined(MBEDTLS_CCM_C)
4016 mbedtls_ccm_context ccm;
4017#endif /* MBEDTLS_CCM_C */
4018#if defined(MBEDTLS_GCM_C)
4019 mbedtls_gcm_context gcm;
4020#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004021#if defined(MBEDTLS_CHACHAPOLY_C)
4022 mbedtls_chachapoly_context chachapoly;
4023#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004024 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004025 psa_algorithm_t core_alg;
4026 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004027 uint8_t tag_length;
4028} aead_operation_t;
4029
Gilles Peskine30a9e412019-01-14 18:36:12 +01004030static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004031{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004032 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004033 {
4034#if defined(MBEDTLS_CCM_C)
4035 case PSA_ALG_CCM:
4036 mbedtls_ccm_free( &operation->ctx.ccm );
4037 break;
4038#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004039#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004040 case PSA_ALG_GCM:
4041 mbedtls_gcm_free( &operation->ctx.gcm );
4042 break;
4043#endif /* MBEDTLS_GCM_C */
4044 }
4045}
4046
4047static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004048 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049 psa_key_usage_t usage,
4050 psa_algorithm_t alg )
4051{
4052 psa_status_t status;
4053 size_t key_bits;
4054 mbedtls_cipher_id_t cipher_id;
4055
Gilles Peskine28f8f302019-07-24 13:30:31 +02004056 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057 if( status != PSA_SUCCESS )
4058 return( status );
4059
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004060 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004061
4062 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004063 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004064 &cipher_id );
4065 if( operation->cipher_info == NULL )
4066 return( PSA_ERROR_NOT_SUPPORTED );
4067
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004068 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004069 {
4070#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004071 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4072 operation->core_alg = PSA_ALG_CCM;
4073 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004074 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4075 * The call to mbedtls_ccm_encrypt_and_tag or
4076 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004077 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004078 return( PSA_ERROR_INVALID_ARGUMENT );
4079 mbedtls_ccm_init( &operation->ctx.ccm );
4080 status = mbedtls_to_psa_error(
4081 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4082 operation->slot->data.raw.data,
4083 (unsigned int) key_bits ) );
4084 if( status != 0 )
4085 goto cleanup;
4086 break;
4087#endif /* MBEDTLS_CCM_C */
4088
4089#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004090 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4091 operation->core_alg = PSA_ALG_GCM;
4092 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004093 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4094 * The call to mbedtls_gcm_crypt_and_tag or
4095 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004096 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 return( PSA_ERROR_INVALID_ARGUMENT );
4098 mbedtls_gcm_init( &operation->ctx.gcm );
4099 status = mbedtls_to_psa_error(
4100 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4101 operation->slot->data.raw.data,
4102 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004103 if( status != 0 )
4104 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004105 break;
4106#endif /* MBEDTLS_GCM_C */
4107
Gilles Peskine26869f22019-05-06 15:25:00 +02004108#if defined(MBEDTLS_CHACHAPOLY_C)
4109 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4110 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4111 operation->full_tag_length = 16;
4112 /* We only support the default tag length. */
4113 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4114 return( PSA_ERROR_NOT_SUPPORTED );
4115 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4116 status = mbedtls_to_psa_error(
4117 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4118 operation->slot->data.raw.data ) );
4119 if( status != 0 )
4120 goto cleanup;
4121 break;
4122#endif /* MBEDTLS_CHACHAPOLY_C */
4123
Gilles Peskineedf9a652018-08-17 18:11:56 +02004124 default:
4125 return( PSA_ERROR_NOT_SUPPORTED );
4126 }
4127
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004128 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4129 {
4130 status = PSA_ERROR_INVALID_ARGUMENT;
4131 goto cleanup;
4132 }
4133 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004134
Gilles Peskineedf9a652018-08-17 18:11:56 +02004135 return( PSA_SUCCESS );
4136
4137cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004138 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004139 return( status );
4140}
4141
Gilles Peskinec5487a82018-12-03 18:08:14 +01004142psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004143 psa_algorithm_t alg,
4144 const uint8_t *nonce,
4145 size_t nonce_length,
4146 const uint8_t *additional_data,
4147 size_t additional_data_length,
4148 const uint8_t *plaintext,
4149 size_t plaintext_length,
4150 uint8_t *ciphertext,
4151 size_t ciphertext_size,
4152 size_t *ciphertext_length )
4153{
mohammad16035955c982018-04-26 00:53:03 +03004154 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004155 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004156 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004157
mohammad1603f08a5502018-06-03 15:05:47 +03004158 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004159
Gilles Peskinec5487a82018-12-03 18:08:14 +01004160 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004161 if( status != PSA_SUCCESS )
4162 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004163
Gilles Peskineedf9a652018-08-17 18:11:56 +02004164 /* For all currently supported modes, the tag is at the end of the
4165 * ciphertext. */
4166 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4167 {
4168 status = PSA_ERROR_BUFFER_TOO_SMALL;
4169 goto exit;
4170 }
4171 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004172
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004173#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004174 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004175 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004176 status = mbedtls_to_psa_error(
4177 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4178 MBEDTLS_GCM_ENCRYPT,
4179 plaintext_length,
4180 nonce, nonce_length,
4181 additional_data, additional_data_length,
4182 plaintext, ciphertext,
4183 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004184 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004185 else
4186#endif /* MBEDTLS_GCM_C */
4187#if defined(MBEDTLS_CCM_C)
4188 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004189 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004190 status = mbedtls_to_psa_error(
4191 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4192 plaintext_length,
4193 nonce, nonce_length,
4194 additional_data,
4195 additional_data_length,
4196 plaintext, ciphertext,
4197 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004198 }
mohammad16035c8845f2018-05-09 05:40:09 -07004199 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004200#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004201#if defined(MBEDTLS_CHACHAPOLY_C)
4202 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4203 {
4204 if( nonce_length != 12 || operation.tag_length != 16 )
4205 {
4206 status = PSA_ERROR_NOT_SUPPORTED;
4207 goto exit;
4208 }
4209 status = mbedtls_to_psa_error(
4210 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4211 plaintext_length,
4212 nonce,
4213 additional_data,
4214 additional_data_length,
4215 plaintext,
4216 ciphertext,
4217 tag ) );
4218 }
4219 else
4220#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004221 {
mohammad1603554faad2018-06-03 15:07:38 +03004222 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004223 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004224
Gilles Peskineedf9a652018-08-17 18:11:56 +02004225 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4226 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004227
Gilles Peskineedf9a652018-08-17 18:11:56 +02004228exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004229 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004230 if( status == PSA_SUCCESS )
4231 *ciphertext_length = plaintext_length + operation.tag_length;
4232 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004233}
4234
Gilles Peskineee652a32018-06-01 19:23:52 +02004235/* Locate the tag in a ciphertext buffer containing the encrypted data
4236 * followed by the tag. Return the length of the part preceding the tag in
4237 * *plaintext_length. This is the size of the plaintext in modes where
4238 * the encrypted data has the same size as the plaintext, such as
4239 * CCM and GCM. */
4240static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4241 const uint8_t *ciphertext,
4242 size_t ciphertext_length,
4243 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004244 const uint8_t **p_tag )
4245{
4246 size_t payload_length;
4247 if( tag_length > ciphertext_length )
4248 return( PSA_ERROR_INVALID_ARGUMENT );
4249 payload_length = ciphertext_length - tag_length;
4250 if( payload_length > plaintext_size )
4251 return( PSA_ERROR_BUFFER_TOO_SMALL );
4252 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004253 return( PSA_SUCCESS );
4254}
4255
Gilles Peskinec5487a82018-12-03 18:08:14 +01004256psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004257 psa_algorithm_t alg,
4258 const uint8_t *nonce,
4259 size_t nonce_length,
4260 const uint8_t *additional_data,
4261 size_t additional_data_length,
4262 const uint8_t *ciphertext,
4263 size_t ciphertext_length,
4264 uint8_t *plaintext,
4265 size_t plaintext_size,
4266 size_t *plaintext_length )
4267{
mohammad16035955c982018-04-26 00:53:03 +03004268 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004269 aead_operation_t operation;
4270 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004271
Gilles Peskineee652a32018-06-01 19:23:52 +02004272 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004273
Gilles Peskinec5487a82018-12-03 18:08:14 +01004274 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004275 if( status != PSA_SUCCESS )
4276 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004277
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004278 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4279 ciphertext, ciphertext_length,
4280 plaintext_size, &tag );
4281 if( status != PSA_SUCCESS )
4282 goto exit;
4283
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004284#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004285 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004286 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004287 status = mbedtls_to_psa_error(
4288 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4289 ciphertext_length - operation.tag_length,
4290 nonce, nonce_length,
4291 additional_data,
4292 additional_data_length,
4293 tag, operation.tag_length,
4294 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004295 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004296 else
4297#endif /* MBEDTLS_GCM_C */
4298#if defined(MBEDTLS_CCM_C)
4299 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004300 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004301 status = mbedtls_to_psa_error(
4302 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4303 ciphertext_length - operation.tag_length,
4304 nonce, nonce_length,
4305 additional_data,
4306 additional_data_length,
4307 ciphertext, plaintext,
4308 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004309 }
mohammad160339574652018-06-01 04:39:53 -07004310 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004311#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004312#if defined(MBEDTLS_CHACHAPOLY_C)
4313 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4314 {
4315 if( nonce_length != 12 || operation.tag_length != 16 )
4316 {
4317 status = PSA_ERROR_NOT_SUPPORTED;
4318 goto exit;
4319 }
4320 status = mbedtls_to_psa_error(
4321 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4322 ciphertext_length - operation.tag_length,
4323 nonce,
4324 additional_data,
4325 additional_data_length,
4326 tag,
4327 ciphertext,
4328 plaintext ) );
4329 }
4330 else
4331#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004332 {
mohammad1603554faad2018-06-03 15:07:38 +03004333 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004334 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004335
Gilles Peskineedf9a652018-08-17 18:11:56 +02004336 if( status != PSA_SUCCESS && plaintext_size != 0 )
4337 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004338
Gilles Peskineedf9a652018-08-17 18:11:56 +02004339exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004340 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004341 if( status == PSA_SUCCESS )
4342 *plaintext_length = ciphertext_length - operation.tag_length;
4343 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004344}
4345
Gilles Peskinea0655c32018-04-30 17:06:50 +02004346
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004347
Gilles Peskine20035e32018-02-03 22:44:14 +01004348/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004349/* Generators */
4350/****************************************************************/
4351
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004352#define HKDF_STATE_INIT 0 /* no input yet */
4353#define HKDF_STATE_STARTED 1 /* got salt */
4354#define HKDF_STATE_KEYED 2 /* got key */
4355#define HKDF_STATE_OUTPUT 3 /* output started */
4356
Gilles Peskinecbe66502019-05-16 16:59:18 +02004357static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004358 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004359{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004360 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4361 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004362 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004364}
4365
4366
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004368{
4369 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004371 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004372 {
4373 /* The object has (apparently) been initialized but it is not
4374 * in use. It's ok to call abort on such an object, and there's
4375 * nothing to do. */
4376 }
4377 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004378#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004379 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004380 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381 mbedtls_free( operation->ctx.hkdf.info );
4382 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004383 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004384 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004385 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004386 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004387 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004388 if( operation->ctx.tls12_prf.seed != NULL )
4389 {
4390 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4391 operation->ctx.tls12_prf.seed_length );
4392 mbedtls_free( operation->ctx.tls12_prf.seed );
4393 }
4394
4395 if( operation->ctx.tls12_prf.label != NULL )
4396 {
4397 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4398 operation->ctx.tls12_prf.label_length );
4399 mbedtls_free( operation->ctx.tls12_prf.label );
4400 }
4401
4402 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4403
4404 /* We leave the fields Ai and output_block to be erased safely by the
4405 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004406 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004407 else
4408#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004409 {
4410 status = PSA_ERROR_BAD_STATE;
4411 }
Janos Follath083036a2019-06-11 10:22:26 +01004412 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413 return( status );
4414}
4415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004417 size_t *capacity)
4418{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004420 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004422 return PSA_ERROR_BAD_STATE;
4423 }
4424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004426 return( PSA_SUCCESS );
4427}
4428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004430 size_t capacity )
4431{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004433 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004435 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004437 return( PSA_SUCCESS );
4438}
4439
Gilles Peskinebef7f142018-07-12 17:22:21 +02004440#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004442 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004443static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004444 psa_algorithm_t hash_alg,
4445 uint8_t *output,
4446 size_t output_length )
4447{
4448 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4449 psa_status_t status;
4450
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004451 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4452 return( PSA_ERROR_BAD_STATE );
4453 hkdf->state = HKDF_STATE_OUTPUT;
4454
Gilles Peskinebef7f142018-07-12 17:22:21 +02004455 while( output_length != 0 )
4456 {
4457 /* Copy what remains of the current block */
4458 uint8_t n = hash_length - hkdf->offset_in_block;
4459 if( n > output_length )
4460 n = (uint8_t) output_length;
4461 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4462 output += n;
4463 output_length -= n;
4464 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004465 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004466 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004467 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004468 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004470 * object was corrupted or if this function is called directly
4471 * inside the library. */
4472 if( hkdf->block_number == 0xff )
4473 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004474
4475 /* We need a new block */
4476 ++hkdf->block_number;
4477 hkdf->offset_in_block = 0;
4478 status = psa_hmac_setup_internal( &hkdf->hmac,
4479 hkdf->prk, hash_length,
4480 hash_alg );
4481 if( status != PSA_SUCCESS )
4482 return( status );
4483 if( hkdf->block_number != 1 )
4484 {
4485 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4486 hkdf->output_block,
4487 hash_length );
4488 if( status != PSA_SUCCESS )
4489 return( status );
4490 }
4491 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4492 hkdf->info,
4493 hkdf->info_length );
4494 if( status != PSA_SUCCESS )
4495 return( status );
4496 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4497 &hkdf->block_number, 1 );
4498 if( status != PSA_SUCCESS )
4499 return( status );
4500 status = psa_hmac_finish_internal( &hkdf->hmac,
4501 hkdf->output_block,
4502 sizeof( hkdf->output_block ) );
4503 if( status != PSA_SUCCESS )
4504 return( status );
4505 }
4506
4507 return( PSA_SUCCESS );
4508}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004509
Janos Follath7742fee2019-06-17 12:58:10 +01004510static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4511 psa_tls12_prf_key_derivation_t *tls12_prf,
4512 psa_algorithm_t alg )
4513{
4514 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4515 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004516 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4517 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004518
Janos Follath7742fee2019-06-17 12:58:10 +01004519 /* We can't be wanting more output after block 0xff, otherwise
4520 * the capacity check in psa_key_derivation_output_bytes() would have
4521 * prevented this call. It could happen only if the operation
4522 * object was corrupted or if this function is called directly
4523 * inside the library. */
4524 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004525 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004526
4527 /* We need a new block */
4528 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004529 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004530
4531 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4532 *
4533 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4534 *
4535 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4536 * HMAC_hash(secret, A(2) + seed) +
4537 * HMAC_hash(secret, A(3) + seed) + ...
4538 *
4539 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004540 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004541 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004542 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004543 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004544 * is currently extracted as `output_block` and where i is
4545 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004546 */
4547
Janos Follathea29bfb2019-06-19 12:21:20 +01004548 /* Save the hash context before using it, to preserve the hash state with
4549 * only the inner padding in it. We need this, because inner padding depends
4550 * on the key (secret in the RFC's terminology). */
4551 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4552 if( status != PSA_SUCCESS )
4553 goto cleanup;
4554
4555 /* Calculate A(i) where i = tls12_prf->block_number. */
4556 if( tls12_prf->block_number == 1 )
4557 {
4558 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4559 * the variable seed and in this instance means it in the context of the
4560 * P_hash function, where seed = label + seed.) */
4561 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4562 tls12_prf->label, tls12_prf->label_length );
4563 if( status != PSA_SUCCESS )
4564 goto cleanup;
4565 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4566 tls12_prf->seed, tls12_prf->seed_length );
4567 if( status != PSA_SUCCESS )
4568 goto cleanup;
4569 }
4570 else
4571 {
4572 /* A(i) = HMAC_hash(secret, A(i-1)) */
4573 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4574 tls12_prf->Ai, hash_length );
4575 if( status != PSA_SUCCESS )
4576 goto cleanup;
4577 }
4578
4579 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4580 tls12_prf->Ai, hash_length );
4581 if( status != PSA_SUCCESS )
4582 goto cleanup;
4583 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4584 if( status != PSA_SUCCESS )
4585 goto cleanup;
4586
4587 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4588 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4589 tls12_prf->Ai, hash_length );
4590 if( status != PSA_SUCCESS )
4591 goto cleanup;
4592 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4593 tls12_prf->label, tls12_prf->label_length );
4594 if( status != PSA_SUCCESS )
4595 goto cleanup;
4596 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4597 tls12_prf->seed, tls12_prf->seed_length );
4598 if( status != PSA_SUCCESS )
4599 goto cleanup;
4600 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4601 tls12_prf->output_block, hash_length );
4602 if( status != PSA_SUCCESS )
4603 goto cleanup;
4604 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4605 if( status != PSA_SUCCESS )
4606 goto cleanup;
4607
Janos Follath7742fee2019-06-17 12:58:10 +01004608
4609cleanup:
4610
Janos Follathea29bfb2019-06-19 12:21:20 +01004611 cleanup_status = psa_hash_abort( &backup );
4612 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4613 status = cleanup_status;
4614
4615 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004616}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004617
Janos Follath844eb0e2019-06-19 12:10:49 +01004618static psa_status_t psa_key_derivation_tls12_prf_read(
4619 psa_tls12_prf_key_derivation_t *tls12_prf,
4620 psa_algorithm_t alg,
4621 uint8_t *output,
4622 size_t output_length )
4623{
4624 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4625 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4626 psa_status_t status;
4627 uint8_t offset, length;
4628
4629 while( output_length != 0 )
4630 {
4631 /* Check if we have fully processed the current block. */
4632 if( tls12_prf->left_in_block == 0 )
4633 {
4634 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4635 alg );
4636 if( status != PSA_SUCCESS )
4637 return( status );
4638
4639 continue;
4640 }
4641
4642 if( tls12_prf->left_in_block > output_length )
4643 length = (uint8_t) output_length;
4644 else
4645 length = tls12_prf->left_in_block;
4646
4647 offset = hash_length - tls12_prf->left_in_block;
4648 memcpy( output, tls12_prf->output_block + offset, length );
4649 output += length;
4650 output_length -= length;
4651 tls12_prf->left_in_block -= length;
4652 }
4653
4654 return( PSA_SUCCESS );
4655}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004656#endif /* MBEDTLS_MD_C */
4657
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004658psa_status_t psa_key_derivation_output_bytes(
4659 psa_key_derivation_operation_t *operation,
4660 uint8_t *output,
4661 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004662{
4663 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004665
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004667 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004669 return PSA_ERROR_BAD_STATE;
4670 }
4671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004673 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004675 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004677 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004678 goto exit;
4679 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004681 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004683 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004684 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4685 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004688 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004689 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004691
Gilles Peskinebef7f142018-07-12 17:22:21 +02004692#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004693 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004694 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004695 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004697 output, output_length );
4698 }
Janos Follathadbec812019-06-14 11:05:39 +01004699 else
Janos Follathadbec812019-06-14 11:05:39 +01004700 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004701 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004702 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004704 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004705 output_length );
4706 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004707 else
4708#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004709 {
4710 return( PSA_ERROR_BAD_STATE );
4711 }
4712
4713exit:
4714 if( status != PSA_SUCCESS )
4715 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004716 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004717 * This allows us to differentiate between exhausted operations and
4718 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4719 * operations. */
4720 psa_algorithm_t alg = operation->alg;
4721 psa_key_derivation_abort( operation );
4722 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004723 memset( output, '!', output_length );
4724 }
4725 return( status );
4726}
4727
Gilles Peskine08542d82018-07-19 17:05:42 +02004728#if defined(MBEDTLS_DES_C)
4729static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4730{
4731 if( data_size >= 8 )
4732 mbedtls_des_key_set_parity( data );
4733 if( data_size >= 16 )
4734 mbedtls_des_key_set_parity( data + 8 );
4735 if( data_size >= 24 )
4736 mbedtls_des_key_set_parity( data + 16 );
4737}
4738#endif /* MBEDTLS_DES_C */
4739
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004740static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004741 psa_key_slot_t *slot,
4742 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004744{
4745 uint8_t *data = NULL;
4746 size_t bytes = PSA_BITS_TO_BYTES( bits );
4747 psa_status_t status;
4748
Gilles Peskine8e338702019-07-30 20:06:31 +02004749 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 return( PSA_ERROR_INVALID_ARGUMENT );
4751 if( bits % 8 != 0 )
4752 return( PSA_ERROR_INVALID_ARGUMENT );
4753 data = mbedtls_calloc( 1, bytes );
4754 if( data == NULL )
4755 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4756
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004758 if( status != PSA_SUCCESS )
4759 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004760#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004761 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004762 psa_des_set_key_parity( data, bytes );
4763#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004764 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004765
4766exit:
4767 mbedtls_free( data );
4768 return( status );
4769}
4770
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004771psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004773 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004774{
4775 psa_status_t status;
4776 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004777 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004778
4779 /* Reject any attempt to create a zero-length key so that we don't
4780 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4781 if( psa_get_key_bits( attributes ) == 0 )
4782 return( PSA_ERROR_INVALID_ARGUMENT );
4783
Gilles Peskinedf179142019-07-15 22:02:14 +02004784 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4785 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004786#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4787 if( driver != NULL )
4788 {
4789 /* Deriving a key in a secure element is not implemented yet. */
4790 status = PSA_ERROR_NOT_SUPPORTED;
4791 }
4792#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004793 if( status == PSA_SUCCESS )
4794 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004795 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004796 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004797 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004798 }
4799 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004800 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004801 if( status != PSA_SUCCESS )
4802 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004803 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004804 *handle = 0;
4805 }
4806 return( status );
4807}
4808
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809
4810
4811/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004812/* Key derivation */
4813/****************************************************************/
4814
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004817 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004818{
Janos Follath5fe19732019-06-20 15:09:30 +01004819 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4820 * initialisers for this union leave some bytes unspecified.) */
4821 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4822
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4826 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4827 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004828 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004830 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4831 if( hash_size == 0 )
4832 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004833 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4834 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004835 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836 {
4837 return( PSA_ERROR_NOT_SUPPORTED );
4838 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004840 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004841 }
4842#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004843 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004844 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004845}
4846
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004848 psa_algorithm_t alg )
4849{
4850 psa_status_t status;
4851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853 return( PSA_ERROR_BAD_STATE );
4854
Gilles Peskine6843c292019-01-18 16:44:49 +01004855 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4856 return( PSA_ERROR_INVALID_ARGUMENT );
4857 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004858 {
4859 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004861 }
4862 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4863 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004865 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004866 else
4867 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004868
4869 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004871 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004872}
4873
4874#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004875static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004876 psa_algorithm_t hash_alg,
4877 psa_key_derivation_step_t step,
4878 const uint8_t *data,
4879 size_t data_length )
4880{
4881 psa_status_t status;
4882 switch( step )
4883 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004884 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004885 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004887 status = psa_hmac_setup_internal( &hkdf->hmac,
4888 data, data_length,
4889 hash_alg );
4890 if( status != PSA_SUCCESS )
4891 return( status );
4892 hkdf->state = HKDF_STATE_STARTED;
4893 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004894 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 /* If no salt was provided, use an empty salt. */
4896 if( hkdf->state == HKDF_STATE_INIT )
4897 {
4898 status = psa_hmac_setup_internal( &hkdf->hmac,
4899 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004900 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 if( status != PSA_SUCCESS )
4902 return( status );
4903 hkdf->state = HKDF_STATE_STARTED;
4904 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004905 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004906 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004907 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4908 data, data_length );
4909 if( status != PSA_SUCCESS )
4910 return( status );
4911 status = psa_hmac_finish_internal( &hkdf->hmac,
4912 hkdf->prk,
4913 sizeof( hkdf->prk ) );
4914 if( status != PSA_SUCCESS )
4915 return( status );
4916 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4917 hkdf->block_number = 0;
4918 hkdf->state = HKDF_STATE_KEYED;
4919 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004920 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004921 if( hkdf->state == HKDF_STATE_OUTPUT )
4922 return( PSA_ERROR_BAD_STATE );
4923 if( hkdf->info_set )
4924 return( PSA_ERROR_BAD_STATE );
4925 hkdf->info_length = data_length;
4926 if( data_length != 0 )
4927 {
4928 hkdf->info = mbedtls_calloc( 1, data_length );
4929 if( hkdf->info == NULL )
4930 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4931 memcpy( hkdf->info, data, data_length );
4932 }
4933 hkdf->info_set = 1;
4934 return( PSA_SUCCESS );
4935 default:
4936 return( PSA_ERROR_INVALID_ARGUMENT );
4937 }
4938}
Janos Follathb03233e2019-06-11 15:30:30 +01004939
Janos Follathf08e2652019-06-13 09:05:41 +01004940static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4941 const uint8_t *data,
4942 size_t data_length )
4943{
4944 if( prf->state != TLS12_PRF_STATE_INIT )
4945 return( PSA_ERROR_BAD_STATE );
4946
Janos Follathd6dce9f2019-07-04 09:11:38 +01004947 if( data_length != 0 )
4948 {
4949 prf->seed = mbedtls_calloc( 1, data_length );
4950 if( prf->seed == NULL )
4951 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004952
Janos Follathd6dce9f2019-07-04 09:11:38 +01004953 memcpy( prf->seed, data, data_length );
4954 prf->seed_length = data_length;
4955 }
Janos Follathf08e2652019-06-13 09:05:41 +01004956
4957 prf->state = TLS12_PRF_STATE_SEED_SET;
4958
4959 return( PSA_SUCCESS );
4960}
4961
Janos Follath81550542019-06-13 14:26:34 +01004962static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4963 psa_algorithm_t hash_alg,
4964 const uint8_t *data,
4965 size_t data_length )
4966{
4967 psa_status_t status;
4968 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4969 return( PSA_ERROR_BAD_STATE );
4970
4971 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4972 if( status != PSA_SUCCESS )
4973 return( status );
4974
4975 prf->state = TLS12_PRF_STATE_KEY_SET;
4976
4977 return( PSA_SUCCESS );
4978}
4979
Janos Follath6660f0e2019-06-17 08:44:03 +01004980static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4981 psa_tls12_prf_key_derivation_t *prf,
4982 psa_algorithm_t hash_alg,
4983 const uint8_t *data,
4984 size_t data_length )
4985{
4986 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004987 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4988 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004989
4990 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992
4993 /* Quoting RFC 4279, Section 2:
4994 *
4995 * The premaster secret is formed as follows: if the PSK is N octets
4996 * long, concatenate a uint16 with the value N, N zero octets, a second
4997 * uint16 with the value N, and the PSK itself.
4998 */
4999
Janos Follath40e13932019-06-26 13:22:29 +01005000 *cur++ = ( data_length >> 8 ) & 0xff;
5001 *cur++ = ( data_length >> 0 ) & 0xff;
5002 memset( cur, 0, data_length );
5003 cur += data_length;
5004 *cur++ = pms[0];
5005 *cur++ = pms[1];
5006 memcpy( cur, data, data_length );
5007 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005008
Janos Follath40e13932019-06-26 13:22:29 +01005009 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005010
5011 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5012 return( status );
5013}
5014
Janos Follath63028dd2019-06-13 09:15:47 +01005015static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5016 const uint8_t *data,
5017 size_t data_length )
5018{
5019 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5020 return( PSA_ERROR_BAD_STATE );
5021
Janos Follathd6dce9f2019-07-04 09:11:38 +01005022 if( data_length != 0 )
5023 {
5024 prf->label = mbedtls_calloc( 1, data_length );
5025 if( prf->label == NULL )
5026 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005027
Janos Follathd6dce9f2019-07-04 09:11:38 +01005028 memcpy( prf->label, data, data_length );
5029 prf->label_length = data_length;
5030 }
Janos Follath63028dd2019-06-13 09:15:47 +01005031
5032 prf->state = TLS12_PRF_STATE_LABEL_SET;
5033
5034 return( PSA_SUCCESS );
5035}
5036
Janos Follathb03233e2019-06-11 15:30:30 +01005037static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5038 psa_algorithm_t hash_alg,
5039 psa_key_derivation_step_t step,
5040 const uint8_t *data,
5041 size_t data_length )
5042{
Janos Follathb03233e2019-06-11 15:30:30 +01005043 switch( step )
5044 {
Janos Follathf08e2652019-06-13 09:05:41 +01005045 case PSA_KEY_DERIVATION_INPUT_SEED:
5046 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005047 case PSA_KEY_DERIVATION_INPUT_SECRET:
5048 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005049 case PSA_KEY_DERIVATION_INPUT_LABEL:
5050 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005051 default:
5052 return( PSA_ERROR_INVALID_ARGUMENT );
5053 }
5054}
Janos Follath6660f0e2019-06-17 08:44:03 +01005055
5056static psa_status_t psa_tls12_prf_psk_to_ms_input(
5057 psa_tls12_prf_key_derivation_t *prf,
5058 psa_algorithm_t hash_alg,
5059 psa_key_derivation_step_t step,
5060 const uint8_t *data,
5061 size_t data_length )
5062{
5063 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005064 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005065 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5066 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005067 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005068
5069 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5070}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005071#endif /* MBEDTLS_MD_C */
5072
Janos Follathb80a94e2019-06-12 15:54:46 +01005073static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005075 psa_key_derivation_step_t step,
5076 const uint8_t *data,
5077 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005078{
5079 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005081
5082#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005083 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005084 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005086 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087 step, data, data_length );
5088 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005089 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005090 {
Janos Follathb03233e2019-06-11 15:30:30 +01005091 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5092 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5093 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005094 }
5095 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5096 {
5097 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5098 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5099 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005100 }
5101 else
5102#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005103 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105 return( PSA_ERROR_BAD_STATE );
5106 }
5107
5108 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005110 return( status );
5111}
5112
Janos Follath51f4a0f2019-06-14 11:35:55 +01005113psa_status_t psa_key_derivation_input_bytes(
5114 psa_key_derivation_operation_t *operation,
5115 psa_key_derivation_step_t step,
5116 const uint8_t *data,
5117 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005118{
Janos Follathc5621512019-06-14 11:27:57 +01005119 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5120 return( PSA_ERROR_INVALID_ARGUMENT );
5121
5122 return( psa_key_derivation_input_internal( operation, step,
5123 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005124}
5125
Janos Follath51f4a0f2019-06-14 11:35:55 +01005126psa_status_t psa_key_derivation_input_key(
5127 psa_key_derivation_operation_t *operation,
5128 psa_key_derivation_step_t step,
5129 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130{
5131 psa_key_slot_t *slot;
5132 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005133 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005134 PSA_KEY_USAGE_DERIVE,
5135 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136 if( status != PSA_SUCCESS )
5137 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005138 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005139 return( PSA_ERROR_INVALID_ARGUMENT );
5140 /* Don't allow a key to be used as an input that is usually public.
5141 * This is debatable. It's ok from a cryptographic perspective to
5142 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005143 * the material should be dedicated to a particular input step,
5144 * otherwise this may allow the key to be used in an unintended way
5145 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005146 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005147 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005148 return( psa_key_derivation_input_internal( operation,
5149 step,
5150 slot->data.raw.data,
5151 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005152}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005153
5154
5155
5156/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005157/* Key agreement */
5158/****************************************************************/
5159
Gilles Peskinea05219c2018-11-16 16:02:56 +01005160#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005161static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5162 size_t peer_key_length,
5163 const mbedtls_ecp_keypair *our_key,
5164 uint8_t *shared_secret,
5165 size_t shared_secret_size,
5166 size_t *shared_secret_length )
5167{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005168 mbedtls_ecp_keypair *their_key = NULL;
5169 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005170 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005171 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005172
Jaeden Ameroccdce902019-01-10 11:42:27 +00005173 status = psa_import_ec_public_key(
5174 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5175 peer_key, peer_key_length,
5176 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005177 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005178 goto exit;
5179
Jaeden Amero97271b32019-01-10 19:38:51 +00005180 status = mbedtls_to_psa_error(
5181 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5182 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005183 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005184 status = mbedtls_to_psa_error(
5185 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5186 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005187 goto exit;
5188
Jaeden Amero97271b32019-01-10 19:38:51 +00005189 status = mbedtls_to_psa_error(
5190 mbedtls_ecdh_calc_secret( &ecdh,
5191 shared_secret_length,
5192 shared_secret, shared_secret_size,
5193 mbedtls_ctr_drbg_random,
5194 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005195
5196exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005197 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005198 mbedtls_ecp_keypair_free( their_key );
5199 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005200 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005201}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005202#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005203
Gilles Peskine01d718c2018-09-18 12:01:02 +02005204#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5205
Gilles Peskine0216fe12019-04-11 21:23:21 +02005206static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5207 psa_key_slot_t *private_key,
5208 const uint8_t *peer_key,
5209 size_t peer_key_length,
5210 uint8_t *shared_secret,
5211 size_t shared_secret_size,
5212 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005213{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005214 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005215 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005216#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005217 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005218 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005219 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005220 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5221 private_key->data.ecp,
5222 shared_secret, shared_secret_size,
5223 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005224#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005225 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005226 (void) private_key;
5227 (void) peer_key;
5228 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005229 (void) shared_secret;
5230 (void) shared_secret_size;
5231 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005232 return( PSA_ERROR_NOT_SUPPORTED );
5233 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005234}
5235
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005236/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005237 * to potentially free embedded data structures and wipe confidential data.
5238 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005241 psa_key_slot_t *private_key,
5242 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005243 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005244{
5245 psa_status_t status;
5246 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5247 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005249
5250 /* Step 1: run the secret agreement algorithm to generate the shared
5251 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005252 status = psa_key_agreement_raw_internal( ka_alg,
5253 private_key,
5254 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005255 shared_secret,
5256 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005257 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005258 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005259 goto exit;
5260
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005261 /* Step 2: set up the key derivation to generate key material from
5262 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005263 status = psa_key_derivation_input_internal( operation, step,
5264 shared_secret,
5265 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005266
Gilles Peskine12313cd2018-06-20 00:20:32 +02005267exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005268 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005269 return( status );
5270}
5271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005273 psa_key_derivation_step_t step,
5274 psa_key_handle_t private_key,
5275 const uint8_t *peer_key,
5276 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005277{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005278 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005279 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005281 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005282 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005283 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005284 if( status != PSA_SUCCESS )
5285 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005287 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005288 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005289 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005291 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005292}
5293
Gilles Peskinebe697d82019-05-16 18:00:41 +02005294psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5295 psa_key_handle_t private_key,
5296 const uint8_t *peer_key,
5297 size_t peer_key_length,
5298 uint8_t *output,
5299 size_t output_size,
5300 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005301{
5302 psa_key_slot_t *slot;
5303 psa_status_t status;
5304
5305 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5306 {
5307 status = PSA_ERROR_INVALID_ARGUMENT;
5308 goto exit;
5309 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005310 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005311 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005312 if( status != PSA_SUCCESS )
5313 goto exit;
5314
5315 status = psa_key_agreement_raw_internal( alg, slot,
5316 peer_key, peer_key_length,
5317 output, output_size,
5318 output_length );
5319
5320exit:
5321 if( status != PSA_SUCCESS )
5322 {
5323 /* If an error happens and is not handled properly, the output
5324 * may be used as a key to protect sensitive data. Arrange for such
5325 * a key to be random, which is likely to result in decryption or
5326 * verification errors. This is better than filling the buffer with
5327 * some constant data such as zeros, which would result in the data
5328 * being protected with a reproducible, easily knowable key.
5329 */
5330 psa_generate_random( output, output_size );
5331 *output_length = output_size;
5332 }
5333 return( status );
5334}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005335
5336
5337/****************************************************************/
5338/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005339/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005340
Gilles Peskine4c317f42018-07-12 01:24:09 +02005341psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005342 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005343{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005344 int ret;
5345 GUARD_MODULE_INITIALIZED;
5346
Gilles Peskinef181eca2019-08-07 13:49:00 +02005347 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5348 {
5349 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5350 output,
5351 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5352 if( ret != 0 )
5353 return( mbedtls_to_psa_error( ret ) );
5354 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5355 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5356 }
5357
Gilles Peskinee59236f2018-01-27 23:32:46 +01005358 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5359 return( mbedtls_to_psa_error( ret ) );
5360}
5361
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005362#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5363#include "mbedtls/entropy_poll.h"
5364
Gilles Peskine7228da22019-07-15 11:06:15 +02005365psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005366 size_t seed_size )
5367{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005368 if( global_data.initialized )
5369 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005370
5371 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5372 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5373 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5374 return( PSA_ERROR_INVALID_ARGUMENT );
5375
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005376 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005377}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005378#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005379
Gilles Peskinee56e8782019-04-26 17:34:02 +02005380#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5381static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5382 size_t domain_parameters_size,
5383 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005384{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005385 size_t i;
5386 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005387
Gilles Peskinee56e8782019-04-26 17:34:02 +02005388 if( domain_parameters_size == 0 )
5389 {
5390 *exponent = 65537;
5391 return( PSA_SUCCESS );
5392 }
5393
5394 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5395 * support values that fit in a 32-bit integer, which is larger than
5396 * int on just about every platform anyway. */
5397 if( domain_parameters_size > sizeof( acc ) )
5398 return( PSA_ERROR_NOT_SUPPORTED );
5399 for( i = 0; i < domain_parameters_size; i++ )
5400 acc = ( acc << 8 ) | domain_parameters[i];
5401 if( acc > INT_MAX )
5402 return( PSA_ERROR_NOT_SUPPORTED );
5403 *exponent = acc;
5404 return( PSA_SUCCESS );
5405}
5406#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5407
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005408static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005409 psa_key_slot_t *slot, size_t bits,
5410 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005411{
Gilles Peskine8e338702019-07-30 20:06:31 +02005412 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005413
Gilles Peskinee56e8782019-04-26 17:34:02 +02005414 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005415 return( PSA_ERROR_INVALID_ARGUMENT );
5416
Gilles Peskinee59236f2018-01-27 23:32:46 +01005417 if( key_type_is_raw_bytes( type ) )
5418 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005419 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005420 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5421 if( status != PSA_SUCCESS )
5422 return( status );
5423 status = psa_generate_random( slot->data.raw.data,
5424 slot->data.raw.bytes );
5425 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005426 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005427#if defined(MBEDTLS_DES_C)
5428 if( type == PSA_KEY_TYPE_DES )
5429 psa_des_set_key_parity( slot->data.raw.data,
5430 slot->data.raw.bytes );
5431#endif /* MBEDTLS_DES_C */
5432 }
5433 else
5434
5435#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005436 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005437 {
5438 mbedtls_rsa_context *rsa;
5439 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005440 int exponent;
5441 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005442 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5443 return( PSA_ERROR_NOT_SUPPORTED );
5444 /* Accept only byte-aligned keys, for the same reasons as
5445 * in psa_import_rsa_key(). */
5446 if( bits % 8 != 0 )
5447 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005448 status = psa_read_rsa_exponent( domain_parameters,
5449 domain_parameters_size,
5450 &exponent );
5451 if( status != PSA_SUCCESS )
5452 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005453 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5454 if( rsa == NULL )
5455 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5456 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5457 ret = mbedtls_rsa_gen_key( rsa,
5458 mbedtls_ctr_drbg_random,
5459 &global_data.ctr_drbg,
5460 (unsigned int) bits,
5461 exponent );
5462 if( ret != 0 )
5463 {
5464 mbedtls_rsa_free( rsa );
5465 mbedtls_free( rsa );
5466 return( mbedtls_to_psa_error( ret ) );
5467 }
5468 slot->data.rsa = rsa;
5469 }
5470 else
5471#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5472
5473#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005474 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005475 {
5476 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5477 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5478 const mbedtls_ecp_curve_info *curve_info =
5479 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5480 mbedtls_ecp_keypair *ecp;
5481 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005482 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005483 return( PSA_ERROR_NOT_SUPPORTED );
5484 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5485 return( PSA_ERROR_NOT_SUPPORTED );
5486 if( curve_info->bit_size != bits )
5487 return( PSA_ERROR_INVALID_ARGUMENT );
5488 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5489 if( ecp == NULL )
5490 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5491 mbedtls_ecp_keypair_init( ecp );
5492 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5493 mbedtls_ctr_drbg_random,
5494 &global_data.ctr_drbg );
5495 if( ret != 0 )
5496 {
5497 mbedtls_ecp_keypair_free( ecp );
5498 mbedtls_free( ecp );
5499 return( mbedtls_to_psa_error( ret ) );
5500 }
5501 slot->data.ecp = ecp;
5502 }
5503 else
5504#endif /* MBEDTLS_ECP_C */
5505
5506 return( PSA_ERROR_NOT_SUPPORTED );
5507
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005508 return( PSA_SUCCESS );
5509}
Darryl Green0c6575a2018-11-07 16:05:30 +00005510
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005511psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005512 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005513{
5514 psa_status_t status;
5515 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005516 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005517
Gilles Peskine0f84d622019-09-12 19:03:13 +02005518 /* Reject any attempt to create a zero-length key so that we don't
5519 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5520 if( psa_get_key_bits( attributes ) == 0 )
5521 return( PSA_ERROR_INVALID_ARGUMENT );
5522
Gilles Peskinedf179142019-07-15 22:02:14 +02005523 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5524 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005525 if( status != PSA_SUCCESS )
5526 goto exit;
5527
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005528#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5529 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005530 {
Gilles Peskine11792082019-08-06 18:36:36 +02005531 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5532 size_t pubkey_length = 0; /* We don't support this feature yet */
5533 if( drv->key_management == NULL ||
5534 drv->key_management->p_generate == NULL )
5535 {
5536 status = PSA_ERROR_NOT_SUPPORTED;
5537 goto exit;
5538 }
5539 status = drv->key_management->p_generate(
5540 psa_get_se_driver_context( driver ),
5541 slot->data.se.slot_number, attributes,
5542 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005543 }
Gilles Peskine11792082019-08-06 18:36:36 +02005544 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005545#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005546 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005547 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005548 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005549 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005550 }
Gilles Peskine11792082019-08-06 18:36:36 +02005551
5552exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005553 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005554 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005555 if( status != PSA_SUCCESS )
5556 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005557 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005558 *handle = 0;
5559 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005560 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005561}
5562
5563
Gilles Peskinee59236f2018-01-27 23:32:46 +01005564
5565/****************************************************************/
5566/* Module setup */
5567/****************************************************************/
5568
Gilles Peskine5e769522018-11-20 21:59:56 +01005569psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5570 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5571 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5572{
5573 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5574 return( PSA_ERROR_BAD_STATE );
5575 global_data.entropy_init = entropy_init;
5576 global_data.entropy_free = entropy_free;
5577 return( PSA_SUCCESS );
5578}
5579
Gilles Peskinee59236f2018-01-27 23:32:46 +01005580void mbedtls_psa_crypto_free( void )
5581{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005582 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005583 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5584 {
5585 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005586 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005587 }
5588 /* Wipe all remaining data, including configuration.
5589 * In particular, this sets all state indicator to the value
5590 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005591 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005592#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005593 /* Unregister all secure element drivers, so that we restart from
5594 * a pristine state. */
5595 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005596#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005597}
5598
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005599#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5600/** Recover a transaction that was interrupted by a power failure.
5601 *
5602 * This function is called during initialization, before psa_crypto_init()
5603 * returns. If this function returns a failure status, the initialization
5604 * fails.
5605 */
5606static psa_status_t psa_crypto_recover_transaction(
5607 const psa_crypto_transaction_t *transaction )
5608{
5609 switch( transaction->unknown.type )
5610 {
5611 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5612 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005613 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005614 * is implemented.
5615 * https://github.com/ARMmbed/mbed-crypto/issues/218
5616 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005617 default:
5618 /* We found an unsupported transaction in the storage.
5619 * We don't know what state the storage is in. Give up. */
5620 return( PSA_ERROR_STORAGE_FAILURE );
5621 }
5622}
5623#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5624
Gilles Peskinee59236f2018-01-27 23:32:46 +01005625psa_status_t psa_crypto_init( void )
5626{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005627 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005628 const unsigned char drbg_seed[] = "PSA";
5629
Gilles Peskinec6b69072018-11-20 21:42:52 +01005630 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005631 if( global_data.initialized != 0 )
5632 return( PSA_SUCCESS );
5633
Gilles Peskine5e769522018-11-20 21:59:56 +01005634 /* Set default configuration if
5635 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5636 if( global_data.entropy_init == NULL )
5637 global_data.entropy_init = mbedtls_entropy_init;
5638 if( global_data.entropy_free == NULL )
5639 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005640
Gilles Peskinec6b69072018-11-20 21:42:52 +01005641 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005642 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005643#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5644 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5645 /* The PSA entropy injection feature depends on using NV seed as an entropy
5646 * source. Add NV seed as an entropy source for PSA entropy injection. */
5647 mbedtls_entropy_add_source( &global_data.entropy,
5648 mbedtls_nv_seed_poll, NULL,
5649 MBEDTLS_ENTROPY_BLOCK_SIZE,
5650 MBEDTLS_ENTROPY_SOURCE_STRONG );
5651#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005652 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005653 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005654 status = mbedtls_to_psa_error(
5655 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5656 mbedtls_entropy_func,
5657 &global_data.entropy,
5658 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5659 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005660 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005661 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005662
Gilles Peskine66fb1262018-12-10 16:29:04 +01005663 status = psa_initialize_key_slots( );
5664 if( status != PSA_SUCCESS )
5665 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005666
Gilles Peskine4b734222019-07-24 15:56:31 +02005667#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005668 status = psa_crypto_load_transaction( );
5669 if( status == PSA_SUCCESS )
5670 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005671 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5672 if( status != PSA_SUCCESS )
5673 goto exit;
5674 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005675 }
5676 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5677 {
5678 /* There's no transaction to complete. It's all good. */
5679 status = PSA_SUCCESS;
5680 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005681#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005682
Gilles Peskinec6b69072018-11-20 21:42:52 +01005683 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005684 global_data.initialized = 1;
5685
5686exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005687 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005689 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005690}
5691
5692#endif /* MBEDTLS_PSA_CRYPTO_C */