blob: 42c2969bfdb1aef3709387da7de12fee017cc4cc [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
454#if defined(MBEDTLS_MD_C)
455 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200456#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
495 /* Allocate memory for the key */
496 raw->bytes = PSA_BITS_TO_BYTES( bits );
497 raw->data = mbedtls_calloc( 1, raw->bytes );
498 if( raw->data == NULL )
499 {
500 raw->bytes = 0;
501 return( PSA_ERROR_INSUFFICIENT_MEMORY );
502 }
503 return( PSA_SUCCESS );
504}
505
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100507/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
508 * that are not a multiple of 8) well. For example, there is only
509 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
510 * way to return the exact bit size of a key.
511 * To keep things simple, reject non-byte-aligned key sizes. */
512static psa_status_t psa_check_rsa_key_byte_aligned(
513 const mbedtls_rsa_context *rsa )
514{
515 mbedtls_mpi n;
516 psa_status_t status;
517 mbedtls_mpi_init( &n );
518 status = mbedtls_to_psa_error(
519 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
520 if( status == PSA_SUCCESS )
521 {
522 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
523 status = PSA_ERROR_NOT_SUPPORTED;
524 }
525 mbedtls_mpi_free( &n );
526 return( status );
527}
528
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000529static psa_status_t psa_import_rsa_key( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 mbedtls_rsa_context **p_rsa )
533{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534 psa_status_t status;
535 mbedtls_pk_context pk;
536 mbedtls_rsa_context *rsa;
537 size_t bits;
538
539 mbedtls_pk_init( &pk );
540
541 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
553 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
559 rsa = mbedtls_pk_rsa( pk );
560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
569 status = psa_check_rsa_key_byte_aligned( rsa );
570
571exit:
572 /* Free the content of the pk object only on error. */
573 if( status != PSA_SUCCESS )
574 {
575 mbedtls_pk_free( &pk );
576 return( status );
577 }
578
579 /* On success, store the content of the object in the RSA context. */
580 *p_rsa = rsa;
581
582 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583}
584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586#if defined(MBEDTLS_ECP_C)
587
588/* Import a public key given as the uncompressed representation defined by SEC1
589 * 2.3.3 as the content of an ECPoint. */
590static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
591 const uint8_t *data,
592 size_t data_length,
593 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000596 mbedtls_ecp_keypair *ecp = NULL;
597 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598
599 *p_ecp = NULL;
600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
601 if( ecp == NULL )
602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
603 mbedtls_ecp_keypair_init( ecp );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the public value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
613 data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616
617 /* Check that the point is on the curve. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 *p_ecp = ecp;
624 return( PSA_SUCCESS );
625
626exit:
627 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair_free( ecp );
630 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
637/* Import a private key given as a byte string which is the private value
638 * in big-endian order. */
639static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
640 const uint8_t *data,
641 size_t data_length,
642 mbedtls_ecp_keypair **p_ecp )
643{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 mbedtls_ecp_keypair *ecp = NULL;
646 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200648 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649 return( PSA_ERROR_INVALID_ARGUMENT );
650
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 *p_ecp = NULL;
652 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
653 if( ecp == NULL )
654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000655 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
657 /* Load the group. */
658 status = mbedtls_to_psa_error(
659 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662 /* Load the secret value. */
663 status = mbedtls_to_psa_error(
664 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
665 if( status != PSA_SUCCESS )
666 goto exit;
667 /* Validate the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
670 if( status != PSA_SUCCESS )
671 goto exit;
672 /* Calculate the public key from the private key. */
673 status = mbedtls_to_psa_error(
674 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
675 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678
679 *p_ecp = ecp;
680 return( PSA_SUCCESS );
681
682exit:
683 if( ecp != NULL )
684 {
685 mbedtls_ecp_keypair_free( ecp );
686 mbedtls_free( ecp );
687 }
688 return( status );
689}
690#endif /* defined(MBEDTLS_ECP_C) */
691
Gilles Peskineb46bef22019-07-30 21:32:04 +0200692
693/** Return the size of the key in the given slot, in bits.
694 *
695 * \param[in] slot A key slot.
696 *
697 * \return The key size in bits, read from the metadata in the slot.
698 */
699static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
700{
701 return( slot->attr.bits );
702}
703
704/** Calculate the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot containing a transparent key.
707 *
708 * \return The key size in bits, calculated from the key data.
709 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200710static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 size_t bits = 0; /* return 0 on an empty slot */
713
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
718 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#endif /* defined(MBEDTLS_RSA_C) */
720#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
722 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724
725 /* We know that the size fits in psa_key_bits_t thanks to checks
726 * when the key was created. */
727 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728}
729
Gilles Peskine8e338702019-07-30 20:06:31 +0200730/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100731 * previously. This function assumes that the slot does not contain
732 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100733psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
734 const uint8_t *data,
735 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Gilles Peskine8e338702019-07-30 20:06:31 +0200739 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200742 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 if( data_length > SIZE_MAX / 8 )
744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750 &slot->data.raw );
751 if( status != PSA_SUCCESS )
752 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200753 if( data_length != 0 )
754 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 }
756 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100757#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200758 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 data, data_length,
762 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000766 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 data, data_length,
769 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000777 data, data_length,
778 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 }
780 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
783 return( PSA_ERROR_NOT_SUPPORTED );
784 }
Darryl Green940d72c2018-07-13 13:18:51 +0100785
Gilles Peskineb46bef22019-07-30 21:32:04 +0200786 if( status == PSA_SUCCESS )
787 {
788 /* Write the actual key size to the slot.
789 * psa_start_key_creation() wrote the size declared by the
790 * caller, which may be 0 (meaning unspecified) or wrong. */
791 slot->attr.bits = psa_calculate_key_bits( slot );
792 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 return( status );
794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
801 psa_algorithm_t alg1,
802 psa_algorithm_t alg2 )
803{
Gilles Peskine549ea862019-05-22 11:45:59 +0200804 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 if( alg1 == alg2 )
806 return( alg1 );
807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
809 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
810 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
811 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
812 {
813 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
814 return( alg2 );
815 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
816 return( alg1 );
817 }
818 /* If the policies are incompatible, allow nothing. */
819 return( 0 );
820}
821
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
823 psa_algorithm_t requested_alg )
824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826 if( requested_alg == policy_alg )
827 return( 1 );
828 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 * and requested_alg is the same hash-and-sign family with any hash,
830 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
832 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
833 {
834 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
835 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
836 }
837 /* If it isn't permitted, it's forbidden. */
838 return( 0 );
839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
844 */
845static int psa_key_policy_permits( const psa_key_policy_t *policy,
846 psa_algorithm_t alg )
847{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200848 return( psa_key_algorithm_permits( policy->alg, alg ) ||
849 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850}
851
Gilles Peskinef603c712019-01-19 13:40:11 +0100852/** Restrict a key policy based on a constraint.
853 *
854 * \param[in,out] policy The policy to restrict.
855 * \param[in] constraint The policy constraint to apply.
856 *
857 * \retval #PSA_SUCCESS
858 * \c *policy contains the intersection of the original value of
859 * \c *policy and \c *constraint.
860 * \retval #PSA_ERROR_INVALID_ARGUMENT
861 * \c *policy and \c *constraint are incompatible.
862 * \c *policy is unchanged.
863 */
864static psa_status_t psa_restrict_key_policy(
865 psa_key_policy_t *policy,
866 const psa_key_policy_t *constraint )
867{
868 psa_algorithm_t intersection_alg =
869 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 psa_algorithm_t intersection_alg2 =
871 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Darryl Green06fd18d2018-07-16 11:21:11 +0100882/** Retrieve a slot which must contain a key. The key must have allow all the
883 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
884 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100885static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100887 psa_key_usage_t usage,
888 psa_algorithm_t alg )
889{
890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100891 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100892
893 *p_slot = NULL;
894
Gilles Peskinec5487a82018-12-03 18:08:14 +0100895 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100896 if( status != PSA_SUCCESS )
897 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 /* Enforce that usage policy for the key slot contains all the flags
900 * required by the usage parameter. There is one exception: public
901 * keys can always be exported, so we treat public key objects as
902 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200903 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907
908 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
911
912 *p_slot = slot;
913 return( PSA_SUCCESS );
914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916/** Retrieve a slot which must contain a transparent key.
917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200921 * This is a temporary function to use instead of psa_get_key_from_slot()
922 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923 */
924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
925static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
926 psa_key_slot_t **p_slot,
927 psa_key_usage_t usage,
928 psa_algorithm_t alg )
929{
930 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200933 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 {
935 *p_slot = NULL;
936 return( PSA_ERROR_NOT_SUPPORTED );
937 }
938 return( PSA_SUCCESS );
939}
940#else /* MBEDTLS_PSA_CRYPTO_SE_C */
941/* With no secure element support, all keys are transparent. */
942#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
943 psa_get_key_from_slot( handle, p_slot, usage, alg )
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
945
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100946/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000948{
Gilles Peskine73167e12019-07-12 23:44:37 +0200949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
950 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000951 {
952 /* No key material to clean. */
953 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200954 else
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200956 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000957 {
958 /* No key material to clean. */
959 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 mbedtls_free( slot->data.raw.data );
963 }
964 else
965#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_rsa_free( slot->data.rsa );
969 mbedtls_free( slot->data.rsa );
970 }
971 else
972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_ecp_keypair_free( slot->data.ecp );
977 mbedtls_free( slot->data.ecp );
978 }
979 else
980#endif /* defined(MBEDTLS_ECP_C) */
981 {
982 /* Shouldn't happen: the key type is not any type that we
983 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200984 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000985 }
986
987 return( PSA_SUCCESS );
988}
989
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990/** Completely wipe a slot in memory, including its policy.
991 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100992psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993{
994 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200995 /* Multipart operations may still be using the key. This is safe
996 * because all multipart operation objects are independent from
997 * the key slot: if they need to access the key after the setup
998 * phase, they have a copy of the key. Note that this means that
999 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000 /* At this point, key material and other type-specific content has
1001 * been wiped. Clear remaining metadata. We can call memset and not
1002 * zeroize because the metadata is not particularly sensitive. */
1003 memset( slot, 0, sizeof( *slot ) );
1004 return( status );
1005}
1006
Gilles Peskinec5487a82018-12-03 18:08:14 +01001007psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001009 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001010 psa_status_t status; /* status of the last operation */
1011 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 psa_se_drv_table_entry_t *driver;
1014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskine1841cf42019-10-08 15:48:25 +02001016 if( handle == 0 )
1017 return( PSA_SUCCESS );
1018
Gilles Peskinec5487a82018-12-03 18:08:14 +01001019 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 if( status != PSA_SUCCESS )
1021 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022
1023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001024 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001025 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001026 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001027 /* For a key in a secure element, we need to do three things:
1028 * remove the key file in internal storage, destroy the
1029 * key inside the secure element, and update the driver's
1030 * persistent data. Start a transaction that will encompass these
1031 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001033 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001034 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 status = psa_crypto_save_transaction( );
1037 if( status != PSA_SUCCESS )
1038 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001039 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001040 /* We should still try to destroy the key in the secure
1041 * element and the key metadata in storage. This is especially
1042 * important if the error is that the storage is full.
1043 * But how to do it exactly without risking an inconsistent
1044 * state after a reset?
1045 * https://github.com/ARMmbed/mbed-crypto/issues/215
1046 */
1047 overall_status = status;
1048 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 }
1050
Gilles Peskine354f7672019-07-12 23:46:38 +02001051 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 if( overall_status == PSA_SUCCESS )
1053 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001054 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001055#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1056
Darryl Greend49a4992018-06-18 17:27:26 +01001057#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001058 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001059 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001060 status = psa_destroy_persistent_key( slot->attr.id );
1061 if( overall_status == PSA_SUCCESS )
1062 overall_status = status;
1063
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001064 /* TODO: other slots may have a copy of the same key. We should
1065 * invalidate them.
1066 * https://github.com/ARMmbed/mbed-crypto/issues/214
1067 */
Darryl Greend49a4992018-06-18 17:27:26 +01001068 }
1069#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001070
Gilles Peskinefc762652019-07-22 19:30:34 +02001071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1072 if( driver != NULL )
1073 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001074 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
1077 status = psa_crypto_stop_transaction( );
1078 if( overall_status == PSA_SUCCESS )
1079 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001080 }
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1082
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1084exit:
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001086 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1088 if( overall_status == PSA_SUCCESS )
1089 overall_status = status;
1090 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091}
1092
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001094{
Gilles Peskineb699f072019-04-26 16:06:02 +02001095 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001096 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001097}
1098
Gilles Peskineb699f072019-04-26 16:06:02 +02001099psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1100 psa_key_type_t type,
1101 const uint8_t *data,
1102 size_t data_length )
1103{
1104 uint8_t *copy = NULL;
1105
1106 if( data_length != 0 )
1107 {
1108 copy = mbedtls_calloc( 1, data_length );
1109 if( copy == NULL )
1110 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1111 memcpy( copy, data, data_length );
1112 }
1113 /* After this point, this function is guaranteed to succeed, so it
1114 * can start modifying `*attributes`. */
1115
1116 if( attributes->domain_parameters != NULL )
1117 {
1118 mbedtls_free( attributes->domain_parameters );
1119 attributes->domain_parameters = NULL;
1120 attributes->domain_parameters_size = 0;
1121 }
1122
1123 attributes->domain_parameters = copy;
1124 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001125 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001126 return( PSA_SUCCESS );
1127}
1128
1129psa_status_t psa_get_key_domain_parameters(
1130 const psa_key_attributes_t *attributes,
1131 uint8_t *data, size_t data_size, size_t *data_length )
1132{
1133 if( attributes->domain_parameters_size > data_size )
1134 return( PSA_ERROR_BUFFER_TOO_SMALL );
1135 *data_length = attributes->domain_parameters_size;
1136 if( attributes->domain_parameters_size != 0 )
1137 memcpy( data, attributes->domain_parameters,
1138 attributes->domain_parameters_size );
1139 return( PSA_SUCCESS );
1140}
1141
1142#if defined(MBEDTLS_RSA_C)
1143static psa_status_t psa_get_rsa_public_exponent(
1144 const mbedtls_rsa_context *rsa,
1145 psa_key_attributes_t *attributes )
1146{
1147 mbedtls_mpi mpi;
1148 int ret;
1149 uint8_t *buffer = NULL;
1150 size_t buflen;
1151 mbedtls_mpi_init( &mpi );
1152
1153 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1154 if( ret != 0 )
1155 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001156 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1157 {
1158 /* It's the default value, which is reported as an empty string,
1159 * so there's nothing to do. */
1160 goto exit;
1161 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001162
1163 buflen = mbedtls_mpi_size( &mpi );
1164 buffer = mbedtls_calloc( 1, buflen );
1165 if( buffer == NULL )
1166 {
1167 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1168 goto exit;
1169 }
1170 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1171 if( ret != 0 )
1172 goto exit;
1173 attributes->domain_parameters = buffer;
1174 attributes->domain_parameters_size = buflen;
1175
1176exit:
1177 mbedtls_mpi_free( &mpi );
1178 if( ret != 0 )
1179 mbedtls_free( buffer );
1180 return( mbedtls_to_psa_error( ret ) );
1181}
1182#endif /* MBEDTLS_RSA_C */
1183
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001184/** Retrieve all the publicly-accessible attributes of a key.
1185 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001186psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1187 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001189 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001190 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001192 psa_reset_key_attributes( attributes );
1193
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001194 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001195 if( status != PSA_SUCCESS )
1196 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001197
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001198 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001199 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1200 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1201
1202#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1203 if( psa_key_slot_is_external( slot ) )
1204 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001206
Gilles Peskine8e338702019-07-30 20:06:31 +02001207 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 {
1209#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001210 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001212#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001213 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001214 * is not yet implemented.
1215 * https://github.com/ARMmbed/mbed-crypto/issues/216
1216 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001217 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001218 break;
1219#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1221 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001222#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 default:
1224 /* Nothing else to do. */
1225 break;
1226 }
1227
1228 if( status != PSA_SUCCESS )
1229 psa_reset_key_attributes( attributes );
1230 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231}
1232
Gilles Peskinec8000c02019-08-02 20:15:51 +02001233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1234psa_status_t psa_get_key_slot_number(
1235 const psa_key_attributes_t *attributes,
1236 psa_key_slot_number_t *slot_number )
1237{
1238 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1239 {
1240 *slot_number = attributes->slot_number;
1241 return( PSA_SUCCESS );
1242 }
1243 else
1244 return( PSA_ERROR_INVALID_ARGUMENT );
1245}
1246#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1247
Jaeden Ameroccdce902019-01-10 11:42:27 +00001248#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001249static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1250 unsigned char *buf, size_t size )
1251{
1252 int ret;
1253 unsigned char *c;
1254 size_t len = 0;
1255
1256 c = buf + size;
1257
1258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1259
1260 return( (int) len );
1261}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001262#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001263
Gilles Peskinef603c712019-01-19 13:40:11 +01001264static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1265 uint8_t *data,
1266 size_t data_size,
1267 size_t *data_length,
1268 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269{
Gilles Peskine5d309672019-07-12 23:47:28 +02001270#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1271 const psa_drv_se_t *drv;
1272 psa_drv_se_context_t *drv_context;
1273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1274
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001275 *data_length = 0;
1276
Gilles Peskine8e338702019-07-30 20:06:31 +02001277 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001278 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001279
Gilles Peskinef9168942019-09-12 19:20:29 +02001280 /* Reject a zero-length output buffer now, since this can never be a
1281 * valid key representation. This way we know that data must be a valid
1282 * pointer and we can do things like memset(data, ..., data_size). */
1283 if( data_size == 0 )
1284 return( PSA_ERROR_BUFFER_TOO_SMALL );
1285
Gilles Peskine5d309672019-07-12 23:47:28 +02001286#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001287 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001288 {
1289 psa_drv_se_export_key_t method;
1290 if( drv->key_management == NULL )
1291 return( PSA_ERROR_NOT_SUPPORTED );
1292 method = ( export_public_key ?
1293 drv->key_management->p_export_public :
1294 drv->key_management->p_export );
1295 if( method == NULL )
1296 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001297 return( method( drv_context,
1298 slot->data.se.slot_number,
1299 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001300 }
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1302
Gilles Peskine8e338702019-07-30 20:06:31 +02001303 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304 {
1305 if( slot->data.raw.bytes > data_size )
1306 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001307 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1308 memset( data + slot->data.raw.bytes, 0,
1309 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310 *data_length = slot->data.raw.bytes;
1311 return( PSA_SUCCESS );
1312 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001313#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001315 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001316 psa_status_t status;
1317
Gilles Peskineb46bef22019-07-30 21:32:04 +02001318 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001319 if( bytes > data_size )
1320 return( PSA_ERROR_BUFFER_TOO_SMALL );
1321 status = mbedtls_to_psa_error(
1322 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1323 if( status != PSA_SUCCESS )
1324 return( status );
1325 memset( data + bytes, 0, data_size - bytes );
1326 *data_length = bytes;
1327 return( PSA_SUCCESS );
1328 }
1329#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330 else
Moran Peker17e36e12018-05-02 12:55:20 +03001331 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001332#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001333 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1334 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001335 {
Moran Pekera998bc62018-04-16 18:16:20 +03001336 mbedtls_pk_context pk;
1337 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001338 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001339 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001340#if defined(MBEDTLS_RSA_C)
1341 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001342 pk.pk_info = &mbedtls_rsa_info;
1343 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001344#else
1345 return( PSA_ERROR_NOT_SUPPORTED );
1346#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001347 }
1348 else
1349 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001350#if defined(MBEDTLS_ECP_C)
1351 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001352 pk.pk_info = &mbedtls_eckey_info;
1353 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001354#else
1355 return( PSA_ERROR_NOT_SUPPORTED );
1356#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001357 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001358 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001359 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001360 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001361 }
Moran Peker17e36e12018-05-02 12:55:20 +03001362 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001363 {
Moran Peker17e36e12018-05-02 12:55:20 +03001364 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001365 }
Moran Peker60364322018-04-29 11:34:58 +03001366 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001368 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001369 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001370 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001371 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1372 * Move the data to the beginning and erase remaining data
1373 * at the original location. */
1374 if( 2 * (size_t) ret <= data_size )
1375 {
1376 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001377 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001378 }
1379 else if( (size_t) ret < data_size )
1380 {
1381 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001382 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001383 }
Moran Pekera998bc62018-04-16 18:16:20 +03001384 *data_length = ret;
1385 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001386 }
1387 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001388#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001389 {
1390 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001391 it is valid for a special-purpose implementation to omit
1392 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 return( PSA_ERROR_NOT_SUPPORTED );
1394 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395 }
1396}
1397
Gilles Peskinec5487a82018-12-03 18:08:14 +01001398psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001399 uint8_t *data,
1400 size_t data_size,
1401 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001402{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001403 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001404 psa_status_t status;
1405
1406 /* Set the key to empty now, so that even when there are errors, we always
1407 * set data_length to a value between 0 and data_size. On error, setting
1408 * the key to empty is a good choice because an empty key representation is
1409 * unlikely to be accepted anywhere. */
1410 *data_length = 0;
1411
1412 /* Export requires the EXPORT flag. There is an exception for public keys,
1413 * which don't require any flag, but psa_get_key_from_slot takes
1414 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001415 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001416 if( status != PSA_SUCCESS )
1417 return( status );
1418 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001419 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001420}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421
Gilles Peskinec5487a82018-12-03 18:08:14 +01001422psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001423 uint8_t *data,
1424 size_t data_size,
1425 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001427 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 psa_status_t status;
1429
1430 /* Set the key to empty now, so that even when there are errors, we always
1431 * set data_length to a value between 0 and data_size. On error, setting
1432 * the key to empty is a good choice because an empty key representation is
1433 * unlikely to be accepted anywhere. */
1434 *data_length = 0;
1435
1436 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001437 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001438 if( status != PSA_SUCCESS )
1439 return( status );
1440 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001441 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001442}
1443
Gilles Peskine91e8c332019-08-02 19:19:39 +02001444#if defined(static_assert)
1445static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1446 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001447static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001448 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001449static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001450 "One or more key attribute flag is listed as both internal-only and external-only" );
1451#endif
1452
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001453/** Validate that a key policy is internally well-formed.
1454 *
1455 * This function only rejects invalid policies. It does not validate the
1456 * consistency of the policy with respect to other attributes of the key
1457 * such as the key type.
1458 */
1459static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001460{
1461 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001462 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 PSA_KEY_USAGE_ENCRYPT |
1464 PSA_KEY_USAGE_DECRYPT |
1465 PSA_KEY_USAGE_SIGN |
1466 PSA_KEY_USAGE_VERIFY |
1467 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1468 return( PSA_ERROR_INVALID_ARGUMENT );
1469
Gilles Peskine4747d192019-04-17 15:05:45 +02001470 return( PSA_SUCCESS );
1471}
1472
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001473/** Validate the internal consistency of key attributes.
1474 *
1475 * This function only rejects invalid attribute values. If does not
1476 * validate the consistency of the attributes with any key data that may
1477 * be involved in the creation of the key.
1478 *
1479 * Call this function early in the key creation process.
1480 *
1481 * \param[in] attributes Key attributes for the new key.
1482 * \param[out] p_drv On any return, the driver for the key, if any.
1483 * NULL for a transparent key.
1484 *
1485 */
1486static psa_status_t psa_validate_key_attributes(
1487 const psa_key_attributes_t *attributes,
1488 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001489{
1490 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491
1492 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001493 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001494 status = psa_validate_persistent_key_parameters(
1495 attributes->core.lifetime, attributes->core.id,
1496 p_drv, 1 );
1497 if( status != PSA_SUCCESS )
1498 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500
1501 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001502 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503 return( status );
1504
1505 /* Refuse to create overly large keys.
1506 * Note that this doesn't trigger on import if the attributes don't
1507 * explicitly specify a size (so psa_get_key_bits returns 0), so
1508 * psa_import_key() needs its own checks. */
1509 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1510 return( PSA_ERROR_NOT_SUPPORTED );
1511
Gilles Peskine91e8c332019-08-02 19:19:39 +02001512 /* Reject invalid flags. These should not be reachable through the API. */
1513 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1514 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1515 return( PSA_ERROR_INVALID_ARGUMENT );
1516
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001517 return( PSA_SUCCESS );
1518}
1519
Gilles Peskine4747d192019-04-17 15:05:45 +02001520/** Prepare a key slot to receive key material.
1521 *
1522 * This function allocates a key slot and sets its metadata.
1523 *
1524 * If this function fails, call psa_fail_key_creation().
1525 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001526 * This function is intended to be used as follows:
1527 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1528 * it with the specified attributes, and assign it a handle.
1529 * -# Populate the slot with the key material.
1530 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1531 * In case of failure at any step, stop the sequence and call
1532 * psa_fail_key_creation().
1533 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001534 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001535 * \param[in] attributes Key attributes for the new key.
1536 * \param[out] handle On success, a handle for the allocated slot.
1537 * \param[out] p_slot On success, a pointer to the prepared slot.
1538 * \param[out] p_drv On any return, the driver for the key, if any.
1539 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * The key slot is ready to receive key material.
1543 * \return If this function fails, the key slot is an invalid state.
1544 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 */
1546static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001547 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001548 const psa_key_attributes_t *attributes,
1549 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001550 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001551 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001552{
1553 psa_status_t status;
1554 psa_key_slot_t *slot;
1555
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001557 *p_drv = NULL;
1558
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559 status = psa_validate_key_attributes( attributes, p_drv );
1560 if( status != PSA_SUCCESS )
1561 return( status );
1562
Gilles Peskineedbed562019-08-07 18:19:59 +02001563 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001564 if( status != PSA_SUCCESS )
1565 return( status );
1566 slot = *p_slot;
1567
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001568 /* We're storing the declared bit-size of the key. It's up to each
1569 * creation mechanism to verify that this information is correct.
1570 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001571 * an input (generate, device) but not for those where the bit-size
1572 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001573
1574 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001575
Gilles Peskine91e8c332019-08-02 19:19:39 +02001576 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * external-only flags, query `attributes`. Thanks to the check
1578 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001579 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001580 * may have set. */
1581 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001582
Gilles Peskinecbaff462019-07-12 23:46:04 +02001583#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001584 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001585 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001586 * create the key file in internal storage, create the
1587 * key inside the secure element, and update the driver's
1588 * persistent data. Start a transaction that will encompass these
1589 * three actions. */
1590 /* The first thing to do is to find a slot number for the new key.
1591 * We save the slot number in persistent storage as part of the
1592 * transaction data. It will be needed to recover if the power
1593 * fails during the key creation process, to clean up on the secure
1594 * element side after restarting. Obtaining a slot number from the
1595 * secure element driver updates its persistent state, but we do not yet
1596 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001597 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001598 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001599 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001600 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001601 &slot->data.se.slot_number );
1602 if( status != PSA_SUCCESS )
1603 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001604 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001605 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001606 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001607 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 status = psa_crypto_save_transaction( );
1609 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 {
1611 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001612 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001613 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001615
1616 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1617 {
1618 /* Key registration only makes sense with a secure element. */
1619 return( PSA_ERROR_INVALID_ARGUMENT );
1620 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001621#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1622
Darryl Green0c6575a2018-11-07 16:05:30 +00001623 return( status );
1624}
Gilles Peskine4747d192019-04-17 15:05:45 +02001625
1626/** Finalize the creation of a key once its key material has been set.
1627 *
1628 * This entails writing the key to persistent storage.
1629 *
1630 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 * See the documentation of psa_start_key_creation() for the intended use
1632 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001634 * \param[in,out] slot Pointer to the slot with key material.
1635 * \param[in] driver The secure element driver for the key,
1636 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001637 *
1638 * \retval #PSA_SUCCESS
1639 * The key was successfully created. The handle is now valid.
1640 * \return If this function fails, the key slot is an invalid state.
1641 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001643static psa_status_t psa_finish_key_creation(
1644 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001645 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001646{
1647 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001648 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001649 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001650
1651#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001652 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001654#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1655 if( driver != NULL )
1656 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001657 psa_se_key_data_storage_t data;
1658#if defined(static_assert)
1659 static_assert( sizeof( slot->data.se.slot_number ) ==
1660 sizeof( data.slot_number ),
1661 "Slot number size does not match psa_se_key_data_storage_t" );
1662 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1663 "Bit-size size does not match psa_se_key_data_storage_t" );
1664#endif
1665 memcpy( &data.slot_number, &slot->data.se.slot_number,
1666 sizeof( slot->data.se.slot_number ) );
1667 memcpy( &data.bits, &slot->attr.bits,
1668 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001669 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001670 (uint8_t*) &data,
1671 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001672 }
1673 else
1674#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1675 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001676 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001677 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001678 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001679 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1680 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001681 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001682 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1683 status = psa_internal_export_key( slot,
1684 buffer, buffer_size, &length,
1685 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001686 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001687 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001688 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001689
Gilles Peskinef9168942019-09-12 19:20:29 +02001690 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001691 mbedtls_free( buffer );
1692 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001694#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1695
Gilles Peskinecbaff462019-07-12 23:46:04 +02001696#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001697 /* Finish the transaction for a key creation. This does not
1698 * happen when registering an existing key. Detect this case
1699 * by checking whether a transaction is in progress (actual
1700 * creation of a key in a secure element requires a transaction,
1701 * but registration doesn't use one). */
1702 if( driver != NULL &&
1703 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001704 {
1705 status = psa_save_se_persistent_data( driver );
1706 if( status != PSA_SUCCESS )
1707 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001708 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001709 return( status );
1710 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001711 status = psa_crypto_stop_transaction( );
1712 if( status != PSA_SUCCESS )
1713 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001714 }
1715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1716
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 return( status );
1718}
1719
1720/** Abort the creation of a key.
1721 *
1722 * You may call this function after calling psa_start_key_creation(),
1723 * or after psa_finish_key_creation() fails. In other circumstances, this
1724 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001725 * See the documentation of psa_start_key_creation() for the intended use
1726 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001727 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001728 * \param[in,out] slot Pointer to the slot with key material.
1729 * \param[in] driver The secure element driver for the key,
1730 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001731 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001732static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001733 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001734{
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 (void) driver;
1736
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 if( slot == NULL )
1738 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001739
1740#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001741 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 * element, and the failure happened later (when saving metadata
1743 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001744 * element.
1745 * https://github.com/ARMmbed/mbed-crypto/issues/217
1746 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001747
Gilles Peskined7729582019-08-05 15:55:54 +02001748 /* Abort the ongoing transaction if any (there may not be one if
1749 * the creation process failed before starting one, or if the
1750 * key creation is a registration of a key in a secure element).
1751 * Earlier functions must already have done what it takes to undo any
1752 * partial creation. All that's left is to update the transaction data
1753 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001754 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001755#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1756
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 psa_wipe_key_slot( slot );
1758}
1759
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001760/** Validate optional attributes during key creation.
1761 *
1762 * Some key attributes are optional during key creation. If they are
1763 * specified in the attributes structure, check that they are consistent
1764 * with the data in the slot.
1765 *
1766 * This function should be called near the end of key creation, after
1767 * the slot in memory is fully populated but before saving persistent data.
1768 */
1769static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001770 const psa_key_slot_t *slot,
1771 const psa_key_attributes_t *attributes )
1772{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001773 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001774 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001775 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001776 return( PSA_ERROR_INVALID_ARGUMENT );
1777 }
1778
1779 if( attributes->domain_parameters_size != 0 )
1780 {
1781#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001782 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 {
1784 mbedtls_mpi actual, required;
1785 int ret;
1786 mbedtls_mpi_init( &actual );
1787 mbedtls_mpi_init( &required );
1788 ret = mbedtls_rsa_export( slot->data.rsa,
1789 NULL, NULL, NULL, NULL, &actual );
1790 if( ret != 0 )
1791 goto rsa_exit;
1792 ret = mbedtls_mpi_read_binary( &required,
1793 attributes->domain_parameters,
1794 attributes->domain_parameters_size );
1795 if( ret != 0 )
1796 goto rsa_exit;
1797 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1798 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1799 rsa_exit:
1800 mbedtls_mpi_free( &actual );
1801 mbedtls_mpi_free( &required );
1802 if( ret != 0)
1803 return( mbedtls_to_psa_error( ret ) );
1804 }
1805 else
1806#endif
1807 {
1808 return( PSA_ERROR_INVALID_ARGUMENT );
1809 }
1810 }
1811
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001812 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001813 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001814 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 return( PSA_ERROR_INVALID_ARGUMENT );
1816 }
1817
1818 return( PSA_SUCCESS );
1819}
1820
Gilles Peskine4747d192019-04-17 15:05:45 +02001821psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001823 size_t data_length,
1824 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001825{
1826 psa_status_t status;
1827 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001828 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829
Gilles Peskine0f84d622019-09-12 19:03:13 +02001830 /* Reject zero-length symmetric keys (including raw data key objects).
1831 * This also rejects any key which might be encoded as an empty string,
1832 * which is never valid. */
1833 if( data_length == 0 )
1834 return( PSA_ERROR_INVALID_ARGUMENT );
1835
Gilles Peskinedf179142019-07-15 22:02:14 +02001836 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1837 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001838 if( status != PSA_SUCCESS )
1839 goto exit;
1840
Gilles Peskine5d309672019-07-12 23:47:28 +02001841#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1842 if( driver != NULL )
1843 {
1844 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001845 /* The driver should set the number of key bits, however in
1846 * case it doesn't, we initialize bits to an invalid value. */
1847 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001848 if( drv->key_management == NULL ||
1849 drv->key_management->p_import == NULL )
1850 {
1851 status = PSA_ERROR_NOT_SUPPORTED;
1852 goto exit;
1853 }
1854 status = drv->key_management->p_import(
1855 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001856 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001857 &bits );
1858 if( status != PSA_SUCCESS )
1859 goto exit;
1860 if( bits > PSA_MAX_KEY_BITS )
1861 {
1862 status = PSA_ERROR_NOT_SUPPORTED;
1863 goto exit;
1864 }
1865 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001866 }
1867 else
1868#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1869 {
1870 status = psa_import_key_into_slot( slot, data, data_length );
1871 if( status != PSA_SUCCESS )
1872 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001873 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001874 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001875 if( status != PSA_SUCCESS )
1876 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001877
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 if( status != PSA_SUCCESS )
1881 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001883 *handle = 0;
1884 }
1885 return( status );
1886}
1887
Gilles Peskined7729582019-08-05 15:55:54 +02001888#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1889psa_status_t mbedtls_psa_register_se_key(
1890 const psa_key_attributes_t *attributes )
1891{
1892 psa_status_t status;
1893 psa_key_slot_t *slot = NULL;
1894 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001895 psa_key_handle_t handle = 0;
1896
1897 /* Leaving attributes unspecified is not currently supported.
1898 * It could make sense to query the key type and size from the
1899 * secure element, but not all secure elements support this
1900 * and the driver HAL doesn't currently support it. */
1901 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1902 return( PSA_ERROR_NOT_SUPPORTED );
1903 if( psa_get_key_bits( attributes ) == 0 )
1904 return( PSA_ERROR_NOT_SUPPORTED );
1905
1906 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1907 &handle, &slot, &driver );
1908 if( status != PSA_SUCCESS )
1909 goto exit;
1910
Gilles Peskined7729582019-08-05 15:55:54 +02001911 status = psa_finish_key_creation( slot, driver );
1912
1913exit:
1914 if( status != PSA_SUCCESS )
1915 {
1916 psa_fail_key_creation( slot, driver );
1917 }
1918 /* Registration doesn't keep the key in RAM. */
1919 psa_close_key( handle );
1920 return( status );
1921}
1922#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1923
Gilles Peskinef603c712019-01-19 13:40:11 +01001924static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001925 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001926{
1927 psa_status_t status;
1928 uint8_t *buffer = NULL;
1929 size_t buffer_size = 0;
1930 size_t length;
1931
Gilles Peskine8e338702019-07-30 20:06:31 +02001932 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001933 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001934 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001935 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001937 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1938 if( status != PSA_SUCCESS )
1939 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001940 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001941 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001942
1943exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001944 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001945 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001946 return( status );
1947}
1948
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001949psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1950 const psa_key_attributes_t *specified_attributes,
1951 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001952{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001953 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001954 psa_key_slot_t *source_slot = NULL;
1955 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001956 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001957 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001958
Gilles Peskine28f8f302019-07-24 13:30:31 +02001959 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001960 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001961 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001962 goto exit;
1963
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001964 status = psa_validate_optional_attributes( source_slot,
1965 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001966 if( status != PSA_SUCCESS )
1967 goto exit;
1968
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001969 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001970 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 if( status != PSA_SUCCESS )
1972 goto exit;
1973
Gilles Peskinedf179142019-07-15 22:02:14 +02001974 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1975 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1981 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001982 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001983 /* Copying to a secure element is not implemented yet. */
1984 status = PSA_ERROR_NOT_SUPPORTED;
1985 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001986 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001988
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001989 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001990 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001992
Gilles Peskine011e4282019-06-26 18:34:38 +02001993 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 if( status != PSA_SUCCESS )
1996 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001997 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001998 *target_handle = 0;
1999 }
2000 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002001}
2002
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002003
2004
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002005/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002006/* Message digests */
2007/****************************************************************/
2008
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002009static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002010{
2011 switch( alg )
2012 {
2013#if defined(MBEDTLS_MD2_C)
2014 case PSA_ALG_MD2:
2015 return( &mbedtls_md2_info );
2016#endif
2017#if defined(MBEDTLS_MD4_C)
2018 case PSA_ALG_MD4:
2019 return( &mbedtls_md4_info );
2020#endif
2021#if defined(MBEDTLS_MD5_C)
2022 case PSA_ALG_MD5:
2023 return( &mbedtls_md5_info );
2024#endif
2025#if defined(MBEDTLS_RIPEMD160_C)
2026 case PSA_ALG_RIPEMD160:
2027 return( &mbedtls_ripemd160_info );
2028#endif
2029#if defined(MBEDTLS_SHA1_C)
2030 case PSA_ALG_SHA_1:
2031 return( &mbedtls_sha1_info );
2032#endif
2033#if defined(MBEDTLS_SHA256_C)
2034 case PSA_ALG_SHA_224:
2035 return( &mbedtls_sha224_info );
2036 case PSA_ALG_SHA_256:
2037 return( &mbedtls_sha256_info );
2038#endif
2039#if defined(MBEDTLS_SHA512_C)
2040 case PSA_ALG_SHA_384:
2041 return( &mbedtls_sha384_info );
2042 case PSA_ALG_SHA_512:
2043 return( &mbedtls_sha512_info );
2044#endif
2045 default:
2046 return( NULL );
2047 }
2048}
2049
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002050psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2051{
2052 switch( operation->alg )
2053 {
Gilles Peskine81736312018-06-26 15:04:31 +02002054 case 0:
2055 /* The object has (apparently) been initialized but it is not
2056 * in use. It's ok to call abort on such an object, and there's
2057 * nothing to do. */
2058 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002059#if defined(MBEDTLS_MD2_C)
2060 case PSA_ALG_MD2:
2061 mbedtls_md2_free( &operation->ctx.md2 );
2062 break;
2063#endif
2064#if defined(MBEDTLS_MD4_C)
2065 case PSA_ALG_MD4:
2066 mbedtls_md4_free( &operation->ctx.md4 );
2067 break;
2068#endif
2069#if defined(MBEDTLS_MD5_C)
2070 case PSA_ALG_MD5:
2071 mbedtls_md5_free( &operation->ctx.md5 );
2072 break;
2073#endif
2074#if defined(MBEDTLS_RIPEMD160_C)
2075 case PSA_ALG_RIPEMD160:
2076 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2077 break;
2078#endif
2079#if defined(MBEDTLS_SHA1_C)
2080 case PSA_ALG_SHA_1:
2081 mbedtls_sha1_free( &operation->ctx.sha1 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_SHA256_C)
2085 case PSA_ALG_SHA_224:
2086 case PSA_ALG_SHA_256:
2087 mbedtls_sha256_free( &operation->ctx.sha256 );
2088 break;
2089#endif
2090#if defined(MBEDTLS_SHA512_C)
2091 case PSA_ALG_SHA_384:
2092 case PSA_ALG_SHA_512:
2093 mbedtls_sha512_free( &operation->ctx.sha512 );
2094 break;
2095#endif
2096 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002097 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002098 }
2099 operation->alg = 0;
2100 return( PSA_SUCCESS );
2101}
2102
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002103psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002104 psa_algorithm_t alg )
2105{
2106 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002107
2108 /* A context must be freshly initialized before it can be set up. */
2109 if( operation->alg != 0 )
2110 {
2111 return( PSA_ERROR_BAD_STATE );
2112 }
2113
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 switch( alg )
2115 {
2116#if defined(MBEDTLS_MD2_C)
2117 case PSA_ALG_MD2:
2118 mbedtls_md2_init( &operation->ctx.md2 );
2119 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2120 break;
2121#endif
2122#if defined(MBEDTLS_MD4_C)
2123 case PSA_ALG_MD4:
2124 mbedtls_md4_init( &operation->ctx.md4 );
2125 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2126 break;
2127#endif
2128#if defined(MBEDTLS_MD5_C)
2129 case PSA_ALG_MD5:
2130 mbedtls_md5_init( &operation->ctx.md5 );
2131 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2132 break;
2133#endif
2134#if defined(MBEDTLS_RIPEMD160_C)
2135 case PSA_ALG_RIPEMD160:
2136 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2137 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2138 break;
2139#endif
2140#if defined(MBEDTLS_SHA1_C)
2141 case PSA_ALG_SHA_1:
2142 mbedtls_sha1_init( &operation->ctx.sha1 );
2143 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2144 break;
2145#endif
2146#if defined(MBEDTLS_SHA256_C)
2147 case PSA_ALG_SHA_224:
2148 mbedtls_sha256_init( &operation->ctx.sha256 );
2149 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2150 break;
2151 case PSA_ALG_SHA_256:
2152 mbedtls_sha256_init( &operation->ctx.sha256 );
2153 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2154 break;
2155#endif
2156#if defined(MBEDTLS_SHA512_C)
2157 case PSA_ALG_SHA_384:
2158 mbedtls_sha512_init( &operation->ctx.sha512 );
2159 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2160 break;
2161 case PSA_ALG_SHA_512:
2162 mbedtls_sha512_init( &operation->ctx.sha512 );
2163 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2164 break;
2165#endif
2166 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002167 return( PSA_ALG_IS_HASH( alg ) ?
2168 PSA_ERROR_NOT_SUPPORTED :
2169 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002170 }
2171 if( ret == 0 )
2172 operation->alg = alg;
2173 else
2174 psa_hash_abort( operation );
2175 return( mbedtls_to_psa_error( ret ) );
2176}
2177
2178psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2179 const uint8_t *input,
2180 size_t input_length )
2181{
2182 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002183
2184 /* Don't require hash implementations to behave correctly on a
2185 * zero-length input, which may have an invalid pointer. */
2186 if( input_length == 0 )
2187 return( PSA_SUCCESS );
2188
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189 switch( operation->alg )
2190 {
2191#if defined(MBEDTLS_MD2_C)
2192 case PSA_ALG_MD2:
2193 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2194 input, input_length );
2195 break;
2196#endif
2197#if defined(MBEDTLS_MD4_C)
2198 case PSA_ALG_MD4:
2199 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2200 input, input_length );
2201 break;
2202#endif
2203#if defined(MBEDTLS_MD5_C)
2204 case PSA_ALG_MD5:
2205 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2206 input, input_length );
2207 break;
2208#endif
2209#if defined(MBEDTLS_RIPEMD160_C)
2210 case PSA_ALG_RIPEMD160:
2211 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2212 input, input_length );
2213 break;
2214#endif
2215#if defined(MBEDTLS_SHA1_C)
2216 case PSA_ALG_SHA_1:
2217 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2218 input, input_length );
2219 break;
2220#endif
2221#if defined(MBEDTLS_SHA256_C)
2222 case PSA_ALG_SHA_224:
2223 case PSA_ALG_SHA_256:
2224 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2225 input, input_length );
2226 break;
2227#endif
2228#if defined(MBEDTLS_SHA512_C)
2229 case PSA_ALG_SHA_384:
2230 case PSA_ALG_SHA_512:
2231 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2232 input, input_length );
2233 break;
2234#endif
2235 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002236 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002238
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 if( ret != 0 )
2240 psa_hash_abort( operation );
2241 return( mbedtls_to_psa_error( ret ) );
2242}
2243
2244psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2245 uint8_t *hash,
2246 size_t hash_size,
2247 size_t *hash_length )
2248{
itayzafrir40835d42018-08-02 13:14:17 +03002249 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002251 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252
2253 /* Fill the output buffer with something that isn't a valid hash
2254 * (barring an attack on the hash and deliberately-crafted input),
2255 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002256 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002257 /* If hash_size is 0 then hash may be NULL and then the
2258 * call to memset would have undefined behavior. */
2259 if( hash_size != 0 )
2260 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261
2262 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002263 {
2264 status = PSA_ERROR_BUFFER_TOO_SMALL;
2265 goto exit;
2266 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267
2268 switch( operation->alg )
2269 {
2270#if defined(MBEDTLS_MD2_C)
2271 case PSA_ALG_MD2:
2272 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2273 break;
2274#endif
2275#if defined(MBEDTLS_MD4_C)
2276 case PSA_ALG_MD4:
2277 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2278 break;
2279#endif
2280#if defined(MBEDTLS_MD5_C)
2281 case PSA_ALG_MD5:
2282 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2283 break;
2284#endif
2285#if defined(MBEDTLS_RIPEMD160_C)
2286 case PSA_ALG_RIPEMD160:
2287 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2288 break;
2289#endif
2290#if defined(MBEDTLS_SHA1_C)
2291 case PSA_ALG_SHA_1:
2292 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2293 break;
2294#endif
2295#if defined(MBEDTLS_SHA256_C)
2296 case PSA_ALG_SHA_224:
2297 case PSA_ALG_SHA_256:
2298 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2299 break;
2300#endif
2301#if defined(MBEDTLS_SHA512_C)
2302 case PSA_ALG_SHA_384:
2303 case PSA_ALG_SHA_512:
2304 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2305 break;
2306#endif
2307 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002308 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309 }
itayzafrir40835d42018-08-02 13:14:17 +03002310 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311
itayzafrir40835d42018-08-02 13:14:17 +03002312exit:
2313 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002315 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 return( psa_hash_abort( operation ) );
2317 }
2318 else
2319 {
2320 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002321 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 }
2323}
2324
Gilles Peskine2d277862018-06-18 15:41:12 +02002325psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2326 const uint8_t *hash,
2327 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328{
2329 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2330 size_t actual_hash_length;
2331 psa_status_t status = psa_hash_finish( operation,
2332 actual_hash, sizeof( actual_hash ),
2333 &actual_hash_length );
2334 if( status != PSA_SUCCESS )
2335 return( status );
2336 if( actual_hash_length != hash_length )
2337 return( PSA_ERROR_INVALID_SIGNATURE );
2338 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2339 return( PSA_ERROR_INVALID_SIGNATURE );
2340 return( PSA_SUCCESS );
2341}
2342
Gilles Peskineeb35d782019-01-22 17:56:16 +01002343psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2344 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002345{
2346 if( target_operation->alg != 0 )
2347 return( PSA_ERROR_BAD_STATE );
2348
2349 switch( source_operation->alg )
2350 {
2351 case 0:
2352 return( PSA_ERROR_BAD_STATE );
2353#if defined(MBEDTLS_MD2_C)
2354 case PSA_ALG_MD2:
2355 mbedtls_md2_clone( &target_operation->ctx.md2,
2356 &source_operation->ctx.md2 );
2357 break;
2358#endif
2359#if defined(MBEDTLS_MD4_C)
2360 case PSA_ALG_MD4:
2361 mbedtls_md4_clone( &target_operation->ctx.md4,
2362 &source_operation->ctx.md4 );
2363 break;
2364#endif
2365#if defined(MBEDTLS_MD5_C)
2366 case PSA_ALG_MD5:
2367 mbedtls_md5_clone( &target_operation->ctx.md5,
2368 &source_operation->ctx.md5 );
2369 break;
2370#endif
2371#if defined(MBEDTLS_RIPEMD160_C)
2372 case PSA_ALG_RIPEMD160:
2373 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2374 &source_operation->ctx.ripemd160 );
2375 break;
2376#endif
2377#if defined(MBEDTLS_SHA1_C)
2378 case PSA_ALG_SHA_1:
2379 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2380 &source_operation->ctx.sha1 );
2381 break;
2382#endif
2383#if defined(MBEDTLS_SHA256_C)
2384 case PSA_ALG_SHA_224:
2385 case PSA_ALG_SHA_256:
2386 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2387 &source_operation->ctx.sha256 );
2388 break;
2389#endif
2390#if defined(MBEDTLS_SHA512_C)
2391 case PSA_ALG_SHA_384:
2392 case PSA_ALG_SHA_512:
2393 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2394 &source_operation->ctx.sha512 );
2395 break;
2396#endif
2397 default:
2398 return( PSA_ERROR_NOT_SUPPORTED );
2399 }
2400
2401 target_operation->alg = source_operation->alg;
2402 return( PSA_SUCCESS );
2403}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404
2405
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407/* MAC */
2408/****************************************************************/
2409
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002410static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 psa_algorithm_t alg,
2412 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002413 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002414 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002417 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002419 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002420 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002421
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2423 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002424 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002426 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002427 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428 mode = MBEDTLS_MODE_STREAM;
2429 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 case PSA_ALG_CTR:
2431 mode = MBEDTLS_MODE_CTR;
2432 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002433 case PSA_ALG_CFB:
2434 mode = MBEDTLS_MODE_CFB;
2435 break;
2436 case PSA_ALG_OFB:
2437 mode = MBEDTLS_MODE_OFB;
2438 break;
2439 case PSA_ALG_CBC_NO_PADDING:
2440 mode = MBEDTLS_MODE_CBC;
2441 break;
2442 case PSA_ALG_CBC_PKCS7:
2443 mode = MBEDTLS_MODE_CBC;
2444 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002445 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446 mode = MBEDTLS_MODE_CCM;
2447 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002448 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449 mode = MBEDTLS_MODE_GCM;
2450 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002451 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2452 mode = MBEDTLS_MODE_CHACHAPOLY;
2453 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 default:
2455 return( NULL );
2456 }
2457 }
2458 else if( alg == PSA_ALG_CMAC )
2459 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460 else
2461 return( NULL );
2462
2463 switch( key_type )
2464 {
2465 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002466 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 break;
2468 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002469 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2470 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002471 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002472 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002474 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002475 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2476 * but two-key Triple-DES is functionally three-key Triple-DES
2477 * with K1=K3, so that's how we present it to mbedtls. */
2478 if( key_bits == 128 )
2479 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 break;
2481 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002482 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 break;
2484 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002485 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002487 case PSA_KEY_TYPE_CHACHA20:
2488 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2489 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 default:
2491 return( NULL );
2492 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002493 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002494 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495
Jaeden Amero23bbb752018-06-26 14:16:54 +01002496 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2497 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498}
2499
Gilles Peskinea05219c2018-11-16 16:02:56 +01002500#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002501static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002502{
Gilles Peskine2d277862018-06-18 15:41:12 +02002503 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002504 {
2505 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002506 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002507 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002508 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002509 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002510 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002511 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002512 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002513 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002514 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002515 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002516 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002517 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002518 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002519 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002520 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002521 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002522 return( 128 );
2523 default:
2524 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002525 }
2526}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002527#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002528
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002529/* Initialize the MAC operation structure. Once this function has been
2530 * called, psa_mac_abort can run and will do the right thing. */
2531static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2532 psa_algorithm_t alg )
2533{
2534 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2535
2536 operation->alg = alg;
2537 operation->key_set = 0;
2538 operation->iv_set = 0;
2539 operation->iv_required = 0;
2540 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002541 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002542
2543#if defined(MBEDTLS_CMAC_C)
2544 if( alg == PSA_ALG_CMAC )
2545 {
2546 operation->iv_required = 0;
2547 mbedtls_cipher_init( &operation->ctx.cmac );
2548 status = PSA_SUCCESS;
2549 }
2550 else
2551#endif /* MBEDTLS_CMAC_C */
2552#if defined(MBEDTLS_MD_C)
2553 if( PSA_ALG_IS_HMAC( operation->alg ) )
2554 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002555 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2556 operation->ctx.hmac.hash_ctx.alg = 0;
2557 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002558 }
2559 else
2560#endif /* MBEDTLS_MD_C */
2561 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002562 if( ! PSA_ALG_IS_MAC( alg ) )
2563 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002564 }
2565
2566 if( status != PSA_SUCCESS )
2567 memset( operation, 0, sizeof( *operation ) );
2568 return( status );
2569}
2570
Gilles Peskine01126fa2018-07-12 17:04:55 +02002571#if defined(MBEDTLS_MD_C)
2572static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2573{
Gilles Peskine3f108122018-12-07 18:14:53 +01002574 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002575 return( psa_hash_abort( &hmac->hash_ctx ) );
2576}
2577#endif /* MBEDTLS_MD_C */
2578
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2580{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002581 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002583 /* The object has (apparently) been initialized but it is not
2584 * in use. It's ok to call abort on such an object, and there's
2585 * nothing to do. */
2586 return( PSA_SUCCESS );
2587 }
2588 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002590 if( operation->alg == PSA_ALG_CMAC )
2591 {
2592 mbedtls_cipher_free( &operation->ctx.cmac );
2593 }
2594 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002597 if( PSA_ALG_IS_HMAC( operation->alg ) )
2598 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002599 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002600 }
2601 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002603 {
2604 /* Sanity check (shouldn't happen: operation->alg should
2605 * always have been initialized to a valid value). */
2606 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 }
Moran Peker41deec42018-04-04 15:43:05 +03002608
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 operation->alg = 0;
2610 operation->key_set = 0;
2611 operation->iv_set = 0;
2612 operation->iv_required = 0;
2613 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002614 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002615
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002617
2618bad_state:
2619 /* If abort is called on an uninitialized object, we can't trust
2620 * anything. Wipe the object in case it contains confidential data.
2621 * This may result in a memory leak if a pointer gets overwritten,
2622 * but it's too late to do anything about this. */
2623 memset( operation, 0, sizeof( *operation ) );
2624 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625}
2626
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002627#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002628static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002629 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002630 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002631 const mbedtls_cipher_info_t *cipher_info )
2632{
2633 int ret;
2634
2635 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002636
2637 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2638 if( ret != 0 )
2639 return( ret );
2640
2641 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2642 slot->data.raw.data,
2643 key_bits );
2644 return( ret );
2645}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002646#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002647
Gilles Peskine248051a2018-06-20 16:09:38 +02002648#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002649static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2650 const uint8_t *key,
2651 size_t key_length,
2652 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002653{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002654 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002655 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002656 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002657 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002658 psa_status_t status;
2659
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002660 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2661 * overflow below. This should never trigger if the hash algorithm
2662 * is implemented correctly. */
2663 /* The size checks against the ipad and opad buffers cannot be written
2664 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2665 * because that triggers -Wlogical-op on GCC 7.3. */
2666 if( block_size > sizeof( ipad ) )
2667 return( PSA_ERROR_NOT_SUPPORTED );
2668 if( block_size > sizeof( hmac->opad ) )
2669 return( PSA_ERROR_NOT_SUPPORTED );
2670 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002671 return( PSA_ERROR_NOT_SUPPORTED );
2672
Gilles Peskined223b522018-06-11 18:12:58 +02002673 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002674 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002675 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002677 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002678 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002679 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002680 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002681 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002682 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002683 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002684 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002685 }
Gilles Peskine96889972018-07-12 17:07:03 +02002686 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2687 * but it is permitted. It is common when HMAC is used in HKDF, for
2688 * example. Don't call `memcpy` in the 0-length because `key` could be
2689 * an invalid pointer which would make the behavior undefined. */
2690 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002691 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002692
Gilles Peskined223b522018-06-11 18:12:58 +02002693 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2694 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002695 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002696 ipad[i] ^= 0x36;
2697 memset( ipad + key_length, 0x36, block_size - key_length );
2698
2699 /* Copy the key material from ipad to opad, flipping the requisite bits,
2700 * and filling the rest of opad with the requisite constant. */
2701 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002702 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2703 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002704
Gilles Peskine01126fa2018-07-12 17:04:55 +02002705 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002707 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002710
2711cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002712 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002713
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002714 return( status );
2715}
Gilles Peskine248051a2018-06-20 16:09:38 +02002716#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717
Gilles Peskine89167cb2018-07-08 20:12:23 +02002718static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002719 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002720 psa_algorithm_t alg,
2721 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002724 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002726 psa_key_usage_t usage =
2727 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002728 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002729 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002731 /* A context must be freshly initialized before it can be set up. */
2732 if( operation->alg != 0 )
2733 {
2734 return( PSA_ERROR_BAD_STATE );
2735 }
2736
Gilles Peskined911eb72018-08-14 15:18:45 +02002737 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002738 if( status != PSA_SUCCESS )
2739 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002740 if( is_sign )
2741 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742
Gilles Peskine28f8f302019-07-24 13:30:31 +02002743 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002744 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002745 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002746 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002747
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002749 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002750 {
2751 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002752 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002753 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002754 int ret;
2755 if( cipher_info == NULL )
2756 {
2757 status = PSA_ERROR_NOT_SUPPORTED;
2758 goto exit;
2759 }
2760 operation->mac_size = cipher_info->block_size;
2761 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2762 status = mbedtls_to_psa_error( ret );
2763 }
2764 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002767 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002768 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002769 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 if( hash_alg == 0 )
2771 {
2772 status = PSA_ERROR_NOT_SUPPORTED;
2773 goto exit;
2774 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002775
2776 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2777 /* Sanity check. This shouldn't fail on a valid configuration. */
2778 if( operation->mac_size == 0 ||
2779 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2780 {
2781 status = PSA_ERROR_NOT_SUPPORTED;
2782 goto exit;
2783 }
2784
Gilles Peskine8e338702019-07-30 20:06:31 +02002785 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002786 {
2787 status = PSA_ERROR_INVALID_ARGUMENT;
2788 goto exit;
2789 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002790
Gilles Peskine01126fa2018-07-12 17:04:55 +02002791 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2792 slot->data.raw.data,
2793 slot->data.raw.bytes,
2794 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002795 }
2796 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002798 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002799 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002800 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002802
Gilles Peskined911eb72018-08-14 15:18:45 +02002803 if( truncated == 0 )
2804 {
2805 /* The "normal" case: untruncated algorithm. Nothing to do. */
2806 }
2807 else if( truncated < 4 )
2808 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002809 /* A very short MAC is too short for security since it can be
2810 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2811 * so we make this our minimum, even though 32 bits is still
2812 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002813 status = PSA_ERROR_NOT_SUPPORTED;
2814 }
2815 else if( truncated > operation->mac_size )
2816 {
2817 /* It's impossible to "truncate" to a larger length. */
2818 status = PSA_ERROR_INVALID_ARGUMENT;
2819 }
2820 else
2821 operation->mac_size = truncated;
2822
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002824 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002826 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002828 else
2829 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002830 operation->key_set = 1;
2831 }
2832 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833}
2834
Gilles Peskine89167cb2018-07-08 20:12:23 +02002835psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002836 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002837 psa_algorithm_t alg )
2838{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002839 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002840}
2841
2842psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002843 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002844 psa_algorithm_t alg )
2845{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002846 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002847}
2848
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2850 const uint8_t *input,
2851 size_t input_length )
2852{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002853 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002855 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002857 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858 operation->has_input = 1;
2859
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002861 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002862 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2864 input, input_length );
2865 status = mbedtls_to_psa_error( ret );
2866 }
2867 else
2868#endif /* MBEDTLS_CMAC_C */
2869#if defined(MBEDTLS_MD_C)
2870 if( PSA_ALG_IS_HMAC( operation->alg ) )
2871 {
2872 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2873 input_length );
2874 }
2875 else
2876#endif /* MBEDTLS_MD_C */
2877 {
2878 /* This shouldn't happen if `operation` was initialized by
2879 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002880 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002881 }
2882
Gilles Peskinefbfac682018-07-08 20:51:54 +02002883 if( status != PSA_SUCCESS )
2884 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002885 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886}
2887
Gilles Peskine01126fa2018-07-12 17:04:55 +02002888#if defined(MBEDTLS_MD_C)
2889static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2890 uint8_t *mac,
2891 size_t mac_size )
2892{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002893 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002894 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2895 size_t hash_size = 0;
2896 size_t block_size = psa_get_hash_block_size( hash_alg );
2897 psa_status_t status;
2898
Gilles Peskine01126fa2018-07-12 17:04:55 +02002899 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2900 if( status != PSA_SUCCESS )
2901 return( status );
2902 /* From here on, tmp needs to be wiped. */
2903
2904 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2905 if( status != PSA_SUCCESS )
2906 goto exit;
2907
2908 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2909 if( status != PSA_SUCCESS )
2910 goto exit;
2911
2912 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2913 if( status != PSA_SUCCESS )
2914 goto exit;
2915
Gilles Peskined911eb72018-08-14 15:18:45 +02002916 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2917 if( status != PSA_SUCCESS )
2918 goto exit;
2919
2920 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921
2922exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002923 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002924 return( status );
2925}
2926#endif /* MBEDTLS_MD_C */
2927
mohammad16036df908f2018-04-02 08:34:15 -07002928static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002929 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002930 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002932 if( ! operation->key_set )
2933 return( PSA_ERROR_BAD_STATE );
2934 if( operation->iv_required && ! operation->iv_set )
2935 return( PSA_ERROR_BAD_STATE );
2936
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937 if( mac_size < operation->mac_size )
2938 return( PSA_ERROR_BUFFER_TOO_SMALL );
2939
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002941 if( operation->alg == PSA_ALG_CMAC )
2942 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002943 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2944 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2945 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002946 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002947 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002948 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002950 else
2951#endif /* MBEDTLS_CMAC_C */
2952#if defined(MBEDTLS_MD_C)
2953 if( PSA_ALG_IS_HMAC( operation->alg ) )
2954 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002955 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002956 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002957 }
2958 else
2959#endif /* MBEDTLS_MD_C */
2960 {
2961 /* This shouldn't happen if `operation` was initialized by
2962 * a setup function. */
2963 return( PSA_ERROR_BAD_STATE );
2964 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965}
2966
Gilles Peskineacd4be32018-07-08 19:56:25 +02002967psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2968 uint8_t *mac,
2969 size_t mac_size,
2970 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002971{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002972 psa_status_t status;
2973
Jaeden Amero252ef282019-02-15 14:05:35 +00002974 if( operation->alg == 0 )
2975 {
2976 return( PSA_ERROR_BAD_STATE );
2977 }
2978
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002979 /* Fill the output buffer with something that isn't a valid mac
2980 * (barring an attack on the mac and deliberately-crafted input),
2981 * in case the caller doesn't check the return status properly. */
2982 *mac_length = mac_size;
2983 /* If mac_size is 0 then mac may be NULL and then the
2984 * call to memset would have undefined behavior. */
2985 if( mac_size != 0 )
2986 memset( mac, '!', mac_size );
2987
Gilles Peskine89167cb2018-07-08 20:12:23 +02002988 if( ! operation->is_sign )
2989 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002990 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002991 }
mohammad16036df908f2018-04-02 08:34:15 -07002992
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002993 status = psa_mac_finish_internal( operation, mac, mac_size );
2994
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002995 if( status == PSA_SUCCESS )
2996 {
2997 status = psa_mac_abort( operation );
2998 if( status == PSA_SUCCESS )
2999 *mac_length = operation->mac_size;
3000 else
3001 memset( mac, '!', mac_size );
3002 }
3003 else
3004 psa_mac_abort( operation );
3005 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003006}
3007
Gilles Peskineacd4be32018-07-08 19:56:25 +02003008psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3009 const uint8_t *mac,
3010 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011{
Gilles Peskine828ed142018-06-18 23:25:51 +02003012 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003013 psa_status_t status;
3014
Jaeden Amero252ef282019-02-15 14:05:35 +00003015 if( operation->alg == 0 )
3016 {
3017 return( PSA_ERROR_BAD_STATE );
3018 }
3019
Gilles Peskine89167cb2018-07-08 20:12:23 +02003020 if( operation->is_sign )
3021 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003022 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003023 }
3024 if( operation->mac_size != mac_length )
3025 {
3026 status = PSA_ERROR_INVALID_SIGNATURE;
3027 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003028 }
mohammad16036df908f2018-04-02 08:34:15 -07003029
3030 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003031 actual_mac, sizeof( actual_mac ) );
3032
3033 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3034 status = PSA_ERROR_INVALID_SIGNATURE;
3035
3036cleanup:
3037 if( status == PSA_SUCCESS )
3038 status = psa_mac_abort( operation );
3039 else
3040 psa_mac_abort( operation );
3041
Gilles Peskine3f108122018-12-07 18:14:53 +01003042 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003043
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003044 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045}
3046
3047
Gilles Peskine20035e32018-02-03 22:44:14 +01003048
Gilles Peskine20035e32018-02-03 22:44:14 +01003049/****************************************************************/
3050/* Asymmetric cryptography */
3051/****************************************************************/
3052
Gilles Peskine2b450e32018-06-27 15:42:46 +02003053#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003054/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003055 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003056static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3057 size_t hash_length,
3058 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003059{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003060 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003061 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003062 *md_alg = mbedtls_md_get_type( md_info );
3063
3064 /* The Mbed TLS RSA module uses an unsigned int for hash length
3065 * parameters. Validate that it fits so that we don't risk an
3066 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003067#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003068 if( hash_length > UINT_MAX )
3069 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003070#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003071
3072#if defined(MBEDTLS_PKCS1_V15)
3073 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3074 * must be correct. */
3075 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3076 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003077 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003078 if( md_info == NULL )
3079 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003080 if( mbedtls_md_get_size( md_info ) != hash_length )
3081 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003082 }
3083#endif /* MBEDTLS_PKCS1_V15 */
3084
3085#if defined(MBEDTLS_PKCS1_V21)
3086 /* PSS requires a hash internally. */
3087 if( PSA_ALG_IS_RSA_PSS( alg ) )
3088 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003089 if( md_info == NULL )
3090 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003091 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003092#endif /* MBEDTLS_PKCS1_V21 */
3093
Gilles Peskine61b91d42018-06-08 16:09:36 +02003094 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003095}
3096
Gilles Peskine2b450e32018-06-27 15:42:46 +02003097static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3098 psa_algorithm_t alg,
3099 const uint8_t *hash,
3100 size_t hash_length,
3101 uint8_t *signature,
3102 size_t signature_size,
3103 size_t *signature_length )
3104{
3105 psa_status_t status;
3106 int ret;
3107 mbedtls_md_type_t md_alg;
3108
3109 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3110 if( status != PSA_SUCCESS )
3111 return( status );
3112
Gilles Peskine630a18a2018-06-29 17:49:35 +02003113 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003114 return( PSA_ERROR_BUFFER_TOO_SMALL );
3115
3116#if defined(MBEDTLS_PKCS1_V15)
3117 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3118 {
3119 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3120 MBEDTLS_MD_NONE );
3121 ret = mbedtls_rsa_pkcs1_sign( rsa,
3122 mbedtls_ctr_drbg_random,
3123 &global_data.ctr_drbg,
3124 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003125 md_alg,
3126 (unsigned int) hash_length,
3127 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003128 signature );
3129 }
3130 else
3131#endif /* MBEDTLS_PKCS1_V15 */
3132#if defined(MBEDTLS_PKCS1_V21)
3133 if( PSA_ALG_IS_RSA_PSS( alg ) )
3134 {
3135 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3136 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3137 mbedtls_ctr_drbg_random,
3138 &global_data.ctr_drbg,
3139 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003140 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003141 (unsigned int) hash_length,
3142 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003143 signature );
3144 }
3145 else
3146#endif /* MBEDTLS_PKCS1_V21 */
3147 {
3148 return( PSA_ERROR_INVALID_ARGUMENT );
3149 }
3150
3151 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003152 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003153 return( mbedtls_to_psa_error( ret ) );
3154}
3155
3156static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3157 psa_algorithm_t alg,
3158 const uint8_t *hash,
3159 size_t hash_length,
3160 const uint8_t *signature,
3161 size_t signature_length )
3162{
3163 psa_status_t status;
3164 int ret;
3165 mbedtls_md_type_t md_alg;
3166
3167 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3168 if( status != PSA_SUCCESS )
3169 return( status );
3170
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003171 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3172 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003173
3174#if defined(MBEDTLS_PKCS1_V15)
3175 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3176 {
3177 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3178 MBEDTLS_MD_NONE );
3179 ret = mbedtls_rsa_pkcs1_verify( rsa,
3180 mbedtls_ctr_drbg_random,
3181 &global_data.ctr_drbg,
3182 MBEDTLS_RSA_PUBLIC,
3183 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003184 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003185 hash,
3186 signature );
3187 }
3188 else
3189#endif /* MBEDTLS_PKCS1_V15 */
3190#if defined(MBEDTLS_PKCS1_V21)
3191 if( PSA_ALG_IS_RSA_PSS( alg ) )
3192 {
3193 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3194 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3195 mbedtls_ctr_drbg_random,
3196 &global_data.ctr_drbg,
3197 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003198 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003199 (unsigned int) hash_length,
3200 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003201 signature );
3202 }
3203 else
3204#endif /* MBEDTLS_PKCS1_V21 */
3205 {
3206 return( PSA_ERROR_INVALID_ARGUMENT );
3207 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003208
3209 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3210 * the rest of the signature is invalid". This has little use in
3211 * practice and PSA doesn't report this distinction. */
3212 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3213 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003214 return( mbedtls_to_psa_error( ret ) );
3215}
3216#endif /* MBEDTLS_RSA_C */
3217
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003218#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003219/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3220 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3221 * (even though these functions don't modify it). */
3222static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3223 psa_algorithm_t alg,
3224 const uint8_t *hash,
3225 size_t hash_length,
3226 uint8_t *signature,
3227 size_t signature_size,
3228 size_t *signature_length )
3229{
3230 int ret;
3231 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003232 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003233 mbedtls_mpi_init( &r );
3234 mbedtls_mpi_init( &s );
3235
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003236 if( signature_size < 2 * curve_bytes )
3237 {
3238 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3239 goto cleanup;
3240 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003241
Gilles Peskinea05219c2018-11-16 16:02:56 +01003242#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003243 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3244 {
3245 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3246 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3247 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003248 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3249 &ecp->d, hash,
3250 hash_length, md_alg,
3251 mbedtls_ctr_drbg_random,
3252 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003253 }
3254 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003255#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003256 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003257 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003258 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3259 hash, hash_length,
3260 mbedtls_ctr_drbg_random,
3261 &global_data.ctr_drbg ) );
3262 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003263
3264 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3265 signature,
3266 curve_bytes ) );
3267 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3268 signature + curve_bytes,
3269 curve_bytes ) );
3270
3271cleanup:
3272 mbedtls_mpi_free( &r );
3273 mbedtls_mpi_free( &s );
3274 if( ret == 0 )
3275 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003276 return( mbedtls_to_psa_error( ret ) );
3277}
3278
3279static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3280 const uint8_t *hash,
3281 size_t hash_length,
3282 const uint8_t *signature,
3283 size_t signature_length )
3284{
3285 int ret;
3286 mbedtls_mpi r, s;
3287 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3288 mbedtls_mpi_init( &r );
3289 mbedtls_mpi_init( &s );
3290
3291 if( signature_length != 2 * curve_bytes )
3292 return( PSA_ERROR_INVALID_SIGNATURE );
3293
3294 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3295 signature,
3296 curve_bytes ) );
3297 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3298 signature + curve_bytes,
3299 curve_bytes ) );
3300
3301 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3302 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003303
3304cleanup:
3305 mbedtls_mpi_free( &r );
3306 mbedtls_mpi_free( &s );
3307 return( mbedtls_to_psa_error( ret ) );
3308}
3309#endif /* MBEDTLS_ECDSA_C */
3310
Gilles Peskinec5487a82018-12-03 18:08:14 +01003311psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003312 psa_algorithm_t alg,
3313 const uint8_t *hash,
3314 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003315 uint8_t *signature,
3316 size_t signature_size,
3317 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003318{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003319 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003320 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003321#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3322 const psa_drv_se_t *drv;
3323 psa_drv_se_context_t *drv_context;
3324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003325
3326 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003327 /* Immediately reject a zero-length signature buffer. This guarantees
3328 * that signature must be a valid pointer. (On the other hand, the hash
3329 * buffer can in principle be empty since it doesn't actually have
3330 * to be a hash.) */
3331 if( signature_size == 0 )
3332 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003333
Gilles Peskineedc64242019-08-07 21:05:07 +02003334 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003335 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003336 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003337 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003338 {
3339 status = PSA_ERROR_INVALID_ARGUMENT;
3340 goto exit;
3341 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003342
Gilles Peskineedc64242019-08-07 21:05:07 +02003343#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3344 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3345 {
3346 if( drv->asymmetric == NULL ||
3347 drv->asymmetric->p_sign == NULL )
3348 {
3349 status = PSA_ERROR_NOT_SUPPORTED;
3350 goto exit;
3351 }
3352 status = drv->asymmetric->p_sign( drv_context,
3353 slot->data.se.slot_number,
3354 alg,
3355 hash, hash_length,
3356 signature, signature_size,
3357 signature_length );
3358 }
3359 else
3360#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003361#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003362 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003363 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003364 status = psa_rsa_sign( slot->data.rsa,
3365 alg,
3366 hash, hash_length,
3367 signature, signature_size,
3368 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003369 }
3370 else
3371#endif /* defined(MBEDTLS_RSA_C) */
3372#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003373 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003374 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003375#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003376 if(
3377#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3378 PSA_ALG_IS_ECDSA( alg )
3379#else
3380 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3381#endif
3382 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003383 status = psa_ecdsa_sign( slot->data.ecp,
3384 alg,
3385 hash, hash_length,
3386 signature, signature_size,
3387 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003388 else
3389#endif /* defined(MBEDTLS_ECDSA_C) */
3390 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003391 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003392 }
itayzafrir5c753392018-05-08 11:18:38 +03003393 }
3394 else
3395#endif /* defined(MBEDTLS_ECP_C) */
3396 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003397 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003398 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003399
3400exit:
3401 /* Fill the unused part of the output buffer (the whole buffer on error,
3402 * the trailing part on success) with something that isn't a valid mac
3403 * (barring an attack on the mac and deliberately-crafted input),
3404 * in case the caller doesn't check the return status properly. */
3405 if( status == PSA_SUCCESS )
3406 memset( signature + *signature_length, '!',
3407 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003408 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003409 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003410 /* If signature_size is 0 then we have nothing to do. We must not call
3411 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003412 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003413}
3414
Gilles Peskinec5487a82018-12-03 18:08:14 +01003415psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003416 psa_algorithm_t alg,
3417 const uint8_t *hash,
3418 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003419 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003420 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003421{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003422 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003423 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003424#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3425 const psa_drv_se_t *drv;
3426 psa_drv_se_context_t *drv_context;
3427#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003428
Gilles Peskineedc64242019-08-07 21:05:07 +02003429 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003430 if( status != PSA_SUCCESS )
3431 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003432
Gilles Peskineedc64242019-08-07 21:05:07 +02003433#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3434 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3435 {
3436 if( drv->asymmetric == NULL ||
3437 drv->asymmetric->p_verify == NULL )
3438 return( PSA_ERROR_NOT_SUPPORTED );
3439 return( drv->asymmetric->p_verify( drv_context,
3440 slot->data.se.slot_number,
3441 alg,
3442 hash, hash_length,
3443 signature, signature_length ) );
3444 }
3445 else
3446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003447#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003448 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003449 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003450 return( psa_rsa_verify( slot->data.rsa,
3451 alg,
3452 hash, hash_length,
3453 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003454 }
3455 else
3456#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003457#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003458 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003459 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003460#if defined(MBEDTLS_ECDSA_C)
3461 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003462 return( psa_ecdsa_verify( slot->data.ecp,
3463 hash, hash_length,
3464 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003465 else
3466#endif /* defined(MBEDTLS_ECDSA_C) */
3467 {
3468 return( PSA_ERROR_INVALID_ARGUMENT );
3469 }
itayzafrir5c753392018-05-08 11:18:38 +03003470 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003471 else
3472#endif /* defined(MBEDTLS_ECP_C) */
3473 {
3474 return( PSA_ERROR_NOT_SUPPORTED );
3475 }
3476}
3477
Gilles Peskine072ac562018-06-30 00:21:29 +02003478#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3479static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3480 mbedtls_rsa_context *rsa )
3481{
3482 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3483 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3484 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3485 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3486}
3487#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3488
Gilles Peskinec5487a82018-12-03 18:08:14 +01003489psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003490 psa_algorithm_t alg,
3491 const uint8_t *input,
3492 size_t input_length,
3493 const uint8_t *salt,
3494 size_t salt_length,
3495 uint8_t *output,
3496 size_t output_size,
3497 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003498{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003499 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003500 psa_status_t status;
3501
Darryl Green5cc689a2018-07-24 15:34:10 +01003502 (void) input;
3503 (void) input_length;
3504 (void) salt;
3505 (void) output;
3506 (void) output_size;
3507
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003508 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003509
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003510 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3511 return( PSA_ERROR_INVALID_ARGUMENT );
3512
Gilles Peskine28f8f302019-07-24 13:30:31 +02003513 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003514 if( status != PSA_SUCCESS )
3515 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003516 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3517 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003518 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519
3520#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003521 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522 {
3523 mbedtls_rsa_context *rsa = slot->data.rsa;
3524 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003525 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003526 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003528 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003530 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3531 mbedtls_ctr_drbg_random,
3532 &global_data.ctr_drbg,
3533 MBEDTLS_RSA_PUBLIC,
3534 input_length,
3535 input,
3536 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003537 }
3538 else
3539#endif /* MBEDTLS_PKCS1_V15 */
3540#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003541 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003543 psa_rsa_oaep_set_padding_mode( alg, rsa );
3544 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3545 mbedtls_ctr_drbg_random,
3546 &global_data.ctr_drbg,
3547 MBEDTLS_RSA_PUBLIC,
3548 salt, salt_length,
3549 input_length,
3550 input,
3551 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003552 }
3553 else
3554#endif /* MBEDTLS_PKCS1_V21 */
3555 {
3556 return( PSA_ERROR_INVALID_ARGUMENT );
3557 }
3558 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003559 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003560 return( mbedtls_to_psa_error( ret ) );
3561 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003563#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564 {
3565 return( PSA_ERROR_NOT_SUPPORTED );
3566 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003567}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568
Gilles Peskinec5487a82018-12-03 18:08:14 +01003569psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003570 psa_algorithm_t alg,
3571 const uint8_t *input,
3572 size_t input_length,
3573 const uint8_t *salt,
3574 size_t salt_length,
3575 uint8_t *output,
3576 size_t output_size,
3577 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003578{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003579 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003580 psa_status_t status;
3581
Darryl Green5cc689a2018-07-24 15:34:10 +01003582 (void) input;
3583 (void) input_length;
3584 (void) salt;
3585 (void) output;
3586 (void) output_size;
3587
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003588 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003590 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3591 return( PSA_ERROR_INVALID_ARGUMENT );
3592
Gilles Peskine28f8f302019-07-24 13:30:31 +02003593 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003594 if( status != PSA_SUCCESS )
3595 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003596 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597 return( PSA_ERROR_INVALID_ARGUMENT );
3598
3599#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003600 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601 {
3602 mbedtls_rsa_context *rsa = slot->data.rsa;
3603 int ret;
3604
Gilles Peskine630a18a2018-06-29 17:49:35 +02003605 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606 return( PSA_ERROR_INVALID_ARGUMENT );
3607
3608#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003609 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003611 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3612 mbedtls_ctr_drbg_random,
3613 &global_data.ctr_drbg,
3614 MBEDTLS_RSA_PRIVATE,
3615 output_length,
3616 input,
3617 output,
3618 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003619 }
3620 else
3621#endif /* MBEDTLS_PKCS1_V15 */
3622#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003623 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003625 psa_rsa_oaep_set_padding_mode( alg, rsa );
3626 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3627 mbedtls_ctr_drbg_random,
3628 &global_data.ctr_drbg,
3629 MBEDTLS_RSA_PRIVATE,
3630 salt, salt_length,
3631 output_length,
3632 input,
3633 output,
3634 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 }
3636 else
3637#endif /* MBEDTLS_PKCS1_V21 */
3638 {
3639 return( PSA_ERROR_INVALID_ARGUMENT );
3640 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003641
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 return( mbedtls_to_psa_error( ret ) );
3643 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003645#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646 {
3647 return( PSA_ERROR_NOT_SUPPORTED );
3648 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003649}
Gilles Peskine20035e32018-02-03 22:44:14 +01003650
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003651
3652
mohammad1603503973b2018-03-12 15:59:30 +02003653/****************************************************************/
3654/* Symmetric cryptography */
3655/****************************************************************/
3656
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003657/* Initialize the cipher operation structure. Once this function has been
3658 * called, psa_cipher_abort can run and will do the right thing. */
3659static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3660 psa_algorithm_t alg )
3661{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003662 if( ! PSA_ALG_IS_CIPHER( alg ) )
3663 {
3664 memset( operation, 0, sizeof( *operation ) );
3665 return( PSA_ERROR_INVALID_ARGUMENT );
3666 }
3667
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003668 operation->alg = alg;
3669 operation->key_set = 0;
3670 operation->iv_set = 0;
3671 operation->iv_required = 1;
3672 operation->iv_size = 0;
3673 operation->block_size = 0;
3674 mbedtls_cipher_init( &operation->ctx.cipher );
3675 return( PSA_SUCCESS );
3676}
3677
Gilles Peskinee553c652018-06-04 16:22:46 +02003678static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003679 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003680 psa_algorithm_t alg,
3681 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003682{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003683 int ret = 0;
3684 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003685 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003686 size_t key_bits;
3687 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003688 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3689 PSA_KEY_USAGE_ENCRYPT :
3690 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003691
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003692 /* A context must be freshly initialized before it can be set up. */
3693 if( operation->alg != 0 )
3694 {
3695 return( PSA_ERROR_BAD_STATE );
3696 }
3697
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003698 status = psa_cipher_init( operation, alg );
3699 if( status != PSA_SUCCESS )
3700 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003701
Gilles Peskine28f8f302019-07-24 13:30:31 +02003702 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003703 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003704 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003705 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003706
Gilles Peskine8e338702019-07-30 20:06:31 +02003707 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003708 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003709 {
3710 status = PSA_ERROR_NOT_SUPPORTED;
3711 goto exit;
3712 }
mohammad1603503973b2018-03-12 15:59:30 +02003713
mohammad1603503973b2018-03-12 15:59:30 +02003714 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003715 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003716 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003717
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003718#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003719 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003720 {
3721 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003722 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003723 memcpy( keys, slot->data.raw.data, 16 );
3724 memcpy( keys + 16, slot->data.raw.data, 8 );
3725 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3726 keys,
3727 192, cipher_operation );
3728 }
3729 else
3730#endif
3731 {
3732 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3733 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003734 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003735 }
Moran Peker41deec42018-04-04 15:43:05 +03003736 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003737 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003738
mohammad16038481e742018-03-18 13:57:31 +02003739#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003740 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003741 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003742 case PSA_ALG_CBC_NO_PADDING:
3743 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3744 MBEDTLS_PADDING_NONE );
3745 break;
3746 case PSA_ALG_CBC_PKCS7:
3747 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3748 MBEDTLS_PADDING_PKCS7 );
3749 break;
3750 default:
3751 /* The algorithm doesn't involve padding. */
3752 ret = 0;
3753 break;
3754 }
3755 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003756 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003757#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3758
mohammad1603503973b2018-03-12 15:59:30 +02003759 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003760 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003761 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003762 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003763 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003764 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003765 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003766#if defined(MBEDTLS_CHACHA20_C)
3767 else
3768 if( alg == PSA_ALG_CHACHA20 )
3769 operation->iv_size = 12;
3770#endif
mohammad1603503973b2018-03-12 15:59:30 +02003771
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003772exit:
3773 if( status == 0 )
3774 status = mbedtls_to_psa_error( ret );
3775 if( status != 0 )
3776 psa_cipher_abort( operation );
3777 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003778}
3779
Gilles Peskinefe119512018-07-08 21:39:34 +02003780psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003781 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003782 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003783{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003784 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003785}
3786
Gilles Peskinefe119512018-07-08 21:39:34 +02003787psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003788 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003789 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003790{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003791 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003792}
3793
Gilles Peskinefe119512018-07-08 21:39:34 +02003794psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003795 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003796 size_t iv_size,
3797 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003798{
itayzafrir534bd7c2018-08-02 13:56:32 +03003799 psa_status_t status;
3800 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003801 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003802 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003803 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003804 }
Moran Peker41deec42018-04-04 15:43:05 +03003805 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003806 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003807 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003808 goto exit;
3809 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003810 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3811 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003812 if( ret != 0 )
3813 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003814 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003815 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003816 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003817
mohammad16038481e742018-03-18 13:57:31 +02003818 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003820
Moran Peker395db872018-05-31 14:07:14 +03003821exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003822 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003823 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003824 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003825}
3826
Gilles Peskinefe119512018-07-08 21:39:34 +02003827psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003828 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003829 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003830{
itayzafrir534bd7c2018-08-02 13:56:32 +03003831 psa_status_t status;
3832 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003833 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003834 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003835 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003836 }
Moran Pekera28258c2018-05-29 16:25:04 +03003837 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003838 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 status = PSA_ERROR_INVALID_ARGUMENT;
3840 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003841 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003842 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3843 status = mbedtls_to_psa_error( ret );
3844exit:
3845 if( status == PSA_SUCCESS )
3846 operation->iv_set = 1;
3847 else
mohammad1603503973b2018-03-12 15:59:30 +02003848 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003849 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003850}
3851
Gilles Peskinee553c652018-06-04 16:22:46 +02003852psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3853 const uint8_t *input,
3854 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003855 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003856 size_t output_size,
3857 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003858{
itayzafrir534bd7c2018-08-02 13:56:32 +03003859 psa_status_t status;
3860 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003861 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003862
3863 if( operation->alg == 0 )
3864 {
3865 return( PSA_ERROR_BAD_STATE );
3866 }
3867
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003868 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003869 {
3870 /* Take the unprocessed partial block left over from previous
3871 * update calls, if any, plus the input to this call. Remove
3872 * the last partial block, if any. You get the data that will be
3873 * output in this call. */
3874 expected_output_size =
3875 ( operation->ctx.cipher.unprocessed_len + input_length )
3876 / operation->block_size * operation->block_size;
3877 }
3878 else
3879 {
3880 expected_output_size = input_length;
3881 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003882
Gilles Peskine89d789c2018-06-04 17:17:16 +02003883 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003884 {
3885 status = PSA_ERROR_BUFFER_TOO_SMALL;
3886 goto exit;
3887 }
mohammad160382759612018-03-12 18:16:40 +02003888
mohammad1603503973b2018-03-12 15:59:30 +02003889 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003890 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003891 status = mbedtls_to_psa_error( ret );
3892exit:
3893 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003894 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003896}
3897
Gilles Peskinee553c652018-06-04 16:22:46 +02003898psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3899 uint8_t *output,
3900 size_t output_size,
3901 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003902{
David Saadab4ecc272019-02-14 13:48:10 +02003903 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003904 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003905 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003906
mohammad1603503973b2018-03-12 15:59:30 +02003907 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003908 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003909 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003910 }
3911 if( operation->iv_required && ! operation->iv_set )
3912 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003913 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003914 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003915
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003916 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003917 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3918 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003919 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003920 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003921 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003922 }
3923
Janos Follath315b51c2018-07-09 16:04:51 +01003924 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3925 temp_output_buffer,
3926 output_length );
3927 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003928 {
Janos Follath315b51c2018-07-09 16:04:51 +01003929 status = mbedtls_to_psa_error( cipher_ret );
3930 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003931 }
Janos Follath315b51c2018-07-09 16:04:51 +01003932
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003933 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003934 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003935 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003936 memcpy( output, temp_output_buffer, *output_length );
3937 else
3938 {
Janos Follath315b51c2018-07-09 16:04:51 +01003939 status = PSA_ERROR_BUFFER_TOO_SMALL;
3940 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003941 }
mohammad1603503973b2018-03-12 15:59:30 +02003942
Gilles Peskine3f108122018-12-07 18:14:53 +01003943 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003944 status = psa_cipher_abort( operation );
3945
3946 return( status );
3947
3948error:
3949
3950 *output_length = 0;
3951
Gilles Peskine3f108122018-12-07 18:14:53 +01003952 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003953 (void) psa_cipher_abort( operation );
3954
3955 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003956}
3957
Gilles Peskinee553c652018-06-04 16:22:46 +02003958psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3959{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003960 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003961 {
3962 /* The object has (apparently) been initialized but it is not
3963 * in use. It's ok to call abort on such an object, and there's
3964 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003965 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003966 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003967
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003968 /* Sanity check (shouldn't happen: operation->alg should
3969 * always have been initialized to a valid value). */
3970 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3971 return( PSA_ERROR_BAD_STATE );
3972
mohammad1603503973b2018-03-12 15:59:30 +02003973 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003974
Moran Peker41deec42018-04-04 15:43:05 +03003975 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003976 operation->key_set = 0;
3977 operation->iv_set = 0;
3978 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003979 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003980 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003981
Moran Peker395db872018-05-31 14:07:14 +03003982 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003983}
3984
Gilles Peskinea0655c32018-04-30 17:06:50 +02003985
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003986
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003987
mohammad16035955c982018-04-26 00:53:03 +03003988/****************************************************************/
3989/* AEAD */
3990/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003991
Gilles Peskineedf9a652018-08-17 18:11:56 +02003992typedef struct
3993{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003994 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003995 const mbedtls_cipher_info_t *cipher_info;
3996 union
3997 {
3998#if defined(MBEDTLS_CCM_C)
3999 mbedtls_ccm_context ccm;
4000#endif /* MBEDTLS_CCM_C */
4001#if defined(MBEDTLS_GCM_C)
4002 mbedtls_gcm_context gcm;
4003#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004004#if defined(MBEDTLS_CHACHAPOLY_C)
4005 mbedtls_chachapoly_context chachapoly;
4006#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004007 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004008 psa_algorithm_t core_alg;
4009 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004010 uint8_t tag_length;
4011} aead_operation_t;
4012
Gilles Peskine30a9e412019-01-14 18:36:12 +01004013static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004014{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004015 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004016 {
4017#if defined(MBEDTLS_CCM_C)
4018 case PSA_ALG_CCM:
4019 mbedtls_ccm_free( &operation->ctx.ccm );
4020 break;
4021#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004022#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004023 case PSA_ALG_GCM:
4024 mbedtls_gcm_free( &operation->ctx.gcm );
4025 break;
4026#endif /* MBEDTLS_GCM_C */
4027 }
4028}
4029
4030static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004031 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004032 psa_key_usage_t usage,
4033 psa_algorithm_t alg )
4034{
4035 psa_status_t status;
4036 size_t key_bits;
4037 mbedtls_cipher_id_t cipher_id;
4038
Gilles Peskine28f8f302019-07-24 13:30:31 +02004039 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004040 if( status != PSA_SUCCESS )
4041 return( status );
4042
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004043 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004044
4045 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004046 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004047 &cipher_id );
4048 if( operation->cipher_info == NULL )
4049 return( PSA_ERROR_NOT_SUPPORTED );
4050
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004051 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052 {
4053#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004054 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4055 operation->core_alg = PSA_ALG_CCM;
4056 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004057 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4058 * The call to mbedtls_ccm_encrypt_and_tag or
4059 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004060 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004061 return( PSA_ERROR_INVALID_ARGUMENT );
4062 mbedtls_ccm_init( &operation->ctx.ccm );
4063 status = mbedtls_to_psa_error(
4064 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4065 operation->slot->data.raw.data,
4066 (unsigned int) key_bits ) );
4067 if( status != 0 )
4068 goto cleanup;
4069 break;
4070#endif /* MBEDTLS_CCM_C */
4071
4072#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004073 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4074 operation->core_alg = PSA_ALG_GCM;
4075 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004076 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4077 * The call to mbedtls_gcm_crypt_and_tag or
4078 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004079 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004080 return( PSA_ERROR_INVALID_ARGUMENT );
4081 mbedtls_gcm_init( &operation->ctx.gcm );
4082 status = mbedtls_to_psa_error(
4083 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4084 operation->slot->data.raw.data,
4085 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004086 if( status != 0 )
4087 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 break;
4089#endif /* MBEDTLS_GCM_C */
4090
Gilles Peskine26869f22019-05-06 15:25:00 +02004091#if defined(MBEDTLS_CHACHAPOLY_C)
4092 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4093 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4094 operation->full_tag_length = 16;
4095 /* We only support the default tag length. */
4096 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4097 return( PSA_ERROR_NOT_SUPPORTED );
4098 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4099 status = mbedtls_to_psa_error(
4100 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4101 operation->slot->data.raw.data ) );
4102 if( status != 0 )
4103 goto cleanup;
4104 break;
4105#endif /* MBEDTLS_CHACHAPOLY_C */
4106
Gilles Peskineedf9a652018-08-17 18:11:56 +02004107 default:
4108 return( PSA_ERROR_NOT_SUPPORTED );
4109 }
4110
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004111 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4112 {
4113 status = PSA_ERROR_INVALID_ARGUMENT;
4114 goto cleanup;
4115 }
4116 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004117
Gilles Peskineedf9a652018-08-17 18:11:56 +02004118 return( PSA_SUCCESS );
4119
4120cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004121 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 return( status );
4123}
4124
Gilles Peskinec5487a82018-12-03 18:08:14 +01004125psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004126 psa_algorithm_t alg,
4127 const uint8_t *nonce,
4128 size_t nonce_length,
4129 const uint8_t *additional_data,
4130 size_t additional_data_length,
4131 const uint8_t *plaintext,
4132 size_t plaintext_length,
4133 uint8_t *ciphertext,
4134 size_t ciphertext_size,
4135 size_t *ciphertext_length )
4136{
mohammad16035955c982018-04-26 00:53:03 +03004137 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004138 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004139 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004140
mohammad1603f08a5502018-06-03 15:05:47 +03004141 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004142
Gilles Peskinec5487a82018-12-03 18:08:14 +01004143 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004144 if( status != PSA_SUCCESS )
4145 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004146
Gilles Peskineedf9a652018-08-17 18:11:56 +02004147 /* For all currently supported modes, the tag is at the end of the
4148 * ciphertext. */
4149 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4150 {
4151 status = PSA_ERROR_BUFFER_TOO_SMALL;
4152 goto exit;
4153 }
4154 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004155
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004156#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004157 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004158 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004159 status = mbedtls_to_psa_error(
4160 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4161 MBEDTLS_GCM_ENCRYPT,
4162 plaintext_length,
4163 nonce, nonce_length,
4164 additional_data, additional_data_length,
4165 plaintext, ciphertext,
4166 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004167 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004168 else
4169#endif /* MBEDTLS_GCM_C */
4170#if defined(MBEDTLS_CCM_C)
4171 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004172 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004173 status = mbedtls_to_psa_error(
4174 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4175 plaintext_length,
4176 nonce, nonce_length,
4177 additional_data,
4178 additional_data_length,
4179 plaintext, ciphertext,
4180 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004181 }
mohammad16035c8845f2018-05-09 05:40:09 -07004182 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004183#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004184#if defined(MBEDTLS_CHACHAPOLY_C)
4185 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4186 {
4187 if( nonce_length != 12 || operation.tag_length != 16 )
4188 {
4189 status = PSA_ERROR_NOT_SUPPORTED;
4190 goto exit;
4191 }
4192 status = mbedtls_to_psa_error(
4193 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4194 plaintext_length,
4195 nonce,
4196 additional_data,
4197 additional_data_length,
4198 plaintext,
4199 ciphertext,
4200 tag ) );
4201 }
4202 else
4203#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004204 {
mohammad1603554faad2018-06-03 15:07:38 +03004205 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004206 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004207
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4209 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004210
Gilles Peskineedf9a652018-08-17 18:11:56 +02004211exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004212 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213 if( status == PSA_SUCCESS )
4214 *ciphertext_length = plaintext_length + operation.tag_length;
4215 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004216}
4217
Gilles Peskineee652a32018-06-01 19:23:52 +02004218/* Locate the tag in a ciphertext buffer containing the encrypted data
4219 * followed by the tag. Return the length of the part preceding the tag in
4220 * *plaintext_length. This is the size of the plaintext in modes where
4221 * the encrypted data has the same size as the plaintext, such as
4222 * CCM and GCM. */
4223static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4224 const uint8_t *ciphertext,
4225 size_t ciphertext_length,
4226 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004227 const uint8_t **p_tag )
4228{
4229 size_t payload_length;
4230 if( tag_length > ciphertext_length )
4231 return( PSA_ERROR_INVALID_ARGUMENT );
4232 payload_length = ciphertext_length - tag_length;
4233 if( payload_length > plaintext_size )
4234 return( PSA_ERROR_BUFFER_TOO_SMALL );
4235 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004236 return( PSA_SUCCESS );
4237}
4238
Gilles Peskinec5487a82018-12-03 18:08:14 +01004239psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004240 psa_algorithm_t alg,
4241 const uint8_t *nonce,
4242 size_t nonce_length,
4243 const uint8_t *additional_data,
4244 size_t additional_data_length,
4245 const uint8_t *ciphertext,
4246 size_t ciphertext_length,
4247 uint8_t *plaintext,
4248 size_t plaintext_size,
4249 size_t *plaintext_length )
4250{
mohammad16035955c982018-04-26 00:53:03 +03004251 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004252 aead_operation_t operation;
4253 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004254
Gilles Peskineee652a32018-06-01 19:23:52 +02004255 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004256
Gilles Peskinec5487a82018-12-03 18:08:14 +01004257 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004258 if( status != PSA_SUCCESS )
4259 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004260
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004261 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4262 ciphertext, ciphertext_length,
4263 plaintext_size, &tag );
4264 if( status != PSA_SUCCESS )
4265 goto exit;
4266
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004267#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004268 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004269 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004270 status = mbedtls_to_psa_error(
4271 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4272 ciphertext_length - operation.tag_length,
4273 nonce, nonce_length,
4274 additional_data,
4275 additional_data_length,
4276 tag, operation.tag_length,
4277 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004278 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004279 else
4280#endif /* MBEDTLS_GCM_C */
4281#if defined(MBEDTLS_CCM_C)
4282 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004283 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004284 status = mbedtls_to_psa_error(
4285 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4286 ciphertext_length - operation.tag_length,
4287 nonce, nonce_length,
4288 additional_data,
4289 additional_data_length,
4290 ciphertext, plaintext,
4291 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004292 }
mohammad160339574652018-06-01 04:39:53 -07004293 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004294#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004295#if defined(MBEDTLS_CHACHAPOLY_C)
4296 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4297 {
4298 if( nonce_length != 12 || operation.tag_length != 16 )
4299 {
4300 status = PSA_ERROR_NOT_SUPPORTED;
4301 goto exit;
4302 }
4303 status = mbedtls_to_psa_error(
4304 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4305 ciphertext_length - operation.tag_length,
4306 nonce,
4307 additional_data,
4308 additional_data_length,
4309 tag,
4310 ciphertext,
4311 plaintext ) );
4312 }
4313 else
4314#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004315 {
mohammad1603554faad2018-06-03 15:07:38 +03004316 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004317 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004318
Gilles Peskineedf9a652018-08-17 18:11:56 +02004319 if( status != PSA_SUCCESS && plaintext_size != 0 )
4320 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004321
Gilles Peskineedf9a652018-08-17 18:11:56 +02004322exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004323 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004324 if( status == PSA_SUCCESS )
4325 *plaintext_length = ciphertext_length - operation.tag_length;
4326 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004327}
4328
Gilles Peskinea0655c32018-04-30 17:06:50 +02004329
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004330
Gilles Peskine20035e32018-02-03 22:44:14 +01004331/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004332/* Generators */
4333/****************************************************************/
4334
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004335#define HKDF_STATE_INIT 0 /* no input yet */
4336#define HKDF_STATE_STARTED 1 /* got salt */
4337#define HKDF_STATE_KEYED 2 /* got key */
4338#define HKDF_STATE_OUTPUT 3 /* output started */
4339
Gilles Peskinecbe66502019-05-16 16:59:18 +02004340static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004342{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4344 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004345 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004346 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347}
4348
4349
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004351{
4352 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004354 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004355 {
4356 /* The object has (apparently) been initialized but it is not
4357 * in use. It's ok to call abort on such an object, and there's
4358 * nothing to do. */
4359 }
4360 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004361#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004362 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004363 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004364 mbedtls_free( operation->ctx.hkdf.info );
4365 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004366 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004367 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004369 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004371 if( operation->ctx.tls12_prf.seed != NULL )
4372 {
4373 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4374 operation->ctx.tls12_prf.seed_length );
4375 mbedtls_free( operation->ctx.tls12_prf.seed );
4376 }
4377
4378 if( operation->ctx.tls12_prf.label != NULL )
4379 {
4380 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4381 operation->ctx.tls12_prf.label_length );
4382 mbedtls_free( operation->ctx.tls12_prf.label );
4383 }
4384
4385 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4386
4387 /* We leave the fields Ai and output_block to be erased safely by the
4388 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004389 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004390 else
4391#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004392 {
4393 status = PSA_ERROR_BAD_STATE;
4394 }
Janos Follath083036a2019-06-11 10:22:26 +01004395 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004396 return( status );
4397}
4398
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004400 size_t *capacity)
4401{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004403 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004405 return PSA_ERROR_BAD_STATE;
4406 }
4407
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004409 return( PSA_SUCCESS );
4410}
4411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004413 size_t capacity )
4414{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004416 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004417 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004418 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004420 return( PSA_SUCCESS );
4421}
4422
Gilles Peskinebef7f142018-07-12 17:22:21 +02004423#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004425 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004426static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004427 psa_algorithm_t hash_alg,
4428 uint8_t *output,
4429 size_t output_length )
4430{
4431 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4432 psa_status_t status;
4433
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004434 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4435 return( PSA_ERROR_BAD_STATE );
4436 hkdf->state = HKDF_STATE_OUTPUT;
4437
Gilles Peskinebef7f142018-07-12 17:22:21 +02004438 while( output_length != 0 )
4439 {
4440 /* Copy what remains of the current block */
4441 uint8_t n = hash_length - hkdf->offset_in_block;
4442 if( n > output_length )
4443 n = (uint8_t) output_length;
4444 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4445 output += n;
4446 output_length -= n;
4447 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004448 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004450 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004451 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004452 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004453 * object was corrupted or if this function is called directly
4454 * inside the library. */
4455 if( hkdf->block_number == 0xff )
4456 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004457
4458 /* We need a new block */
4459 ++hkdf->block_number;
4460 hkdf->offset_in_block = 0;
4461 status = psa_hmac_setup_internal( &hkdf->hmac,
4462 hkdf->prk, hash_length,
4463 hash_alg );
4464 if( status != PSA_SUCCESS )
4465 return( status );
4466 if( hkdf->block_number != 1 )
4467 {
4468 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4469 hkdf->output_block,
4470 hash_length );
4471 if( status != PSA_SUCCESS )
4472 return( status );
4473 }
4474 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4475 hkdf->info,
4476 hkdf->info_length );
4477 if( status != PSA_SUCCESS )
4478 return( status );
4479 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4480 &hkdf->block_number, 1 );
4481 if( status != PSA_SUCCESS )
4482 return( status );
4483 status = psa_hmac_finish_internal( &hkdf->hmac,
4484 hkdf->output_block,
4485 sizeof( hkdf->output_block ) );
4486 if( status != PSA_SUCCESS )
4487 return( status );
4488 }
4489
4490 return( PSA_SUCCESS );
4491}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004492
Janos Follath7742fee2019-06-17 12:58:10 +01004493static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4494 psa_tls12_prf_key_derivation_t *tls12_prf,
4495 psa_algorithm_t alg )
4496{
4497 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4498 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004499 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4500 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004501
Janos Follath7742fee2019-06-17 12:58:10 +01004502 /* We can't be wanting more output after block 0xff, otherwise
4503 * the capacity check in psa_key_derivation_output_bytes() would have
4504 * prevented this call. It could happen only if the operation
4505 * object was corrupted or if this function is called directly
4506 * inside the library. */
4507 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004508 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004509
4510 /* We need a new block */
4511 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004512 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004513
4514 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4515 *
4516 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4517 *
4518 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4519 * HMAC_hash(secret, A(2) + seed) +
4520 * HMAC_hash(secret, A(3) + seed) + ...
4521 *
4522 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004523 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004524 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004525 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004526 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004527 * is currently extracted as `output_block` and where i is
4528 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004529 */
4530
Janos Follathea29bfb2019-06-19 12:21:20 +01004531 /* Save the hash context before using it, to preserve the hash state with
4532 * only the inner padding in it. We need this, because inner padding depends
4533 * on the key (secret in the RFC's terminology). */
4534 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4535 if( status != PSA_SUCCESS )
4536 goto cleanup;
4537
4538 /* Calculate A(i) where i = tls12_prf->block_number. */
4539 if( tls12_prf->block_number == 1 )
4540 {
4541 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4542 * the variable seed and in this instance means it in the context of the
4543 * P_hash function, where seed = label + seed.) */
4544 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4545 tls12_prf->label, tls12_prf->label_length );
4546 if( status != PSA_SUCCESS )
4547 goto cleanup;
4548 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4549 tls12_prf->seed, tls12_prf->seed_length );
4550 if( status != PSA_SUCCESS )
4551 goto cleanup;
4552 }
4553 else
4554 {
4555 /* A(i) = HMAC_hash(secret, A(i-1)) */
4556 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4557 tls12_prf->Ai, hash_length );
4558 if( status != PSA_SUCCESS )
4559 goto cleanup;
4560 }
4561
4562 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4563 tls12_prf->Ai, hash_length );
4564 if( status != PSA_SUCCESS )
4565 goto cleanup;
4566 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4567 if( status != PSA_SUCCESS )
4568 goto cleanup;
4569
4570 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4571 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4572 tls12_prf->Ai, hash_length );
4573 if( status != PSA_SUCCESS )
4574 goto cleanup;
4575 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4576 tls12_prf->label, tls12_prf->label_length );
4577 if( status != PSA_SUCCESS )
4578 goto cleanup;
4579 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4580 tls12_prf->seed, tls12_prf->seed_length );
4581 if( status != PSA_SUCCESS )
4582 goto cleanup;
4583 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4584 tls12_prf->output_block, hash_length );
4585 if( status != PSA_SUCCESS )
4586 goto cleanup;
4587 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4588 if( status != PSA_SUCCESS )
4589 goto cleanup;
4590
Janos Follath7742fee2019-06-17 12:58:10 +01004591
4592cleanup:
4593
Janos Follathea29bfb2019-06-19 12:21:20 +01004594 cleanup_status = psa_hash_abort( &backup );
4595 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4596 status = cleanup_status;
4597
4598 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004599}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004600
Janos Follath844eb0e2019-06-19 12:10:49 +01004601static psa_status_t psa_key_derivation_tls12_prf_read(
4602 psa_tls12_prf_key_derivation_t *tls12_prf,
4603 psa_algorithm_t alg,
4604 uint8_t *output,
4605 size_t output_length )
4606{
4607 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4608 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4609 psa_status_t status;
4610 uint8_t offset, length;
4611
4612 while( output_length != 0 )
4613 {
4614 /* Check if we have fully processed the current block. */
4615 if( tls12_prf->left_in_block == 0 )
4616 {
4617 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4618 alg );
4619 if( status != PSA_SUCCESS )
4620 return( status );
4621
4622 continue;
4623 }
4624
4625 if( tls12_prf->left_in_block > output_length )
4626 length = (uint8_t) output_length;
4627 else
4628 length = tls12_prf->left_in_block;
4629
4630 offset = hash_length - tls12_prf->left_in_block;
4631 memcpy( output, tls12_prf->output_block + offset, length );
4632 output += length;
4633 output_length -= length;
4634 tls12_prf->left_in_block -= length;
4635 }
4636
4637 return( PSA_SUCCESS );
4638}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004639#endif /* MBEDTLS_MD_C */
4640
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004641psa_status_t psa_key_derivation_output_bytes(
4642 psa_key_derivation_operation_t *operation,
4643 uint8_t *output,
4644 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004645{
4646 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004648
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004650 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004652 return PSA_ERROR_BAD_STATE;
4653 }
4654
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004656 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004658 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004660 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004661 goto exit;
4662 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004664 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004666 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4668 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004670 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004671 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004672 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674
Gilles Peskinebef7f142018-07-12 17:22:21 +02004675#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004676 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004677 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004678 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004680 output, output_length );
4681 }
Janos Follathadbec812019-06-14 11:05:39 +01004682 else
Janos Follathadbec812019-06-14 11:05:39 +01004683 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004684 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004685 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004687 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004688 output_length );
4689 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004690 else
4691#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004692 {
4693 return( PSA_ERROR_BAD_STATE );
4694 }
4695
4696exit:
4697 if( status != PSA_SUCCESS )
4698 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004699 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 * This allows us to differentiate between exhausted operations and
4701 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4702 * operations. */
4703 psa_algorithm_t alg = operation->alg;
4704 psa_key_derivation_abort( operation );
4705 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004706 memset( output, '!', output_length );
4707 }
4708 return( status );
4709}
4710
Gilles Peskine08542d82018-07-19 17:05:42 +02004711#if defined(MBEDTLS_DES_C)
4712static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4713{
4714 if( data_size >= 8 )
4715 mbedtls_des_key_set_parity( data );
4716 if( data_size >= 16 )
4717 mbedtls_des_key_set_parity( data + 8 );
4718 if( data_size >= 24 )
4719 mbedtls_des_key_set_parity( data + 16 );
4720}
4721#endif /* MBEDTLS_DES_C */
4722
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004723static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_key_slot_t *slot,
4725 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727{
4728 uint8_t *data = NULL;
4729 size_t bytes = PSA_BITS_TO_BYTES( bits );
4730 psa_status_t status;
4731
Gilles Peskine8e338702019-07-30 20:06:31 +02004732 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004733 return( PSA_ERROR_INVALID_ARGUMENT );
4734 if( bits % 8 != 0 )
4735 return( PSA_ERROR_INVALID_ARGUMENT );
4736 data = mbedtls_calloc( 1, bytes );
4737 if( data == NULL )
4738 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741 if( status != PSA_SUCCESS )
4742 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004743#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004744 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004745 psa_des_set_key_parity( data, bytes );
4746#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004747 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748
4749exit:
4750 mbedtls_free( data );
4751 return( status );
4752}
4753
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004754psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004756 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004757{
4758 psa_status_t status;
4759 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004760 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004761
4762 /* Reject any attempt to create a zero-length key so that we don't
4763 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4764 if( psa_get_key_bits( attributes ) == 0 )
4765 return( PSA_ERROR_INVALID_ARGUMENT );
4766
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004767 if( ! operation->can_output_key )
4768 return( PSA_ERROR_NOT_PERMITTED );
4769
Gilles Peskinedf179142019-07-15 22:02:14 +02004770 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4771 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004772#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4773 if( driver != NULL )
4774 {
4775 /* Deriving a key in a secure element is not implemented yet. */
4776 status = PSA_ERROR_NOT_SUPPORTED;
4777 }
4778#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004779 if( status == PSA_SUCCESS )
4780 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004781 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004782 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 }
4785 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004786 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004787 if( status != PSA_SUCCESS )
4788 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004789 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004790 *handle = 0;
4791 }
4792 return( status );
4793}
4794
Gilles Peskineeab56e42018-07-12 17:12:33 +02004795
4796
4797/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004798/* Key derivation */
4799/****************************************************************/
4800
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004803 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004804{
Janos Follath5fe19732019-06-20 15:09:30 +01004805 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4806 * initialisers for this union leave some bytes unspecified.) */
4807 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4808
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004811 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4812 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4813 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004814 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004816 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4817 if( hash_size == 0 )
4818 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004819 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4820 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004821 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822 {
4823 return( PSA_ERROR_NOT_SUPPORTED );
4824 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004826 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 }
4828#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004830 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831}
4832
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004834 psa_algorithm_t alg )
4835{
4836 psa_status_t status;
4837
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004839 return( PSA_ERROR_BAD_STATE );
4840
Gilles Peskine6843c292019-01-18 16:44:49 +01004841 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4842 return( PSA_ERROR_INVALID_ARGUMENT );
4843 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004844 {
4845 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004847 }
4848 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4849 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004851 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004852 else
4853 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004854
4855 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004857 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004858}
4859
4860#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004861static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004862 psa_algorithm_t hash_alg,
4863 psa_key_derivation_step_t step,
4864 const uint8_t *data,
4865 size_t data_length )
4866{
4867 psa_status_t status;
4868 switch( step )
4869 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004870 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004871 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004872 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004873 status = psa_hmac_setup_internal( &hkdf->hmac,
4874 data, data_length,
4875 hash_alg );
4876 if( status != PSA_SUCCESS )
4877 return( status );
4878 hkdf->state = HKDF_STATE_STARTED;
4879 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004880 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004881 /* If no salt was provided, use an empty salt. */
4882 if( hkdf->state == HKDF_STATE_INIT )
4883 {
4884 status = psa_hmac_setup_internal( &hkdf->hmac,
4885 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004886 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 if( status != PSA_SUCCESS )
4888 return( status );
4889 hkdf->state = HKDF_STATE_STARTED;
4890 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004891 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004893 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4894 data, data_length );
4895 if( status != PSA_SUCCESS )
4896 return( status );
4897 status = psa_hmac_finish_internal( &hkdf->hmac,
4898 hkdf->prk,
4899 sizeof( hkdf->prk ) );
4900 if( status != PSA_SUCCESS )
4901 return( status );
4902 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4903 hkdf->block_number = 0;
4904 hkdf->state = HKDF_STATE_KEYED;
4905 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004906 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907 if( hkdf->state == HKDF_STATE_OUTPUT )
4908 return( PSA_ERROR_BAD_STATE );
4909 if( hkdf->info_set )
4910 return( PSA_ERROR_BAD_STATE );
4911 hkdf->info_length = data_length;
4912 if( data_length != 0 )
4913 {
4914 hkdf->info = mbedtls_calloc( 1, data_length );
4915 if( hkdf->info == NULL )
4916 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4917 memcpy( hkdf->info, data, data_length );
4918 }
4919 hkdf->info_set = 1;
4920 return( PSA_SUCCESS );
4921 default:
4922 return( PSA_ERROR_INVALID_ARGUMENT );
4923 }
4924}
Janos Follathb03233e2019-06-11 15:30:30 +01004925
Janos Follathf08e2652019-06-13 09:05:41 +01004926static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4927 const uint8_t *data,
4928 size_t data_length )
4929{
4930 if( prf->state != TLS12_PRF_STATE_INIT )
4931 return( PSA_ERROR_BAD_STATE );
4932
Janos Follathd6dce9f2019-07-04 09:11:38 +01004933 if( data_length != 0 )
4934 {
4935 prf->seed = mbedtls_calloc( 1, data_length );
4936 if( prf->seed == NULL )
4937 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004938
Janos Follathd6dce9f2019-07-04 09:11:38 +01004939 memcpy( prf->seed, data, data_length );
4940 prf->seed_length = data_length;
4941 }
Janos Follathf08e2652019-06-13 09:05:41 +01004942
4943 prf->state = TLS12_PRF_STATE_SEED_SET;
4944
4945 return( PSA_SUCCESS );
4946}
4947
Janos Follath81550542019-06-13 14:26:34 +01004948static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4949 psa_algorithm_t hash_alg,
4950 const uint8_t *data,
4951 size_t data_length )
4952{
4953 psa_status_t status;
4954 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4955 return( PSA_ERROR_BAD_STATE );
4956
4957 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4958 if( status != PSA_SUCCESS )
4959 return( status );
4960
4961 prf->state = TLS12_PRF_STATE_KEY_SET;
4962
4963 return( PSA_SUCCESS );
4964}
4965
Janos Follath6660f0e2019-06-17 08:44:03 +01004966static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4967 psa_tls12_prf_key_derivation_t *prf,
4968 psa_algorithm_t hash_alg,
4969 const uint8_t *data,
4970 size_t data_length )
4971{
4972 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004973 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4974 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004975
4976 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4977 return( PSA_ERROR_INVALID_ARGUMENT );
4978
4979 /* Quoting RFC 4279, Section 2:
4980 *
4981 * The premaster secret is formed as follows: if the PSK is N octets
4982 * long, concatenate a uint16 with the value N, N zero octets, a second
4983 * uint16 with the value N, and the PSK itself.
4984 */
4985
Janos Follath40e13932019-06-26 13:22:29 +01004986 *cur++ = ( data_length >> 8 ) & 0xff;
4987 *cur++ = ( data_length >> 0 ) & 0xff;
4988 memset( cur, 0, data_length );
4989 cur += data_length;
4990 *cur++ = pms[0];
4991 *cur++ = pms[1];
4992 memcpy( cur, data, data_length );
4993 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004994
Janos Follath40e13932019-06-26 13:22:29 +01004995 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01004996
4997 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4998 return( status );
4999}
5000
Janos Follath63028dd2019-06-13 09:15:47 +01005001static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5002 const uint8_t *data,
5003 size_t data_length )
5004{
5005 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5006 return( PSA_ERROR_BAD_STATE );
5007
Janos Follathd6dce9f2019-07-04 09:11:38 +01005008 if( data_length != 0 )
5009 {
5010 prf->label = mbedtls_calloc( 1, data_length );
5011 if( prf->label == NULL )
5012 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005013
Janos Follathd6dce9f2019-07-04 09:11:38 +01005014 memcpy( prf->label, data, data_length );
5015 prf->label_length = data_length;
5016 }
Janos Follath63028dd2019-06-13 09:15:47 +01005017
5018 prf->state = TLS12_PRF_STATE_LABEL_SET;
5019
5020 return( PSA_SUCCESS );
5021}
5022
Janos Follathb03233e2019-06-11 15:30:30 +01005023static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5024 psa_algorithm_t hash_alg,
5025 psa_key_derivation_step_t step,
5026 const uint8_t *data,
5027 size_t data_length )
5028{
Janos Follathb03233e2019-06-11 15:30:30 +01005029 switch( step )
5030 {
Janos Follathf08e2652019-06-13 09:05:41 +01005031 case PSA_KEY_DERIVATION_INPUT_SEED:
5032 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005033 case PSA_KEY_DERIVATION_INPUT_SECRET:
5034 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005035 case PSA_KEY_DERIVATION_INPUT_LABEL:
5036 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005037 default:
5038 return( PSA_ERROR_INVALID_ARGUMENT );
5039 }
5040}
Janos Follath6660f0e2019-06-17 08:44:03 +01005041
5042static psa_status_t psa_tls12_prf_psk_to_ms_input(
5043 psa_tls12_prf_key_derivation_t *prf,
5044 psa_algorithm_t hash_alg,
5045 psa_key_derivation_step_t step,
5046 const uint8_t *data,
5047 size_t data_length )
5048{
5049 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005050 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005051 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5052 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005053 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005054
5055 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5056}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005057#endif /* MBEDTLS_MD_C */
5058
Gilles Peskineb8965192019-09-24 16:21:10 +02005059/** Check whether the given key type is acceptable for the given
5060 * input step of a key derivation.
5061 *
5062 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5063 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5064 * Both secret and non-secret inputs can alternatively have the type
5065 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5066 * that the input was passed as a buffer rather than via a key object.
5067 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005068static int psa_key_derivation_check_input_type(
5069 psa_key_derivation_step_t step,
5070 psa_key_type_t key_type )
5071{
5072 switch( step )
5073 {
5074 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005075 if( key_type == PSA_KEY_TYPE_DERIVE )
5076 return( PSA_SUCCESS );
5077 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005078 return( PSA_SUCCESS );
5079 break;
5080 case PSA_KEY_DERIVATION_INPUT_LABEL:
5081 case PSA_KEY_DERIVATION_INPUT_SALT:
5082 case PSA_KEY_DERIVATION_INPUT_INFO:
5083 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005084 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5085 return( PSA_SUCCESS );
5086 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005087 return( PSA_SUCCESS );
5088 break;
5089 }
5090 return( PSA_ERROR_INVALID_ARGUMENT );
5091}
5092
Janos Follathb80a94e2019-06-12 15:54:46 +01005093static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005095 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005096 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005097 const uint8_t *data,
5098 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005100 psa_status_t status;
5101 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5102
5103 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005104 if( status != PSA_SUCCESS )
5105 goto exit;
5106
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005107#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 step, data, data_length );
5113 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005114 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115 {
Janos Follathb03233e2019-06-11 15:30:30 +01005116 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5117 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5118 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005119 }
5120 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5121 {
5122 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5123 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5124 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005125 }
5126 else
5127#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130 return( PSA_ERROR_BAD_STATE );
5131 }
5132
Gilles Peskine224b0d62019-09-23 18:13:17 +02005133exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005134 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136 return( status );
5137}
5138
Janos Follath51f4a0f2019-06-14 11:35:55 +01005139psa_status_t psa_key_derivation_input_bytes(
5140 psa_key_derivation_operation_t *operation,
5141 psa_key_derivation_step_t step,
5142 const uint8_t *data,
5143 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005144{
Gilles Peskineb8965192019-09-24 16:21:10 +02005145 return( psa_key_derivation_input_internal( operation, step,
5146 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005147 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005148}
5149
Janos Follath51f4a0f2019-06-14 11:35:55 +01005150psa_status_t psa_key_derivation_input_key(
5151 psa_key_derivation_operation_t *operation,
5152 psa_key_derivation_step_t step,
5153 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154{
5155 psa_key_slot_t *slot;
5156 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005157
Gilles Peskine28f8f302019-07-24 13:30:31 +02005158 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005159 PSA_KEY_USAGE_DERIVE,
5160 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005161 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005162 {
5163 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005165 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005166
5167 /* Passing a key object as a SECRET input unlocks the permission
5168 * to output to a key object. */
5169 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5170 operation->can_output_key = 1;
5171
Janos Follathb80a94e2019-06-12 15:54:46 +01005172 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005173 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005174 slot->data.raw.data,
5175 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005176}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005177
5178
5179
5180/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005181/* Key agreement */
5182/****************************************************************/
5183
Gilles Peskinea05219c2018-11-16 16:02:56 +01005184#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005185static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5186 size_t peer_key_length,
5187 const mbedtls_ecp_keypair *our_key,
5188 uint8_t *shared_secret,
5189 size_t shared_secret_size,
5190 size_t *shared_secret_length )
5191{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005192 mbedtls_ecp_keypair *their_key = NULL;
5193 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005194 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005195 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005196
Jaeden Ameroccdce902019-01-10 11:42:27 +00005197 status = psa_import_ec_public_key(
5198 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5199 peer_key, peer_key_length,
5200 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005201 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005202 goto exit;
5203
Jaeden Amero97271b32019-01-10 19:38:51 +00005204 status = mbedtls_to_psa_error(
5205 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5206 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005207 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005208 status = mbedtls_to_psa_error(
5209 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5210 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005211 goto exit;
5212
Jaeden Amero97271b32019-01-10 19:38:51 +00005213 status = mbedtls_to_psa_error(
5214 mbedtls_ecdh_calc_secret( &ecdh,
5215 shared_secret_length,
5216 shared_secret, shared_secret_size,
5217 mbedtls_ctr_drbg_random,
5218 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005219
5220exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005221 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005222 mbedtls_ecp_keypair_free( their_key );
5223 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005224 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005226#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005227
Gilles Peskine01d718c2018-09-18 12:01:02 +02005228#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5229
Gilles Peskine0216fe12019-04-11 21:23:21 +02005230static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5231 psa_key_slot_t *private_key,
5232 const uint8_t *peer_key,
5233 size_t peer_key_length,
5234 uint8_t *shared_secret,
5235 size_t shared_secret_size,
5236 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005237{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005238 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005239 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005240#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005241 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005242 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005243 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005244 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5245 private_key->data.ecp,
5246 shared_secret, shared_secret_size,
5247 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005248#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005249 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005250 (void) private_key;
5251 (void) peer_key;
5252 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005253 (void) shared_secret;
5254 (void) shared_secret_size;
5255 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005256 return( PSA_ERROR_NOT_SUPPORTED );
5257 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005258}
5259
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005260/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261 * to potentially free embedded data structures and wipe confidential data.
5262 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005264 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265 psa_key_slot_t *private_key,
5266 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005267 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005268{
5269 psa_status_t status;
5270 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5271 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273
5274 /* Step 1: run the secret agreement algorithm to generate the shared
5275 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005276 status = psa_key_agreement_raw_internal( ka_alg,
5277 private_key,
5278 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005279 shared_secret,
5280 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005281 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005282 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005283 goto exit;
5284
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005285 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005286 * the shared secret. A shared secret is permitted wherever a key
5287 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005288 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005289 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005290 shared_secret,
5291 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292
Gilles Peskine12313cd2018-06-20 00:20:32 +02005293exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005294 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005295 return( status );
5296}
5297
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005299 psa_key_derivation_step_t step,
5300 psa_key_handle_t private_key,
5301 const uint8_t *peer_key,
5302 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005303{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005304 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005305 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005307 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005308 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005309 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005310 if( status != PSA_SUCCESS )
5311 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005313 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005314 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005315 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005317 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005318}
5319
Gilles Peskinebe697d82019-05-16 18:00:41 +02005320psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5321 psa_key_handle_t private_key,
5322 const uint8_t *peer_key,
5323 size_t peer_key_length,
5324 uint8_t *output,
5325 size_t output_size,
5326 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005327{
5328 psa_key_slot_t *slot;
5329 psa_status_t status;
5330
5331 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5332 {
5333 status = PSA_ERROR_INVALID_ARGUMENT;
5334 goto exit;
5335 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005336 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005337 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005338 if( status != PSA_SUCCESS )
5339 goto exit;
5340
5341 status = psa_key_agreement_raw_internal( alg, slot,
5342 peer_key, peer_key_length,
5343 output, output_size,
5344 output_length );
5345
5346exit:
5347 if( status != PSA_SUCCESS )
5348 {
5349 /* If an error happens and is not handled properly, the output
5350 * may be used as a key to protect sensitive data. Arrange for such
5351 * a key to be random, which is likely to result in decryption or
5352 * verification errors. This is better than filling the buffer with
5353 * some constant data such as zeros, which would result in the data
5354 * being protected with a reproducible, easily knowable key.
5355 */
5356 psa_generate_random( output, output_size );
5357 *output_length = output_size;
5358 }
5359 return( status );
5360}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005361
5362
5363/****************************************************************/
5364/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005365/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005366
Gilles Peskine4c317f42018-07-12 01:24:09 +02005367psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005368 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005369{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005370 int ret;
5371 GUARD_MODULE_INITIALIZED;
5372
Gilles Peskinef181eca2019-08-07 13:49:00 +02005373 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5374 {
5375 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5376 output,
5377 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5378 if( ret != 0 )
5379 return( mbedtls_to_psa_error( ret ) );
5380 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5381 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5382 }
5383
Gilles Peskinee59236f2018-01-27 23:32:46 +01005384 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5385 return( mbedtls_to_psa_error( ret ) );
5386}
5387
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005388#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5389#include "mbedtls/entropy_poll.h"
5390
Gilles Peskine7228da22019-07-15 11:06:15 +02005391psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005392 size_t seed_size )
5393{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005394 if( global_data.initialized )
5395 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005396
5397 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5398 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5399 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5400 return( PSA_ERROR_INVALID_ARGUMENT );
5401
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005402 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005403}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005404#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005405
Gilles Peskinee56e8782019-04-26 17:34:02 +02005406#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5407static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5408 size_t domain_parameters_size,
5409 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005410{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005411 size_t i;
5412 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005413
Gilles Peskinee56e8782019-04-26 17:34:02 +02005414 if( domain_parameters_size == 0 )
5415 {
5416 *exponent = 65537;
5417 return( PSA_SUCCESS );
5418 }
5419
5420 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5421 * support values that fit in a 32-bit integer, which is larger than
5422 * int on just about every platform anyway. */
5423 if( domain_parameters_size > sizeof( acc ) )
5424 return( PSA_ERROR_NOT_SUPPORTED );
5425 for( i = 0; i < domain_parameters_size; i++ )
5426 acc = ( acc << 8 ) | domain_parameters[i];
5427 if( acc > INT_MAX )
5428 return( PSA_ERROR_NOT_SUPPORTED );
5429 *exponent = acc;
5430 return( PSA_SUCCESS );
5431}
5432#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5433
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005434static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005435 psa_key_slot_t *slot, size_t bits,
5436 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005437{
Gilles Peskine8e338702019-07-30 20:06:31 +02005438 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005439
Gilles Peskinee56e8782019-04-26 17:34:02 +02005440 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005441 return( PSA_ERROR_INVALID_ARGUMENT );
5442
Gilles Peskinee59236f2018-01-27 23:32:46 +01005443 if( key_type_is_raw_bytes( type ) )
5444 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005445 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005446 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5447 if( status != PSA_SUCCESS )
5448 return( status );
5449 status = psa_generate_random( slot->data.raw.data,
5450 slot->data.raw.bytes );
5451 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005453#if defined(MBEDTLS_DES_C)
5454 if( type == PSA_KEY_TYPE_DES )
5455 psa_des_set_key_parity( slot->data.raw.data,
5456 slot->data.raw.bytes );
5457#endif /* MBEDTLS_DES_C */
5458 }
5459 else
5460
5461#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005462 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005463 {
5464 mbedtls_rsa_context *rsa;
5465 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005466 int exponent;
5467 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005468 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5469 return( PSA_ERROR_NOT_SUPPORTED );
5470 /* Accept only byte-aligned keys, for the same reasons as
5471 * in psa_import_rsa_key(). */
5472 if( bits % 8 != 0 )
5473 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005474 status = psa_read_rsa_exponent( domain_parameters,
5475 domain_parameters_size,
5476 &exponent );
5477 if( status != PSA_SUCCESS )
5478 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5480 if( rsa == NULL )
5481 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5482 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5483 ret = mbedtls_rsa_gen_key( rsa,
5484 mbedtls_ctr_drbg_random,
5485 &global_data.ctr_drbg,
5486 (unsigned int) bits,
5487 exponent );
5488 if( ret != 0 )
5489 {
5490 mbedtls_rsa_free( rsa );
5491 mbedtls_free( rsa );
5492 return( mbedtls_to_psa_error( ret ) );
5493 }
5494 slot->data.rsa = rsa;
5495 }
5496 else
5497#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5498
5499#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005500 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005501 {
5502 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5503 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5504 const mbedtls_ecp_curve_info *curve_info =
5505 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5506 mbedtls_ecp_keypair *ecp;
5507 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005508 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005509 return( PSA_ERROR_NOT_SUPPORTED );
5510 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5511 return( PSA_ERROR_NOT_SUPPORTED );
5512 if( curve_info->bit_size != bits )
5513 return( PSA_ERROR_INVALID_ARGUMENT );
5514 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5515 if( ecp == NULL )
5516 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5517 mbedtls_ecp_keypair_init( ecp );
5518 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5519 mbedtls_ctr_drbg_random,
5520 &global_data.ctr_drbg );
5521 if( ret != 0 )
5522 {
5523 mbedtls_ecp_keypair_free( ecp );
5524 mbedtls_free( ecp );
5525 return( mbedtls_to_psa_error( ret ) );
5526 }
5527 slot->data.ecp = ecp;
5528 }
5529 else
5530#endif /* MBEDTLS_ECP_C */
5531
5532 return( PSA_ERROR_NOT_SUPPORTED );
5533
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005534 return( PSA_SUCCESS );
5535}
Darryl Green0c6575a2018-11-07 16:05:30 +00005536
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005537psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005538 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005539{
5540 psa_status_t status;
5541 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005542 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005543
Gilles Peskine0f84d622019-09-12 19:03:13 +02005544 /* Reject any attempt to create a zero-length key so that we don't
5545 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5546 if( psa_get_key_bits( attributes ) == 0 )
5547 return( PSA_ERROR_INVALID_ARGUMENT );
5548
Gilles Peskinedf179142019-07-15 22:02:14 +02005549 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5550 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005551 if( status != PSA_SUCCESS )
5552 goto exit;
5553
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005554#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5555 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005556 {
Gilles Peskine11792082019-08-06 18:36:36 +02005557 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5558 size_t pubkey_length = 0; /* We don't support this feature yet */
5559 if( drv->key_management == NULL ||
5560 drv->key_management->p_generate == NULL )
5561 {
5562 status = PSA_ERROR_NOT_SUPPORTED;
5563 goto exit;
5564 }
5565 status = drv->key_management->p_generate(
5566 psa_get_se_driver_context( driver ),
5567 slot->data.se.slot_number, attributes,
5568 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005569 }
Gilles Peskine11792082019-08-06 18:36:36 +02005570 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005571#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005572 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005573 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005574 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005575 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005576 }
Gilles Peskine11792082019-08-06 18:36:36 +02005577
5578exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005579 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005580 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005581 if( status != PSA_SUCCESS )
5582 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005583 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005584 *handle = 0;
5585 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005586 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587}
5588
5589
Gilles Peskinee59236f2018-01-27 23:32:46 +01005590
5591/****************************************************************/
5592/* Module setup */
5593/****************************************************************/
5594
Gilles Peskine5e769522018-11-20 21:59:56 +01005595psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5596 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5597 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5598{
5599 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5600 return( PSA_ERROR_BAD_STATE );
5601 global_data.entropy_init = entropy_init;
5602 global_data.entropy_free = entropy_free;
5603 return( PSA_SUCCESS );
5604}
5605
Gilles Peskinee59236f2018-01-27 23:32:46 +01005606void mbedtls_psa_crypto_free( void )
5607{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005608 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005609 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5610 {
5611 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005612 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005613 }
5614 /* Wipe all remaining data, including configuration.
5615 * In particular, this sets all state indicator to the value
5616 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005617 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005618#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005619 /* Unregister all secure element drivers, so that we restart from
5620 * a pristine state. */
5621 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005622#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005623}
5624
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005625#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5626/** Recover a transaction that was interrupted by a power failure.
5627 *
5628 * This function is called during initialization, before psa_crypto_init()
5629 * returns. If this function returns a failure status, the initialization
5630 * fails.
5631 */
5632static psa_status_t psa_crypto_recover_transaction(
5633 const psa_crypto_transaction_t *transaction )
5634{
5635 switch( transaction->unknown.type )
5636 {
5637 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5638 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005639 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005640 * is implemented.
5641 * https://github.com/ARMmbed/mbed-crypto/issues/218
5642 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005643 default:
5644 /* We found an unsupported transaction in the storage.
5645 * We don't know what state the storage is in. Give up. */
5646 return( PSA_ERROR_STORAGE_FAILURE );
5647 }
5648}
5649#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5650
Gilles Peskinee59236f2018-01-27 23:32:46 +01005651psa_status_t psa_crypto_init( void )
5652{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005653 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005654 const unsigned char drbg_seed[] = "PSA";
5655
Gilles Peskinec6b69072018-11-20 21:42:52 +01005656 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005657 if( global_data.initialized != 0 )
5658 return( PSA_SUCCESS );
5659
Gilles Peskine5e769522018-11-20 21:59:56 +01005660 /* Set default configuration if
5661 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5662 if( global_data.entropy_init == NULL )
5663 global_data.entropy_init = mbedtls_entropy_init;
5664 if( global_data.entropy_free == NULL )
5665 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005666
Gilles Peskinec6b69072018-11-20 21:42:52 +01005667 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005668 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005669#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5670 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5671 /* The PSA entropy injection feature depends on using NV seed as an entropy
5672 * source. Add NV seed as an entropy source for PSA entropy injection. */
5673 mbedtls_entropy_add_source( &global_data.entropy,
5674 mbedtls_nv_seed_poll, NULL,
5675 MBEDTLS_ENTROPY_BLOCK_SIZE,
5676 MBEDTLS_ENTROPY_SOURCE_STRONG );
5677#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005678 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005679 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005680 status = mbedtls_to_psa_error(
5681 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5682 mbedtls_entropy_func,
5683 &global_data.entropy,
5684 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5685 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005686 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005687 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688
Gilles Peskine66fb1262018-12-10 16:29:04 +01005689 status = psa_initialize_key_slots( );
5690 if( status != PSA_SUCCESS )
5691 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005692
Gilles Peskined9348f22019-10-01 15:22:29 +02005693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5694 status = psa_init_all_se_drivers( );
5695 if( status != PSA_SUCCESS )
5696 goto exit;
5697#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5698
Gilles Peskine4b734222019-07-24 15:56:31 +02005699#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005700 status = psa_crypto_load_transaction( );
5701 if( status == PSA_SUCCESS )
5702 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005703 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5704 if( status != PSA_SUCCESS )
5705 goto exit;
5706 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005707 }
5708 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5709 {
5710 /* There's no transaction to complete. It's all good. */
5711 status = PSA_SUCCESS;
5712 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005713#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005714
Gilles Peskinec6b69072018-11-20 21:42:52 +01005715 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005716 global_data.initialized = 1;
5717
5718exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005719 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005720 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005721 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005722}
5723
5724#endif /* MBEDTLS_PSA_CRYPTO_C */