blob: 90158f85251111513b16d4b893a869f4f6866a1c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
454#if defined(MBEDTLS_MD_C)
455 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200456#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
495 /* Allocate memory for the key */
496 raw->bytes = PSA_BITS_TO_BYTES( bits );
497 raw->data = mbedtls_calloc( 1, raw->bytes );
498 if( raw->data == NULL )
499 {
500 raw->bytes = 0;
501 return( PSA_ERROR_INSUFFICIENT_MEMORY );
502 }
503 return( PSA_SUCCESS );
504}
505
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100507/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
508 * that are not a multiple of 8) well. For example, there is only
509 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
510 * way to return the exact bit size of a key.
511 * To keep things simple, reject non-byte-aligned key sizes. */
512static psa_status_t psa_check_rsa_key_byte_aligned(
513 const mbedtls_rsa_context *rsa )
514{
515 mbedtls_mpi n;
516 psa_status_t status;
517 mbedtls_mpi_init( &n );
518 status = mbedtls_to_psa_error(
519 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
520 if( status == PSA_SUCCESS )
521 {
522 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
523 status = PSA_ERROR_NOT_SUPPORTED;
524 }
525 mbedtls_mpi_free( &n );
526 return( status );
527}
528
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000529static psa_status_t psa_import_rsa_key( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 mbedtls_rsa_context **p_rsa )
533{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534 psa_status_t status;
535 mbedtls_pk_context pk;
536 mbedtls_rsa_context *rsa;
537 size_t bits;
538
539 mbedtls_pk_init( &pk );
540
541 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
553 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
559 rsa = mbedtls_pk_rsa( pk );
560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
569 status = psa_check_rsa_key_byte_aligned( rsa );
570
571exit:
572 /* Free the content of the pk object only on error. */
573 if( status != PSA_SUCCESS )
574 {
575 mbedtls_pk_free( &pk );
576 return( status );
577 }
578
579 /* On success, store the content of the object in the RSA context. */
580 *p_rsa = rsa;
581
582 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583}
584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586#if defined(MBEDTLS_ECP_C)
587
588/* Import a public key given as the uncompressed representation defined by SEC1
589 * 2.3.3 as the content of an ECPoint. */
590static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
591 const uint8_t *data,
592 size_t data_length,
593 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000596 mbedtls_ecp_keypair *ecp = NULL;
597 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598
599 *p_ecp = NULL;
600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
601 if( ecp == NULL )
602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
603 mbedtls_ecp_keypair_init( ecp );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the public value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
613 data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616
617 /* Check that the point is on the curve. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 *p_ecp = ecp;
624 return( PSA_SUCCESS );
625
626exit:
627 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair_free( ecp );
630 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
637/* Import a private key given as a byte string which is the private value
638 * in big-endian order. */
639static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
640 const uint8_t *data,
641 size_t data_length,
642 mbedtls_ecp_keypair **p_ecp )
643{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 mbedtls_ecp_keypair *ecp = NULL;
646 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200648 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649 return( PSA_ERROR_INVALID_ARGUMENT );
650
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 *p_ecp = NULL;
652 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
653 if( ecp == NULL )
654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000655 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
657 /* Load the group. */
658 status = mbedtls_to_psa_error(
659 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662 /* Load the secret value. */
663 status = mbedtls_to_psa_error(
664 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
665 if( status != PSA_SUCCESS )
666 goto exit;
667 /* Validate the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
670 if( status != PSA_SUCCESS )
671 goto exit;
672 /* Calculate the public key from the private key. */
673 status = mbedtls_to_psa_error(
674 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
675 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678
679 *p_ecp = ecp;
680 return( PSA_SUCCESS );
681
682exit:
683 if( ecp != NULL )
684 {
685 mbedtls_ecp_keypair_free( ecp );
686 mbedtls_free( ecp );
687 }
688 return( status );
689}
690#endif /* defined(MBEDTLS_ECP_C) */
691
Gilles Peskineb46bef22019-07-30 21:32:04 +0200692
693/** Return the size of the key in the given slot, in bits.
694 *
695 * \param[in] slot A key slot.
696 *
697 * \return The key size in bits, read from the metadata in the slot.
698 */
699static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
700{
701 return( slot->attr.bits );
702}
703
704/** Calculate the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot containing a transparent key.
707 *
708 * \return The key size in bits, calculated from the key data.
709 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200710static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 size_t bits = 0; /* return 0 on an empty slot */
713
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
718 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#endif /* defined(MBEDTLS_RSA_C) */
720#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
722 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724
725 /* We know that the size fits in psa_key_bits_t thanks to checks
726 * when the key was created. */
727 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728}
729
Gilles Peskine8e338702019-07-30 20:06:31 +0200730/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100731 * previously. This function assumes that the slot does not contain
732 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100733psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
734 const uint8_t *data,
735 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Gilles Peskine8e338702019-07-30 20:06:31 +0200739 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200742 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 if( data_length > SIZE_MAX / 8 )
744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750 &slot->data.raw );
751 if( status != PSA_SUCCESS )
752 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200753 if( data_length != 0 )
754 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 }
756 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100757#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200758 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 data, data_length,
762 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000766 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 data, data_length,
769 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000777 data, data_length,
778 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 }
780 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
783 return( PSA_ERROR_NOT_SUPPORTED );
784 }
Darryl Green940d72c2018-07-13 13:18:51 +0100785
Gilles Peskineb46bef22019-07-30 21:32:04 +0200786 if( status == PSA_SUCCESS )
787 {
788 /* Write the actual key size to the slot.
789 * psa_start_key_creation() wrote the size declared by the
790 * caller, which may be 0 (meaning unspecified) or wrong. */
791 slot->attr.bits = psa_calculate_key_bits( slot );
792 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 return( status );
794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
801 psa_algorithm_t alg1,
802 psa_algorithm_t alg2 )
803{
Gilles Peskine549ea862019-05-22 11:45:59 +0200804 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 if( alg1 == alg2 )
806 return( alg1 );
807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
809 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
810 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
811 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
812 {
813 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
814 return( alg2 );
815 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
816 return( alg1 );
817 }
818 /* If the policies are incompatible, allow nothing. */
819 return( 0 );
820}
821
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
823 psa_algorithm_t requested_alg )
824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826 if( requested_alg == policy_alg )
827 return( 1 );
828 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 * and requested_alg is the same hash-and-sign family with any hash,
830 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
832 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
833 {
834 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
835 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
836 }
837 /* If it isn't permitted, it's forbidden. */
838 return( 0 );
839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
844 */
845static int psa_key_policy_permits( const psa_key_policy_t *policy,
846 psa_algorithm_t alg )
847{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200848 return( psa_key_algorithm_permits( policy->alg, alg ) ||
849 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850}
851
Gilles Peskinef603c712019-01-19 13:40:11 +0100852/** Restrict a key policy based on a constraint.
853 *
854 * \param[in,out] policy The policy to restrict.
855 * \param[in] constraint The policy constraint to apply.
856 *
857 * \retval #PSA_SUCCESS
858 * \c *policy contains the intersection of the original value of
859 * \c *policy and \c *constraint.
860 * \retval #PSA_ERROR_INVALID_ARGUMENT
861 * \c *policy and \c *constraint are incompatible.
862 * \c *policy is unchanged.
863 */
864static psa_status_t psa_restrict_key_policy(
865 psa_key_policy_t *policy,
866 const psa_key_policy_t *constraint )
867{
868 psa_algorithm_t intersection_alg =
869 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 psa_algorithm_t intersection_alg2 =
871 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Darryl Green06fd18d2018-07-16 11:21:11 +0100882/** Retrieve a slot which must contain a key. The key must have allow all the
883 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
884 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100885static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100887 psa_key_usage_t usage,
888 psa_algorithm_t alg )
889{
890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100891 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100892
893 *p_slot = NULL;
894
Gilles Peskinec5487a82018-12-03 18:08:14 +0100895 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100896 if( status != PSA_SUCCESS )
897 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 /* Enforce that usage policy for the key slot contains all the flags
900 * required by the usage parameter. There is one exception: public
901 * keys can always be exported, so we treat public key objects as
902 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200903 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907
908 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
911
912 *p_slot = slot;
913 return( PSA_SUCCESS );
914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916/** Retrieve a slot which must contain a transparent key.
917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200921 * This is a temporary function to use instead of psa_get_key_from_slot()
922 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923 */
924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
925static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
926 psa_key_slot_t **p_slot,
927 psa_key_usage_t usage,
928 psa_algorithm_t alg )
929{
930 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200933 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 {
935 *p_slot = NULL;
936 return( PSA_ERROR_NOT_SUPPORTED );
937 }
938 return( PSA_SUCCESS );
939}
940#else /* MBEDTLS_PSA_CRYPTO_SE_C */
941/* With no secure element support, all keys are transparent. */
942#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
943 psa_get_key_from_slot( handle, p_slot, usage, alg )
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
945
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100946/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000948{
Gilles Peskine73167e12019-07-12 23:44:37 +0200949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
950 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000951 {
952 /* No key material to clean. */
953 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200954 else
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200956 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000957 {
958 /* No key material to clean. */
959 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 mbedtls_free( slot->data.raw.data );
963 }
964 else
965#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_rsa_free( slot->data.rsa );
969 mbedtls_free( slot->data.rsa );
970 }
971 else
972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_ecp_keypair_free( slot->data.ecp );
977 mbedtls_free( slot->data.ecp );
978 }
979 else
980#endif /* defined(MBEDTLS_ECP_C) */
981 {
982 /* Shouldn't happen: the key type is not any type that we
983 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200984 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000985 }
986
987 return( PSA_SUCCESS );
988}
989
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990/** Completely wipe a slot in memory, including its policy.
991 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100992psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993{
994 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200995 /* Multipart operations may still be using the key. This is safe
996 * because all multipart operation objects are independent from
997 * the key slot: if they need to access the key after the setup
998 * phase, they have a copy of the key. Note that this means that
999 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000 /* At this point, key material and other type-specific content has
1001 * been wiped. Clear remaining metadata. We can call memset and not
1002 * zeroize because the metadata is not particularly sensitive. */
1003 memset( slot, 0, sizeof( *slot ) );
1004 return( status );
1005}
1006
Gilles Peskinec5487a82018-12-03 18:08:14 +01001007psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001009 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001010 psa_status_t status; /* status of the last operation */
1011 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 psa_se_drv_table_entry_t *driver;
1014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 if( status != PSA_SUCCESS )
1018 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001019
1020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001023 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001024 /* For a key in a secure element, we need to do three things:
1025 * remove the key file in internal storage, destroy the
1026 * key inside the secure element, and update the driver's
1027 * persistent data. Start a transaction that will encompass these
1028 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 status = psa_crypto_save_transaction( );
1034 if( status != PSA_SUCCESS )
1035 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001036 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001037 /* We should still try to destroy the key in the secure
1038 * element and the key metadata in storage. This is especially
1039 * important if the error is that the storage is full.
1040 * But how to do it exactly without risking an inconsistent
1041 * state after a reset?
1042 * https://github.com/ARMmbed/mbed-crypto/issues/215
1043 */
1044 overall_status = status;
1045 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 }
1047
Gilles Peskine354f7672019-07-12 23:46:38 +02001048 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001049 if( overall_status == PSA_SUCCESS )
1050 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001055 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001057 status = psa_destroy_persistent_key( slot->attr.id );
1058 if( overall_status == PSA_SUCCESS )
1059 overall_status = status;
1060
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001061 /* TODO: other slots may have a copy of the same key. We should
1062 * invalidate them.
1063 * https://github.com/ARMmbed/mbed-crypto/issues/214
1064 */
Darryl Greend49a4992018-06-18 17:27:26 +01001065 }
1066#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001067
Gilles Peskinefc762652019-07-22 19:30:34 +02001068#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1069 if( driver != NULL )
1070 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001071 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074 status = psa_crypto_stop_transaction( );
1075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001077 }
1078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1079
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1081exit:
1082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001084 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
1087 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088}
1089
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001091{
Gilles Peskineb699f072019-04-26 16:06:02 +02001092 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001094}
1095
Gilles Peskineb699f072019-04-26 16:06:02 +02001096psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1097 psa_key_type_t type,
1098 const uint8_t *data,
1099 size_t data_length )
1100{
1101 uint8_t *copy = NULL;
1102
1103 if( data_length != 0 )
1104 {
1105 copy = mbedtls_calloc( 1, data_length );
1106 if( copy == NULL )
1107 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1108 memcpy( copy, data, data_length );
1109 }
1110 /* After this point, this function is guaranteed to succeed, so it
1111 * can start modifying `*attributes`. */
1112
1113 if( attributes->domain_parameters != NULL )
1114 {
1115 mbedtls_free( attributes->domain_parameters );
1116 attributes->domain_parameters = NULL;
1117 attributes->domain_parameters_size = 0;
1118 }
1119
1120 attributes->domain_parameters = copy;
1121 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001122 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001123 return( PSA_SUCCESS );
1124}
1125
1126psa_status_t psa_get_key_domain_parameters(
1127 const psa_key_attributes_t *attributes,
1128 uint8_t *data, size_t data_size, size_t *data_length )
1129{
1130 if( attributes->domain_parameters_size > data_size )
1131 return( PSA_ERROR_BUFFER_TOO_SMALL );
1132 *data_length = attributes->domain_parameters_size;
1133 if( attributes->domain_parameters_size != 0 )
1134 memcpy( data, attributes->domain_parameters,
1135 attributes->domain_parameters_size );
1136 return( PSA_SUCCESS );
1137}
1138
1139#if defined(MBEDTLS_RSA_C)
1140static psa_status_t psa_get_rsa_public_exponent(
1141 const mbedtls_rsa_context *rsa,
1142 psa_key_attributes_t *attributes )
1143{
1144 mbedtls_mpi mpi;
1145 int ret;
1146 uint8_t *buffer = NULL;
1147 size_t buflen;
1148 mbedtls_mpi_init( &mpi );
1149
1150 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1151 if( ret != 0 )
1152 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001153 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1154 {
1155 /* It's the default value, which is reported as an empty string,
1156 * so there's nothing to do. */
1157 goto exit;
1158 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001159
1160 buflen = mbedtls_mpi_size( &mpi );
1161 buffer = mbedtls_calloc( 1, buflen );
1162 if( buffer == NULL )
1163 {
1164 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1165 goto exit;
1166 }
1167 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1168 if( ret != 0 )
1169 goto exit;
1170 attributes->domain_parameters = buffer;
1171 attributes->domain_parameters_size = buflen;
1172
1173exit:
1174 mbedtls_mpi_free( &mpi );
1175 if( ret != 0 )
1176 mbedtls_free( buffer );
1177 return( mbedtls_to_psa_error( ret ) );
1178}
1179#endif /* MBEDTLS_RSA_C */
1180
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001181/** Retrieve all the publicly-accessible attributes of a key.
1182 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1184 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001187 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189 psa_reset_key_attributes( attributes );
1190
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001192 if( status != PSA_SUCCESS )
1193 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001194
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001195 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001196 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1197 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1198
1199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1200 if( psa_key_slot_is_external( slot ) )
1201 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
Gilles Peskine8e338702019-07-30 20:06:31 +02001204 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 {
1206#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001207 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001210 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001211 * is not yet implemented.
1212 * https://github.com/ARMmbed/mbed-crypto/issues/216
1213 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001214 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001215 break;
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1218 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001219#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 default:
1221 /* Nothing else to do. */
1222 break;
1223 }
1224
1225 if( status != PSA_SUCCESS )
1226 psa_reset_key_attributes( attributes );
1227 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228}
1229
Gilles Peskinec8000c02019-08-02 20:15:51 +02001230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1231psa_status_t psa_get_key_slot_number(
1232 const psa_key_attributes_t *attributes,
1233 psa_key_slot_number_t *slot_number )
1234{
1235 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1236 {
1237 *slot_number = attributes->slot_number;
1238 return( PSA_SUCCESS );
1239 }
1240 else
1241 return( PSA_ERROR_INVALID_ARGUMENT );
1242}
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1244
Jaeden Ameroccdce902019-01-10 11:42:27 +00001245#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001246static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1247 unsigned char *buf, size_t size )
1248{
1249 int ret;
1250 unsigned char *c;
1251 size_t len = 0;
1252
1253 c = buf + size;
1254
1255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1256
1257 return( (int) len );
1258}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001260
Gilles Peskinef603c712019-01-19 13:40:11 +01001261static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1262 uint8_t *data,
1263 size_t data_size,
1264 size_t *data_length,
1265 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266{
Gilles Peskine5d309672019-07-12 23:47:28 +02001267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1268 const psa_drv_se_t *drv;
1269 psa_drv_se_context_t *drv_context;
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1271
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001272 *data_length = 0;
1273
Gilles Peskine8e338702019-07-30 20:06:31 +02001274 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001275 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001276
Gilles Peskinef9168942019-09-12 19:20:29 +02001277 /* Reject a zero-length output buffer now, since this can never be a
1278 * valid key representation. This way we know that data must be a valid
1279 * pointer and we can do things like memset(data, ..., data_size). */
1280 if( data_size == 0 )
1281 return( PSA_ERROR_BUFFER_TOO_SMALL );
1282
Gilles Peskine5d309672019-07-12 23:47:28 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001285 {
1286 psa_drv_se_export_key_t method;
1287 if( drv->key_management == NULL )
1288 return( PSA_ERROR_NOT_SUPPORTED );
1289 method = ( export_public_key ?
1290 drv->key_management->p_export_public :
1291 drv->key_management->p_export );
1292 if( method == NULL )
1293 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001294 return( method( drv_context,
1295 slot->data.se.slot_number,
1296 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001297 }
1298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1299
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301 {
1302 if( slot->data.raw.bytes > data_size )
1303 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001304 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1305 memset( data + slot->data.raw.bytes, 0,
1306 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 *data_length = slot->data.raw.bytes;
1308 return( PSA_SUCCESS );
1309 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001310#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001312 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001313 psa_status_t status;
1314
Gilles Peskineb46bef22019-07-30 21:32:04 +02001315 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316 if( bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
1318 status = mbedtls_to_psa_error(
1319 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1320 if( status != PSA_SUCCESS )
1321 return( status );
1322 memset( data + bytes, 0, data_size - bytes );
1323 *data_length = bytes;
1324 return( PSA_SUCCESS );
1325 }
1326#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 else
Moran Peker17e36e12018-05-02 12:55:20 +03001328 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001329#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001330 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1331 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001332 {
Moran Pekera998bc62018-04-16 18:16:20 +03001333 mbedtls_pk_context pk;
1334 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001335 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001336 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001337#if defined(MBEDTLS_RSA_C)
1338 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001339 pk.pk_info = &mbedtls_rsa_info;
1340 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#else
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001344 }
1345 else
1346 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001347#if defined(MBEDTLS_ECP_C)
1348 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001349 pk.pk_info = &mbedtls_eckey_info;
1350 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#else
1352 return( PSA_ERROR_NOT_SUPPORTED );
1353#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001354 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001355 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001356 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001357 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001358 }
Moran Peker17e36e12018-05-02 12:55:20 +03001359 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001360 {
Moran Peker17e36e12018-05-02 12:55:20 +03001361 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 }
Moran Peker60364322018-04-29 11:34:58 +03001363 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001364 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001365 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001366 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001368 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1369 * Move the data to the beginning and erase remaining data
1370 * at the original location. */
1371 if( 2 * (size_t) ret <= data_size )
1372 {
1373 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001374 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001375 }
1376 else if( (size_t) ret < data_size )
1377 {
1378 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 }
Moran Pekera998bc62018-04-16 18:16:20 +03001381 *data_length = ret;
1382 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001383 }
1384 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001385#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001386 {
1387 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001388 it is valid for a special-purpose implementation to omit
1389 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001390 return( PSA_ERROR_NOT_SUPPORTED );
1391 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001392 }
1393}
1394
Gilles Peskinec5487a82018-12-03 18:08:14 +01001395psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001396 uint8_t *data,
1397 size_t data_size,
1398 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001400 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001401 psa_status_t status;
1402
1403 /* Set the key to empty now, so that even when there are errors, we always
1404 * set data_length to a value between 0 and data_size. On error, setting
1405 * the key to empty is a good choice because an empty key representation is
1406 * unlikely to be accepted anywhere. */
1407 *data_length = 0;
1408
1409 /* Export requires the EXPORT flag. There is an exception for public keys,
1410 * which don't require any flag, but psa_get_key_from_slot takes
1411 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001412 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001413 if( status != PSA_SUCCESS )
1414 return( status );
1415 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001416 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001417}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418
Gilles Peskinec5487a82018-12-03 18:08:14 +01001419psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001420 uint8_t *data,
1421 size_t data_size,
1422 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001423{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001424 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001425 psa_status_t status;
1426
1427 /* Set the key to empty now, so that even when there are errors, we always
1428 * set data_length to a value between 0 and data_size. On error, setting
1429 * the key to empty is a good choice because an empty key representation is
1430 * unlikely to be accepted anywhere. */
1431 *data_length = 0;
1432
1433 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001434 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001435 if( status != PSA_SUCCESS )
1436 return( status );
1437 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001438 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001439}
1440
Gilles Peskine91e8c332019-08-02 19:19:39 +02001441#if defined(static_assert)
1442static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1443 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001444static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001445 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001446static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001447 "One or more key attribute flag is listed as both internal-only and external-only" );
1448#endif
1449
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001450/** Validate that a key policy is internally well-formed.
1451 *
1452 * This function only rejects invalid policies. It does not validate the
1453 * consistency of the policy with respect to other attributes of the key
1454 * such as the key type.
1455 */
1456static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001457{
1458 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001459 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 PSA_KEY_USAGE_ENCRYPT |
1461 PSA_KEY_USAGE_DECRYPT |
1462 PSA_KEY_USAGE_SIGN |
1463 PSA_KEY_USAGE_VERIFY |
1464 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1465 return( PSA_ERROR_INVALID_ARGUMENT );
1466
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 return( PSA_SUCCESS );
1468}
1469
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001470/** Validate the internal consistency of key attributes.
1471 *
1472 * This function only rejects invalid attribute values. If does not
1473 * validate the consistency of the attributes with any key data that may
1474 * be involved in the creation of the key.
1475 *
1476 * Call this function early in the key creation process.
1477 *
1478 * \param[in] attributes Key attributes for the new key.
1479 * \param[out] p_drv On any return, the driver for the key, if any.
1480 * NULL for a transparent key.
1481 *
1482 */
1483static psa_status_t psa_validate_key_attributes(
1484 const psa_key_attributes_t *attributes,
1485 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001486{
1487 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001488
1489 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001490 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491 status = psa_validate_persistent_key_parameters(
1492 attributes->core.lifetime, attributes->core.id,
1493 p_drv, 1 );
1494 if( status != PSA_SUCCESS )
1495 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001496 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001497
1498 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500 return( status );
1501
1502 /* Refuse to create overly large keys.
1503 * Note that this doesn't trigger on import if the attributes don't
1504 * explicitly specify a size (so psa_get_key_bits returns 0), so
1505 * psa_import_key() needs its own checks. */
1506 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1507 return( PSA_ERROR_NOT_SUPPORTED );
1508
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509 /* Reject invalid flags. These should not be reachable through the API. */
1510 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1511 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1512 return( PSA_ERROR_INVALID_ARGUMENT );
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( PSA_SUCCESS );
1515}
1516
Gilles Peskine4747d192019-04-17 15:05:45 +02001517/** Prepare a key slot to receive key material.
1518 *
1519 * This function allocates a key slot and sets its metadata.
1520 *
1521 * If this function fails, call psa_fail_key_creation().
1522 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001523 * This function is intended to be used as follows:
1524 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1525 * it with the specified attributes, and assign it a handle.
1526 * -# Populate the slot with the key material.
1527 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1528 * In case of failure at any step, stop the sequence and call
1529 * psa_fail_key_creation().
1530 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001531 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] handle On success, a handle for the allocated slot.
1534 * \param[out] p_slot On success, a pointer to the prepared slot.
1535 * \param[out] p_drv On any return, the driver for the key, if any.
1536 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The key slot is ready to receive key material.
1540 * \return If this function fails, the key slot is an invalid state.
1541 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 */
1543static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001544 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 const psa_key_attributes_t *attributes,
1546 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001548 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001549{
1550 psa_status_t status;
1551 psa_key_slot_t *slot;
1552
Gilles Peskinedf179142019-07-15 22:02:14 +02001553 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 *p_drv = NULL;
1555
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001556 status = psa_validate_key_attributes( attributes, p_drv );
1557 if( status != PSA_SUCCESS )
1558 return( status );
1559
Gilles Peskineedbed562019-08-07 18:19:59 +02001560 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 if( status != PSA_SUCCESS )
1562 return( status );
1563 slot = *p_slot;
1564
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001565 /* We're storing the declared bit-size of the key. It's up to each
1566 * creation mechanism to verify that this information is correct.
1567 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001568 * an input (generate, device) but not for those where the bit-size
1569 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001570
1571 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001572
Gilles Peskine91e8c332019-08-02 19:19:39 +02001573 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001574 * external-only flags, query `attributes`. Thanks to the check
1575 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001576 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * may have set. */
1578 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskinecbaff462019-07-12 23:46:04 +02001580#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001581 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001582 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001583 * create the key file in internal storage, create the
1584 * key inside the secure element, and update the driver's
1585 * persistent data. Start a transaction that will encompass these
1586 * three actions. */
1587 /* The first thing to do is to find a slot number for the new key.
1588 * We save the slot number in persistent storage as part of the
1589 * transaction data. It will be needed to recover if the power
1590 * fails during the key creation process, to clean up on the secure
1591 * element side after restarting. Obtaining a slot number from the
1592 * secure element driver updates its persistent state, but we do not yet
1593 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001594 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001595 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001596 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001597 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001598 &slot->data.se.slot_number );
1599 if( status != PSA_SUCCESS )
1600 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001601 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001602 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001603 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001604 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001605 status = psa_crypto_save_transaction( );
1606 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001607 {
1608 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001609 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001612
1613 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1614 {
1615 /* Key registration only makes sense with a secure element. */
1616 return( PSA_ERROR_INVALID_ARGUMENT );
1617 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001618#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1619
Darryl Green0c6575a2018-11-07 16:05:30 +00001620 return( status );
1621}
Gilles Peskine4747d192019-04-17 15:05:45 +02001622
1623/** Finalize the creation of a key once its key material has been set.
1624 *
1625 * This entails writing the key to persistent storage.
1626 *
1627 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001628 * See the documentation of psa_start_key_creation() for the intended use
1629 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001630 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001631 * \param[in,out] slot Pointer to the slot with key material.
1632 * \param[in] driver The secure element driver for the key,
1633 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 *
1635 * \retval #PSA_SUCCESS
1636 * The key was successfully created. The handle is now valid.
1637 * \return If this function fails, the key slot is an invalid state.
1638 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001640static psa_status_t psa_finish_key_creation(
1641 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001642 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001643{
1644 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001645 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001647
1648#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001649 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001650 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001651#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1652 if( driver != NULL )
1653 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001654 psa_se_key_data_storage_t data;
1655#if defined(static_assert)
1656 static_assert( sizeof( slot->data.se.slot_number ) ==
1657 sizeof( data.slot_number ),
1658 "Slot number size does not match psa_se_key_data_storage_t" );
1659 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1660 "Bit-size size does not match psa_se_key_data_storage_t" );
1661#endif
1662 memcpy( &data.slot_number, &slot->data.se.slot_number,
1663 sizeof( slot->data.se.slot_number ) );
1664 memcpy( &data.bits, &slot->attr.bits,
1665 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001666 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001667 (uint8_t*) &data,
1668 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001669 }
1670 else
1671#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1672 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001673 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001674 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001676 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1677 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001678 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001679 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1680 status = psa_internal_export_key( slot,
1681 buffer, buffer_size, &length,
1682 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001683 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001684 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001685 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001686
Gilles Peskinef9168942019-09-12 19:20:29 +02001687 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001688 mbedtls_free( buffer );
1689 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001691#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1692
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001694 /* Finish the transaction for a key creation. This does not
1695 * happen when registering an existing key. Detect this case
1696 * by checking whether a transaction is in progress (actual
1697 * creation of a key in a secure element requires a transaction,
1698 * but registration doesn't use one). */
1699 if( driver != NULL &&
1700 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001701 {
1702 status = psa_save_se_persistent_data( driver );
1703 if( status != PSA_SUCCESS )
1704 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001705 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001706 return( status );
1707 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001708 status = psa_crypto_stop_transaction( );
1709 if( status != PSA_SUCCESS )
1710 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001711 }
1712#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1713
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 return( status );
1715}
1716
1717/** Abort the creation of a key.
1718 *
1719 * You may call this function after calling psa_start_key_creation(),
1720 * or after psa_finish_key_creation() fails. In other circumstances, this
1721 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001722 * See the documentation of psa_start_key_creation() for the intended use
1723 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001725 * \param[in,out] slot Pointer to the slot with key material.
1726 * \param[in] driver The secure element driver for the key,
1727 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001729static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001730 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001731{
Gilles Peskine011e4282019-06-26 18:34:38 +02001732 (void) driver;
1733
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 if( slot == NULL )
1735 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001736
1737#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001738 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 * element, and the failure happened later (when saving metadata
1740 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001741 * element.
1742 * https://github.com/ARMmbed/mbed-crypto/issues/217
1743 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001744
Gilles Peskined7729582019-08-05 15:55:54 +02001745 /* Abort the ongoing transaction if any (there may not be one if
1746 * the creation process failed before starting one, or if the
1747 * key creation is a registration of a key in a secure element).
1748 * Earlier functions must already have done what it takes to undo any
1749 * partial creation. All that's left is to update the transaction data
1750 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001751 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001752#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1753
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 psa_wipe_key_slot( slot );
1755}
1756
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001757/** Validate optional attributes during key creation.
1758 *
1759 * Some key attributes are optional during key creation. If they are
1760 * specified in the attributes structure, check that they are consistent
1761 * with the data in the slot.
1762 *
1763 * This function should be called near the end of key creation, after
1764 * the slot in memory is fully populated but before saving persistent data.
1765 */
1766static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001767 const psa_key_slot_t *slot,
1768 const psa_key_attributes_t *attributes )
1769{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001770 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001771 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001772 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 return( PSA_ERROR_INVALID_ARGUMENT );
1774 }
1775
1776 if( attributes->domain_parameters_size != 0 )
1777 {
1778#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001779 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001780 {
1781 mbedtls_mpi actual, required;
1782 int ret;
1783 mbedtls_mpi_init( &actual );
1784 mbedtls_mpi_init( &required );
1785 ret = mbedtls_rsa_export( slot->data.rsa,
1786 NULL, NULL, NULL, NULL, &actual );
1787 if( ret != 0 )
1788 goto rsa_exit;
1789 ret = mbedtls_mpi_read_binary( &required,
1790 attributes->domain_parameters,
1791 attributes->domain_parameters_size );
1792 if( ret != 0 )
1793 goto rsa_exit;
1794 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1795 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1796 rsa_exit:
1797 mbedtls_mpi_free( &actual );
1798 mbedtls_mpi_free( &required );
1799 if( ret != 0)
1800 return( mbedtls_to_psa_error( ret ) );
1801 }
1802 else
1803#endif
1804 {
1805 return( PSA_ERROR_INVALID_ARGUMENT );
1806 }
1807 }
1808
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001809 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001811 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 return( PSA_ERROR_INVALID_ARGUMENT );
1813 }
1814
1815 return( PSA_SUCCESS );
1816}
1817
Gilles Peskine4747d192019-04-17 15:05:45 +02001818psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001820 size_t data_length,
1821 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001822{
1823 psa_status_t status;
1824 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001825 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826
Gilles Peskine0f84d622019-09-12 19:03:13 +02001827 /* Reject zero-length symmetric keys (including raw data key objects).
1828 * This also rejects any key which might be encoded as an empty string,
1829 * which is never valid. */
1830 if( data_length == 0 )
1831 return( PSA_ERROR_INVALID_ARGUMENT );
1832
Gilles Peskinedf179142019-07-15 22:02:14 +02001833 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1834 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 if( status != PSA_SUCCESS )
1836 goto exit;
1837
Gilles Peskine5d309672019-07-12 23:47:28 +02001838#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1839 if( driver != NULL )
1840 {
1841 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001842 /* The driver should set the number of key bits, however in
1843 * case it doesn't, we initialize bits to an invalid value. */
1844 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001845 if( drv->key_management == NULL ||
1846 drv->key_management->p_import == NULL )
1847 {
1848 status = PSA_ERROR_NOT_SUPPORTED;
1849 goto exit;
1850 }
1851 status = drv->key_management->p_import(
1852 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001853 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001854 &bits );
1855 if( status != PSA_SUCCESS )
1856 goto exit;
1857 if( bits > PSA_MAX_KEY_BITS )
1858 {
1859 status = PSA_ERROR_NOT_SUPPORTED;
1860 goto exit;
1861 }
1862 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001863 }
1864 else
1865#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1866 {
1867 status = psa_import_key_into_slot( slot, data, data_length );
1868 if( status != PSA_SUCCESS )
1869 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001870 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001871 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001872 if( status != PSA_SUCCESS )
1873 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001874
Gilles Peskine011e4282019-06-26 18:34:38 +02001875 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001876exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 if( status != PSA_SUCCESS )
1878 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 *handle = 0;
1881 }
1882 return( status );
1883}
1884
Gilles Peskined7729582019-08-05 15:55:54 +02001885#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1886psa_status_t mbedtls_psa_register_se_key(
1887 const psa_key_attributes_t *attributes )
1888{
1889 psa_status_t status;
1890 psa_key_slot_t *slot = NULL;
1891 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001892 psa_key_handle_t handle = 0;
1893
1894 /* Leaving attributes unspecified is not currently supported.
1895 * It could make sense to query the key type and size from the
1896 * secure element, but not all secure elements support this
1897 * and the driver HAL doesn't currently support it. */
1898 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1899 return( PSA_ERROR_NOT_SUPPORTED );
1900 if( psa_get_key_bits( attributes ) == 0 )
1901 return( PSA_ERROR_NOT_SUPPORTED );
1902
1903 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1904 &handle, &slot, &driver );
1905 if( status != PSA_SUCCESS )
1906 goto exit;
1907
Gilles Peskined7729582019-08-05 15:55:54 +02001908 status = psa_finish_key_creation( slot, driver );
1909
1910exit:
1911 if( status != PSA_SUCCESS )
1912 {
1913 psa_fail_key_creation( slot, driver );
1914 }
1915 /* Registration doesn't keep the key in RAM. */
1916 psa_close_key( handle );
1917 return( status );
1918}
1919#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1920
Gilles Peskinef603c712019-01-19 13:40:11 +01001921static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001922 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001923{
1924 psa_status_t status;
1925 uint8_t *buffer = NULL;
1926 size_t buffer_size = 0;
1927 size_t length;
1928
Gilles Peskine8e338702019-07-30 20:06:31 +02001929 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001930 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001931 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001932 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001933 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001934 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1935 if( status != PSA_SUCCESS )
1936 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001937 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001938 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001939
1940exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001941 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001942 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001943 return( status );
1944}
1945
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001946psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1947 const psa_key_attributes_t *specified_attributes,
1948 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001949{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001950 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001951 psa_key_slot_t *source_slot = NULL;
1952 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001953 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001954 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001955
Gilles Peskine28f8f302019-07-24 13:30:31 +02001956 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001957 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001958 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001959 goto exit;
1960
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001961 status = psa_validate_optional_attributes( source_slot,
1962 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 if( status != PSA_SUCCESS )
1964 goto exit;
1965
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001966 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001967 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968 if( status != PSA_SUCCESS )
1969 goto exit;
1970
Gilles Peskinedf179142019-07-15 22:02:14 +02001971 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1972 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 if( status != PSA_SUCCESS )
1975 goto exit;
1976
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1978 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001979 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001980 /* Copying to a secure element is not implemented yet. */
1981 status = PSA_ERROR_NOT_SUPPORTED;
1982 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001983 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001984#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001985
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001987 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001989
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001991exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001992 if( status != PSA_SUCCESS )
1993 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001994 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 *target_handle = 0;
1996 }
1997 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998}
1999
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002000
2001
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002002/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002003/* Message digests */
2004/****************************************************************/
2005
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002006static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002007{
2008 switch( alg )
2009 {
2010#if defined(MBEDTLS_MD2_C)
2011 case PSA_ALG_MD2:
2012 return( &mbedtls_md2_info );
2013#endif
2014#if defined(MBEDTLS_MD4_C)
2015 case PSA_ALG_MD4:
2016 return( &mbedtls_md4_info );
2017#endif
2018#if defined(MBEDTLS_MD5_C)
2019 case PSA_ALG_MD5:
2020 return( &mbedtls_md5_info );
2021#endif
2022#if defined(MBEDTLS_RIPEMD160_C)
2023 case PSA_ALG_RIPEMD160:
2024 return( &mbedtls_ripemd160_info );
2025#endif
2026#if defined(MBEDTLS_SHA1_C)
2027 case PSA_ALG_SHA_1:
2028 return( &mbedtls_sha1_info );
2029#endif
2030#if defined(MBEDTLS_SHA256_C)
2031 case PSA_ALG_SHA_224:
2032 return( &mbedtls_sha224_info );
2033 case PSA_ALG_SHA_256:
2034 return( &mbedtls_sha256_info );
2035#endif
2036#if defined(MBEDTLS_SHA512_C)
2037 case PSA_ALG_SHA_384:
2038 return( &mbedtls_sha384_info );
2039 case PSA_ALG_SHA_512:
2040 return( &mbedtls_sha512_info );
2041#endif
2042 default:
2043 return( NULL );
2044 }
2045}
2046
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002047psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2048{
2049 switch( operation->alg )
2050 {
Gilles Peskine81736312018-06-26 15:04:31 +02002051 case 0:
2052 /* The object has (apparently) been initialized but it is not
2053 * in use. It's ok to call abort on such an object, and there's
2054 * nothing to do. */
2055 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002056#if defined(MBEDTLS_MD2_C)
2057 case PSA_ALG_MD2:
2058 mbedtls_md2_free( &operation->ctx.md2 );
2059 break;
2060#endif
2061#if defined(MBEDTLS_MD4_C)
2062 case PSA_ALG_MD4:
2063 mbedtls_md4_free( &operation->ctx.md4 );
2064 break;
2065#endif
2066#if defined(MBEDTLS_MD5_C)
2067 case PSA_ALG_MD5:
2068 mbedtls_md5_free( &operation->ctx.md5 );
2069 break;
2070#endif
2071#if defined(MBEDTLS_RIPEMD160_C)
2072 case PSA_ALG_RIPEMD160:
2073 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2074 break;
2075#endif
2076#if defined(MBEDTLS_SHA1_C)
2077 case PSA_ALG_SHA_1:
2078 mbedtls_sha1_free( &operation->ctx.sha1 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_SHA256_C)
2082 case PSA_ALG_SHA_224:
2083 case PSA_ALG_SHA_256:
2084 mbedtls_sha256_free( &operation->ctx.sha256 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_SHA512_C)
2088 case PSA_ALG_SHA_384:
2089 case PSA_ALG_SHA_512:
2090 mbedtls_sha512_free( &operation->ctx.sha512 );
2091 break;
2092#endif
2093 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002094 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002095 }
2096 operation->alg = 0;
2097 return( PSA_SUCCESS );
2098}
2099
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002100psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002101 psa_algorithm_t alg )
2102{
2103 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002104
2105 /* A context must be freshly initialized before it can be set up. */
2106 if( operation->alg != 0 )
2107 {
2108 return( PSA_ERROR_BAD_STATE );
2109 }
2110
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002111 switch( alg )
2112 {
2113#if defined(MBEDTLS_MD2_C)
2114 case PSA_ALG_MD2:
2115 mbedtls_md2_init( &operation->ctx.md2 );
2116 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2117 break;
2118#endif
2119#if defined(MBEDTLS_MD4_C)
2120 case PSA_ALG_MD4:
2121 mbedtls_md4_init( &operation->ctx.md4 );
2122 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2123 break;
2124#endif
2125#if defined(MBEDTLS_MD5_C)
2126 case PSA_ALG_MD5:
2127 mbedtls_md5_init( &operation->ctx.md5 );
2128 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2129 break;
2130#endif
2131#if defined(MBEDTLS_RIPEMD160_C)
2132 case PSA_ALG_RIPEMD160:
2133 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2134 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2135 break;
2136#endif
2137#if defined(MBEDTLS_SHA1_C)
2138 case PSA_ALG_SHA_1:
2139 mbedtls_sha1_init( &operation->ctx.sha1 );
2140 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2141 break;
2142#endif
2143#if defined(MBEDTLS_SHA256_C)
2144 case PSA_ALG_SHA_224:
2145 mbedtls_sha256_init( &operation->ctx.sha256 );
2146 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2147 break;
2148 case PSA_ALG_SHA_256:
2149 mbedtls_sha256_init( &operation->ctx.sha256 );
2150 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_SHA512_C)
2154 case PSA_ALG_SHA_384:
2155 mbedtls_sha512_init( &operation->ctx.sha512 );
2156 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2157 break;
2158 case PSA_ALG_SHA_512:
2159 mbedtls_sha512_init( &operation->ctx.sha512 );
2160 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2161 break;
2162#endif
2163 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002164 return( PSA_ALG_IS_HASH( alg ) ?
2165 PSA_ERROR_NOT_SUPPORTED :
2166 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002167 }
2168 if( ret == 0 )
2169 operation->alg = alg;
2170 else
2171 psa_hash_abort( operation );
2172 return( mbedtls_to_psa_error( ret ) );
2173}
2174
2175psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2176 const uint8_t *input,
2177 size_t input_length )
2178{
2179 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002180
2181 /* Don't require hash implementations to behave correctly on a
2182 * zero-length input, which may have an invalid pointer. */
2183 if( input_length == 0 )
2184 return( PSA_SUCCESS );
2185
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186 switch( operation->alg )
2187 {
2188#if defined(MBEDTLS_MD2_C)
2189 case PSA_ALG_MD2:
2190 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2191 input, input_length );
2192 break;
2193#endif
2194#if defined(MBEDTLS_MD4_C)
2195 case PSA_ALG_MD4:
2196 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2197 input, input_length );
2198 break;
2199#endif
2200#if defined(MBEDTLS_MD5_C)
2201 case PSA_ALG_MD5:
2202 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2203 input, input_length );
2204 break;
2205#endif
2206#if defined(MBEDTLS_RIPEMD160_C)
2207 case PSA_ALG_RIPEMD160:
2208 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2209 input, input_length );
2210 break;
2211#endif
2212#if defined(MBEDTLS_SHA1_C)
2213 case PSA_ALG_SHA_1:
2214 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2215 input, input_length );
2216 break;
2217#endif
2218#if defined(MBEDTLS_SHA256_C)
2219 case PSA_ALG_SHA_224:
2220 case PSA_ALG_SHA_256:
2221 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2222 input, input_length );
2223 break;
2224#endif
2225#if defined(MBEDTLS_SHA512_C)
2226 case PSA_ALG_SHA_384:
2227 case PSA_ALG_SHA_512:
2228 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2229 input, input_length );
2230 break;
2231#endif
2232 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002233 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002235
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236 if( ret != 0 )
2237 psa_hash_abort( operation );
2238 return( mbedtls_to_psa_error( ret ) );
2239}
2240
2241psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2242 uint8_t *hash,
2243 size_t hash_size,
2244 size_t *hash_length )
2245{
itayzafrir40835d42018-08-02 13:14:17 +03002246 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002248 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249
2250 /* Fill the output buffer with something that isn't a valid hash
2251 * (barring an attack on the hash and deliberately-crafted input),
2252 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002253 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002254 /* If hash_size is 0 then hash may be NULL and then the
2255 * call to memset would have undefined behavior. */
2256 if( hash_size != 0 )
2257 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258
2259 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002260 {
2261 status = PSA_ERROR_BUFFER_TOO_SMALL;
2262 goto exit;
2263 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264
2265 switch( operation->alg )
2266 {
2267#if defined(MBEDTLS_MD2_C)
2268 case PSA_ALG_MD2:
2269 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2270 break;
2271#endif
2272#if defined(MBEDTLS_MD4_C)
2273 case PSA_ALG_MD4:
2274 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2275 break;
2276#endif
2277#if defined(MBEDTLS_MD5_C)
2278 case PSA_ALG_MD5:
2279 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2280 break;
2281#endif
2282#if defined(MBEDTLS_RIPEMD160_C)
2283 case PSA_ALG_RIPEMD160:
2284 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2285 break;
2286#endif
2287#if defined(MBEDTLS_SHA1_C)
2288 case PSA_ALG_SHA_1:
2289 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2290 break;
2291#endif
2292#if defined(MBEDTLS_SHA256_C)
2293 case PSA_ALG_SHA_224:
2294 case PSA_ALG_SHA_256:
2295 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_SHA512_C)
2299 case PSA_ALG_SHA_384:
2300 case PSA_ALG_SHA_512:
2301 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2302 break;
2303#endif
2304 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002305 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002306 }
itayzafrir40835d42018-08-02 13:14:17 +03002307 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308
itayzafrir40835d42018-08-02 13:14:17 +03002309exit:
2310 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002312 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313 return( psa_hash_abort( operation ) );
2314 }
2315 else
2316 {
2317 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002318 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 }
2320}
2321
Gilles Peskine2d277862018-06-18 15:41:12 +02002322psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2323 const uint8_t *hash,
2324 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325{
2326 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2327 size_t actual_hash_length;
2328 psa_status_t status = psa_hash_finish( operation,
2329 actual_hash, sizeof( actual_hash ),
2330 &actual_hash_length );
2331 if( status != PSA_SUCCESS )
2332 return( status );
2333 if( actual_hash_length != hash_length )
2334 return( PSA_ERROR_INVALID_SIGNATURE );
2335 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2336 return( PSA_ERROR_INVALID_SIGNATURE );
2337 return( PSA_SUCCESS );
2338}
2339
Gilles Peskineeb35d782019-01-22 17:56:16 +01002340psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2341 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002342{
2343 if( target_operation->alg != 0 )
2344 return( PSA_ERROR_BAD_STATE );
2345
2346 switch( source_operation->alg )
2347 {
2348 case 0:
2349 return( PSA_ERROR_BAD_STATE );
2350#if defined(MBEDTLS_MD2_C)
2351 case PSA_ALG_MD2:
2352 mbedtls_md2_clone( &target_operation->ctx.md2,
2353 &source_operation->ctx.md2 );
2354 break;
2355#endif
2356#if defined(MBEDTLS_MD4_C)
2357 case PSA_ALG_MD4:
2358 mbedtls_md4_clone( &target_operation->ctx.md4,
2359 &source_operation->ctx.md4 );
2360 break;
2361#endif
2362#if defined(MBEDTLS_MD5_C)
2363 case PSA_ALG_MD5:
2364 mbedtls_md5_clone( &target_operation->ctx.md5,
2365 &source_operation->ctx.md5 );
2366 break;
2367#endif
2368#if defined(MBEDTLS_RIPEMD160_C)
2369 case PSA_ALG_RIPEMD160:
2370 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2371 &source_operation->ctx.ripemd160 );
2372 break;
2373#endif
2374#if defined(MBEDTLS_SHA1_C)
2375 case PSA_ALG_SHA_1:
2376 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2377 &source_operation->ctx.sha1 );
2378 break;
2379#endif
2380#if defined(MBEDTLS_SHA256_C)
2381 case PSA_ALG_SHA_224:
2382 case PSA_ALG_SHA_256:
2383 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2384 &source_operation->ctx.sha256 );
2385 break;
2386#endif
2387#if defined(MBEDTLS_SHA512_C)
2388 case PSA_ALG_SHA_384:
2389 case PSA_ALG_SHA_512:
2390 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2391 &source_operation->ctx.sha512 );
2392 break;
2393#endif
2394 default:
2395 return( PSA_ERROR_NOT_SUPPORTED );
2396 }
2397
2398 target_operation->alg = source_operation->alg;
2399 return( PSA_SUCCESS );
2400}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401
2402
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404/* MAC */
2405/****************************************************************/
2406
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002407static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408 psa_algorithm_t alg,
2409 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002410 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002411 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002414 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002416 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002417 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002418
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2420 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002421 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002423 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002424 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425 mode = MBEDTLS_MODE_STREAM;
2426 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 case PSA_ALG_CTR:
2428 mode = MBEDTLS_MODE_CTR;
2429 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002430 case PSA_ALG_CFB:
2431 mode = MBEDTLS_MODE_CFB;
2432 break;
2433 case PSA_ALG_OFB:
2434 mode = MBEDTLS_MODE_OFB;
2435 break;
2436 case PSA_ALG_CBC_NO_PADDING:
2437 mode = MBEDTLS_MODE_CBC;
2438 break;
2439 case PSA_ALG_CBC_PKCS7:
2440 mode = MBEDTLS_MODE_CBC;
2441 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002442 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002443 mode = MBEDTLS_MODE_CCM;
2444 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002445 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446 mode = MBEDTLS_MODE_GCM;
2447 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002448 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2449 mode = MBEDTLS_MODE_CHACHAPOLY;
2450 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 default:
2452 return( NULL );
2453 }
2454 }
2455 else if( alg == PSA_ALG_CMAC )
2456 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457 else
2458 return( NULL );
2459
2460 switch( key_type )
2461 {
2462 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002463 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464 break;
2465 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002466 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2467 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002469 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002471 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002472 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2473 * but two-key Triple-DES is functionally three-key Triple-DES
2474 * with K1=K3, so that's how we present it to mbedtls. */
2475 if( key_bits == 128 )
2476 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 break;
2478 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002479 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 break;
2481 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002482 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002484 case PSA_KEY_TYPE_CHACHA20:
2485 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2486 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487 default:
2488 return( NULL );
2489 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002490 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002491 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492
Jaeden Amero23bbb752018-06-26 14:16:54 +01002493 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2494 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495}
2496
Gilles Peskinea05219c2018-11-16 16:02:56 +01002497#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002498static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002499{
Gilles Peskine2d277862018-06-18 15:41:12 +02002500 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002501 {
2502 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002503 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002504 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002505 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002506 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002507 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002508 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002509 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002510 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002511 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002512 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002513 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002514 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002515 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002516 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002517 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002518 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002519 return( 128 );
2520 default:
2521 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002522 }
2523}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002524#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002525
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002526/* Initialize the MAC operation structure. Once this function has been
2527 * called, psa_mac_abort can run and will do the right thing. */
2528static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2529 psa_algorithm_t alg )
2530{
2531 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2532
2533 operation->alg = alg;
2534 operation->key_set = 0;
2535 operation->iv_set = 0;
2536 operation->iv_required = 0;
2537 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002538 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002539
2540#if defined(MBEDTLS_CMAC_C)
2541 if( alg == PSA_ALG_CMAC )
2542 {
2543 operation->iv_required = 0;
2544 mbedtls_cipher_init( &operation->ctx.cmac );
2545 status = PSA_SUCCESS;
2546 }
2547 else
2548#endif /* MBEDTLS_CMAC_C */
2549#if defined(MBEDTLS_MD_C)
2550 if( PSA_ALG_IS_HMAC( operation->alg ) )
2551 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002552 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2553 operation->ctx.hmac.hash_ctx.alg = 0;
2554 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002555 }
2556 else
2557#endif /* MBEDTLS_MD_C */
2558 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002559 if( ! PSA_ALG_IS_MAC( alg ) )
2560 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002561 }
2562
2563 if( status != PSA_SUCCESS )
2564 memset( operation, 0, sizeof( *operation ) );
2565 return( status );
2566}
2567
Gilles Peskine01126fa2018-07-12 17:04:55 +02002568#if defined(MBEDTLS_MD_C)
2569static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2570{
Gilles Peskine3f108122018-12-07 18:14:53 +01002571 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002572 return( psa_hash_abort( &hmac->hash_ctx ) );
2573}
2574#endif /* MBEDTLS_MD_C */
2575
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2577{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002578 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002580 /* The object has (apparently) been initialized but it is not
2581 * in use. It's ok to call abort on such an object, and there's
2582 * nothing to do. */
2583 return( PSA_SUCCESS );
2584 }
2585 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002587 if( operation->alg == PSA_ALG_CMAC )
2588 {
2589 mbedtls_cipher_free( &operation->ctx.cmac );
2590 }
2591 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002594 if( PSA_ALG_IS_HMAC( operation->alg ) )
2595 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002596 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002597 }
2598 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002600 {
2601 /* Sanity check (shouldn't happen: operation->alg should
2602 * always have been initialized to a valid value). */
2603 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604 }
Moran Peker41deec42018-04-04 15:43:05 +03002605
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 operation->alg = 0;
2607 operation->key_set = 0;
2608 operation->iv_set = 0;
2609 operation->iv_required = 0;
2610 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002611 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002612
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002614
2615bad_state:
2616 /* If abort is called on an uninitialized object, we can't trust
2617 * anything. Wipe the object in case it contains confidential data.
2618 * This may result in a memory leak if a pointer gets overwritten,
2619 * but it's too late to do anything about this. */
2620 memset( operation, 0, sizeof( *operation ) );
2621 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622}
2623
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002624#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002625static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002626 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002627 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002628 const mbedtls_cipher_info_t *cipher_info )
2629{
2630 int ret;
2631
2632 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002633
2634 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2635 if( ret != 0 )
2636 return( ret );
2637
2638 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2639 slot->data.raw.data,
2640 key_bits );
2641 return( ret );
2642}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002643#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002644
Gilles Peskine248051a2018-06-20 16:09:38 +02002645#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002646static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2647 const uint8_t *key,
2648 size_t key_length,
2649 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002650{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002651 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002653 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002654 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002655 psa_status_t status;
2656
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002657 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2658 * overflow below. This should never trigger if the hash algorithm
2659 * is implemented correctly. */
2660 /* The size checks against the ipad and opad buffers cannot be written
2661 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2662 * because that triggers -Wlogical-op on GCC 7.3. */
2663 if( block_size > sizeof( ipad ) )
2664 return( PSA_ERROR_NOT_SUPPORTED );
2665 if( block_size > sizeof( hmac->opad ) )
2666 return( PSA_ERROR_NOT_SUPPORTED );
2667 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002668 return( PSA_ERROR_NOT_SUPPORTED );
2669
Gilles Peskined223b522018-06-11 18:12:58 +02002670 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002671 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002672 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002674 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002675 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002677 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002678 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002679 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002680 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002681 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002682 }
Gilles Peskine96889972018-07-12 17:07:03 +02002683 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2684 * but it is permitted. It is common when HMAC is used in HKDF, for
2685 * example. Don't call `memcpy` in the 0-length because `key` could be
2686 * an invalid pointer which would make the behavior undefined. */
2687 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002688 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689
Gilles Peskined223b522018-06-11 18:12:58 +02002690 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2691 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002692 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002693 ipad[i] ^= 0x36;
2694 memset( ipad + key_length, 0x36, block_size - key_length );
2695
2696 /* Copy the key material from ipad to opad, flipping the requisite bits,
2697 * and filling the rest of opad with the requisite constant. */
2698 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002699 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2700 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002701
Gilles Peskine01126fa2018-07-12 17:04:55 +02002702 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002703 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002704 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002705
Gilles Peskine01126fa2018-07-12 17:04:55 +02002706 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002707
2708cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002709 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002710
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 return( status );
2712}
Gilles Peskine248051a2018-06-20 16:09:38 +02002713#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002714
Gilles Peskine89167cb2018-07-08 20:12:23 +02002715static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002716 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002717 psa_algorithm_t alg,
2718 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002721 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002723 psa_key_usage_t usage =
2724 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002725 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002726 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002728 /* A context must be freshly initialized before it can be set up. */
2729 if( operation->alg != 0 )
2730 {
2731 return( PSA_ERROR_BAD_STATE );
2732 }
2733
Gilles Peskined911eb72018-08-14 15:18:45 +02002734 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002735 if( status != PSA_SUCCESS )
2736 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002737 if( is_sign )
2738 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739
Gilles Peskine28f8f302019-07-24 13:30:31 +02002740 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002741 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002742 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002743 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002744
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002746 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002747 {
2748 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002749 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002750 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002751 int ret;
2752 if( cipher_info == NULL )
2753 {
2754 status = PSA_ERROR_NOT_SUPPORTED;
2755 goto exit;
2756 }
2757 operation->mac_size = cipher_info->block_size;
2758 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2759 status = mbedtls_to_psa_error( ret );
2760 }
2761 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002764 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002765 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002766 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002767 if( hash_alg == 0 )
2768 {
2769 status = PSA_ERROR_NOT_SUPPORTED;
2770 goto exit;
2771 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002772
2773 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2774 /* Sanity check. This shouldn't fail on a valid configuration. */
2775 if( operation->mac_size == 0 ||
2776 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2777 {
2778 status = PSA_ERROR_NOT_SUPPORTED;
2779 goto exit;
2780 }
2781
Gilles Peskine8e338702019-07-30 20:06:31 +02002782 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002783 {
2784 status = PSA_ERROR_INVALID_ARGUMENT;
2785 goto exit;
2786 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002787
Gilles Peskine01126fa2018-07-12 17:04:55 +02002788 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2789 slot->data.raw.data,
2790 slot->data.raw.bytes,
2791 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002792 }
2793 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002795 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002796 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002797 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002799
Gilles Peskined911eb72018-08-14 15:18:45 +02002800 if( truncated == 0 )
2801 {
2802 /* The "normal" case: untruncated algorithm. Nothing to do. */
2803 }
2804 else if( truncated < 4 )
2805 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002806 /* A very short MAC is too short for security since it can be
2807 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2808 * so we make this our minimum, even though 32 bits is still
2809 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002810 status = PSA_ERROR_NOT_SUPPORTED;
2811 }
2812 else if( truncated > operation->mac_size )
2813 {
2814 /* It's impossible to "truncate" to a larger length. */
2815 status = PSA_ERROR_INVALID_ARGUMENT;
2816 }
2817 else
2818 operation->mac_size = truncated;
2819
Gilles Peskinefbfac682018-07-08 20:51:54 +02002820exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002821 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002823 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002825 else
2826 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002827 operation->key_set = 1;
2828 }
2829 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830}
2831
Gilles Peskine89167cb2018-07-08 20:12:23 +02002832psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002833 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002834 psa_algorithm_t alg )
2835{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002836 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002837}
2838
2839psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002840 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002841 psa_algorithm_t alg )
2842{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002843 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002844}
2845
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2847 const uint8_t *input,
2848 size_t input_length )
2849{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002850 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002852 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002854 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 operation->has_input = 1;
2856
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002859 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002860 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2861 input, input_length );
2862 status = mbedtls_to_psa_error( ret );
2863 }
2864 else
2865#endif /* MBEDTLS_CMAC_C */
2866#if defined(MBEDTLS_MD_C)
2867 if( PSA_ALG_IS_HMAC( operation->alg ) )
2868 {
2869 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2870 input_length );
2871 }
2872 else
2873#endif /* MBEDTLS_MD_C */
2874 {
2875 /* This shouldn't happen if `operation` was initialized by
2876 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002877 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002878 }
2879
Gilles Peskinefbfac682018-07-08 20:51:54 +02002880 if( status != PSA_SUCCESS )
2881 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002882 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883}
2884
Gilles Peskine01126fa2018-07-12 17:04:55 +02002885#if defined(MBEDTLS_MD_C)
2886static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2887 uint8_t *mac,
2888 size_t mac_size )
2889{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002890 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002891 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2892 size_t hash_size = 0;
2893 size_t block_size = psa_get_hash_block_size( hash_alg );
2894 psa_status_t status;
2895
Gilles Peskine01126fa2018-07-12 17:04:55 +02002896 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2897 if( status != PSA_SUCCESS )
2898 return( status );
2899 /* From here on, tmp needs to be wiped. */
2900
2901 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2902 if( status != PSA_SUCCESS )
2903 goto exit;
2904
2905 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2906 if( status != PSA_SUCCESS )
2907 goto exit;
2908
2909 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2910 if( status != PSA_SUCCESS )
2911 goto exit;
2912
Gilles Peskined911eb72018-08-14 15:18:45 +02002913 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2914 if( status != PSA_SUCCESS )
2915 goto exit;
2916
2917 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002918
2919exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002920 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921 return( status );
2922}
2923#endif /* MBEDTLS_MD_C */
2924
mohammad16036df908f2018-04-02 08:34:15 -07002925static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002926 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002927 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002929 if( ! operation->key_set )
2930 return( PSA_ERROR_BAD_STATE );
2931 if( operation->iv_required && ! operation->iv_set )
2932 return( PSA_ERROR_BAD_STATE );
2933
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 if( mac_size < operation->mac_size )
2935 return( PSA_ERROR_BUFFER_TOO_SMALL );
2936
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002938 if( operation->alg == PSA_ALG_CMAC )
2939 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002940 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2941 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2942 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002943 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002944 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002945 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002947 else
2948#endif /* MBEDTLS_CMAC_C */
2949#if defined(MBEDTLS_MD_C)
2950 if( PSA_ALG_IS_HMAC( operation->alg ) )
2951 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002952 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002953 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 }
2955 else
2956#endif /* MBEDTLS_MD_C */
2957 {
2958 /* This shouldn't happen if `operation` was initialized by
2959 * a setup function. */
2960 return( PSA_ERROR_BAD_STATE );
2961 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962}
2963
Gilles Peskineacd4be32018-07-08 19:56:25 +02002964psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2965 uint8_t *mac,
2966 size_t mac_size,
2967 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002968{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002969 psa_status_t status;
2970
Jaeden Amero252ef282019-02-15 14:05:35 +00002971 if( operation->alg == 0 )
2972 {
2973 return( PSA_ERROR_BAD_STATE );
2974 }
2975
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002976 /* Fill the output buffer with something that isn't a valid mac
2977 * (barring an attack on the mac and deliberately-crafted input),
2978 * in case the caller doesn't check the return status properly. */
2979 *mac_length = mac_size;
2980 /* If mac_size is 0 then mac may be NULL and then the
2981 * call to memset would have undefined behavior. */
2982 if( mac_size != 0 )
2983 memset( mac, '!', mac_size );
2984
Gilles Peskine89167cb2018-07-08 20:12:23 +02002985 if( ! operation->is_sign )
2986 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002987 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002988 }
mohammad16036df908f2018-04-02 08:34:15 -07002989
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002990 status = psa_mac_finish_internal( operation, mac, mac_size );
2991
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002992 if( status == PSA_SUCCESS )
2993 {
2994 status = psa_mac_abort( operation );
2995 if( status == PSA_SUCCESS )
2996 *mac_length = operation->mac_size;
2997 else
2998 memset( mac, '!', mac_size );
2999 }
3000 else
3001 psa_mac_abort( operation );
3002 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003003}
3004
Gilles Peskineacd4be32018-07-08 19:56:25 +02003005psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3006 const uint8_t *mac,
3007 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008{
Gilles Peskine828ed142018-06-18 23:25:51 +02003009 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003010 psa_status_t status;
3011
Jaeden Amero252ef282019-02-15 14:05:35 +00003012 if( operation->alg == 0 )
3013 {
3014 return( PSA_ERROR_BAD_STATE );
3015 }
3016
Gilles Peskine89167cb2018-07-08 20:12:23 +02003017 if( operation->is_sign )
3018 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003019 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003020 }
3021 if( operation->mac_size != mac_length )
3022 {
3023 status = PSA_ERROR_INVALID_SIGNATURE;
3024 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003025 }
mohammad16036df908f2018-04-02 08:34:15 -07003026
3027 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003028 actual_mac, sizeof( actual_mac ) );
3029
3030 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3031 status = PSA_ERROR_INVALID_SIGNATURE;
3032
3033cleanup:
3034 if( status == PSA_SUCCESS )
3035 status = psa_mac_abort( operation );
3036 else
3037 psa_mac_abort( operation );
3038
Gilles Peskine3f108122018-12-07 18:14:53 +01003039 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003040
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003041 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042}
3043
3044
Gilles Peskine20035e32018-02-03 22:44:14 +01003045
Gilles Peskine20035e32018-02-03 22:44:14 +01003046/****************************************************************/
3047/* Asymmetric cryptography */
3048/****************************************************************/
3049
Gilles Peskine2b450e32018-06-27 15:42:46 +02003050#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003051/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003052 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003053static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3054 size_t hash_length,
3055 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003056{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003057 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003058 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003059 *md_alg = mbedtls_md_get_type( md_info );
3060
3061 /* The Mbed TLS RSA module uses an unsigned int for hash length
3062 * parameters. Validate that it fits so that we don't risk an
3063 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003064#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003065 if( hash_length > UINT_MAX )
3066 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003067#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003068
3069#if defined(MBEDTLS_PKCS1_V15)
3070 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3071 * must be correct. */
3072 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3073 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003074 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003075 if( md_info == NULL )
3076 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003077 if( mbedtls_md_get_size( md_info ) != hash_length )
3078 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003079 }
3080#endif /* MBEDTLS_PKCS1_V15 */
3081
3082#if defined(MBEDTLS_PKCS1_V21)
3083 /* PSS requires a hash internally. */
3084 if( PSA_ALG_IS_RSA_PSS( alg ) )
3085 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003086 if( md_info == NULL )
3087 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003088 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003089#endif /* MBEDTLS_PKCS1_V21 */
3090
Gilles Peskine61b91d42018-06-08 16:09:36 +02003091 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003092}
3093
Gilles Peskine2b450e32018-06-27 15:42:46 +02003094static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3095 psa_algorithm_t alg,
3096 const uint8_t *hash,
3097 size_t hash_length,
3098 uint8_t *signature,
3099 size_t signature_size,
3100 size_t *signature_length )
3101{
3102 psa_status_t status;
3103 int ret;
3104 mbedtls_md_type_t md_alg;
3105
3106 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3107 if( status != PSA_SUCCESS )
3108 return( status );
3109
Gilles Peskine630a18a2018-06-29 17:49:35 +02003110 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003111 return( PSA_ERROR_BUFFER_TOO_SMALL );
3112
3113#if defined(MBEDTLS_PKCS1_V15)
3114 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3115 {
3116 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3117 MBEDTLS_MD_NONE );
3118 ret = mbedtls_rsa_pkcs1_sign( rsa,
3119 mbedtls_ctr_drbg_random,
3120 &global_data.ctr_drbg,
3121 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003122 md_alg,
3123 (unsigned int) hash_length,
3124 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003125 signature );
3126 }
3127 else
3128#endif /* MBEDTLS_PKCS1_V15 */
3129#if defined(MBEDTLS_PKCS1_V21)
3130 if( PSA_ALG_IS_RSA_PSS( alg ) )
3131 {
3132 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3133 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3134 mbedtls_ctr_drbg_random,
3135 &global_data.ctr_drbg,
3136 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003137 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003138 (unsigned int) hash_length,
3139 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003140 signature );
3141 }
3142 else
3143#endif /* MBEDTLS_PKCS1_V21 */
3144 {
3145 return( PSA_ERROR_INVALID_ARGUMENT );
3146 }
3147
3148 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003149 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003150 return( mbedtls_to_psa_error( ret ) );
3151}
3152
3153static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3154 psa_algorithm_t alg,
3155 const uint8_t *hash,
3156 size_t hash_length,
3157 const uint8_t *signature,
3158 size_t signature_length )
3159{
3160 psa_status_t status;
3161 int ret;
3162 mbedtls_md_type_t md_alg;
3163
3164 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3165 if( status != PSA_SUCCESS )
3166 return( status );
3167
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003168 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3169 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003170
3171#if defined(MBEDTLS_PKCS1_V15)
3172 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3173 {
3174 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3175 MBEDTLS_MD_NONE );
3176 ret = mbedtls_rsa_pkcs1_verify( rsa,
3177 mbedtls_ctr_drbg_random,
3178 &global_data.ctr_drbg,
3179 MBEDTLS_RSA_PUBLIC,
3180 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003181 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003182 hash,
3183 signature );
3184 }
3185 else
3186#endif /* MBEDTLS_PKCS1_V15 */
3187#if defined(MBEDTLS_PKCS1_V21)
3188 if( PSA_ALG_IS_RSA_PSS( alg ) )
3189 {
3190 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3191 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3192 mbedtls_ctr_drbg_random,
3193 &global_data.ctr_drbg,
3194 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003195 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003196 (unsigned int) hash_length,
3197 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003198 signature );
3199 }
3200 else
3201#endif /* MBEDTLS_PKCS1_V21 */
3202 {
3203 return( PSA_ERROR_INVALID_ARGUMENT );
3204 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003205
3206 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3207 * the rest of the signature is invalid". This has little use in
3208 * practice and PSA doesn't report this distinction. */
3209 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3210 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003211 return( mbedtls_to_psa_error( ret ) );
3212}
3213#endif /* MBEDTLS_RSA_C */
3214
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003215#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003216/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3217 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3218 * (even though these functions don't modify it). */
3219static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3220 psa_algorithm_t alg,
3221 const uint8_t *hash,
3222 size_t hash_length,
3223 uint8_t *signature,
3224 size_t signature_size,
3225 size_t *signature_length )
3226{
3227 int ret;
3228 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003229 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003230 mbedtls_mpi_init( &r );
3231 mbedtls_mpi_init( &s );
3232
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003233 if( signature_size < 2 * curve_bytes )
3234 {
3235 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3236 goto cleanup;
3237 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003238
Gilles Peskinea05219c2018-11-16 16:02:56 +01003239#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003240 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3241 {
3242 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3243 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3244 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003245 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3246 &ecp->d, hash,
3247 hash_length, md_alg,
3248 mbedtls_ctr_drbg_random,
3249 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003250 }
3251 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003252#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003253 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003254 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003255 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3256 hash, hash_length,
3257 mbedtls_ctr_drbg_random,
3258 &global_data.ctr_drbg ) );
3259 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003260
3261 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3262 signature,
3263 curve_bytes ) );
3264 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3265 signature + curve_bytes,
3266 curve_bytes ) );
3267
3268cleanup:
3269 mbedtls_mpi_free( &r );
3270 mbedtls_mpi_free( &s );
3271 if( ret == 0 )
3272 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003273 return( mbedtls_to_psa_error( ret ) );
3274}
3275
3276static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3277 const uint8_t *hash,
3278 size_t hash_length,
3279 const uint8_t *signature,
3280 size_t signature_length )
3281{
3282 int ret;
3283 mbedtls_mpi r, s;
3284 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3285 mbedtls_mpi_init( &r );
3286 mbedtls_mpi_init( &s );
3287
3288 if( signature_length != 2 * curve_bytes )
3289 return( PSA_ERROR_INVALID_SIGNATURE );
3290
3291 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3292 signature,
3293 curve_bytes ) );
3294 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3295 signature + curve_bytes,
3296 curve_bytes ) );
3297
3298 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3299 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003300
3301cleanup:
3302 mbedtls_mpi_free( &r );
3303 mbedtls_mpi_free( &s );
3304 return( mbedtls_to_psa_error( ret ) );
3305}
3306#endif /* MBEDTLS_ECDSA_C */
3307
Gilles Peskinec5487a82018-12-03 18:08:14 +01003308psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003309 psa_algorithm_t alg,
3310 const uint8_t *hash,
3311 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003312 uint8_t *signature,
3313 size_t signature_size,
3314 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003315{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003316 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003317 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3319 const psa_drv_se_t *drv;
3320 psa_drv_se_context_t *drv_context;
3321#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003322
3323 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003324 /* Immediately reject a zero-length signature buffer. This guarantees
3325 * that signature must be a valid pointer. (On the other hand, the hash
3326 * buffer can in principle be empty since it doesn't actually have
3327 * to be a hash.) */
3328 if( signature_size == 0 )
3329 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003330
Gilles Peskineedc64242019-08-07 21:05:07 +02003331 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003332 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003333 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003334 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003335 {
3336 status = PSA_ERROR_INVALID_ARGUMENT;
3337 goto exit;
3338 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003339
Gilles Peskineedc64242019-08-07 21:05:07 +02003340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3341 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3342 {
3343 if( drv->asymmetric == NULL ||
3344 drv->asymmetric->p_sign == NULL )
3345 {
3346 status = PSA_ERROR_NOT_SUPPORTED;
3347 goto exit;
3348 }
3349 status = drv->asymmetric->p_sign( drv_context,
3350 slot->data.se.slot_number,
3351 alg,
3352 hash, hash_length,
3353 signature, signature_size,
3354 signature_length );
3355 }
3356 else
3357#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003358#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003359 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003360 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003361 status = psa_rsa_sign( slot->data.rsa,
3362 alg,
3363 hash, hash_length,
3364 signature, signature_size,
3365 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003366 }
3367 else
3368#endif /* defined(MBEDTLS_RSA_C) */
3369#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003370 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003371 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003372#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003373 if(
3374#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3375 PSA_ALG_IS_ECDSA( alg )
3376#else
3377 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3378#endif
3379 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003380 status = psa_ecdsa_sign( slot->data.ecp,
3381 alg,
3382 hash, hash_length,
3383 signature, signature_size,
3384 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003385 else
3386#endif /* defined(MBEDTLS_ECDSA_C) */
3387 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003388 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003389 }
itayzafrir5c753392018-05-08 11:18:38 +03003390 }
3391 else
3392#endif /* defined(MBEDTLS_ECP_C) */
3393 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003394 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003395 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003396
3397exit:
3398 /* Fill the unused part of the output buffer (the whole buffer on error,
3399 * the trailing part on success) with something that isn't a valid mac
3400 * (barring an attack on the mac and deliberately-crafted input),
3401 * in case the caller doesn't check the return status properly. */
3402 if( status == PSA_SUCCESS )
3403 memset( signature + *signature_length, '!',
3404 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003405 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003407 /* If signature_size is 0 then we have nothing to do. We must not call
3408 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003409 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003410}
3411
Gilles Peskinec5487a82018-12-03 18:08:14 +01003412psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003413 psa_algorithm_t alg,
3414 const uint8_t *hash,
3415 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003416 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003417 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003418{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003419 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003420 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003421#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3422 const psa_drv_se_t *drv;
3423 psa_drv_se_context_t *drv_context;
3424#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003425
Gilles Peskineedc64242019-08-07 21:05:07 +02003426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003427 if( status != PSA_SUCCESS )
3428 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003429
Gilles Peskineedc64242019-08-07 21:05:07 +02003430#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3431 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3432 {
3433 if( drv->asymmetric == NULL ||
3434 drv->asymmetric->p_verify == NULL )
3435 return( PSA_ERROR_NOT_SUPPORTED );
3436 return( drv->asymmetric->p_verify( drv_context,
3437 slot->data.se.slot_number,
3438 alg,
3439 hash, hash_length,
3440 signature, signature_length ) );
3441 }
3442 else
3443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003444#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003445 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003446 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003447 return( psa_rsa_verify( slot->data.rsa,
3448 alg,
3449 hash, hash_length,
3450 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451 }
3452 else
3453#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003454#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003455 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003456 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003457#if defined(MBEDTLS_ECDSA_C)
3458 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003459 return( psa_ecdsa_verify( slot->data.ecp,
3460 hash, hash_length,
3461 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003462 else
3463#endif /* defined(MBEDTLS_ECDSA_C) */
3464 {
3465 return( PSA_ERROR_INVALID_ARGUMENT );
3466 }
itayzafrir5c753392018-05-08 11:18:38 +03003467 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003468 else
3469#endif /* defined(MBEDTLS_ECP_C) */
3470 {
3471 return( PSA_ERROR_NOT_SUPPORTED );
3472 }
3473}
3474
Gilles Peskine072ac562018-06-30 00:21:29 +02003475#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3476static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3477 mbedtls_rsa_context *rsa )
3478{
3479 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3480 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3481 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3482 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3483}
3484#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3485
Gilles Peskinec5487a82018-12-03 18:08:14 +01003486psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003487 psa_algorithm_t alg,
3488 const uint8_t *input,
3489 size_t input_length,
3490 const uint8_t *salt,
3491 size_t salt_length,
3492 uint8_t *output,
3493 size_t output_size,
3494 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003495{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003496 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003497 psa_status_t status;
3498
Darryl Green5cc689a2018-07-24 15:34:10 +01003499 (void) input;
3500 (void) input_length;
3501 (void) salt;
3502 (void) output;
3503 (void) output_size;
3504
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003505 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003506
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003507 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3508 return( PSA_ERROR_INVALID_ARGUMENT );
3509
Gilles Peskine28f8f302019-07-24 13:30:31 +02003510 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003511 if( status != PSA_SUCCESS )
3512 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003513 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3514 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003515 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003516
3517#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003518 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519 {
3520 mbedtls_rsa_context *rsa = slot->data.rsa;
3521 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003522 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003523 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003525 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003527 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3528 mbedtls_ctr_drbg_random,
3529 &global_data.ctr_drbg,
3530 MBEDTLS_RSA_PUBLIC,
3531 input_length,
3532 input,
3533 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534 }
3535 else
3536#endif /* MBEDTLS_PKCS1_V15 */
3537#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003538 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003540 psa_rsa_oaep_set_padding_mode( alg, rsa );
3541 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3542 mbedtls_ctr_drbg_random,
3543 &global_data.ctr_drbg,
3544 MBEDTLS_RSA_PUBLIC,
3545 salt, salt_length,
3546 input_length,
3547 input,
3548 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003549 }
3550 else
3551#endif /* MBEDTLS_PKCS1_V21 */
3552 {
3553 return( PSA_ERROR_INVALID_ARGUMENT );
3554 }
3555 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003556 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 return( mbedtls_to_psa_error( ret ) );
3558 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003560#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003561 {
3562 return( PSA_ERROR_NOT_SUPPORTED );
3563 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003564}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565
Gilles Peskinec5487a82018-12-03 18:08:14 +01003566psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003567 psa_algorithm_t alg,
3568 const uint8_t *input,
3569 size_t input_length,
3570 const uint8_t *salt,
3571 size_t salt_length,
3572 uint8_t *output,
3573 size_t output_size,
3574 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003575{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003576 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003577 psa_status_t status;
3578
Darryl Green5cc689a2018-07-24 15:34:10 +01003579 (void) input;
3580 (void) input_length;
3581 (void) salt;
3582 (void) output;
3583 (void) output_size;
3584
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003585 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003587 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3588 return( PSA_ERROR_INVALID_ARGUMENT );
3589
Gilles Peskine28f8f302019-07-24 13:30:31 +02003590 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003591 if( status != PSA_SUCCESS )
3592 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003593 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594 return( PSA_ERROR_INVALID_ARGUMENT );
3595
3596#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003597 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598 {
3599 mbedtls_rsa_context *rsa = slot->data.rsa;
3600 int ret;
3601
Gilles Peskine630a18a2018-06-29 17:49:35 +02003602 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603 return( PSA_ERROR_INVALID_ARGUMENT );
3604
3605#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003606 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003608 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3609 mbedtls_ctr_drbg_random,
3610 &global_data.ctr_drbg,
3611 MBEDTLS_RSA_PRIVATE,
3612 output_length,
3613 input,
3614 output,
3615 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 }
3617 else
3618#endif /* MBEDTLS_PKCS1_V15 */
3619#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003620 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003622 psa_rsa_oaep_set_padding_mode( alg, rsa );
3623 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3624 mbedtls_ctr_drbg_random,
3625 &global_data.ctr_drbg,
3626 MBEDTLS_RSA_PRIVATE,
3627 salt, salt_length,
3628 output_length,
3629 input,
3630 output,
3631 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632 }
3633 else
3634#endif /* MBEDTLS_PKCS1_V21 */
3635 {
3636 return( PSA_ERROR_INVALID_ARGUMENT );
3637 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003638
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 return( mbedtls_to_psa_error( ret ) );
3640 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003642#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 {
3644 return( PSA_ERROR_NOT_SUPPORTED );
3645 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003646}
Gilles Peskine20035e32018-02-03 22:44:14 +01003647
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003648
3649
mohammad1603503973b2018-03-12 15:59:30 +02003650/****************************************************************/
3651/* Symmetric cryptography */
3652/****************************************************************/
3653
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003654/* Initialize the cipher operation structure. Once this function has been
3655 * called, psa_cipher_abort can run and will do the right thing. */
3656static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3657 psa_algorithm_t alg )
3658{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003659 if( ! PSA_ALG_IS_CIPHER( alg ) )
3660 {
3661 memset( operation, 0, sizeof( *operation ) );
3662 return( PSA_ERROR_INVALID_ARGUMENT );
3663 }
3664
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003665 operation->alg = alg;
3666 operation->key_set = 0;
3667 operation->iv_set = 0;
3668 operation->iv_required = 1;
3669 operation->iv_size = 0;
3670 operation->block_size = 0;
3671 mbedtls_cipher_init( &operation->ctx.cipher );
3672 return( PSA_SUCCESS );
3673}
3674
Gilles Peskinee553c652018-06-04 16:22:46 +02003675static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003676 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003677 psa_algorithm_t alg,
3678 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003679{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003680 int ret = 0;
3681 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003682 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003683 size_t key_bits;
3684 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003685 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3686 PSA_KEY_USAGE_ENCRYPT :
3687 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003688
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003689 /* A context must be freshly initialized before it can be set up. */
3690 if( operation->alg != 0 )
3691 {
3692 return( PSA_ERROR_BAD_STATE );
3693 }
3694
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003695 status = psa_cipher_init( operation, alg );
3696 if( status != PSA_SUCCESS )
3697 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003698
Gilles Peskine28f8f302019-07-24 13:30:31 +02003699 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003700 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003701 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003702 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003703
Gilles Peskine8e338702019-07-30 20:06:31 +02003704 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003705 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003706 {
3707 status = PSA_ERROR_NOT_SUPPORTED;
3708 goto exit;
3709 }
mohammad1603503973b2018-03-12 15:59:30 +02003710
mohammad1603503973b2018-03-12 15:59:30 +02003711 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003712 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003713 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003714
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003715#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003716 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003717 {
3718 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003719 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003720 memcpy( keys, slot->data.raw.data, 16 );
3721 memcpy( keys + 16, slot->data.raw.data, 8 );
3722 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3723 keys,
3724 192, cipher_operation );
3725 }
3726 else
3727#endif
3728 {
3729 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3730 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003731 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003732 }
Moran Peker41deec42018-04-04 15:43:05 +03003733 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003734 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003735
mohammad16038481e742018-03-18 13:57:31 +02003736#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003737 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003738 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003739 case PSA_ALG_CBC_NO_PADDING:
3740 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3741 MBEDTLS_PADDING_NONE );
3742 break;
3743 case PSA_ALG_CBC_PKCS7:
3744 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3745 MBEDTLS_PADDING_PKCS7 );
3746 break;
3747 default:
3748 /* The algorithm doesn't involve padding. */
3749 ret = 0;
3750 break;
3751 }
3752 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003753 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003754#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3755
mohammad1603503973b2018-03-12 15:59:30 +02003756 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003757 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003758 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003759 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003760 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003761 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003762 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003763#if defined(MBEDTLS_CHACHA20_C)
3764 else
3765 if( alg == PSA_ALG_CHACHA20 )
3766 operation->iv_size = 12;
3767#endif
mohammad1603503973b2018-03-12 15:59:30 +02003768
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003769exit:
3770 if( status == 0 )
3771 status = mbedtls_to_psa_error( ret );
3772 if( status != 0 )
3773 psa_cipher_abort( operation );
3774 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003775}
3776
Gilles Peskinefe119512018-07-08 21:39:34 +02003777psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003778 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003779 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003780{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003781 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003782}
3783
Gilles Peskinefe119512018-07-08 21:39:34 +02003784psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003785 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003786 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003787{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003788 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003789}
3790
Gilles Peskinefe119512018-07-08 21:39:34 +02003791psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003792 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003793 size_t iv_size,
3794 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003795{
itayzafrir534bd7c2018-08-02 13:56:32 +03003796 psa_status_t status;
3797 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003798 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003799 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003800 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003801 }
Moran Peker41deec42018-04-04 15:43:05 +03003802 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003803 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003804 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003805 goto exit;
3806 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003807 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3808 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003809 if( ret != 0 )
3810 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003811 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003812 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003813 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003814
mohammad16038481e742018-03-18 13:57:31 +02003815 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003816 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003817
Moran Peker395db872018-05-31 14:07:14 +03003818exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003820 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003821 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003822}
3823
Gilles Peskinefe119512018-07-08 21:39:34 +02003824psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003825 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003826 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003827{
itayzafrir534bd7c2018-08-02 13:56:32 +03003828 psa_status_t status;
3829 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003830 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003831 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003832 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003833 }
Moran Pekera28258c2018-05-29 16:25:04 +03003834 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003835 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003836 status = PSA_ERROR_INVALID_ARGUMENT;
3837 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003838 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3840 status = mbedtls_to_psa_error( ret );
3841exit:
3842 if( status == PSA_SUCCESS )
3843 operation->iv_set = 1;
3844 else
mohammad1603503973b2018-03-12 15:59:30 +02003845 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003846 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003847}
3848
Gilles Peskinee553c652018-06-04 16:22:46 +02003849psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3850 const uint8_t *input,
3851 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003852 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003853 size_t output_size,
3854 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003855{
itayzafrir534bd7c2018-08-02 13:56:32 +03003856 psa_status_t status;
3857 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003858 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003859
3860 if( operation->alg == 0 )
3861 {
3862 return( PSA_ERROR_BAD_STATE );
3863 }
3864
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003865 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003866 {
3867 /* Take the unprocessed partial block left over from previous
3868 * update calls, if any, plus the input to this call. Remove
3869 * the last partial block, if any. You get the data that will be
3870 * output in this call. */
3871 expected_output_size =
3872 ( operation->ctx.cipher.unprocessed_len + input_length )
3873 / operation->block_size * operation->block_size;
3874 }
3875 else
3876 {
3877 expected_output_size = input_length;
3878 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003879
Gilles Peskine89d789c2018-06-04 17:17:16 +02003880 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003881 {
3882 status = PSA_ERROR_BUFFER_TOO_SMALL;
3883 goto exit;
3884 }
mohammad160382759612018-03-12 18:16:40 +02003885
mohammad1603503973b2018-03-12 15:59:30 +02003886 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003887 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003888 status = mbedtls_to_psa_error( ret );
3889exit:
3890 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003891 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003893}
3894
Gilles Peskinee553c652018-06-04 16:22:46 +02003895psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3896 uint8_t *output,
3897 size_t output_size,
3898 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003899{
David Saadab4ecc272019-02-14 13:48:10 +02003900 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003901 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003902 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003903
mohammad1603503973b2018-03-12 15:59:30 +02003904 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003905 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003906 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003907 }
3908 if( operation->iv_required && ! operation->iv_set )
3909 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003910 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003911 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003912
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003913 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003914 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3915 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003916 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003917 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003918 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003919 }
3920
Janos Follath315b51c2018-07-09 16:04:51 +01003921 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3922 temp_output_buffer,
3923 output_length );
3924 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003925 {
Janos Follath315b51c2018-07-09 16:04:51 +01003926 status = mbedtls_to_psa_error( cipher_ret );
3927 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003928 }
Janos Follath315b51c2018-07-09 16:04:51 +01003929
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003930 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003931 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003932 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003933 memcpy( output, temp_output_buffer, *output_length );
3934 else
3935 {
Janos Follath315b51c2018-07-09 16:04:51 +01003936 status = PSA_ERROR_BUFFER_TOO_SMALL;
3937 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003938 }
mohammad1603503973b2018-03-12 15:59:30 +02003939
Gilles Peskine3f108122018-12-07 18:14:53 +01003940 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003941 status = psa_cipher_abort( operation );
3942
3943 return( status );
3944
3945error:
3946
3947 *output_length = 0;
3948
Gilles Peskine3f108122018-12-07 18:14:53 +01003949 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003950 (void) psa_cipher_abort( operation );
3951
3952 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003953}
3954
Gilles Peskinee553c652018-06-04 16:22:46 +02003955psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3956{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003957 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003958 {
3959 /* The object has (apparently) been initialized but it is not
3960 * in use. It's ok to call abort on such an object, and there's
3961 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003962 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003963 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003964
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003965 /* Sanity check (shouldn't happen: operation->alg should
3966 * always have been initialized to a valid value). */
3967 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3968 return( PSA_ERROR_BAD_STATE );
3969
mohammad1603503973b2018-03-12 15:59:30 +02003970 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003971
Moran Peker41deec42018-04-04 15:43:05 +03003972 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003973 operation->key_set = 0;
3974 operation->iv_set = 0;
3975 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003976 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003977 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003978
Moran Peker395db872018-05-31 14:07:14 +03003979 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003980}
3981
Gilles Peskinea0655c32018-04-30 17:06:50 +02003982
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003983
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003984
mohammad16035955c982018-04-26 00:53:03 +03003985/****************************************************************/
3986/* AEAD */
3987/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003988
Gilles Peskineedf9a652018-08-17 18:11:56 +02003989typedef struct
3990{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003991 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003992 const mbedtls_cipher_info_t *cipher_info;
3993 union
3994 {
3995#if defined(MBEDTLS_CCM_C)
3996 mbedtls_ccm_context ccm;
3997#endif /* MBEDTLS_CCM_C */
3998#if defined(MBEDTLS_GCM_C)
3999 mbedtls_gcm_context gcm;
4000#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004001#if defined(MBEDTLS_CHACHAPOLY_C)
4002 mbedtls_chachapoly_context chachapoly;
4003#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004004 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004005 psa_algorithm_t core_alg;
4006 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004007 uint8_t tag_length;
4008} aead_operation_t;
4009
Gilles Peskine30a9e412019-01-14 18:36:12 +01004010static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004011{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004012 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004013 {
4014#if defined(MBEDTLS_CCM_C)
4015 case PSA_ALG_CCM:
4016 mbedtls_ccm_free( &operation->ctx.ccm );
4017 break;
4018#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004019#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004020 case PSA_ALG_GCM:
4021 mbedtls_gcm_free( &operation->ctx.gcm );
4022 break;
4023#endif /* MBEDTLS_GCM_C */
4024 }
4025}
4026
4027static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004028 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004029 psa_key_usage_t usage,
4030 psa_algorithm_t alg )
4031{
4032 psa_status_t status;
4033 size_t key_bits;
4034 mbedtls_cipher_id_t cipher_id;
4035
Gilles Peskine28f8f302019-07-24 13:30:31 +02004036 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004037 if( status != PSA_SUCCESS )
4038 return( status );
4039
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004040 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004041
4042 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004043 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004044 &cipher_id );
4045 if( operation->cipher_info == NULL )
4046 return( PSA_ERROR_NOT_SUPPORTED );
4047
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004048 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049 {
4050#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004051 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4052 operation->core_alg = PSA_ALG_CCM;
4053 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004054 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4055 * The call to mbedtls_ccm_encrypt_and_tag or
4056 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004057 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004058 return( PSA_ERROR_INVALID_ARGUMENT );
4059 mbedtls_ccm_init( &operation->ctx.ccm );
4060 status = mbedtls_to_psa_error(
4061 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4062 operation->slot->data.raw.data,
4063 (unsigned int) key_bits ) );
4064 if( status != 0 )
4065 goto cleanup;
4066 break;
4067#endif /* MBEDTLS_CCM_C */
4068
4069#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004070 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4071 operation->core_alg = PSA_ALG_GCM;
4072 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004073 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4074 * The call to mbedtls_gcm_crypt_and_tag or
4075 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004076 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004077 return( PSA_ERROR_INVALID_ARGUMENT );
4078 mbedtls_gcm_init( &operation->ctx.gcm );
4079 status = mbedtls_to_psa_error(
4080 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4081 operation->slot->data.raw.data,
4082 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004083 if( status != 0 )
4084 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085 break;
4086#endif /* MBEDTLS_GCM_C */
4087
Gilles Peskine26869f22019-05-06 15:25:00 +02004088#if defined(MBEDTLS_CHACHAPOLY_C)
4089 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4090 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4091 operation->full_tag_length = 16;
4092 /* We only support the default tag length. */
4093 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4094 return( PSA_ERROR_NOT_SUPPORTED );
4095 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4096 status = mbedtls_to_psa_error(
4097 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4098 operation->slot->data.raw.data ) );
4099 if( status != 0 )
4100 goto cleanup;
4101 break;
4102#endif /* MBEDTLS_CHACHAPOLY_C */
4103
Gilles Peskineedf9a652018-08-17 18:11:56 +02004104 default:
4105 return( PSA_ERROR_NOT_SUPPORTED );
4106 }
4107
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004108 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4109 {
4110 status = PSA_ERROR_INVALID_ARGUMENT;
4111 goto cleanup;
4112 }
4113 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004114
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115 return( PSA_SUCCESS );
4116
4117cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004118 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004119 return( status );
4120}
4121
Gilles Peskinec5487a82018-12-03 18:08:14 +01004122psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004123 psa_algorithm_t alg,
4124 const uint8_t *nonce,
4125 size_t nonce_length,
4126 const uint8_t *additional_data,
4127 size_t additional_data_length,
4128 const uint8_t *plaintext,
4129 size_t plaintext_length,
4130 uint8_t *ciphertext,
4131 size_t ciphertext_size,
4132 size_t *ciphertext_length )
4133{
mohammad16035955c982018-04-26 00:53:03 +03004134 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004135 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004136 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004137
mohammad1603f08a5502018-06-03 15:05:47 +03004138 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004139
Gilles Peskinec5487a82018-12-03 18:08:14 +01004140 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004141 if( status != PSA_SUCCESS )
4142 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004143
Gilles Peskineedf9a652018-08-17 18:11:56 +02004144 /* For all currently supported modes, the tag is at the end of the
4145 * ciphertext. */
4146 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4147 {
4148 status = PSA_ERROR_BUFFER_TOO_SMALL;
4149 goto exit;
4150 }
4151 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004152
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004153#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004154 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004155 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004156 status = mbedtls_to_psa_error(
4157 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4158 MBEDTLS_GCM_ENCRYPT,
4159 plaintext_length,
4160 nonce, nonce_length,
4161 additional_data, additional_data_length,
4162 plaintext, ciphertext,
4163 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004164 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004165 else
4166#endif /* MBEDTLS_GCM_C */
4167#if defined(MBEDTLS_CCM_C)
4168 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004169 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004170 status = mbedtls_to_psa_error(
4171 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4172 plaintext_length,
4173 nonce, nonce_length,
4174 additional_data,
4175 additional_data_length,
4176 plaintext, ciphertext,
4177 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004178 }
mohammad16035c8845f2018-05-09 05:40:09 -07004179 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004180#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004181#if defined(MBEDTLS_CHACHAPOLY_C)
4182 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4183 {
4184 if( nonce_length != 12 || operation.tag_length != 16 )
4185 {
4186 status = PSA_ERROR_NOT_SUPPORTED;
4187 goto exit;
4188 }
4189 status = mbedtls_to_psa_error(
4190 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4191 plaintext_length,
4192 nonce,
4193 additional_data,
4194 additional_data_length,
4195 plaintext,
4196 ciphertext,
4197 tag ) );
4198 }
4199 else
4200#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004201 {
mohammad1603554faad2018-06-03 15:07:38 +03004202 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004203 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004204
Gilles Peskineedf9a652018-08-17 18:11:56 +02004205 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4206 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004207
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004209 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004210 if( status == PSA_SUCCESS )
4211 *ciphertext_length = plaintext_length + operation.tag_length;
4212 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004213}
4214
Gilles Peskineee652a32018-06-01 19:23:52 +02004215/* Locate the tag in a ciphertext buffer containing the encrypted data
4216 * followed by the tag. Return the length of the part preceding the tag in
4217 * *plaintext_length. This is the size of the plaintext in modes where
4218 * the encrypted data has the same size as the plaintext, such as
4219 * CCM and GCM. */
4220static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4221 const uint8_t *ciphertext,
4222 size_t ciphertext_length,
4223 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004224 const uint8_t **p_tag )
4225{
4226 size_t payload_length;
4227 if( tag_length > ciphertext_length )
4228 return( PSA_ERROR_INVALID_ARGUMENT );
4229 payload_length = ciphertext_length - tag_length;
4230 if( payload_length > plaintext_size )
4231 return( PSA_ERROR_BUFFER_TOO_SMALL );
4232 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004233 return( PSA_SUCCESS );
4234}
4235
Gilles Peskinec5487a82018-12-03 18:08:14 +01004236psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004237 psa_algorithm_t alg,
4238 const uint8_t *nonce,
4239 size_t nonce_length,
4240 const uint8_t *additional_data,
4241 size_t additional_data_length,
4242 const uint8_t *ciphertext,
4243 size_t ciphertext_length,
4244 uint8_t *plaintext,
4245 size_t plaintext_size,
4246 size_t *plaintext_length )
4247{
mohammad16035955c982018-04-26 00:53:03 +03004248 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004249 aead_operation_t operation;
4250 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004251
Gilles Peskineee652a32018-06-01 19:23:52 +02004252 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004253
Gilles Peskinec5487a82018-12-03 18:08:14 +01004254 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004255 if( status != PSA_SUCCESS )
4256 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004257
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004258 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4259 ciphertext, ciphertext_length,
4260 plaintext_size, &tag );
4261 if( status != PSA_SUCCESS )
4262 goto exit;
4263
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004264#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004265 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004266 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004267 status = mbedtls_to_psa_error(
4268 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4269 ciphertext_length - operation.tag_length,
4270 nonce, nonce_length,
4271 additional_data,
4272 additional_data_length,
4273 tag, operation.tag_length,
4274 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004275 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004276 else
4277#endif /* MBEDTLS_GCM_C */
4278#if defined(MBEDTLS_CCM_C)
4279 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004280 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004281 status = mbedtls_to_psa_error(
4282 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4283 ciphertext_length - operation.tag_length,
4284 nonce, nonce_length,
4285 additional_data,
4286 additional_data_length,
4287 ciphertext, plaintext,
4288 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004289 }
mohammad160339574652018-06-01 04:39:53 -07004290 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004291#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004292#if defined(MBEDTLS_CHACHAPOLY_C)
4293 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4294 {
4295 if( nonce_length != 12 || operation.tag_length != 16 )
4296 {
4297 status = PSA_ERROR_NOT_SUPPORTED;
4298 goto exit;
4299 }
4300 status = mbedtls_to_psa_error(
4301 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4302 ciphertext_length - operation.tag_length,
4303 nonce,
4304 additional_data,
4305 additional_data_length,
4306 tag,
4307 ciphertext,
4308 plaintext ) );
4309 }
4310 else
4311#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004312 {
mohammad1603554faad2018-06-03 15:07:38 +03004313 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004314 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004315
Gilles Peskineedf9a652018-08-17 18:11:56 +02004316 if( status != PSA_SUCCESS && plaintext_size != 0 )
4317 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004318
Gilles Peskineedf9a652018-08-17 18:11:56 +02004319exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004320 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004321 if( status == PSA_SUCCESS )
4322 *plaintext_length = ciphertext_length - operation.tag_length;
4323 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004324}
4325
Gilles Peskinea0655c32018-04-30 17:06:50 +02004326
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004327
Gilles Peskine20035e32018-02-03 22:44:14 +01004328/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004329/* Generators */
4330/****************************************************************/
4331
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004332#define HKDF_STATE_INIT 0 /* no input yet */
4333#define HKDF_STATE_STARTED 1 /* got salt */
4334#define HKDF_STATE_KEYED 2 /* got key */
4335#define HKDF_STATE_OUTPUT 3 /* output started */
4336
Gilles Peskinecbe66502019-05-16 16:59:18 +02004337static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004339{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004340 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4341 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004342 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004344}
4345
4346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004348{
4349 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004351 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004352 {
4353 /* The object has (apparently) been initialized but it is not
4354 * in use. It's ok to call abort on such an object, and there's
4355 * nothing to do. */
4356 }
4357 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004358#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004359 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004360 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361 mbedtls_free( operation->ctx.hkdf.info );
4362 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004363 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004364 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004366 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004367 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004368 if( operation->ctx.tls12_prf.seed != NULL )
4369 {
4370 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4371 operation->ctx.tls12_prf.seed_length );
4372 mbedtls_free( operation->ctx.tls12_prf.seed );
4373 }
4374
4375 if( operation->ctx.tls12_prf.label != NULL )
4376 {
4377 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4378 operation->ctx.tls12_prf.label_length );
4379 mbedtls_free( operation->ctx.tls12_prf.label );
4380 }
4381
4382 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4383
4384 /* We leave the fields Ai and output_block to be erased safely by the
4385 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004386 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004387 else
4388#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004389 {
4390 status = PSA_ERROR_BAD_STATE;
4391 }
Janos Follath083036a2019-06-11 10:22:26 +01004392 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004393 return( status );
4394}
4395
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004397 size_t *capacity)
4398{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004400 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004402 return PSA_ERROR_BAD_STATE;
4403 }
4404
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004406 return( PSA_SUCCESS );
4407}
4408
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004409psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004410 size_t capacity )
4411{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004413 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004417 return( PSA_SUCCESS );
4418}
4419
Gilles Peskinebef7f142018-07-12 17:22:21 +02004420#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004422 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004423static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424 psa_algorithm_t hash_alg,
4425 uint8_t *output,
4426 size_t output_length )
4427{
4428 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4429 psa_status_t status;
4430
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004431 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4432 return( PSA_ERROR_BAD_STATE );
4433 hkdf->state = HKDF_STATE_OUTPUT;
4434
Gilles Peskinebef7f142018-07-12 17:22:21 +02004435 while( output_length != 0 )
4436 {
4437 /* Copy what remains of the current block */
4438 uint8_t n = hash_length - hkdf->offset_in_block;
4439 if( n > output_length )
4440 n = (uint8_t) output_length;
4441 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4442 output += n;
4443 output_length -= n;
4444 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004445 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004447 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004448 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004450 * object was corrupted or if this function is called directly
4451 * inside the library. */
4452 if( hkdf->block_number == 0xff )
4453 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004454
4455 /* We need a new block */
4456 ++hkdf->block_number;
4457 hkdf->offset_in_block = 0;
4458 status = psa_hmac_setup_internal( &hkdf->hmac,
4459 hkdf->prk, hash_length,
4460 hash_alg );
4461 if( status != PSA_SUCCESS )
4462 return( status );
4463 if( hkdf->block_number != 1 )
4464 {
4465 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4466 hkdf->output_block,
4467 hash_length );
4468 if( status != PSA_SUCCESS )
4469 return( status );
4470 }
4471 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4472 hkdf->info,
4473 hkdf->info_length );
4474 if( status != PSA_SUCCESS )
4475 return( status );
4476 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4477 &hkdf->block_number, 1 );
4478 if( status != PSA_SUCCESS )
4479 return( status );
4480 status = psa_hmac_finish_internal( &hkdf->hmac,
4481 hkdf->output_block,
4482 sizeof( hkdf->output_block ) );
4483 if( status != PSA_SUCCESS )
4484 return( status );
4485 }
4486
4487 return( PSA_SUCCESS );
4488}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004489
Janos Follath7742fee2019-06-17 12:58:10 +01004490static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4491 psa_tls12_prf_key_derivation_t *tls12_prf,
4492 psa_algorithm_t alg )
4493{
4494 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4495 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004496 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4497 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004498
Janos Follath7742fee2019-06-17 12:58:10 +01004499 /* We can't be wanting more output after block 0xff, otherwise
4500 * the capacity check in psa_key_derivation_output_bytes() would have
4501 * prevented this call. It could happen only if the operation
4502 * object was corrupted or if this function is called directly
4503 * inside the library. */
4504 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004505 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004506
4507 /* We need a new block */
4508 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004509 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004510
4511 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4512 *
4513 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4514 *
4515 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4516 * HMAC_hash(secret, A(2) + seed) +
4517 * HMAC_hash(secret, A(3) + seed) + ...
4518 *
4519 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004520 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004521 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004523 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004524 * is currently extracted as `output_block` and where i is
4525 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004526 */
4527
Janos Follathea29bfb2019-06-19 12:21:20 +01004528 /* Save the hash context before using it, to preserve the hash state with
4529 * only the inner padding in it. We need this, because inner padding depends
4530 * on the key (secret in the RFC's terminology). */
4531 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4532 if( status != PSA_SUCCESS )
4533 goto cleanup;
4534
4535 /* Calculate A(i) where i = tls12_prf->block_number. */
4536 if( tls12_prf->block_number == 1 )
4537 {
4538 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4539 * the variable seed and in this instance means it in the context of the
4540 * P_hash function, where seed = label + seed.) */
4541 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4542 tls12_prf->label, tls12_prf->label_length );
4543 if( status != PSA_SUCCESS )
4544 goto cleanup;
4545 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4546 tls12_prf->seed, tls12_prf->seed_length );
4547 if( status != PSA_SUCCESS )
4548 goto cleanup;
4549 }
4550 else
4551 {
4552 /* A(i) = HMAC_hash(secret, A(i-1)) */
4553 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4554 tls12_prf->Ai, hash_length );
4555 if( status != PSA_SUCCESS )
4556 goto cleanup;
4557 }
4558
4559 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4560 tls12_prf->Ai, hash_length );
4561 if( status != PSA_SUCCESS )
4562 goto cleanup;
4563 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4564 if( status != PSA_SUCCESS )
4565 goto cleanup;
4566
4567 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4568 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4569 tls12_prf->Ai, hash_length );
4570 if( status != PSA_SUCCESS )
4571 goto cleanup;
4572 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4573 tls12_prf->label, tls12_prf->label_length );
4574 if( status != PSA_SUCCESS )
4575 goto cleanup;
4576 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4577 tls12_prf->seed, tls12_prf->seed_length );
4578 if( status != PSA_SUCCESS )
4579 goto cleanup;
4580 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4581 tls12_prf->output_block, hash_length );
4582 if( status != PSA_SUCCESS )
4583 goto cleanup;
4584 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4585 if( status != PSA_SUCCESS )
4586 goto cleanup;
4587
Janos Follath7742fee2019-06-17 12:58:10 +01004588
4589cleanup:
4590
Janos Follathea29bfb2019-06-19 12:21:20 +01004591 cleanup_status = psa_hash_abort( &backup );
4592 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4593 status = cleanup_status;
4594
4595 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004596}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004597
Janos Follath844eb0e2019-06-19 12:10:49 +01004598static psa_status_t psa_key_derivation_tls12_prf_read(
4599 psa_tls12_prf_key_derivation_t *tls12_prf,
4600 psa_algorithm_t alg,
4601 uint8_t *output,
4602 size_t output_length )
4603{
4604 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4605 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4606 psa_status_t status;
4607 uint8_t offset, length;
4608
4609 while( output_length != 0 )
4610 {
4611 /* Check if we have fully processed the current block. */
4612 if( tls12_prf->left_in_block == 0 )
4613 {
4614 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4615 alg );
4616 if( status != PSA_SUCCESS )
4617 return( status );
4618
4619 continue;
4620 }
4621
4622 if( tls12_prf->left_in_block > output_length )
4623 length = (uint8_t) output_length;
4624 else
4625 length = tls12_prf->left_in_block;
4626
4627 offset = hash_length - tls12_prf->left_in_block;
4628 memcpy( output, tls12_prf->output_block + offset, length );
4629 output += length;
4630 output_length -= length;
4631 tls12_prf->left_in_block -= length;
4632 }
4633
4634 return( PSA_SUCCESS );
4635}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004636#endif /* MBEDTLS_MD_C */
4637
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004638psa_status_t psa_key_derivation_output_bytes(
4639 psa_key_derivation_operation_t *operation,
4640 uint8_t *output,
4641 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004642{
4643 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004645
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004647 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004649 return PSA_ERROR_BAD_STATE;
4650 }
4651
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004653 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004655 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004657 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004658 goto exit;
4659 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004661 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004663 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004664 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4665 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004668 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671
Gilles Peskinebef7f142018-07-12 17:22:21 +02004672#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004673 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004674 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004675 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004677 output, output_length );
4678 }
Janos Follathadbec812019-06-14 11:05:39 +01004679 else
Janos Follathadbec812019-06-14 11:05:39 +01004680 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004681 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004682 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004684 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004685 output_length );
4686 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004687 else
4688#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004689 {
4690 return( PSA_ERROR_BAD_STATE );
4691 }
4692
4693exit:
4694 if( status != PSA_SUCCESS )
4695 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004696 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 * This allows us to differentiate between exhausted operations and
4698 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4699 * operations. */
4700 psa_algorithm_t alg = operation->alg;
4701 psa_key_derivation_abort( operation );
4702 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004703 memset( output, '!', output_length );
4704 }
4705 return( status );
4706}
4707
Gilles Peskine08542d82018-07-19 17:05:42 +02004708#if defined(MBEDTLS_DES_C)
4709static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4710{
4711 if( data_size >= 8 )
4712 mbedtls_des_key_set_parity( data );
4713 if( data_size >= 16 )
4714 mbedtls_des_key_set_parity( data + 8 );
4715 if( data_size >= 24 )
4716 mbedtls_des_key_set_parity( data + 16 );
4717}
4718#endif /* MBEDTLS_DES_C */
4719
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004720static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004721 psa_key_slot_t *slot,
4722 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004724{
4725 uint8_t *data = NULL;
4726 size_t bytes = PSA_BITS_TO_BYTES( bits );
4727 psa_status_t status;
4728
Gilles Peskine8e338702019-07-30 20:06:31 +02004729 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004730 return( PSA_ERROR_INVALID_ARGUMENT );
4731 if( bits % 8 != 0 )
4732 return( PSA_ERROR_INVALID_ARGUMENT );
4733 data = mbedtls_calloc( 1, bytes );
4734 if( data == NULL )
4735 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4736
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738 if( status != PSA_SUCCESS )
4739 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004740#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004741 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004742 psa_des_set_key_parity( data, bytes );
4743#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004744 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745
4746exit:
4747 mbedtls_free( data );
4748 return( status );
4749}
4750
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004751psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004753 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004754{
4755 psa_status_t status;
4756 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004757 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004758
4759 /* Reject any attempt to create a zero-length key so that we don't
4760 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4761 if( psa_get_key_bits( attributes ) == 0 )
4762 return( PSA_ERROR_INVALID_ARGUMENT );
4763
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004764 if( ! operation->can_output_key )
4765 return( PSA_ERROR_NOT_PERMITTED );
4766
Gilles Peskinedf179142019-07-15 22:02:14 +02004767 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4768 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004769#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4770 if( driver != NULL )
4771 {
4772 /* Deriving a key in a secure element is not implemented yet. */
4773 status = PSA_ERROR_NOT_SUPPORTED;
4774 }
4775#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004776 if( status == PSA_SUCCESS )
4777 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004778 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004779 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004781 }
4782 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004783 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 if( status != PSA_SUCCESS )
4785 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004786 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004787 *handle = 0;
4788 }
4789 return( status );
4790}
4791
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792
4793
4794/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004795/* Key derivation */
4796/****************************************************************/
4797
Gilles Peskine969c5d62019-01-16 15:53:06 +01004798static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004800 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004801{
Janos Follath5fe19732019-06-20 15:09:30 +01004802 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4803 * initialisers for this union leave some bytes unspecified.) */
4804 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4805
Gilles Peskine969c5d62019-01-16 15:53:06 +01004806 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004807#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004808 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4809 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4810 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004811 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004812 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004813 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4814 if( hash_size == 0 )
4815 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004816 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4817 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004818 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819 {
4820 return( PSA_ERROR_NOT_SUPPORTED );
4821 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824 }
4825#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004826 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004828}
4829
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831 psa_algorithm_t alg )
4832{
4833 psa_status_t status;
4834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004836 return( PSA_ERROR_BAD_STATE );
4837
Gilles Peskine6843c292019-01-18 16:44:49 +01004838 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4839 return( PSA_ERROR_INVALID_ARGUMENT );
4840 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004841 {
4842 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004844 }
4845 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4846 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004848 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004849 else
4850 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004851
4852 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004854 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004855}
4856
4857#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004858static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004859 psa_algorithm_t hash_alg,
4860 psa_key_derivation_step_t step,
4861 const uint8_t *data,
4862 size_t data_length )
4863{
4864 psa_status_t status;
4865 switch( step )
4866 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004867 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004868 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004870 status = psa_hmac_setup_internal( &hkdf->hmac,
4871 data, data_length,
4872 hash_alg );
4873 if( status != PSA_SUCCESS )
4874 return( status );
4875 hkdf->state = HKDF_STATE_STARTED;
4876 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004877 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878 /* If no salt was provided, use an empty salt. */
4879 if( hkdf->state == HKDF_STATE_INIT )
4880 {
4881 status = psa_hmac_setup_internal( &hkdf->hmac,
4882 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004883 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884 if( status != PSA_SUCCESS )
4885 return( status );
4886 hkdf->state = HKDF_STATE_STARTED;
4887 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004888 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004890 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4891 data, data_length );
4892 if( status != PSA_SUCCESS )
4893 return( status );
4894 status = psa_hmac_finish_internal( &hkdf->hmac,
4895 hkdf->prk,
4896 sizeof( hkdf->prk ) );
4897 if( status != PSA_SUCCESS )
4898 return( status );
4899 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4900 hkdf->block_number = 0;
4901 hkdf->state = HKDF_STATE_KEYED;
4902 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004903 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004904 if( hkdf->state == HKDF_STATE_OUTPUT )
4905 return( PSA_ERROR_BAD_STATE );
4906 if( hkdf->info_set )
4907 return( PSA_ERROR_BAD_STATE );
4908 hkdf->info_length = data_length;
4909 if( data_length != 0 )
4910 {
4911 hkdf->info = mbedtls_calloc( 1, data_length );
4912 if( hkdf->info == NULL )
4913 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4914 memcpy( hkdf->info, data, data_length );
4915 }
4916 hkdf->info_set = 1;
4917 return( PSA_SUCCESS );
4918 default:
4919 return( PSA_ERROR_INVALID_ARGUMENT );
4920 }
4921}
Janos Follathb03233e2019-06-11 15:30:30 +01004922
Janos Follathf08e2652019-06-13 09:05:41 +01004923static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4924 const uint8_t *data,
4925 size_t data_length )
4926{
4927 if( prf->state != TLS12_PRF_STATE_INIT )
4928 return( PSA_ERROR_BAD_STATE );
4929
Janos Follathd6dce9f2019-07-04 09:11:38 +01004930 if( data_length != 0 )
4931 {
4932 prf->seed = mbedtls_calloc( 1, data_length );
4933 if( prf->seed == NULL )
4934 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004935
Janos Follathd6dce9f2019-07-04 09:11:38 +01004936 memcpy( prf->seed, data, data_length );
4937 prf->seed_length = data_length;
4938 }
Janos Follathf08e2652019-06-13 09:05:41 +01004939
4940 prf->state = TLS12_PRF_STATE_SEED_SET;
4941
4942 return( PSA_SUCCESS );
4943}
4944
Janos Follath81550542019-06-13 14:26:34 +01004945static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4946 psa_algorithm_t hash_alg,
4947 const uint8_t *data,
4948 size_t data_length )
4949{
4950 psa_status_t status;
4951 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4952 return( PSA_ERROR_BAD_STATE );
4953
4954 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4955 if( status != PSA_SUCCESS )
4956 return( status );
4957
4958 prf->state = TLS12_PRF_STATE_KEY_SET;
4959
4960 return( PSA_SUCCESS );
4961}
4962
Janos Follath6660f0e2019-06-17 08:44:03 +01004963static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4964 psa_tls12_prf_key_derivation_t *prf,
4965 psa_algorithm_t hash_alg,
4966 const uint8_t *data,
4967 size_t data_length )
4968{
4969 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004970 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4971 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004972
4973 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4974 return( PSA_ERROR_INVALID_ARGUMENT );
4975
4976 /* Quoting RFC 4279, Section 2:
4977 *
4978 * The premaster secret is formed as follows: if the PSK is N octets
4979 * long, concatenate a uint16 with the value N, N zero octets, a second
4980 * uint16 with the value N, and the PSK itself.
4981 */
4982
Janos Follath40e13932019-06-26 13:22:29 +01004983 *cur++ = ( data_length >> 8 ) & 0xff;
4984 *cur++ = ( data_length >> 0 ) & 0xff;
4985 memset( cur, 0, data_length );
4986 cur += data_length;
4987 *cur++ = pms[0];
4988 *cur++ = pms[1];
4989 memcpy( cur, data, data_length );
4990 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004991
Janos Follath40e13932019-06-26 13:22:29 +01004992 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01004993
4994 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4995 return( status );
4996}
4997
Janos Follath63028dd2019-06-13 09:15:47 +01004998static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4999 const uint8_t *data,
5000 size_t data_length )
5001{
5002 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5003 return( PSA_ERROR_BAD_STATE );
5004
Janos Follathd6dce9f2019-07-04 09:11:38 +01005005 if( data_length != 0 )
5006 {
5007 prf->label = mbedtls_calloc( 1, data_length );
5008 if( prf->label == NULL )
5009 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005010
Janos Follathd6dce9f2019-07-04 09:11:38 +01005011 memcpy( prf->label, data, data_length );
5012 prf->label_length = data_length;
5013 }
Janos Follath63028dd2019-06-13 09:15:47 +01005014
5015 prf->state = TLS12_PRF_STATE_LABEL_SET;
5016
5017 return( PSA_SUCCESS );
5018}
5019
Janos Follathb03233e2019-06-11 15:30:30 +01005020static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5021 psa_algorithm_t hash_alg,
5022 psa_key_derivation_step_t step,
5023 const uint8_t *data,
5024 size_t data_length )
5025{
Janos Follathb03233e2019-06-11 15:30:30 +01005026 switch( step )
5027 {
Janos Follathf08e2652019-06-13 09:05:41 +01005028 case PSA_KEY_DERIVATION_INPUT_SEED:
5029 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005030 case PSA_KEY_DERIVATION_INPUT_SECRET:
5031 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005032 case PSA_KEY_DERIVATION_INPUT_LABEL:
5033 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005034 default:
5035 return( PSA_ERROR_INVALID_ARGUMENT );
5036 }
5037}
Janos Follath6660f0e2019-06-17 08:44:03 +01005038
5039static psa_status_t psa_tls12_prf_psk_to_ms_input(
5040 psa_tls12_prf_key_derivation_t *prf,
5041 psa_algorithm_t hash_alg,
5042 psa_key_derivation_step_t step,
5043 const uint8_t *data,
5044 size_t data_length )
5045{
5046 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005047 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005048 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5049 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005050 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005051
5052 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5053}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005054#endif /* MBEDTLS_MD_C */
5055
Gilles Peskineb8965192019-09-24 16:21:10 +02005056/** Check whether the given key type is acceptable for the given
5057 * input step of a key derivation.
5058 *
5059 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5060 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5061 * Both secret and non-secret inputs can alternatively have the type
5062 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5063 * that the input was passed as a buffer rather than via a key object.
5064 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005065static int psa_key_derivation_check_input_type(
5066 psa_key_derivation_step_t step,
5067 psa_key_type_t key_type )
5068{
5069 switch( step )
5070 {
5071 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005072 if( key_type == PSA_KEY_TYPE_DERIVE )
5073 return( PSA_SUCCESS );
5074 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005075 return( PSA_SUCCESS );
5076 break;
5077 case PSA_KEY_DERIVATION_INPUT_LABEL:
5078 case PSA_KEY_DERIVATION_INPUT_SALT:
5079 case PSA_KEY_DERIVATION_INPUT_INFO:
5080 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005081 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5082 return( PSA_SUCCESS );
5083 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005084 return( PSA_SUCCESS );
5085 break;
5086 }
5087 return( PSA_ERROR_INVALID_ARGUMENT );
5088}
5089
Janos Follathb80a94e2019-06-12 15:54:46 +01005090static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005092 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005093 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005094 const uint8_t *data,
5095 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005097 psa_status_t status;
5098 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5099
5100 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005101 if( status != PSA_SUCCESS )
5102 goto exit;
5103
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005104#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005105 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005106 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 step, data, data_length );
5110 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005111 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 {
Janos Follathb03233e2019-06-11 15:30:30 +01005113 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5114 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5115 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005116 }
5117 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5118 {
5119 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5120 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5121 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122 }
5123 else
5124#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005125 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005127 return( PSA_ERROR_BAD_STATE );
5128 }
5129
Gilles Peskine224b0d62019-09-23 18:13:17 +02005130exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005131 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133 return( status );
5134}
5135
Janos Follath51f4a0f2019-06-14 11:35:55 +01005136psa_status_t psa_key_derivation_input_bytes(
5137 psa_key_derivation_operation_t *operation,
5138 psa_key_derivation_step_t step,
5139 const uint8_t *data,
5140 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005141{
Gilles Peskineb8965192019-09-24 16:21:10 +02005142 return( psa_key_derivation_input_internal( operation, step,
5143 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005144 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005145}
5146
Janos Follath51f4a0f2019-06-14 11:35:55 +01005147psa_status_t psa_key_derivation_input_key(
5148 psa_key_derivation_operation_t *operation,
5149 psa_key_derivation_step_t step,
5150 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005151{
5152 psa_key_slot_t *slot;
5153 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005154
Gilles Peskine28f8f302019-07-24 13:30:31 +02005155 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005156 PSA_KEY_USAGE_DERIVE,
5157 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005159 {
5160 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005161 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005162 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005163
5164 /* Passing a key object as a SECRET input unlocks the permission
5165 * to output to a key object. */
5166 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5167 operation->can_output_key = 1;
5168
Janos Follathb80a94e2019-06-12 15:54:46 +01005169 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005170 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005171 slot->data.raw.data,
5172 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005173}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005174
5175
5176
5177/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005178/* Key agreement */
5179/****************************************************************/
5180
Gilles Peskinea05219c2018-11-16 16:02:56 +01005181#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005182static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5183 size_t peer_key_length,
5184 const mbedtls_ecp_keypair *our_key,
5185 uint8_t *shared_secret,
5186 size_t shared_secret_size,
5187 size_t *shared_secret_length )
5188{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005189 mbedtls_ecp_keypair *their_key = NULL;
5190 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005191 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005192 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005193
Jaeden Ameroccdce902019-01-10 11:42:27 +00005194 status = psa_import_ec_public_key(
5195 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5196 peer_key, peer_key_length,
5197 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005198 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005199 goto exit;
5200
Jaeden Amero97271b32019-01-10 19:38:51 +00005201 status = mbedtls_to_psa_error(
5202 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5203 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005204 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005205 status = mbedtls_to_psa_error(
5206 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5207 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005208 goto exit;
5209
Jaeden Amero97271b32019-01-10 19:38:51 +00005210 status = mbedtls_to_psa_error(
5211 mbedtls_ecdh_calc_secret( &ecdh,
5212 shared_secret_length,
5213 shared_secret, shared_secret_size,
5214 mbedtls_ctr_drbg_random,
5215 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005216
5217exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005218 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005219 mbedtls_ecp_keypair_free( their_key );
5220 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005221 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005222}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005223#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005224
Gilles Peskine01d718c2018-09-18 12:01:02 +02005225#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5226
Gilles Peskine0216fe12019-04-11 21:23:21 +02005227static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5228 psa_key_slot_t *private_key,
5229 const uint8_t *peer_key,
5230 size_t peer_key_length,
5231 uint8_t *shared_secret,
5232 size_t shared_secret_size,
5233 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005234{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005235 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005236 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005237#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005238 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005239 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005240 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005241 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5242 private_key->data.ecp,
5243 shared_secret, shared_secret_size,
5244 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005245#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005246 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005247 (void) private_key;
5248 (void) peer_key;
5249 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005250 (void) shared_secret;
5251 (void) shared_secret_size;
5252 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253 return( PSA_ERROR_NOT_SUPPORTED );
5254 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005255}
5256
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005257/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005258 * to potentially free embedded data structures and wipe confidential data.
5259 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005261 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005262 psa_key_slot_t *private_key,
5263 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005264 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265{
5266 psa_status_t status;
5267 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5268 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005270
5271 /* Step 1: run the secret agreement algorithm to generate the shared
5272 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005273 status = psa_key_agreement_raw_internal( ka_alg,
5274 private_key,
5275 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005276 shared_secret,
5277 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005278 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005279 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005280 goto exit;
5281
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005282 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005283 * the shared secret. A shared secret is permitted wherever a key
5284 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005285 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005286 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005287 shared_secret,
5288 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005289
Gilles Peskine12313cd2018-06-20 00:20:32 +02005290exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005291 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005292 return( status );
5293}
5294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005295psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005296 psa_key_derivation_step_t step,
5297 psa_key_handle_t private_key,
5298 const uint8_t *peer_key,
5299 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005300{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005301 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005302 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005304 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005305 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005306 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005307 if( status != PSA_SUCCESS )
5308 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005310 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005311 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005312 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005314 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005315}
5316
Gilles Peskinebe697d82019-05-16 18:00:41 +02005317psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5318 psa_key_handle_t private_key,
5319 const uint8_t *peer_key,
5320 size_t peer_key_length,
5321 uint8_t *output,
5322 size_t output_size,
5323 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005324{
5325 psa_key_slot_t *slot;
5326 psa_status_t status;
5327
5328 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5329 {
5330 status = PSA_ERROR_INVALID_ARGUMENT;
5331 goto exit;
5332 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005333 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005334 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005335 if( status != PSA_SUCCESS )
5336 goto exit;
5337
5338 status = psa_key_agreement_raw_internal( alg, slot,
5339 peer_key, peer_key_length,
5340 output, output_size,
5341 output_length );
5342
5343exit:
5344 if( status != PSA_SUCCESS )
5345 {
5346 /* If an error happens and is not handled properly, the output
5347 * may be used as a key to protect sensitive data. Arrange for such
5348 * a key to be random, which is likely to result in decryption or
5349 * verification errors. This is better than filling the buffer with
5350 * some constant data such as zeros, which would result in the data
5351 * being protected with a reproducible, easily knowable key.
5352 */
5353 psa_generate_random( output, output_size );
5354 *output_length = output_size;
5355 }
5356 return( status );
5357}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005358
5359
5360/****************************************************************/
5361/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005362/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005363
Gilles Peskine4c317f42018-07-12 01:24:09 +02005364psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005365 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005366{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005367 int ret;
5368 GUARD_MODULE_INITIALIZED;
5369
Gilles Peskinef181eca2019-08-07 13:49:00 +02005370 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5371 {
5372 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5373 output,
5374 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5375 if( ret != 0 )
5376 return( mbedtls_to_psa_error( ret ) );
5377 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5378 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5379 }
5380
Gilles Peskinee59236f2018-01-27 23:32:46 +01005381 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5382 return( mbedtls_to_psa_error( ret ) );
5383}
5384
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005385#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5386#include "mbedtls/entropy_poll.h"
5387
Gilles Peskine7228da22019-07-15 11:06:15 +02005388psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005389 size_t seed_size )
5390{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005391 if( global_data.initialized )
5392 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005393
5394 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5395 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5396 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5397 return( PSA_ERROR_INVALID_ARGUMENT );
5398
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005399 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005400}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005401#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005402
Gilles Peskinee56e8782019-04-26 17:34:02 +02005403#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5404static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5405 size_t domain_parameters_size,
5406 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005407{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005408 size_t i;
5409 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005410
Gilles Peskinee56e8782019-04-26 17:34:02 +02005411 if( domain_parameters_size == 0 )
5412 {
5413 *exponent = 65537;
5414 return( PSA_SUCCESS );
5415 }
5416
5417 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5418 * support values that fit in a 32-bit integer, which is larger than
5419 * int on just about every platform anyway. */
5420 if( domain_parameters_size > sizeof( acc ) )
5421 return( PSA_ERROR_NOT_SUPPORTED );
5422 for( i = 0; i < domain_parameters_size; i++ )
5423 acc = ( acc << 8 ) | domain_parameters[i];
5424 if( acc > INT_MAX )
5425 return( PSA_ERROR_NOT_SUPPORTED );
5426 *exponent = acc;
5427 return( PSA_SUCCESS );
5428}
5429#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5430
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005431static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005432 psa_key_slot_t *slot, size_t bits,
5433 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005434{
Gilles Peskine8e338702019-07-30 20:06:31 +02005435 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005436
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005438 return( PSA_ERROR_INVALID_ARGUMENT );
5439
Gilles Peskinee59236f2018-01-27 23:32:46 +01005440 if( key_type_is_raw_bytes( type ) )
5441 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005442 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005443 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5444 if( status != PSA_SUCCESS )
5445 return( status );
5446 status = psa_generate_random( slot->data.raw.data,
5447 slot->data.raw.bytes );
5448 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005450#if defined(MBEDTLS_DES_C)
5451 if( type == PSA_KEY_TYPE_DES )
5452 psa_des_set_key_parity( slot->data.raw.data,
5453 slot->data.raw.bytes );
5454#endif /* MBEDTLS_DES_C */
5455 }
5456 else
5457
5458#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005459 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005460 {
5461 mbedtls_rsa_context *rsa;
5462 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005463 int exponent;
5464 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5466 return( PSA_ERROR_NOT_SUPPORTED );
5467 /* Accept only byte-aligned keys, for the same reasons as
5468 * in psa_import_rsa_key(). */
5469 if( bits % 8 != 0 )
5470 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005471 status = psa_read_rsa_exponent( domain_parameters,
5472 domain_parameters_size,
5473 &exponent );
5474 if( status != PSA_SUCCESS )
5475 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005476 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5477 if( rsa == NULL )
5478 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5479 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5480 ret = mbedtls_rsa_gen_key( rsa,
5481 mbedtls_ctr_drbg_random,
5482 &global_data.ctr_drbg,
5483 (unsigned int) bits,
5484 exponent );
5485 if( ret != 0 )
5486 {
5487 mbedtls_rsa_free( rsa );
5488 mbedtls_free( rsa );
5489 return( mbedtls_to_psa_error( ret ) );
5490 }
5491 slot->data.rsa = rsa;
5492 }
5493 else
5494#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5495
5496#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005497 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005498 {
5499 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5500 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5501 const mbedtls_ecp_curve_info *curve_info =
5502 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5503 mbedtls_ecp_keypair *ecp;
5504 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005505 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506 return( PSA_ERROR_NOT_SUPPORTED );
5507 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5508 return( PSA_ERROR_NOT_SUPPORTED );
5509 if( curve_info->bit_size != bits )
5510 return( PSA_ERROR_INVALID_ARGUMENT );
5511 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5512 if( ecp == NULL )
5513 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5514 mbedtls_ecp_keypair_init( ecp );
5515 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5516 mbedtls_ctr_drbg_random,
5517 &global_data.ctr_drbg );
5518 if( ret != 0 )
5519 {
5520 mbedtls_ecp_keypair_free( ecp );
5521 mbedtls_free( ecp );
5522 return( mbedtls_to_psa_error( ret ) );
5523 }
5524 slot->data.ecp = ecp;
5525 }
5526 else
5527#endif /* MBEDTLS_ECP_C */
5528
5529 return( PSA_ERROR_NOT_SUPPORTED );
5530
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005531 return( PSA_SUCCESS );
5532}
Darryl Green0c6575a2018-11-07 16:05:30 +00005533
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005534psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005535 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005536{
5537 psa_status_t status;
5538 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005539 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005540
Gilles Peskine0f84d622019-09-12 19:03:13 +02005541 /* Reject any attempt to create a zero-length key so that we don't
5542 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5543 if( psa_get_key_bits( attributes ) == 0 )
5544 return( PSA_ERROR_INVALID_ARGUMENT );
5545
Gilles Peskinedf179142019-07-15 22:02:14 +02005546 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5547 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005548 if( status != PSA_SUCCESS )
5549 goto exit;
5550
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005551#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5552 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005553 {
Gilles Peskine11792082019-08-06 18:36:36 +02005554 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5555 size_t pubkey_length = 0; /* We don't support this feature yet */
5556 if( drv->key_management == NULL ||
5557 drv->key_management->p_generate == NULL )
5558 {
5559 status = PSA_ERROR_NOT_SUPPORTED;
5560 goto exit;
5561 }
5562 status = drv->key_management->p_generate(
5563 psa_get_se_driver_context( driver ),
5564 slot->data.se.slot_number, attributes,
5565 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005566 }
Gilles Peskine11792082019-08-06 18:36:36 +02005567 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005568#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005569 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005570 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005571 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005572 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005573 }
Gilles Peskine11792082019-08-06 18:36:36 +02005574
5575exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005576 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005577 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005578 if( status != PSA_SUCCESS )
5579 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005580 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005581 *handle = 0;
5582 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005583 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005584}
5585
5586
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587
5588/****************************************************************/
5589/* Module setup */
5590/****************************************************************/
5591
Gilles Peskine5e769522018-11-20 21:59:56 +01005592psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5593 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5594 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5595{
5596 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5597 return( PSA_ERROR_BAD_STATE );
5598 global_data.entropy_init = entropy_init;
5599 global_data.entropy_free = entropy_free;
5600 return( PSA_SUCCESS );
5601}
5602
Gilles Peskinee59236f2018-01-27 23:32:46 +01005603void mbedtls_psa_crypto_free( void )
5604{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005605 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005606 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5607 {
5608 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005609 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005610 }
5611 /* Wipe all remaining data, including configuration.
5612 * In particular, this sets all state indicator to the value
5613 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005614 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005615#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005616 /* Unregister all secure element drivers, so that we restart from
5617 * a pristine state. */
5618 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005619#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005620}
5621
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005622#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5623/** Recover a transaction that was interrupted by a power failure.
5624 *
5625 * This function is called during initialization, before psa_crypto_init()
5626 * returns. If this function returns a failure status, the initialization
5627 * fails.
5628 */
5629static psa_status_t psa_crypto_recover_transaction(
5630 const psa_crypto_transaction_t *transaction )
5631{
5632 switch( transaction->unknown.type )
5633 {
5634 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5635 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005636 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005637 * is implemented.
5638 * https://github.com/ARMmbed/mbed-crypto/issues/218
5639 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005640 default:
5641 /* We found an unsupported transaction in the storage.
5642 * We don't know what state the storage is in. Give up. */
5643 return( PSA_ERROR_STORAGE_FAILURE );
5644 }
5645}
5646#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5647
Gilles Peskinee59236f2018-01-27 23:32:46 +01005648psa_status_t psa_crypto_init( void )
5649{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005650 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005651 const unsigned char drbg_seed[] = "PSA";
5652
Gilles Peskinec6b69072018-11-20 21:42:52 +01005653 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005654 if( global_data.initialized != 0 )
5655 return( PSA_SUCCESS );
5656
Gilles Peskine5e769522018-11-20 21:59:56 +01005657 /* Set default configuration if
5658 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5659 if( global_data.entropy_init == NULL )
5660 global_data.entropy_init = mbedtls_entropy_init;
5661 if( global_data.entropy_free == NULL )
5662 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005663
Gilles Peskinec6b69072018-11-20 21:42:52 +01005664 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005665 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005666#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5667 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5668 /* The PSA entropy injection feature depends on using NV seed as an entropy
5669 * source. Add NV seed as an entropy source for PSA entropy injection. */
5670 mbedtls_entropy_add_source( &global_data.entropy,
5671 mbedtls_nv_seed_poll, NULL,
5672 MBEDTLS_ENTROPY_BLOCK_SIZE,
5673 MBEDTLS_ENTROPY_SOURCE_STRONG );
5674#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005676 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005677 status = mbedtls_to_psa_error(
5678 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5679 mbedtls_entropy_func,
5680 &global_data.entropy,
5681 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5682 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005683 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005684 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005685
Gilles Peskine66fb1262018-12-10 16:29:04 +01005686 status = psa_initialize_key_slots( );
5687 if( status != PSA_SUCCESS )
5688 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005689
Gilles Peskine4b734222019-07-24 15:56:31 +02005690#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005691 status = psa_crypto_load_transaction( );
5692 if( status == PSA_SUCCESS )
5693 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005694 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5695 if( status != PSA_SUCCESS )
5696 goto exit;
5697 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005698 }
5699 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5700 {
5701 /* There's no transaction to complete. It's all good. */
5702 status = PSA_SUCCESS;
5703 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005704#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005705
Gilles Peskinec6b69072018-11-20 21:42:52 +01005706 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005707 global_data.initialized = 1;
5708
5709exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005710 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005711 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005712 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005713}
5714
5715#endif /* MBEDTLS_PSA_CRYPTO_C */