blob: f59a68d92ba016eaf0d401330abc2c774067bce7 [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
Michael Thomas863b5d62020-02-10 19:41:16 +0000131psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132{
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
Michael Thomas863b5d62020-02-10 19:41:16 +0000410mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200411{
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:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Ameroccdce902019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
Michael Thomas863b5d62020-02-10 19:41:16 +0000597psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
Jaeden Ameroccdce902019-01-10 11:42:27 +0000598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 }
778 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100779#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Darryl Green940d72c2018-07-13 13:18:51 +0100792
Gilles Peskineb46bef22019-07-30 21:32:04 +0200793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100800 return( status );
801}
802
Gilles Peskinef603c712019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskine549ea862019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
815 * if one is a wildcard. If so the other has the specific algorithm. */
816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskined6f371b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskine549ea862019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
884 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Michael Thomas863b5d62020-02-10 19:41:16 +0000956 #if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
957 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
958 {
959 psa_remove_key_data_from_memory_vendor(slot);
960 }
961 else
962#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
963
Gilles Peskine73167e12019-07-12 23:44:37 +0200964#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
965 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000966 {
967 /* No key material to clean. */
968 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200969 else
970#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200971 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000972 {
973 /* No key material to clean. */
974 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200975 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000976 {
977 mbedtls_free( slot->data.raw.data );
978 }
979 else
980#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_rsa_free( slot->data.rsa );
984 mbedtls_free( slot->data.rsa );
985 }
986 else
987#endif /* defined(MBEDTLS_RSA_C) */
988#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200989 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000990 {
991 mbedtls_ecp_keypair_free( slot->data.ecp );
992 mbedtls_free( slot->data.ecp );
993 }
994 else
995#endif /* defined(MBEDTLS_ECP_C) */
996 {
997 /* Shouldn't happen: the key type is not any type that we
998 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200999 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001000 }
1001
1002 return( PSA_SUCCESS );
1003}
1004
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001005/** Completely wipe a slot in memory, including its policy.
1006 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001007psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001008{
1009 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001010 /* Multipart operations may still be using the key. This is safe
1011 * because all multipart operation objects are independent from
1012 * the key slot: if they need to access the key after the setup
1013 * phase, they have a copy of the key. Note that this means that
1014 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001015 /* At this point, key material and other type-specific content has
1016 * been wiped. Clear remaining metadata. We can call memset and not
1017 * zeroize because the metadata is not particularly sensitive. */
1018 memset( slot, 0, sizeof( *slot ) );
1019 return( status );
1020}
1021
Gilles Peskinec5487a82018-12-03 18:08:14 +01001022psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001024 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001025 psa_status_t status; /* status of the last operation */
1026 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1028 psa_se_drv_table_entry_t *driver;
1029#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030
Gilles Peskinec5487a82018-12-03 18:08:14 +01001031 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 if( status != PSA_SUCCESS )
1033 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001034
1035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001036 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001037 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001039 /* For a key in a secure element, we need to do three things:
1040 * remove the key file in internal storage, destroy the
1041 * key inside the secure element, and update the driver's
1042 * persistent data. Start a transaction that will encompass these
1043 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001045 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001047 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001048 status = psa_crypto_save_transaction( );
1049 if( status != PSA_SUCCESS )
1050 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001051 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 /* We should still try to destroy the key in the secure
1053 * element and the key metadata in storage. This is especially
1054 * important if the error is that the storage is full.
1055 * But how to do it exactly without risking an inconsistent
1056 * state after a reset?
1057 * https://github.com/ARMmbed/mbed-crypto/issues/215
1058 */
1059 overall_status = status;
1060 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001061 }
1062
Gilles Peskine354f7672019-07-12 23:46:38 +02001063 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 if( overall_status == PSA_SUCCESS )
1065 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001066 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001067#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1068
Darryl Greend49a4992018-06-18 17:27:26 +01001069#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001070 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001071 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 status = psa_destroy_persistent_key( slot->attr.id );
1073 if( overall_status == PSA_SUCCESS )
1074 overall_status = status;
1075
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001076 /* TODO: other slots may have a copy of the same key. We should
1077 * invalidate them.
1078 * https://github.com/ARMmbed/mbed-crypto/issues/214
1079 */
Darryl Greend49a4992018-06-18 17:27:26 +01001080 }
1081#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001082
Gilles Peskinefc762652019-07-22 19:30:34 +02001083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1084 if( driver != NULL )
1085 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001086 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087 if( overall_status == PSA_SUCCESS )
1088 overall_status = status;
1089 status = psa_crypto_stop_transaction( );
1090 if( overall_status == PSA_SUCCESS )
1091 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001092 }
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1094
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1096exit:
1097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001098 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001099 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1100 if( overall_status == PSA_SUCCESS )
1101 overall_status = status;
1102 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001103}
1104
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001105void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001106{
Gilles Peskineb699f072019-04-26 16:06:02 +02001107 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001108 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001109}
1110
Gilles Peskineb699f072019-04-26 16:06:02 +02001111psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1112 psa_key_type_t type,
1113 const uint8_t *data,
1114 size_t data_length )
1115{
1116 uint8_t *copy = NULL;
1117
1118 if( data_length != 0 )
1119 {
1120 copy = mbedtls_calloc( 1, data_length );
1121 if( copy == NULL )
1122 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1123 memcpy( copy, data, data_length );
1124 }
1125 /* After this point, this function is guaranteed to succeed, so it
1126 * can start modifying `*attributes`. */
1127
1128 if( attributes->domain_parameters != NULL )
1129 {
1130 mbedtls_free( attributes->domain_parameters );
1131 attributes->domain_parameters = NULL;
1132 attributes->domain_parameters_size = 0;
1133 }
1134
1135 attributes->domain_parameters = copy;
1136 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001137 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001138 return( PSA_SUCCESS );
1139}
1140
1141psa_status_t psa_get_key_domain_parameters(
1142 const psa_key_attributes_t *attributes,
1143 uint8_t *data, size_t data_size, size_t *data_length )
1144{
1145 if( attributes->domain_parameters_size > data_size )
1146 return( PSA_ERROR_BUFFER_TOO_SMALL );
1147 *data_length = attributes->domain_parameters_size;
1148 if( attributes->domain_parameters_size != 0 )
1149 memcpy( data, attributes->domain_parameters,
1150 attributes->domain_parameters_size );
1151 return( PSA_SUCCESS );
1152}
1153
1154#if defined(MBEDTLS_RSA_C)
1155static psa_status_t psa_get_rsa_public_exponent(
1156 const mbedtls_rsa_context *rsa,
1157 psa_key_attributes_t *attributes )
1158{
1159 mbedtls_mpi mpi;
1160 int ret;
1161 uint8_t *buffer = NULL;
1162 size_t buflen;
1163 mbedtls_mpi_init( &mpi );
1164
1165 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1166 if( ret != 0 )
1167 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001168 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1169 {
1170 /* It's the default value, which is reported as an empty string,
1171 * so there's nothing to do. */
1172 goto exit;
1173 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001174
1175 buflen = mbedtls_mpi_size( &mpi );
1176 buffer = mbedtls_calloc( 1, buflen );
1177 if( buffer == NULL )
1178 {
1179 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1180 goto exit;
1181 }
1182 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1183 if( ret != 0 )
1184 goto exit;
1185 attributes->domain_parameters = buffer;
1186 attributes->domain_parameters_size = buflen;
1187
1188exit:
1189 mbedtls_mpi_free( &mpi );
1190 if( ret != 0 )
1191 mbedtls_free( buffer );
1192 return( mbedtls_to_psa_error( ret ) );
1193}
1194#endif /* MBEDTLS_RSA_C */
1195
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001196/** Retrieve all the publicly-accessible attributes of a key.
1197 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001198psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1199 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001201 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001202 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204 psa_reset_key_attributes( attributes );
1205
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001206 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001207 if( status != PSA_SUCCESS )
1208 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001209
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001210 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001211 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1212 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1213
1214#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1215 if( psa_key_slot_is_external( slot ) )
1216 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1217#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001218
Gilles Peskine8e338702019-07-30 20:06:31 +02001219 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 {
1221#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001222 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001225 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001226 * is not yet implemented.
1227 * https://github.com/ARMmbed/mbed-crypto/issues/216
1228 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001229 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001230 break;
1231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001232 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1233 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001234#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 default:
1236 /* Nothing else to do. */
1237 break;
1238 }
1239
1240 if( status != PSA_SUCCESS )
1241 psa_reset_key_attributes( attributes );
1242 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001243}
1244
Gilles Peskinec8000c02019-08-02 20:15:51 +02001245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1246psa_status_t psa_get_key_slot_number(
1247 const psa_key_attributes_t *attributes,
1248 psa_key_slot_number_t *slot_number )
1249{
1250 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1251 {
1252 *slot_number = attributes->slot_number;
1253 return( PSA_SUCCESS );
1254 }
1255 else
1256 return( PSA_ERROR_INVALID_ARGUMENT );
1257}
1258#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1259
Jaeden Ameroccdce902019-01-10 11:42:27 +00001260#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001261static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1262 unsigned char *buf, size_t size )
1263{
1264 int ret;
1265 unsigned char *c;
1266 size_t len = 0;
1267
1268 c = buf + size;
1269
1270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1271
1272 return( (int) len );
1273}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001274#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001275
Gilles Peskinef603c712019-01-19 13:40:11 +01001276static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1277 uint8_t *data,
1278 size_t data_size,
1279 size_t *data_length,
1280 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281{
Gilles Peskine5d309672019-07-12 23:47:28 +02001282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1283 const psa_drv_se_t *drv;
1284 psa_drv_se_context_t *drv_context;
1285#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1286
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001287 *data_length = 0;
1288
Gilles Peskine8e338702019-07-30 20:06:31 +02001289 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001290 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001291
Gilles Peskine5d309672019-07-12 23:47:28 +02001292#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001293 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001294 {
1295 psa_drv_se_export_key_t method;
1296 if( drv->key_management == NULL )
1297 return( PSA_ERROR_NOT_SUPPORTED );
1298 method = ( export_public_key ?
1299 drv->key_management->p_export_public :
1300 drv->key_management->p_export );
1301 if( method == NULL )
1302 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001303 return( method( drv_context,
1304 slot->data.se.slot_number,
1305 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001306 }
1307#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1308
Gilles Peskine8e338702019-07-30 20:06:31 +02001309 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310 {
1311 if( slot->data.raw.bytes > data_size )
1312 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001313 if( data_size != 0 )
1314 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001315 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001316 memset( data + slot->data.raw.bytes, 0,
1317 data_size - slot->data.raw.bytes );
1318 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319 *data_length = slot->data.raw.bytes;
1320 return( PSA_SUCCESS );
1321 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001322#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001323 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001324 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001325 psa_status_t status;
1326
Gilles Peskineb46bef22019-07-30 21:32:04 +02001327 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001328 if( bytes > data_size )
1329 return( PSA_ERROR_BUFFER_TOO_SMALL );
1330 status = mbedtls_to_psa_error(
1331 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1332 if( status != PSA_SUCCESS )
1333 return( status );
1334 memset( data + bytes, 0, data_size - bytes );
1335 *data_length = bytes;
1336 return( PSA_SUCCESS );
1337 }
1338#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339 else
Moran Peker17e36e12018-05-02 12:55:20 +03001340 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001341#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001342 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1343 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001344 {
Moran Pekera998bc62018-04-16 18:16:20 +03001345 mbedtls_pk_context pk;
1346 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001347 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001348 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001349#if defined(MBEDTLS_RSA_C)
1350 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001351 pk.pk_info = &mbedtls_rsa_info;
1352 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001353#else
1354 return( PSA_ERROR_NOT_SUPPORTED );
1355#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001356 }
1357 else
1358 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001359#if defined(MBEDTLS_ECP_C)
1360 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001361 pk.pk_info = &mbedtls_eckey_info;
1362 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001363#else
1364 return( PSA_ERROR_NOT_SUPPORTED );
1365#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001366 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001367 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001368 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001369 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 }
Moran Peker17e36e12018-05-02 12:55:20 +03001371 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 {
Moran Peker17e36e12018-05-02 12:55:20 +03001373 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 }
Moran Peker60364322018-04-29 11:34:58 +03001375 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001376 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001377 /* If data_size is 0 then data may be NULL and then the
1378 * call to memset would have undefined behavior. */
1379 if( data_size != 0 )
1380 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001381 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001382 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001383 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1384 * Move the data to the beginning and erase remaining data
1385 * at the original location. */
1386 if( 2 * (size_t) ret <= data_size )
1387 {
1388 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001389 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001390 }
1391 else if( (size_t) ret < data_size )
1392 {
1393 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001394 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001395 }
Moran Pekera998bc62018-04-16 18:16:20 +03001396 *data_length = ret;
1397 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001398 }
1399 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001400#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001401 {
1402 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001403 it is valid for a special-purpose implementation to omit
1404 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001405 return( PSA_ERROR_NOT_SUPPORTED );
1406 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407 }
1408}
1409
Gilles Peskinec5487a82018-12-03 18:08:14 +01001410psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001411 uint8_t *data,
1412 size_t data_size,
1413 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001414{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001415 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001416 psa_status_t status;
1417
1418 /* Set the key to empty now, so that even when there are errors, we always
1419 * set data_length to a value between 0 and data_size. On error, setting
1420 * the key to empty is a good choice because an empty key representation is
1421 * unlikely to be accepted anywhere. */
1422 *data_length = 0;
1423
1424 /* Export requires the EXPORT flag. There is an exception for public keys,
1425 * which don't require any flag, but psa_get_key_from_slot takes
1426 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001427 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 if( status != PSA_SUCCESS )
1429 return( status );
1430 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001431 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001433
Gilles Peskinec5487a82018-12-03 18:08:14 +01001434psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001435 uint8_t *data,
1436 size_t data_size,
1437 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001438{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001439 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001440 psa_status_t status;
1441
1442 /* Set the key to empty now, so that even when there are errors, we always
1443 * set data_length to a value between 0 and data_size. On error, setting
1444 * the key to empty is a good choice because an empty key representation is
1445 * unlikely to be accepted anywhere. */
1446 *data_length = 0;
1447
1448 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001449 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001450 if( status != PSA_SUCCESS )
1451 return( status );
1452 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001453 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001454}
1455
Gilles Peskine91e8c332019-08-02 19:19:39 +02001456#if defined(static_assert)
1457static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1458 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001459static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001460 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001461static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001462 "One or more key attribute flag is listed as both internal-only and external-only" );
1463#endif
1464
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001465/** Validate that a key policy is internally well-formed.
1466 *
1467 * This function only rejects invalid policies. It does not validate the
1468 * consistency of the policy with respect to other attributes of the key
1469 * such as the key type.
1470 */
1471static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001472{
1473 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001474 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 PSA_KEY_USAGE_ENCRYPT |
1476 PSA_KEY_USAGE_DECRYPT |
1477 PSA_KEY_USAGE_SIGN |
1478 PSA_KEY_USAGE_VERIFY |
1479 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1480 return( PSA_ERROR_INVALID_ARGUMENT );
1481
Gilles Peskine4747d192019-04-17 15:05:45 +02001482 return( PSA_SUCCESS );
1483}
1484
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485/** Validate the internal consistency of key attributes.
1486 *
1487 * This function only rejects invalid attribute values. If does not
1488 * validate the consistency of the attributes with any key data that may
1489 * be involved in the creation of the key.
1490 *
1491 * Call this function early in the key creation process.
1492 *
1493 * \param[in] attributes Key attributes for the new key.
1494 * \param[out] p_drv On any return, the driver for the key, if any.
1495 * NULL for a transparent key.
1496 *
1497 */
1498static psa_status_t psa_validate_key_attributes(
1499 const psa_key_attributes_t *attributes,
1500 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001501{
1502 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503
Michael Thomas153cd2f2020-01-22 16:54:38 +00001504 if( PSA_KEY_LIFETIME_IS_PERSISTENT (attributes->core.lifetime))
Darryl Green0c6575a2018-11-07 16:05:30 +00001505 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506 status = psa_validate_persistent_key_parameters(
1507 attributes->core.lifetime, attributes->core.id,
1508 p_drv, 1 );
1509 if( status != PSA_SUCCESS )
1510 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001511 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512
1513 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001514 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001515 return( status );
1516
1517 /* Refuse to create overly large keys.
1518 * Note that this doesn't trigger on import if the attributes don't
1519 * explicitly specify a size (so psa_get_key_bits returns 0), so
1520 * psa_import_key() needs its own checks. */
1521 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1522 return( PSA_ERROR_NOT_SUPPORTED );
1523
Gilles Peskine91e8c332019-08-02 19:19:39 +02001524 /* Reject invalid flags. These should not be reachable through the API. */
1525 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1526 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1527 return( PSA_ERROR_INVALID_ARGUMENT );
1528
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529 return( PSA_SUCCESS );
1530}
1531
Gilles Peskine4747d192019-04-17 15:05:45 +02001532/** Prepare a key slot to receive key material.
1533 *
1534 * This function allocates a key slot and sets its metadata.
1535 *
1536 * If this function fails, call psa_fail_key_creation().
1537 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * This function is intended to be used as follows:
1539 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1540 * it with the specified attributes, and assign it a handle.
1541 * -# Populate the slot with the key material.
1542 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1543 * In case of failure at any step, stop the sequence and call
1544 * psa_fail_key_creation().
1545 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 * \param[in] attributes Key attributes for the new key.
1548 * \param[out] handle On success, a handle for the allocated slot.
1549 * \param[out] p_slot On success, a pointer to the prepared slot.
1550 * \param[out] p_drv On any return, the driver for the key, if any.
1551 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001552 *
1553 * \retval #PSA_SUCCESS
1554 * The key slot is ready to receive key material.
1555 * \return If this function fails, the key slot is an invalid state.
1556 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 */
1558static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001559 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 const psa_key_attributes_t *attributes,
1561 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001562 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001563 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001564{
1565 psa_status_t status;
1566 psa_key_slot_t *slot;
1567
Gilles Peskinedf179142019-07-15 22:02:14 +02001568 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001569 *p_drv = NULL;
1570
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571 status = psa_validate_key_attributes( attributes, p_drv );
1572 if( status != PSA_SUCCESS )
1573 return( status );
1574
Gilles Peskineedbed562019-08-07 18:19:59 +02001575 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001576 if( status != PSA_SUCCESS )
1577 return( status );
1578 slot = *p_slot;
1579
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001580 /* We're storing the declared bit-size of the key. It's up to each
1581 * creation mechanism to verify that this information is correct.
1582 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001583 * an input (generate, device) but not for those where the bit-size
1584 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585
1586 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001587
Gilles Peskine91e8c332019-08-02 19:19:39 +02001588 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001589 * external-only flags, query `attributes`. Thanks to the check
1590 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001591 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001592 * may have set. */
1593 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001594
Gilles Peskinecbaff462019-07-12 23:46:04 +02001595#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001596 /* For a key in a secure element, we need to do three things
1597 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001598 * create the key file in internal storage, create the
1599 * key inside the secure element, and update the driver's
1600 * persistent data. Start a transaction that will encompass these
1601 * three actions. */
1602 /* The first thing to do is to find a slot number for the new key.
1603 * We save the slot number in persistent storage as part of the
1604 * transaction data. It will be needed to recover if the power
1605 * fails during the key creation process, to clean up on the secure
1606 * element side after restarting. Obtaining a slot number from the
1607 * secure element driver updates its persistent state, but we do not yet
1608 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001609 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001610 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001612 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001613 &slot->data.se.slot_number );
1614 if( status != PSA_SUCCESS )
1615 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001616 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001617 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001618 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001619 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001620 status = psa_crypto_save_transaction( );
1621 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001622 {
1623 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001624 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001625 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001626 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001627#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1628
Darryl Green0c6575a2018-11-07 16:05:30 +00001629 return( status );
1630}
Gilles Peskine4747d192019-04-17 15:05:45 +02001631
1632/** Finalize the creation of a key once its key material has been set.
1633 *
1634 * This entails writing the key to persistent storage.
1635 *
1636 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001637 * See the documentation of psa_start_key_creation() for the intended use
1638 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 * \param[in,out] slot Pointer to the slot with key material.
1641 * \param[in] driver The secure element driver for the key,
1642 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001643 *
1644 * \retval #PSA_SUCCESS
1645 * The key was successfully created. The handle is now valid.
1646 * \return If this function fails, the key slot is an invalid state.
1647 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 */
Michael Thomas863b5d62020-02-10 19:41:16 +00001649psa_status_t psa_finish_key_creation(
Gilles Peskine011e4282019-06-26 18:34:38 +02001650 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001651 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001652{
1653 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001654 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001655 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001656
1657#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001658 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001659 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001660#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1661 if( driver != NULL )
1662 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001663 psa_se_key_data_storage_t data;
1664#if defined(static_assert)
1665 static_assert( sizeof( slot->data.se.slot_number ) ==
1666 sizeof( data.slot_number ),
1667 "Slot number size does not match psa_se_key_data_storage_t" );
1668 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1669 "Bit-size size does not match psa_se_key_data_storage_t" );
1670#endif
1671 memcpy( &data.slot_number, &slot->data.se.slot_number,
1672 sizeof( slot->data.se.slot_number ) );
1673 memcpy( &data.bits, &slot->attr.bits,
1674 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001676 (uint8_t*) &data,
1677 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001678 }
1679 else
1680#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1681 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001682 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001683 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001684 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001685 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1686 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001687 if( buffer == NULL && buffer_size != 0 )
1688 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1689 status = psa_internal_export_key( slot,
1690 buffer, buffer_size, &length,
1691 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001692 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001693 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001694 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001695
Gilles Peskine1df83d42019-07-23 16:13:14 +02001696 if( buffer_size != 0 )
1697 mbedtls_platform_zeroize( buffer, buffer_size );
1698 mbedtls_free( buffer );
1699 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001700 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001701#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1702
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001704 /* Finish the transaction for a key creation. This does not
1705 * happen when registering an existing key. Detect this case
1706 * by checking whether a transaction is in progress (actual
1707 * creation of a key in a secure element requires a transaction,
1708 * but registration doesn't use one). */
1709 if( driver != NULL &&
1710 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001711 {
1712 status = psa_save_se_persistent_data( driver );
1713 if( status != PSA_SUCCESS )
1714 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001715 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001716 return( status );
1717 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001718 status = psa_crypto_stop_transaction( );
1719 if( status != PSA_SUCCESS )
1720 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001721 }
1722#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1723
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 return( status );
1725}
1726
1727/** Abort the creation of a key.
1728 *
1729 * You may call this function after calling psa_start_key_creation(),
1730 * or after psa_finish_key_creation() fails. In other circumstances, this
1731 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 * See the documentation of psa_start_key_creation() for the intended use
1733 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 * \param[in,out] slot Pointer to the slot with key material.
1736 * \param[in] driver The secure element driver for the key,
1737 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001739static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001740 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001741{
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 (void) driver;
1743
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 if( slot == NULL )
1745 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001746
1747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001748 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 * element, and the failure happened later (when saving metadata
1750 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001751 * element.
1752 * https://github.com/ARMmbed/mbed-crypto/issues/217
1753 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001754
Gilles Peskined7729582019-08-05 15:55:54 +02001755 /* Abort the ongoing transaction if any (there may not be one if
1756 * the creation process failed before starting one, or if the
1757 * key creation is a registration of a key in a secure element).
1758 * Earlier functions must already have done what it takes to undo any
1759 * partial creation. All that's left is to update the transaction data
1760 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001761 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1763
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 psa_wipe_key_slot( slot );
1765}
1766
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001767/** Validate optional attributes during key creation.
1768 *
1769 * Some key attributes are optional during key creation. If they are
1770 * specified in the attributes structure, check that they are consistent
1771 * with the data in the slot.
1772 *
1773 * This function should be called near the end of key creation, after
1774 * the slot in memory is fully populated but before saving persistent data.
1775 */
1776static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001777 const psa_key_slot_t *slot,
1778 const psa_key_attributes_t *attributes )
1779{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001780 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001781 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001782 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 return( PSA_ERROR_INVALID_ARGUMENT );
1784 }
1785
1786 if( attributes->domain_parameters_size != 0 )
1787 {
1788#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001789 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001790 {
1791 mbedtls_mpi actual, required;
1792 int ret;
1793 mbedtls_mpi_init( &actual );
1794 mbedtls_mpi_init( &required );
1795 ret = mbedtls_rsa_export( slot->data.rsa,
1796 NULL, NULL, NULL, NULL, &actual );
1797 if( ret != 0 )
1798 goto rsa_exit;
1799 ret = mbedtls_mpi_read_binary( &required,
1800 attributes->domain_parameters,
1801 attributes->domain_parameters_size );
1802 if( ret != 0 )
1803 goto rsa_exit;
1804 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1805 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1806 rsa_exit:
1807 mbedtls_mpi_free( &actual );
1808 mbedtls_mpi_free( &required );
1809 if( ret != 0)
1810 return( mbedtls_to_psa_error( ret ) );
1811 }
1812 else
1813#endif
1814 {
1815 return( PSA_ERROR_INVALID_ARGUMENT );
1816 }
1817 }
1818
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001819 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001820 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001821 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001822 return( PSA_ERROR_INVALID_ARGUMENT );
1823 }
1824
1825 return( PSA_SUCCESS );
1826}
1827
Gilles Peskine4747d192019-04-17 15:05:45 +02001828psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001829 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001830 size_t data_length,
1831 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001832{
1833 psa_status_t status;
1834 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001835 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836
Gilles Peskinedf179142019-07-15 22:02:14 +02001837 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1838 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001839 if( status != PSA_SUCCESS )
1840 goto exit;
1841
Gilles Peskine5d309672019-07-12 23:47:28 +02001842#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1843 if( driver != NULL )
1844 {
1845 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001846 /* The driver should set the number of key bits, however in
1847 * case it doesn't, we initialize bits to an invalid value. */
1848 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001849 if( drv->key_management == NULL ||
1850 drv->key_management->p_import == NULL )
1851 {
1852 status = PSA_ERROR_NOT_SUPPORTED;
1853 goto exit;
1854 }
1855 status = drv->key_management->p_import(
1856 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001857 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001858 &bits );
1859 if( status != PSA_SUCCESS )
1860 goto exit;
1861 if( bits > PSA_MAX_KEY_BITS )
1862 {
1863 status = PSA_ERROR_NOT_SUPPORTED;
1864 goto exit;
1865 }
1866 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001867 }
1868 else
1869#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas863b5d62020-02-10 19:41:16 +00001870#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
1871 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
1872 {
1873 status = psa_import_key_into_slot_vendor( slot, data, data_length);
1874 goto exit;
1875 }
1876 else
1877#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskine5d309672019-07-12 23:47:28 +02001878 {
1879 status = psa_import_key_into_slot( slot, data, data_length );
1880 if( status != PSA_SUCCESS )
1881 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001882 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001883 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001884 if( status != PSA_SUCCESS )
1885 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886
Gilles Peskine011e4282019-06-26 18:34:38 +02001887 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 if( status != PSA_SUCCESS )
1890 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001891 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 *handle = 0;
1893 }
1894 return( status );
1895}
1896
Gilles Peskined7729582019-08-05 15:55:54 +02001897#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1898psa_status_t mbedtls_psa_register_se_key(
1899 const psa_key_attributes_t *attributes )
1900{
1901 psa_status_t status;
1902 psa_key_slot_t *slot = NULL;
1903 psa_se_drv_table_entry_t *driver = NULL;
1904 const psa_drv_se_t *drv;
1905 psa_key_handle_t handle = 0;
1906
1907 /* Leaving attributes unspecified is not currently supported.
1908 * It could make sense to query the key type and size from the
1909 * secure element, but not all secure elements support this
1910 * and the driver HAL doesn't currently support it. */
1911 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1912 return( PSA_ERROR_NOT_SUPPORTED );
1913 if( psa_get_key_bits( attributes ) == 0 )
1914 return( PSA_ERROR_NOT_SUPPORTED );
1915
1916 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1917 &handle, &slot, &driver );
1918 if( status != PSA_SUCCESS )
1919 goto exit;
1920
1921 if( driver == NULL )
1922 {
1923 status = PSA_ERROR_INVALID_ARGUMENT;
1924 goto exit;
1925 }
1926 drv = psa_get_se_driver_methods( driver );
1927
1928 if ( psa_get_key_slot_number( attributes,
1929 &slot->data.se.slot_number ) != PSA_SUCCESS )
1930 {
1931 /* The application didn't specify a slot number. This doesn't
1932 * make sense when registering a slot. */
1933 status = PSA_ERROR_INVALID_ARGUMENT;
1934 goto exit;
1935 }
1936
Gilles Peskinea5f87492019-08-05 16:46:18 +02001937 /* If the driver has a slot number validation method, call it.
1938 * If it doesn't, it means the secure element is unable to validate
1939 * anything and so we have to trust the application. */
1940 if( drv->key_management != NULL &&
1941 drv->key_management->p_validate_slot_number != NULL )
1942 {
1943 status = drv->key_management->p_validate_slot_number(
1944 psa_get_se_driver_context( driver ),
1945 attributes,
1946 PSA_KEY_CREATION_REGISTER,
1947 slot->data.se.slot_number );
1948 if( status != PSA_SUCCESS )
1949 goto exit;
1950 }
1951
Gilles Peskined7729582019-08-05 15:55:54 +02001952 status = psa_finish_key_creation( slot, driver );
1953
1954exit:
1955 if( status != PSA_SUCCESS )
1956 {
1957 psa_fail_key_creation( slot, driver );
1958 }
1959 /* Registration doesn't keep the key in RAM. */
1960 psa_close_key( handle );
1961 return( status );
1962}
1963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1964
Gilles Peskinef603c712019-01-19 13:40:11 +01001965static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001966 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001967{
1968 psa_status_t status;
1969 uint8_t *buffer = NULL;
1970 size_t buffer_size = 0;
1971 size_t length;
1972
Gilles Peskine8e338702019-07-30 20:06:31 +02001973 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001974 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001975 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001976 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001977 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001978 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1979 if( status != PSA_SUCCESS )
1980 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001981 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001983
1984exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001985 if( buffer_size != 0 )
1986 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001987 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001988 return( status );
1989}
1990
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001991psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1992 const psa_key_attributes_t *specified_attributes,
1993 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001994{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001996 psa_key_slot_t *source_slot = NULL;
1997 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001998 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001999 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002000
Gilles Peskine28f8f302019-07-24 13:30:31 +02002001 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002002 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002003 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004 goto exit;
2005
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002006 status = psa_validate_optional_attributes( source_slot,
2007 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 if( status != PSA_SUCCESS )
2009 goto exit;
2010
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002011 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002012 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013 if( status != PSA_SUCCESS )
2014 goto exit;
2015
Gilles Peskinedf179142019-07-15 22:02:14 +02002016 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2017 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002018 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002019 if( status != PSA_SUCCESS )
2020 goto exit;
2021
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002022#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2023 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002024 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002025 /* Copying to a secure element is not implemented yet. */
2026 status = PSA_ERROR_NOT_SUPPORTED;
2027 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002028 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002029#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002030
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002031 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002032 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002033 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002034
Gilles Peskine011e4282019-06-26 18:34:38 +02002035 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002036exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002037 if( status != PSA_SUCCESS )
2038 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002039 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002040 *target_handle = 0;
2041 }
2042 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002043}
2044
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002045
2046
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002047/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002048/* Message digests */
2049/****************************************************************/
2050
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002051static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002052{
2053 switch( alg )
2054 {
2055#if defined(MBEDTLS_MD2_C)
2056 case PSA_ALG_MD2:
2057 return( &mbedtls_md2_info );
2058#endif
2059#if defined(MBEDTLS_MD4_C)
2060 case PSA_ALG_MD4:
2061 return( &mbedtls_md4_info );
2062#endif
2063#if defined(MBEDTLS_MD5_C)
2064 case PSA_ALG_MD5:
2065 return( &mbedtls_md5_info );
2066#endif
2067#if defined(MBEDTLS_RIPEMD160_C)
2068 case PSA_ALG_RIPEMD160:
2069 return( &mbedtls_ripemd160_info );
2070#endif
2071#if defined(MBEDTLS_SHA1_C)
2072 case PSA_ALG_SHA_1:
2073 return( &mbedtls_sha1_info );
2074#endif
2075#if defined(MBEDTLS_SHA256_C)
2076 case PSA_ALG_SHA_224:
2077 return( &mbedtls_sha224_info );
2078 case PSA_ALG_SHA_256:
2079 return( &mbedtls_sha256_info );
2080#endif
2081#if defined(MBEDTLS_SHA512_C)
2082 case PSA_ALG_SHA_384:
2083 return( &mbedtls_sha384_info );
2084 case PSA_ALG_SHA_512:
2085 return( &mbedtls_sha512_info );
2086#endif
2087 default:
2088 return( NULL );
2089 }
2090}
2091
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002092psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2093{
2094 switch( operation->alg )
2095 {
Gilles Peskine81736312018-06-26 15:04:31 +02002096 case 0:
2097 /* The object has (apparently) been initialized but it is not
2098 * in use. It's ok to call abort on such an object, and there's
2099 * nothing to do. */
2100 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002101#if defined(MBEDTLS_MD2_C)
2102 case PSA_ALG_MD2:
2103 mbedtls_md2_free( &operation->ctx.md2 );
2104 break;
2105#endif
2106#if defined(MBEDTLS_MD4_C)
2107 case PSA_ALG_MD4:
2108 mbedtls_md4_free( &operation->ctx.md4 );
2109 break;
2110#endif
2111#if defined(MBEDTLS_MD5_C)
2112 case PSA_ALG_MD5:
2113 mbedtls_md5_free( &operation->ctx.md5 );
2114 break;
2115#endif
2116#if defined(MBEDTLS_RIPEMD160_C)
2117 case PSA_ALG_RIPEMD160:
2118 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2119 break;
2120#endif
2121#if defined(MBEDTLS_SHA1_C)
2122 case PSA_ALG_SHA_1:
2123 mbedtls_sha1_free( &operation->ctx.sha1 );
2124 break;
2125#endif
2126#if defined(MBEDTLS_SHA256_C)
2127 case PSA_ALG_SHA_224:
2128 case PSA_ALG_SHA_256:
2129 mbedtls_sha256_free( &operation->ctx.sha256 );
2130 break;
2131#endif
2132#if defined(MBEDTLS_SHA512_C)
2133 case PSA_ALG_SHA_384:
2134 case PSA_ALG_SHA_512:
2135 mbedtls_sha512_free( &operation->ctx.sha512 );
2136 break;
2137#endif
2138 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002139 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002140 }
2141 operation->alg = 0;
2142 return( PSA_SUCCESS );
2143}
2144
Gilles Peskineda8191d2018-07-08 19:46:38 +02002145psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002146 psa_algorithm_t alg )
2147{
2148 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002149
2150 /* A context must be freshly initialized before it can be set up. */
2151 if( operation->alg != 0 )
2152 {
2153 return( PSA_ERROR_BAD_STATE );
2154 }
2155
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002156 switch( alg )
2157 {
2158#if defined(MBEDTLS_MD2_C)
2159 case PSA_ALG_MD2:
2160 mbedtls_md2_init( &operation->ctx.md2 );
2161 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_MD4_C)
2165 case PSA_ALG_MD4:
2166 mbedtls_md4_init( &operation->ctx.md4 );
2167 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_MD5_C)
2171 case PSA_ALG_MD5:
2172 mbedtls_md5_init( &operation->ctx.md5 );
2173 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2174 break;
2175#endif
2176#if defined(MBEDTLS_RIPEMD160_C)
2177 case PSA_ALG_RIPEMD160:
2178 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2179 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2180 break;
2181#endif
2182#if defined(MBEDTLS_SHA1_C)
2183 case PSA_ALG_SHA_1:
2184 mbedtls_sha1_init( &operation->ctx.sha1 );
2185 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2186 break;
2187#endif
2188#if defined(MBEDTLS_SHA256_C)
2189 case PSA_ALG_SHA_224:
2190 mbedtls_sha256_init( &operation->ctx.sha256 );
2191 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2192 break;
2193 case PSA_ALG_SHA_256:
2194 mbedtls_sha256_init( &operation->ctx.sha256 );
2195 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2196 break;
2197#endif
2198#if defined(MBEDTLS_SHA512_C)
2199 case PSA_ALG_SHA_384:
2200 mbedtls_sha512_init( &operation->ctx.sha512 );
2201 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2202 break;
2203 case PSA_ALG_SHA_512:
2204 mbedtls_sha512_init( &operation->ctx.sha512 );
2205 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2206 break;
2207#endif
2208 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002209 return( PSA_ALG_IS_HASH( alg ) ?
2210 PSA_ERROR_NOT_SUPPORTED :
2211 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212 }
2213 if( ret == 0 )
2214 operation->alg = alg;
2215 else
2216 psa_hash_abort( operation );
2217 return( mbedtls_to_psa_error( ret ) );
2218}
2219
2220psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2221 const uint8_t *input,
2222 size_t input_length )
2223{
2224 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002225
2226 /* Don't require hash implementations to behave correctly on a
2227 * zero-length input, which may have an invalid pointer. */
2228 if( input_length == 0 )
2229 return( PSA_SUCCESS );
2230
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231 switch( operation->alg )
2232 {
2233#if defined(MBEDTLS_MD2_C)
2234 case PSA_ALG_MD2:
2235 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2236 input, input_length );
2237 break;
2238#endif
2239#if defined(MBEDTLS_MD4_C)
2240 case PSA_ALG_MD4:
2241 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2242 input, input_length );
2243 break;
2244#endif
2245#if defined(MBEDTLS_MD5_C)
2246 case PSA_ALG_MD5:
2247 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2248 input, input_length );
2249 break;
2250#endif
2251#if defined(MBEDTLS_RIPEMD160_C)
2252 case PSA_ALG_RIPEMD160:
2253 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2254 input, input_length );
2255 break;
2256#endif
2257#if defined(MBEDTLS_SHA1_C)
2258 case PSA_ALG_SHA_1:
2259 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2260 input, input_length );
2261 break;
2262#endif
2263#if defined(MBEDTLS_SHA256_C)
2264 case PSA_ALG_SHA_224:
2265 case PSA_ALG_SHA_256:
2266 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2267 input, input_length );
2268 break;
2269#endif
2270#if defined(MBEDTLS_SHA512_C)
2271 case PSA_ALG_SHA_384:
2272 case PSA_ALG_SHA_512:
2273 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2274 input, input_length );
2275 break;
2276#endif
2277 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002278 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002280
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281 if( ret != 0 )
2282 psa_hash_abort( operation );
2283 return( mbedtls_to_psa_error( ret ) );
2284}
2285
2286psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2287 uint8_t *hash,
2288 size_t hash_size,
2289 size_t *hash_length )
2290{
itayzafrir40835d42018-08-02 13:14:17 +03002291 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002293 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294
2295 /* Fill the output buffer with something that isn't a valid hash
2296 * (barring an attack on the hash and deliberately-crafted input),
2297 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002298 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002299 /* If hash_size is 0 then hash may be NULL and then the
2300 * call to memset would have undefined behavior. */
2301 if( hash_size != 0 )
2302 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303
2304 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002305 {
2306 status = PSA_ERROR_BUFFER_TOO_SMALL;
2307 goto exit;
2308 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309
2310 switch( operation->alg )
2311 {
2312#if defined(MBEDTLS_MD2_C)
2313 case PSA_ALG_MD2:
2314 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2315 break;
2316#endif
2317#if defined(MBEDTLS_MD4_C)
2318 case PSA_ALG_MD4:
2319 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2320 break;
2321#endif
2322#if defined(MBEDTLS_MD5_C)
2323 case PSA_ALG_MD5:
2324 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2325 break;
2326#endif
2327#if defined(MBEDTLS_RIPEMD160_C)
2328 case PSA_ALG_RIPEMD160:
2329 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2330 break;
2331#endif
2332#if defined(MBEDTLS_SHA1_C)
2333 case PSA_ALG_SHA_1:
2334 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2335 break;
2336#endif
2337#if defined(MBEDTLS_SHA256_C)
2338 case PSA_ALG_SHA_224:
2339 case PSA_ALG_SHA_256:
2340 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2341 break;
2342#endif
2343#if defined(MBEDTLS_SHA512_C)
2344 case PSA_ALG_SHA_384:
2345 case PSA_ALG_SHA_512:
2346 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2347 break;
2348#endif
2349 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002350 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 }
itayzafrir40835d42018-08-02 13:14:17 +03002352 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353
itayzafrir40835d42018-08-02 13:14:17 +03002354exit:
2355 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002357 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 return( psa_hash_abort( operation ) );
2359 }
2360 else
2361 {
2362 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002363 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364 }
2365}
2366
Gilles Peskine2d277862018-06-18 15:41:12 +02002367psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2368 const uint8_t *hash,
2369 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370{
2371 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2372 size_t actual_hash_length;
2373 psa_status_t status = psa_hash_finish( operation,
2374 actual_hash, sizeof( actual_hash ),
2375 &actual_hash_length );
2376 if( status != PSA_SUCCESS )
2377 return( status );
2378 if( actual_hash_length != hash_length )
2379 return( PSA_ERROR_INVALID_SIGNATURE );
2380 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2381 return( PSA_ERROR_INVALID_SIGNATURE );
2382 return( PSA_SUCCESS );
2383}
2384
Gilles Peskineeb35d782019-01-22 17:56:16 +01002385psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2386 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002387{
2388 if( target_operation->alg != 0 )
2389 return( PSA_ERROR_BAD_STATE );
2390
2391 switch( source_operation->alg )
2392 {
2393 case 0:
2394 return( PSA_ERROR_BAD_STATE );
2395#if defined(MBEDTLS_MD2_C)
2396 case PSA_ALG_MD2:
2397 mbedtls_md2_clone( &target_operation->ctx.md2,
2398 &source_operation->ctx.md2 );
2399 break;
2400#endif
2401#if defined(MBEDTLS_MD4_C)
2402 case PSA_ALG_MD4:
2403 mbedtls_md4_clone( &target_operation->ctx.md4,
2404 &source_operation->ctx.md4 );
2405 break;
2406#endif
2407#if defined(MBEDTLS_MD5_C)
2408 case PSA_ALG_MD5:
2409 mbedtls_md5_clone( &target_operation->ctx.md5,
2410 &source_operation->ctx.md5 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_RIPEMD160_C)
2414 case PSA_ALG_RIPEMD160:
2415 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2416 &source_operation->ctx.ripemd160 );
2417 break;
2418#endif
2419#if defined(MBEDTLS_SHA1_C)
2420 case PSA_ALG_SHA_1:
2421 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2422 &source_operation->ctx.sha1 );
2423 break;
2424#endif
2425#if defined(MBEDTLS_SHA256_C)
2426 case PSA_ALG_SHA_224:
2427 case PSA_ALG_SHA_256:
2428 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2429 &source_operation->ctx.sha256 );
2430 break;
2431#endif
2432#if defined(MBEDTLS_SHA512_C)
2433 case PSA_ALG_SHA_384:
2434 case PSA_ALG_SHA_512:
2435 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2436 &source_operation->ctx.sha512 );
2437 break;
2438#endif
2439 default:
2440 return( PSA_ERROR_NOT_SUPPORTED );
2441 }
2442
2443 target_operation->alg = source_operation->alg;
2444 return( PSA_SUCCESS );
2445}
Michael Thomas4c53fc72020-01-24 03:45:51 +00002446
2447
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449/* MAC */
2450/****************************************************************/
2451
Michael Thomas863b5d62020-02-10 19:41:16 +00002452const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 psa_algorithm_t alg,
2454 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002455 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002456 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002459 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002461 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002462 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002463
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2465 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002466 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002468 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002469 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470 mode = MBEDTLS_MODE_STREAM;
2471 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 case PSA_ALG_CTR:
2473 mode = MBEDTLS_MODE_CTR;
2474 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002475 case PSA_ALG_CFB:
2476 mode = MBEDTLS_MODE_CFB;
2477 break;
2478 case PSA_ALG_OFB:
2479 mode = MBEDTLS_MODE_OFB;
2480 break;
2481 case PSA_ALG_CBC_NO_PADDING:
2482 mode = MBEDTLS_MODE_CBC;
2483 break;
2484 case PSA_ALG_CBC_PKCS7:
2485 mode = MBEDTLS_MODE_CBC;
2486 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002487 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 mode = MBEDTLS_MODE_CCM;
2489 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002490 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 mode = MBEDTLS_MODE_GCM;
2492 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002493 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2494 mode = MBEDTLS_MODE_CHACHAPOLY;
2495 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496 default:
2497 return( NULL );
2498 }
2499 }
2500 else if( alg == PSA_ALG_CMAC )
2501 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502 else
2503 return( NULL );
2504
2505 switch( key_type )
2506 {
2507 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002508 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 break;
2510 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002511 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2512 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002514 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002516 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002517 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2518 * but two-key Triple-DES is functionally three-key Triple-DES
2519 * with K1=K3, so that's how we present it to mbedtls. */
2520 if( key_bits == 128 )
2521 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522 break;
2523 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002524 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525 break;
2526 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002527 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002529 case PSA_KEY_TYPE_CHACHA20:
2530 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2531 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 default:
2533 return( NULL );
2534 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002535 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002536 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537
Jaeden Amero23bbb752018-06-26 14:16:54 +01002538 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2539 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540}
2541
Gilles Peskinea05219c2018-11-16 16:02:56 +01002542#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002543static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002544{
Gilles Peskine2d277862018-06-18 15:41:12 +02002545 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002546 {
2547 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002548 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002549 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002550 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002551 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002552 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002553 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002554 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002555 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002556 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002557 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002558 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002559 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002560 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002561 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002562 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002563 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002564 return( 128 );
2565 default:
2566 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002567 }
2568}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002569#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002570
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571/* Initialize the MAC operation structure. Once this function has been
2572 * called, psa_mac_abort can run and will do the right thing. */
2573static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2574 psa_algorithm_t alg )
2575{
2576 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2577
2578 operation->alg = alg;
2579 operation->key_set = 0;
2580 operation->iv_set = 0;
2581 operation->iv_required = 0;
2582 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002583 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584
2585#if defined(MBEDTLS_CMAC_C)
2586 if( alg == PSA_ALG_CMAC )
2587 {
2588 operation->iv_required = 0;
2589 mbedtls_cipher_init( &operation->ctx.cmac );
2590 status = PSA_SUCCESS;
2591 }
2592 else
2593#endif /* MBEDTLS_CMAC_C */
2594#if defined(MBEDTLS_MD_C)
2595 if( PSA_ALG_IS_HMAC( operation->alg ) )
2596 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002597 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2598 operation->ctx.hmac.hash_ctx.alg = 0;
2599 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002600 }
2601 else
2602#endif /* MBEDTLS_MD_C */
2603 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002604 if( ! PSA_ALG_IS_MAC( alg ) )
2605 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002606 }
2607
2608 if( status != PSA_SUCCESS )
2609 memset( operation, 0, sizeof( *operation ) );
2610 return( status );
2611}
2612
Gilles Peskine01126fa2018-07-12 17:04:55 +02002613#if defined(MBEDTLS_MD_C)
2614static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2615{
Gilles Peskine3f108122018-12-07 18:14:53 +01002616 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002617 return( psa_hash_abort( &hmac->hash_ctx ) );
2618}
2619#endif /* MBEDTLS_MD_C */
2620
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2622{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625 /* The object has (apparently) been initialized but it is not
2626 * in use. It's ok to call abort on such an object, and there's
2627 * nothing to do. */
2628 return( PSA_SUCCESS );
2629 }
2630 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002632 if( operation->alg == PSA_ALG_CMAC )
2633 {
2634 mbedtls_cipher_free( &operation->ctx.cmac );
2635 }
2636 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002639 if( PSA_ALG_IS_HMAC( operation->alg ) )
2640 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002641 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002642 }
2643 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002645 {
2646 /* Sanity check (shouldn't happen: operation->alg should
2647 * always have been initialized to a valid value). */
2648 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649 }
Moran Peker41deec42018-04-04 15:43:05 +03002650
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651 operation->alg = 0;
2652 operation->key_set = 0;
2653 operation->iv_set = 0;
2654 operation->iv_required = 0;
2655 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002656 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002657
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002659
2660bad_state:
2661 /* If abort is called on an uninitialized object, we can't trust
2662 * anything. Wipe the object in case it contains confidential data.
2663 * This may result in a memory leak if a pointer gets overwritten,
2664 * but it's too late to do anything about this. */
2665 memset( operation, 0, sizeof( *operation ) );
2666 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667}
2668
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002669#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002670static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002671 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002672 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673 const mbedtls_cipher_info_t *cipher_info )
2674{
2675 int ret;
2676
2677 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002678
2679 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2680 if( ret != 0 )
2681 return( ret );
2682
2683 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2684 slot->data.raw.data,
2685 key_bits );
2686 return( ret );
2687}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002688#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689
Gilles Peskine248051a2018-06-20 16:09:38 +02002690#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002691static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2692 const uint8_t *key,
2693 size_t key_length,
2694 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002695{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002696 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002698 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002699 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002700 psa_status_t status;
2701
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002702 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2703 * overflow below. This should never trigger if the hash algorithm
2704 * is implemented correctly. */
2705 /* The size checks against the ipad and opad buffers cannot be written
2706 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2707 * because that triggers -Wlogical-op on GCC 7.3. */
2708 if( block_size > sizeof( ipad ) )
2709 return( PSA_ERROR_NOT_SUPPORTED );
2710 if( block_size > sizeof( hmac->opad ) )
2711 return( PSA_ERROR_NOT_SUPPORTED );
2712 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 return( PSA_ERROR_NOT_SUPPORTED );
2714
Gilles Peskined223b522018-06-11 18:12:58 +02002715 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002717 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002719 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002720 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002722 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002723 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002724 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002725 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002726 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727 }
Gilles Peskine96889972018-07-12 17:07:03 +02002728 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2729 * but it is permitted. It is common when HMAC is used in HKDF, for
2730 * example. Don't call `memcpy` in the 0-length because `key` could be
2731 * an invalid pointer which would make the behavior undefined. */
2732 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002733 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734
Gilles Peskined223b522018-06-11 18:12:58 +02002735 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2736 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002738 ipad[i] ^= 0x36;
2739 memset( ipad + key_length, 0x36, block_size - key_length );
2740
2741 /* Copy the key material from ipad to opad, flipping the requisite bits,
2742 * and filling the rest of opad with the requisite constant. */
2743 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002744 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2745 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746
Gilles Peskine01126fa2018-07-12 17:04:55 +02002747 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002749 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002750
Gilles Peskine01126fa2018-07-12 17:04:55 +02002751 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002752
2753cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002754 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002755
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 return( status );
2757}
Gilles Peskine248051a2018-06-20 16:09:38 +02002758#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002759
Gilles Peskine89167cb2018-07-08 20:12:23 +02002760static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002761 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002762 psa_algorithm_t alg,
2763 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002766 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002768 psa_key_usage_t usage =
2769 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002770 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002771 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002773 /* A context must be freshly initialized before it can be set up. */
2774 if( operation->alg != 0 )
2775 {
2776 return( PSA_ERROR_BAD_STATE );
2777 }
2778
Gilles Peskined911eb72018-08-14 15:18:45 +02002779 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002780 if( status != PSA_SUCCESS )
2781 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002782 if( is_sign )
2783 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784
Gilles Peskine28f8f302019-07-24 13:30:31 +02002785 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002786 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002787 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002788 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002789
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002791 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002792 {
2793 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002794 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002795 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002796 int ret;
2797 if( cipher_info == NULL )
2798 {
2799 status = PSA_ERROR_NOT_SUPPORTED;
2800 goto exit;
2801 }
2802 operation->mac_size = cipher_info->block_size;
2803 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2804 status = mbedtls_to_psa_error( ret );
2805 }
2806 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002808#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002809 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002810 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002811 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002812 if( hash_alg == 0 )
2813 {
2814 status = PSA_ERROR_NOT_SUPPORTED;
2815 goto exit;
2816 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002817
2818 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2819 /* Sanity check. This shouldn't fail on a valid configuration. */
2820 if( operation->mac_size == 0 ||
2821 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2822 {
2823 status = PSA_ERROR_NOT_SUPPORTED;
2824 goto exit;
2825 }
2826
Gilles Peskine8e338702019-07-30 20:06:31 +02002827 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002828 {
2829 status = PSA_ERROR_INVALID_ARGUMENT;
2830 goto exit;
2831 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002832
Gilles Peskine01126fa2018-07-12 17:04:55 +02002833 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2834 slot->data.raw.data,
2835 slot->data.raw.bytes,
2836 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002837 }
2838 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002840 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002841 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002844
Gilles Peskined911eb72018-08-14 15:18:45 +02002845 if( truncated == 0 )
2846 {
2847 /* The "normal" case: untruncated algorithm. Nothing to do. */
2848 }
2849 else if( truncated < 4 )
2850 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002851 /* A very short MAC is too short for security since it can be
2852 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2853 * so we make this our minimum, even though 32 bits is still
2854 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002855 status = PSA_ERROR_NOT_SUPPORTED;
2856 }
2857 else if( truncated > operation->mac_size )
2858 {
2859 /* It's impossible to "truncate" to a larger length. */
2860 status = PSA_ERROR_INVALID_ARGUMENT;
2861 }
2862 else
2863 operation->mac_size = truncated;
2864
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002866 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002867 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002868 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002870 else
2871 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002872 operation->key_set = 1;
2873 }
2874 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875}
2876
Gilles Peskine89167cb2018-07-08 20:12:23 +02002877psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002878 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002879 psa_algorithm_t alg )
2880{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002881 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002882}
2883
2884psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002885 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002886 psa_algorithm_t alg )
2887{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002888 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002889}
2890
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2892 const uint8_t *input,
2893 size_t input_length )
2894{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002897 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002899 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900 operation->has_input = 1;
2901
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002903 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002904 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002905 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2906 input, input_length );
2907 status = mbedtls_to_psa_error( ret );
2908 }
2909 else
2910#endif /* MBEDTLS_CMAC_C */
2911#if defined(MBEDTLS_MD_C)
2912 if( PSA_ALG_IS_HMAC( operation->alg ) )
2913 {
2914 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2915 input_length );
2916 }
2917 else
2918#endif /* MBEDTLS_MD_C */
2919 {
2920 /* This shouldn't happen if `operation` was initialized by
2921 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002922 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002923 }
2924
Gilles Peskinefbfac682018-07-08 20:51:54 +02002925 if( status != PSA_SUCCESS )
2926 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002927 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928}
2929
Gilles Peskine01126fa2018-07-12 17:04:55 +02002930#if defined(MBEDTLS_MD_C)
2931static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2932 uint8_t *mac,
2933 size_t mac_size )
2934{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002935 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002936 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2937 size_t hash_size = 0;
2938 size_t block_size = psa_get_hash_block_size( hash_alg );
2939 psa_status_t status;
2940
Gilles Peskine01126fa2018-07-12 17:04:55 +02002941 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2942 if( status != PSA_SUCCESS )
2943 return( status );
2944 /* From here on, tmp needs to be wiped. */
2945
2946 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2947 if( status != PSA_SUCCESS )
2948 goto exit;
2949
2950 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2951 if( status != PSA_SUCCESS )
2952 goto exit;
2953
2954 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2955 if( status != PSA_SUCCESS )
2956 goto exit;
2957
Gilles Peskined911eb72018-08-14 15:18:45 +02002958 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2959 if( status != PSA_SUCCESS )
2960 goto exit;
2961
2962 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002963
2964exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002965 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002966 return( status );
2967}
2968#endif /* MBEDTLS_MD_C */
2969
mohammad16036df908f2018-04-02 08:34:15 -07002970static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002971 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002972 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002973{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002974 if( ! operation->key_set )
2975 return( PSA_ERROR_BAD_STATE );
2976 if( operation->iv_required && ! operation->iv_set )
2977 return( PSA_ERROR_BAD_STATE );
2978
Gilles Peskine8c9def32018-02-08 10:02:12 +01002979 if( mac_size < operation->mac_size )
2980 return( PSA_ERROR_BUFFER_TOO_SMALL );
2981
Gilles Peskine8c9def32018-02-08 10:02:12 +01002982#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002983 if( operation->alg == PSA_ALG_CMAC )
2984 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002985 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2986 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2987 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002988 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002989 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002990 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002992 else
2993#endif /* MBEDTLS_CMAC_C */
2994#if defined(MBEDTLS_MD_C)
2995 if( PSA_ALG_IS_HMAC( operation->alg ) )
2996 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002998 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002999 }
3000 else
3001#endif /* MBEDTLS_MD_C */
3002 {
3003 /* This shouldn't happen if `operation` was initialized by
3004 * a setup function. */
3005 return( PSA_ERROR_BAD_STATE );
3006 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007}
3008
Gilles Peskineacd4be32018-07-08 19:56:25 +02003009psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3010 uint8_t *mac,
3011 size_t mac_size,
3012 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003013{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003014 psa_status_t status;
3015
Jaeden Amero252ef282019-02-15 14:05:35 +00003016 if( operation->alg == 0 )
3017 {
3018 return( PSA_ERROR_BAD_STATE );
3019 }
3020
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003021 /* Fill the output buffer with something that isn't a valid mac
3022 * (barring an attack on the mac and deliberately-crafted input),
3023 * in case the caller doesn't check the return status properly. */
3024 *mac_length = mac_size;
3025 /* If mac_size is 0 then mac may be NULL and then the
3026 * call to memset would have undefined behavior. */
3027 if( mac_size != 0 )
3028 memset( mac, '!', mac_size );
3029
Gilles Peskine89167cb2018-07-08 20:12:23 +02003030 if( ! operation->is_sign )
3031 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003032 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003033 }
mohammad16036df908f2018-04-02 08:34:15 -07003034
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003035 status = psa_mac_finish_internal( operation, mac, mac_size );
3036
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003037 if( status == PSA_SUCCESS )
3038 {
3039 status = psa_mac_abort( operation );
3040 if( status == PSA_SUCCESS )
3041 *mac_length = operation->mac_size;
3042 else
3043 memset( mac, '!', mac_size );
3044 }
3045 else
3046 psa_mac_abort( operation );
3047 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003048}
3049
Gilles Peskineacd4be32018-07-08 19:56:25 +02003050psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3051 const uint8_t *mac,
3052 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053{
Gilles Peskine828ed142018-06-18 23:25:51 +02003054 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003055 psa_status_t status;
3056
Jaeden Amero252ef282019-02-15 14:05:35 +00003057 if( operation->alg == 0 )
3058 {
3059 return( PSA_ERROR_BAD_STATE );
3060 }
3061
Gilles Peskine89167cb2018-07-08 20:12:23 +02003062 if( operation->is_sign )
3063 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003064 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003065 }
3066 if( operation->mac_size != mac_length )
3067 {
3068 status = PSA_ERROR_INVALID_SIGNATURE;
3069 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003070 }
mohammad16036df908f2018-04-02 08:34:15 -07003071
3072 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003073 actual_mac, sizeof( actual_mac ) );
3074
3075 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3076 status = PSA_ERROR_INVALID_SIGNATURE;
3077
3078cleanup:
3079 if( status == PSA_SUCCESS )
3080 status = psa_mac_abort( operation );
3081 else
3082 psa_mac_abort( operation );
3083
Gilles Peskine3f108122018-12-07 18:14:53 +01003084 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003085
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003086 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087}
3088
3089
Gilles Peskine20035e32018-02-03 22:44:14 +01003090
Gilles Peskine20035e32018-02-03 22:44:14 +01003091/****************************************************************/
3092/* Asymmetric cryptography */
3093/****************************************************************/
3094
Gilles Peskine2b450e32018-06-27 15:42:46 +02003095#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003096/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003097 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003098static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3099 size_t hash_length,
3100 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003101{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003102 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003103 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003104 *md_alg = mbedtls_md_get_type( md_info );
3105
3106 /* The Mbed TLS RSA module uses an unsigned int for hash length
3107 * parameters. Validate that it fits so that we don't risk an
3108 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003109#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003110 if( hash_length > UINT_MAX )
3111 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003112#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003113
3114#if defined(MBEDTLS_PKCS1_V15)
3115 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3116 * must be correct. */
3117 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3118 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003119 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003120 if( md_info == NULL )
3121 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003122 if( mbedtls_md_get_size( md_info ) != hash_length )
3123 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003124 }
3125#endif /* MBEDTLS_PKCS1_V15 */
3126
3127#if defined(MBEDTLS_PKCS1_V21)
3128 /* PSS requires a hash internally. */
3129 if( PSA_ALG_IS_RSA_PSS( alg ) )
3130 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003131 if( md_info == NULL )
3132 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003133 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003134#endif /* MBEDTLS_PKCS1_V21 */
3135
Gilles Peskine61b91d42018-06-08 16:09:36 +02003136 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003137}
3138
Gilles Peskine2b450e32018-06-27 15:42:46 +02003139static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3140 psa_algorithm_t alg,
3141 const uint8_t *hash,
3142 size_t hash_length,
3143 uint8_t *signature,
3144 size_t signature_size,
3145 size_t *signature_length )
3146{
3147 psa_status_t status;
3148 int ret;
3149 mbedtls_md_type_t md_alg;
3150
3151 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3152 if( status != PSA_SUCCESS )
3153 return( status );
3154
Gilles Peskine630a18a2018-06-29 17:49:35 +02003155 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003156 return( PSA_ERROR_BUFFER_TOO_SMALL );
3157
3158#if defined(MBEDTLS_PKCS1_V15)
3159 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3160 {
3161 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3162 MBEDTLS_MD_NONE );
3163 ret = mbedtls_rsa_pkcs1_sign( rsa,
3164 mbedtls_ctr_drbg_random,
3165 &global_data.ctr_drbg,
3166 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003167 md_alg,
3168 (unsigned int) hash_length,
3169 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003170 signature );
3171 }
3172 else
3173#endif /* MBEDTLS_PKCS1_V15 */
3174#if defined(MBEDTLS_PKCS1_V21)
3175 if( PSA_ALG_IS_RSA_PSS( alg ) )
3176 {
3177 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3178 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3179 mbedtls_ctr_drbg_random,
3180 &global_data.ctr_drbg,
3181 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003182 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003183 (unsigned int) hash_length,
3184 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003185 signature );
3186 }
3187 else
3188#endif /* MBEDTLS_PKCS1_V21 */
3189 {
3190 return( PSA_ERROR_INVALID_ARGUMENT );
3191 }
3192
3193 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003194 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003195 return( mbedtls_to_psa_error( ret ) );
3196}
3197
3198static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3199 psa_algorithm_t alg,
3200 const uint8_t *hash,
3201 size_t hash_length,
3202 const uint8_t *signature,
3203 size_t signature_length )
3204{
3205 psa_status_t status;
3206 int ret;
3207 mbedtls_md_type_t md_alg;
3208
3209 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3210 if( status != PSA_SUCCESS )
3211 return( status );
3212
Gilles Peskine630a18a2018-06-29 17:49:35 +02003213 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003214 return( PSA_ERROR_BUFFER_TOO_SMALL );
3215
3216#if defined(MBEDTLS_PKCS1_V15)
3217 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3218 {
3219 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3220 MBEDTLS_MD_NONE );
3221 ret = mbedtls_rsa_pkcs1_verify( rsa,
3222 mbedtls_ctr_drbg_random,
3223 &global_data.ctr_drbg,
3224 MBEDTLS_RSA_PUBLIC,
3225 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003226 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003227 hash,
3228 signature );
3229 }
3230 else
3231#endif /* MBEDTLS_PKCS1_V15 */
3232#if defined(MBEDTLS_PKCS1_V21)
3233 if( PSA_ALG_IS_RSA_PSS( alg ) )
3234 {
3235 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3236 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3237 mbedtls_ctr_drbg_random,
3238 &global_data.ctr_drbg,
3239 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003240 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003241 (unsigned int) hash_length,
3242 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003243 signature );
3244 }
3245 else
3246#endif /* MBEDTLS_PKCS1_V21 */
3247 {
3248 return( PSA_ERROR_INVALID_ARGUMENT );
3249 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003250
3251 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3252 * the rest of the signature is invalid". This has little use in
3253 * practice and PSA doesn't report this distinction. */
3254 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3255 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003256 return( mbedtls_to_psa_error( ret ) );
3257}
3258#endif /* MBEDTLS_RSA_C */
3259
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003260#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003261/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3262 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3263 * (even though these functions don't modify it). */
3264static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3265 psa_algorithm_t alg,
3266 const uint8_t *hash,
3267 size_t hash_length,
3268 uint8_t *signature,
3269 size_t signature_size,
3270 size_t *signature_length )
3271{
3272 int ret;
3273 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003274 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003275 mbedtls_mpi_init( &r );
3276 mbedtls_mpi_init( &s );
3277
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003278 if( signature_size < 2 * curve_bytes )
3279 {
3280 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3281 goto cleanup;
3282 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003283
Gilles Peskinea05219c2018-11-16 16:02:56 +01003284#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003285 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3286 {
3287 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3288 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3289 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003290 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3291 &ecp->d, hash,
3292 hash_length, md_alg,
3293 mbedtls_ctr_drbg_random,
3294 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003295 }
3296 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003297#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003298 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003299 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003300 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3301 hash, hash_length,
3302 mbedtls_ctr_drbg_random,
3303 &global_data.ctr_drbg ) );
3304 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003305
3306 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3307 signature,
3308 curve_bytes ) );
3309 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3310 signature + curve_bytes,
3311 curve_bytes ) );
3312
3313cleanup:
3314 mbedtls_mpi_free( &r );
3315 mbedtls_mpi_free( &s );
3316 if( ret == 0 )
3317 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003318 return( mbedtls_to_psa_error( ret ) );
3319}
3320
3321static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3322 const uint8_t *hash,
3323 size_t hash_length,
3324 const uint8_t *signature,
3325 size_t signature_length )
3326{
3327 int ret;
3328 mbedtls_mpi r, s;
3329 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3330 mbedtls_mpi_init( &r );
3331 mbedtls_mpi_init( &s );
3332
3333 if( signature_length != 2 * curve_bytes )
3334 return( PSA_ERROR_INVALID_SIGNATURE );
3335
3336 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3337 signature,
3338 curve_bytes ) );
3339 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3340 signature + curve_bytes,
3341 curve_bytes ) );
3342
3343 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3344 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003345
3346cleanup:
3347 mbedtls_mpi_free( &r );
3348 mbedtls_mpi_free( &s );
3349 return( mbedtls_to_psa_error( ret ) );
3350}
3351#endif /* MBEDTLS_ECDSA_C */
3352
Gilles Peskinec5487a82018-12-03 18:08:14 +01003353psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003354 psa_algorithm_t alg,
3355 const uint8_t *hash,
3356 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003357 uint8_t *signature,
3358 size_t signature_size,
3359 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003360{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003361 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003362 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003363#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3364 const psa_drv_se_t *drv;
3365 psa_drv_se_context_t *drv_context;
3366#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003367
3368 *signature_length = signature_size;
3369
Gilles Peskineedc64242019-08-07 21:05:07 +02003370 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003371 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003372 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003373 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003374 {
3375 status = PSA_ERROR_INVALID_ARGUMENT;
3376 goto exit;
3377 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003378
Gilles Peskineedc64242019-08-07 21:05:07 +02003379#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3380 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3381 {
3382 if( drv->asymmetric == NULL ||
3383 drv->asymmetric->p_sign == NULL )
3384 {
3385 status = PSA_ERROR_NOT_SUPPORTED;
3386 goto exit;
3387 }
3388 status = drv->asymmetric->p_sign( drv_context,
3389 slot->data.se.slot_number,
3390 alg,
3391 hash, hash_length,
3392 signature, signature_size,
3393 signature_length );
3394 }
3395 else
3396#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas4c53fc72020-01-24 03:45:51 +00003397#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
Michael Thomas153cd2f2020-01-22 16:54:38 +00003398 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
Michael Thomasff0e8b92020-01-21 15:01:10 +00003399 {
Michael Thomas153cd2f2020-01-22 16:54:38 +00003400 status = psa_asymmetric_sign_vendor(slot,alg,
Michael Thomasecb72842020-01-22 23:24:41 +00003401 hash, hash_length,
3402 signature, signature_size,
3403 signature_length );
Michael Thomasff0e8b92020-01-21 15:01:10 +00003404 }
3405 else
Michael Thomas4c53fc72020-01-24 03:45:51 +00003406#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003407#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003408 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003409 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003410 status = psa_rsa_sign( slot->data.rsa,
3411 alg,
3412 hash, hash_length,
3413 signature, signature_size,
3414 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003415 }
3416 else
3417#endif /* defined(MBEDTLS_RSA_C) */
3418#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003419 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003420 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003421#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003422 if(
3423#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3424 PSA_ALG_IS_ECDSA( alg )
3425#else
3426 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3427#endif
3428 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003429 status = psa_ecdsa_sign( slot->data.ecp,
3430 alg,
3431 hash, hash_length,
3432 signature, signature_size,
3433 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003434 else
3435#endif /* defined(MBEDTLS_ECDSA_C) */
3436 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003437 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003438 }
itayzafrir5c753392018-05-08 11:18:38 +03003439 }
3440 else
3441#endif /* defined(MBEDTLS_ECP_C) */
3442 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003443 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003444 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003445
3446exit:
3447 /* Fill the unused part of the output buffer (the whole buffer on error,
3448 * the trailing part on success) with something that isn't a valid mac
3449 * (barring an attack on the mac and deliberately-crafted input),
3450 * in case the caller doesn't check the return status properly. */
3451 if( status == PSA_SUCCESS )
3452 memset( signature + *signature_length, '!',
3453 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003454 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003455 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003456 /* If signature_size is 0 then we have nothing to do. We must not call
3457 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003458 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003459}
3460
Gilles Peskinec5487a82018-12-03 18:08:14 +01003461psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003462 psa_algorithm_t alg,
3463 const uint8_t *hash,
3464 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003465 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003466 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003467{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003468 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003469 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003470#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3471 const psa_drv_se_t *drv;
3472 psa_drv_se_context_t *drv_context;
3473#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003474
Gilles Peskineedc64242019-08-07 21:05:07 +02003475 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003476 if( status != PSA_SUCCESS )
3477 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003478
Gilles Peskineedc64242019-08-07 21:05:07 +02003479#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3480 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3481 {
3482 if( drv->asymmetric == NULL ||
3483 drv->asymmetric->p_verify == NULL )
3484 return( PSA_ERROR_NOT_SUPPORTED );
3485 return( drv->asymmetric->p_verify( drv_context,
3486 slot->data.se.slot_number,
3487 alg,
3488 hash, hash_length,
3489 signature, signature_length ) );
3490 }
3491 else
3492#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas4c53fc72020-01-24 03:45:51 +00003493#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
Michael Thomas153cd2f2020-01-22 16:54:38 +00003494if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
Michael Thomasff0e8b92020-01-21 15:01:10 +00003495 {
Michael Thomas153cd2f2020-01-22 16:54:38 +00003496 return( psa_asymmetric_verify_vendor(slot,alg,
Michael Thomasecb72842020-01-22 23:24:41 +00003497 hash, hash_length,
3498 signature, signature_length ) );
Michael Thomasff0e8b92020-01-21 15:01:10 +00003499 }
3500 else
Michael Thomas4c53fc72020-01-24 03:45:51 +00003501#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003502#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003503 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003504 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003505 return( psa_rsa_verify( slot->data.rsa,
3506 alg,
3507 hash, hash_length,
3508 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003509 }
3510 else
3511#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003512#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003513 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003514 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003515#if defined(MBEDTLS_ECDSA_C)
3516 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003517 return( psa_ecdsa_verify( slot->data.ecp,
3518 hash, hash_length,
3519 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003520 else
3521#endif /* defined(MBEDTLS_ECDSA_C) */
3522 {
3523 return( PSA_ERROR_INVALID_ARGUMENT );
3524 }
itayzafrir5c753392018-05-08 11:18:38 +03003525 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003526 else
3527#endif /* defined(MBEDTLS_ECP_C) */
3528 {
3529 return( PSA_ERROR_NOT_SUPPORTED );
3530 }
3531}
3532
Gilles Peskine072ac562018-06-30 00:21:29 +02003533#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3534static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3535 mbedtls_rsa_context *rsa )
3536{
3537 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3538 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3539 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3540 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3541}
3542#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3543
Gilles Peskinec5487a82018-12-03 18:08:14 +01003544psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003545 psa_algorithm_t alg,
3546 const uint8_t *input,
3547 size_t input_length,
3548 const uint8_t *salt,
3549 size_t salt_length,
3550 uint8_t *output,
3551 size_t output_size,
3552 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003554 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003555 psa_status_t status;
3556
Darryl Green5cc689a2018-07-24 15:34:10 +01003557 (void) input;
3558 (void) input_length;
3559 (void) salt;
3560 (void) output;
3561 (void) output_size;
3562
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003563 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003565 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3566 return( PSA_ERROR_INVALID_ARGUMENT );
3567
Gilles Peskine28f8f302019-07-24 13:30:31 +02003568 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003569 if( status != PSA_SUCCESS )
3570 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003571 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3572 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003574
3575#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003576 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577 {
3578 mbedtls_rsa_context *rsa = slot->data.rsa;
3579 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003580 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003581 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003583 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003585 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3586 mbedtls_ctr_drbg_random,
3587 &global_data.ctr_drbg,
3588 MBEDTLS_RSA_PUBLIC,
3589 input_length,
3590 input,
3591 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592 }
3593 else
3594#endif /* MBEDTLS_PKCS1_V15 */
3595#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003596 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003598 psa_rsa_oaep_set_padding_mode( alg, rsa );
3599 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3600 mbedtls_ctr_drbg_random,
3601 &global_data.ctr_drbg,
3602 MBEDTLS_RSA_PUBLIC,
3603 salt, salt_length,
3604 input_length,
3605 input,
3606 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607 }
3608 else
3609#endif /* MBEDTLS_PKCS1_V21 */
3610 {
3611 return( PSA_ERROR_INVALID_ARGUMENT );
3612 }
3613 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003614 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 return( mbedtls_to_psa_error( ret ) );
3616 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003618#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003619 {
3620 return( PSA_ERROR_NOT_SUPPORTED );
3621 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003622}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623
Gilles Peskinec5487a82018-12-03 18:08:14 +01003624psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003625 psa_algorithm_t alg,
3626 const uint8_t *input,
3627 size_t input_length,
3628 const uint8_t *salt,
3629 size_t salt_length,
3630 uint8_t *output,
3631 size_t output_size,
3632 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003634 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003635 psa_status_t status;
3636
Darryl Green5cc689a2018-07-24 15:34:10 +01003637 (void) input;
3638 (void) input_length;
3639 (void) salt;
3640 (void) output;
3641 (void) output_size;
3642
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003643 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003645 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3646 return( PSA_ERROR_INVALID_ARGUMENT );
3647
Gilles Peskine28f8f302019-07-24 13:30:31 +02003648 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003649 if( status != PSA_SUCCESS )
3650 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003651 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 return( PSA_ERROR_INVALID_ARGUMENT );
3653
3654#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003655 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003656 {
3657 mbedtls_rsa_context *rsa = slot->data.rsa;
3658 int ret;
3659
Gilles Peskine630a18a2018-06-29 17:49:35 +02003660 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003661 return( PSA_ERROR_INVALID_ARGUMENT );
3662
3663#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003664 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003665 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003666 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3667 mbedtls_ctr_drbg_random,
3668 &global_data.ctr_drbg,
3669 MBEDTLS_RSA_PRIVATE,
3670 output_length,
3671 input,
3672 output,
3673 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 }
3675 else
3676#endif /* MBEDTLS_PKCS1_V15 */
3677#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003678 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003680 psa_rsa_oaep_set_padding_mode( alg, rsa );
3681 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3682 mbedtls_ctr_drbg_random,
3683 &global_data.ctr_drbg,
3684 MBEDTLS_RSA_PRIVATE,
3685 salt, salt_length,
3686 output_length,
3687 input,
3688 output,
3689 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690 }
3691 else
3692#endif /* MBEDTLS_PKCS1_V21 */
3693 {
3694 return( PSA_ERROR_INVALID_ARGUMENT );
3695 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003696
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697 return( mbedtls_to_psa_error( ret ) );
3698 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003700#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003701 {
3702 return( PSA_ERROR_NOT_SUPPORTED );
3703 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003704}
Gilles Peskine20035e32018-02-03 22:44:14 +01003705
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003706
3707
mohammad1603503973b2018-03-12 15:59:30 +02003708/****************************************************************/
3709/* Symmetric cryptography */
3710/****************************************************************/
3711
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003712/* Initialize the cipher operation structure. Once this function has been
3713 * called, psa_cipher_abort can run and will do the right thing. */
3714static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3715 psa_algorithm_t alg )
3716{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003717 if( ! PSA_ALG_IS_CIPHER( alg ) )
3718 {
3719 memset( operation, 0, sizeof( *operation ) );
3720 return( PSA_ERROR_INVALID_ARGUMENT );
3721 }
3722
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003723 operation->alg = alg;
3724 operation->key_set = 0;
3725 operation->iv_set = 0;
3726 operation->iv_required = 1;
3727 operation->iv_size = 0;
3728 operation->block_size = 0;
3729 mbedtls_cipher_init( &operation->ctx.cipher );
3730 return( PSA_SUCCESS );
3731}
3732
Gilles Peskinee553c652018-06-04 16:22:46 +02003733static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003734 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003735 psa_algorithm_t alg,
3736 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003737{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003738 int ret = 0;
3739 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003740 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003741 size_t key_bits;
3742 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003743 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3744 PSA_KEY_USAGE_ENCRYPT :
3745 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003746
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003747 /* A context must be freshly initialized before it can be set up. */
3748 if( operation->alg != 0 )
3749 {
3750 return( PSA_ERROR_BAD_STATE );
3751 }
3752
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003753 status = psa_cipher_init( operation, alg );
3754 if( status != PSA_SUCCESS )
3755 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003756
Gilles Peskine28f8f302019-07-24 13:30:31 +02003757 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003758 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003759 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003760 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003761
Michael Thomas863b5d62020-02-10 19:41:16 +00003762#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
3763 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
3764 {
3765 status = psa_cipher_setup_vendor(operation, handle, alg, cipher_operation);
3766 goto exit;
3767 }
3768#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
3769
Gilles Peskine8e338702019-07-30 20:06:31 +02003770 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003771 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003772 {
3773 status = PSA_ERROR_NOT_SUPPORTED;
3774 goto exit;
3775 }
mohammad1603503973b2018-03-12 15:59:30 +02003776
mohammad1603503973b2018-03-12 15:59:30 +02003777 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003778 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003779 goto exit;
Michael Thomas17547bc2020-01-14 16:47:03 +00003780
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003781#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003782 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003783 {
3784 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003785 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003786 memcpy( keys, slot->data.raw.data, 16 );
3787 memcpy( keys + 16, slot->data.raw.data, 8 );
3788 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3789 keys,
3790 192, cipher_operation );
3791 }
3792 else
3793#endif
3794 {
3795 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3796 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003797 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003798 }
Moran Peker41deec42018-04-04 15:43:05 +03003799 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003800 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003801
mohammad16038481e742018-03-18 13:57:31 +02003802#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003803 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003804 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003805 case PSA_ALG_CBC_NO_PADDING:
3806 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3807 MBEDTLS_PADDING_NONE );
3808 break;
3809 case PSA_ALG_CBC_PKCS7:
3810 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3811 MBEDTLS_PADDING_PKCS7 );
3812 break;
3813 default:
3814 /* The algorithm doesn't involve padding. */
3815 ret = 0;
3816 break;
3817 }
3818 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003819 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003820#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3821
mohammad1603503973b2018-03-12 15:59:30 +02003822 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003823 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003824 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003825 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003826 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003827 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003828 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003829#if defined(MBEDTLS_CHACHA20_C)
3830 else
3831 if( alg == PSA_ALG_CHACHA20 )
3832 operation->iv_size = 12;
3833#endif
mohammad1603503973b2018-03-12 15:59:30 +02003834
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003835exit:
3836 if( status == 0 )
3837 status = mbedtls_to_psa_error( ret );
3838 if( status != 0 )
3839 psa_cipher_abort( operation );
3840 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003841}
3842
Gilles Peskinefe119512018-07-08 21:39:34 +02003843psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003844 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003845 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003846{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003847 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003848}
3849
Gilles Peskinefe119512018-07-08 21:39:34 +02003850psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003851 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003852 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003853{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003854 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003855}
3856
Gilles Peskinefe119512018-07-08 21:39:34 +02003857psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003858 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003859 size_t iv_size,
3860 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003861{
itayzafrir534bd7c2018-08-02 13:56:32 +03003862 psa_status_t status;
3863 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003864 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003865 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003866 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003867 }
Moran Peker41deec42018-04-04 15:43:05 +03003868 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003869 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003870 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003871 goto exit;
3872 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003873 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3874 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003875 if( ret != 0 )
3876 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003877 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003878 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003879 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003880
mohammad16038481e742018-03-18 13:57:31 +02003881 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003882 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003883
Moran Peker395db872018-05-31 14:07:14 +03003884exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003886 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003887 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003888}
3889
Gilles Peskinefe119512018-07-08 21:39:34 +02003890psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003891 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003892 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003893{
itayzafrir534bd7c2018-08-02 13:56:32 +03003894 psa_status_t status;
3895 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003896 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003898 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 }
Moran Pekera28258c2018-05-29 16:25:04 +03003900 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003901 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003902 status = PSA_ERROR_INVALID_ARGUMENT;
3903 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003904 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003905 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3906 status = mbedtls_to_psa_error( ret );
3907exit:
3908 if( status == PSA_SUCCESS )
3909 operation->iv_set = 1;
3910 else
mohammad1603503973b2018-03-12 15:59:30 +02003911 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003913}
3914
Gilles Peskinee553c652018-06-04 16:22:46 +02003915psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3916 const uint8_t *input,
3917 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003918 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003919 size_t output_size,
3920 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003921{
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 psa_status_t status;
3923 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003924 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003925
3926 if( operation->alg == 0 )
3927 {
3928 return( PSA_ERROR_BAD_STATE );
3929 }
3930
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003931 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003932 {
3933 /* Take the unprocessed partial block left over from previous
3934 * update calls, if any, plus the input to this call. Remove
3935 * the last partial block, if any. You get the data that will be
3936 * output in this call. */
3937 expected_output_size =
3938 ( operation->ctx.cipher.unprocessed_len + input_length )
3939 / operation->block_size * operation->block_size;
3940 }
3941 else
3942 {
3943 expected_output_size = input_length;
3944 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003945
Gilles Peskine89d789c2018-06-04 17:17:16 +02003946 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003947 {
3948 status = PSA_ERROR_BUFFER_TOO_SMALL;
3949 goto exit;
3950 }
mohammad160382759612018-03-12 18:16:40 +02003951
mohammad1603503973b2018-03-12 15:59:30 +02003952 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003953 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003954 status = mbedtls_to_psa_error( ret );
3955exit:
3956 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003957 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003958 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003959}
3960
Gilles Peskinee553c652018-06-04 16:22:46 +02003961psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3962 uint8_t *output,
3963 size_t output_size,
3964 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003965{
David Saadab4ecc272019-02-14 13:48:10 +02003966 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003967 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003968 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003969
mohammad1603503973b2018-03-12 15:59:30 +02003970 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003971 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003972 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003973 }
3974 if( operation->iv_required && ! operation->iv_set )
3975 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003976 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003977 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003978
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003979 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003980 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3981 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003982 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003983 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003984 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003985 }
3986
Janos Follath315b51c2018-07-09 16:04:51 +01003987 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3988 temp_output_buffer,
3989 output_length );
3990 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003991 {
Janos Follath315b51c2018-07-09 16:04:51 +01003992 status = mbedtls_to_psa_error( cipher_ret );
3993 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003994 }
Janos Follath315b51c2018-07-09 16:04:51 +01003995
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003996 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003997 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003998 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003999 memcpy( output, temp_output_buffer, *output_length );
4000 else
4001 {
Janos Follath315b51c2018-07-09 16:04:51 +01004002 status = PSA_ERROR_BUFFER_TOO_SMALL;
4003 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004004 }
mohammad1603503973b2018-03-12 15:59:30 +02004005
Gilles Peskine3f108122018-12-07 18:14:53 +01004006 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004007 status = psa_cipher_abort( operation );
4008
4009 return( status );
4010
4011error:
4012
4013 *output_length = 0;
4014
Gilles Peskine3f108122018-12-07 18:14:53 +01004015 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004016 (void) psa_cipher_abort( operation );
4017
4018 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004019}
4020
Gilles Peskinee553c652018-06-04 16:22:46 +02004021psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4022{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004023 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004024 {
4025 /* The object has (apparently) been initialized but it is not
4026 * in use. It's ok to call abort on such an object, and there's
4027 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004028 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004029 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004030
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004031 /* Sanity check (shouldn't happen: operation->alg should
4032 * always have been initialized to a valid value). */
4033 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4034 return( PSA_ERROR_BAD_STATE );
Michael Thomas863b5d62020-02-10 19:41:16 +00004035#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
Michael Thomas7de9ac52020-01-15 18:08:15 +00004036 psa_cipher_abort_vendor(operation);
Michael Thomas863b5d62020-02-10 19:41:16 +00004037#endif //MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C
mohammad1603503973b2018-03-12 15:59:30 +02004038 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004039
Moran Peker41deec42018-04-04 15:43:05 +03004040 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004041 operation->key_set = 0;
4042 operation->iv_set = 0;
4043 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004044 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004045 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004046
Moran Peker395db872018-05-31 14:07:14 +03004047 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004048}
4049
Gilles Peskinea0655c32018-04-30 17:06:50 +02004050
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004051
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004052
mohammad16035955c982018-04-26 00:53:03 +03004053/****************************************************************/
4054/* AEAD */
4055/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004056
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057typedef struct
4058{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004059 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 const mbedtls_cipher_info_t *cipher_info;
4061 union
4062 {
4063#if defined(MBEDTLS_CCM_C)
4064 mbedtls_ccm_context ccm;
4065#endif /* MBEDTLS_CCM_C */
4066#if defined(MBEDTLS_GCM_C)
4067 mbedtls_gcm_context gcm;
4068#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004069#if defined(MBEDTLS_CHACHAPOLY_C)
4070 mbedtls_chachapoly_context chachapoly;
4071#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004072 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004073 psa_algorithm_t core_alg;
4074 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004075 uint8_t tag_length;
4076} aead_operation_t;
4077
Gilles Peskine30a9e412019-01-14 18:36:12 +01004078static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004079{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004080 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004081 {
4082#if defined(MBEDTLS_CCM_C)
4083 case PSA_ALG_CCM:
4084 mbedtls_ccm_free( &operation->ctx.ccm );
4085 break;
4086#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004087#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 case PSA_ALG_GCM:
4089 mbedtls_gcm_free( &operation->ctx.gcm );
4090 break;
4091#endif /* MBEDTLS_GCM_C */
4092 }
4093}
4094
4095static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004096 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 psa_key_usage_t usage,
4098 psa_algorithm_t alg )
4099{
4100 psa_status_t status;
4101 size_t key_bits;
4102 mbedtls_cipher_id_t cipher_id;
4103
Gilles Peskine28f8f302019-07-24 13:30:31 +02004104 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004105 if( status != PSA_SUCCESS )
4106 return( status );
4107
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004108 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004109
4110 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004111 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112 &cipher_id );
4113 if( operation->cipher_info == NULL )
4114 return( PSA_ERROR_NOT_SUPPORTED );
4115
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004116 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 {
4118#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004119 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4120 operation->core_alg = PSA_ALG_CCM;
4121 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004122 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4123 * The call to mbedtls_ccm_encrypt_and_tag or
4124 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004125 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 return( PSA_ERROR_INVALID_ARGUMENT );
4127 mbedtls_ccm_init( &operation->ctx.ccm );
4128 status = mbedtls_to_psa_error(
4129 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4130 operation->slot->data.raw.data,
4131 (unsigned int) key_bits ) );
4132 if( status != 0 )
4133 goto cleanup;
4134 break;
4135#endif /* MBEDTLS_CCM_C */
4136
4137#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004138 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4139 operation->core_alg = PSA_ALG_GCM;
4140 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004141 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4142 * The call to mbedtls_gcm_crypt_and_tag or
4143 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004144 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004145 return( PSA_ERROR_INVALID_ARGUMENT );
4146 mbedtls_gcm_init( &operation->ctx.gcm );
4147 status = mbedtls_to_psa_error(
4148 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4149 operation->slot->data.raw.data,
4150 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004151 if( status != 0 )
4152 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004153 break;
4154#endif /* MBEDTLS_GCM_C */
4155
Gilles Peskine26869f22019-05-06 15:25:00 +02004156#if defined(MBEDTLS_CHACHAPOLY_C)
4157 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4158 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4159 operation->full_tag_length = 16;
4160 /* We only support the default tag length. */
4161 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4162 return( PSA_ERROR_NOT_SUPPORTED );
4163 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4164 status = mbedtls_to_psa_error(
4165 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4166 operation->slot->data.raw.data ) );
4167 if( status != 0 )
4168 goto cleanup;
4169 break;
4170#endif /* MBEDTLS_CHACHAPOLY_C */
4171
Gilles Peskineedf9a652018-08-17 18:11:56 +02004172 default:
4173 return( PSA_ERROR_NOT_SUPPORTED );
4174 }
4175
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004176 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4177 {
4178 status = PSA_ERROR_INVALID_ARGUMENT;
4179 goto cleanup;
4180 }
4181 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004182
Gilles Peskineedf9a652018-08-17 18:11:56 +02004183 return( PSA_SUCCESS );
4184
4185cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004186 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004187 return( status );
4188}
4189
Gilles Peskinec5487a82018-12-03 18:08:14 +01004190psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004191 psa_algorithm_t alg,
4192 const uint8_t *nonce,
4193 size_t nonce_length,
4194 const uint8_t *additional_data,
4195 size_t additional_data_length,
4196 const uint8_t *plaintext,
4197 size_t plaintext_length,
4198 uint8_t *ciphertext,
4199 size_t ciphertext_size,
4200 size_t *ciphertext_length )
4201{
mohammad16035955c982018-04-26 00:53:03 +03004202 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004203 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004204 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004205
mohammad1603f08a5502018-06-03 15:05:47 +03004206 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004207
Gilles Peskinec5487a82018-12-03 18:08:14 +01004208 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004209 if( status != PSA_SUCCESS )
4210 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004211
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212 /* For all currently supported modes, the tag is at the end of the
4213 * ciphertext. */
4214 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4215 {
4216 status = PSA_ERROR_BUFFER_TOO_SMALL;
4217 goto exit;
4218 }
4219 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004220
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004221#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004222 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004223 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004224 status = mbedtls_to_psa_error(
4225 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4226 MBEDTLS_GCM_ENCRYPT,
4227 plaintext_length,
4228 nonce, nonce_length,
4229 additional_data, additional_data_length,
4230 plaintext, ciphertext,
4231 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004232 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004233 else
4234#endif /* MBEDTLS_GCM_C */
4235#if defined(MBEDTLS_CCM_C)
4236 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004237 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004238 status = mbedtls_to_psa_error(
4239 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4240 plaintext_length,
4241 nonce, nonce_length,
4242 additional_data,
4243 additional_data_length,
4244 plaintext, ciphertext,
4245 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004246 }
mohammad16035c8845f2018-05-09 05:40:09 -07004247 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004248#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004249#if defined(MBEDTLS_CHACHAPOLY_C)
4250 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4251 {
4252 if( nonce_length != 12 || operation.tag_length != 16 )
4253 {
4254 status = PSA_ERROR_NOT_SUPPORTED;
4255 goto exit;
4256 }
4257 status = mbedtls_to_psa_error(
4258 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4259 plaintext_length,
4260 nonce,
4261 additional_data,
4262 additional_data_length,
4263 plaintext,
4264 ciphertext,
4265 tag ) );
4266 }
4267 else
4268#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004269 {
mohammad1603554faad2018-06-03 15:07:38 +03004270 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004271 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004272
Gilles Peskineedf9a652018-08-17 18:11:56 +02004273 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4274 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004275
Gilles Peskineedf9a652018-08-17 18:11:56 +02004276exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004277 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004278 if( status == PSA_SUCCESS )
4279 *ciphertext_length = plaintext_length + operation.tag_length;
4280 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004281}
4282
Gilles Peskineee652a32018-06-01 19:23:52 +02004283/* Locate the tag in a ciphertext buffer containing the encrypted data
4284 * followed by the tag. Return the length of the part preceding the tag in
4285 * *plaintext_length. This is the size of the plaintext in modes where
4286 * the encrypted data has the same size as the plaintext, such as
4287 * CCM and GCM. */
4288static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4289 const uint8_t *ciphertext,
4290 size_t ciphertext_length,
4291 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004292 const uint8_t **p_tag )
4293{
4294 size_t payload_length;
4295 if( tag_length > ciphertext_length )
4296 return( PSA_ERROR_INVALID_ARGUMENT );
4297 payload_length = ciphertext_length - tag_length;
4298 if( payload_length > plaintext_size )
4299 return( PSA_ERROR_BUFFER_TOO_SMALL );
4300 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004301 return( PSA_SUCCESS );
4302}
4303
Gilles Peskinec5487a82018-12-03 18:08:14 +01004304psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004305 psa_algorithm_t alg,
4306 const uint8_t *nonce,
4307 size_t nonce_length,
4308 const uint8_t *additional_data,
4309 size_t additional_data_length,
4310 const uint8_t *ciphertext,
4311 size_t ciphertext_length,
4312 uint8_t *plaintext,
4313 size_t plaintext_size,
4314 size_t *plaintext_length )
4315{
mohammad16035955c982018-04-26 00:53:03 +03004316 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004317 aead_operation_t operation;
4318 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004319
Gilles Peskineee652a32018-06-01 19:23:52 +02004320 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004321
Gilles Peskinec5487a82018-12-03 18:08:14 +01004322 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004323 if( status != PSA_SUCCESS )
4324 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004325
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004326 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4327 ciphertext, ciphertext_length,
4328 plaintext_size, &tag );
4329 if( status != PSA_SUCCESS )
4330 goto exit;
4331
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004332#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004333 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004334 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004335 status = mbedtls_to_psa_error(
4336 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4337 ciphertext_length - operation.tag_length,
4338 nonce, nonce_length,
4339 additional_data,
4340 additional_data_length,
4341 tag, operation.tag_length,
4342 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004343 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004344 else
4345#endif /* MBEDTLS_GCM_C */
4346#if defined(MBEDTLS_CCM_C)
4347 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004348 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004349 status = mbedtls_to_psa_error(
4350 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4351 ciphertext_length - operation.tag_length,
4352 nonce, nonce_length,
4353 additional_data,
4354 additional_data_length,
4355 ciphertext, plaintext,
4356 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004357 }
mohammad160339574652018-06-01 04:39:53 -07004358 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004359#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004360#if defined(MBEDTLS_CHACHAPOLY_C)
4361 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4362 {
4363 if( nonce_length != 12 || operation.tag_length != 16 )
4364 {
4365 status = PSA_ERROR_NOT_SUPPORTED;
4366 goto exit;
4367 }
4368 status = mbedtls_to_psa_error(
4369 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4370 ciphertext_length - operation.tag_length,
4371 nonce,
4372 additional_data,
4373 additional_data_length,
4374 tag,
4375 ciphertext,
4376 plaintext ) );
4377 }
4378 else
4379#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004380 {
mohammad1603554faad2018-06-03 15:07:38 +03004381 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004382 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004383
Gilles Peskineedf9a652018-08-17 18:11:56 +02004384 if( status != PSA_SUCCESS && plaintext_size != 0 )
4385 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004386
Gilles Peskineedf9a652018-08-17 18:11:56 +02004387exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004388 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004389 if( status == PSA_SUCCESS )
4390 *plaintext_length = ciphertext_length - operation.tag_length;
4391 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004392}
4393
Gilles Peskinea0655c32018-04-30 17:06:50 +02004394
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004395
Gilles Peskine20035e32018-02-03 22:44:14 +01004396/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004397/* Generators */
4398/****************************************************************/
4399
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004400#define HKDF_STATE_INIT 0 /* no input yet */
4401#define HKDF_STATE_STARTED 1 /* got salt */
4402#define HKDF_STATE_KEYED 2 /* got key */
4403#define HKDF_STATE_OUTPUT 3 /* output started */
4404
Gilles Peskinecbe66502019-05-16 16:59:18 +02004405static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004407{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4409 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004410 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004412}
4413
4414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004416{
4417 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004419 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004420 {
4421 /* The object has (apparently) been initialized but it is not
4422 * in use. It's ok to call abort on such an object, and there's
4423 * nothing to do. */
4424 }
4425 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004426#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004428 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 mbedtls_free( operation->ctx.hkdf.info );
4430 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004435 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004436 if( operation->ctx.tls12_prf.seed != NULL )
4437 {
4438 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4439 operation->ctx.tls12_prf.seed_length );
4440 mbedtls_free( operation->ctx.tls12_prf.seed );
4441 }
4442
4443 if( operation->ctx.tls12_prf.label != NULL )
4444 {
4445 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4446 operation->ctx.tls12_prf.label_length );
4447 mbedtls_free( operation->ctx.tls12_prf.label );
4448 }
4449
4450 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4451
4452 /* We leave the fields Ai and output_block to be erased safely by the
4453 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004454 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004455 else
4456#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004457 {
4458 status = PSA_ERROR_BAD_STATE;
4459 }
Janos Follath083036a2019-06-11 10:22:26 +01004460 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004461 return( status );
4462}
4463
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004465 size_t *capacity)
4466{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004468 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004470 return PSA_ERROR_BAD_STATE;
4471 }
4472
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004474 return( PSA_SUCCESS );
4475}
4476
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478 size_t capacity )
4479{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004481 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004483 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004485 return( PSA_SUCCESS );
4486}
4487
Gilles Peskinebef7f142018-07-12 17:22:21 +02004488#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004490 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004491static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004492 psa_algorithm_t hash_alg,
4493 uint8_t *output,
4494 size_t output_length )
4495{
4496 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4497 psa_status_t status;
4498
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004499 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4500 return( PSA_ERROR_BAD_STATE );
4501 hkdf->state = HKDF_STATE_OUTPUT;
4502
Gilles Peskinebef7f142018-07-12 17:22:21 +02004503 while( output_length != 0 )
4504 {
4505 /* Copy what remains of the current block */
4506 uint8_t n = hash_length - hkdf->offset_in_block;
4507 if( n > output_length )
4508 n = (uint8_t) output_length;
4509 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4510 output += n;
4511 output_length -= n;
4512 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004513 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004514 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004515 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004516 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004518 * object was corrupted or if this function is called directly
4519 * inside the library. */
4520 if( hkdf->block_number == 0xff )
4521 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004522
4523 /* We need a new block */
4524 ++hkdf->block_number;
4525 hkdf->offset_in_block = 0;
4526 status = psa_hmac_setup_internal( &hkdf->hmac,
4527 hkdf->prk, hash_length,
4528 hash_alg );
4529 if( status != PSA_SUCCESS )
4530 return( status );
4531 if( hkdf->block_number != 1 )
4532 {
4533 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4534 hkdf->output_block,
4535 hash_length );
4536 if( status != PSA_SUCCESS )
4537 return( status );
4538 }
4539 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4540 hkdf->info,
4541 hkdf->info_length );
4542 if( status != PSA_SUCCESS )
4543 return( status );
4544 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4545 &hkdf->block_number, 1 );
4546 if( status != PSA_SUCCESS )
4547 return( status );
4548 status = psa_hmac_finish_internal( &hkdf->hmac,
4549 hkdf->output_block,
4550 sizeof( hkdf->output_block ) );
4551 if( status != PSA_SUCCESS )
4552 return( status );
4553 }
4554
4555 return( PSA_SUCCESS );
4556}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004557
Janos Follath7742fee2019-06-17 12:58:10 +01004558static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4559 psa_tls12_prf_key_derivation_t *tls12_prf,
4560 psa_algorithm_t alg )
4561{
4562 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4563 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004564 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4565 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004566
Janos Follath7742fee2019-06-17 12:58:10 +01004567 /* We can't be wanting more output after block 0xff, otherwise
4568 * the capacity check in psa_key_derivation_output_bytes() would have
4569 * prevented this call. It could happen only if the operation
4570 * object was corrupted or if this function is called directly
4571 * inside the library. */
4572 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004573 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004574
4575 /* We need a new block */
4576 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004577 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004578
4579 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4580 *
4581 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4582 *
4583 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4584 * HMAC_hash(secret, A(2) + seed) +
4585 * HMAC_hash(secret, A(3) + seed) + ...
4586 *
4587 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004588 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004589 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004590 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004591 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004592 * is currently extracted as `output_block` and where i is
4593 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004594 */
4595
Janos Follathea29bfb2019-06-19 12:21:20 +01004596 /* Save the hash context before using it, to preserve the hash state with
4597 * only the inner padding in it. We need this, because inner padding depends
4598 * on the key (secret in the RFC's terminology). */
4599 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4600 if( status != PSA_SUCCESS )
4601 goto cleanup;
4602
4603 /* Calculate A(i) where i = tls12_prf->block_number. */
4604 if( tls12_prf->block_number == 1 )
4605 {
4606 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4607 * the variable seed and in this instance means it in the context of the
4608 * P_hash function, where seed = label + seed.) */
4609 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4610 tls12_prf->label, tls12_prf->label_length );
4611 if( status != PSA_SUCCESS )
4612 goto cleanup;
4613 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4614 tls12_prf->seed, tls12_prf->seed_length );
4615 if( status != PSA_SUCCESS )
4616 goto cleanup;
4617 }
4618 else
4619 {
4620 /* A(i) = HMAC_hash(secret, A(i-1)) */
4621 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4622 tls12_prf->Ai, hash_length );
4623 if( status != PSA_SUCCESS )
4624 goto cleanup;
4625 }
4626
4627 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4628 tls12_prf->Ai, hash_length );
4629 if( status != PSA_SUCCESS )
4630 goto cleanup;
4631 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4632 if( status != PSA_SUCCESS )
4633 goto cleanup;
4634
4635 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4636 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4637 tls12_prf->Ai, hash_length );
4638 if( status != PSA_SUCCESS )
4639 goto cleanup;
4640 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4641 tls12_prf->label, tls12_prf->label_length );
4642 if( status != PSA_SUCCESS )
4643 goto cleanup;
4644 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4645 tls12_prf->seed, tls12_prf->seed_length );
4646 if( status != PSA_SUCCESS )
4647 goto cleanup;
4648 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4649 tls12_prf->output_block, hash_length );
4650 if( status != PSA_SUCCESS )
4651 goto cleanup;
4652 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4653 if( status != PSA_SUCCESS )
4654 goto cleanup;
4655
Janos Follath7742fee2019-06-17 12:58:10 +01004656
4657cleanup:
4658
Janos Follathea29bfb2019-06-19 12:21:20 +01004659 cleanup_status = psa_hash_abort( &backup );
4660 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4661 status = cleanup_status;
4662
4663 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004664}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004665
Janos Follath844eb0e2019-06-19 12:10:49 +01004666static psa_status_t psa_key_derivation_tls12_prf_read(
4667 psa_tls12_prf_key_derivation_t *tls12_prf,
4668 psa_algorithm_t alg,
4669 uint8_t *output,
4670 size_t output_length )
4671{
4672 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4673 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4674 psa_status_t status;
4675 uint8_t offset, length;
4676
4677 while( output_length != 0 )
4678 {
4679 /* Check if we have fully processed the current block. */
4680 if( tls12_prf->left_in_block == 0 )
4681 {
4682 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4683 alg );
4684 if( status != PSA_SUCCESS )
4685 return( status );
4686
4687 continue;
4688 }
4689
4690 if( tls12_prf->left_in_block > output_length )
4691 length = (uint8_t) output_length;
4692 else
4693 length = tls12_prf->left_in_block;
4694
4695 offset = hash_length - tls12_prf->left_in_block;
4696 memcpy( output, tls12_prf->output_block + offset, length );
4697 output += length;
4698 output_length -= length;
4699 tls12_prf->left_in_block -= length;
4700 }
4701
4702 return( PSA_SUCCESS );
4703}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004704#endif /* MBEDTLS_MD_C */
4705
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004706psa_status_t psa_key_derivation_output_bytes(
4707 psa_key_derivation_operation_t *operation,
4708 uint8_t *output,
4709 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004710{
4711 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004715 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004717 return PSA_ERROR_BAD_STATE;
4718 }
4719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004723 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004725 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726 goto exit;
4727 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004731 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4733 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004736 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004739
Gilles Peskinebef7f142018-07-12 17:22:21 +02004740#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004741 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004742 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004743 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004745 output, output_length );
4746 }
Janos Follathadbec812019-06-14 11:05:39 +01004747 else
Janos Follathadbec812019-06-14 11:05:39 +01004748 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004750 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004752 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004753 output_length );
4754 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755 else
4756#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757 {
4758 return( PSA_ERROR_BAD_STATE );
4759 }
4760
4761exit:
4762 if( status != PSA_SUCCESS )
4763 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004764 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 * This allows us to differentiate between exhausted operations and
4766 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4767 * operations. */
4768 psa_algorithm_t alg = operation->alg;
4769 psa_key_derivation_abort( operation );
4770 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004771 memset( output, '!', output_length );
4772 }
4773 return( status );
4774}
4775
Gilles Peskine08542d82018-07-19 17:05:42 +02004776#if defined(MBEDTLS_DES_C)
4777static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4778{
4779 if( data_size >= 8 )
4780 mbedtls_des_key_set_parity( data );
4781 if( data_size >= 16 )
4782 mbedtls_des_key_set_parity( data + 8 );
4783 if( data_size >= 24 )
4784 mbedtls_des_key_set_parity( data + 16 );
4785}
4786#endif /* MBEDTLS_DES_C */
4787
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004788static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 psa_key_slot_t *slot,
4790 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792{
4793 uint8_t *data = NULL;
4794 size_t bytes = PSA_BITS_TO_BYTES( bits );
4795 psa_status_t status;
4796
Gilles Peskine8e338702019-07-30 20:06:31 +02004797 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004798 return( PSA_ERROR_INVALID_ARGUMENT );
4799 if( bits % 8 != 0 )
4800 return( PSA_ERROR_INVALID_ARGUMENT );
4801 data = mbedtls_calloc( 1, bytes );
4802 if( data == NULL )
4803 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 if( status != PSA_SUCCESS )
4807 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004808#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004809 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004810 psa_des_set_key_parity( data, bytes );
4811#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004812 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004813
4814exit:
4815 mbedtls_free( data );
4816 return( status );
4817}
4818
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004819psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004821 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822{
4823 psa_status_t status;
4824 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004825 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004826 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4827 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004828#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4829 if( driver != NULL )
4830 {
4831 /* Deriving a key in a secure element is not implemented yet. */
4832 status = PSA_ERROR_NOT_SUPPORTED;
4833 }
4834#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004835 if( status == PSA_SUCCESS )
4836 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004837 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004838 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004840 }
4841 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004842 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004843 if( status != PSA_SUCCESS )
4844 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004845 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004846 *handle = 0;
4847 }
4848 return( status );
4849}
4850
Gilles Peskineeab56e42018-07-12 17:12:33 +02004851
4852
4853/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004854/* Key derivation */
4855/****************************************************************/
4856
Gilles Peskine969c5d62019-01-16 15:53:06 +01004857static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004859 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004860{
Janos Follath5fe19732019-06-20 15:09:30 +01004861 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4862 * initialisers for this union leave some bytes unspecified.) */
4863 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4864
Gilles Peskine969c5d62019-01-16 15:53:06 +01004865 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004866#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004867 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4868 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4869 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004870 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004871 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004872 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4873 if( hash_size == 0 )
4874 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004875 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4876 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004877 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878 {
4879 return( PSA_ERROR_NOT_SUPPORTED );
4880 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883 }
4884#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004887}
4888
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 psa_algorithm_t alg )
4891{
4892 psa_status_t status;
4893
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004895 return( PSA_ERROR_BAD_STATE );
4896
Gilles Peskine6843c292019-01-18 16:44:49 +01004897 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4898 return( PSA_ERROR_INVALID_ARGUMENT );
4899 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900 {
4901 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903 }
4904 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4905 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908 else
4909 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910
4911 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914}
4915
4916#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004917static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004918 psa_algorithm_t hash_alg,
4919 psa_key_derivation_step_t step,
4920 const uint8_t *data,
4921 size_t data_length )
4922{
4923 psa_status_t status;
4924 switch( step )
4925 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004926 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004927 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004929 status = psa_hmac_setup_internal( &hkdf->hmac,
4930 data, data_length,
4931 hash_alg );
4932 if( status != PSA_SUCCESS )
4933 return( status );
4934 hkdf->state = HKDF_STATE_STARTED;
4935 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004936 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004937 /* If no salt was provided, use an empty salt. */
4938 if( hkdf->state == HKDF_STATE_INIT )
4939 {
4940 status = psa_hmac_setup_internal( &hkdf->hmac,
4941 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004942 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004943 if( status != PSA_SUCCESS )
4944 return( status );
4945 hkdf->state = HKDF_STATE_STARTED;
4946 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004947 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004948 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004949 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4950 data, data_length );
4951 if( status != PSA_SUCCESS )
4952 return( status );
4953 status = psa_hmac_finish_internal( &hkdf->hmac,
4954 hkdf->prk,
4955 sizeof( hkdf->prk ) );
4956 if( status != PSA_SUCCESS )
4957 return( status );
4958 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4959 hkdf->block_number = 0;
4960 hkdf->state = HKDF_STATE_KEYED;
4961 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004962 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004963 if( hkdf->state == HKDF_STATE_OUTPUT )
4964 return( PSA_ERROR_BAD_STATE );
4965 if( hkdf->info_set )
4966 return( PSA_ERROR_BAD_STATE );
4967 hkdf->info_length = data_length;
4968 if( data_length != 0 )
4969 {
4970 hkdf->info = mbedtls_calloc( 1, data_length );
4971 if( hkdf->info == NULL )
4972 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4973 memcpy( hkdf->info, data, data_length );
4974 }
4975 hkdf->info_set = 1;
4976 return( PSA_SUCCESS );
4977 default:
4978 return( PSA_ERROR_INVALID_ARGUMENT );
4979 }
4980}
Janos Follathb03233e2019-06-11 15:30:30 +01004981
Janos Follathf08e2652019-06-13 09:05:41 +01004982static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4983 const uint8_t *data,
4984 size_t data_length )
4985{
4986 if( prf->state != TLS12_PRF_STATE_INIT )
4987 return( PSA_ERROR_BAD_STATE );
4988
Janos Follathd6dce9f2019-07-04 09:11:38 +01004989 if( data_length != 0 )
4990 {
4991 prf->seed = mbedtls_calloc( 1, data_length );
4992 if( prf->seed == NULL )
4993 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004994
Janos Follathd6dce9f2019-07-04 09:11:38 +01004995 memcpy( prf->seed, data, data_length );
4996 prf->seed_length = data_length;
4997 }
Janos Follathf08e2652019-06-13 09:05:41 +01004998
4999 prf->state = TLS12_PRF_STATE_SEED_SET;
5000
5001 return( PSA_SUCCESS );
5002}
5003
Janos Follath81550542019-06-13 14:26:34 +01005004static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5005 psa_algorithm_t hash_alg,
5006 const uint8_t *data,
5007 size_t data_length )
5008{
5009 psa_status_t status;
5010 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5011 return( PSA_ERROR_BAD_STATE );
5012
5013 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5014 if( status != PSA_SUCCESS )
5015 return( status );
5016
5017 prf->state = TLS12_PRF_STATE_KEY_SET;
5018
5019 return( PSA_SUCCESS );
5020}
5021
Janos Follath6660f0e2019-06-17 08:44:03 +01005022static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5023 psa_tls12_prf_key_derivation_t *prf,
5024 psa_algorithm_t hash_alg,
5025 const uint8_t *data,
5026 size_t data_length )
5027{
5028 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005029 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5030 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005031
5032 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5033 return( PSA_ERROR_INVALID_ARGUMENT );
5034
5035 /* Quoting RFC 4279, Section 2:
5036 *
5037 * The premaster secret is formed as follows: if the PSK is N octets
5038 * long, concatenate a uint16 with the value N, N zero octets, a second
5039 * uint16 with the value N, and the PSK itself.
5040 */
5041
Janos Follath40e13932019-06-26 13:22:29 +01005042 *cur++ = ( data_length >> 8 ) & 0xff;
5043 *cur++ = ( data_length >> 0 ) & 0xff;
5044 memset( cur, 0, data_length );
5045 cur += data_length;
5046 *cur++ = pms[0];
5047 *cur++ = pms[1];
5048 memcpy( cur, data, data_length );
5049 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005050
Janos Follath40e13932019-06-26 13:22:29 +01005051 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005052
5053 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5054 return( status );
5055}
5056
Janos Follath63028dd2019-06-13 09:15:47 +01005057static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5058 const uint8_t *data,
5059 size_t data_length )
5060{
5061 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5062 return( PSA_ERROR_BAD_STATE );
5063
Janos Follathd6dce9f2019-07-04 09:11:38 +01005064 if( data_length != 0 )
5065 {
5066 prf->label = mbedtls_calloc( 1, data_length );
5067 if( prf->label == NULL )
5068 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005069
Janos Follathd6dce9f2019-07-04 09:11:38 +01005070 memcpy( prf->label, data, data_length );
5071 prf->label_length = data_length;
5072 }
Janos Follath63028dd2019-06-13 09:15:47 +01005073
5074 prf->state = TLS12_PRF_STATE_LABEL_SET;
5075
5076 return( PSA_SUCCESS );
5077}
5078
Janos Follathb03233e2019-06-11 15:30:30 +01005079static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5080 psa_algorithm_t hash_alg,
5081 psa_key_derivation_step_t step,
5082 const uint8_t *data,
5083 size_t data_length )
5084{
Janos Follathb03233e2019-06-11 15:30:30 +01005085 switch( step )
5086 {
Janos Follathf08e2652019-06-13 09:05:41 +01005087 case PSA_KEY_DERIVATION_INPUT_SEED:
5088 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005089 case PSA_KEY_DERIVATION_INPUT_SECRET:
5090 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005091 case PSA_KEY_DERIVATION_INPUT_LABEL:
5092 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005093 default:
5094 return( PSA_ERROR_INVALID_ARGUMENT );
5095 }
5096}
Janos Follath6660f0e2019-06-17 08:44:03 +01005097
5098static psa_status_t psa_tls12_prf_psk_to_ms_input(
5099 psa_tls12_prf_key_derivation_t *prf,
5100 psa_algorithm_t hash_alg,
5101 psa_key_derivation_step_t step,
5102 const uint8_t *data,
5103 size_t data_length )
5104{
5105 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005106 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005107 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5108 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005109 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005110
5111 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5112}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005113#endif /* MBEDTLS_MD_C */
5114
Janos Follathb80a94e2019-06-12 15:54:46 +01005115static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005117 psa_key_derivation_step_t step,
5118 const uint8_t *data,
5119 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120{
5121 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005122 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005123
5124#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005125 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005126 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005128 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005129 step, data, data_length );
5130 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005131 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132 {
Janos Follathb03233e2019-06-11 15:30:30 +01005133 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5134 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5135 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005136 }
5137 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5138 {
5139 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5140 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5141 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005142 }
5143 else
5144#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005147 return( PSA_ERROR_BAD_STATE );
5148 }
5149
5150 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005152 return( status );
5153}
5154
Janos Follath51f4a0f2019-06-14 11:35:55 +01005155psa_status_t psa_key_derivation_input_bytes(
5156 psa_key_derivation_operation_t *operation,
5157 psa_key_derivation_step_t step,
5158 const uint8_t *data,
5159 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005160{
Janos Follathc5621512019-06-14 11:27:57 +01005161 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5162 return( PSA_ERROR_INVALID_ARGUMENT );
5163
5164 return( psa_key_derivation_input_internal( operation, step,
5165 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005166}
5167
Janos Follath51f4a0f2019-06-14 11:35:55 +01005168psa_status_t psa_key_derivation_input_key(
5169 psa_key_derivation_operation_t *operation,
5170 psa_key_derivation_step_t step,
5171 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005172{
5173 psa_key_slot_t *slot;
5174 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005175 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005176 PSA_KEY_USAGE_DERIVE,
5177 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005178 if( status != PSA_SUCCESS )
5179 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005180 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181 return( PSA_ERROR_INVALID_ARGUMENT );
5182 /* Don't allow a key to be used as an input that is usually public.
5183 * This is debatable. It's ok from a cryptographic perspective to
5184 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005185 * the material should be dedicated to a particular input step,
5186 * otherwise this may allow the key to be used in an unintended way
5187 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005188 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005190 return( psa_key_derivation_input_internal( operation,
5191 step,
5192 slot->data.raw.data,
5193 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005195
5196
5197
5198/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005199/* Key agreement */
5200/****************************************************************/
5201
Gilles Peskinea05219c2018-11-16 16:02:56 +01005202#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005203static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5204 size_t peer_key_length,
5205 const mbedtls_ecp_keypair *our_key,
5206 uint8_t *shared_secret,
5207 size_t shared_secret_size,
5208 size_t *shared_secret_length )
5209{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005210 mbedtls_ecp_keypair *their_key = NULL;
5211 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005212 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005213 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005214
Jaeden Ameroccdce902019-01-10 11:42:27 +00005215 status = psa_import_ec_public_key(
5216 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5217 peer_key, peer_key_length,
5218 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005219 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005220 goto exit;
5221
Jaeden Amero97271b32019-01-10 19:38:51 +00005222 status = mbedtls_to_psa_error(
5223 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5224 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005226 status = mbedtls_to_psa_error(
5227 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5228 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005229 goto exit;
5230
Jaeden Amero97271b32019-01-10 19:38:51 +00005231 status = mbedtls_to_psa_error(
5232 mbedtls_ecdh_calc_secret( &ecdh,
5233 shared_secret_length,
5234 shared_secret, shared_secret_size,
5235 mbedtls_ctr_drbg_random,
5236 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005237
5238exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005239 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005240 mbedtls_ecp_keypair_free( their_key );
5241 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005242 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005243}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005244#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005245
Gilles Peskine01d718c2018-09-18 12:01:02 +02005246#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5247
Gilles Peskine0216fe12019-04-11 21:23:21 +02005248static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5249 psa_key_slot_t *private_key,
5250 const uint8_t *peer_key,
5251 size_t peer_key_length,
5252 uint8_t *shared_secret,
5253 size_t shared_secret_size,
5254 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005255{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005256 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005257 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005258#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005259 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005260 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005261 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005262 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5263 private_key->data.ecp,
5264 shared_secret, shared_secret_size,
5265 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005266#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005267 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005268 (void) private_key;
5269 (void) peer_key;
5270 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005271 (void) shared_secret;
5272 (void) shared_secret_size;
5273 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005274 return( PSA_ERROR_NOT_SUPPORTED );
5275 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005276}
5277
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005278/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005279 * to potentially free embedded data structures and wipe confidential data.
5280 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005282 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005283 psa_key_slot_t *private_key,
5284 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005285 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005286{
5287 psa_status_t status;
5288 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5289 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005291
5292 /* Step 1: run the secret agreement algorithm to generate the shared
5293 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005294 status = psa_key_agreement_raw_internal( ka_alg,
5295 private_key,
5296 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005297 shared_secret,
5298 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005299 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005300 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005301 goto exit;
5302
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005303 /* Step 2: set up the key derivation to generate key material from
5304 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005305 status = psa_key_derivation_input_internal( operation, step,
5306 shared_secret,
5307 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005308
Gilles Peskine12313cd2018-06-20 00:20:32 +02005309exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005310 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005311 return( status );
5312}
5313
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005315 psa_key_derivation_step_t step,
5316 psa_key_handle_t private_key,
5317 const uint8_t *peer_key,
5318 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005319{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005320 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005321 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005323 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005324 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005325 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005326 if( status != PSA_SUCCESS )
5327 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005329 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005330 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005331 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005333 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005334}
5335
Gilles Peskinebe697d82019-05-16 18:00:41 +02005336psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5337 psa_key_handle_t private_key,
5338 const uint8_t *peer_key,
5339 size_t peer_key_length,
5340 uint8_t *output,
5341 size_t output_size,
5342 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005343{
5344 psa_key_slot_t *slot;
5345 psa_status_t status;
5346
5347 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5348 {
5349 status = PSA_ERROR_INVALID_ARGUMENT;
5350 goto exit;
5351 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005352 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005353 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005354 if( status != PSA_SUCCESS )
5355 goto exit;
5356
5357 status = psa_key_agreement_raw_internal( alg, slot,
5358 peer_key, peer_key_length,
5359 output, output_size,
5360 output_length );
5361
5362exit:
5363 if( status != PSA_SUCCESS )
5364 {
5365 /* If an error happens and is not handled properly, the output
5366 * may be used as a key to protect sensitive data. Arrange for such
5367 * a key to be random, which is likely to result in decryption or
5368 * verification errors. This is better than filling the buffer with
5369 * some constant data such as zeros, which would result in the data
5370 * being protected with a reproducible, easily knowable key.
5371 */
5372 psa_generate_random( output, output_size );
5373 *output_length = output_size;
5374 }
5375 return( status );
5376}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005377
5378
5379/****************************************************************/
5380/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005381/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005382
Gilles Peskine4c317f42018-07-12 01:24:09 +02005383psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005384 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005385{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005386 int ret;
5387 GUARD_MODULE_INITIALIZED;
5388
Gilles Peskinef181eca2019-08-07 13:49:00 +02005389 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5390 {
5391 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5392 output,
5393 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5394 if( ret != 0 )
5395 return( mbedtls_to_psa_error( ret ) );
5396 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5397 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5398 }
5399
Gilles Peskinee59236f2018-01-27 23:32:46 +01005400 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5401 return( mbedtls_to_psa_error( ret ) );
5402}
5403
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005404#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5405#include "mbedtls/entropy_poll.h"
5406
Gilles Peskine7228da22019-07-15 11:06:15 +02005407psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005408 size_t seed_size )
5409{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005410 if( global_data.initialized )
5411 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005412
5413 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5414 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5415 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5416 return( PSA_ERROR_INVALID_ARGUMENT );
5417
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005418 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005419}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005420#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005421
Gilles Peskinee56e8782019-04-26 17:34:02 +02005422#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Michael Thomas863b5d62020-02-10 19:41:16 +00005423psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005424 size_t domain_parameters_size,
5425 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005426{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005427 size_t i;
5428 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005429
Gilles Peskinee56e8782019-04-26 17:34:02 +02005430 if( domain_parameters_size == 0 )
5431 {
5432 *exponent = 65537;
5433 return( PSA_SUCCESS );
5434 }
5435
5436 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5437 * support values that fit in a 32-bit integer, which is larger than
5438 * int on just about every platform anyway. */
5439 if( domain_parameters_size > sizeof( acc ) )
5440 return( PSA_ERROR_NOT_SUPPORTED );
5441 for( i = 0; i < domain_parameters_size; i++ )
5442 acc = ( acc << 8 ) | domain_parameters[i];
5443 if( acc > INT_MAX )
5444 return( PSA_ERROR_NOT_SUPPORTED );
5445 *exponent = acc;
5446 return( PSA_SUCCESS );
5447}
5448#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5449
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005450static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005451 psa_key_slot_t *slot, size_t bits,
5452 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005453{
Gilles Peskine8e338702019-07-30 20:06:31 +02005454 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005455
Gilles Peskinee56e8782019-04-26 17:34:02 +02005456 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005457 return( PSA_ERROR_INVALID_ARGUMENT );
5458
Gilles Peskinee59236f2018-01-27 23:32:46 +01005459 if( key_type_is_raw_bytes( type ) )
5460 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005461 psa_status_t status;
Michael Thomas1d573ec2020-01-21 04:02:54 +00005462 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5463 if( status != PSA_SUCCESS )
5464 return( status );
5465 status = psa_generate_random( slot->data.raw.data,
Michael Thomasa8ac0162020-01-23 02:55:53 +00005466 slot->data.raw.bytes );
Michael Thomas1d573ec2020-01-21 04:02:54 +00005467 if( status != PSA_SUCCESS )
5468 return( status );
Michael Thomasa8ac0162020-01-23 02:55:53 +00005469#if defined(MBEDTLS_DES_C)
Michael Thomas1d573ec2020-01-21 04:02:54 +00005470 if( type == PSA_KEY_TYPE_DES )
5471 psa_des_set_key_parity( slot->data.raw.data,
5472 slot->data.raw.bytes );
Michael Thomasa8ac0162020-01-23 02:55:53 +00005473#endif /* MBEDTLS_DES_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005474 }
5475 else
5476
5477#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005478 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479 {
5480 mbedtls_rsa_context *rsa;
5481 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005482 int exponent;
5483 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005484 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5485 return( PSA_ERROR_NOT_SUPPORTED );
5486 /* Accept only byte-aligned keys, for the same reasons as
5487 * in psa_import_rsa_key(). */
5488 if( bits % 8 != 0 )
5489 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005490 status = psa_read_rsa_exponent( domain_parameters,
5491 domain_parameters_size,
5492 &exponent );
5493 if( status != PSA_SUCCESS )
5494 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005495 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5496 if( rsa == NULL )
5497 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5498 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5499 ret = mbedtls_rsa_gen_key( rsa,
5500 mbedtls_ctr_drbg_random,
5501 &global_data.ctr_drbg,
5502 (unsigned int) bits,
5503 exponent );
5504 if( ret != 0 )
5505 {
5506 mbedtls_rsa_free( rsa );
5507 mbedtls_free( rsa );
5508 return( mbedtls_to_psa_error( ret ) );
5509 }
5510 slot->data.rsa = rsa;
5511 }
5512 else
5513#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5514
5515#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005516 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517 {
5518 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5519 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5520 const mbedtls_ecp_curve_info *curve_info =
5521 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5522 mbedtls_ecp_keypair *ecp;
5523 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005524 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005525 return( PSA_ERROR_NOT_SUPPORTED );
5526 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5527 return( PSA_ERROR_NOT_SUPPORTED );
5528 if( curve_info->bit_size != bits )
5529 return( PSA_ERROR_INVALID_ARGUMENT );
5530 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5531 if( ecp == NULL )
5532 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5533 mbedtls_ecp_keypair_init( ecp );
5534 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5535 mbedtls_ctr_drbg_random,
5536 &global_data.ctr_drbg );
5537 if( ret != 0 )
5538 {
5539 mbedtls_ecp_keypair_free( ecp );
5540 mbedtls_free( ecp );
5541 return( mbedtls_to_psa_error( ret ) );
5542 }
5543 slot->data.ecp = ecp;
5544 }
5545 else
5546#endif /* MBEDTLS_ECP_C */
5547
5548 return( PSA_ERROR_NOT_SUPPORTED );
5549
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005550 return( PSA_SUCCESS );
5551}
Michael Thomas4c53fc72020-01-24 03:45:51 +00005552
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005553psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005554 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005555{
5556 psa_status_t status;
5557 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005558 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005559
Gilles Peskinedf179142019-07-15 22:02:14 +02005560 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5561 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005562 if( status != PSA_SUCCESS )
5563 goto exit;
5564
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005565#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5566 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005567 {
Gilles Peskine11792082019-08-06 18:36:36 +02005568 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5569 size_t pubkey_length = 0; /* We don't support this feature yet */
5570 if( drv->key_management == NULL ||
5571 drv->key_management->p_generate == NULL )
5572 {
5573 status = PSA_ERROR_NOT_SUPPORTED;
5574 goto exit;
5575 }
5576 status = drv->key_management->p_generate(
5577 psa_get_se_driver_context( driver ),
5578 slot->data.se.slot_number, attributes,
5579 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005580 }
Gilles Peskine11792082019-08-06 18:36:36 +02005581 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005582#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas4c53fc72020-01-24 03:45:51 +00005583#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
Michael Thomas153cd2f2020-01-22 16:54:38 +00005584 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
Michael Thomas1d573ec2020-01-21 04:02:54 +00005585 {
5586 status = psa_generate_key_vendor(slot, attributes->core.bits,
5587 attributes->domain_parameters, attributes->domain_parameters_size);
5588 }
5589 else
Michael Thomas863b5d62020-02-10 19:41:16 +00005590#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005591 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005592 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005593 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005594 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005595 }
Gilles Peskine11792082019-08-06 18:36:36 +02005596exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005597 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005598 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005599 if( status != PSA_SUCCESS )
5600 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005601 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005602 *handle = 0;
5603 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005604 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005605}
5606
5607
Gilles Peskinee59236f2018-01-27 23:32:46 +01005608
5609/****************************************************************/
5610/* Module setup */
5611/****************************************************************/
5612
Gilles Peskine5e769522018-11-20 21:59:56 +01005613psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5614 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5615 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5616{
5617 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5618 return( PSA_ERROR_BAD_STATE );
5619 global_data.entropy_init = entropy_init;
5620 global_data.entropy_free = entropy_free;
5621 return( PSA_SUCCESS );
5622}
5623
Gilles Peskinee59236f2018-01-27 23:32:46 +01005624void mbedtls_psa_crypto_free( void )
5625{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005626 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005627 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5628 {
5629 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005630 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005631 }
5632 /* Wipe all remaining data, including configuration.
5633 * In particular, this sets all state indicator to the value
5634 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005635 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005636#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005637 /* Unregister all secure element drivers, so that we restart from
5638 * a pristine state. */
5639 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005640#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005641}
5642
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005643#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5644/** Recover a transaction that was interrupted by a power failure.
5645 *
5646 * This function is called during initialization, before psa_crypto_init()
5647 * returns. If this function returns a failure status, the initialization
5648 * fails.
5649 */
5650static psa_status_t psa_crypto_recover_transaction(
5651 const psa_crypto_transaction_t *transaction )
5652{
5653 switch( transaction->unknown.type )
5654 {
5655 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5656 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005657 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005658 * is implemented.
5659 * https://github.com/ARMmbed/mbed-crypto/issues/218
5660 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005661 default:
5662 /* We found an unsupported transaction in the storage.
5663 * We don't know what state the storage is in. Give up. */
5664 return( PSA_ERROR_STORAGE_FAILURE );
5665 }
5666}
5667#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5668
Gilles Peskinee59236f2018-01-27 23:32:46 +01005669psa_status_t psa_crypto_init( void )
5670{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005671 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005672 const unsigned char drbg_seed[] = "PSA";
5673
Gilles Peskinec6b69072018-11-20 21:42:52 +01005674 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675 if( global_data.initialized != 0 )
5676 return( PSA_SUCCESS );
5677
Gilles Peskine5e769522018-11-20 21:59:56 +01005678 /* Set default configuration if
5679 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5680 if( global_data.entropy_init == NULL )
5681 global_data.entropy_init = mbedtls_entropy_init;
5682 if( global_data.entropy_free == NULL )
5683 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005684
Gilles Peskinec6b69072018-11-20 21:42:52 +01005685 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005686 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005687#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5688 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5689 /* The PSA entropy injection feature depends on using NV seed as an entropy
5690 * source. Add NV seed as an entropy source for PSA entropy injection. */
5691 mbedtls_entropy_add_source( &global_data.entropy,
5692 mbedtls_nv_seed_poll, NULL,
5693 MBEDTLS_ENTROPY_BLOCK_SIZE,
5694 MBEDTLS_ENTROPY_SOURCE_STRONG );
5695#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005696 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005697 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005698 status = mbedtls_to_psa_error(
5699 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5700 mbedtls_entropy_func,
5701 &global_data.entropy,
5702 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5703 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005704 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005705 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005706
Gilles Peskine66fb1262018-12-10 16:29:04 +01005707 status = psa_initialize_key_slots( );
5708 if( status != PSA_SUCCESS )
5709 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005710
Gilles Peskine4b734222019-07-24 15:56:31 +02005711#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005712 status = psa_crypto_load_transaction( );
5713 if( status == PSA_SUCCESS )
5714 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005715 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5716 if( status != PSA_SUCCESS )
5717 goto exit;
5718 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005719 }
5720 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5721 {
5722 /* There's no transaction to complete. It's all good. */
5723 status = PSA_SUCCESS;
5724 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005725#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005726
Gilles Peskinec6b69072018-11-20 21:42:52 +01005727 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005728 global_data.initialized = 1;
5729
5730exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005731 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005732 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005733 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005734}
5735
5736#endif /* MBEDTLS_PSA_CRYPTO_C */