blob: 0853c149b96de26eb03e9c26a5cfe0dbcf1129af [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
Michael Thomas1ed98bd2020-02-18 22:11:30 +0000536psa_status_t psa_import_rsa_key( psa_key_type_t type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537 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{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000958 {
959 /* No key material to clean. */
960 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997/** Completely wipe a slot in memory, including its policy.
998 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100999psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000{
1001 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001002 /* Multipart operations may still be using the key. This is safe
1003 * because all multipart operation objects are independent from
1004 * the key slot: if they need to access the key after the setup
1005 * phase, they have a copy of the key. Note that this means that
1006 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007 /* At this point, key material and other type-specific content has
1008 * been wiped. Clear remaining metadata. We can call memset and not
1009 * zeroize because the metadata is not particularly sensitive. */
1010 memset( slot, 0, sizeof( *slot ) );
1011 return( status );
1012}
1013
Gilles Peskinec5487a82018-12-03 18:08:14 +01001014psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001016 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001017 psa_status_t status; /* status of the last operation */
1018 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1020 psa_se_drv_table_entry_t *driver;
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskinec5487a82018-12-03 18:08:14 +01001023 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 if( status != PSA_SUCCESS )
1025 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001026
1027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001030 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001031 /* For a key in a secure element, we need to do three things:
1032 * remove the key file in internal storage, destroy the
1033 * key inside the secure element, and update the driver's
1034 * persistent data. Start a transaction that will encompass these
1035 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 status = psa_crypto_save_transaction( );
1041 if( status != PSA_SUCCESS )
1042 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001043 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001044 /* We should still try to destroy the key in the secure
1045 * element and the key metadata in storage. This is especially
1046 * important if the error is that the storage is full.
1047 * But how to do it exactly without risking an inconsistent
1048 * state after a reset?
1049 * https://github.com/ARMmbed/mbed-crypto/issues/215
1050 */
1051 overall_status = status;
1052 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001053 }
1054
Gilles Peskine354f7672019-07-12 23:46:38 +02001055 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 if( overall_status == PSA_SUCCESS )
1057 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001058 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1060
Darryl Greend49a4992018-06-18 17:27:26 +01001061#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001062 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001063 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 status = psa_destroy_persistent_key( slot->attr.id );
1065 if( overall_status == PSA_SUCCESS )
1066 overall_status = status;
1067
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001068 /* TODO: other slots may have a copy of the same key. We should
1069 * invalidate them.
1070 * https://github.com/ARMmbed/mbed-crypto/issues/214
1071 */
Darryl Greend49a4992018-06-18 17:27:26 +01001072 }
1073#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001074
Gilles Peskinefc762652019-07-22 19:30:34 +02001075#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1076 if( driver != NULL )
1077 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001078 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001079 if( overall_status == PSA_SUCCESS )
1080 overall_status = status;
1081 status = psa_crypto_stop_transaction( );
1082 if( overall_status == PSA_SUCCESS )
1083 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001084 }
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1086
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088exit:
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001090 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1092 if( overall_status == PSA_SUCCESS )
1093 overall_status = status;
1094 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095}
1096
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001097void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001098{
Gilles Peskineb699f072019-04-26 16:06:02 +02001099 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001101}
1102
Gilles Peskineb699f072019-04-26 16:06:02 +02001103psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1104 psa_key_type_t type,
1105 const uint8_t *data,
1106 size_t data_length )
1107{
1108 uint8_t *copy = NULL;
1109
1110 if( data_length != 0 )
1111 {
1112 copy = mbedtls_calloc( 1, data_length );
1113 if( copy == NULL )
1114 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1115 memcpy( copy, data, data_length );
1116 }
1117 /* After this point, this function is guaranteed to succeed, so it
1118 * can start modifying `*attributes`. */
1119
1120 if( attributes->domain_parameters != NULL )
1121 {
1122 mbedtls_free( attributes->domain_parameters );
1123 attributes->domain_parameters = NULL;
1124 attributes->domain_parameters_size = 0;
1125 }
1126
1127 attributes->domain_parameters = copy;
1128 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001129 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001130 return( PSA_SUCCESS );
1131}
1132
1133psa_status_t psa_get_key_domain_parameters(
1134 const psa_key_attributes_t *attributes,
1135 uint8_t *data, size_t data_size, size_t *data_length )
1136{
1137 if( attributes->domain_parameters_size > data_size )
1138 return( PSA_ERROR_BUFFER_TOO_SMALL );
1139 *data_length = attributes->domain_parameters_size;
1140 if( attributes->domain_parameters_size != 0 )
1141 memcpy( data, attributes->domain_parameters,
1142 attributes->domain_parameters_size );
1143 return( PSA_SUCCESS );
1144}
1145
1146#if defined(MBEDTLS_RSA_C)
1147static psa_status_t psa_get_rsa_public_exponent(
1148 const mbedtls_rsa_context *rsa,
1149 psa_key_attributes_t *attributes )
1150{
1151 mbedtls_mpi mpi;
1152 int ret;
1153 uint8_t *buffer = NULL;
1154 size_t buflen;
1155 mbedtls_mpi_init( &mpi );
1156
1157 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1158 if( ret != 0 )
1159 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001160 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1161 {
1162 /* It's the default value, which is reported as an empty string,
1163 * so there's nothing to do. */
1164 goto exit;
1165 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001166
1167 buflen = mbedtls_mpi_size( &mpi );
1168 buffer = mbedtls_calloc( 1, buflen );
1169 if( buffer == NULL )
1170 {
1171 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1172 goto exit;
1173 }
1174 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1175 if( ret != 0 )
1176 goto exit;
1177 attributes->domain_parameters = buffer;
1178 attributes->domain_parameters_size = buflen;
1179
1180exit:
1181 mbedtls_mpi_free( &mpi );
1182 if( ret != 0 )
1183 mbedtls_free( buffer );
1184 return( mbedtls_to_psa_error( ret ) );
1185}
1186#endif /* MBEDTLS_RSA_C */
1187
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001188/** Retrieve all the publicly-accessible attributes of a key.
1189 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1191 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001193 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001194 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001196 psa_reset_key_attributes( attributes );
1197
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001198 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001199 if( status != PSA_SUCCESS )
1200 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001201
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001202 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001203 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1204 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1205
1206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1207 if( psa_key_slot_is_external( slot ) )
1208 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 {
1213#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001214 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001215 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001216#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001217 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001218 * is not yet implemented.
1219 * https://github.com/ARMmbed/mbed-crypto/issues/216
1220 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001221 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001222 break;
1223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1225 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001226#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 default:
1228 /* Nothing else to do. */
1229 break;
1230 }
1231
1232 if( status != PSA_SUCCESS )
1233 psa_reset_key_attributes( attributes );
1234 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235}
1236
Gilles Peskinec8000c02019-08-02 20:15:51 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1238psa_status_t psa_get_key_slot_number(
1239 const psa_key_attributes_t *attributes,
1240 psa_key_slot_number_t *slot_number )
1241{
1242 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1243 {
1244 *slot_number = attributes->slot_number;
1245 return( PSA_SUCCESS );
1246 }
1247 else
1248 return( PSA_ERROR_INVALID_ARGUMENT );
1249}
1250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1251
Jaeden Ameroccdce902019-01-10 11:42:27 +00001252#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001253static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1254 unsigned char *buf, size_t size )
1255{
1256 int ret;
1257 unsigned char *c;
1258 size_t len = 0;
1259
1260 c = buf + size;
1261
1262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1263
1264 return( (int) len );
1265}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001266#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001267
Gilles Peskinef603c712019-01-19 13:40:11 +01001268static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1269 uint8_t *data,
1270 size_t data_size,
1271 size_t *data_length,
1272 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001273{
Gilles Peskine5d309672019-07-12 23:47:28 +02001274#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1275 const psa_drv_se_t *drv;
1276 psa_drv_se_context_t *drv_context;
1277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1278
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001279 *data_length = 0;
1280
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001282 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001283
Gilles Peskine5d309672019-07-12 23:47:28 +02001284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001285 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001286 {
1287 psa_drv_se_export_key_t method;
1288 if( drv->key_management == NULL )
1289 return( PSA_ERROR_NOT_SUPPORTED );
1290 method = ( export_public_key ?
1291 drv->key_management->p_export_public :
1292 drv->key_management->p_export );
1293 if( method == NULL )
1294 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001295 return( method( drv_context,
1296 slot->data.se.slot_number,
1297 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001298 }
1299#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1300
Gilles Peskine8e338702019-07-30 20:06:31 +02001301 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302 {
1303 if( slot->data.raw.bytes > data_size )
1304 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001305 if( data_size != 0 )
1306 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001307 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001308 memset( data + slot->data.raw.bytes, 0,
1309 data_size - slot->data.raw.bytes );
1310 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311 *data_length = slot->data.raw.bytes;
1312 return( PSA_SUCCESS );
1313 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001314#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001315 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001317 psa_status_t status;
1318
Gilles Peskineb46bef22019-07-30 21:32:04 +02001319 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001320 if( bytes > data_size )
1321 return( PSA_ERROR_BUFFER_TOO_SMALL );
1322 status = mbedtls_to_psa_error(
1323 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1324 if( status != PSA_SUCCESS )
1325 return( status );
1326 memset( data + bytes, 0, data_size - bytes );
1327 *data_length = bytes;
1328 return( PSA_SUCCESS );
1329 }
1330#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331 else
Moran Peker17e36e12018-05-02 12:55:20 +03001332 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001333#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001334 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1335 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001336 {
Moran Pekera998bc62018-04-16 18:16:20 +03001337 mbedtls_pk_context pk;
1338 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001339 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001340 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#if defined(MBEDTLS_RSA_C)
1342 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001343 pk.pk_info = &mbedtls_rsa_info;
1344 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001345#else
1346 return( PSA_ERROR_NOT_SUPPORTED );
1347#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001348 }
1349 else
1350 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#if defined(MBEDTLS_ECP_C)
1352 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001353 pk.pk_info = &mbedtls_eckey_info;
1354 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001358 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001359 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001360 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001361 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 }
Moran Peker17e36e12018-05-02 12:55:20 +03001363 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001364 {
Moran Peker17e36e12018-05-02 12:55:20 +03001365 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001366 }
Moran Peker60364322018-04-29 11:34:58 +03001367 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001368 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001369 /* If data_size is 0 then data may be NULL and then the
1370 * call to memset would have undefined behavior. */
1371 if( data_size != 0 )
1372 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001373 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001374 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001375 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1376 * Move the data to the beginning and erase remaining data
1377 * at the original location. */
1378 if( 2 * (size_t) ret <= data_size )
1379 {
1380 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 }
1383 else if( (size_t) ret < data_size )
1384 {
1385 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001386 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001387 }
Moran Pekera998bc62018-04-16 18:16:20 +03001388 *data_length = ret;
1389 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001390 }
1391 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001392#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 {
1394 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001395 it is valid for a special-purpose implementation to omit
1396 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001397 return( PSA_ERROR_NOT_SUPPORTED );
1398 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001399 }
1400}
1401
Gilles Peskinec5487a82018-12-03 18:08:14 +01001402psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001403 uint8_t *data,
1404 size_t data_size,
1405 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001406{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001407 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001408 psa_status_t status;
1409
1410 /* Set the key to empty now, so that even when there are errors, we always
1411 * set data_length to a value between 0 and data_size. On error, setting
1412 * the key to empty is a good choice because an empty key representation is
1413 * unlikely to be accepted anywhere. */
1414 *data_length = 0;
1415
1416 /* Export requires the EXPORT flag. There is an exception for public keys,
1417 * which don't require any flag, but psa_get_key_from_slot takes
1418 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001419 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001420 if( status != PSA_SUCCESS )
1421 return( status );
1422 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001423 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001427 uint8_t *data,
1428 size_t data_size,
1429 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001430{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001431 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001432 psa_status_t status;
1433
1434 /* Set the key to empty now, so that even when there are errors, we always
1435 * set data_length to a value between 0 and data_size. On error, setting
1436 * the key to empty is a good choice because an empty key representation is
1437 * unlikely to be accepted anywhere. */
1438 *data_length = 0;
1439
1440 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001441 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001442 if( status != PSA_SUCCESS )
1443 return( status );
1444 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001445 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001446}
1447
Gilles Peskine91e8c332019-08-02 19:19:39 +02001448#if defined(static_assert)
1449static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1450 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001451static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001452 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001453static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001454 "One or more key attribute flag is listed as both internal-only and external-only" );
1455#endif
1456
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001457/** Validate that a key policy is internally well-formed.
1458 *
1459 * This function only rejects invalid policies. It does not validate the
1460 * consistency of the policy with respect to other attributes of the key
1461 * such as the key type.
1462 */
1463static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001464{
1465 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001466 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 PSA_KEY_USAGE_ENCRYPT |
1468 PSA_KEY_USAGE_DECRYPT |
1469 PSA_KEY_USAGE_SIGN |
1470 PSA_KEY_USAGE_VERIFY |
1471 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1472 return( PSA_ERROR_INVALID_ARGUMENT );
1473
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 return( PSA_SUCCESS );
1475}
1476
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001477/** Validate the internal consistency of key attributes.
1478 *
1479 * This function only rejects invalid attribute values. If does not
1480 * validate the consistency of the attributes with any key data that may
1481 * be involved in the creation of the key.
1482 *
1483 * Call this function early in the key creation process.
1484 *
1485 * \param[in] attributes Key attributes for the new key.
1486 * \param[out] p_drv On any return, the driver for the key, if any.
1487 * NULL for a transparent key.
1488 *
1489 */
1490static psa_status_t psa_validate_key_attributes(
1491 const psa_key_attributes_t *attributes,
1492 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001493{
1494 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001495
Michael Thomas153cd2f2020-01-22 16:54:38 +00001496 if( PSA_KEY_LIFETIME_IS_PERSISTENT (attributes->core.lifetime))
Darryl Green0c6575a2018-11-07 16:05:30 +00001497 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001498 status = psa_validate_persistent_key_parameters(
1499 attributes->core.lifetime, attributes->core.id,
1500 p_drv, 1 );
1501 if( status != PSA_SUCCESS )
1502 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001503 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001504
1505 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001506 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507 return( status );
1508
1509 /* Refuse to create overly large keys.
1510 * Note that this doesn't trigger on import if the attributes don't
1511 * explicitly specify a size (so psa_get_key_bits returns 0), so
1512 * psa_import_key() needs its own checks. */
1513 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1514 return( PSA_ERROR_NOT_SUPPORTED );
1515
Gilles Peskine91e8c332019-08-02 19:19:39 +02001516 /* Reject invalid flags. These should not be reachable through the API. */
1517 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1518 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1519 return( PSA_ERROR_INVALID_ARGUMENT );
1520
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001521 return( PSA_SUCCESS );
1522}
1523
Gilles Peskine4747d192019-04-17 15:05:45 +02001524/** Prepare a key slot to receive key material.
1525 *
1526 * This function allocates a key slot and sets its metadata.
1527 *
1528 * If this function fails, call psa_fail_key_creation().
1529 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001530 * This function is intended to be used as follows:
1531 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1532 * it with the specified attributes, and assign it a handle.
1533 * -# Populate the slot with the key material.
1534 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1535 * In case of failure at any step, stop the sequence and call
1536 * psa_fail_key_creation().
1537 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001538 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001539 * \param[in] attributes Key attributes for the new key.
1540 * \param[out] handle On success, a handle for the allocated slot.
1541 * \param[out] p_slot On success, a pointer to the prepared slot.
1542 * \param[out] p_drv On any return, the driver for the key, if any.
1543 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001544 *
1545 * \retval #PSA_SUCCESS
1546 * The key slot is ready to receive key material.
1547 * \return If this function fails, the key slot is an invalid state.
1548 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001549 */
1550static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001551 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001552 const psa_key_attributes_t *attributes,
1553 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001555 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001556{
1557 psa_status_t status;
1558 psa_key_slot_t *slot;
1559
Gilles Peskinedf179142019-07-15 22:02:14 +02001560 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001561 *p_drv = NULL;
1562
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001563 status = psa_validate_key_attributes( attributes, p_drv );
1564 if( status != PSA_SUCCESS )
1565 return( status );
1566
Gilles Peskineedbed562019-08-07 18:19:59 +02001567 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 if( status != PSA_SUCCESS )
1569 return( status );
1570 slot = *p_slot;
1571
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001572 /* We're storing the declared bit-size of the key. It's up to each
1573 * creation mechanism to verify that this information is correct.
1574 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001575 * an input (generate, device) but not for those where the bit-size
1576 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001577
1578 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001579
Gilles Peskine91e8c332019-08-02 19:19:39 +02001580 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001581 * external-only flags, query `attributes`. Thanks to the check
1582 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001583 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001584 * may have set. */
1585 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001586
Gilles Peskinecbaff462019-07-12 23:46:04 +02001587#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001588 /* For a key in a secure element, we need to do three things
1589 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001590 * create the key file in internal storage, create the
1591 * key inside the secure element, and update the driver's
1592 * persistent data. Start a transaction that will encompass these
1593 * three actions. */
1594 /* The first thing to do is to find a slot number for the new key.
1595 * We save the slot number in persistent storage as part of the
1596 * transaction data. It will be needed to recover if the power
1597 * fails during the key creation process, to clean up on the secure
1598 * element side after restarting. Obtaining a slot number from the
1599 * secure element driver updates its persistent state, but we do not yet
1600 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001601 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001602 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Darryl Green0c6575a2018-11-07 16:05:30 +00001603 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001604 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001605 &slot->data.se.slot_number );
1606 if( status != PSA_SUCCESS )
1607 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001609 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001610 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001611 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001612 status = psa_crypto_save_transaction( );
1613 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001614 {
1615 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001616 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001617 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001618 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001619#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1620
Darryl Green0c6575a2018-11-07 16:05:30 +00001621 return( status );
1622}
Gilles Peskine4747d192019-04-17 15:05:45 +02001623
1624/** Finalize the creation of a key once its key material has been set.
1625 *
1626 * This entails writing the key to persistent storage.
1627 *
1628 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001629 * See the documentation of psa_start_key_creation() for the intended use
1630 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001632 * \param[in,out] slot Pointer to the slot with key material.
1633 * \param[in] driver The secure element driver for the key,
1634 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001635 *
1636 * \retval #PSA_SUCCESS
1637 * The key was successfully created. The handle is now valid.
1638 * \return If this function fails, the key slot is an invalid state.
1639 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001640 */
Michael Thomas863b5d62020-02-10 19:41:16 +00001641psa_status_t psa_finish_key_creation(
Gilles Peskine011e4282019-06-26 18:34:38 +02001642 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001643 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001644{
1645 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001646 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001648
1649#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Michael Thomas58de7842020-02-26 17:30:52 +00001650 if (!PSA_KEY_LIFETIME_IS_VOLATILE(lifetime))
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001652#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1653 if( driver != NULL )
1654 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001655 psa_se_key_data_storage_t data;
1656#if defined(static_assert)
1657 static_assert( sizeof( slot->data.se.slot_number ) ==
1658 sizeof( data.slot_number ),
1659 "Slot number size does not match psa_se_key_data_storage_t" );
1660 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1661 "Bit-size size does not match psa_se_key_data_storage_t" );
1662#endif
1663 memcpy( &data.slot_number, &slot->data.se.slot_number,
1664 sizeof( slot->data.se.slot_number ) );
1665 memcpy( &data.bits, &slot->attr.bits,
1666 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001667 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001668 (uint8_t*) &data,
1669 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001670 }
1671 else
1672#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1673 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001674 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001675 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001676 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001677 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1678 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001679 if( buffer == NULL && buffer_size != 0 )
1680 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1681 status = psa_internal_export_key( slot,
1682 buffer, buffer_size, &length,
1683 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001684 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001685 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001686 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001687
Gilles Peskine1df83d42019-07-23 16:13:14 +02001688 if( buffer_size != 0 )
1689 mbedtls_platform_zeroize( buffer, buffer_size );
1690 mbedtls_free( buffer );
1691 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001693#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1694
Gilles Peskinecbaff462019-07-12 23:46:04 +02001695#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001696 /* Finish the transaction for a key creation. This does not
1697 * happen when registering an existing key. Detect this case
1698 * by checking whether a transaction is in progress (actual
1699 * creation of a key in a secure element requires a transaction,
1700 * but registration doesn't use one). */
1701 if( driver != NULL &&
1702 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703 {
1704 status = psa_save_se_persistent_data( driver );
1705 if( status != PSA_SUCCESS )
1706 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001707 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001708 return( status );
1709 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001710 status = psa_crypto_stop_transaction( );
1711 if( status != PSA_SUCCESS )
1712 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713 }
1714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1715
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 return( status );
1717}
1718
1719/** Abort the creation of a key.
1720 *
1721 * You may call this function after calling psa_start_key_creation(),
1722 * or after psa_finish_key_creation() fails. In other circumstances, this
1723 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001724 * See the documentation of psa_start_key_creation() for the intended use
1725 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in,out] slot Pointer to the slot with key material.
1728 * \param[in] driver The secure element driver for the key,
1729 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001731static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001732 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001733{
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 (void) driver;
1735
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 if( slot == NULL )
1737 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001738
1739#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001740 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * element, and the failure happened later (when saving metadata
1742 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001743 * element.
1744 * https://github.com/ARMmbed/mbed-crypto/issues/217
1745 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001746
Gilles Peskined7729582019-08-05 15:55:54 +02001747 /* Abort the ongoing transaction if any (there may not be one if
1748 * the creation process failed before starting one, or if the
1749 * key creation is a registration of a key in a secure element).
1750 * Earlier functions must already have done what it takes to undo any
1751 * partial creation. All that's left is to update the transaction data
1752 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001753 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1755
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 psa_wipe_key_slot( slot );
1757}
1758
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001759/** Validate optional attributes during key creation.
1760 *
1761 * Some key attributes are optional during key creation. If they are
1762 * specified in the attributes structure, check that they are consistent
1763 * with the data in the slot.
1764 *
1765 * This function should be called near the end of key creation, after
1766 * the slot in memory is fully populated but before saving persistent data.
1767 */
1768static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001769 const psa_key_slot_t *slot,
1770 const psa_key_attributes_t *attributes )
1771{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001772 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001774 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001775 return( PSA_ERROR_INVALID_ARGUMENT );
1776 }
1777
1778 if( attributes->domain_parameters_size != 0 )
1779 {
1780#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001782 {
1783 mbedtls_mpi actual, required;
1784 int ret;
1785 mbedtls_mpi_init( &actual );
1786 mbedtls_mpi_init( &required );
1787 ret = mbedtls_rsa_export( slot->data.rsa,
1788 NULL, NULL, NULL, NULL, &actual );
1789 if( ret != 0 )
1790 goto rsa_exit;
1791 ret = mbedtls_mpi_read_binary( &required,
1792 attributes->domain_parameters,
1793 attributes->domain_parameters_size );
1794 if( ret != 0 )
1795 goto rsa_exit;
1796 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1797 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1798 rsa_exit:
1799 mbedtls_mpi_free( &actual );
1800 mbedtls_mpi_free( &required );
1801 if( ret != 0)
1802 return( mbedtls_to_psa_error( ret ) );
1803 }
1804 else
1805#endif
1806 {
1807 return( PSA_ERROR_INVALID_ARGUMENT );
1808 }
1809 }
1810
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001811 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001813 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001814 return( PSA_ERROR_INVALID_ARGUMENT );
1815 }
1816
1817 return( PSA_SUCCESS );
1818}
1819
Gilles Peskine4747d192019-04-17 15:05:45 +02001820psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001822 size_t data_length,
1823 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001824{
1825 psa_status_t status;
1826 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001827 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828
Gilles Peskinedf179142019-07-15 22:02:14 +02001829 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1830 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 if( status != PSA_SUCCESS )
1832 goto exit;
1833
Gilles Peskine5d309672019-07-12 23:47:28 +02001834#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1835 if( driver != NULL )
1836 {
1837 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001838 /* The driver should set the number of key bits, however in
1839 * case it doesn't, we initialize bits to an invalid value. */
1840 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001841 if( drv->key_management == NULL ||
1842 drv->key_management->p_import == NULL )
1843 {
1844 status = PSA_ERROR_NOT_SUPPORTED;
1845 goto exit;
1846 }
1847 status = drv->key_management->p_import(
1848 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001849 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001850 &bits );
1851 if( status != PSA_SUCCESS )
1852 goto exit;
1853 if( bits > PSA_MAX_KEY_BITS )
1854 {
1855 status = PSA_ERROR_NOT_SUPPORTED;
1856 goto exit;
1857 }
1858 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001859 }
1860 else
1861#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas863b5d62020-02-10 19:41:16 +00001862#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
1863 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
1864 {
1865 status = psa_import_key_into_slot_vendor( slot, data, data_length);
1866 goto exit;
1867 }
1868 else
1869#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskine5d309672019-07-12 23:47:28 +02001870 {
1871 status = psa_import_key_into_slot( slot, data, data_length );
1872 if( status != PSA_SUCCESS )
1873 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001874 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001875 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001876 if( status != PSA_SUCCESS )
1877 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001878
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 if( status != PSA_SUCCESS )
1882 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001883 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001884 *handle = 0;
1885 }
1886 return( status );
1887}
1888
Gilles Peskined7729582019-08-05 15:55:54 +02001889#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1890psa_status_t mbedtls_psa_register_se_key(
1891 const psa_key_attributes_t *attributes )
1892{
1893 psa_status_t status;
1894 psa_key_slot_t *slot = NULL;
1895 psa_se_drv_table_entry_t *driver = NULL;
1896 const psa_drv_se_t *drv;
1897 psa_key_handle_t handle = 0;
1898
1899 /* Leaving attributes unspecified is not currently supported.
1900 * It could make sense to query the key type and size from the
1901 * secure element, but not all secure elements support this
1902 * and the driver HAL doesn't currently support it. */
1903 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1904 return( PSA_ERROR_NOT_SUPPORTED );
1905 if( psa_get_key_bits( attributes ) == 0 )
1906 return( PSA_ERROR_NOT_SUPPORTED );
1907
1908 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1909 &handle, &slot, &driver );
1910 if( status != PSA_SUCCESS )
1911 goto exit;
1912
1913 if( driver == NULL )
1914 {
1915 status = PSA_ERROR_INVALID_ARGUMENT;
1916 goto exit;
1917 }
1918 drv = psa_get_se_driver_methods( driver );
1919
1920 if ( psa_get_key_slot_number( attributes,
1921 &slot->data.se.slot_number ) != PSA_SUCCESS )
1922 {
1923 /* The application didn't specify a slot number. This doesn't
1924 * make sense when registering a slot. */
1925 status = PSA_ERROR_INVALID_ARGUMENT;
1926 goto exit;
1927 }
1928
Gilles Peskinea5f87492019-08-05 16:46:18 +02001929 /* If the driver has a slot number validation method, call it.
1930 * If it doesn't, it means the secure element is unable to validate
1931 * anything and so we have to trust the application. */
1932 if( drv->key_management != NULL &&
1933 drv->key_management->p_validate_slot_number != NULL )
1934 {
1935 status = drv->key_management->p_validate_slot_number(
1936 psa_get_se_driver_context( driver ),
1937 attributes,
1938 PSA_KEY_CREATION_REGISTER,
1939 slot->data.se.slot_number );
1940 if( status != PSA_SUCCESS )
1941 goto exit;
1942 }
1943
Gilles Peskined7729582019-08-05 15:55:54 +02001944 status = psa_finish_key_creation( slot, driver );
1945
1946exit:
1947 if( status != PSA_SUCCESS )
1948 {
1949 psa_fail_key_creation( slot, driver );
1950 }
1951 /* Registration doesn't keep the key in RAM. */
1952 psa_close_key( handle );
1953 return( status );
1954}
1955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1956
Gilles Peskinef603c712019-01-19 13:40:11 +01001957static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001958 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001959{
1960 psa_status_t status;
1961 uint8_t *buffer = NULL;
1962 size_t buffer_size = 0;
1963 size_t length;
1964
Gilles Peskine8e338702019-07-30 20:06:31 +02001965 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001966 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001967 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001968 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001969 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1971 if( status != PSA_SUCCESS )
1972 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001973 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001975
1976exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001977 if( buffer_size != 0 )
1978 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001979 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001980 return( status );
1981}
1982
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001983psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1984 const psa_key_attributes_t *specified_attributes,
1985 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001986{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001987 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001988 psa_key_slot_t *source_slot = NULL;
1989 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001991 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001992
Gilles Peskine28f8f302019-07-24 13:30:31 +02001993 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001994 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001995 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001996 goto exit;
1997
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001998 status = psa_validate_optional_attributes( source_slot,
1999 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000 if( status != PSA_SUCCESS )
2001 goto exit;
2002
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002003 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002004 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 if( status != PSA_SUCCESS )
2006 goto exit;
2007
Gilles Peskinedf179142019-07-15 22:02:14 +02002008 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2009 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002010 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 if( status != PSA_SUCCESS )
2012 goto exit;
2013
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2015 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002016 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002017 /* Copying to a secure element is not implemented yet. */
2018 status = PSA_ERROR_NOT_SUPPORTED;
2019 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002020 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002022
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002023 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002024 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002026
Gilles Peskine011e4282019-06-26 18:34:38 +02002027 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002028exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002029 if( status != PSA_SUCCESS )
2030 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002031 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002032 *target_handle = 0;
2033 }
2034 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002035}
2036
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002037
2038
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002039/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002040/* Message digests */
2041/****************************************************************/
2042
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002043static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002044{
2045 switch( alg )
2046 {
2047#if defined(MBEDTLS_MD2_C)
2048 case PSA_ALG_MD2:
2049 return( &mbedtls_md2_info );
2050#endif
2051#if defined(MBEDTLS_MD4_C)
2052 case PSA_ALG_MD4:
2053 return( &mbedtls_md4_info );
2054#endif
2055#if defined(MBEDTLS_MD5_C)
2056 case PSA_ALG_MD5:
2057 return( &mbedtls_md5_info );
2058#endif
2059#if defined(MBEDTLS_RIPEMD160_C)
2060 case PSA_ALG_RIPEMD160:
2061 return( &mbedtls_ripemd160_info );
2062#endif
2063#if defined(MBEDTLS_SHA1_C)
2064 case PSA_ALG_SHA_1:
2065 return( &mbedtls_sha1_info );
2066#endif
2067#if defined(MBEDTLS_SHA256_C)
2068 case PSA_ALG_SHA_224:
2069 return( &mbedtls_sha224_info );
2070 case PSA_ALG_SHA_256:
2071 return( &mbedtls_sha256_info );
2072#endif
2073#if defined(MBEDTLS_SHA512_C)
2074 case PSA_ALG_SHA_384:
2075 return( &mbedtls_sha384_info );
2076 case PSA_ALG_SHA_512:
2077 return( &mbedtls_sha512_info );
2078#endif
2079 default:
2080 return( NULL );
2081 }
2082}
2083
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002084psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2085{
2086 switch( operation->alg )
2087 {
Gilles Peskine81736312018-06-26 15:04:31 +02002088 case 0:
2089 /* The object has (apparently) been initialized but it is not
2090 * in use. It's ok to call abort on such an object, and there's
2091 * nothing to do. */
2092 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002093#if defined(MBEDTLS_MD2_C)
2094 case PSA_ALG_MD2:
2095 mbedtls_md2_free( &operation->ctx.md2 );
2096 break;
2097#endif
2098#if defined(MBEDTLS_MD4_C)
2099 case PSA_ALG_MD4:
2100 mbedtls_md4_free( &operation->ctx.md4 );
2101 break;
2102#endif
2103#if defined(MBEDTLS_MD5_C)
2104 case PSA_ALG_MD5:
2105 mbedtls_md5_free( &operation->ctx.md5 );
2106 break;
2107#endif
2108#if defined(MBEDTLS_RIPEMD160_C)
2109 case PSA_ALG_RIPEMD160:
2110 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2111 break;
2112#endif
2113#if defined(MBEDTLS_SHA1_C)
2114 case PSA_ALG_SHA_1:
2115 mbedtls_sha1_free( &operation->ctx.sha1 );
2116 break;
2117#endif
2118#if defined(MBEDTLS_SHA256_C)
2119 case PSA_ALG_SHA_224:
2120 case PSA_ALG_SHA_256:
2121 mbedtls_sha256_free( &operation->ctx.sha256 );
2122 break;
2123#endif
2124#if defined(MBEDTLS_SHA512_C)
2125 case PSA_ALG_SHA_384:
2126 case PSA_ALG_SHA_512:
2127 mbedtls_sha512_free( &operation->ctx.sha512 );
2128 break;
2129#endif
2130 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002131 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002132 }
2133 operation->alg = 0;
2134 return( PSA_SUCCESS );
2135}
2136
Gilles Peskineda8191d2018-07-08 19:46:38 +02002137psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002138 psa_algorithm_t alg )
2139{
2140 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002141
2142 /* A context must be freshly initialized before it can be set up. */
2143 if( operation->alg != 0 )
2144 {
2145 return( PSA_ERROR_BAD_STATE );
2146 }
2147
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002148 switch( alg )
2149 {
2150#if defined(MBEDTLS_MD2_C)
2151 case PSA_ALG_MD2:
2152 mbedtls_md2_init( &operation->ctx.md2 );
2153 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2154 break;
2155#endif
2156#if defined(MBEDTLS_MD4_C)
2157 case PSA_ALG_MD4:
2158 mbedtls_md4_init( &operation->ctx.md4 );
2159 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2160 break;
2161#endif
2162#if defined(MBEDTLS_MD5_C)
2163 case PSA_ALG_MD5:
2164 mbedtls_md5_init( &operation->ctx.md5 );
2165 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2166 break;
2167#endif
2168#if defined(MBEDTLS_RIPEMD160_C)
2169 case PSA_ALG_RIPEMD160:
2170 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2171 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2172 break;
2173#endif
2174#if defined(MBEDTLS_SHA1_C)
2175 case PSA_ALG_SHA_1:
2176 mbedtls_sha1_init( &operation->ctx.sha1 );
2177 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2178 break;
2179#endif
2180#if defined(MBEDTLS_SHA256_C)
2181 case PSA_ALG_SHA_224:
2182 mbedtls_sha256_init( &operation->ctx.sha256 );
2183 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2184 break;
2185 case PSA_ALG_SHA_256:
2186 mbedtls_sha256_init( &operation->ctx.sha256 );
2187 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2188 break;
2189#endif
2190#if defined(MBEDTLS_SHA512_C)
2191 case PSA_ALG_SHA_384:
2192 mbedtls_sha512_init( &operation->ctx.sha512 );
2193 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2194 break;
2195 case PSA_ALG_SHA_512:
2196 mbedtls_sha512_init( &operation->ctx.sha512 );
2197 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2198 break;
2199#endif
2200 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002201 return( PSA_ALG_IS_HASH( alg ) ?
2202 PSA_ERROR_NOT_SUPPORTED :
2203 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204 }
2205 if( ret == 0 )
2206 operation->alg = alg;
2207 else
2208 psa_hash_abort( operation );
2209 return( mbedtls_to_psa_error( ret ) );
2210}
2211
2212psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2213 const uint8_t *input,
2214 size_t input_length )
2215{
2216 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002217
2218 /* Don't require hash implementations to behave correctly on a
2219 * zero-length input, which may have an invalid pointer. */
2220 if( input_length == 0 )
2221 return( PSA_SUCCESS );
2222
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223 switch( operation->alg )
2224 {
2225#if defined(MBEDTLS_MD2_C)
2226 case PSA_ALG_MD2:
2227 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2228 input, input_length );
2229 break;
2230#endif
2231#if defined(MBEDTLS_MD4_C)
2232 case PSA_ALG_MD4:
2233 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2234 input, input_length );
2235 break;
2236#endif
2237#if defined(MBEDTLS_MD5_C)
2238 case PSA_ALG_MD5:
2239 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2240 input, input_length );
2241 break;
2242#endif
2243#if defined(MBEDTLS_RIPEMD160_C)
2244 case PSA_ALG_RIPEMD160:
2245 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2246 input, input_length );
2247 break;
2248#endif
2249#if defined(MBEDTLS_SHA1_C)
2250 case PSA_ALG_SHA_1:
2251 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2252 input, input_length );
2253 break;
2254#endif
2255#if defined(MBEDTLS_SHA256_C)
2256 case PSA_ALG_SHA_224:
2257 case PSA_ALG_SHA_256:
2258 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2259 input, input_length );
2260 break;
2261#endif
2262#if defined(MBEDTLS_SHA512_C)
2263 case PSA_ALG_SHA_384:
2264 case PSA_ALG_SHA_512:
2265 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2266 input, input_length );
2267 break;
2268#endif
2269 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002270 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002272
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 if( ret != 0 )
2274 psa_hash_abort( operation );
2275 return( mbedtls_to_psa_error( ret ) );
2276}
2277
2278psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2279 uint8_t *hash,
2280 size_t hash_size,
2281 size_t *hash_length )
2282{
itayzafrir40835d42018-08-02 13:14:17 +03002283 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002285 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
2287 /* Fill the output buffer with something that isn't a valid hash
2288 * (barring an attack on the hash and deliberately-crafted input),
2289 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002290 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002291 /* If hash_size is 0 then hash may be NULL and then the
2292 * call to memset would have undefined behavior. */
2293 if( hash_size != 0 )
2294 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295
2296 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002297 {
2298 status = PSA_ERROR_BUFFER_TOO_SMALL;
2299 goto exit;
2300 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301
2302 switch( operation->alg )
2303 {
2304#if defined(MBEDTLS_MD2_C)
2305 case PSA_ALG_MD2:
2306 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2307 break;
2308#endif
2309#if defined(MBEDTLS_MD4_C)
2310 case PSA_ALG_MD4:
2311 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2312 break;
2313#endif
2314#if defined(MBEDTLS_MD5_C)
2315 case PSA_ALG_MD5:
2316 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2317 break;
2318#endif
2319#if defined(MBEDTLS_RIPEMD160_C)
2320 case PSA_ALG_RIPEMD160:
2321 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA1_C)
2325 case PSA_ALG_SHA_1:
2326 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2327 break;
2328#endif
2329#if defined(MBEDTLS_SHA256_C)
2330 case PSA_ALG_SHA_224:
2331 case PSA_ALG_SHA_256:
2332 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2333 break;
2334#endif
2335#if defined(MBEDTLS_SHA512_C)
2336 case PSA_ALG_SHA_384:
2337 case PSA_ALG_SHA_512:
2338 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2339 break;
2340#endif
2341 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002342 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 }
itayzafrir40835d42018-08-02 13:14:17 +03002344 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345
itayzafrir40835d42018-08-02 13:14:17 +03002346exit:
2347 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002349 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 return( psa_hash_abort( operation ) );
2351 }
2352 else
2353 {
2354 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002355 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 }
2357}
2358
Gilles Peskine2d277862018-06-18 15:41:12 +02002359psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2360 const uint8_t *hash,
2361 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362{
2363 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2364 size_t actual_hash_length;
2365 psa_status_t status = psa_hash_finish( operation,
2366 actual_hash, sizeof( actual_hash ),
2367 &actual_hash_length );
2368 if( status != PSA_SUCCESS )
2369 return( status );
2370 if( actual_hash_length != hash_length )
2371 return( PSA_ERROR_INVALID_SIGNATURE );
2372 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2373 return( PSA_ERROR_INVALID_SIGNATURE );
2374 return( PSA_SUCCESS );
2375}
2376
Gilles Peskineeb35d782019-01-22 17:56:16 +01002377psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2378 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002379{
2380 if( target_operation->alg != 0 )
2381 return( PSA_ERROR_BAD_STATE );
2382
2383 switch( source_operation->alg )
2384 {
2385 case 0:
2386 return( PSA_ERROR_BAD_STATE );
2387#if defined(MBEDTLS_MD2_C)
2388 case PSA_ALG_MD2:
2389 mbedtls_md2_clone( &target_operation->ctx.md2,
2390 &source_operation->ctx.md2 );
2391 break;
2392#endif
2393#if defined(MBEDTLS_MD4_C)
2394 case PSA_ALG_MD4:
2395 mbedtls_md4_clone( &target_operation->ctx.md4,
2396 &source_operation->ctx.md4 );
2397 break;
2398#endif
2399#if defined(MBEDTLS_MD5_C)
2400 case PSA_ALG_MD5:
2401 mbedtls_md5_clone( &target_operation->ctx.md5,
2402 &source_operation->ctx.md5 );
2403 break;
2404#endif
2405#if defined(MBEDTLS_RIPEMD160_C)
2406 case PSA_ALG_RIPEMD160:
2407 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2408 &source_operation->ctx.ripemd160 );
2409 break;
2410#endif
2411#if defined(MBEDTLS_SHA1_C)
2412 case PSA_ALG_SHA_1:
2413 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2414 &source_operation->ctx.sha1 );
2415 break;
2416#endif
2417#if defined(MBEDTLS_SHA256_C)
2418 case PSA_ALG_SHA_224:
2419 case PSA_ALG_SHA_256:
2420 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2421 &source_operation->ctx.sha256 );
2422 break;
2423#endif
2424#if defined(MBEDTLS_SHA512_C)
2425 case PSA_ALG_SHA_384:
2426 case PSA_ALG_SHA_512:
2427 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2428 &source_operation->ctx.sha512 );
2429 break;
2430#endif
2431 default:
2432 return( PSA_ERROR_NOT_SUPPORTED );
2433 }
2434
2435 target_operation->alg = source_operation->alg;
2436 return( PSA_SUCCESS );
2437}
Michael Thomas4c53fc72020-01-24 03:45:51 +00002438
2439
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002440/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441/* MAC */
2442/****************************************************************/
2443
Michael Thomas863b5d62020-02-10 19:41:16 +00002444const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445 psa_algorithm_t alg,
2446 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002447 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002448 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002449{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002451 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002453 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002454 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002455
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2457 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002458 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002460 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002461 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462 mode = MBEDTLS_MODE_STREAM;
2463 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464 case PSA_ALG_CTR:
2465 mode = MBEDTLS_MODE_CTR;
2466 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002467 case PSA_ALG_CFB:
2468 mode = MBEDTLS_MODE_CFB;
2469 break;
2470 case PSA_ALG_OFB:
2471 mode = MBEDTLS_MODE_OFB;
2472 break;
2473 case PSA_ALG_CBC_NO_PADDING:
2474 mode = MBEDTLS_MODE_CBC;
2475 break;
2476 case PSA_ALG_CBC_PKCS7:
2477 mode = MBEDTLS_MODE_CBC;
2478 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002479 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 mode = MBEDTLS_MODE_CCM;
2481 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002482 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 mode = MBEDTLS_MODE_GCM;
2484 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002485 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2486 mode = MBEDTLS_MODE_CHACHAPOLY;
2487 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 default:
2489 return( NULL );
2490 }
2491 }
2492 else if( alg == PSA_ALG_CMAC )
2493 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 else
2495 return( NULL );
2496
2497 switch( key_type )
2498 {
2499 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002500 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 break;
2502 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002503 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2504 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002506 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002508 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002509 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2510 * but two-key Triple-DES is functionally three-key Triple-DES
2511 * with K1=K3, so that's how we present it to mbedtls. */
2512 if( key_bits == 128 )
2513 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 break;
2515 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002516 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517 break;
2518 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002519 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002521 case PSA_KEY_TYPE_CHACHA20:
2522 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2523 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 default:
2525 return( NULL );
2526 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002527 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002528 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529
Jaeden Amero23bbb752018-06-26 14:16:54 +01002530 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2531 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532}
2533
Gilles Peskinea05219c2018-11-16 16:02:56 +01002534#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002535static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002536{
Gilles Peskine2d277862018-06-18 15:41:12 +02002537 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002538 {
2539 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002540 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002541 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002542 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002543 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002544 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002545 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002546 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002547 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002548 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002549 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002550 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002551 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002552 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002553 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002554 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002555 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002556 return( 128 );
2557 default:
2558 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002559 }
2560}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002561#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002562
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563/* Initialize the MAC operation structure. Once this function has been
2564 * called, psa_mac_abort can run and will do the right thing. */
2565static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2566 psa_algorithm_t alg )
2567{
2568 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2569
2570 operation->alg = alg;
2571 operation->key_set = 0;
2572 operation->iv_set = 0;
2573 operation->iv_required = 0;
2574 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002575 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002576
2577#if defined(MBEDTLS_CMAC_C)
2578 if( alg == PSA_ALG_CMAC )
2579 {
2580 operation->iv_required = 0;
2581 mbedtls_cipher_init( &operation->ctx.cmac );
2582 status = PSA_SUCCESS;
2583 }
2584 else
2585#endif /* MBEDTLS_CMAC_C */
2586#if defined(MBEDTLS_MD_C)
2587 if( PSA_ALG_IS_HMAC( operation->alg ) )
2588 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002589 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2590 operation->ctx.hmac.hash_ctx.alg = 0;
2591 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592 }
2593 else
2594#endif /* MBEDTLS_MD_C */
2595 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002596 if( ! PSA_ALG_IS_MAC( alg ) )
2597 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002598 }
2599
2600 if( status != PSA_SUCCESS )
2601 memset( operation, 0, sizeof( *operation ) );
2602 return( status );
2603}
2604
Gilles Peskine01126fa2018-07-12 17:04:55 +02002605#if defined(MBEDTLS_MD_C)
2606static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2607{
Gilles Peskine3f108122018-12-07 18:14:53 +01002608 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002609 return( psa_hash_abort( &hmac->hash_ctx ) );
2610}
2611#endif /* MBEDTLS_MD_C */
2612
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2614{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002615 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002617 /* The object has (apparently) been initialized but it is not
2618 * in use. It's ok to call abort on such an object, and there's
2619 * nothing to do. */
2620 return( PSA_SUCCESS );
2621 }
2622 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002624 if( operation->alg == PSA_ALG_CMAC )
2625 {
2626 mbedtls_cipher_free( &operation->ctx.cmac );
2627 }
2628 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002631 if( PSA_ALG_IS_HMAC( operation->alg ) )
2632 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002633 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002634 }
2635 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002637 {
2638 /* Sanity check (shouldn't happen: operation->alg should
2639 * always have been initialized to a valid value). */
2640 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641 }
Moran Peker41deec42018-04-04 15:43:05 +03002642
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643 operation->alg = 0;
2644 operation->key_set = 0;
2645 operation->iv_set = 0;
2646 operation->iv_required = 0;
2647 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002648 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002649
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002651
2652bad_state:
2653 /* If abort is called on an uninitialized object, we can't trust
2654 * anything. Wipe the object in case it contains confidential data.
2655 * This may result in a memory leak if a pointer gets overwritten,
2656 * but it's too late to do anything about this. */
2657 memset( operation, 0, sizeof( *operation ) );
2658 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659}
2660
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002661#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002662static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002663 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002664 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002665 const mbedtls_cipher_info_t *cipher_info )
2666{
2667 int ret;
2668
2669 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002670
2671 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2672 if( ret != 0 )
2673 return( ret );
2674
2675 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2676 slot->data.raw.data,
2677 key_bits );
2678 return( ret );
2679}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002680#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681
Gilles Peskine248051a2018-06-20 16:09:38 +02002682#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2684 const uint8_t *key,
2685 size_t key_length,
2686 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002687{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002688 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002690 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002691 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002692 psa_status_t status;
2693
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002694 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2695 * overflow below. This should never trigger if the hash algorithm
2696 * is implemented correctly. */
2697 /* The size checks against the ipad and opad buffers cannot be written
2698 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2699 * because that triggers -Wlogical-op on GCC 7.3. */
2700 if( block_size > sizeof( ipad ) )
2701 return( PSA_ERROR_NOT_SUPPORTED );
2702 if( block_size > sizeof( hmac->opad ) )
2703 return( PSA_ERROR_NOT_SUPPORTED );
2704 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002705 return( PSA_ERROR_NOT_SUPPORTED );
2706
Gilles Peskined223b522018-06-11 18:12:58 +02002707 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002709 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002711 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002712 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002714 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002715 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002716 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002718 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719 }
Gilles Peskine96889972018-07-12 17:07:03 +02002720 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2721 * but it is permitted. It is common when HMAC is used in HKDF, for
2722 * example. Don't call `memcpy` in the 0-length because `key` could be
2723 * an invalid pointer which would make the behavior undefined. */
2724 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002725 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002726
Gilles Peskined223b522018-06-11 18:12:58 +02002727 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2728 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002730 ipad[i] ^= 0x36;
2731 memset( ipad + key_length, 0x36, block_size - key_length );
2732
2733 /* Copy the key material from ipad to opad, flipping the requisite bits,
2734 * and filling the rest of opad with the requisite constant. */
2735 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002736 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2737 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002738
Gilles Peskine01126fa2018-07-12 17:04:55 +02002739 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002741 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002742
Gilles Peskine01126fa2018-07-12 17:04:55 +02002743 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002744
2745cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002746 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002747
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 return( status );
2749}
Gilles Peskine248051a2018-06-20 16:09:38 +02002750#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751
Gilles Peskine89167cb2018-07-08 20:12:23 +02002752static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002753 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002754 psa_algorithm_t alg,
2755 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002758 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002760 psa_key_usage_t usage =
2761 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002762 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002763 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002765 /* A context must be freshly initialized before it can be set up. */
2766 if( operation->alg != 0 )
2767 {
2768 return( PSA_ERROR_BAD_STATE );
2769 }
2770
Gilles Peskined911eb72018-08-14 15:18:45 +02002771 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002772 if( status != PSA_SUCCESS )
2773 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002774 if( is_sign )
2775 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776
Gilles Peskine28f8f302019-07-24 13:30:31 +02002777 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002778 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002779 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002780 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002781
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002783 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002784 {
2785 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002786 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002787 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002788 int ret;
2789 if( cipher_info == NULL )
2790 {
2791 status = PSA_ERROR_NOT_SUPPORTED;
2792 goto exit;
2793 }
2794 operation->mac_size = cipher_info->block_size;
2795 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2796 status = mbedtls_to_psa_error( ret );
2797 }
2798 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002801 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002802 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002803 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002804 if( hash_alg == 0 )
2805 {
2806 status = PSA_ERROR_NOT_SUPPORTED;
2807 goto exit;
2808 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002809
2810 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2811 /* Sanity check. This shouldn't fail on a valid configuration. */
2812 if( operation->mac_size == 0 ||
2813 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2814 {
2815 status = PSA_ERROR_NOT_SUPPORTED;
2816 goto exit;
2817 }
2818
Gilles Peskine8e338702019-07-30 20:06:31 +02002819 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002820 {
2821 status = PSA_ERROR_INVALID_ARGUMENT;
2822 goto exit;
2823 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002824
Gilles Peskine01126fa2018-07-12 17:04:55 +02002825 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2826 slot->data.raw.data,
2827 slot->data.raw.bytes,
2828 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002829 }
2830 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002832 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002833 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002834 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002836
Gilles Peskined911eb72018-08-14 15:18:45 +02002837 if( truncated == 0 )
2838 {
2839 /* The "normal" case: untruncated algorithm. Nothing to do. */
2840 }
2841 else if( truncated < 4 )
2842 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002843 /* A very short MAC is too short for security since it can be
2844 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2845 * so we make this our minimum, even though 32 bits is still
2846 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002847 status = PSA_ERROR_NOT_SUPPORTED;
2848 }
2849 else if( truncated > operation->mac_size )
2850 {
2851 /* It's impossible to "truncate" to a larger length. */
2852 status = PSA_ERROR_INVALID_ARGUMENT;
2853 }
2854 else
2855 operation->mac_size = truncated;
2856
Gilles Peskinefbfac682018-07-08 20:51:54 +02002857exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002858 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002860 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002862 else
2863 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002864 operation->key_set = 1;
2865 }
2866 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002867}
2868
Gilles Peskine89167cb2018-07-08 20:12:23 +02002869psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002870 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002871 psa_algorithm_t alg )
2872{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002873 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002874}
2875
2876psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002877 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002878 psa_algorithm_t alg )
2879{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002880 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002881}
2882
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2884 const uint8_t *input,
2885 size_t input_length )
2886{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002887 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002889 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002891 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002892 operation->has_input = 1;
2893
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002896 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002897 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2898 input, input_length );
2899 status = mbedtls_to_psa_error( ret );
2900 }
2901 else
2902#endif /* MBEDTLS_CMAC_C */
2903#if defined(MBEDTLS_MD_C)
2904 if( PSA_ALG_IS_HMAC( operation->alg ) )
2905 {
2906 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2907 input_length );
2908 }
2909 else
2910#endif /* MBEDTLS_MD_C */
2911 {
2912 /* This shouldn't happen if `operation` was initialized by
2913 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002914 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002915 }
2916
Gilles Peskinefbfac682018-07-08 20:51:54 +02002917 if( status != PSA_SUCCESS )
2918 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002919 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002920}
2921
Gilles Peskine01126fa2018-07-12 17:04:55 +02002922#if defined(MBEDTLS_MD_C)
2923static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2924 uint8_t *mac,
2925 size_t mac_size )
2926{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002927 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002928 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2929 size_t hash_size = 0;
2930 size_t block_size = psa_get_hash_block_size( hash_alg );
2931 psa_status_t status;
2932
Gilles Peskine01126fa2018-07-12 17:04:55 +02002933 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2934 if( status != PSA_SUCCESS )
2935 return( status );
2936 /* From here on, tmp needs to be wiped. */
2937
2938 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2939 if( status != PSA_SUCCESS )
2940 goto exit;
2941
2942 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2943 if( status != PSA_SUCCESS )
2944 goto exit;
2945
2946 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2947 if( status != PSA_SUCCESS )
2948 goto exit;
2949
Gilles Peskined911eb72018-08-14 15:18:45 +02002950 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2951 if( status != PSA_SUCCESS )
2952 goto exit;
2953
2954 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002955
2956exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002957 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002958 return( status );
2959}
2960#endif /* MBEDTLS_MD_C */
2961
mohammad16036df908f2018-04-02 08:34:15 -07002962static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002963 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002964 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002966 if( ! operation->key_set )
2967 return( PSA_ERROR_BAD_STATE );
2968 if( operation->iv_required && ! operation->iv_set )
2969 return( PSA_ERROR_BAD_STATE );
2970
Gilles Peskine8c9def32018-02-08 10:02:12 +01002971 if( mac_size < operation->mac_size )
2972 return( PSA_ERROR_BUFFER_TOO_SMALL );
2973
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002975 if( operation->alg == PSA_ALG_CMAC )
2976 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002977 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2978 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2979 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002980 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002981 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002982 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002983 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002984 else
2985#endif /* MBEDTLS_CMAC_C */
2986#if defined(MBEDTLS_MD_C)
2987 if( PSA_ALG_IS_HMAC( operation->alg ) )
2988 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002989 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002990 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002991 }
2992 else
2993#endif /* MBEDTLS_MD_C */
2994 {
2995 /* This shouldn't happen if `operation` was initialized by
2996 * a setup function. */
2997 return( PSA_ERROR_BAD_STATE );
2998 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999}
3000
Gilles Peskineacd4be32018-07-08 19:56:25 +02003001psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3002 uint8_t *mac,
3003 size_t mac_size,
3004 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003005{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003006 psa_status_t status;
3007
Jaeden Amero252ef282019-02-15 14:05:35 +00003008 if( operation->alg == 0 )
3009 {
3010 return( PSA_ERROR_BAD_STATE );
3011 }
3012
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003013 /* Fill the output buffer with something that isn't a valid mac
3014 * (barring an attack on the mac and deliberately-crafted input),
3015 * in case the caller doesn't check the return status properly. */
3016 *mac_length = mac_size;
3017 /* If mac_size is 0 then mac may be NULL and then the
3018 * call to memset would have undefined behavior. */
3019 if( mac_size != 0 )
3020 memset( mac, '!', mac_size );
3021
Gilles Peskine89167cb2018-07-08 20:12:23 +02003022 if( ! operation->is_sign )
3023 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003024 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003025 }
mohammad16036df908f2018-04-02 08:34:15 -07003026
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003027 status = psa_mac_finish_internal( operation, mac, mac_size );
3028
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003029 if( status == PSA_SUCCESS )
3030 {
3031 status = psa_mac_abort( operation );
3032 if( status == PSA_SUCCESS )
3033 *mac_length = operation->mac_size;
3034 else
3035 memset( mac, '!', mac_size );
3036 }
3037 else
3038 psa_mac_abort( operation );
3039 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003040}
3041
Gilles Peskineacd4be32018-07-08 19:56:25 +02003042psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3043 const uint8_t *mac,
3044 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045{
Gilles Peskine828ed142018-06-18 23:25:51 +02003046 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003047 psa_status_t status;
3048
Jaeden Amero252ef282019-02-15 14:05:35 +00003049 if( operation->alg == 0 )
3050 {
3051 return( PSA_ERROR_BAD_STATE );
3052 }
3053
Gilles Peskine89167cb2018-07-08 20:12:23 +02003054 if( operation->is_sign )
3055 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003056 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003057 }
3058 if( operation->mac_size != mac_length )
3059 {
3060 status = PSA_ERROR_INVALID_SIGNATURE;
3061 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003062 }
mohammad16036df908f2018-04-02 08:34:15 -07003063
3064 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003065 actual_mac, sizeof( actual_mac ) );
3066
3067 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3068 status = PSA_ERROR_INVALID_SIGNATURE;
3069
3070cleanup:
3071 if( status == PSA_SUCCESS )
3072 status = psa_mac_abort( operation );
3073 else
3074 psa_mac_abort( operation );
3075
Gilles Peskine3f108122018-12-07 18:14:53 +01003076 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003077
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003078 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003079}
3080
3081
Gilles Peskine20035e32018-02-03 22:44:14 +01003082
Gilles Peskine20035e32018-02-03 22:44:14 +01003083/****************************************************************/
3084/* Asymmetric cryptography */
3085/****************************************************************/
3086
Gilles Peskine2b450e32018-06-27 15:42:46 +02003087#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003088/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003089 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003090static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3091 size_t hash_length,
3092 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003093{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003094 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003095 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003096 *md_alg = mbedtls_md_get_type( md_info );
3097
3098 /* The Mbed TLS RSA module uses an unsigned int for hash length
3099 * parameters. Validate that it fits so that we don't risk an
3100 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003101#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003102 if( hash_length > UINT_MAX )
3103 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003104#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003105
3106#if defined(MBEDTLS_PKCS1_V15)
3107 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3108 * must be correct. */
3109 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3110 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003111 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003112 if( md_info == NULL )
3113 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003114 if( mbedtls_md_get_size( md_info ) != hash_length )
3115 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003116 }
3117#endif /* MBEDTLS_PKCS1_V15 */
3118
3119#if defined(MBEDTLS_PKCS1_V21)
3120 /* PSS requires a hash internally. */
3121 if( PSA_ALG_IS_RSA_PSS( alg ) )
3122 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003123 if( md_info == NULL )
3124 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003125 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003126#endif /* MBEDTLS_PKCS1_V21 */
3127
Gilles Peskine61b91d42018-06-08 16:09:36 +02003128 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003129}
3130
Michael Thomas7ab51742020-02-12 03:48:56 +00003131psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003132 psa_algorithm_t alg,
3133 const uint8_t *hash,
3134 size_t hash_length,
3135 uint8_t *signature,
3136 size_t signature_size,
3137 size_t *signature_length )
3138{
3139 psa_status_t status;
3140 int ret;
3141 mbedtls_md_type_t md_alg;
3142
3143 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3144 if( status != PSA_SUCCESS )
3145 return( status );
3146
Gilles Peskine630a18a2018-06-29 17:49:35 +02003147 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003148 return( PSA_ERROR_BUFFER_TOO_SMALL );
3149
3150#if defined(MBEDTLS_PKCS1_V15)
3151 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3152 {
3153 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3154 MBEDTLS_MD_NONE );
3155 ret = mbedtls_rsa_pkcs1_sign( rsa,
3156 mbedtls_ctr_drbg_random,
3157 &global_data.ctr_drbg,
3158 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003159 md_alg,
3160 (unsigned int) hash_length,
3161 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003162 signature );
3163 }
3164 else
3165#endif /* MBEDTLS_PKCS1_V15 */
3166#if defined(MBEDTLS_PKCS1_V21)
3167 if( PSA_ALG_IS_RSA_PSS( alg ) )
3168 {
3169 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3170 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3171 mbedtls_ctr_drbg_random,
3172 &global_data.ctr_drbg,
3173 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003174 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003175 (unsigned int) hash_length,
3176 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003177 signature );
3178 }
3179 else
3180#endif /* MBEDTLS_PKCS1_V21 */
3181 {
3182 return( PSA_ERROR_INVALID_ARGUMENT );
3183 }
3184
3185 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003186 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003187 return( mbedtls_to_psa_error( ret ) );
3188}
3189
Michael Thomas7ab51742020-02-12 03:48:56 +00003190psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003191 psa_algorithm_t alg,
3192 const uint8_t *hash,
3193 size_t hash_length,
3194 const uint8_t *signature,
3195 size_t signature_length )
3196{
3197 psa_status_t status;
3198 int ret;
3199 mbedtls_md_type_t md_alg;
3200
3201 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3202 if( status != PSA_SUCCESS )
3203 return( status );
3204
Gilles Peskine630a18a2018-06-29 17:49:35 +02003205 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003206 return( PSA_ERROR_BUFFER_TOO_SMALL );
3207
3208#if defined(MBEDTLS_PKCS1_V15)
3209 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3210 {
3211 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3212 MBEDTLS_MD_NONE );
3213 ret = mbedtls_rsa_pkcs1_verify( rsa,
3214 mbedtls_ctr_drbg_random,
3215 &global_data.ctr_drbg,
3216 MBEDTLS_RSA_PUBLIC,
3217 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003218 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003219 hash,
3220 signature );
3221 }
3222 else
3223#endif /* MBEDTLS_PKCS1_V15 */
3224#if defined(MBEDTLS_PKCS1_V21)
3225 if( PSA_ALG_IS_RSA_PSS( alg ) )
3226 {
3227 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3228 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3229 mbedtls_ctr_drbg_random,
3230 &global_data.ctr_drbg,
3231 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003232 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003233 (unsigned int) hash_length,
3234 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003235 signature );
3236 }
3237 else
3238#endif /* MBEDTLS_PKCS1_V21 */
3239 {
3240 return( PSA_ERROR_INVALID_ARGUMENT );
3241 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003242
3243 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3244 * the rest of the signature is invalid". This has little use in
3245 * practice and PSA doesn't report this distinction. */
3246 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3247 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003248 return( mbedtls_to_psa_error( ret ) );
3249}
3250#endif /* MBEDTLS_RSA_C */
3251
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003252#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003253/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3254 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3255 * (even though these functions don't modify it). */
Michael Thomas257d3be2020-02-24 17:16:42 +00003256psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003257 psa_algorithm_t alg,
3258 const uint8_t *hash,
3259 size_t hash_length,
3260 uint8_t *signature,
3261 size_t signature_size,
3262 size_t *signature_length )
3263{
3264 int ret;
3265 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003266 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003267 mbedtls_mpi_init( &r );
3268 mbedtls_mpi_init( &s );
3269
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003270 if( signature_size < 2 * curve_bytes )
3271 {
3272 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3273 goto cleanup;
3274 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003275
Gilles Peskinea05219c2018-11-16 16:02:56 +01003276#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003277 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3278 {
3279 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3280 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3281 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003282 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3283 &ecp->d, hash,
3284 hash_length, md_alg,
3285 mbedtls_ctr_drbg_random,
3286 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003287 }
3288 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003289#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003290 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003291 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003292 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3293 hash, hash_length,
3294 mbedtls_ctr_drbg_random,
3295 &global_data.ctr_drbg ) );
3296 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003297
3298 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3299 signature,
3300 curve_bytes ) );
3301 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3302 signature + curve_bytes,
3303 curve_bytes ) );
3304
3305cleanup:
3306 mbedtls_mpi_free( &r );
3307 mbedtls_mpi_free( &s );
3308 if( ret == 0 )
3309 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003310 return( mbedtls_to_psa_error( ret ) );
3311}
3312
Michael Thomas257d3be2020-02-24 17:16:42 +00003313psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003314 const uint8_t *hash,
3315 size_t hash_length,
3316 const uint8_t *signature,
3317 size_t signature_length )
3318{
3319 int ret;
3320 mbedtls_mpi r, s;
3321 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3322 mbedtls_mpi_init( &r );
3323 mbedtls_mpi_init( &s );
3324
3325 if( signature_length != 2 * curve_bytes )
3326 return( PSA_ERROR_INVALID_SIGNATURE );
3327
3328 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3329 signature,
3330 curve_bytes ) );
3331 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3332 signature + curve_bytes,
3333 curve_bytes ) );
3334
3335 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3336 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003337
3338cleanup:
3339 mbedtls_mpi_free( &r );
3340 mbedtls_mpi_free( &s );
3341 return( mbedtls_to_psa_error( ret ) );
3342}
3343#endif /* MBEDTLS_ECDSA_C */
3344
Gilles Peskinec5487a82018-12-03 18:08:14 +01003345psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003346 psa_algorithm_t alg,
3347 const uint8_t *hash,
3348 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003349 uint8_t *signature,
3350 size_t signature_size,
3351 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003352{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003353 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003354 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003355#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3356 const psa_drv_se_t *drv;
3357 psa_drv_se_context_t *drv_context;
3358#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003359
3360 *signature_length = signature_size;
3361
Gilles Peskineedc64242019-08-07 21:05:07 +02003362 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003363 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003364 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003365 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003366 {
3367 status = PSA_ERROR_INVALID_ARGUMENT;
3368 goto exit;
3369 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003370
Gilles Peskineedc64242019-08-07 21:05:07 +02003371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3372 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3373 {
3374 if( drv->asymmetric == NULL ||
3375 drv->asymmetric->p_sign == NULL )
3376 {
3377 status = PSA_ERROR_NOT_SUPPORTED;
3378 goto exit;
3379 }
3380 status = drv->asymmetric->p_sign( drv_context,
3381 slot->data.se.slot_number,
3382 alg,
3383 hash, hash_length,
3384 signature, signature_size,
3385 signature_length );
3386 }
3387 else
3388#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003389#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003390 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003391 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003392 status = psa_rsa_sign( slot->data.rsa,
3393 alg,
3394 hash, hash_length,
3395 signature, signature_size,
3396 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003397 }
3398 else
3399#endif /* defined(MBEDTLS_RSA_C) */
3400#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003401 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003402 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003403#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003404 if(
3405#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3406 PSA_ALG_IS_ECDSA( alg )
3407#else
3408 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3409#endif
3410 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 status = psa_ecdsa_sign( slot->data.ecp,
3412 alg,
3413 hash, hash_length,
3414 signature, signature_size,
3415 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003416 else
3417#endif /* defined(MBEDTLS_ECDSA_C) */
3418 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003419 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003420 }
itayzafrir5c753392018-05-08 11:18:38 +03003421 }
3422 else
3423#endif /* defined(MBEDTLS_ECP_C) */
3424 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003425 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003426 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003427
3428exit:
3429 /* Fill the unused part of the output buffer (the whole buffer on error,
3430 * the trailing part on success) with something that isn't a valid mac
3431 * (barring an attack on the mac and deliberately-crafted input),
3432 * in case the caller doesn't check the return status properly. */
3433 if( status == PSA_SUCCESS )
3434 memset( signature + *signature_length, '!',
3435 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003436 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003437 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003438 /* If signature_size is 0 then we have nothing to do. We must not call
3439 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003440 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003441}
3442
Gilles Peskinec5487a82018-12-03 18:08:14 +01003443psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003444 psa_algorithm_t alg,
3445 const uint8_t *hash,
3446 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003447 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003448 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003449{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003450 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003451 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003452#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3453 const psa_drv_se_t *drv;
3454 psa_drv_se_context_t *drv_context;
3455#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003456
Gilles Peskineedc64242019-08-07 21:05:07 +02003457 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003458 if( status != PSA_SUCCESS )
3459 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003460
Gilles Peskineedc64242019-08-07 21:05:07 +02003461#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3462 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3463 {
3464 if( drv->asymmetric == NULL ||
3465 drv->asymmetric->p_verify == NULL )
3466 return( PSA_ERROR_NOT_SUPPORTED );
3467 return( drv->asymmetric->p_verify( drv_context,
3468 slot->data.se.slot_number,
3469 alg,
3470 hash, hash_length,
3471 signature, signature_length ) );
3472 }
3473 else
3474#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003475#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003476 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003477 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003478 return( psa_rsa_verify( slot->data.rsa,
3479 alg,
3480 hash, hash_length,
3481 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003482 }
3483 else
3484#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003485#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003486 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003487 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003488#if defined(MBEDTLS_ECDSA_C)
3489 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003490 return( psa_ecdsa_verify( slot->data.ecp,
3491 hash, hash_length,
3492 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003493 else
3494#endif /* defined(MBEDTLS_ECDSA_C) */
3495 {
3496 return( PSA_ERROR_INVALID_ARGUMENT );
3497 }
itayzafrir5c753392018-05-08 11:18:38 +03003498 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003499 else
3500#endif /* defined(MBEDTLS_ECP_C) */
3501 {
3502 return( PSA_ERROR_NOT_SUPPORTED );
3503 }
3504}
3505
Gilles Peskine072ac562018-06-30 00:21:29 +02003506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3507static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3508 mbedtls_rsa_context *rsa )
3509{
3510 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3511 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3512 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3513 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3514}
3515#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3516
Gilles Peskinec5487a82018-12-03 18:08:14 +01003517psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003518 psa_algorithm_t alg,
3519 const uint8_t *input,
3520 size_t input_length,
3521 const uint8_t *salt,
3522 size_t salt_length,
3523 uint8_t *output,
3524 size_t output_size,
3525 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003527 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003528 psa_status_t status;
3529
Darryl Green5cc689a2018-07-24 15:34:10 +01003530 (void) input;
3531 (void) input_length;
3532 (void) salt;
3533 (void) output;
3534 (void) output_size;
3535
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003536 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003537
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003538 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3539 return( PSA_ERROR_INVALID_ARGUMENT );
3540
Gilles Peskine28f8f302019-07-24 13:30:31 +02003541 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003542 if( status != PSA_SUCCESS )
3543 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003544 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3545 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547
3548#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003549 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550 {
3551 mbedtls_rsa_context *rsa = slot->data.rsa;
3552 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003553 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003554 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003555#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003556 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003558 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3559 mbedtls_ctr_drbg_random,
3560 &global_data.ctr_drbg,
3561 MBEDTLS_RSA_PUBLIC,
3562 input_length,
3563 input,
3564 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565 }
3566 else
3567#endif /* MBEDTLS_PKCS1_V15 */
3568#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003569 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003570 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003571 psa_rsa_oaep_set_padding_mode( alg, rsa );
3572 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3573 mbedtls_ctr_drbg_random,
3574 &global_data.ctr_drbg,
3575 MBEDTLS_RSA_PUBLIC,
3576 salt, salt_length,
3577 input_length,
3578 input,
3579 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580 }
3581 else
3582#endif /* MBEDTLS_PKCS1_V21 */
3583 {
3584 return( PSA_ERROR_INVALID_ARGUMENT );
3585 }
3586 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003587 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588 return( mbedtls_to_psa_error( ret ) );
3589 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003591#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592 {
3593 return( PSA_ERROR_NOT_SUPPORTED );
3594 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003595}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596
Gilles Peskinec5487a82018-12-03 18:08:14 +01003597psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003598 psa_algorithm_t alg,
3599 const uint8_t *input,
3600 size_t input_length,
3601 const uint8_t *salt,
3602 size_t salt_length,
3603 uint8_t *output,
3604 size_t output_size,
3605 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003607 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003608 psa_status_t status;
3609
Darryl Green5cc689a2018-07-24 15:34:10 +01003610 (void) input;
3611 (void) input_length;
3612 (void) salt;
3613 (void) output;
3614 (void) output_size;
3615
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003616 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003618 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3619 return( PSA_ERROR_INVALID_ARGUMENT );
3620
Gilles Peskine28f8f302019-07-24 13:30:31 +02003621 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003622 if( status != PSA_SUCCESS )
3623 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003624 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003625 return( PSA_ERROR_INVALID_ARGUMENT );
3626
3627#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003628 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629 {
3630 mbedtls_rsa_context *rsa = slot->data.rsa;
3631 int ret;
3632
Gilles Peskine630a18a2018-06-29 17:49:35 +02003633 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 return( PSA_ERROR_INVALID_ARGUMENT );
3635
3636#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003637 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003639 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3640 mbedtls_ctr_drbg_random,
3641 &global_data.ctr_drbg,
3642 MBEDTLS_RSA_PRIVATE,
3643 output_length,
3644 input,
3645 output,
3646 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647 }
3648 else
3649#endif /* MBEDTLS_PKCS1_V15 */
3650#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003651 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003653 psa_rsa_oaep_set_padding_mode( alg, rsa );
3654 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3655 mbedtls_ctr_drbg_random,
3656 &global_data.ctr_drbg,
3657 MBEDTLS_RSA_PRIVATE,
3658 salt, salt_length,
3659 output_length,
3660 input,
3661 output,
3662 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003663 }
3664 else
3665#endif /* MBEDTLS_PKCS1_V21 */
3666 {
3667 return( PSA_ERROR_INVALID_ARGUMENT );
3668 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003669
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 return( mbedtls_to_psa_error( ret ) );
3671 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003673#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 {
3675 return( PSA_ERROR_NOT_SUPPORTED );
3676 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003677}
Gilles Peskine20035e32018-02-03 22:44:14 +01003678
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003679
3680
mohammad1603503973b2018-03-12 15:59:30 +02003681/****************************************************************/
3682/* Symmetric cryptography */
3683/****************************************************************/
3684
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003685/* Initialize the cipher operation structure. Once this function has been
3686 * called, psa_cipher_abort can run and will do the right thing. */
3687static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3688 psa_algorithm_t alg )
3689{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003690 if( ! PSA_ALG_IS_CIPHER( alg ) )
3691 {
3692 memset( operation, 0, sizeof( *operation ) );
3693 return( PSA_ERROR_INVALID_ARGUMENT );
3694 }
3695
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003696 operation->alg = alg;
3697 operation->key_set = 0;
3698 operation->iv_set = 0;
3699 operation->iv_required = 1;
3700 operation->iv_size = 0;
3701 operation->block_size = 0;
3702 mbedtls_cipher_init( &operation->ctx.cipher );
3703 return( PSA_SUCCESS );
3704}
3705
Gilles Peskinee553c652018-06-04 16:22:46 +02003706static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003707 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003708 psa_algorithm_t alg,
3709 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003710{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003711 int ret = 0;
3712 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003713 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003714 size_t key_bits;
3715 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003716 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3717 PSA_KEY_USAGE_ENCRYPT :
3718 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003719
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003720 /* A context must be freshly initialized before it can be set up. */
3721 if( operation->alg != 0 )
3722 {
3723 return( PSA_ERROR_BAD_STATE );
3724 }
3725
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003726 status = psa_cipher_init( operation, alg );
3727 if( status != PSA_SUCCESS )
3728 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003729
Gilles Peskine28f8f302019-07-24 13:30:31 +02003730 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003731 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003732 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003733 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003734
Michael Thomas863b5d62020-02-10 19:41:16 +00003735#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
3736 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
3737 {
3738 status = psa_cipher_setup_vendor(operation, handle, alg, cipher_operation);
3739 goto exit;
3740 }
3741#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
3742
Gilles Peskine8e338702019-07-30 20:06:31 +02003743 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003744 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003745 {
3746 status = PSA_ERROR_NOT_SUPPORTED;
3747 goto exit;
3748 }
mohammad1603503973b2018-03-12 15:59:30 +02003749
mohammad1603503973b2018-03-12 15:59:30 +02003750 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003751 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003752 goto exit;
Michael Thomas17547bc2020-01-14 16:47:03 +00003753
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003754#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003755 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003756 {
3757 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003758 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003759 memcpy( keys, slot->data.raw.data, 16 );
3760 memcpy( keys + 16, slot->data.raw.data, 8 );
3761 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3762 keys,
3763 192, cipher_operation );
3764 }
3765 else
3766#endif
3767 {
3768 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3769 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003770 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003771 }
Moran Peker41deec42018-04-04 15:43:05 +03003772 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003773 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003774
mohammad16038481e742018-03-18 13:57:31 +02003775#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003776 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003777 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003778 case PSA_ALG_CBC_NO_PADDING:
3779 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3780 MBEDTLS_PADDING_NONE );
3781 break;
3782 case PSA_ALG_CBC_PKCS7:
3783 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3784 MBEDTLS_PADDING_PKCS7 );
3785 break;
3786 default:
3787 /* The algorithm doesn't involve padding. */
3788 ret = 0;
3789 break;
3790 }
3791 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003792 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003793#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3794
mohammad1603503973b2018-03-12 15:59:30 +02003795 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003796 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003797 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003798 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003799 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003800 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003801 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003802#if defined(MBEDTLS_CHACHA20_C)
3803 else
3804 if( alg == PSA_ALG_CHACHA20 )
3805 operation->iv_size = 12;
3806#endif
mohammad1603503973b2018-03-12 15:59:30 +02003807
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003808exit:
3809 if( status == 0 )
3810 status = mbedtls_to_psa_error( ret );
3811 if( status != 0 )
3812 psa_cipher_abort( operation );
3813 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003814}
3815
Gilles Peskinefe119512018-07-08 21:39:34 +02003816psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003817 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003818 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003819{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003820 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003821}
3822
Gilles Peskinefe119512018-07-08 21:39:34 +02003823psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003824 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003825 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003826{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003827 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003828}
3829
Gilles Peskinefe119512018-07-08 21:39:34 +02003830psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003831 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003832 size_t iv_size,
3833 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003834{
itayzafrir534bd7c2018-08-02 13:56:32 +03003835 psa_status_t status;
3836 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003837 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003838 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003839 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003840 }
Moran Peker41deec42018-04-04 15:43:05 +03003841 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003842 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003843 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003844 goto exit;
3845 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003846 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3847 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003848 if( ret != 0 )
3849 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003850 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003851 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003852 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003853
mohammad16038481e742018-03-18 13:57:31 +02003854 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003855 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003856
Moran Peker395db872018-05-31 14:07:14 +03003857exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003858 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003859 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003860 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003861}
3862
Gilles Peskinefe119512018-07-08 21:39:34 +02003863psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003864 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003865 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003866{
itayzafrir534bd7c2018-08-02 13:56:32 +03003867 psa_status_t status;
3868 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003869 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003870 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003871 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 }
Moran Pekera28258c2018-05-29 16:25:04 +03003873 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003874 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003875 status = PSA_ERROR_INVALID_ARGUMENT;
3876 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003877 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003878 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3879 status = mbedtls_to_psa_error( ret );
3880exit:
3881 if( status == PSA_SUCCESS )
3882 operation->iv_set = 1;
3883 else
mohammad1603503973b2018-03-12 15:59:30 +02003884 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003886}
3887
Gilles Peskinee553c652018-06-04 16:22:46 +02003888psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3889 const uint8_t *input,
3890 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003891 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003892 size_t output_size,
3893 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003894{
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 psa_status_t status;
3896 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003897 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003898
3899 if( operation->alg == 0 )
3900 {
3901 return( PSA_ERROR_BAD_STATE );
3902 }
3903
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003904 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003905 {
3906 /* Take the unprocessed partial block left over from previous
3907 * update calls, if any, plus the input to this call. Remove
3908 * the last partial block, if any. You get the data that will be
3909 * output in this call. */
3910 expected_output_size =
3911 ( operation->ctx.cipher.unprocessed_len + input_length )
3912 / operation->block_size * operation->block_size;
3913 }
3914 else
3915 {
3916 expected_output_size = input_length;
3917 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003918
Gilles Peskine89d789c2018-06-04 17:17:16 +02003919 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003920 {
3921 status = PSA_ERROR_BUFFER_TOO_SMALL;
3922 goto exit;
3923 }
mohammad160382759612018-03-12 18:16:40 +02003924
mohammad1603503973b2018-03-12 15:59:30 +02003925 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003926 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 status = mbedtls_to_psa_error( ret );
3928exit:
3929 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003930 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003931 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003932}
3933
Gilles Peskinee553c652018-06-04 16:22:46 +02003934psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3935 uint8_t *output,
3936 size_t output_size,
3937 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003938{
David Saadab4ecc272019-02-14 13:48:10 +02003939 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003940 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003941 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003942
mohammad1603503973b2018-03-12 15:59:30 +02003943 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003944 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003945 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003946 }
3947 if( operation->iv_required && ! operation->iv_set )
3948 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003949 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003950 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003951
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003952 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003953 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3954 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003955 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003956 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003957 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003958 }
3959
Janos Follath315b51c2018-07-09 16:04:51 +01003960 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3961 temp_output_buffer,
3962 output_length );
3963 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003964 {
Janos Follath315b51c2018-07-09 16:04:51 +01003965 status = mbedtls_to_psa_error( cipher_ret );
3966 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003967 }
Janos Follath315b51c2018-07-09 16:04:51 +01003968
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003969 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003970 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003971 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003972 memcpy( output, temp_output_buffer, *output_length );
3973 else
3974 {
Janos Follath315b51c2018-07-09 16:04:51 +01003975 status = PSA_ERROR_BUFFER_TOO_SMALL;
3976 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003977 }
mohammad1603503973b2018-03-12 15:59:30 +02003978
Gilles Peskine3f108122018-12-07 18:14:53 +01003979 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003980 status = psa_cipher_abort( operation );
3981
3982 return( status );
3983
3984error:
3985
3986 *output_length = 0;
3987
Gilles Peskine3f108122018-12-07 18:14:53 +01003988 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003989 (void) psa_cipher_abort( operation );
3990
3991 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003992}
3993
Gilles Peskinee553c652018-06-04 16:22:46 +02003994psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3995{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003996 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003997 {
3998 /* The object has (apparently) been initialized but it is not
3999 * in use. It's ok to call abort on such an object, and there's
4000 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004001 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004002 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004003
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004004 /* Sanity check (shouldn't happen: operation->alg should
4005 * always have been initialized to a valid value). */
4006 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4007 return( PSA_ERROR_BAD_STATE );
mohammad1603503973b2018-03-12 15:59:30 +02004008 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004009
Moran Peker41deec42018-04-04 15:43:05 +03004010 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004011 operation->key_set = 0;
4012 operation->iv_set = 0;
4013 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004014 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004015 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004016
Moran Peker395db872018-05-31 14:07:14 +03004017 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004018}
4019
Gilles Peskinea0655c32018-04-30 17:06:50 +02004020
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004021
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004022
mohammad16035955c982018-04-26 00:53:03 +03004023/****************************************************************/
4024/* AEAD */
4025/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004026
Gilles Peskineedf9a652018-08-17 18:11:56 +02004027typedef struct
4028{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004029 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004030 const mbedtls_cipher_info_t *cipher_info;
4031 union
4032 {
4033#if defined(MBEDTLS_CCM_C)
4034 mbedtls_ccm_context ccm;
4035#endif /* MBEDTLS_CCM_C */
4036#if defined(MBEDTLS_GCM_C)
4037 mbedtls_gcm_context gcm;
4038#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004039#if defined(MBEDTLS_CHACHAPOLY_C)
4040 mbedtls_chachapoly_context chachapoly;
4041#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004042 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004043 psa_algorithm_t core_alg;
4044 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004045 uint8_t tag_length;
4046} aead_operation_t;
4047
Gilles Peskine30a9e412019-01-14 18:36:12 +01004048static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004050 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004051 {
4052#if defined(MBEDTLS_CCM_C)
4053 case PSA_ALG_CCM:
4054 mbedtls_ccm_free( &operation->ctx.ccm );
4055 break;
4056#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004057#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004058 case PSA_ALG_GCM:
4059 mbedtls_gcm_free( &operation->ctx.gcm );
4060 break;
4061#endif /* MBEDTLS_GCM_C */
4062 }
4063}
4064
4065static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004066 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004067 psa_key_usage_t usage,
4068 psa_algorithm_t alg )
4069{
4070 psa_status_t status;
4071 size_t key_bits;
4072 mbedtls_cipher_id_t cipher_id;
4073
Gilles Peskine28f8f302019-07-24 13:30:31 +02004074 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004075 if( status != PSA_SUCCESS )
4076 return( status );
4077
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004078 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004079
4080 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004081 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004082 &cipher_id );
4083 if( operation->cipher_info == NULL )
4084 return( PSA_ERROR_NOT_SUPPORTED );
4085
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004086 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 {
4088#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004089 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4090 operation->core_alg = PSA_ALG_CCM;
4091 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004092 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4093 * The call to mbedtls_ccm_encrypt_and_tag or
4094 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004095 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 return( PSA_ERROR_INVALID_ARGUMENT );
4097 mbedtls_ccm_init( &operation->ctx.ccm );
4098 status = mbedtls_to_psa_error(
4099 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4100 operation->slot->data.raw.data,
4101 (unsigned int) key_bits ) );
4102 if( status != 0 )
4103 goto cleanup;
4104 break;
4105#endif /* MBEDTLS_CCM_C */
4106
4107#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004108 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4109 operation->core_alg = PSA_ALG_GCM;
4110 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004111 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4112 * The call to mbedtls_gcm_crypt_and_tag or
4113 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004114 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115 return( PSA_ERROR_INVALID_ARGUMENT );
4116 mbedtls_gcm_init( &operation->ctx.gcm );
4117 status = mbedtls_to_psa_error(
4118 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4119 operation->slot->data.raw.data,
4120 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004121 if( status != 0 )
4122 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004123 break;
4124#endif /* MBEDTLS_GCM_C */
4125
Gilles Peskine26869f22019-05-06 15:25:00 +02004126#if defined(MBEDTLS_CHACHAPOLY_C)
4127 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4128 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4129 operation->full_tag_length = 16;
4130 /* We only support the default tag length. */
4131 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4132 return( PSA_ERROR_NOT_SUPPORTED );
4133 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4134 status = mbedtls_to_psa_error(
4135 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4136 operation->slot->data.raw.data ) );
4137 if( status != 0 )
4138 goto cleanup;
4139 break;
4140#endif /* MBEDTLS_CHACHAPOLY_C */
4141
Gilles Peskineedf9a652018-08-17 18:11:56 +02004142 default:
4143 return( PSA_ERROR_NOT_SUPPORTED );
4144 }
4145
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004146 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4147 {
4148 status = PSA_ERROR_INVALID_ARGUMENT;
4149 goto cleanup;
4150 }
4151 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004152
Gilles Peskineedf9a652018-08-17 18:11:56 +02004153 return( PSA_SUCCESS );
4154
4155cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004156 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004157 return( status );
4158}
4159
Gilles Peskinec5487a82018-12-03 18:08:14 +01004160psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004161 psa_algorithm_t alg,
4162 const uint8_t *nonce,
4163 size_t nonce_length,
4164 const uint8_t *additional_data,
4165 size_t additional_data_length,
4166 const uint8_t *plaintext,
4167 size_t plaintext_length,
4168 uint8_t *ciphertext,
4169 size_t ciphertext_size,
4170 size_t *ciphertext_length )
4171{
mohammad16035955c982018-04-26 00:53:03 +03004172 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004173 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004174 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004175
mohammad1603f08a5502018-06-03 15:05:47 +03004176 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004177
Gilles Peskinec5487a82018-12-03 18:08:14 +01004178 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004179 if( status != PSA_SUCCESS )
4180 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004181
Gilles Peskineedf9a652018-08-17 18:11:56 +02004182 /* For all currently supported modes, the tag is at the end of the
4183 * ciphertext. */
4184 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4185 {
4186 status = PSA_ERROR_BUFFER_TOO_SMALL;
4187 goto exit;
4188 }
4189 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004190
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004191#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004192 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004193 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004194 status = mbedtls_to_psa_error(
4195 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4196 MBEDTLS_GCM_ENCRYPT,
4197 plaintext_length,
4198 nonce, nonce_length,
4199 additional_data, additional_data_length,
4200 plaintext, ciphertext,
4201 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004202 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004203 else
4204#endif /* MBEDTLS_GCM_C */
4205#if defined(MBEDTLS_CCM_C)
4206 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004207 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208 status = mbedtls_to_psa_error(
4209 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4210 plaintext_length,
4211 nonce, nonce_length,
4212 additional_data,
4213 additional_data_length,
4214 plaintext, ciphertext,
4215 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004216 }
mohammad16035c8845f2018-05-09 05:40:09 -07004217 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004218#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004219#if defined(MBEDTLS_CHACHAPOLY_C)
4220 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4221 {
4222 if( nonce_length != 12 || operation.tag_length != 16 )
4223 {
4224 status = PSA_ERROR_NOT_SUPPORTED;
4225 goto exit;
4226 }
4227 status = mbedtls_to_psa_error(
4228 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4229 plaintext_length,
4230 nonce,
4231 additional_data,
4232 additional_data_length,
4233 plaintext,
4234 ciphertext,
4235 tag ) );
4236 }
4237 else
4238#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004239 {
mohammad1603554faad2018-06-03 15:07:38 +03004240 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004241 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004242
Gilles Peskineedf9a652018-08-17 18:11:56 +02004243 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4244 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004245
Gilles Peskineedf9a652018-08-17 18:11:56 +02004246exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004247 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004248 if( status == PSA_SUCCESS )
4249 *ciphertext_length = plaintext_length + operation.tag_length;
4250 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004251}
4252
Gilles Peskineee652a32018-06-01 19:23:52 +02004253/* Locate the tag in a ciphertext buffer containing the encrypted data
4254 * followed by the tag. Return the length of the part preceding the tag in
4255 * *plaintext_length. This is the size of the plaintext in modes where
4256 * the encrypted data has the same size as the plaintext, such as
4257 * CCM and GCM. */
4258static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4259 const uint8_t *ciphertext,
4260 size_t ciphertext_length,
4261 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004262 const uint8_t **p_tag )
4263{
4264 size_t payload_length;
4265 if( tag_length > ciphertext_length )
4266 return( PSA_ERROR_INVALID_ARGUMENT );
4267 payload_length = ciphertext_length - tag_length;
4268 if( payload_length > plaintext_size )
4269 return( PSA_ERROR_BUFFER_TOO_SMALL );
4270 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004271 return( PSA_SUCCESS );
4272}
4273
Gilles Peskinec5487a82018-12-03 18:08:14 +01004274psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004275 psa_algorithm_t alg,
4276 const uint8_t *nonce,
4277 size_t nonce_length,
4278 const uint8_t *additional_data,
4279 size_t additional_data_length,
4280 const uint8_t *ciphertext,
4281 size_t ciphertext_length,
4282 uint8_t *plaintext,
4283 size_t plaintext_size,
4284 size_t *plaintext_length )
4285{
mohammad16035955c982018-04-26 00:53:03 +03004286 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004287 aead_operation_t operation;
4288 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004289
Gilles Peskineee652a32018-06-01 19:23:52 +02004290 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004291
Gilles Peskinec5487a82018-12-03 18:08:14 +01004292 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004293 if( status != PSA_SUCCESS )
4294 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004295
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004296 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4297 ciphertext, ciphertext_length,
4298 plaintext_size, &tag );
4299 if( status != PSA_SUCCESS )
4300 goto exit;
4301
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004302#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004303 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004304 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004305 status = mbedtls_to_psa_error(
4306 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4307 ciphertext_length - operation.tag_length,
4308 nonce, nonce_length,
4309 additional_data,
4310 additional_data_length,
4311 tag, operation.tag_length,
4312 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004313 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004314 else
4315#endif /* MBEDTLS_GCM_C */
4316#if defined(MBEDTLS_CCM_C)
4317 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004318 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004319 status = mbedtls_to_psa_error(
4320 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4321 ciphertext_length - operation.tag_length,
4322 nonce, nonce_length,
4323 additional_data,
4324 additional_data_length,
4325 ciphertext, plaintext,
4326 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004327 }
mohammad160339574652018-06-01 04:39:53 -07004328 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004329#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004330#if defined(MBEDTLS_CHACHAPOLY_C)
4331 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4332 {
4333 if( nonce_length != 12 || operation.tag_length != 16 )
4334 {
4335 status = PSA_ERROR_NOT_SUPPORTED;
4336 goto exit;
4337 }
4338 status = mbedtls_to_psa_error(
4339 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4340 ciphertext_length - operation.tag_length,
4341 nonce,
4342 additional_data,
4343 additional_data_length,
4344 tag,
4345 ciphertext,
4346 plaintext ) );
4347 }
4348 else
4349#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004350 {
mohammad1603554faad2018-06-03 15:07:38 +03004351 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004352 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004353
Gilles Peskineedf9a652018-08-17 18:11:56 +02004354 if( status != PSA_SUCCESS && plaintext_size != 0 )
4355 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004356
Gilles Peskineedf9a652018-08-17 18:11:56 +02004357exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004358 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004359 if( status == PSA_SUCCESS )
4360 *plaintext_length = ciphertext_length - operation.tag_length;
4361 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004362}
4363
Gilles Peskinea0655c32018-04-30 17:06:50 +02004364
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004365
Gilles Peskine20035e32018-02-03 22:44:14 +01004366/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004367/* Generators */
4368/****************************************************************/
4369
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004370#define HKDF_STATE_INIT 0 /* no input yet */
4371#define HKDF_STATE_STARTED 1 /* got salt */
4372#define HKDF_STATE_KEYED 2 /* got key */
4373#define HKDF_STATE_OUTPUT 3 /* output started */
4374
Gilles Peskinecbe66502019-05-16 16:59:18 +02004375static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004377{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4379 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004380 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004382}
4383
4384
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004385psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004386{
4387 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004389 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004390 {
4391 /* The object has (apparently) been initialized but it is not
4392 * in use. It's ok to call abort on such an object, and there's
4393 * nothing to do. */
4394 }
4395 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004396#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004397 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004398 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399 mbedtls_free( operation->ctx.hkdf.info );
4400 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004401 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004402 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004404 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004405 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004406 if( operation->ctx.tls12_prf.seed != NULL )
4407 {
4408 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4409 operation->ctx.tls12_prf.seed_length );
4410 mbedtls_free( operation->ctx.tls12_prf.seed );
4411 }
4412
4413 if( operation->ctx.tls12_prf.label != NULL )
4414 {
4415 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4416 operation->ctx.tls12_prf.label_length );
4417 mbedtls_free( operation->ctx.tls12_prf.label );
4418 }
4419
4420 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4421
4422 /* We leave the fields Ai and output_block to be erased safely by the
4423 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004424 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004425 else
4426#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004427 {
4428 status = PSA_ERROR_BAD_STATE;
4429 }
Janos Follath083036a2019-06-11 10:22:26 +01004430 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004431 return( status );
4432}
4433
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004435 size_t *capacity)
4436{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004440 return PSA_ERROR_BAD_STATE;
4441 }
4442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004444 return( PSA_SUCCESS );
4445}
4446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004448 size_t capacity )
4449{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004451 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004452 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004453 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004454 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004455 return( PSA_SUCCESS );
4456}
4457
Gilles Peskinebef7f142018-07-12 17:22:21 +02004458#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004460 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004461static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004462 psa_algorithm_t hash_alg,
4463 uint8_t *output,
4464 size_t output_length )
4465{
4466 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4467 psa_status_t status;
4468
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004469 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4470 return( PSA_ERROR_BAD_STATE );
4471 hkdf->state = HKDF_STATE_OUTPUT;
4472
Gilles Peskinebef7f142018-07-12 17:22:21 +02004473 while( output_length != 0 )
4474 {
4475 /* Copy what remains of the current block */
4476 uint8_t n = hash_length - hkdf->offset_in_block;
4477 if( n > output_length )
4478 n = (uint8_t) output_length;
4479 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4480 output += n;
4481 output_length -= n;
4482 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004483 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004484 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004485 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004486 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004488 * object was corrupted or if this function is called directly
4489 * inside the library. */
4490 if( hkdf->block_number == 0xff )
4491 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004492
4493 /* We need a new block */
4494 ++hkdf->block_number;
4495 hkdf->offset_in_block = 0;
4496 status = psa_hmac_setup_internal( &hkdf->hmac,
4497 hkdf->prk, hash_length,
4498 hash_alg );
4499 if( status != PSA_SUCCESS )
4500 return( status );
4501 if( hkdf->block_number != 1 )
4502 {
4503 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4504 hkdf->output_block,
4505 hash_length );
4506 if( status != PSA_SUCCESS )
4507 return( status );
4508 }
4509 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4510 hkdf->info,
4511 hkdf->info_length );
4512 if( status != PSA_SUCCESS )
4513 return( status );
4514 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4515 &hkdf->block_number, 1 );
4516 if( status != PSA_SUCCESS )
4517 return( status );
4518 status = psa_hmac_finish_internal( &hkdf->hmac,
4519 hkdf->output_block,
4520 sizeof( hkdf->output_block ) );
4521 if( status != PSA_SUCCESS )
4522 return( status );
4523 }
4524
4525 return( PSA_SUCCESS );
4526}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004527
Janos Follath7742fee2019-06-17 12:58:10 +01004528static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4529 psa_tls12_prf_key_derivation_t *tls12_prf,
4530 psa_algorithm_t alg )
4531{
4532 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4533 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004534 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4535 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004536
Janos Follath7742fee2019-06-17 12:58:10 +01004537 /* We can't be wanting more output after block 0xff, otherwise
4538 * the capacity check in psa_key_derivation_output_bytes() would have
4539 * prevented this call. It could happen only if the operation
4540 * object was corrupted or if this function is called directly
4541 * inside the library. */
4542 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004543 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004544
4545 /* We need a new block */
4546 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004547 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004548
4549 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4550 *
4551 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4552 *
4553 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4554 * HMAC_hash(secret, A(2) + seed) +
4555 * HMAC_hash(secret, A(3) + seed) + ...
4556 *
4557 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004558 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004559 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004560 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004561 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004562 * is currently extracted as `output_block` and where i is
4563 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004564 */
4565
Janos Follathea29bfb2019-06-19 12:21:20 +01004566 /* Save the hash context before using it, to preserve the hash state with
4567 * only the inner padding in it. We need this, because inner padding depends
4568 * on the key (secret in the RFC's terminology). */
4569 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4570 if( status != PSA_SUCCESS )
4571 goto cleanup;
4572
4573 /* Calculate A(i) where i = tls12_prf->block_number. */
4574 if( tls12_prf->block_number == 1 )
4575 {
4576 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4577 * the variable seed and in this instance means it in the context of the
4578 * P_hash function, where seed = label + seed.) */
4579 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4580 tls12_prf->label, tls12_prf->label_length );
4581 if( status != PSA_SUCCESS )
4582 goto cleanup;
4583 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4584 tls12_prf->seed, tls12_prf->seed_length );
4585 if( status != PSA_SUCCESS )
4586 goto cleanup;
4587 }
4588 else
4589 {
4590 /* A(i) = HMAC_hash(secret, A(i-1)) */
4591 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4592 tls12_prf->Ai, hash_length );
4593 if( status != PSA_SUCCESS )
4594 goto cleanup;
4595 }
4596
4597 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4598 tls12_prf->Ai, hash_length );
4599 if( status != PSA_SUCCESS )
4600 goto cleanup;
4601 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4602 if( status != PSA_SUCCESS )
4603 goto cleanup;
4604
4605 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4606 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4607 tls12_prf->Ai, hash_length );
4608 if( status != PSA_SUCCESS )
4609 goto cleanup;
4610 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4611 tls12_prf->label, tls12_prf->label_length );
4612 if( status != PSA_SUCCESS )
4613 goto cleanup;
4614 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4615 tls12_prf->seed, tls12_prf->seed_length );
4616 if( status != PSA_SUCCESS )
4617 goto cleanup;
4618 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4619 tls12_prf->output_block, hash_length );
4620 if( status != PSA_SUCCESS )
4621 goto cleanup;
4622 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4623 if( status != PSA_SUCCESS )
4624 goto cleanup;
4625
Janos Follath7742fee2019-06-17 12:58:10 +01004626
4627cleanup:
4628
Janos Follathea29bfb2019-06-19 12:21:20 +01004629 cleanup_status = psa_hash_abort( &backup );
4630 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4631 status = cleanup_status;
4632
4633 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004634}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004635
Janos Follath844eb0e2019-06-19 12:10:49 +01004636static psa_status_t psa_key_derivation_tls12_prf_read(
4637 psa_tls12_prf_key_derivation_t *tls12_prf,
4638 psa_algorithm_t alg,
4639 uint8_t *output,
4640 size_t output_length )
4641{
4642 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4643 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4644 psa_status_t status;
4645 uint8_t offset, length;
4646
4647 while( output_length != 0 )
4648 {
4649 /* Check if we have fully processed the current block. */
4650 if( tls12_prf->left_in_block == 0 )
4651 {
4652 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4653 alg );
4654 if( status != PSA_SUCCESS )
4655 return( status );
4656
4657 continue;
4658 }
4659
4660 if( tls12_prf->left_in_block > output_length )
4661 length = (uint8_t) output_length;
4662 else
4663 length = tls12_prf->left_in_block;
4664
4665 offset = hash_length - tls12_prf->left_in_block;
4666 memcpy( output, tls12_prf->output_block + offset, length );
4667 output += length;
4668 output_length -= length;
4669 tls12_prf->left_in_block -= length;
4670 }
4671
4672 return( PSA_SUCCESS );
4673}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004674#endif /* MBEDTLS_MD_C */
4675
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004676psa_status_t psa_key_derivation_output_bytes(
4677 psa_key_derivation_operation_t *operation,
4678 uint8_t *output,
4679 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004680{
4681 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004683
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004685 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004687 return PSA_ERROR_BAD_STATE;
4688 }
4689
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004691 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004692 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004694 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004695 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004696 goto exit;
4697 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004698 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004699 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004701 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004702 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4703 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004704 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004705 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004706 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004707 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004708 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004709
Gilles Peskinebef7f142018-07-12 17:22:21 +02004710#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004711 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004712 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004713 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004715 output, output_length );
4716 }
Janos Follathadbec812019-06-14 11:05:39 +01004717 else
Janos Follathadbec812019-06-14 11:05:39 +01004718 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004719 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004720 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004722 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004723 output_length );
4724 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004725 else
4726#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727 {
4728 return( PSA_ERROR_BAD_STATE );
4729 }
4730
4731exit:
4732 if( status != PSA_SUCCESS )
4733 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004734 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 * This allows us to differentiate between exhausted operations and
4736 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4737 * operations. */
4738 psa_algorithm_t alg = operation->alg;
4739 psa_key_derivation_abort( operation );
4740 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741 memset( output, '!', output_length );
4742 }
4743 return( status );
4744}
4745
Gilles Peskine08542d82018-07-19 17:05:42 +02004746#if defined(MBEDTLS_DES_C)
4747static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4748{
4749 if( data_size >= 8 )
4750 mbedtls_des_key_set_parity( data );
4751 if( data_size >= 16 )
4752 mbedtls_des_key_set_parity( data + 8 );
4753 if( data_size >= 24 )
4754 mbedtls_des_key_set_parity( data + 16 );
4755}
4756#endif /* MBEDTLS_DES_C */
4757
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004758static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004759 psa_key_slot_t *slot,
4760 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004762{
4763 uint8_t *data = NULL;
4764 size_t bytes = PSA_BITS_TO_BYTES( bits );
4765 psa_status_t status;
4766
Gilles Peskine8e338702019-07-30 20:06:31 +02004767 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004768 return( PSA_ERROR_INVALID_ARGUMENT );
4769 if( bits % 8 != 0 )
4770 return( PSA_ERROR_INVALID_ARGUMENT );
4771 data = mbedtls_calloc( 1, bytes );
4772 if( data == NULL )
4773 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4774
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004776 if( status != PSA_SUCCESS )
4777 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004778#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004779 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004780 psa_des_set_key_parity( data, bytes );
4781#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004782 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004783
4784exit:
4785 mbedtls_free( data );
4786 return( status );
4787}
4788
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004789psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004791 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004792{
4793 psa_status_t status;
4794 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004795 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004796 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4797 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004798#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4799 if( driver != NULL )
4800 {
4801 /* Deriving a key in a secure element is not implemented yet. */
4802 status = PSA_ERROR_NOT_SUPPORTED;
4803 }
4804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004805 if( status == PSA_SUCCESS )
4806 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004807 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004808 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004810 }
4811 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004812 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004813 if( status != PSA_SUCCESS )
4814 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004815 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004816 *handle = 0;
4817 }
4818 return( status );
4819}
4820
Gilles Peskineeab56e42018-07-12 17:12:33 +02004821
4822
4823/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004824/* Key derivation */
4825/****************************************************************/
4826
Gilles Peskine969c5d62019-01-16 15:53:06 +01004827static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004830{
Janos Follath5fe19732019-06-20 15:09:30 +01004831 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4832 * initialisers for this union leave some bytes unspecified.) */
4833 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4834
Gilles Peskine969c5d62019-01-16 15:53:06 +01004835 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004837 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4838 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4839 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004840 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004841 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004842 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4843 if( hash_size == 0 )
4844 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004845 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4846 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004847 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004848 {
4849 return( PSA_ERROR_NOT_SUPPORTED );
4850 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004852 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004853 }
4854#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004855 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004856 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004857}
4858
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004860 psa_algorithm_t alg )
4861{
4862 psa_status_t status;
4863
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004865 return( PSA_ERROR_BAD_STATE );
4866
Gilles Peskine6843c292019-01-18 16:44:49 +01004867 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4868 return( PSA_ERROR_INVALID_ARGUMENT );
4869 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004870 {
4871 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004872 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004873 }
4874 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4875 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004876 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004877 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878 else
4879 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004880
4881 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004883 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884}
4885
4886#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004887static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004888 psa_algorithm_t hash_alg,
4889 psa_key_derivation_step_t step,
4890 const uint8_t *data,
4891 size_t data_length )
4892{
4893 psa_status_t status;
4894 switch( step )
4895 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004896 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004897 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004899 status = psa_hmac_setup_internal( &hkdf->hmac,
4900 data, data_length,
4901 hash_alg );
4902 if( status != PSA_SUCCESS )
4903 return( status );
4904 hkdf->state = HKDF_STATE_STARTED;
4905 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004906 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907 /* If no salt was provided, use an empty salt. */
4908 if( hkdf->state == HKDF_STATE_INIT )
4909 {
4910 status = psa_hmac_setup_internal( &hkdf->hmac,
4911 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004912 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913 if( status != PSA_SUCCESS )
4914 return( status );
4915 hkdf->state = HKDF_STATE_STARTED;
4916 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004917 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004918 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004919 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4920 data, data_length );
4921 if( status != PSA_SUCCESS )
4922 return( status );
4923 status = psa_hmac_finish_internal( &hkdf->hmac,
4924 hkdf->prk,
4925 sizeof( hkdf->prk ) );
4926 if( status != PSA_SUCCESS )
4927 return( status );
4928 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4929 hkdf->block_number = 0;
4930 hkdf->state = HKDF_STATE_KEYED;
4931 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004932 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004933 if( hkdf->state == HKDF_STATE_OUTPUT )
4934 return( PSA_ERROR_BAD_STATE );
4935 if( hkdf->info_set )
4936 return( PSA_ERROR_BAD_STATE );
4937 hkdf->info_length = data_length;
4938 if( data_length != 0 )
4939 {
4940 hkdf->info = mbedtls_calloc( 1, data_length );
4941 if( hkdf->info == NULL )
4942 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4943 memcpy( hkdf->info, data, data_length );
4944 }
4945 hkdf->info_set = 1;
4946 return( PSA_SUCCESS );
4947 default:
4948 return( PSA_ERROR_INVALID_ARGUMENT );
4949 }
4950}
Janos Follathb03233e2019-06-11 15:30:30 +01004951
Janos Follathf08e2652019-06-13 09:05:41 +01004952static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4953 const uint8_t *data,
4954 size_t data_length )
4955{
4956 if( prf->state != TLS12_PRF_STATE_INIT )
4957 return( PSA_ERROR_BAD_STATE );
4958
Janos Follathd6dce9f2019-07-04 09:11:38 +01004959 if( data_length != 0 )
4960 {
4961 prf->seed = mbedtls_calloc( 1, data_length );
4962 if( prf->seed == NULL )
4963 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004964
Janos Follathd6dce9f2019-07-04 09:11:38 +01004965 memcpy( prf->seed, data, data_length );
4966 prf->seed_length = data_length;
4967 }
Janos Follathf08e2652019-06-13 09:05:41 +01004968
4969 prf->state = TLS12_PRF_STATE_SEED_SET;
4970
4971 return( PSA_SUCCESS );
4972}
4973
Janos Follath81550542019-06-13 14:26:34 +01004974static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4975 psa_algorithm_t hash_alg,
4976 const uint8_t *data,
4977 size_t data_length )
4978{
4979 psa_status_t status;
4980 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4981 return( PSA_ERROR_BAD_STATE );
4982
4983 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4984 if( status != PSA_SUCCESS )
4985 return( status );
4986
4987 prf->state = TLS12_PRF_STATE_KEY_SET;
4988
4989 return( PSA_SUCCESS );
4990}
4991
Janos Follath6660f0e2019-06-17 08:44:03 +01004992static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4993 psa_tls12_prf_key_derivation_t *prf,
4994 psa_algorithm_t hash_alg,
4995 const uint8_t *data,
4996 size_t data_length )
4997{
4998 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004999 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5000 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005001
5002 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5003 return( PSA_ERROR_INVALID_ARGUMENT );
5004
5005 /* Quoting RFC 4279, Section 2:
5006 *
5007 * The premaster secret is formed as follows: if the PSK is N octets
5008 * long, concatenate a uint16 with the value N, N zero octets, a second
5009 * uint16 with the value N, and the PSK itself.
5010 */
5011
Janos Follath40e13932019-06-26 13:22:29 +01005012 *cur++ = ( data_length >> 8 ) & 0xff;
5013 *cur++ = ( data_length >> 0 ) & 0xff;
5014 memset( cur, 0, data_length );
5015 cur += data_length;
5016 *cur++ = pms[0];
5017 *cur++ = pms[1];
5018 memcpy( cur, data, data_length );
5019 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005020
Janos Follath40e13932019-06-26 13:22:29 +01005021 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005022
5023 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5024 return( status );
5025}
5026
Janos Follath63028dd2019-06-13 09:15:47 +01005027static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5028 const uint8_t *data,
5029 size_t data_length )
5030{
5031 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5032 return( PSA_ERROR_BAD_STATE );
5033
Janos Follathd6dce9f2019-07-04 09:11:38 +01005034 if( data_length != 0 )
5035 {
5036 prf->label = mbedtls_calloc( 1, data_length );
5037 if( prf->label == NULL )
5038 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005039
Janos Follathd6dce9f2019-07-04 09:11:38 +01005040 memcpy( prf->label, data, data_length );
5041 prf->label_length = data_length;
5042 }
Janos Follath63028dd2019-06-13 09:15:47 +01005043
5044 prf->state = TLS12_PRF_STATE_LABEL_SET;
5045
5046 return( PSA_SUCCESS );
5047}
5048
Janos Follathb03233e2019-06-11 15:30:30 +01005049static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5050 psa_algorithm_t hash_alg,
5051 psa_key_derivation_step_t step,
5052 const uint8_t *data,
5053 size_t data_length )
5054{
Janos Follathb03233e2019-06-11 15:30:30 +01005055 switch( step )
5056 {
Janos Follathf08e2652019-06-13 09:05:41 +01005057 case PSA_KEY_DERIVATION_INPUT_SEED:
5058 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005059 case PSA_KEY_DERIVATION_INPUT_SECRET:
5060 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005061 case PSA_KEY_DERIVATION_INPUT_LABEL:
5062 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005063 default:
5064 return( PSA_ERROR_INVALID_ARGUMENT );
5065 }
5066}
Janos Follath6660f0e2019-06-17 08:44:03 +01005067
5068static psa_status_t psa_tls12_prf_psk_to_ms_input(
5069 psa_tls12_prf_key_derivation_t *prf,
5070 psa_algorithm_t hash_alg,
5071 psa_key_derivation_step_t step,
5072 const uint8_t *data,
5073 size_t data_length )
5074{
5075 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005076 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005077 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5078 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005079 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005080
5081 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5082}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005083#endif /* MBEDTLS_MD_C */
5084
Janos Follathb80a94e2019-06-12 15:54:46 +01005085static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005087 psa_key_derivation_step_t step,
5088 const uint8_t *data,
5089 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005090{
5091 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005093
5094#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005095 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005097 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005098 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 step, data, data_length );
5100 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005101 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005102 {
Janos Follathb03233e2019-06-11 15:30:30 +01005103 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5104 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5105 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005106 }
5107 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5108 {
5109 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5110 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5111 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 }
5113 else
5114#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117 return( PSA_ERROR_BAD_STATE );
5118 }
5119
5120 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122 return( status );
5123}
5124
Janos Follath51f4a0f2019-06-14 11:35:55 +01005125psa_status_t psa_key_derivation_input_bytes(
5126 psa_key_derivation_operation_t *operation,
5127 psa_key_derivation_step_t step,
5128 const uint8_t *data,
5129 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005130{
Janos Follathc5621512019-06-14 11:27:57 +01005131 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5132 return( PSA_ERROR_INVALID_ARGUMENT );
5133
5134 return( psa_key_derivation_input_internal( operation, step,
5135 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005136}
5137
Janos Follath51f4a0f2019-06-14 11:35:55 +01005138psa_status_t psa_key_derivation_input_key(
5139 psa_key_derivation_operation_t *operation,
5140 psa_key_derivation_step_t step,
5141 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005142{
5143 psa_key_slot_t *slot;
5144 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005145 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005146 PSA_KEY_USAGE_DERIVE,
5147 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005148 if( status != PSA_SUCCESS )
5149 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005150 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005151 return( PSA_ERROR_INVALID_ARGUMENT );
5152 /* Don't allow a key to be used as an input that is usually public.
5153 * This is debatable. It's ok from a cryptographic perspective to
5154 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005155 * the material should be dedicated to a particular input step,
5156 * otherwise this may allow the key to be used in an unintended way
5157 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005158 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005159 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005160 return( psa_key_derivation_input_internal( operation,
5161 step,
5162 slot->data.raw.data,
5163 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005165
5166
5167
5168/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005169/* Key agreement */
5170/****************************************************************/
5171
Gilles Peskinea05219c2018-11-16 16:02:56 +01005172#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005173static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5174 size_t peer_key_length,
5175 const mbedtls_ecp_keypair *our_key,
5176 uint8_t *shared_secret,
5177 size_t shared_secret_size,
5178 size_t *shared_secret_length )
5179{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005180 mbedtls_ecp_keypair *their_key = NULL;
5181 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005182 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005183 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005184
Jaeden Ameroccdce902019-01-10 11:42:27 +00005185 status = psa_import_ec_public_key(
5186 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5187 peer_key, peer_key_length,
5188 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005189 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005190 goto exit;
5191
Jaeden Amero97271b32019-01-10 19:38:51 +00005192 status = mbedtls_to_psa_error(
5193 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5194 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005195 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005196 status = mbedtls_to_psa_error(
5197 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5198 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005199 goto exit;
5200
Jaeden Amero97271b32019-01-10 19:38:51 +00005201 status = mbedtls_to_psa_error(
5202 mbedtls_ecdh_calc_secret( &ecdh,
5203 shared_secret_length,
5204 shared_secret, shared_secret_size,
5205 mbedtls_ctr_drbg_random,
5206 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005207
5208exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005209 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005210 mbedtls_ecp_keypair_free( their_key );
5211 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005212 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005213}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005214#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005215
Gilles Peskine01d718c2018-09-18 12:01:02 +02005216#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5217
Gilles Peskine0216fe12019-04-11 21:23:21 +02005218static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5219 psa_key_slot_t *private_key,
5220 const uint8_t *peer_key,
5221 size_t peer_key_length,
5222 uint8_t *shared_secret,
5223 size_t shared_secret_size,
5224 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005225{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005226 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005227 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005228#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005229 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005230 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005231 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005232 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5233 private_key->data.ecp,
5234 shared_secret, shared_secret_size,
5235 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005236#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005237 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005238 (void) private_key;
5239 (void) peer_key;
5240 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005241 (void) shared_secret;
5242 (void) shared_secret_size;
5243 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005244 return( PSA_ERROR_NOT_SUPPORTED );
5245 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005246}
5247
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005248/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005249 * to potentially free embedded data structures and wipe confidential data.
5250 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253 psa_key_slot_t *private_key,
5254 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005255 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005256{
5257 psa_status_t status;
5258 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5259 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261
5262 /* Step 1: run the secret agreement algorithm to generate the shared
5263 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005264 status = psa_key_agreement_raw_internal( ka_alg,
5265 private_key,
5266 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005267 shared_secret,
5268 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005269 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005270 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005271 goto exit;
5272
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005273 /* Step 2: set up the key derivation to generate key material from
5274 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005275 status = psa_key_derivation_input_internal( operation, step,
5276 shared_secret,
5277 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005278
Gilles Peskine12313cd2018-06-20 00:20:32 +02005279exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005280 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005281 return( status );
5282}
5283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005285 psa_key_derivation_step_t step,
5286 psa_key_handle_t private_key,
5287 const uint8_t *peer_key,
5288 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005289{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005290 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005291 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005293 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005294 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005295 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005296 if( status != PSA_SUCCESS )
5297 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005299 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005301 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005303 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005304}
5305
Gilles Peskinebe697d82019-05-16 18:00:41 +02005306psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5307 psa_key_handle_t private_key,
5308 const uint8_t *peer_key,
5309 size_t peer_key_length,
5310 uint8_t *output,
5311 size_t output_size,
5312 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005313{
5314 psa_key_slot_t *slot;
5315 psa_status_t status;
5316
5317 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5318 {
5319 status = PSA_ERROR_INVALID_ARGUMENT;
5320 goto exit;
5321 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005322 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005323 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005324 if( status != PSA_SUCCESS )
5325 goto exit;
5326
5327 status = psa_key_agreement_raw_internal( alg, slot,
5328 peer_key, peer_key_length,
5329 output, output_size,
5330 output_length );
5331
5332exit:
5333 if( status != PSA_SUCCESS )
5334 {
5335 /* If an error happens and is not handled properly, the output
5336 * may be used as a key to protect sensitive data. Arrange for such
5337 * a key to be random, which is likely to result in decryption or
5338 * verification errors. This is better than filling the buffer with
5339 * some constant data such as zeros, which would result in the data
5340 * being protected with a reproducible, easily knowable key.
5341 */
5342 psa_generate_random( output, output_size );
5343 *output_length = output_size;
5344 }
5345 return( status );
5346}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005347
5348
5349/****************************************************************/
5350/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005351/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005352
Gilles Peskine4c317f42018-07-12 01:24:09 +02005353psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005354 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005355{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005356 int ret;
5357 GUARD_MODULE_INITIALIZED;
5358
Gilles Peskinef181eca2019-08-07 13:49:00 +02005359 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5360 {
5361 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5362 output,
5363 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5364 if( ret != 0 )
5365 return( mbedtls_to_psa_error( ret ) );
5366 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5367 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5368 }
5369
Gilles Peskinee59236f2018-01-27 23:32:46 +01005370 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5371 return( mbedtls_to_psa_error( ret ) );
5372}
5373
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005374#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5375#include "mbedtls/entropy_poll.h"
5376
Gilles Peskine7228da22019-07-15 11:06:15 +02005377psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005378 size_t seed_size )
5379{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005380 if( global_data.initialized )
5381 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005382
5383 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5384 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5385 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5386 return( PSA_ERROR_INVALID_ARGUMENT );
5387
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005388 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005389}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005390#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005391
Gilles Peskinee56e8782019-04-26 17:34:02 +02005392#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Michael Thomas863b5d62020-02-10 19:41:16 +00005393psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005394 size_t domain_parameters_size,
5395 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005396{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005397 size_t i;
5398 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005399
Gilles Peskinee56e8782019-04-26 17:34:02 +02005400 if( domain_parameters_size == 0 )
5401 {
5402 *exponent = 65537;
5403 return( PSA_SUCCESS );
5404 }
5405
5406 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5407 * support values that fit in a 32-bit integer, which is larger than
5408 * int on just about every platform anyway. */
5409 if( domain_parameters_size > sizeof( acc ) )
5410 return( PSA_ERROR_NOT_SUPPORTED );
5411 for( i = 0; i < domain_parameters_size; i++ )
5412 acc = ( acc << 8 ) | domain_parameters[i];
5413 if( acc > INT_MAX )
5414 return( PSA_ERROR_NOT_SUPPORTED );
5415 *exponent = acc;
5416 return( PSA_SUCCESS );
5417}
5418#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5419
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005420static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005421 psa_key_slot_t *slot, size_t bits,
5422 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005423{
Gilles Peskine8e338702019-07-30 20:06:31 +02005424 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005425
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005427 return( PSA_ERROR_INVALID_ARGUMENT );
5428
Gilles Peskinee59236f2018-01-27 23:32:46 +01005429 if( key_type_is_raw_bytes( type ) )
5430 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005431 psa_status_t status;
Michael Thomas1d573ec2020-01-21 04:02:54 +00005432 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5433 if( status != PSA_SUCCESS )
5434 return( status );
5435 status = psa_generate_random( slot->data.raw.data,
Michael Thomasa8ac0162020-01-23 02:55:53 +00005436 slot->data.raw.bytes );
Michael Thomas1d573ec2020-01-21 04:02:54 +00005437 if( status != PSA_SUCCESS )
5438 return( status );
Michael Thomasa8ac0162020-01-23 02:55:53 +00005439#if defined(MBEDTLS_DES_C)
Michael Thomas1d573ec2020-01-21 04:02:54 +00005440 if( type == PSA_KEY_TYPE_DES )
5441 psa_des_set_key_parity( slot->data.raw.data,
5442 slot->data.raw.bytes );
Michael Thomasa8ac0162020-01-23 02:55:53 +00005443#endif /* MBEDTLS_DES_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005444 }
5445 else
5446
5447#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005448 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449 {
5450 mbedtls_rsa_context *rsa;
5451 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005452 int exponent;
5453 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005454 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5455 return( PSA_ERROR_NOT_SUPPORTED );
5456 /* Accept only byte-aligned keys, for the same reasons as
5457 * in psa_import_rsa_key(). */
5458 if( bits % 8 != 0 )
5459 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005460 status = psa_read_rsa_exponent( domain_parameters,
5461 domain_parameters_size,
5462 &exponent );
5463 if( status != PSA_SUCCESS )
5464 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5466 if( rsa == NULL )
5467 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5468 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5469 ret = mbedtls_rsa_gen_key( rsa,
5470 mbedtls_ctr_drbg_random,
5471 &global_data.ctr_drbg,
5472 (unsigned int) bits,
5473 exponent );
5474 if( ret != 0 )
5475 {
5476 mbedtls_rsa_free( rsa );
5477 mbedtls_free( rsa );
5478 return( mbedtls_to_psa_error( ret ) );
5479 }
5480 slot->data.rsa = rsa;
5481 }
5482 else
5483#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5484
5485#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005486 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005487 {
5488 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5489 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5490 const mbedtls_ecp_curve_info *curve_info =
5491 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5492 mbedtls_ecp_keypair *ecp;
5493 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005494 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005495 return( PSA_ERROR_NOT_SUPPORTED );
5496 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5497 return( PSA_ERROR_NOT_SUPPORTED );
5498 if( curve_info->bit_size != bits )
5499 return( PSA_ERROR_INVALID_ARGUMENT );
5500 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5501 if( ecp == NULL )
5502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5503 mbedtls_ecp_keypair_init( ecp );
5504 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5505 mbedtls_ctr_drbg_random,
5506 &global_data.ctr_drbg );
5507 if( ret != 0 )
5508 {
5509 mbedtls_ecp_keypair_free( ecp );
5510 mbedtls_free( ecp );
5511 return( mbedtls_to_psa_error( ret ) );
5512 }
5513 slot->data.ecp = ecp;
5514 }
5515 else
5516#endif /* MBEDTLS_ECP_C */
5517
5518 return( PSA_ERROR_NOT_SUPPORTED );
5519
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005520 return( PSA_SUCCESS );
5521}
Michael Thomas4c53fc72020-01-24 03:45:51 +00005522
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005523psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005524 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005525{
5526 psa_status_t status;
5527 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005528 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005529
Gilles Peskinedf179142019-07-15 22:02:14 +02005530 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5531 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005532 if( status != PSA_SUCCESS )
5533 goto exit;
5534
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005535#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5536 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005537 {
Gilles Peskine11792082019-08-06 18:36:36 +02005538 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5539 size_t pubkey_length = 0; /* We don't support this feature yet */
5540 if( drv->key_management == NULL ||
5541 drv->key_management->p_generate == NULL )
5542 {
5543 status = PSA_ERROR_NOT_SUPPORTED;
5544 goto exit;
5545 }
5546 status = drv->key_management->p_generate(
5547 psa_get_se_driver_context( driver ),
5548 slot->data.se.slot_number, attributes,
5549 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005550 }
Gilles Peskine11792082019-08-06 18:36:36 +02005551 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Michael Thomas4c53fc72020-01-24 03:45:51 +00005553#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
Michael Thomas153cd2f2020-01-22 16:54:38 +00005554 if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
Michael Thomas1d573ec2020-01-21 04:02:54 +00005555 {
5556 status = psa_generate_key_vendor(slot, attributes->core.bits,
5557 attributes->domain_parameters, attributes->domain_parameters_size);
Michael Thomasefcf4cf2020-02-24 17:46:31 +00005558 goto exit;
Michael Thomas1d573ec2020-01-21 04:02:54 +00005559 }
5560 else
Michael Thomas863b5d62020-02-10 19:41:16 +00005561#endif /* MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005562 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005563 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005564 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005565 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005566 }
Gilles Peskine11792082019-08-06 18:36:36 +02005567exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005568 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005569 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005570 if( status != PSA_SUCCESS )
5571 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005572 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005573 *handle = 0;
5574 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005575 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005576}
5577
5578
Gilles Peskinee59236f2018-01-27 23:32:46 +01005579
5580/****************************************************************/
5581/* Module setup */
5582/****************************************************************/
5583
Gilles Peskine5e769522018-11-20 21:59:56 +01005584psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5585 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5586 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5587{
5588 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5589 return( PSA_ERROR_BAD_STATE );
5590 global_data.entropy_init = entropy_init;
5591 global_data.entropy_free = entropy_free;
5592 return( PSA_SUCCESS );
5593}
5594
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595void mbedtls_psa_crypto_free( void )
5596{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005597 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005598 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5599 {
5600 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005601 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005602 }
5603 /* Wipe all remaining data, including configuration.
5604 * In particular, this sets all state indicator to the value
5605 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005606 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005607#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005608 /* Unregister all secure element drivers, so that we restart from
5609 * a pristine state. */
5610 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005611#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005612}
5613
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005614#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5615/** Recover a transaction that was interrupted by a power failure.
5616 *
5617 * This function is called during initialization, before psa_crypto_init()
5618 * returns. If this function returns a failure status, the initialization
5619 * fails.
5620 */
5621static psa_status_t psa_crypto_recover_transaction(
5622 const psa_crypto_transaction_t *transaction )
5623{
5624 switch( transaction->unknown.type )
5625 {
5626 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5627 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005628 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005629 * is implemented.
5630 * https://github.com/ARMmbed/mbed-crypto/issues/218
5631 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005632 default:
5633 /* We found an unsupported transaction in the storage.
5634 * We don't know what state the storage is in. Give up. */
5635 return( PSA_ERROR_STORAGE_FAILURE );
5636 }
5637}
5638#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5639
Gilles Peskinee59236f2018-01-27 23:32:46 +01005640psa_status_t psa_crypto_init( void )
5641{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005642 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005643 const unsigned char drbg_seed[] = "PSA";
5644
Gilles Peskinec6b69072018-11-20 21:42:52 +01005645 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005646 if( global_data.initialized != 0 )
5647 return( PSA_SUCCESS );
5648
Gilles Peskine5e769522018-11-20 21:59:56 +01005649 /* Set default configuration if
5650 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5651 if( global_data.entropy_init == NULL )
5652 global_data.entropy_init = mbedtls_entropy_init;
5653 if( global_data.entropy_free == NULL )
5654 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005655
Gilles Peskinec6b69072018-11-20 21:42:52 +01005656 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005657 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005658#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5659 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5660 /* The PSA entropy injection feature depends on using NV seed as an entropy
5661 * source. Add NV seed as an entropy source for PSA entropy injection. */
5662 mbedtls_entropy_add_source( &global_data.entropy,
5663 mbedtls_nv_seed_poll, NULL,
5664 MBEDTLS_ENTROPY_BLOCK_SIZE,
5665 MBEDTLS_ENTROPY_SOURCE_STRONG );
5666#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005667 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005668 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005669 status = mbedtls_to_psa_error(
5670 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5671 mbedtls_entropy_func,
5672 &global_data.entropy,
5673 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5674 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005676 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005677
Gilles Peskine66fb1262018-12-10 16:29:04 +01005678 status = psa_initialize_key_slots( );
5679 if( status != PSA_SUCCESS )
5680 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005681
Gilles Peskine4b734222019-07-24 15:56:31 +02005682#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005683 status = psa_crypto_load_transaction( );
5684 if( status == PSA_SUCCESS )
5685 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005686 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5687 if( status != PSA_SUCCESS )
5688 goto exit;
5689 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005690 }
5691 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5692 {
5693 /* There's no transaction to complete. It's all good. */
5694 status = PSA_SUCCESS;
5695 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005696#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005697
Gilles Peskinec6b69072018-11-20 21:42:52 +01005698 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005699 global_data.initialized = 1;
5700
5701exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005702 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005703 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005704 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005705}
5706
5707#endif /* MBEDTLS_PSA_CRYPTO_C */