blob: e8ab01f639d7ee67ae7f467ac7c800e8d037238f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
454#if defined(MBEDTLS_MD_C)
455 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200456#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
495 /* Allocate memory for the key */
496 raw->bytes = PSA_BITS_TO_BYTES( bits );
497 raw->data = mbedtls_calloc( 1, raw->bytes );
498 if( raw->data == NULL )
499 {
500 raw->bytes = 0;
501 return( PSA_ERROR_INSUFFICIENT_MEMORY );
502 }
503 return( PSA_SUCCESS );
504}
505
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100507/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
508 * that are not a multiple of 8) well. For example, there is only
509 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
510 * way to return the exact bit size of a key.
511 * To keep things simple, reject non-byte-aligned key sizes. */
512static psa_status_t psa_check_rsa_key_byte_aligned(
513 const mbedtls_rsa_context *rsa )
514{
515 mbedtls_mpi n;
516 psa_status_t status;
517 mbedtls_mpi_init( &n );
518 status = mbedtls_to_psa_error(
519 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
520 if( status == PSA_SUCCESS )
521 {
522 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
523 status = PSA_ERROR_NOT_SUPPORTED;
524 }
525 mbedtls_mpi_free( &n );
526 return( status );
527}
528
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000529static psa_status_t psa_import_rsa_key( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 mbedtls_rsa_context **p_rsa )
533{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534 psa_status_t status;
535 mbedtls_pk_context pk;
536 mbedtls_rsa_context *rsa;
537 size_t bits;
538
539 mbedtls_pk_init( &pk );
540
541 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
553 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
559 rsa = mbedtls_pk_rsa( pk );
560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
569 status = psa_check_rsa_key_byte_aligned( rsa );
570
571exit:
572 /* Free the content of the pk object only on error. */
573 if( status != PSA_SUCCESS )
574 {
575 mbedtls_pk_free( &pk );
576 return( status );
577 }
578
579 /* On success, store the content of the object in the RSA context. */
580 *p_rsa = rsa;
581
582 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583}
584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586#if defined(MBEDTLS_ECP_C)
587
588/* Import a public key given as the uncompressed representation defined by SEC1
589 * 2.3.3 as the content of an ECPoint. */
590static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
591 const uint8_t *data,
592 size_t data_length,
593 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000596 mbedtls_ecp_keypair *ecp = NULL;
597 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598
599 *p_ecp = NULL;
600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
601 if( ecp == NULL )
602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
603 mbedtls_ecp_keypair_init( ecp );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the public value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
613 data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616
617 /* Check that the point is on the curve. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 *p_ecp = ecp;
624 return( PSA_SUCCESS );
625
626exit:
627 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair_free( ecp );
630 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
637/* Import a private key given as a byte string which is the private value
638 * in big-endian order. */
639static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
640 const uint8_t *data,
641 size_t data_length,
642 mbedtls_ecp_keypair **p_ecp )
643{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 mbedtls_ecp_keypair *ecp = NULL;
646 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200648 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649 return( PSA_ERROR_INVALID_ARGUMENT );
650
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 *p_ecp = NULL;
652 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
653 if( ecp == NULL )
654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000655 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
657 /* Load the group. */
658 status = mbedtls_to_psa_error(
659 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662 /* Load the secret value. */
663 status = mbedtls_to_psa_error(
664 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
665 if( status != PSA_SUCCESS )
666 goto exit;
667 /* Validate the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
670 if( status != PSA_SUCCESS )
671 goto exit;
672 /* Calculate the public key from the private key. */
673 status = mbedtls_to_psa_error(
674 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
675 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678
679 *p_ecp = ecp;
680 return( PSA_SUCCESS );
681
682exit:
683 if( ecp != NULL )
684 {
685 mbedtls_ecp_keypair_free( ecp );
686 mbedtls_free( ecp );
687 }
688 return( status );
689}
690#endif /* defined(MBEDTLS_ECP_C) */
691
Gilles Peskineb46bef22019-07-30 21:32:04 +0200692
693/** Return the size of the key in the given slot, in bits.
694 *
695 * \param[in] slot A key slot.
696 *
697 * \return The key size in bits, read from the metadata in the slot.
698 */
699static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
700{
701 return( slot->attr.bits );
702}
703
704/** Calculate the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot containing a transparent key.
707 *
708 * \return The key size in bits, calculated from the key data.
709 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200710static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 size_t bits = 0; /* return 0 on an empty slot */
713
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
718 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#endif /* defined(MBEDTLS_RSA_C) */
720#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
722 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724
725 /* We know that the size fits in psa_key_bits_t thanks to checks
726 * when the key was created. */
727 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728}
729
Gilles Peskine8e338702019-07-30 20:06:31 +0200730/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100731 * previously. This function assumes that the slot does not contain
732 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100733psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
734 const uint8_t *data,
735 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Gilles Peskine8e338702019-07-30 20:06:31 +0200739 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200742 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 if( data_length > SIZE_MAX / 8 )
744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750 &slot->data.raw );
751 if( status != PSA_SUCCESS )
752 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200753 if( data_length != 0 )
754 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 }
756 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100757#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200758 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 data, data_length,
762 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000766 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 data, data_length,
769 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000777 data, data_length,
778 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 }
780 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
783 return( PSA_ERROR_NOT_SUPPORTED );
784 }
Darryl Green940d72c2018-07-13 13:18:51 +0100785
Gilles Peskineb46bef22019-07-30 21:32:04 +0200786 if( status == PSA_SUCCESS )
787 {
788 /* Write the actual key size to the slot.
789 * psa_start_key_creation() wrote the size declared by the
790 * caller, which may be 0 (meaning unspecified) or wrong. */
791 slot->attr.bits = psa_calculate_key_bits( slot );
792 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 return( status );
794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
801 psa_algorithm_t alg1,
802 psa_algorithm_t alg2 )
803{
Gilles Peskine549ea862019-05-22 11:45:59 +0200804 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 if( alg1 == alg2 )
806 return( alg1 );
807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
809 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
810 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
811 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
812 {
813 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
814 return( alg2 );
815 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
816 return( alg1 );
817 }
818 /* If the policies are incompatible, allow nothing. */
819 return( 0 );
820}
821
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
823 psa_algorithm_t requested_alg )
824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826 if( requested_alg == policy_alg )
827 return( 1 );
828 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 * and requested_alg is the same hash-and-sign family with any hash,
830 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
832 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
833 {
834 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
835 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
836 }
837 /* If it isn't permitted, it's forbidden. */
838 return( 0 );
839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
844 */
845static int psa_key_policy_permits( const psa_key_policy_t *policy,
846 psa_algorithm_t alg )
847{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200848 return( psa_key_algorithm_permits( policy->alg, alg ) ||
849 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850}
851
Gilles Peskinef603c712019-01-19 13:40:11 +0100852/** Restrict a key policy based on a constraint.
853 *
854 * \param[in,out] policy The policy to restrict.
855 * \param[in] constraint The policy constraint to apply.
856 *
857 * \retval #PSA_SUCCESS
858 * \c *policy contains the intersection of the original value of
859 * \c *policy and \c *constraint.
860 * \retval #PSA_ERROR_INVALID_ARGUMENT
861 * \c *policy and \c *constraint are incompatible.
862 * \c *policy is unchanged.
863 */
864static psa_status_t psa_restrict_key_policy(
865 psa_key_policy_t *policy,
866 const psa_key_policy_t *constraint )
867{
868 psa_algorithm_t intersection_alg =
869 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 psa_algorithm_t intersection_alg2 =
871 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Darryl Green06fd18d2018-07-16 11:21:11 +0100882/** Retrieve a slot which must contain a key. The key must have allow all the
883 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
884 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100885static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100887 psa_key_usage_t usage,
888 psa_algorithm_t alg )
889{
890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100891 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100892
893 *p_slot = NULL;
894
Gilles Peskinec5487a82018-12-03 18:08:14 +0100895 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100896 if( status != PSA_SUCCESS )
897 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 /* Enforce that usage policy for the key slot contains all the flags
900 * required by the usage parameter. There is one exception: public
901 * keys can always be exported, so we treat public key objects as
902 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200903 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907
908 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
911
912 *p_slot = slot;
913 return( PSA_SUCCESS );
914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916/** Retrieve a slot which must contain a transparent key.
917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200921 * This is a temporary function to use instead of psa_get_key_from_slot()
922 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923 */
924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
925static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
926 psa_key_slot_t **p_slot,
927 psa_key_usage_t usage,
928 psa_algorithm_t alg )
929{
930 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200933 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 {
935 *p_slot = NULL;
936 return( PSA_ERROR_NOT_SUPPORTED );
937 }
938 return( PSA_SUCCESS );
939}
940#else /* MBEDTLS_PSA_CRYPTO_SE_C */
941/* With no secure element support, all keys are transparent. */
942#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
943 psa_get_key_from_slot( handle, p_slot, usage, alg )
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
945
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100946/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000948{
Gilles Peskine73167e12019-07-12 23:44:37 +0200949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
950 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000951 {
952 /* No key material to clean. */
953 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200954 else
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200956 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000957 {
958 /* No key material to clean. */
959 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 mbedtls_free( slot->data.raw.data );
963 }
964 else
965#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_rsa_free( slot->data.rsa );
969 mbedtls_free( slot->data.rsa );
970 }
971 else
972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_ecp_keypair_free( slot->data.ecp );
977 mbedtls_free( slot->data.ecp );
978 }
979 else
980#endif /* defined(MBEDTLS_ECP_C) */
981 {
982 /* Shouldn't happen: the key type is not any type that we
983 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200984 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000985 }
986
987 return( PSA_SUCCESS );
988}
989
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990/** Completely wipe a slot in memory, including its policy.
991 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100992psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993{
994 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200995 /* Multipart operations may still be using the key. This is safe
996 * because all multipart operation objects are independent from
997 * the key slot: if they need to access the key after the setup
998 * phase, they have a copy of the key. Note that this means that
999 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000 /* At this point, key material and other type-specific content has
1001 * been wiped. Clear remaining metadata. We can call memset and not
1002 * zeroize because the metadata is not particularly sensitive. */
1003 memset( slot, 0, sizeof( *slot ) );
1004 return( status );
1005}
1006
Gilles Peskinec5487a82018-12-03 18:08:14 +01001007psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001009 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001010 psa_status_t status; /* status of the last operation */
1011 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 psa_se_drv_table_entry_t *driver;
1014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskine1841cf42019-10-08 15:48:25 +02001016 if( handle == 0 )
1017 return( PSA_SUCCESS );
1018
Gilles Peskinec5487a82018-12-03 18:08:14 +01001019 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 if( status != PSA_SUCCESS )
1021 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022
1023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001024 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001025 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001026 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001027 /* For a key in a secure element, we need to do three things:
1028 * remove the key file in internal storage, destroy the
1029 * key inside the secure element, and update the driver's
1030 * persistent data. Start a transaction that will encompass these
1031 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001033 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001034 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 status = psa_crypto_save_transaction( );
1037 if( status != PSA_SUCCESS )
1038 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001039 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001040 /* We should still try to destroy the key in the secure
1041 * element and the key metadata in storage. This is especially
1042 * important if the error is that the storage is full.
1043 * But how to do it exactly without risking an inconsistent
1044 * state after a reset?
1045 * https://github.com/ARMmbed/mbed-crypto/issues/215
1046 */
1047 overall_status = status;
1048 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 }
1050
Gilles Peskine354f7672019-07-12 23:46:38 +02001051 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 if( overall_status == PSA_SUCCESS )
1053 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001054 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001055#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1056
Darryl Greend49a4992018-06-18 17:27:26 +01001057#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001058 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001059 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001060 status = psa_destroy_persistent_key( slot->attr.id );
1061 if( overall_status == PSA_SUCCESS )
1062 overall_status = status;
1063
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001064 /* TODO: other slots may have a copy of the same key. We should
1065 * invalidate them.
1066 * https://github.com/ARMmbed/mbed-crypto/issues/214
1067 */
Darryl Greend49a4992018-06-18 17:27:26 +01001068 }
1069#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001070
Gilles Peskinefc762652019-07-22 19:30:34 +02001071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1072 if( driver != NULL )
1073 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001074 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
1077 status = psa_crypto_stop_transaction( );
1078 if( overall_status == PSA_SUCCESS )
1079 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001080 }
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1082
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1084exit:
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001086 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1088 if( overall_status == PSA_SUCCESS )
1089 overall_status = status;
1090 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091}
1092
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001094{
Gilles Peskineb699f072019-04-26 16:06:02 +02001095 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001096 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001097}
1098
Gilles Peskineb699f072019-04-26 16:06:02 +02001099psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1100 psa_key_type_t type,
1101 const uint8_t *data,
1102 size_t data_length )
1103{
1104 uint8_t *copy = NULL;
1105
1106 if( data_length != 0 )
1107 {
1108 copy = mbedtls_calloc( 1, data_length );
1109 if( copy == NULL )
1110 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1111 memcpy( copy, data, data_length );
1112 }
1113 /* After this point, this function is guaranteed to succeed, so it
1114 * can start modifying `*attributes`. */
1115
1116 if( attributes->domain_parameters != NULL )
1117 {
1118 mbedtls_free( attributes->domain_parameters );
1119 attributes->domain_parameters = NULL;
1120 attributes->domain_parameters_size = 0;
1121 }
1122
1123 attributes->domain_parameters = copy;
1124 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001125 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001126 return( PSA_SUCCESS );
1127}
1128
1129psa_status_t psa_get_key_domain_parameters(
1130 const psa_key_attributes_t *attributes,
1131 uint8_t *data, size_t data_size, size_t *data_length )
1132{
1133 if( attributes->domain_parameters_size > data_size )
1134 return( PSA_ERROR_BUFFER_TOO_SMALL );
1135 *data_length = attributes->domain_parameters_size;
1136 if( attributes->domain_parameters_size != 0 )
1137 memcpy( data, attributes->domain_parameters,
1138 attributes->domain_parameters_size );
1139 return( PSA_SUCCESS );
1140}
1141
1142#if defined(MBEDTLS_RSA_C)
1143static psa_status_t psa_get_rsa_public_exponent(
1144 const mbedtls_rsa_context *rsa,
1145 psa_key_attributes_t *attributes )
1146{
1147 mbedtls_mpi mpi;
1148 int ret;
1149 uint8_t *buffer = NULL;
1150 size_t buflen;
1151 mbedtls_mpi_init( &mpi );
1152
1153 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1154 if( ret != 0 )
1155 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001156 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1157 {
1158 /* It's the default value, which is reported as an empty string,
1159 * so there's nothing to do. */
1160 goto exit;
1161 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001162
1163 buflen = mbedtls_mpi_size( &mpi );
1164 buffer = mbedtls_calloc( 1, buflen );
1165 if( buffer == NULL )
1166 {
1167 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1168 goto exit;
1169 }
1170 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1171 if( ret != 0 )
1172 goto exit;
1173 attributes->domain_parameters = buffer;
1174 attributes->domain_parameters_size = buflen;
1175
1176exit:
1177 mbedtls_mpi_free( &mpi );
1178 if( ret != 0 )
1179 mbedtls_free( buffer );
1180 return( mbedtls_to_psa_error( ret ) );
1181}
1182#endif /* MBEDTLS_RSA_C */
1183
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001184/** Retrieve all the publicly-accessible attributes of a key.
1185 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001186psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1187 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001189 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001190 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001192 psa_reset_key_attributes( attributes );
1193
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001194 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001195 if( status != PSA_SUCCESS )
1196 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001197
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001198 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001199 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1200 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1201
1202#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1203 if( psa_key_slot_is_external( slot ) )
1204 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001206
Gilles Peskine8e338702019-07-30 20:06:31 +02001207 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 {
1209#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001210 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001212#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001213 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001214 * is not yet implemented.
1215 * https://github.com/ARMmbed/mbed-crypto/issues/216
1216 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001217 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001218 break;
1219#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1221 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001222#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 default:
1224 /* Nothing else to do. */
1225 break;
1226 }
1227
1228 if( status != PSA_SUCCESS )
1229 psa_reset_key_attributes( attributes );
1230 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231}
1232
Gilles Peskinec8000c02019-08-02 20:15:51 +02001233#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1234psa_status_t psa_get_key_slot_number(
1235 const psa_key_attributes_t *attributes,
1236 psa_key_slot_number_t *slot_number )
1237{
1238 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1239 {
1240 *slot_number = attributes->slot_number;
1241 return( PSA_SUCCESS );
1242 }
1243 else
1244 return( PSA_ERROR_INVALID_ARGUMENT );
1245}
1246#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1247
Jaeden Ameroccdce902019-01-10 11:42:27 +00001248#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001249static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1250 unsigned char *buf, size_t size )
1251{
1252 int ret;
1253 unsigned char *c;
1254 size_t len = 0;
1255
1256 c = buf + size;
1257
1258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1259
1260 return( (int) len );
1261}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001262#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001263
Gilles Peskinef603c712019-01-19 13:40:11 +01001264static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1265 uint8_t *data,
1266 size_t data_size,
1267 size_t *data_length,
1268 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269{
Gilles Peskine5d309672019-07-12 23:47:28 +02001270#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1271 const psa_drv_se_t *drv;
1272 psa_drv_se_context_t *drv_context;
1273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1274
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001275 *data_length = 0;
1276
Gilles Peskine8e338702019-07-30 20:06:31 +02001277 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001278 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001279
Gilles Peskinef9168942019-09-12 19:20:29 +02001280 /* Reject a zero-length output buffer now, since this can never be a
1281 * valid key representation. This way we know that data must be a valid
1282 * pointer and we can do things like memset(data, ..., data_size). */
1283 if( data_size == 0 )
1284 return( PSA_ERROR_BUFFER_TOO_SMALL );
1285
Gilles Peskine5d309672019-07-12 23:47:28 +02001286#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001287 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001288 {
1289 psa_drv_se_export_key_t method;
1290 if( drv->key_management == NULL )
1291 return( PSA_ERROR_NOT_SUPPORTED );
1292 method = ( export_public_key ?
1293 drv->key_management->p_export_public :
1294 drv->key_management->p_export );
1295 if( method == NULL )
1296 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001297 return( method( drv_context,
1298 slot->data.se.slot_number,
1299 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001300 }
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1302
Gilles Peskine8e338702019-07-30 20:06:31 +02001303 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304 {
1305 if( slot->data.raw.bytes > data_size )
1306 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001307 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1308 memset( data + slot->data.raw.bytes, 0,
1309 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310 *data_length = slot->data.raw.bytes;
1311 return( PSA_SUCCESS );
1312 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001313#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001315 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001316 psa_status_t status;
1317
Gilles Peskineb46bef22019-07-30 21:32:04 +02001318 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001319 if( bytes > data_size )
1320 return( PSA_ERROR_BUFFER_TOO_SMALL );
1321 status = mbedtls_to_psa_error(
1322 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1323 if( status != PSA_SUCCESS )
1324 return( status );
1325 memset( data + bytes, 0, data_size - bytes );
1326 *data_length = bytes;
1327 return( PSA_SUCCESS );
1328 }
1329#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330 else
Moran Peker17e36e12018-05-02 12:55:20 +03001331 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001332#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001333 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1334 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001335 {
Moran Pekera998bc62018-04-16 18:16:20 +03001336 mbedtls_pk_context pk;
1337 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001338 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001339 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001340#if defined(MBEDTLS_RSA_C)
1341 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001342 pk.pk_info = &mbedtls_rsa_info;
1343 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001344#else
1345 return( PSA_ERROR_NOT_SUPPORTED );
1346#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001347 }
1348 else
1349 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001350#if defined(MBEDTLS_ECP_C)
1351 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001352 pk.pk_info = &mbedtls_eckey_info;
1353 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001354#else
1355 return( PSA_ERROR_NOT_SUPPORTED );
1356#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001357 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001358 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001359 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001360 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001361 }
Moran Peker17e36e12018-05-02 12:55:20 +03001362 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001363 {
Moran Peker17e36e12018-05-02 12:55:20 +03001364 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001365 }
Moran Peker60364322018-04-29 11:34:58 +03001366 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001368 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001369 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001370 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001371 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1372 * Move the data to the beginning and erase remaining data
1373 * at the original location. */
1374 if( 2 * (size_t) ret <= data_size )
1375 {
1376 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001377 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001378 }
1379 else if( (size_t) ret < data_size )
1380 {
1381 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001382 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001383 }
Moran Pekera998bc62018-04-16 18:16:20 +03001384 *data_length = ret;
1385 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001386 }
1387 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001388#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001389 {
1390 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001391 it is valid for a special-purpose implementation to omit
1392 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 return( PSA_ERROR_NOT_SUPPORTED );
1394 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395 }
1396}
1397
Gilles Peskinec5487a82018-12-03 18:08:14 +01001398psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001399 uint8_t *data,
1400 size_t data_size,
1401 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001402{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001403 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001404 psa_status_t status;
1405
1406 /* Set the key to empty now, so that even when there are errors, we always
1407 * set data_length to a value between 0 and data_size. On error, setting
1408 * the key to empty is a good choice because an empty key representation is
1409 * unlikely to be accepted anywhere. */
1410 *data_length = 0;
1411
1412 /* Export requires the EXPORT flag. There is an exception for public keys,
1413 * which don't require any flag, but psa_get_key_from_slot takes
1414 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001415 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001416 if( status != PSA_SUCCESS )
1417 return( status );
1418 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001419 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001420}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421
Gilles Peskinec5487a82018-12-03 18:08:14 +01001422psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001423 uint8_t *data,
1424 size_t data_size,
1425 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001427 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 psa_status_t status;
1429
1430 /* Set the key to empty now, so that even when there are errors, we always
1431 * set data_length to a value between 0 and data_size. On error, setting
1432 * the key to empty is a good choice because an empty key representation is
1433 * unlikely to be accepted anywhere. */
1434 *data_length = 0;
1435
1436 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001437 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001438 if( status != PSA_SUCCESS )
1439 return( status );
1440 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001441 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001442}
1443
Gilles Peskine91e8c332019-08-02 19:19:39 +02001444#if defined(static_assert)
1445static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1446 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001447static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001448 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001449static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001450 "One or more key attribute flag is listed as both internal-only and external-only" );
1451#endif
1452
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001453/** Validate that a key policy is internally well-formed.
1454 *
1455 * This function only rejects invalid policies. It does not validate the
1456 * consistency of the policy with respect to other attributes of the key
1457 * such as the key type.
1458 */
1459static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001460{
1461 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001462 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 PSA_KEY_USAGE_ENCRYPT |
1464 PSA_KEY_USAGE_DECRYPT |
1465 PSA_KEY_USAGE_SIGN |
1466 PSA_KEY_USAGE_VERIFY |
1467 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1468 return( PSA_ERROR_INVALID_ARGUMENT );
1469
Gilles Peskine4747d192019-04-17 15:05:45 +02001470 return( PSA_SUCCESS );
1471}
1472
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001473/** Validate the internal consistency of key attributes.
1474 *
1475 * This function only rejects invalid attribute values. If does not
1476 * validate the consistency of the attributes with any key data that may
1477 * be involved in the creation of the key.
1478 *
1479 * Call this function early in the key creation process.
1480 *
1481 * \param[in] attributes Key attributes for the new key.
1482 * \param[out] p_drv On any return, the driver for the key, if any.
1483 * NULL for a transparent key.
1484 *
1485 */
1486static psa_status_t psa_validate_key_attributes(
1487 const psa_key_attributes_t *attributes,
1488 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001489{
1490 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491
1492 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001493 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001494 status = psa_validate_persistent_key_parameters(
1495 attributes->core.lifetime, attributes->core.id,
1496 p_drv, 1 );
1497 if( status != PSA_SUCCESS )
1498 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500
1501 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001502 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503 return( status );
1504
1505 /* Refuse to create overly large keys.
1506 * Note that this doesn't trigger on import if the attributes don't
1507 * explicitly specify a size (so psa_get_key_bits returns 0), so
1508 * psa_import_key() needs its own checks. */
1509 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1510 return( PSA_ERROR_NOT_SUPPORTED );
1511
Gilles Peskine91e8c332019-08-02 19:19:39 +02001512 /* Reject invalid flags. These should not be reachable through the API. */
1513 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1514 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1515 return( PSA_ERROR_INVALID_ARGUMENT );
1516
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001517 return( PSA_SUCCESS );
1518}
1519
Gilles Peskine4747d192019-04-17 15:05:45 +02001520/** Prepare a key slot to receive key material.
1521 *
1522 * This function allocates a key slot and sets its metadata.
1523 *
1524 * If this function fails, call psa_fail_key_creation().
1525 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001526 * This function is intended to be used as follows:
1527 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1528 * it with the specified attributes, and assign it a handle.
1529 * -# Populate the slot with the key material.
1530 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1531 * In case of failure at any step, stop the sequence and call
1532 * psa_fail_key_creation().
1533 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001534 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001535 * \param[in] attributes Key attributes for the new key.
1536 * \param[out] handle On success, a handle for the allocated slot.
1537 * \param[out] p_slot On success, a pointer to the prepared slot.
1538 * \param[out] p_drv On any return, the driver for the key, if any.
1539 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * The key slot is ready to receive key material.
1543 * \return If this function fails, the key slot is an invalid state.
1544 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 */
1546static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001547 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001548 const psa_key_attributes_t *attributes,
1549 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001550 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001551 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001552{
1553 psa_status_t status;
1554 psa_key_slot_t *slot;
1555
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001557 *p_drv = NULL;
1558
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559 status = psa_validate_key_attributes( attributes, p_drv );
1560 if( status != PSA_SUCCESS )
1561 return( status );
1562
Gilles Peskineedbed562019-08-07 18:19:59 +02001563 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001564 if( status != PSA_SUCCESS )
1565 return( status );
1566 slot = *p_slot;
1567
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001568 /* We're storing the declared bit-size of the key. It's up to each
1569 * creation mechanism to verify that this information is correct.
1570 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001571 * an input (generate, device) but not for those where the bit-size
1572 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001573
1574 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001575
Gilles Peskine91e8c332019-08-02 19:19:39 +02001576 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * external-only flags, query `attributes`. Thanks to the check
1578 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001579 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001580 * may have set. */
1581 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001582
Gilles Peskinecbaff462019-07-12 23:46:04 +02001583#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001584 /* For a key in a secure element, we need to do three things
1585 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001586 * create the key file in internal storage, create the
1587 * key inside the secure element, and update the driver's
1588 * persistent data. Start a transaction that will encompass these
1589 * three actions. */
1590 /* The first thing to do is to find a slot number for the new key.
1591 * We save the slot number in persistent storage as part of the
1592 * transaction data. It will be needed to recover if the power
1593 * fails during the key creation process, to clean up on the secure
1594 * element side after restarting. Obtaining a slot number from the
1595 * secure element driver updates its persistent state, but we do not yet
1596 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001597 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001598 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Darryl Green0c6575a2018-11-07 16:05:30 +00001599 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001600 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001601 &slot->data.se.slot_number );
1602 if( status != PSA_SUCCESS )
1603 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001604 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001605 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001606 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001607 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001608 status = psa_crypto_save_transaction( );
1609 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 {
1611 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001612 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001613 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001615#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1616
Darryl Green0c6575a2018-11-07 16:05:30 +00001617 return( status );
1618}
Gilles Peskine4747d192019-04-17 15:05:45 +02001619
1620/** Finalize the creation of a key once its key material has been set.
1621 *
1622 * This entails writing the key to persistent storage.
1623 *
1624 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001625 * See the documentation of psa_start_key_creation() for the intended use
1626 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001627 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001628 * \param[in,out] slot Pointer to the slot with key material.
1629 * \param[in] driver The secure element driver for the key,
1630 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 *
1632 * \retval #PSA_SUCCESS
1633 * The key was successfully created. The handle is now valid.
1634 * \return If this function fails, the key slot is an invalid state.
1635 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001637static psa_status_t psa_finish_key_creation(
1638 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001639 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001640{
1641 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001642 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001644
1645#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001646 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001647 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001648#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1649 if( driver != NULL )
1650 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001651 psa_se_key_data_storage_t data;
1652#if defined(static_assert)
1653 static_assert( sizeof( slot->data.se.slot_number ) ==
1654 sizeof( data.slot_number ),
1655 "Slot number size does not match psa_se_key_data_storage_t" );
1656 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1657 "Bit-size size does not match psa_se_key_data_storage_t" );
1658#endif
1659 memcpy( &data.slot_number, &slot->data.se.slot_number,
1660 sizeof( slot->data.se.slot_number ) );
1661 memcpy( &data.bits, &slot->attr.bits,
1662 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001663 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001664 (uint8_t*) &data,
1665 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001666 }
1667 else
1668#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1669 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001670 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001671 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001672 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001673 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1674 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001675 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001676 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1677 status = psa_internal_export_key( slot,
1678 buffer, buffer_size, &length,
1679 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001680 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001681 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001682 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001683
Gilles Peskinef9168942019-09-12 19:20:29 +02001684 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001685 mbedtls_free( buffer );
1686 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001688#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1689
Gilles Peskinecbaff462019-07-12 23:46:04 +02001690#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001691 /* Finish the transaction for a key creation. This does not
1692 * happen when registering an existing key. Detect this case
1693 * by checking whether a transaction is in progress (actual
1694 * creation of a key in a secure element requires a transaction,
1695 * but registration doesn't use one). */
1696 if( driver != NULL &&
1697 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001698 {
1699 status = psa_save_se_persistent_data( driver );
1700 if( status != PSA_SUCCESS )
1701 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001702 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703 return( status );
1704 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001705 status = psa_crypto_stop_transaction( );
1706 if( status != PSA_SUCCESS )
1707 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001708 }
1709#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1710
Gilles Peskine4747d192019-04-17 15:05:45 +02001711 return( status );
1712}
1713
1714/** Abort the creation of a key.
1715 *
1716 * You may call this function after calling psa_start_key_creation(),
1717 * or after psa_finish_key_creation() fails. In other circumstances, this
1718 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001719 * See the documentation of psa_start_key_creation() for the intended use
1720 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001722 * \param[in,out] slot Pointer to the slot with key material.
1723 * \param[in] driver The secure element driver for the key,
1724 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001726static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001727 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001728{
Gilles Peskine011e4282019-06-26 18:34:38 +02001729 (void) driver;
1730
Gilles Peskine4747d192019-04-17 15:05:45 +02001731 if( slot == NULL )
1732 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001733
1734#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001735 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001736 * element, and the failure happened later (when saving metadata
1737 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001738 * element.
1739 * https://github.com/ARMmbed/mbed-crypto/issues/217
1740 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001741
Gilles Peskined7729582019-08-05 15:55:54 +02001742 /* Abort the ongoing transaction if any (there may not be one if
1743 * the creation process failed before starting one, or if the
1744 * key creation is a registration of a key in a secure element).
1745 * Earlier functions must already have done what it takes to undo any
1746 * partial creation. All that's left is to update the transaction data
1747 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001748 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001749#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1750
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 psa_wipe_key_slot( slot );
1752}
1753
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001754/** Validate optional attributes during key creation.
1755 *
1756 * Some key attributes are optional during key creation. If they are
1757 * specified in the attributes structure, check that they are consistent
1758 * with the data in the slot.
1759 *
1760 * This function should be called near the end of key creation, after
1761 * the slot in memory is fully populated but before saving persistent data.
1762 */
1763static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001764 const psa_key_slot_t *slot,
1765 const psa_key_attributes_t *attributes )
1766{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001767 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001768 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001769 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001770 return( PSA_ERROR_INVALID_ARGUMENT );
1771 }
1772
1773 if( attributes->domain_parameters_size != 0 )
1774 {
1775#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001776 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001777 {
1778 mbedtls_mpi actual, required;
1779 int ret;
1780 mbedtls_mpi_init( &actual );
1781 mbedtls_mpi_init( &required );
1782 ret = mbedtls_rsa_export( slot->data.rsa,
1783 NULL, NULL, NULL, NULL, &actual );
1784 if( ret != 0 )
1785 goto rsa_exit;
1786 ret = mbedtls_mpi_read_binary( &required,
1787 attributes->domain_parameters,
1788 attributes->domain_parameters_size );
1789 if( ret != 0 )
1790 goto rsa_exit;
1791 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1792 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1793 rsa_exit:
1794 mbedtls_mpi_free( &actual );
1795 mbedtls_mpi_free( &required );
1796 if( ret != 0)
1797 return( mbedtls_to_psa_error( ret ) );
1798 }
1799 else
1800#endif
1801 {
1802 return( PSA_ERROR_INVALID_ARGUMENT );
1803 }
1804 }
1805
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001806 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001808 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809 return( PSA_ERROR_INVALID_ARGUMENT );
1810 }
1811
1812 return( PSA_SUCCESS );
1813}
1814
Gilles Peskine4747d192019-04-17 15:05:45 +02001815psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001817 size_t data_length,
1818 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001819{
1820 psa_status_t status;
1821 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001822 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001823
Gilles Peskine0f84d622019-09-12 19:03:13 +02001824 /* Reject zero-length symmetric keys (including raw data key objects).
1825 * This also rejects any key which might be encoded as an empty string,
1826 * which is never valid. */
1827 if( data_length == 0 )
1828 return( PSA_ERROR_INVALID_ARGUMENT );
1829
Gilles Peskinedf179142019-07-15 22:02:14 +02001830 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1831 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001832 if( status != PSA_SUCCESS )
1833 goto exit;
1834
Gilles Peskine5d309672019-07-12 23:47:28 +02001835#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1836 if( driver != NULL )
1837 {
1838 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001839 /* The driver should set the number of key bits, however in
1840 * case it doesn't, we initialize bits to an invalid value. */
1841 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001842 if( drv->key_management == NULL ||
1843 drv->key_management->p_import == NULL )
1844 {
1845 status = PSA_ERROR_NOT_SUPPORTED;
1846 goto exit;
1847 }
1848 status = drv->key_management->p_import(
1849 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001850 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001851 &bits );
1852 if( status != PSA_SUCCESS )
1853 goto exit;
1854 if( bits > PSA_MAX_KEY_BITS )
1855 {
1856 status = PSA_ERROR_NOT_SUPPORTED;
1857 goto exit;
1858 }
1859 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001860 }
1861 else
1862#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1863 {
1864 status = psa_import_key_into_slot( slot, data, data_length );
1865 if( status != PSA_SUCCESS )
1866 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001867 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001868 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001869 if( status != PSA_SUCCESS )
1870 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001871
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001873exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 if( status != PSA_SUCCESS )
1875 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001876 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 *handle = 0;
1878 }
1879 return( status );
1880}
1881
Gilles Peskined7729582019-08-05 15:55:54 +02001882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1883psa_status_t mbedtls_psa_register_se_key(
1884 const psa_key_attributes_t *attributes )
1885{
1886 psa_status_t status;
1887 psa_key_slot_t *slot = NULL;
1888 psa_se_drv_table_entry_t *driver = NULL;
1889 const psa_drv_se_t *drv;
1890 psa_key_handle_t handle = 0;
1891
1892 /* Leaving attributes unspecified is not currently supported.
1893 * It could make sense to query the key type and size from the
1894 * secure element, but not all secure elements support this
1895 * and the driver HAL doesn't currently support it. */
1896 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1897 return( PSA_ERROR_NOT_SUPPORTED );
1898 if( psa_get_key_bits( attributes ) == 0 )
1899 return( PSA_ERROR_NOT_SUPPORTED );
1900
1901 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1902 &handle, &slot, &driver );
1903 if( status != PSA_SUCCESS )
1904 goto exit;
1905
1906 if( driver == NULL )
1907 {
1908 status = PSA_ERROR_INVALID_ARGUMENT;
1909 goto exit;
1910 }
1911 drv = psa_get_se_driver_methods( driver );
1912
1913 if ( psa_get_key_slot_number( attributes,
1914 &slot->data.se.slot_number ) != PSA_SUCCESS )
1915 {
1916 /* The application didn't specify a slot number. This doesn't
1917 * make sense when registering a slot. */
1918 status = PSA_ERROR_INVALID_ARGUMENT;
1919 goto exit;
1920 }
1921
Gilles Peskinea5f87492019-08-05 16:46:18 +02001922 /* If the driver has a slot number validation method, call it.
1923 * If it doesn't, it means the secure element is unable to validate
1924 * anything and so we have to trust the application. */
1925 if( drv->key_management != NULL &&
1926 drv->key_management->p_validate_slot_number != NULL )
1927 {
1928 status = drv->key_management->p_validate_slot_number(
1929 psa_get_se_driver_context( driver ),
1930 attributes,
1931 PSA_KEY_CREATION_REGISTER,
1932 slot->data.se.slot_number );
1933 if( status != PSA_SUCCESS )
1934 goto exit;
1935 }
1936
Gilles Peskined7729582019-08-05 15:55:54 +02001937 status = psa_finish_key_creation( slot, driver );
1938
1939exit:
1940 if( status != PSA_SUCCESS )
1941 {
1942 psa_fail_key_creation( slot, driver );
1943 }
1944 /* Registration doesn't keep the key in RAM. */
1945 psa_close_key( handle );
1946 return( status );
1947}
1948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1949
Gilles Peskinef603c712019-01-19 13:40:11 +01001950static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001951 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001952{
1953 psa_status_t status;
1954 uint8_t *buffer = NULL;
1955 size_t buffer_size = 0;
1956 size_t length;
1957
Gilles Peskine8e338702019-07-30 20:06:31 +02001958 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001959 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001960 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001961 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001962 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001963 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1964 if( status != PSA_SUCCESS )
1965 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001966 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001967 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001968
1969exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001970 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001971 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001972 return( status );
1973}
1974
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001975psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1976 const psa_key_attributes_t *specified_attributes,
1977 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001978{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001980 psa_key_slot_t *source_slot = NULL;
1981 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001983 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001984
Gilles Peskine28f8f302019-07-24 13:30:31 +02001985 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001986 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001987 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001988 goto exit;
1989
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001990 status = psa_validate_optional_attributes( source_slot,
1991 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992 if( status != PSA_SUCCESS )
1993 goto exit;
1994
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001995 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001996 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 if( status != PSA_SUCCESS )
1998 goto exit;
1999
Gilles Peskinedf179142019-07-15 22:02:14 +02002000 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2001 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002002 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 if( status != PSA_SUCCESS )
2004 goto exit;
2005
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002006#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2007 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002008 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002009 /* Copying to a secure element is not implemented yet. */
2010 status = PSA_ERROR_NOT_SUPPORTED;
2011 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002012 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002013#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002014
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002015 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002016 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002018
Gilles Peskine011e4282019-06-26 18:34:38 +02002019 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002020exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 if( status != PSA_SUCCESS )
2022 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002023 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002024 *target_handle = 0;
2025 }
2026 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002027}
2028
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002029
2030
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002031/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002032/* Message digests */
2033/****************************************************************/
2034
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002035static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002036{
2037 switch( alg )
2038 {
2039#if defined(MBEDTLS_MD2_C)
2040 case PSA_ALG_MD2:
2041 return( &mbedtls_md2_info );
2042#endif
2043#if defined(MBEDTLS_MD4_C)
2044 case PSA_ALG_MD4:
2045 return( &mbedtls_md4_info );
2046#endif
2047#if defined(MBEDTLS_MD5_C)
2048 case PSA_ALG_MD5:
2049 return( &mbedtls_md5_info );
2050#endif
2051#if defined(MBEDTLS_RIPEMD160_C)
2052 case PSA_ALG_RIPEMD160:
2053 return( &mbedtls_ripemd160_info );
2054#endif
2055#if defined(MBEDTLS_SHA1_C)
2056 case PSA_ALG_SHA_1:
2057 return( &mbedtls_sha1_info );
2058#endif
2059#if defined(MBEDTLS_SHA256_C)
2060 case PSA_ALG_SHA_224:
2061 return( &mbedtls_sha224_info );
2062 case PSA_ALG_SHA_256:
2063 return( &mbedtls_sha256_info );
2064#endif
2065#if defined(MBEDTLS_SHA512_C)
2066 case PSA_ALG_SHA_384:
2067 return( &mbedtls_sha384_info );
2068 case PSA_ALG_SHA_512:
2069 return( &mbedtls_sha512_info );
2070#endif
2071 default:
2072 return( NULL );
2073 }
2074}
2075
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002076psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2077{
2078 switch( operation->alg )
2079 {
Gilles Peskine81736312018-06-26 15:04:31 +02002080 case 0:
2081 /* The object has (apparently) been initialized but it is not
2082 * in use. It's ok to call abort on such an object, and there's
2083 * nothing to do. */
2084 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002085#if defined(MBEDTLS_MD2_C)
2086 case PSA_ALG_MD2:
2087 mbedtls_md2_free( &operation->ctx.md2 );
2088 break;
2089#endif
2090#if defined(MBEDTLS_MD4_C)
2091 case PSA_ALG_MD4:
2092 mbedtls_md4_free( &operation->ctx.md4 );
2093 break;
2094#endif
2095#if defined(MBEDTLS_MD5_C)
2096 case PSA_ALG_MD5:
2097 mbedtls_md5_free( &operation->ctx.md5 );
2098 break;
2099#endif
2100#if defined(MBEDTLS_RIPEMD160_C)
2101 case PSA_ALG_RIPEMD160:
2102 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2103 break;
2104#endif
2105#if defined(MBEDTLS_SHA1_C)
2106 case PSA_ALG_SHA_1:
2107 mbedtls_sha1_free( &operation->ctx.sha1 );
2108 break;
2109#endif
2110#if defined(MBEDTLS_SHA256_C)
2111 case PSA_ALG_SHA_224:
2112 case PSA_ALG_SHA_256:
2113 mbedtls_sha256_free( &operation->ctx.sha256 );
2114 break;
2115#endif
2116#if defined(MBEDTLS_SHA512_C)
2117 case PSA_ALG_SHA_384:
2118 case PSA_ALG_SHA_512:
2119 mbedtls_sha512_free( &operation->ctx.sha512 );
2120 break;
2121#endif
2122 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002123 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124 }
2125 operation->alg = 0;
2126 return( PSA_SUCCESS );
2127}
2128
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002129psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002130 psa_algorithm_t alg )
2131{
2132 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002133
2134 /* A context must be freshly initialized before it can be set up. */
2135 if( operation->alg != 0 )
2136 {
2137 return( PSA_ERROR_BAD_STATE );
2138 }
2139
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002140 switch( alg )
2141 {
2142#if defined(MBEDTLS_MD2_C)
2143 case PSA_ALG_MD2:
2144 mbedtls_md2_init( &operation->ctx.md2 );
2145 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_MD4_C)
2149 case PSA_ALG_MD4:
2150 mbedtls_md4_init( &operation->ctx.md4 );
2151 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_MD5_C)
2155 case PSA_ALG_MD5:
2156 mbedtls_md5_init( &operation->ctx.md5 );
2157 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_RIPEMD160_C)
2161 case PSA_ALG_RIPEMD160:
2162 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2163 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_SHA1_C)
2167 case PSA_ALG_SHA_1:
2168 mbedtls_sha1_init( &operation->ctx.sha1 );
2169 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2170 break;
2171#endif
2172#if defined(MBEDTLS_SHA256_C)
2173 case PSA_ALG_SHA_224:
2174 mbedtls_sha256_init( &operation->ctx.sha256 );
2175 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2176 break;
2177 case PSA_ALG_SHA_256:
2178 mbedtls_sha256_init( &operation->ctx.sha256 );
2179 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2180 break;
2181#endif
2182#if defined(MBEDTLS_SHA512_C)
2183 case PSA_ALG_SHA_384:
2184 mbedtls_sha512_init( &operation->ctx.sha512 );
2185 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2186 break;
2187 case PSA_ALG_SHA_512:
2188 mbedtls_sha512_init( &operation->ctx.sha512 );
2189 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2190 break;
2191#endif
2192 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002193 return( PSA_ALG_IS_HASH( alg ) ?
2194 PSA_ERROR_NOT_SUPPORTED :
2195 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 }
2197 if( ret == 0 )
2198 operation->alg = alg;
2199 else
2200 psa_hash_abort( operation );
2201 return( mbedtls_to_psa_error( ret ) );
2202}
2203
2204psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2205 const uint8_t *input,
2206 size_t input_length )
2207{
2208 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002209
2210 /* Don't require hash implementations to behave correctly on a
2211 * zero-length input, which may have an invalid pointer. */
2212 if( input_length == 0 )
2213 return( PSA_SUCCESS );
2214
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215 switch( operation->alg )
2216 {
2217#if defined(MBEDTLS_MD2_C)
2218 case PSA_ALG_MD2:
2219 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2220 input, input_length );
2221 break;
2222#endif
2223#if defined(MBEDTLS_MD4_C)
2224 case PSA_ALG_MD4:
2225 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2226 input, input_length );
2227 break;
2228#endif
2229#if defined(MBEDTLS_MD5_C)
2230 case PSA_ALG_MD5:
2231 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_RIPEMD160_C)
2236 case PSA_ALG_RIPEMD160:
2237 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2238 input, input_length );
2239 break;
2240#endif
2241#if defined(MBEDTLS_SHA1_C)
2242 case PSA_ALG_SHA_1:
2243 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2244 input, input_length );
2245 break;
2246#endif
2247#if defined(MBEDTLS_SHA256_C)
2248 case PSA_ALG_SHA_224:
2249 case PSA_ALG_SHA_256:
2250 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2251 input, input_length );
2252 break;
2253#endif
2254#if defined(MBEDTLS_SHA512_C)
2255 case PSA_ALG_SHA_384:
2256 case PSA_ALG_SHA_512:
2257 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2258 input, input_length );
2259 break;
2260#endif
2261 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002262 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002264
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265 if( ret != 0 )
2266 psa_hash_abort( operation );
2267 return( mbedtls_to_psa_error( ret ) );
2268}
2269
2270psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2271 uint8_t *hash,
2272 size_t hash_size,
2273 size_t *hash_length )
2274{
itayzafrir40835d42018-08-02 13:14:17 +03002275 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002277 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278
2279 /* Fill the output buffer with something that isn't a valid hash
2280 * (barring an attack on the hash and deliberately-crafted input),
2281 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002282 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002283 /* If hash_size is 0 then hash may be NULL and then the
2284 * call to memset would have undefined behavior. */
2285 if( hash_size != 0 )
2286 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287
2288 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002289 {
2290 status = PSA_ERROR_BUFFER_TOO_SMALL;
2291 goto exit;
2292 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293
2294 switch( operation->alg )
2295 {
2296#if defined(MBEDTLS_MD2_C)
2297 case PSA_ALG_MD2:
2298 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2299 break;
2300#endif
2301#if defined(MBEDTLS_MD4_C)
2302 case PSA_ALG_MD4:
2303 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2304 break;
2305#endif
2306#if defined(MBEDTLS_MD5_C)
2307 case PSA_ALG_MD5:
2308 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2309 break;
2310#endif
2311#if defined(MBEDTLS_RIPEMD160_C)
2312 case PSA_ALG_RIPEMD160:
2313 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2314 break;
2315#endif
2316#if defined(MBEDTLS_SHA1_C)
2317 case PSA_ALG_SHA_1:
2318 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2319 break;
2320#endif
2321#if defined(MBEDTLS_SHA256_C)
2322 case PSA_ALG_SHA_224:
2323 case PSA_ALG_SHA_256:
2324 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2325 break;
2326#endif
2327#if defined(MBEDTLS_SHA512_C)
2328 case PSA_ALG_SHA_384:
2329 case PSA_ALG_SHA_512:
2330 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2331 break;
2332#endif
2333 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002334 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335 }
itayzafrir40835d42018-08-02 13:14:17 +03002336 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337
itayzafrir40835d42018-08-02 13:14:17 +03002338exit:
2339 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002341 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 return( psa_hash_abort( operation ) );
2343 }
2344 else
2345 {
2346 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002347 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 }
2349}
2350
Gilles Peskine2d277862018-06-18 15:41:12 +02002351psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2352 const uint8_t *hash,
2353 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354{
2355 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2356 size_t actual_hash_length;
2357 psa_status_t status = psa_hash_finish( operation,
2358 actual_hash, sizeof( actual_hash ),
2359 &actual_hash_length );
2360 if( status != PSA_SUCCESS )
2361 return( status );
2362 if( actual_hash_length != hash_length )
2363 return( PSA_ERROR_INVALID_SIGNATURE );
2364 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2365 return( PSA_ERROR_INVALID_SIGNATURE );
2366 return( PSA_SUCCESS );
2367}
2368
Gilles Peskineeb35d782019-01-22 17:56:16 +01002369psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2370 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002371{
2372 if( target_operation->alg != 0 )
2373 return( PSA_ERROR_BAD_STATE );
2374
2375 switch( source_operation->alg )
2376 {
2377 case 0:
2378 return( PSA_ERROR_BAD_STATE );
2379#if defined(MBEDTLS_MD2_C)
2380 case PSA_ALG_MD2:
2381 mbedtls_md2_clone( &target_operation->ctx.md2,
2382 &source_operation->ctx.md2 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_MD4_C)
2386 case PSA_ALG_MD4:
2387 mbedtls_md4_clone( &target_operation->ctx.md4,
2388 &source_operation->ctx.md4 );
2389 break;
2390#endif
2391#if defined(MBEDTLS_MD5_C)
2392 case PSA_ALG_MD5:
2393 mbedtls_md5_clone( &target_operation->ctx.md5,
2394 &source_operation->ctx.md5 );
2395 break;
2396#endif
2397#if defined(MBEDTLS_RIPEMD160_C)
2398 case PSA_ALG_RIPEMD160:
2399 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2400 &source_operation->ctx.ripemd160 );
2401 break;
2402#endif
2403#if defined(MBEDTLS_SHA1_C)
2404 case PSA_ALG_SHA_1:
2405 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2406 &source_operation->ctx.sha1 );
2407 break;
2408#endif
2409#if defined(MBEDTLS_SHA256_C)
2410 case PSA_ALG_SHA_224:
2411 case PSA_ALG_SHA_256:
2412 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2413 &source_operation->ctx.sha256 );
2414 break;
2415#endif
2416#if defined(MBEDTLS_SHA512_C)
2417 case PSA_ALG_SHA_384:
2418 case PSA_ALG_SHA_512:
2419 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2420 &source_operation->ctx.sha512 );
2421 break;
2422#endif
2423 default:
2424 return( PSA_ERROR_NOT_SUPPORTED );
2425 }
2426
2427 target_operation->alg = source_operation->alg;
2428 return( PSA_SUCCESS );
2429}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002430
2431
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433/* MAC */
2434/****************************************************************/
2435
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002436static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437 psa_algorithm_t alg,
2438 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002439 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002440 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002442 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002443 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002444
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002445 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002446 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002447
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2449 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002450 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002452 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002453 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 mode = MBEDTLS_MODE_STREAM;
2455 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 case PSA_ALG_CTR:
2457 mode = MBEDTLS_MODE_CTR;
2458 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002459 case PSA_ALG_CFB:
2460 mode = MBEDTLS_MODE_CFB;
2461 break;
2462 case PSA_ALG_OFB:
2463 mode = MBEDTLS_MODE_OFB;
2464 break;
2465 case PSA_ALG_CBC_NO_PADDING:
2466 mode = MBEDTLS_MODE_CBC;
2467 break;
2468 case PSA_ALG_CBC_PKCS7:
2469 mode = MBEDTLS_MODE_CBC;
2470 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002471 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 mode = MBEDTLS_MODE_CCM;
2473 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002474 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475 mode = MBEDTLS_MODE_GCM;
2476 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002477 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2478 mode = MBEDTLS_MODE_CHACHAPOLY;
2479 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 default:
2481 return( NULL );
2482 }
2483 }
2484 else if( alg == PSA_ALG_CMAC )
2485 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 else
2487 return( NULL );
2488
2489 switch( key_type )
2490 {
2491 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 break;
2494 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002495 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2496 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002498 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002500 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002501 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2502 * but two-key Triple-DES is functionally three-key Triple-DES
2503 * with K1=K3, so that's how we present it to mbedtls. */
2504 if( key_bits == 128 )
2505 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 break;
2507 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002508 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 break;
2510 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002511 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002513 case PSA_KEY_TYPE_CHACHA20:
2514 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2515 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516 default:
2517 return( NULL );
2518 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002519 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002520 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521
Jaeden Amero23bbb752018-06-26 14:16:54 +01002522 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2523 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524}
2525
Gilles Peskinea05219c2018-11-16 16:02:56 +01002526#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002527static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002528{
Gilles Peskine2d277862018-06-18 15:41:12 +02002529 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002530 {
2531 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002532 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002533 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002534 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002535 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002536 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002537 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002538 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002539 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002540 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002541 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002542 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002543 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002544 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002545 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002546 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002547 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002548 return( 128 );
2549 default:
2550 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002551 }
2552}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002553#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002554
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002555/* Initialize the MAC operation structure. Once this function has been
2556 * called, psa_mac_abort can run and will do the right thing. */
2557static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2558 psa_algorithm_t alg )
2559{
2560 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2561
2562 operation->alg = alg;
2563 operation->key_set = 0;
2564 operation->iv_set = 0;
2565 operation->iv_required = 0;
2566 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002567 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568
2569#if defined(MBEDTLS_CMAC_C)
2570 if( alg == PSA_ALG_CMAC )
2571 {
2572 operation->iv_required = 0;
2573 mbedtls_cipher_init( &operation->ctx.cmac );
2574 status = PSA_SUCCESS;
2575 }
2576 else
2577#endif /* MBEDTLS_CMAC_C */
2578#if defined(MBEDTLS_MD_C)
2579 if( PSA_ALG_IS_HMAC( operation->alg ) )
2580 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002581 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2582 operation->ctx.hmac.hash_ctx.alg = 0;
2583 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584 }
2585 else
2586#endif /* MBEDTLS_MD_C */
2587 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002588 if( ! PSA_ALG_IS_MAC( alg ) )
2589 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590 }
2591
2592 if( status != PSA_SUCCESS )
2593 memset( operation, 0, sizeof( *operation ) );
2594 return( status );
2595}
2596
Gilles Peskine01126fa2018-07-12 17:04:55 +02002597#if defined(MBEDTLS_MD_C)
2598static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2599{
Gilles Peskine3f108122018-12-07 18:14:53 +01002600 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002601 return( psa_hash_abort( &hmac->hash_ctx ) );
2602}
2603#endif /* MBEDTLS_MD_C */
2604
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2606{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002607 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002609 /* The object has (apparently) been initialized but it is not
2610 * in use. It's ok to call abort on such an object, and there's
2611 * nothing to do. */
2612 return( PSA_SUCCESS );
2613 }
2614 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002616 if( operation->alg == PSA_ALG_CMAC )
2617 {
2618 mbedtls_cipher_free( &operation->ctx.cmac );
2619 }
2620 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 if( PSA_ALG_IS_HMAC( operation->alg ) )
2624 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002625 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002626 }
2627 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002628#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002629 {
2630 /* Sanity check (shouldn't happen: operation->alg should
2631 * always have been initialized to a valid value). */
2632 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 }
Moran Peker41deec42018-04-04 15:43:05 +03002634
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635 operation->alg = 0;
2636 operation->key_set = 0;
2637 operation->iv_set = 0;
2638 operation->iv_required = 0;
2639 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002640 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002641
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002643
2644bad_state:
2645 /* If abort is called on an uninitialized object, we can't trust
2646 * anything. Wipe the object in case it contains confidential data.
2647 * This may result in a memory leak if a pointer gets overwritten,
2648 * but it's too late to do anything about this. */
2649 memset( operation, 0, sizeof( *operation ) );
2650 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651}
2652
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002653#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002654static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002655 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002656 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002657 const mbedtls_cipher_info_t *cipher_info )
2658{
2659 int ret;
2660
2661 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002662
2663 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2664 if( ret != 0 )
2665 return( ret );
2666
2667 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2668 slot->data.raw.data,
2669 key_bits );
2670 return( ret );
2671}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002672#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002673
Gilles Peskine248051a2018-06-20 16:09:38 +02002674#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002675static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2676 const uint8_t *key,
2677 size_t key_length,
2678 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002679{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002680 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002682 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002684 psa_status_t status;
2685
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002686 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2687 * overflow below. This should never trigger if the hash algorithm
2688 * is implemented correctly. */
2689 /* The size checks against the ipad and opad buffers cannot be written
2690 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2691 * because that triggers -Wlogical-op on GCC 7.3. */
2692 if( block_size > sizeof( ipad ) )
2693 return( PSA_ERROR_NOT_SUPPORTED );
2694 if( block_size > sizeof( hmac->opad ) )
2695 return( PSA_ERROR_NOT_SUPPORTED );
2696 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697 return( PSA_ERROR_NOT_SUPPORTED );
2698
Gilles Peskined223b522018-06-11 18:12:58 +02002699 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002700 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002701 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002702 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002703 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002705 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002706 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002707 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002708 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002709 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002710 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 }
Gilles Peskine96889972018-07-12 17:07:03 +02002712 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2713 * but it is permitted. It is common when HMAC is used in HKDF, for
2714 * example. Don't call `memcpy` in the 0-length because `key` could be
2715 * an invalid pointer which would make the behavior undefined. */
2716 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002717 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718
Gilles Peskined223b522018-06-11 18:12:58 +02002719 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2720 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002722 ipad[i] ^= 0x36;
2723 memset( ipad + key_length, 0x36, block_size - key_length );
2724
2725 /* Copy the key material from ipad to opad, flipping the requisite bits,
2726 * and filling the rest of opad with the requisite constant. */
2727 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002728 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2729 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730
Gilles Peskine01126fa2018-07-12 17:04:55 +02002731 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002733 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734
Gilles Peskine01126fa2018-07-12 17:04:55 +02002735 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002736
2737cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002738 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002739
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740 return( status );
2741}
Gilles Peskine248051a2018-06-20 16:09:38 +02002742#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002743
Gilles Peskine89167cb2018-07-08 20:12:23 +02002744static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002745 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002746 psa_algorithm_t alg,
2747 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002750 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002752 psa_key_usage_t usage =
2753 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002754 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002755 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002757 /* A context must be freshly initialized before it can be set up. */
2758 if( operation->alg != 0 )
2759 {
2760 return( PSA_ERROR_BAD_STATE );
2761 }
2762
Gilles Peskined911eb72018-08-14 15:18:45 +02002763 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002764 if( status != PSA_SUCCESS )
2765 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002766 if( is_sign )
2767 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768
Gilles Peskine28f8f302019-07-24 13:30:31 +02002769 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002770 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002771 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002772 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002773
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002775 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002776 {
2777 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002778 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002779 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002780 int ret;
2781 if( cipher_info == NULL )
2782 {
2783 status = PSA_ERROR_NOT_SUPPORTED;
2784 goto exit;
2785 }
2786 operation->mac_size = cipher_info->block_size;
2787 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2788 status = mbedtls_to_psa_error( ret );
2789 }
2790 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002793 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002794 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002795 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002796 if( hash_alg == 0 )
2797 {
2798 status = PSA_ERROR_NOT_SUPPORTED;
2799 goto exit;
2800 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002801
2802 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2803 /* Sanity check. This shouldn't fail on a valid configuration. */
2804 if( operation->mac_size == 0 ||
2805 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2806 {
2807 status = PSA_ERROR_NOT_SUPPORTED;
2808 goto exit;
2809 }
2810
Gilles Peskine8e338702019-07-30 20:06:31 +02002811 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002812 {
2813 status = PSA_ERROR_INVALID_ARGUMENT;
2814 goto exit;
2815 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002816
Gilles Peskine01126fa2018-07-12 17:04:55 +02002817 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2818 slot->data.raw.data,
2819 slot->data.raw.bytes,
2820 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002821 }
2822 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002824 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002825 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002826 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002828
Gilles Peskined911eb72018-08-14 15:18:45 +02002829 if( truncated == 0 )
2830 {
2831 /* The "normal" case: untruncated algorithm. Nothing to do. */
2832 }
2833 else if( truncated < 4 )
2834 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002835 /* A very short MAC is too short for security since it can be
2836 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2837 * so we make this our minimum, even though 32 bits is still
2838 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002839 status = PSA_ERROR_NOT_SUPPORTED;
2840 }
2841 else if( truncated > operation->mac_size )
2842 {
2843 /* It's impossible to "truncate" to a larger length. */
2844 status = PSA_ERROR_INVALID_ARGUMENT;
2845 }
2846 else
2847 operation->mac_size = truncated;
2848
Gilles Peskinefbfac682018-07-08 20:51:54 +02002849exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002850 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002852 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002854 else
2855 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002856 operation->key_set = 1;
2857 }
2858 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859}
2860
Gilles Peskine89167cb2018-07-08 20:12:23 +02002861psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002862 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002863 psa_algorithm_t alg )
2864{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002865 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002866}
2867
2868psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002869 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002870 psa_algorithm_t alg )
2871{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002872 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002873}
2874
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2876 const uint8_t *input,
2877 size_t input_length )
2878{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002881 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002882 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002883 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002884 operation->has_input = 1;
2885
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002887 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002888 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2890 input, input_length );
2891 status = mbedtls_to_psa_error( ret );
2892 }
2893 else
2894#endif /* MBEDTLS_CMAC_C */
2895#if defined(MBEDTLS_MD_C)
2896 if( PSA_ALG_IS_HMAC( operation->alg ) )
2897 {
2898 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2899 input_length );
2900 }
2901 else
2902#endif /* MBEDTLS_MD_C */
2903 {
2904 /* This shouldn't happen if `operation` was initialized by
2905 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002906 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002907 }
2908
Gilles Peskinefbfac682018-07-08 20:51:54 +02002909 if( status != PSA_SUCCESS )
2910 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002911 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912}
2913
Gilles Peskine01126fa2018-07-12 17:04:55 +02002914#if defined(MBEDTLS_MD_C)
2915static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2916 uint8_t *mac,
2917 size_t mac_size )
2918{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002919 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002920 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2921 size_t hash_size = 0;
2922 size_t block_size = psa_get_hash_block_size( hash_alg );
2923 psa_status_t status;
2924
Gilles Peskine01126fa2018-07-12 17:04:55 +02002925 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2926 if( status != PSA_SUCCESS )
2927 return( status );
2928 /* From here on, tmp needs to be wiped. */
2929
2930 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2931 if( status != PSA_SUCCESS )
2932 goto exit;
2933
2934 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2935 if( status != PSA_SUCCESS )
2936 goto exit;
2937
2938 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2939 if( status != PSA_SUCCESS )
2940 goto exit;
2941
Gilles Peskined911eb72018-08-14 15:18:45 +02002942 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2943 if( status != PSA_SUCCESS )
2944 goto exit;
2945
2946 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947
2948exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002949 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002950 return( status );
2951}
2952#endif /* MBEDTLS_MD_C */
2953
mohammad16036df908f2018-04-02 08:34:15 -07002954static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002955 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002956 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002958 if( ! operation->key_set )
2959 return( PSA_ERROR_BAD_STATE );
2960 if( operation->iv_required && ! operation->iv_set )
2961 return( PSA_ERROR_BAD_STATE );
2962
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963 if( mac_size < operation->mac_size )
2964 return( PSA_ERROR_BUFFER_TOO_SMALL );
2965
Gilles Peskine8c9def32018-02-08 10:02:12 +01002966#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002967 if( operation->alg == PSA_ALG_CMAC )
2968 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002969 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2970 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2971 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002972 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002973 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002974 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002976 else
2977#endif /* MBEDTLS_CMAC_C */
2978#if defined(MBEDTLS_MD_C)
2979 if( PSA_ALG_IS_HMAC( operation->alg ) )
2980 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002981 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002982 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002983 }
2984 else
2985#endif /* MBEDTLS_MD_C */
2986 {
2987 /* This shouldn't happen if `operation` was initialized by
2988 * a setup function. */
2989 return( PSA_ERROR_BAD_STATE );
2990 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991}
2992
Gilles Peskineacd4be32018-07-08 19:56:25 +02002993psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2994 uint8_t *mac,
2995 size_t mac_size,
2996 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002997{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002998 psa_status_t status;
2999
Jaeden Amero252ef282019-02-15 14:05:35 +00003000 if( operation->alg == 0 )
3001 {
3002 return( PSA_ERROR_BAD_STATE );
3003 }
3004
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003005 /* Fill the output buffer with something that isn't a valid mac
3006 * (barring an attack on the mac and deliberately-crafted input),
3007 * in case the caller doesn't check the return status properly. */
3008 *mac_length = mac_size;
3009 /* If mac_size is 0 then mac may be NULL and then the
3010 * call to memset would have undefined behavior. */
3011 if( mac_size != 0 )
3012 memset( mac, '!', mac_size );
3013
Gilles Peskine89167cb2018-07-08 20:12:23 +02003014 if( ! operation->is_sign )
3015 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003016 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003017 }
mohammad16036df908f2018-04-02 08:34:15 -07003018
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003019 status = psa_mac_finish_internal( operation, mac, mac_size );
3020
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003021 if( status == PSA_SUCCESS )
3022 {
3023 status = psa_mac_abort( operation );
3024 if( status == PSA_SUCCESS )
3025 *mac_length = operation->mac_size;
3026 else
3027 memset( mac, '!', mac_size );
3028 }
3029 else
3030 psa_mac_abort( operation );
3031 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003032}
3033
Gilles Peskineacd4be32018-07-08 19:56:25 +02003034psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3035 const uint8_t *mac,
3036 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037{
Gilles Peskine828ed142018-06-18 23:25:51 +02003038 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003039 psa_status_t status;
3040
Jaeden Amero252ef282019-02-15 14:05:35 +00003041 if( operation->alg == 0 )
3042 {
3043 return( PSA_ERROR_BAD_STATE );
3044 }
3045
Gilles Peskine89167cb2018-07-08 20:12:23 +02003046 if( operation->is_sign )
3047 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003048 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003049 }
3050 if( operation->mac_size != mac_length )
3051 {
3052 status = PSA_ERROR_INVALID_SIGNATURE;
3053 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003054 }
mohammad16036df908f2018-04-02 08:34:15 -07003055
3056 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003057 actual_mac, sizeof( actual_mac ) );
3058
3059 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3060 status = PSA_ERROR_INVALID_SIGNATURE;
3061
3062cleanup:
3063 if( status == PSA_SUCCESS )
3064 status = psa_mac_abort( operation );
3065 else
3066 psa_mac_abort( operation );
3067
Gilles Peskine3f108122018-12-07 18:14:53 +01003068 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003069
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003070 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003071}
3072
3073
Gilles Peskine20035e32018-02-03 22:44:14 +01003074
Gilles Peskine20035e32018-02-03 22:44:14 +01003075/****************************************************************/
3076/* Asymmetric cryptography */
3077/****************************************************************/
3078
Gilles Peskine2b450e32018-06-27 15:42:46 +02003079#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003080/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003081 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003082static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3083 size_t hash_length,
3084 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003085{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003086 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003087 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003088 *md_alg = mbedtls_md_get_type( md_info );
3089
3090 /* The Mbed TLS RSA module uses an unsigned int for hash length
3091 * parameters. Validate that it fits so that we don't risk an
3092 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003093#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003094 if( hash_length > UINT_MAX )
3095 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003096#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003097
3098#if defined(MBEDTLS_PKCS1_V15)
3099 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3100 * must be correct. */
3101 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3102 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003103 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003104 if( md_info == NULL )
3105 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003106 if( mbedtls_md_get_size( md_info ) != hash_length )
3107 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003108 }
3109#endif /* MBEDTLS_PKCS1_V15 */
3110
3111#if defined(MBEDTLS_PKCS1_V21)
3112 /* PSS requires a hash internally. */
3113 if( PSA_ALG_IS_RSA_PSS( alg ) )
3114 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003115 if( md_info == NULL )
3116 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003117 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003118#endif /* MBEDTLS_PKCS1_V21 */
3119
Gilles Peskine61b91d42018-06-08 16:09:36 +02003120 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003121}
3122
Gilles Peskine2b450e32018-06-27 15:42:46 +02003123static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3124 psa_algorithm_t alg,
3125 const uint8_t *hash,
3126 size_t hash_length,
3127 uint8_t *signature,
3128 size_t signature_size,
3129 size_t *signature_length )
3130{
3131 psa_status_t status;
3132 int ret;
3133 mbedtls_md_type_t md_alg;
3134
3135 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3136 if( status != PSA_SUCCESS )
3137 return( status );
3138
Gilles Peskine630a18a2018-06-29 17:49:35 +02003139 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003140 return( PSA_ERROR_BUFFER_TOO_SMALL );
3141
3142#if defined(MBEDTLS_PKCS1_V15)
3143 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3144 {
3145 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3146 MBEDTLS_MD_NONE );
3147 ret = mbedtls_rsa_pkcs1_sign( rsa,
3148 mbedtls_ctr_drbg_random,
3149 &global_data.ctr_drbg,
3150 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003151 md_alg,
3152 (unsigned int) hash_length,
3153 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003154 signature );
3155 }
3156 else
3157#endif /* MBEDTLS_PKCS1_V15 */
3158#if defined(MBEDTLS_PKCS1_V21)
3159 if( PSA_ALG_IS_RSA_PSS( alg ) )
3160 {
3161 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3162 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3163 mbedtls_ctr_drbg_random,
3164 &global_data.ctr_drbg,
3165 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003166 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003167 (unsigned int) hash_length,
3168 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003169 signature );
3170 }
3171 else
3172#endif /* MBEDTLS_PKCS1_V21 */
3173 {
3174 return( PSA_ERROR_INVALID_ARGUMENT );
3175 }
3176
3177 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003178 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003179 return( mbedtls_to_psa_error( ret ) );
3180}
3181
3182static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3183 psa_algorithm_t alg,
3184 const uint8_t *hash,
3185 size_t hash_length,
3186 const uint8_t *signature,
3187 size_t signature_length )
3188{
3189 psa_status_t status;
3190 int ret;
3191 mbedtls_md_type_t md_alg;
3192
3193 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3194 if( status != PSA_SUCCESS )
3195 return( status );
3196
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003197 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3198 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003199
3200#if defined(MBEDTLS_PKCS1_V15)
3201 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3202 {
3203 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3204 MBEDTLS_MD_NONE );
3205 ret = mbedtls_rsa_pkcs1_verify( rsa,
3206 mbedtls_ctr_drbg_random,
3207 &global_data.ctr_drbg,
3208 MBEDTLS_RSA_PUBLIC,
3209 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003210 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003211 hash,
3212 signature );
3213 }
3214 else
3215#endif /* MBEDTLS_PKCS1_V15 */
3216#if defined(MBEDTLS_PKCS1_V21)
3217 if( PSA_ALG_IS_RSA_PSS( alg ) )
3218 {
3219 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3220 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3221 mbedtls_ctr_drbg_random,
3222 &global_data.ctr_drbg,
3223 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003224 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003225 (unsigned int) hash_length,
3226 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003227 signature );
3228 }
3229 else
3230#endif /* MBEDTLS_PKCS1_V21 */
3231 {
3232 return( PSA_ERROR_INVALID_ARGUMENT );
3233 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003234
3235 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3236 * the rest of the signature is invalid". This has little use in
3237 * practice and PSA doesn't report this distinction. */
3238 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3239 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003240 return( mbedtls_to_psa_error( ret ) );
3241}
3242#endif /* MBEDTLS_RSA_C */
3243
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003244#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003245/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3246 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3247 * (even though these functions don't modify it). */
3248static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3249 psa_algorithm_t alg,
3250 const uint8_t *hash,
3251 size_t hash_length,
3252 uint8_t *signature,
3253 size_t signature_size,
3254 size_t *signature_length )
3255{
3256 int ret;
3257 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003258 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003259 mbedtls_mpi_init( &r );
3260 mbedtls_mpi_init( &s );
3261
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003262 if( signature_size < 2 * curve_bytes )
3263 {
3264 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3265 goto cleanup;
3266 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003267
Gilles Peskinea05219c2018-11-16 16:02:56 +01003268#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003269 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3270 {
3271 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3272 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3273 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003274 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3275 &ecp->d, hash,
3276 hash_length, md_alg,
3277 mbedtls_ctr_drbg_random,
3278 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003279 }
3280 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003281#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003282 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003283 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003284 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3285 hash, hash_length,
3286 mbedtls_ctr_drbg_random,
3287 &global_data.ctr_drbg ) );
3288 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003289
3290 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3291 signature,
3292 curve_bytes ) );
3293 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3294 signature + curve_bytes,
3295 curve_bytes ) );
3296
3297cleanup:
3298 mbedtls_mpi_free( &r );
3299 mbedtls_mpi_free( &s );
3300 if( ret == 0 )
3301 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003302 return( mbedtls_to_psa_error( ret ) );
3303}
3304
3305static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3306 const uint8_t *hash,
3307 size_t hash_length,
3308 const uint8_t *signature,
3309 size_t signature_length )
3310{
3311 int ret;
3312 mbedtls_mpi r, s;
3313 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3314 mbedtls_mpi_init( &r );
3315 mbedtls_mpi_init( &s );
3316
3317 if( signature_length != 2 * curve_bytes )
3318 return( PSA_ERROR_INVALID_SIGNATURE );
3319
3320 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3321 signature,
3322 curve_bytes ) );
3323 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3324 signature + curve_bytes,
3325 curve_bytes ) );
3326
3327 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3328 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003329
3330cleanup:
3331 mbedtls_mpi_free( &r );
3332 mbedtls_mpi_free( &s );
3333 return( mbedtls_to_psa_error( ret ) );
3334}
3335#endif /* MBEDTLS_ECDSA_C */
3336
Gilles Peskinec5487a82018-12-03 18:08:14 +01003337psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003338 psa_algorithm_t alg,
3339 const uint8_t *hash,
3340 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003341 uint8_t *signature,
3342 size_t signature_size,
3343 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003344{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003345 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003346 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3348 const psa_drv_se_t *drv;
3349 psa_drv_se_context_t *drv_context;
3350#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003351
3352 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003353 /* Immediately reject a zero-length signature buffer. This guarantees
3354 * that signature must be a valid pointer. (On the other hand, the hash
3355 * buffer can in principle be empty since it doesn't actually have
3356 * to be a hash.) */
3357 if( signature_size == 0 )
3358 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003359
Gilles Peskineedc64242019-08-07 21:05:07 +02003360 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003361 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003362 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003363 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003364 {
3365 status = PSA_ERROR_INVALID_ARGUMENT;
3366 goto exit;
3367 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003368
Gilles Peskineedc64242019-08-07 21:05:07 +02003369#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3370 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3371 {
3372 if( drv->asymmetric == NULL ||
3373 drv->asymmetric->p_sign == NULL )
3374 {
3375 status = PSA_ERROR_NOT_SUPPORTED;
3376 goto exit;
3377 }
3378 status = drv->asymmetric->p_sign( drv_context,
3379 slot->data.se.slot_number,
3380 alg,
3381 hash, hash_length,
3382 signature, signature_size,
3383 signature_length );
3384 }
3385 else
3386#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003387#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003388 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003389 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003390 status = psa_rsa_sign( slot->data.rsa,
3391 alg,
3392 hash, hash_length,
3393 signature, signature_size,
3394 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003395 }
3396 else
3397#endif /* defined(MBEDTLS_RSA_C) */
3398#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003399 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003400 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003401#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003402 if(
3403#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3404 PSA_ALG_IS_ECDSA( alg )
3405#else
3406 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3407#endif
3408 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003409 status = psa_ecdsa_sign( slot->data.ecp,
3410 alg,
3411 hash, hash_length,
3412 signature, signature_size,
3413 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003414 else
3415#endif /* defined(MBEDTLS_ECDSA_C) */
3416 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003418 }
itayzafrir5c753392018-05-08 11:18:38 +03003419 }
3420 else
3421#endif /* defined(MBEDTLS_ECP_C) */
3422 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003423 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003424 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003425
3426exit:
3427 /* Fill the unused part of the output buffer (the whole buffer on error,
3428 * the trailing part on success) with something that isn't a valid mac
3429 * (barring an attack on the mac and deliberately-crafted input),
3430 * in case the caller doesn't check the return status properly. */
3431 if( status == PSA_SUCCESS )
3432 memset( signature + *signature_length, '!',
3433 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003434 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003435 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003436 /* If signature_size is 0 then we have nothing to do. We must not call
3437 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003438 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003439}
3440
Gilles Peskinec5487a82018-12-03 18:08:14 +01003441psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003442 psa_algorithm_t alg,
3443 const uint8_t *hash,
3444 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003445 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003446 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003447{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003448 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003449 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003450#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3451 const psa_drv_se_t *drv;
3452 psa_drv_se_context_t *drv_context;
3453#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003454
Gilles Peskineedc64242019-08-07 21:05:07 +02003455 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003456 if( status != PSA_SUCCESS )
3457 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003458
Gilles Peskineedc64242019-08-07 21:05:07 +02003459#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3460 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3461 {
3462 if( drv->asymmetric == NULL ||
3463 drv->asymmetric->p_verify == NULL )
3464 return( PSA_ERROR_NOT_SUPPORTED );
3465 return( drv->asymmetric->p_verify( drv_context,
3466 slot->data.se.slot_number,
3467 alg,
3468 hash, hash_length,
3469 signature, signature_length ) );
3470 }
3471 else
3472#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003473#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003474 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003475 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003476 return( psa_rsa_verify( slot->data.rsa,
3477 alg,
3478 hash, hash_length,
3479 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003480 }
3481 else
3482#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003483#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003484 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003485 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003486#if defined(MBEDTLS_ECDSA_C)
3487 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003488 return( psa_ecdsa_verify( slot->data.ecp,
3489 hash, hash_length,
3490 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003491 else
3492#endif /* defined(MBEDTLS_ECDSA_C) */
3493 {
3494 return( PSA_ERROR_INVALID_ARGUMENT );
3495 }
itayzafrir5c753392018-05-08 11:18:38 +03003496 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003497 else
3498#endif /* defined(MBEDTLS_ECP_C) */
3499 {
3500 return( PSA_ERROR_NOT_SUPPORTED );
3501 }
3502}
3503
Gilles Peskine072ac562018-06-30 00:21:29 +02003504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3505static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3506 mbedtls_rsa_context *rsa )
3507{
3508 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3509 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3510 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3511 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3512}
3513#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3514
Gilles Peskinec5487a82018-12-03 18:08:14 +01003515psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003516 psa_algorithm_t alg,
3517 const uint8_t *input,
3518 size_t input_length,
3519 const uint8_t *salt,
3520 size_t salt_length,
3521 uint8_t *output,
3522 size_t output_size,
3523 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003525 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003526 psa_status_t status;
3527
Darryl Green5cc689a2018-07-24 15:34:10 +01003528 (void) input;
3529 (void) input_length;
3530 (void) salt;
3531 (void) output;
3532 (void) output_size;
3533
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003534 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003536 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3537 return( PSA_ERROR_INVALID_ARGUMENT );
3538
Gilles Peskine28f8f302019-07-24 13:30:31 +02003539 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003540 if( status != PSA_SUCCESS )
3541 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003542 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3543 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003544 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545
3546#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003547 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003548 {
3549 mbedtls_rsa_context *rsa = slot->data.rsa;
3550 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003551 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003552 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003554 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003555 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003556 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3557 mbedtls_ctr_drbg_random,
3558 &global_data.ctr_drbg,
3559 MBEDTLS_RSA_PUBLIC,
3560 input_length,
3561 input,
3562 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003563 }
3564 else
3565#endif /* MBEDTLS_PKCS1_V15 */
3566#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003567 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003569 psa_rsa_oaep_set_padding_mode( alg, rsa );
3570 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3571 mbedtls_ctr_drbg_random,
3572 &global_data.ctr_drbg,
3573 MBEDTLS_RSA_PUBLIC,
3574 salt, salt_length,
3575 input_length,
3576 input,
3577 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003578 }
3579 else
3580#endif /* MBEDTLS_PKCS1_V21 */
3581 {
3582 return( PSA_ERROR_INVALID_ARGUMENT );
3583 }
3584 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003585 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586 return( mbedtls_to_psa_error( ret ) );
3587 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003589#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590 {
3591 return( PSA_ERROR_NOT_SUPPORTED );
3592 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003593}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594
Gilles Peskinec5487a82018-12-03 18:08:14 +01003595psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003596 psa_algorithm_t alg,
3597 const uint8_t *input,
3598 size_t input_length,
3599 const uint8_t *salt,
3600 size_t salt_length,
3601 uint8_t *output,
3602 size_t output_size,
3603 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003605 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003606 psa_status_t status;
3607
Darryl Green5cc689a2018-07-24 15:34:10 +01003608 (void) input;
3609 (void) input_length;
3610 (void) salt;
3611 (void) output;
3612 (void) output_size;
3613
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003614 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003616 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3617 return( PSA_ERROR_INVALID_ARGUMENT );
3618
Gilles Peskine28f8f302019-07-24 13:30:31 +02003619 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003620 if( status != PSA_SUCCESS )
3621 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003622 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 return( PSA_ERROR_INVALID_ARGUMENT );
3624
3625#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003626 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627 {
3628 mbedtls_rsa_context *rsa = slot->data.rsa;
3629 int ret;
3630
Gilles Peskine630a18a2018-06-29 17:49:35 +02003631 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632 return( PSA_ERROR_INVALID_ARGUMENT );
3633
3634#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003635 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003636 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003637 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3638 mbedtls_ctr_drbg_random,
3639 &global_data.ctr_drbg,
3640 MBEDTLS_RSA_PRIVATE,
3641 output_length,
3642 input,
3643 output,
3644 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645 }
3646 else
3647#endif /* MBEDTLS_PKCS1_V15 */
3648#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003649 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003651 psa_rsa_oaep_set_padding_mode( alg, rsa );
3652 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3653 mbedtls_ctr_drbg_random,
3654 &global_data.ctr_drbg,
3655 MBEDTLS_RSA_PRIVATE,
3656 salt, salt_length,
3657 output_length,
3658 input,
3659 output,
3660 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003661 }
3662 else
3663#endif /* MBEDTLS_PKCS1_V21 */
3664 {
3665 return( PSA_ERROR_INVALID_ARGUMENT );
3666 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003667
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668 return( mbedtls_to_psa_error( ret ) );
3669 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003671#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672 {
3673 return( PSA_ERROR_NOT_SUPPORTED );
3674 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003675}
Gilles Peskine20035e32018-02-03 22:44:14 +01003676
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003677
3678
mohammad1603503973b2018-03-12 15:59:30 +02003679/****************************************************************/
3680/* Symmetric cryptography */
3681/****************************************************************/
3682
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003683/* Initialize the cipher operation structure. Once this function has been
3684 * called, psa_cipher_abort can run and will do the right thing. */
3685static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3686 psa_algorithm_t alg )
3687{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003688 if( ! PSA_ALG_IS_CIPHER( alg ) )
3689 {
3690 memset( operation, 0, sizeof( *operation ) );
3691 return( PSA_ERROR_INVALID_ARGUMENT );
3692 }
3693
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003694 operation->alg = alg;
3695 operation->key_set = 0;
3696 operation->iv_set = 0;
3697 operation->iv_required = 1;
3698 operation->iv_size = 0;
3699 operation->block_size = 0;
3700 mbedtls_cipher_init( &operation->ctx.cipher );
3701 return( PSA_SUCCESS );
3702}
3703
Gilles Peskinee553c652018-06-04 16:22:46 +02003704static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003705 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003706 psa_algorithm_t alg,
3707 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003708{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003709 int ret = 0;
3710 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003711 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003712 size_t key_bits;
3713 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003714 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3715 PSA_KEY_USAGE_ENCRYPT :
3716 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003717
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003718 /* A context must be freshly initialized before it can be set up. */
3719 if( operation->alg != 0 )
3720 {
3721 return( PSA_ERROR_BAD_STATE );
3722 }
3723
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003724 status = psa_cipher_init( operation, alg );
3725 if( status != PSA_SUCCESS )
3726 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003727
Gilles Peskine28f8f302019-07-24 13:30:31 +02003728 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003729 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003730 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003731 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003732
Gilles Peskine8e338702019-07-30 20:06:31 +02003733 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003734 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003735 {
3736 status = PSA_ERROR_NOT_SUPPORTED;
3737 goto exit;
3738 }
mohammad1603503973b2018-03-12 15:59:30 +02003739
mohammad1603503973b2018-03-12 15:59:30 +02003740 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003741 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003742 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003743
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003744#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003745 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003746 {
3747 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003748 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003749 memcpy( keys, slot->data.raw.data, 16 );
3750 memcpy( keys + 16, slot->data.raw.data, 8 );
3751 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3752 keys,
3753 192, cipher_operation );
3754 }
3755 else
3756#endif
3757 {
3758 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3759 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003760 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003761 }
Moran Peker41deec42018-04-04 15:43:05 +03003762 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003763 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003764
mohammad16038481e742018-03-18 13:57:31 +02003765#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003766 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003767 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003768 case PSA_ALG_CBC_NO_PADDING:
3769 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3770 MBEDTLS_PADDING_NONE );
3771 break;
3772 case PSA_ALG_CBC_PKCS7:
3773 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3774 MBEDTLS_PADDING_PKCS7 );
3775 break;
3776 default:
3777 /* The algorithm doesn't involve padding. */
3778 ret = 0;
3779 break;
3780 }
3781 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003782 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003783#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3784
mohammad1603503973b2018-03-12 15:59:30 +02003785 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003786 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003787 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003788 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003789 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003790 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003791 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003792#if defined(MBEDTLS_CHACHA20_C)
3793 else
3794 if( alg == PSA_ALG_CHACHA20 )
3795 operation->iv_size = 12;
3796#endif
mohammad1603503973b2018-03-12 15:59:30 +02003797
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003798exit:
3799 if( status == 0 )
3800 status = mbedtls_to_psa_error( ret );
3801 if( status != 0 )
3802 psa_cipher_abort( operation );
3803 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003804}
3805
Gilles Peskinefe119512018-07-08 21:39:34 +02003806psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003807 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003808 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003809{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003810 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003811}
3812
Gilles Peskinefe119512018-07-08 21:39:34 +02003813psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003814 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003815 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003816{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003817 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003818}
3819
Gilles Peskinefe119512018-07-08 21:39:34 +02003820psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003821 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003822 size_t iv_size,
3823 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003824{
itayzafrir534bd7c2018-08-02 13:56:32 +03003825 psa_status_t status;
3826 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003827 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003828 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003829 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003830 }
Moran Peker41deec42018-04-04 15:43:05 +03003831 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003832 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003833 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003834 goto exit;
3835 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003836 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3837 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003838 if( ret != 0 )
3839 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003840 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003841 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003842 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003843
mohammad16038481e742018-03-18 13:57:31 +02003844 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003845 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003846
Moran Peker395db872018-05-31 14:07:14 +03003847exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003848 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003849 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003850 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003851}
3852
Gilles Peskinefe119512018-07-08 21:39:34 +02003853psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003854 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003855 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003856{
itayzafrir534bd7c2018-08-02 13:56:32 +03003857 psa_status_t status;
3858 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003859 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003860 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003861 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003862 }
Moran Pekera28258c2018-05-29 16:25:04 +03003863 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003864 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003865 status = PSA_ERROR_INVALID_ARGUMENT;
3866 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003867 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003868 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3869 status = mbedtls_to_psa_error( ret );
3870exit:
3871 if( status == PSA_SUCCESS )
3872 operation->iv_set = 1;
3873 else
mohammad1603503973b2018-03-12 15:59:30 +02003874 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003875 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003876}
3877
Gilles Peskinee553c652018-06-04 16:22:46 +02003878psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3879 const uint8_t *input,
3880 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003881 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003882 size_t output_size,
3883 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003884{
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 psa_status_t status;
3886 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003887 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003888
3889 if( operation->alg == 0 )
3890 {
3891 return( PSA_ERROR_BAD_STATE );
3892 }
3893
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003894 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003895 {
3896 /* Take the unprocessed partial block left over from previous
3897 * update calls, if any, plus the input to this call. Remove
3898 * the last partial block, if any. You get the data that will be
3899 * output in this call. */
3900 expected_output_size =
3901 ( operation->ctx.cipher.unprocessed_len + input_length )
3902 / operation->block_size * operation->block_size;
3903 }
3904 else
3905 {
3906 expected_output_size = input_length;
3907 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003908
Gilles Peskine89d789c2018-06-04 17:17:16 +02003909 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003910 {
3911 status = PSA_ERROR_BUFFER_TOO_SMALL;
3912 goto exit;
3913 }
mohammad160382759612018-03-12 18:16:40 +02003914
mohammad1603503973b2018-03-12 15:59:30 +02003915 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003916 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003917 status = mbedtls_to_psa_error( ret );
3918exit:
3919 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003920 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003921 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003922}
3923
Gilles Peskinee553c652018-06-04 16:22:46 +02003924psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3925 uint8_t *output,
3926 size_t output_size,
3927 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003928{
David Saadab4ecc272019-02-14 13:48:10 +02003929 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003930 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003931 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003932
mohammad1603503973b2018-03-12 15:59:30 +02003933 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003934 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003935 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003936 }
3937 if( operation->iv_required && ! operation->iv_set )
3938 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003939 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003940 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003941
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003942 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003943 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3944 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003945 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003946 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003947 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003948 }
3949
Janos Follath315b51c2018-07-09 16:04:51 +01003950 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3951 temp_output_buffer,
3952 output_length );
3953 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003954 {
Janos Follath315b51c2018-07-09 16:04:51 +01003955 status = mbedtls_to_psa_error( cipher_ret );
3956 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003957 }
Janos Follath315b51c2018-07-09 16:04:51 +01003958
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003959 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003960 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003961 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003962 memcpy( output, temp_output_buffer, *output_length );
3963 else
3964 {
Janos Follath315b51c2018-07-09 16:04:51 +01003965 status = PSA_ERROR_BUFFER_TOO_SMALL;
3966 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003967 }
mohammad1603503973b2018-03-12 15:59:30 +02003968
Gilles Peskine3f108122018-12-07 18:14:53 +01003969 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003970 status = psa_cipher_abort( operation );
3971
3972 return( status );
3973
3974error:
3975
3976 *output_length = 0;
3977
Gilles Peskine3f108122018-12-07 18:14:53 +01003978 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003979 (void) psa_cipher_abort( operation );
3980
3981 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003982}
3983
Gilles Peskinee553c652018-06-04 16:22:46 +02003984psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3985{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003986 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003987 {
3988 /* The object has (apparently) been initialized but it is not
3989 * in use. It's ok to call abort on such an object, and there's
3990 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003991 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003992 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003993
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003994 /* Sanity check (shouldn't happen: operation->alg should
3995 * always have been initialized to a valid value). */
3996 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3997 return( PSA_ERROR_BAD_STATE );
3998
mohammad1603503973b2018-03-12 15:59:30 +02003999 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004000
Moran Peker41deec42018-04-04 15:43:05 +03004001 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004002 operation->key_set = 0;
4003 operation->iv_set = 0;
4004 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004005 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004006 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004007
Moran Peker395db872018-05-31 14:07:14 +03004008 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004009}
4010
Gilles Peskinea0655c32018-04-30 17:06:50 +02004011
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004012
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004013
mohammad16035955c982018-04-26 00:53:03 +03004014/****************************************************************/
4015/* AEAD */
4016/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004017
Gilles Peskineedf9a652018-08-17 18:11:56 +02004018typedef struct
4019{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004020 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004021 const mbedtls_cipher_info_t *cipher_info;
4022 union
4023 {
4024#if defined(MBEDTLS_CCM_C)
4025 mbedtls_ccm_context ccm;
4026#endif /* MBEDTLS_CCM_C */
4027#if defined(MBEDTLS_GCM_C)
4028 mbedtls_gcm_context gcm;
4029#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004030#if defined(MBEDTLS_CHACHAPOLY_C)
4031 mbedtls_chachapoly_context chachapoly;
4032#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004033 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004034 psa_algorithm_t core_alg;
4035 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004036 uint8_t tag_length;
4037} aead_operation_t;
4038
Gilles Peskine30a9e412019-01-14 18:36:12 +01004039static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004040{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004041 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004042 {
4043#if defined(MBEDTLS_CCM_C)
4044 case PSA_ALG_CCM:
4045 mbedtls_ccm_free( &operation->ctx.ccm );
4046 break;
4047#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004048#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049 case PSA_ALG_GCM:
4050 mbedtls_gcm_free( &operation->ctx.gcm );
4051 break;
4052#endif /* MBEDTLS_GCM_C */
4053 }
4054}
4055
4056static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004057 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004058 psa_key_usage_t usage,
4059 psa_algorithm_t alg )
4060{
4061 psa_status_t status;
4062 size_t key_bits;
4063 mbedtls_cipher_id_t cipher_id;
4064
Gilles Peskine28f8f302019-07-24 13:30:31 +02004065 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004066 if( status != PSA_SUCCESS )
4067 return( status );
4068
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004069 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004070
4071 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004072 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073 &cipher_id );
4074 if( operation->cipher_info == NULL )
4075 return( PSA_ERROR_NOT_SUPPORTED );
4076
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004077 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004078 {
4079#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004080 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4081 operation->core_alg = PSA_ALG_CCM;
4082 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004083 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4084 * The call to mbedtls_ccm_encrypt_and_tag or
4085 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004086 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 return( PSA_ERROR_INVALID_ARGUMENT );
4088 mbedtls_ccm_init( &operation->ctx.ccm );
4089 status = mbedtls_to_psa_error(
4090 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4091 operation->slot->data.raw.data,
4092 (unsigned int) key_bits ) );
4093 if( status != 0 )
4094 goto cleanup;
4095 break;
4096#endif /* MBEDTLS_CCM_C */
4097
4098#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004099 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4100 operation->core_alg = PSA_ALG_GCM;
4101 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004102 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4103 * The call to mbedtls_gcm_crypt_and_tag or
4104 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004105 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004106 return( PSA_ERROR_INVALID_ARGUMENT );
4107 mbedtls_gcm_init( &operation->ctx.gcm );
4108 status = mbedtls_to_psa_error(
4109 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4110 operation->slot->data.raw.data,
4111 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004112 if( status != 0 )
4113 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114 break;
4115#endif /* MBEDTLS_GCM_C */
4116
Gilles Peskine26869f22019-05-06 15:25:00 +02004117#if defined(MBEDTLS_CHACHAPOLY_C)
4118 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4119 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4120 operation->full_tag_length = 16;
4121 /* We only support the default tag length. */
4122 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4123 return( PSA_ERROR_NOT_SUPPORTED );
4124 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4125 status = mbedtls_to_psa_error(
4126 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4127 operation->slot->data.raw.data ) );
4128 if( status != 0 )
4129 goto cleanup;
4130 break;
4131#endif /* MBEDTLS_CHACHAPOLY_C */
4132
Gilles Peskineedf9a652018-08-17 18:11:56 +02004133 default:
4134 return( PSA_ERROR_NOT_SUPPORTED );
4135 }
4136
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004137 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4138 {
4139 status = PSA_ERROR_INVALID_ARGUMENT;
4140 goto cleanup;
4141 }
4142 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004143
Gilles Peskineedf9a652018-08-17 18:11:56 +02004144 return( PSA_SUCCESS );
4145
4146cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004147 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004148 return( status );
4149}
4150
Gilles Peskinec5487a82018-12-03 18:08:14 +01004151psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004152 psa_algorithm_t alg,
4153 const uint8_t *nonce,
4154 size_t nonce_length,
4155 const uint8_t *additional_data,
4156 size_t additional_data_length,
4157 const uint8_t *plaintext,
4158 size_t plaintext_length,
4159 uint8_t *ciphertext,
4160 size_t ciphertext_size,
4161 size_t *ciphertext_length )
4162{
mohammad16035955c982018-04-26 00:53:03 +03004163 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004164 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004165 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004166
mohammad1603f08a5502018-06-03 15:05:47 +03004167 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004168
Gilles Peskinec5487a82018-12-03 18:08:14 +01004169 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004170 if( status != PSA_SUCCESS )
4171 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004172
Gilles Peskineedf9a652018-08-17 18:11:56 +02004173 /* For all currently supported modes, the tag is at the end of the
4174 * ciphertext. */
4175 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4176 {
4177 status = PSA_ERROR_BUFFER_TOO_SMALL;
4178 goto exit;
4179 }
4180 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004181
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004182#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004183 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004184 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004185 status = mbedtls_to_psa_error(
4186 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4187 MBEDTLS_GCM_ENCRYPT,
4188 plaintext_length,
4189 nonce, nonce_length,
4190 additional_data, additional_data_length,
4191 plaintext, ciphertext,
4192 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004193 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004194 else
4195#endif /* MBEDTLS_GCM_C */
4196#if defined(MBEDTLS_CCM_C)
4197 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004198 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004199 status = mbedtls_to_psa_error(
4200 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4201 plaintext_length,
4202 nonce, nonce_length,
4203 additional_data,
4204 additional_data_length,
4205 plaintext, ciphertext,
4206 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004207 }
mohammad16035c8845f2018-05-09 05:40:09 -07004208 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004209#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004210#if defined(MBEDTLS_CHACHAPOLY_C)
4211 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4212 {
4213 if( nonce_length != 12 || operation.tag_length != 16 )
4214 {
4215 status = PSA_ERROR_NOT_SUPPORTED;
4216 goto exit;
4217 }
4218 status = mbedtls_to_psa_error(
4219 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4220 plaintext_length,
4221 nonce,
4222 additional_data,
4223 additional_data_length,
4224 plaintext,
4225 ciphertext,
4226 tag ) );
4227 }
4228 else
4229#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004230 {
mohammad1603554faad2018-06-03 15:07:38 +03004231 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004232 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004233
Gilles Peskineedf9a652018-08-17 18:11:56 +02004234 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4235 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004236
Gilles Peskineedf9a652018-08-17 18:11:56 +02004237exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004238 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004239 if( status == PSA_SUCCESS )
4240 *ciphertext_length = plaintext_length + operation.tag_length;
4241 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004242}
4243
Gilles Peskineee652a32018-06-01 19:23:52 +02004244/* Locate the tag in a ciphertext buffer containing the encrypted data
4245 * followed by the tag. Return the length of the part preceding the tag in
4246 * *plaintext_length. This is the size of the plaintext in modes where
4247 * the encrypted data has the same size as the plaintext, such as
4248 * CCM and GCM. */
4249static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4250 const uint8_t *ciphertext,
4251 size_t ciphertext_length,
4252 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004253 const uint8_t **p_tag )
4254{
4255 size_t payload_length;
4256 if( tag_length > ciphertext_length )
4257 return( PSA_ERROR_INVALID_ARGUMENT );
4258 payload_length = ciphertext_length - tag_length;
4259 if( payload_length > plaintext_size )
4260 return( PSA_ERROR_BUFFER_TOO_SMALL );
4261 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004262 return( PSA_SUCCESS );
4263}
4264
Gilles Peskinec5487a82018-12-03 18:08:14 +01004265psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004266 psa_algorithm_t alg,
4267 const uint8_t *nonce,
4268 size_t nonce_length,
4269 const uint8_t *additional_data,
4270 size_t additional_data_length,
4271 const uint8_t *ciphertext,
4272 size_t ciphertext_length,
4273 uint8_t *plaintext,
4274 size_t plaintext_size,
4275 size_t *plaintext_length )
4276{
mohammad16035955c982018-04-26 00:53:03 +03004277 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004278 aead_operation_t operation;
4279 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004280
Gilles Peskineee652a32018-06-01 19:23:52 +02004281 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004282
Gilles Peskinec5487a82018-12-03 18:08:14 +01004283 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004284 if( status != PSA_SUCCESS )
4285 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004286
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004287 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4288 ciphertext, ciphertext_length,
4289 plaintext_size, &tag );
4290 if( status != PSA_SUCCESS )
4291 goto exit;
4292
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004293#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004294 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004295 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004296 status = mbedtls_to_psa_error(
4297 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4298 ciphertext_length - operation.tag_length,
4299 nonce, nonce_length,
4300 additional_data,
4301 additional_data_length,
4302 tag, operation.tag_length,
4303 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004304 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004305 else
4306#endif /* MBEDTLS_GCM_C */
4307#if defined(MBEDTLS_CCM_C)
4308 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004309 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004310 status = mbedtls_to_psa_error(
4311 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4312 ciphertext_length - operation.tag_length,
4313 nonce, nonce_length,
4314 additional_data,
4315 additional_data_length,
4316 ciphertext, plaintext,
4317 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004318 }
mohammad160339574652018-06-01 04:39:53 -07004319 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004320#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004321#if defined(MBEDTLS_CHACHAPOLY_C)
4322 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4323 {
4324 if( nonce_length != 12 || operation.tag_length != 16 )
4325 {
4326 status = PSA_ERROR_NOT_SUPPORTED;
4327 goto exit;
4328 }
4329 status = mbedtls_to_psa_error(
4330 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4331 ciphertext_length - operation.tag_length,
4332 nonce,
4333 additional_data,
4334 additional_data_length,
4335 tag,
4336 ciphertext,
4337 plaintext ) );
4338 }
4339 else
4340#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004341 {
mohammad1603554faad2018-06-03 15:07:38 +03004342 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004343 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004344
Gilles Peskineedf9a652018-08-17 18:11:56 +02004345 if( status != PSA_SUCCESS && plaintext_size != 0 )
4346 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004347
Gilles Peskineedf9a652018-08-17 18:11:56 +02004348exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004349 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004350 if( status == PSA_SUCCESS )
4351 *plaintext_length = ciphertext_length - operation.tag_length;
4352 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004353}
4354
Gilles Peskinea0655c32018-04-30 17:06:50 +02004355
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004356
Gilles Peskine20035e32018-02-03 22:44:14 +01004357/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004358/* Generators */
4359/****************************************************************/
4360
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004361#define HKDF_STATE_INIT 0 /* no input yet */
4362#define HKDF_STATE_STARTED 1 /* got salt */
4363#define HKDF_STATE_KEYED 2 /* got key */
4364#define HKDF_STATE_OUTPUT 3 /* output started */
4365
Gilles Peskinecbe66502019-05-16 16:59:18 +02004366static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004368{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4370 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004371 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004373}
4374
4375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004377{
4378 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004380 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004381 {
4382 /* The object has (apparently) been initialized but it is not
4383 * in use. It's ok to call abort on such an object, and there's
4384 * nothing to do. */
4385 }
4386 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004387#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004388 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004389 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 mbedtls_free( operation->ctx.hkdf.info );
4391 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004392 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004393 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004395 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004396 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004397 if( operation->ctx.tls12_prf.seed != NULL )
4398 {
4399 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4400 operation->ctx.tls12_prf.seed_length );
4401 mbedtls_free( operation->ctx.tls12_prf.seed );
4402 }
4403
4404 if( operation->ctx.tls12_prf.label != NULL )
4405 {
4406 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4407 operation->ctx.tls12_prf.label_length );
4408 mbedtls_free( operation->ctx.tls12_prf.label );
4409 }
4410
4411 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4412
4413 /* We leave the fields Ai and output_block to be erased safely by the
4414 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004415 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004416 else
4417#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004418 {
4419 status = PSA_ERROR_BAD_STATE;
4420 }
Janos Follath083036a2019-06-11 10:22:26 +01004421 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004422 return( status );
4423}
4424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004426 size_t *capacity)
4427{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004429 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004431 return PSA_ERROR_BAD_STATE;
4432 }
4433
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004435 return( PSA_SUCCESS );
4436}
4437
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439 size_t capacity )
4440{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004442 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004444 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004446 return( PSA_SUCCESS );
4447}
4448
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004451 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004452static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004453 psa_algorithm_t hash_alg,
4454 uint8_t *output,
4455 size_t output_length )
4456{
4457 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4458 psa_status_t status;
4459
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004460 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4461 return( PSA_ERROR_BAD_STATE );
4462 hkdf->state = HKDF_STATE_OUTPUT;
4463
Gilles Peskinebef7f142018-07-12 17:22:21 +02004464 while( output_length != 0 )
4465 {
4466 /* Copy what remains of the current block */
4467 uint8_t n = hash_length - hkdf->offset_in_block;
4468 if( n > output_length )
4469 n = (uint8_t) output_length;
4470 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4471 output += n;
4472 output_length -= n;
4473 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004474 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004475 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004476 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004477 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004478 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004479 * object was corrupted or if this function is called directly
4480 * inside the library. */
4481 if( hkdf->block_number == 0xff )
4482 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004483
4484 /* We need a new block */
4485 ++hkdf->block_number;
4486 hkdf->offset_in_block = 0;
4487 status = psa_hmac_setup_internal( &hkdf->hmac,
4488 hkdf->prk, hash_length,
4489 hash_alg );
4490 if( status != PSA_SUCCESS )
4491 return( status );
4492 if( hkdf->block_number != 1 )
4493 {
4494 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4495 hkdf->output_block,
4496 hash_length );
4497 if( status != PSA_SUCCESS )
4498 return( status );
4499 }
4500 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4501 hkdf->info,
4502 hkdf->info_length );
4503 if( status != PSA_SUCCESS )
4504 return( status );
4505 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4506 &hkdf->block_number, 1 );
4507 if( status != PSA_SUCCESS )
4508 return( status );
4509 status = psa_hmac_finish_internal( &hkdf->hmac,
4510 hkdf->output_block,
4511 sizeof( hkdf->output_block ) );
4512 if( status != PSA_SUCCESS )
4513 return( status );
4514 }
4515
4516 return( PSA_SUCCESS );
4517}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004518
Janos Follath7742fee2019-06-17 12:58:10 +01004519static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4520 psa_tls12_prf_key_derivation_t *tls12_prf,
4521 psa_algorithm_t alg )
4522{
4523 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4524 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004525 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4526 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004527
Janos Follath7742fee2019-06-17 12:58:10 +01004528 /* We can't be wanting more output after block 0xff, otherwise
4529 * the capacity check in psa_key_derivation_output_bytes() would have
4530 * prevented this call. It could happen only if the operation
4531 * object was corrupted or if this function is called directly
4532 * inside the library. */
4533 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004534 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004535
4536 /* We need a new block */
4537 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004538 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004539
4540 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4541 *
4542 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4543 *
4544 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4545 * HMAC_hash(secret, A(2) + seed) +
4546 * HMAC_hash(secret, A(3) + seed) + ...
4547 *
4548 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004549 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004550 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004551 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004552 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004553 * is currently extracted as `output_block` and where i is
4554 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004555 */
4556
Janos Follathea29bfb2019-06-19 12:21:20 +01004557 /* Save the hash context before using it, to preserve the hash state with
4558 * only the inner padding in it. We need this, because inner padding depends
4559 * on the key (secret in the RFC's terminology). */
4560 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4561 if( status != PSA_SUCCESS )
4562 goto cleanup;
4563
4564 /* Calculate A(i) where i = tls12_prf->block_number. */
4565 if( tls12_prf->block_number == 1 )
4566 {
4567 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4568 * the variable seed and in this instance means it in the context of the
4569 * P_hash function, where seed = label + seed.) */
4570 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4571 tls12_prf->label, tls12_prf->label_length );
4572 if( status != PSA_SUCCESS )
4573 goto cleanup;
4574 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4575 tls12_prf->seed, tls12_prf->seed_length );
4576 if( status != PSA_SUCCESS )
4577 goto cleanup;
4578 }
4579 else
4580 {
4581 /* A(i) = HMAC_hash(secret, A(i-1)) */
4582 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4583 tls12_prf->Ai, hash_length );
4584 if( status != PSA_SUCCESS )
4585 goto cleanup;
4586 }
4587
4588 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4589 tls12_prf->Ai, hash_length );
4590 if( status != PSA_SUCCESS )
4591 goto cleanup;
4592 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4593 if( status != PSA_SUCCESS )
4594 goto cleanup;
4595
4596 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4597 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4598 tls12_prf->Ai, hash_length );
4599 if( status != PSA_SUCCESS )
4600 goto cleanup;
4601 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4602 tls12_prf->label, tls12_prf->label_length );
4603 if( status != PSA_SUCCESS )
4604 goto cleanup;
4605 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4606 tls12_prf->seed, tls12_prf->seed_length );
4607 if( status != PSA_SUCCESS )
4608 goto cleanup;
4609 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4610 tls12_prf->output_block, hash_length );
4611 if( status != PSA_SUCCESS )
4612 goto cleanup;
4613 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4614 if( status != PSA_SUCCESS )
4615 goto cleanup;
4616
Janos Follath7742fee2019-06-17 12:58:10 +01004617
4618cleanup:
4619
Janos Follathea29bfb2019-06-19 12:21:20 +01004620 cleanup_status = psa_hash_abort( &backup );
4621 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4622 status = cleanup_status;
4623
4624 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004625}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004626
Janos Follath844eb0e2019-06-19 12:10:49 +01004627static psa_status_t psa_key_derivation_tls12_prf_read(
4628 psa_tls12_prf_key_derivation_t *tls12_prf,
4629 psa_algorithm_t alg,
4630 uint8_t *output,
4631 size_t output_length )
4632{
4633 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4634 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4635 psa_status_t status;
4636 uint8_t offset, length;
4637
4638 while( output_length != 0 )
4639 {
4640 /* Check if we have fully processed the current block. */
4641 if( tls12_prf->left_in_block == 0 )
4642 {
4643 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4644 alg );
4645 if( status != PSA_SUCCESS )
4646 return( status );
4647
4648 continue;
4649 }
4650
4651 if( tls12_prf->left_in_block > output_length )
4652 length = (uint8_t) output_length;
4653 else
4654 length = tls12_prf->left_in_block;
4655
4656 offset = hash_length - tls12_prf->left_in_block;
4657 memcpy( output, tls12_prf->output_block + offset, length );
4658 output += length;
4659 output_length -= length;
4660 tls12_prf->left_in_block -= length;
4661 }
4662
4663 return( PSA_SUCCESS );
4664}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004665#endif /* MBEDTLS_MD_C */
4666
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004667psa_status_t psa_key_derivation_output_bytes(
4668 psa_key_derivation_operation_t *operation,
4669 uint8_t *output,
4670 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671{
4672 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004676 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004678 return PSA_ERROR_BAD_STATE;
4679 }
4680
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004684 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004686 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 goto exit;
4688 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004692 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4694 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004696 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004697 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004698 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004700
Gilles Peskinebef7f142018-07-12 17:22:21 +02004701#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004702 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004703 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004704 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004706 output, output_length );
4707 }
Janos Follathadbec812019-06-14 11:05:39 +01004708 else
Janos Follathadbec812019-06-14 11:05:39 +01004709 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004710 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004711 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004713 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004714 output_length );
4715 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004716 else
4717#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004718 {
4719 return( PSA_ERROR_BAD_STATE );
4720 }
4721
4722exit:
4723 if( status != PSA_SUCCESS )
4724 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004725 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 * This allows us to differentiate between exhausted operations and
4727 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4728 * operations. */
4729 psa_algorithm_t alg = operation->alg;
4730 psa_key_derivation_abort( operation );
4731 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 memset( output, '!', output_length );
4733 }
4734 return( status );
4735}
4736
Gilles Peskine08542d82018-07-19 17:05:42 +02004737#if defined(MBEDTLS_DES_C)
4738static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4739{
4740 if( data_size >= 8 )
4741 mbedtls_des_key_set_parity( data );
4742 if( data_size >= 16 )
4743 mbedtls_des_key_set_parity( data + 8 );
4744 if( data_size >= 24 )
4745 mbedtls_des_key_set_parity( data + 16 );
4746}
4747#endif /* MBEDTLS_DES_C */
4748
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004749static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004750 psa_key_slot_t *slot,
4751 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753{
4754 uint8_t *data = NULL;
4755 size_t bytes = PSA_BITS_TO_BYTES( bits );
4756 psa_status_t status;
4757
Gilles Peskine8e338702019-07-30 20:06:31 +02004758 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004759 return( PSA_ERROR_INVALID_ARGUMENT );
4760 if( bits % 8 != 0 )
4761 return( PSA_ERROR_INVALID_ARGUMENT );
4762 data = mbedtls_calloc( 1, bytes );
4763 if( data == NULL )
4764 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4765
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004767 if( status != PSA_SUCCESS )
4768 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004769#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004770 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004771 psa_des_set_key_parity( data, bytes );
4772#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004773 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004774
4775exit:
4776 mbedtls_free( data );
4777 return( status );
4778}
4779
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004780psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004782 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004783{
4784 psa_status_t status;
4785 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004786 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004787
4788 /* Reject any attempt to create a zero-length key so that we don't
4789 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4790 if( psa_get_key_bits( attributes ) == 0 )
4791 return( PSA_ERROR_INVALID_ARGUMENT );
4792
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004793 if( ! operation->can_output_key )
4794 return( PSA_ERROR_NOT_PERMITTED );
4795
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
Gilles Peskineb8965192019-09-24 16:21:10 +02005085/** Check whether the given key type is acceptable for the given
5086 * input step of a key derivation.
5087 *
5088 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5089 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5090 * Both secret and non-secret inputs can alternatively have the type
5091 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5092 * that the input was passed as a buffer rather than via a key object.
5093 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005094static int psa_key_derivation_check_input_type(
5095 psa_key_derivation_step_t step,
5096 psa_key_type_t key_type )
5097{
5098 switch( step )
5099 {
5100 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005101 if( key_type == PSA_KEY_TYPE_DERIVE )
5102 return( PSA_SUCCESS );
5103 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005104 return( PSA_SUCCESS );
5105 break;
5106 case PSA_KEY_DERIVATION_INPUT_LABEL:
5107 case PSA_KEY_DERIVATION_INPUT_SALT:
5108 case PSA_KEY_DERIVATION_INPUT_INFO:
5109 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005110 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5111 return( PSA_SUCCESS );
5112 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005113 return( PSA_SUCCESS );
5114 break;
5115 }
5116 return( PSA_ERROR_INVALID_ARGUMENT );
5117}
5118
Janos Follathb80a94e2019-06-12 15:54:46 +01005119static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005121 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005122 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005123 const uint8_t *data,
5124 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005125{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005126 psa_status_t status;
5127 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5128
5129 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005130 if( status != PSA_SUCCESS )
5131 goto exit;
5132
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005134 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005137 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138 step, data, data_length );
5139 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005140 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005141 {
Janos Follathb03233e2019-06-11 15:30:30 +01005142 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5143 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5144 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005145 }
5146 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5147 {
5148 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5149 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5150 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005151 }
5152 else
5153#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156 return( PSA_ERROR_BAD_STATE );
5157 }
5158
Gilles Peskine224b0d62019-09-23 18:13:17 +02005159exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005160 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005162 return( status );
5163}
5164
Janos Follath51f4a0f2019-06-14 11:35:55 +01005165psa_status_t psa_key_derivation_input_bytes(
5166 psa_key_derivation_operation_t *operation,
5167 psa_key_derivation_step_t step,
5168 const uint8_t *data,
5169 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005170{
Gilles Peskineb8965192019-09-24 16:21:10 +02005171 return( psa_key_derivation_input_internal( operation, step,
5172 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005173 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005174}
5175
Janos Follath51f4a0f2019-06-14 11:35:55 +01005176psa_status_t psa_key_derivation_input_key(
5177 psa_key_derivation_operation_t *operation,
5178 psa_key_derivation_step_t step,
5179 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180{
5181 psa_key_slot_t *slot;
5182 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005183
Gilles Peskine28f8f302019-07-24 13:30:31 +02005184 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005185 PSA_KEY_USAGE_DERIVE,
5186 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005188 {
5189 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005191 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005192
5193 /* Passing a key object as a SECRET input unlocks the permission
5194 * to output to a key object. */
5195 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5196 operation->can_output_key = 1;
5197
Janos Follathb80a94e2019-06-12 15:54:46 +01005198 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005199 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005200 slot->data.raw.data,
5201 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005203
5204
5205
5206/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005207/* Key agreement */
5208/****************************************************************/
5209
Gilles Peskinea05219c2018-11-16 16:02:56 +01005210#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005211static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5212 size_t peer_key_length,
5213 const mbedtls_ecp_keypair *our_key,
5214 uint8_t *shared_secret,
5215 size_t shared_secret_size,
5216 size_t *shared_secret_length )
5217{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005218 mbedtls_ecp_keypair *their_key = NULL;
5219 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005220 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005221 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005222
Jaeden Ameroccdce902019-01-10 11:42:27 +00005223 status = psa_import_ec_public_key(
5224 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5225 peer_key, peer_key_length,
5226 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005227 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005228 goto exit;
5229
Jaeden Amero97271b32019-01-10 19:38:51 +00005230 status = mbedtls_to_psa_error(
5231 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5232 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005233 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005234 status = mbedtls_to_psa_error(
5235 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5236 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005237 goto exit;
5238
Jaeden Amero97271b32019-01-10 19:38:51 +00005239 status = mbedtls_to_psa_error(
5240 mbedtls_ecdh_calc_secret( &ecdh,
5241 shared_secret_length,
5242 shared_secret, shared_secret_size,
5243 mbedtls_ctr_drbg_random,
5244 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005245
5246exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005247 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005248 mbedtls_ecp_keypair_free( their_key );
5249 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005250 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005251}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005252#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005253
Gilles Peskine01d718c2018-09-18 12:01:02 +02005254#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5255
Gilles Peskine0216fe12019-04-11 21:23:21 +02005256static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5257 psa_key_slot_t *private_key,
5258 const uint8_t *peer_key,
5259 size_t peer_key_length,
5260 uint8_t *shared_secret,
5261 size_t shared_secret_size,
5262 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005263{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005264 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005266#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005267 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005268 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005269 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005270 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5271 private_key->data.ecp,
5272 shared_secret, shared_secret_size,
5273 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005274#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005275 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005276 (void) private_key;
5277 (void) peer_key;
5278 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005279 (void) shared_secret;
5280 (void) shared_secret_size;
5281 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005282 return( PSA_ERROR_NOT_SUPPORTED );
5283 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005284}
5285
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005286/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005287 * to potentially free embedded data structures and wipe confidential data.
5288 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005290 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005291 psa_key_slot_t *private_key,
5292 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005293 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005294{
5295 psa_status_t status;
5296 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5297 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005299
5300 /* Step 1: run the secret agreement algorithm to generate the shared
5301 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005302 status = psa_key_agreement_raw_internal( ka_alg,
5303 private_key,
5304 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005305 shared_secret,
5306 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005307 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005308 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005309 goto exit;
5310
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005311 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005312 * the shared secret. A shared secret is permitted wherever a key
5313 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005314 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005315 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005316 shared_secret,
5317 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318
Gilles Peskine12313cd2018-06-20 00:20:32 +02005319exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005320 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005321 return( status );
5322}
5323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005325 psa_key_derivation_step_t step,
5326 psa_key_handle_t private_key,
5327 const uint8_t *peer_key,
5328 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005329{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005330 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005331 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005333 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005334 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005335 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005336 if( status != PSA_SUCCESS )
5337 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005339 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005340 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005341 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005343 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005344}
5345
Gilles Peskinebe697d82019-05-16 18:00:41 +02005346psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5347 psa_key_handle_t private_key,
5348 const uint8_t *peer_key,
5349 size_t peer_key_length,
5350 uint8_t *output,
5351 size_t output_size,
5352 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005353{
5354 psa_key_slot_t *slot;
5355 psa_status_t status;
5356
5357 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5358 {
5359 status = PSA_ERROR_INVALID_ARGUMENT;
5360 goto exit;
5361 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005362 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005363 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005364 if( status != PSA_SUCCESS )
5365 goto exit;
5366
5367 status = psa_key_agreement_raw_internal( alg, slot,
5368 peer_key, peer_key_length,
5369 output, output_size,
5370 output_length );
5371
5372exit:
5373 if( status != PSA_SUCCESS )
5374 {
5375 /* If an error happens and is not handled properly, the output
5376 * may be used as a key to protect sensitive data. Arrange for such
5377 * a key to be random, which is likely to result in decryption or
5378 * verification errors. This is better than filling the buffer with
5379 * some constant data such as zeros, which would result in the data
5380 * being protected with a reproducible, easily knowable key.
5381 */
5382 psa_generate_random( output, output_size );
5383 *output_length = output_size;
5384 }
5385 return( status );
5386}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005387
5388
5389/****************************************************************/
5390/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005391/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005392
Gilles Peskine4c317f42018-07-12 01:24:09 +02005393psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005394 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005395{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005396 int ret;
5397 GUARD_MODULE_INITIALIZED;
5398
Gilles Peskinef181eca2019-08-07 13:49:00 +02005399 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5400 {
5401 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5402 output,
5403 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5404 if( ret != 0 )
5405 return( mbedtls_to_psa_error( ret ) );
5406 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5407 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5408 }
5409
Gilles Peskinee59236f2018-01-27 23:32:46 +01005410 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5411 return( mbedtls_to_psa_error( ret ) );
5412}
5413
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005414#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5415#include "mbedtls/entropy_poll.h"
5416
Gilles Peskine7228da22019-07-15 11:06:15 +02005417psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005418 size_t seed_size )
5419{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005420 if( global_data.initialized )
5421 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005422
5423 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5424 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5425 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5426 return( PSA_ERROR_INVALID_ARGUMENT );
5427
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005428 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005429}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005430#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005431
Gilles Peskinee56e8782019-04-26 17:34:02 +02005432#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5433static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5434 size_t domain_parameters_size,
5435 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005436{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 size_t i;
5438 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005439
Gilles Peskinee56e8782019-04-26 17:34:02 +02005440 if( domain_parameters_size == 0 )
5441 {
5442 *exponent = 65537;
5443 return( PSA_SUCCESS );
5444 }
5445
5446 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5447 * support values that fit in a 32-bit integer, which is larger than
5448 * int on just about every platform anyway. */
5449 if( domain_parameters_size > sizeof( acc ) )
5450 return( PSA_ERROR_NOT_SUPPORTED );
5451 for( i = 0; i < domain_parameters_size; i++ )
5452 acc = ( acc << 8 ) | domain_parameters[i];
5453 if( acc > INT_MAX )
5454 return( PSA_ERROR_NOT_SUPPORTED );
5455 *exponent = acc;
5456 return( PSA_SUCCESS );
5457}
5458#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5459
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005460static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005461 psa_key_slot_t *slot, size_t bits,
5462 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005463{
Gilles Peskine8e338702019-07-30 20:06:31 +02005464 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465
Gilles Peskinee56e8782019-04-26 17:34:02 +02005466 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005467 return( PSA_ERROR_INVALID_ARGUMENT );
5468
Gilles Peskinee59236f2018-01-27 23:32:46 +01005469 if( key_type_is_raw_bytes( type ) )
5470 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005471 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005472 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5473 if( status != PSA_SUCCESS )
5474 return( status );
5475 status = psa_generate_random( slot->data.raw.data,
5476 slot->data.raw.bytes );
5477 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005478 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479#if defined(MBEDTLS_DES_C)
5480 if( type == PSA_KEY_TYPE_DES )
5481 psa_des_set_key_parity( slot->data.raw.data,
5482 slot->data.raw.bytes );
5483#endif /* MBEDTLS_DES_C */
5484 }
5485 else
5486
5487#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005488 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005489 {
5490 mbedtls_rsa_context *rsa;
5491 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005492 int exponent;
5493 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005494 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5495 return( PSA_ERROR_NOT_SUPPORTED );
5496 /* Accept only byte-aligned keys, for the same reasons as
5497 * in psa_import_rsa_key(). */
5498 if( bits % 8 != 0 )
5499 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005500 status = psa_read_rsa_exponent( domain_parameters,
5501 domain_parameters_size,
5502 &exponent );
5503 if( status != PSA_SUCCESS )
5504 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005505 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5506 if( rsa == NULL )
5507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5508 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5509 ret = mbedtls_rsa_gen_key( rsa,
5510 mbedtls_ctr_drbg_random,
5511 &global_data.ctr_drbg,
5512 (unsigned int) bits,
5513 exponent );
5514 if( ret != 0 )
5515 {
5516 mbedtls_rsa_free( rsa );
5517 mbedtls_free( rsa );
5518 return( mbedtls_to_psa_error( ret ) );
5519 }
5520 slot->data.rsa = rsa;
5521 }
5522 else
5523#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5524
5525#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005526 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005527 {
5528 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5529 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5530 const mbedtls_ecp_curve_info *curve_info =
5531 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5532 mbedtls_ecp_keypair *ecp;
5533 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005534 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535 return( PSA_ERROR_NOT_SUPPORTED );
5536 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5537 return( PSA_ERROR_NOT_SUPPORTED );
5538 if( curve_info->bit_size != bits )
5539 return( PSA_ERROR_INVALID_ARGUMENT );
5540 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5541 if( ecp == NULL )
5542 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5543 mbedtls_ecp_keypair_init( ecp );
5544 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5545 mbedtls_ctr_drbg_random,
5546 &global_data.ctr_drbg );
5547 if( ret != 0 )
5548 {
5549 mbedtls_ecp_keypair_free( ecp );
5550 mbedtls_free( ecp );
5551 return( mbedtls_to_psa_error( ret ) );
5552 }
5553 slot->data.ecp = ecp;
5554 }
5555 else
5556#endif /* MBEDTLS_ECP_C */
5557
5558 return( PSA_ERROR_NOT_SUPPORTED );
5559
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005560 return( PSA_SUCCESS );
5561}
Darryl Green0c6575a2018-11-07 16:05:30 +00005562
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005563psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005564 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005565{
5566 psa_status_t status;
5567 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005568 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005569
Gilles Peskine0f84d622019-09-12 19:03:13 +02005570 /* Reject any attempt to create a zero-length key so that we don't
5571 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5572 if( psa_get_key_bits( attributes ) == 0 )
5573 return( PSA_ERROR_INVALID_ARGUMENT );
5574
Gilles Peskinedf179142019-07-15 22:02:14 +02005575 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5576 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005577 if( status != PSA_SUCCESS )
5578 goto exit;
5579
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005580#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5581 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005582 {
Gilles Peskine11792082019-08-06 18:36:36 +02005583 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5584 size_t pubkey_length = 0; /* We don't support this feature yet */
5585 if( drv->key_management == NULL ||
5586 drv->key_management->p_generate == NULL )
5587 {
5588 status = PSA_ERROR_NOT_SUPPORTED;
5589 goto exit;
5590 }
5591 status = drv->key_management->p_generate(
5592 psa_get_se_driver_context( driver ),
5593 slot->data.se.slot_number, attributes,
5594 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005595 }
Gilles Peskine11792082019-08-06 18:36:36 +02005596 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005597#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005598 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005599 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005600 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005601 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005602 }
Gilles Peskine11792082019-08-06 18:36:36 +02005603
5604exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005605 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005606 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005607 if( status != PSA_SUCCESS )
5608 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005609 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005610 *handle = 0;
5611 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005612 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005613}
5614
5615
Gilles Peskinee59236f2018-01-27 23:32:46 +01005616
5617/****************************************************************/
5618/* Module setup */
5619/****************************************************************/
5620
Gilles Peskine5e769522018-11-20 21:59:56 +01005621psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5622 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5623 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5624{
5625 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5626 return( PSA_ERROR_BAD_STATE );
5627 global_data.entropy_init = entropy_init;
5628 global_data.entropy_free = entropy_free;
5629 return( PSA_SUCCESS );
5630}
5631
Gilles Peskinee59236f2018-01-27 23:32:46 +01005632void mbedtls_psa_crypto_free( void )
5633{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005634 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005635 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5636 {
5637 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005638 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005639 }
5640 /* Wipe all remaining data, including configuration.
5641 * In particular, this sets all state indicator to the value
5642 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005643 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005644#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005645 /* Unregister all secure element drivers, so that we restart from
5646 * a pristine state. */
5647 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005648#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005649}
5650
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005651#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5652/** Recover a transaction that was interrupted by a power failure.
5653 *
5654 * This function is called during initialization, before psa_crypto_init()
5655 * returns. If this function returns a failure status, the initialization
5656 * fails.
5657 */
5658static psa_status_t psa_crypto_recover_transaction(
5659 const psa_crypto_transaction_t *transaction )
5660{
5661 switch( transaction->unknown.type )
5662 {
5663 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5664 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005665 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005666 * is implemented.
5667 * https://github.com/ARMmbed/mbed-crypto/issues/218
5668 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005669 default:
5670 /* We found an unsupported transaction in the storage.
5671 * We don't know what state the storage is in. Give up. */
5672 return( PSA_ERROR_STORAGE_FAILURE );
5673 }
5674}
5675#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5676
Gilles Peskinee59236f2018-01-27 23:32:46 +01005677psa_status_t psa_crypto_init( void )
5678{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005679 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005680 const unsigned char drbg_seed[] = "PSA";
5681
Gilles Peskinec6b69072018-11-20 21:42:52 +01005682 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005683 if( global_data.initialized != 0 )
5684 return( PSA_SUCCESS );
5685
Gilles Peskine5e769522018-11-20 21:59:56 +01005686 /* Set default configuration if
5687 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5688 if( global_data.entropy_init == NULL )
5689 global_data.entropy_init = mbedtls_entropy_init;
5690 if( global_data.entropy_free == NULL )
5691 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005692
Gilles Peskinec6b69072018-11-20 21:42:52 +01005693 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005694 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005695#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5696 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5697 /* The PSA entropy injection feature depends on using NV seed as an entropy
5698 * source. Add NV seed as an entropy source for PSA entropy injection. */
5699 mbedtls_entropy_add_source( &global_data.entropy,
5700 mbedtls_nv_seed_poll, NULL,
5701 MBEDTLS_ENTROPY_BLOCK_SIZE,
5702 MBEDTLS_ENTROPY_SOURCE_STRONG );
5703#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005704 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005705 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005706 status = mbedtls_to_psa_error(
5707 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5708 mbedtls_entropy_func,
5709 &global_data.entropy,
5710 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5711 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005712 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005713 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005714
Gilles Peskine66fb1262018-12-10 16:29:04 +01005715 status = psa_initialize_key_slots( );
5716 if( status != PSA_SUCCESS )
5717 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005718
Gilles Peskine4b734222019-07-24 15:56:31 +02005719#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005720 status = psa_crypto_load_transaction( );
5721 if( status == PSA_SUCCESS )
5722 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005723 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5724 if( status != PSA_SUCCESS )
5725 goto exit;
5726 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005727 }
5728 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5729 {
5730 /* There's no transaction to complete. It's all good. */
5731 status = PSA_SUCCESS;
5732 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005733#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005734
Gilles Peskinec6b69072018-11-20 21:42:52 +01005735 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005736 global_data.initialized = 1;
5737
5738exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005739 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005740 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005741 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005742}
5743
5744#endif /* MBEDTLS_PSA_CRYPTO_C */