blob: 09254b2496bc44564cb20c39e04005307d96679c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
454#if defined(MBEDTLS_MD_C)
455 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200456#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
495 /* Allocate memory for the key */
496 raw->bytes = PSA_BITS_TO_BYTES( bits );
497 raw->data = mbedtls_calloc( 1, raw->bytes );
498 if( raw->data == NULL )
499 {
500 raw->bytes = 0;
501 return( PSA_ERROR_INSUFFICIENT_MEMORY );
502 }
503 return( PSA_SUCCESS );
504}
505
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100507/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
508 * that are not a multiple of 8) well. For example, there is only
509 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
510 * way to return the exact bit size of a key.
511 * To keep things simple, reject non-byte-aligned key sizes. */
512static psa_status_t psa_check_rsa_key_byte_aligned(
513 const mbedtls_rsa_context *rsa )
514{
515 mbedtls_mpi n;
516 psa_status_t status;
517 mbedtls_mpi_init( &n );
518 status = mbedtls_to_psa_error(
519 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
520 if( status == PSA_SUCCESS )
521 {
522 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
523 status = PSA_ERROR_NOT_SUPPORTED;
524 }
525 mbedtls_mpi_free( &n );
526 return( status );
527}
528
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000529static psa_status_t psa_import_rsa_key( psa_key_type_t type,
530 const uint8_t *data,
531 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 mbedtls_rsa_context **p_rsa )
533{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534 psa_status_t status;
535 mbedtls_pk_context pk;
536 mbedtls_rsa_context *rsa;
537 size_t bits;
538
539 mbedtls_pk_init( &pk );
540
541 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
544 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
553 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
559 rsa = mbedtls_pk_rsa( pk );
560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
569 status = psa_check_rsa_key_byte_aligned( rsa );
570
571exit:
572 /* Free the content of the pk object only on error. */
573 if( status != PSA_SUCCESS )
574 {
575 mbedtls_pk_free( &pk );
576 return( status );
577 }
578
579 /* On success, store the content of the object in the RSA context. */
580 *p_rsa = rsa;
581
582 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200583}
584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
585
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586#if defined(MBEDTLS_ECP_C)
587
588/* Import a public key given as the uncompressed representation defined by SEC1
589 * 2.3.3 as the content of an ECPoint. */
590static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
591 const uint8_t *data,
592 size_t data_length,
593 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000596 mbedtls_ecp_keypair *ecp = NULL;
597 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
598
599 *p_ecp = NULL;
600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
601 if( ecp == NULL )
602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
603 mbedtls_ecp_keypair_init( ecp );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the public value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
613 data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616
617 /* Check that the point is on the curve. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 *p_ecp = ecp;
624 return( PSA_SUCCESS );
625
626exit:
627 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair_free( ecp );
630 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
637/* Import a private key given as a byte string which is the private value
638 * in big-endian order. */
639static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
640 const uint8_t *data,
641 size_t data_length,
642 mbedtls_ecp_keypair **p_ecp )
643{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 mbedtls_ecp_keypair *ecp = NULL;
646 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
647
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200648 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
649 return( PSA_ERROR_INVALID_ARGUMENT );
650
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 *p_ecp = NULL;
652 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
653 if( ecp == NULL )
654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000655 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
657 /* Load the group. */
658 status = mbedtls_to_psa_error(
659 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662 /* Load the secret value. */
663 status = mbedtls_to_psa_error(
664 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
665 if( status != PSA_SUCCESS )
666 goto exit;
667 /* Validate the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
670 if( status != PSA_SUCCESS )
671 goto exit;
672 /* Calculate the public key from the private key. */
673 status = mbedtls_to_psa_error(
674 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
675 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678
679 *p_ecp = ecp;
680 return( PSA_SUCCESS );
681
682exit:
683 if( ecp != NULL )
684 {
685 mbedtls_ecp_keypair_free( ecp );
686 mbedtls_free( ecp );
687 }
688 return( status );
689}
690#endif /* defined(MBEDTLS_ECP_C) */
691
Gilles Peskineb46bef22019-07-30 21:32:04 +0200692
693/** Return the size of the key in the given slot, in bits.
694 *
695 * \param[in] slot A key slot.
696 *
697 * \return The key size in bits, read from the metadata in the slot.
698 */
699static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
700{
701 return( slot->attr.bits );
702}
703
704/** Calculate the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot containing a transparent key.
707 *
708 * \return The key size in bits, calculated from the key data.
709 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200710static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 size_t bits = 0; /* return 0 on an empty slot */
713
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200715 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200716#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
718 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719#endif /* defined(MBEDTLS_RSA_C) */
720#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
722 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724
725 /* We know that the size fits in psa_key_bits_t thanks to checks
726 * when the key was created. */
727 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728}
729
Gilles Peskine8e338702019-07-30 20:06:31 +0200730/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100731 * previously. This function assumes that the slot does not contain
732 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100733psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
734 const uint8_t *data,
735 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Gilles Peskine8e338702019-07-30 20:06:31 +0200739 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200742 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 if( data_length > SIZE_MAX / 8 )
744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750 &slot->data.raw );
751 if( status != PSA_SUCCESS )
752 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200753 if( data_length != 0 )
754 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 }
756 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100757#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200758 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100759 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761 data, data_length,
762 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000766 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000768 data, data_length,
769 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000777 data, data_length,
778 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 }
780 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
783 return( PSA_ERROR_NOT_SUPPORTED );
784 }
Darryl Green940d72c2018-07-13 13:18:51 +0100785
Gilles Peskineb46bef22019-07-30 21:32:04 +0200786 if( status == PSA_SUCCESS )
787 {
788 /* Write the actual key size to the slot.
789 * psa_start_key_creation() wrote the size declared by the
790 * caller, which may be 0 (meaning unspecified) or wrong. */
791 slot->attr.bits = psa_calculate_key_bits( slot );
792 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100793 return( status );
794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
801 psa_algorithm_t alg1,
802 psa_algorithm_t alg2 )
803{
Gilles Peskine549ea862019-05-22 11:45:59 +0200804 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 if( alg1 == alg2 )
806 return( alg1 );
807 /* If the policies are from the same hash-and-sign family, check
808 * if one is a wildcard. If so the other has the specific algorithm. */
809 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
810 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
811 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
812 {
813 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
814 return( alg2 );
815 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
816 return( alg1 );
817 }
818 /* If the policies are incompatible, allow nothing. */
819 return( 0 );
820}
821
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
823 psa_algorithm_t requested_alg )
824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826 if( requested_alg == policy_alg )
827 return( 1 );
828 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 * and requested_alg is the same hash-and-sign family with any hash,
830 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200831 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
832 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
833 {
834 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
835 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
836 }
837 /* If it isn't permitted, it's forbidden. */
838 return( 0 );
839}
840
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841/** Test whether a policy permits an algorithm.
842 *
843 * The caller must test usage flags separately.
844 */
845static int psa_key_policy_permits( const psa_key_policy_t *policy,
846 psa_algorithm_t alg )
847{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200848 return( psa_key_algorithm_permits( policy->alg, alg ) ||
849 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100850}
851
Gilles Peskinef603c712019-01-19 13:40:11 +0100852/** Restrict a key policy based on a constraint.
853 *
854 * \param[in,out] policy The policy to restrict.
855 * \param[in] constraint The policy constraint to apply.
856 *
857 * \retval #PSA_SUCCESS
858 * \c *policy contains the intersection of the original value of
859 * \c *policy and \c *constraint.
860 * \retval #PSA_ERROR_INVALID_ARGUMENT
861 * \c *policy and \c *constraint are incompatible.
862 * \c *policy is unchanged.
863 */
864static psa_status_t psa_restrict_key_policy(
865 psa_key_policy_t *policy,
866 const psa_key_policy_t *constraint )
867{
868 psa_algorithm_t intersection_alg =
869 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 psa_algorithm_t intersection_alg2 =
871 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100872 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 policy->usage &= constraint->usage;
877 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 return( PSA_SUCCESS );
880}
881
Darryl Green06fd18d2018-07-16 11:21:11 +0100882/** Retrieve a slot which must contain a key. The key must have allow all the
883 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
884 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100885static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100886 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100887 psa_key_usage_t usage,
888 psa_algorithm_t alg )
889{
890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100891 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100892
893 *p_slot = NULL;
894
Gilles Peskinec5487a82018-12-03 18:08:14 +0100895 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100896 if( status != PSA_SUCCESS )
897 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 /* Enforce that usage policy for the key slot contains all the flags
900 * required by the usage parameter. There is one exception: public
901 * keys can always be exported, so we treat public key objects as
902 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200903 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200905 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100906 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907
908 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
911
912 *p_slot = slot;
913 return( PSA_SUCCESS );
914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916/** Retrieve a slot which must contain a transparent key.
917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200921 * This is a temporary function to use instead of psa_get_key_from_slot()
922 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923 */
924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
925static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
926 psa_key_slot_t **p_slot,
927 psa_key_usage_t usage,
928 psa_algorithm_t alg )
929{
930 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200933 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 {
935 *p_slot = NULL;
936 return( PSA_ERROR_NOT_SUPPORTED );
937 }
938 return( PSA_SUCCESS );
939}
940#else /* MBEDTLS_PSA_CRYPTO_SE_C */
941/* With no secure element support, all keys are transparent. */
942#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
943 psa_get_key_from_slot( handle, p_slot, usage, alg )
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
945
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100946/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000948{
Gilles Peskine73167e12019-07-12 23:44:37 +0200949#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
950 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000951 {
952 /* No key material to clean. */
953 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200954 else
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200956 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000957 {
958 /* No key material to clean. */
959 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 mbedtls_free( slot->data.raw.data );
963 }
964 else
965#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_rsa_free( slot->data.rsa );
969 mbedtls_free( slot->data.rsa );
970 }
971 else
972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_ecp_keypair_free( slot->data.ecp );
977 mbedtls_free( slot->data.ecp );
978 }
979 else
980#endif /* defined(MBEDTLS_ECP_C) */
981 {
982 /* Shouldn't happen: the key type is not any type that we
983 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200984 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000985 }
986
987 return( PSA_SUCCESS );
988}
989
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990/** Completely wipe a slot in memory, including its policy.
991 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100992psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100993{
994 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200995 /* Multipart operations may still be using the key. This is safe
996 * because all multipart operation objects are independent from
997 * the key slot: if they need to access the key after the setup
998 * phase, they have a copy of the key. Note that this means that
999 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000 /* At this point, key material and other type-specific content has
1001 * been wiped. Clear remaining metadata. We can call memset and not
1002 * zeroize because the metadata is not particularly sensitive. */
1003 memset( slot, 0, sizeof( *slot ) );
1004 return( status );
1005}
1006
Gilles Peskinec5487a82018-12-03 18:08:14 +01001007psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001009 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001010 psa_status_t status; /* status of the last operation */
1011 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 psa_se_drv_table_entry_t *driver;
1014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 if( status != PSA_SUCCESS )
1018 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001019
1020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001021 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001022 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001023 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001024 /* For a key in a secure element, we need to do three things:
1025 * remove the key file in internal storage, destroy the
1026 * key inside the secure element, and update the driver's
1027 * persistent data. Start a transaction that will encompass these
1028 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001029 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 status = psa_crypto_save_transaction( );
1034 if( status != PSA_SUCCESS )
1035 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001036 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001037 /* We should still try to destroy the key in the secure
1038 * element and the key metadata in storage. This is especially
1039 * important if the error is that the storage is full.
1040 * But how to do it exactly without risking an inconsistent
1041 * state after a reset?
1042 * https://github.com/ARMmbed/mbed-crypto/issues/215
1043 */
1044 overall_status = status;
1045 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 }
1047
Gilles Peskine354f7672019-07-12 23:46:38 +02001048 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001049 if( overall_status == PSA_SUCCESS )
1050 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001055 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001057 status = psa_destroy_persistent_key( slot->attr.id );
1058 if( overall_status == PSA_SUCCESS )
1059 overall_status = status;
1060
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001061 /* TODO: other slots may have a copy of the same key. We should
1062 * invalidate them.
1063 * https://github.com/ARMmbed/mbed-crypto/issues/214
1064 */
Darryl Greend49a4992018-06-18 17:27:26 +01001065 }
1066#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001067
Gilles Peskinefc762652019-07-22 19:30:34 +02001068#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1069 if( driver != NULL )
1070 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001071 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074 status = psa_crypto_stop_transaction( );
1075 if( overall_status == PSA_SUCCESS )
1076 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001077 }
1078#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1079
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1081exit:
1082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001084 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
1087 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088}
1089
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001091{
Gilles Peskineb699f072019-04-26 16:06:02 +02001092 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001093 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001094}
1095
Gilles Peskineb699f072019-04-26 16:06:02 +02001096psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1097 psa_key_type_t type,
1098 const uint8_t *data,
1099 size_t data_length )
1100{
1101 uint8_t *copy = NULL;
1102
1103 if( data_length != 0 )
1104 {
1105 copy = mbedtls_calloc( 1, data_length );
1106 if( copy == NULL )
1107 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1108 memcpy( copy, data, data_length );
1109 }
1110 /* After this point, this function is guaranteed to succeed, so it
1111 * can start modifying `*attributes`. */
1112
1113 if( attributes->domain_parameters != NULL )
1114 {
1115 mbedtls_free( attributes->domain_parameters );
1116 attributes->domain_parameters = NULL;
1117 attributes->domain_parameters_size = 0;
1118 }
1119
1120 attributes->domain_parameters = copy;
1121 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001122 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001123 return( PSA_SUCCESS );
1124}
1125
1126psa_status_t psa_get_key_domain_parameters(
1127 const psa_key_attributes_t *attributes,
1128 uint8_t *data, size_t data_size, size_t *data_length )
1129{
1130 if( attributes->domain_parameters_size > data_size )
1131 return( PSA_ERROR_BUFFER_TOO_SMALL );
1132 *data_length = attributes->domain_parameters_size;
1133 if( attributes->domain_parameters_size != 0 )
1134 memcpy( data, attributes->domain_parameters,
1135 attributes->domain_parameters_size );
1136 return( PSA_SUCCESS );
1137}
1138
1139#if defined(MBEDTLS_RSA_C)
1140static psa_status_t psa_get_rsa_public_exponent(
1141 const mbedtls_rsa_context *rsa,
1142 psa_key_attributes_t *attributes )
1143{
1144 mbedtls_mpi mpi;
1145 int ret;
1146 uint8_t *buffer = NULL;
1147 size_t buflen;
1148 mbedtls_mpi_init( &mpi );
1149
1150 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1151 if( ret != 0 )
1152 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001153 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1154 {
1155 /* It's the default value, which is reported as an empty string,
1156 * so there's nothing to do. */
1157 goto exit;
1158 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001159
1160 buflen = mbedtls_mpi_size( &mpi );
1161 buffer = mbedtls_calloc( 1, buflen );
1162 if( buffer == NULL )
1163 {
1164 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1165 goto exit;
1166 }
1167 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1168 if( ret != 0 )
1169 goto exit;
1170 attributes->domain_parameters = buffer;
1171 attributes->domain_parameters_size = buflen;
1172
1173exit:
1174 mbedtls_mpi_free( &mpi );
1175 if( ret != 0 )
1176 mbedtls_free( buffer );
1177 return( mbedtls_to_psa_error( ret ) );
1178}
1179#endif /* MBEDTLS_RSA_C */
1180
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001181/** Retrieve all the publicly-accessible attributes of a key.
1182 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1184 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001187 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189 psa_reset_key_attributes( attributes );
1190
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001192 if( status != PSA_SUCCESS )
1193 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001194
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001195 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001196 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1197 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1198
1199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1200 if( psa_key_slot_is_external( slot ) )
1201 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
Gilles Peskine8e338702019-07-30 20:06:31 +02001204 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 {
1206#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001207 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001210 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001211 * is not yet implemented.
1212 * https://github.com/ARMmbed/mbed-crypto/issues/216
1213 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001214 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001215 break;
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1218 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001219#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 default:
1221 /* Nothing else to do. */
1222 break;
1223 }
1224
1225 if( status != PSA_SUCCESS )
1226 psa_reset_key_attributes( attributes );
1227 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228}
1229
Gilles Peskinec8000c02019-08-02 20:15:51 +02001230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1231psa_status_t psa_get_key_slot_number(
1232 const psa_key_attributes_t *attributes,
1233 psa_key_slot_number_t *slot_number )
1234{
1235 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1236 {
1237 *slot_number = attributes->slot_number;
1238 return( PSA_SUCCESS );
1239 }
1240 else
1241 return( PSA_ERROR_INVALID_ARGUMENT );
1242}
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1244
Jaeden Ameroccdce902019-01-10 11:42:27 +00001245#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001246static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1247 unsigned char *buf, size_t size )
1248{
1249 int ret;
1250 unsigned char *c;
1251 size_t len = 0;
1252
1253 c = buf + size;
1254
1255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1256
1257 return( (int) len );
1258}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001260
Gilles Peskinef603c712019-01-19 13:40:11 +01001261static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1262 uint8_t *data,
1263 size_t data_size,
1264 size_t *data_length,
1265 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266{
Gilles Peskine5d309672019-07-12 23:47:28 +02001267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1268 const psa_drv_se_t *drv;
1269 psa_drv_se_context_t *drv_context;
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1271
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001272 *data_length = 0;
1273
Gilles Peskine8e338702019-07-30 20:06:31 +02001274 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001275 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001276
Gilles Peskinef9168942019-09-12 19:20:29 +02001277 /* Reject a zero-length output buffer now, since this can never be a
1278 * valid key representation. This way we know that data must be a valid
1279 * pointer and we can do things like memset(data, ..., data_size). */
1280 if( data_size == 0 )
1281 return( PSA_ERROR_BUFFER_TOO_SMALL );
1282
Gilles Peskine5d309672019-07-12 23:47:28 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001285 {
1286 psa_drv_se_export_key_t method;
1287 if( drv->key_management == NULL )
1288 return( PSA_ERROR_NOT_SUPPORTED );
1289 method = ( export_public_key ?
1290 drv->key_management->p_export_public :
1291 drv->key_management->p_export );
1292 if( method == NULL )
1293 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001294 return( method( drv_context,
1295 slot->data.se.slot_number,
1296 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001297 }
1298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1299
Gilles Peskine8e338702019-07-30 20:06:31 +02001300 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001301 {
1302 if( slot->data.raw.bytes > data_size )
1303 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001304 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1305 memset( data + slot->data.raw.bytes, 0,
1306 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 *data_length = slot->data.raw.bytes;
1308 return( PSA_SUCCESS );
1309 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001310#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001312 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001313 psa_status_t status;
1314
Gilles Peskineb46bef22019-07-30 21:32:04 +02001315 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001316 if( bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
1318 status = mbedtls_to_psa_error(
1319 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1320 if( status != PSA_SUCCESS )
1321 return( status );
1322 memset( data + bytes, 0, data_size - bytes );
1323 *data_length = bytes;
1324 return( PSA_SUCCESS );
1325 }
1326#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 else
Moran Peker17e36e12018-05-02 12:55:20 +03001328 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001329#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001330 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1331 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001332 {
Moran Pekera998bc62018-04-16 18:16:20 +03001333 mbedtls_pk_context pk;
1334 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001335 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001336 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001337#if defined(MBEDTLS_RSA_C)
1338 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001339 pk.pk_info = &mbedtls_rsa_info;
1340 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001341#else
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001344 }
1345 else
1346 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001347#if defined(MBEDTLS_ECP_C)
1348 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001349 pk.pk_info = &mbedtls_eckey_info;
1350 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#else
1352 return( PSA_ERROR_NOT_SUPPORTED );
1353#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001354 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001355 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001356 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001357 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001358 }
Moran Peker17e36e12018-05-02 12:55:20 +03001359 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001360 {
Moran Peker17e36e12018-05-02 12:55:20 +03001361 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 }
Moran Peker60364322018-04-29 11:34:58 +03001363 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001364 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001365 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001366 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001367 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001368 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1369 * Move the data to the beginning and erase remaining data
1370 * at the original location. */
1371 if( 2 * (size_t) ret <= data_size )
1372 {
1373 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001374 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001375 }
1376 else if( (size_t) ret < data_size )
1377 {
1378 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 }
Moran Pekera998bc62018-04-16 18:16:20 +03001381 *data_length = ret;
1382 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001383 }
1384 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001385#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001386 {
1387 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001388 it is valid for a special-purpose implementation to omit
1389 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001390 return( PSA_ERROR_NOT_SUPPORTED );
1391 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001392 }
1393}
1394
Gilles Peskinec5487a82018-12-03 18:08:14 +01001395psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001396 uint8_t *data,
1397 size_t data_size,
1398 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001400 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001401 psa_status_t status;
1402
1403 /* Set the key to empty now, so that even when there are errors, we always
1404 * set data_length to a value between 0 and data_size. On error, setting
1405 * the key to empty is a good choice because an empty key representation is
1406 * unlikely to be accepted anywhere. */
1407 *data_length = 0;
1408
1409 /* Export requires the EXPORT flag. There is an exception for public keys,
1410 * which don't require any flag, but psa_get_key_from_slot takes
1411 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001412 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001413 if( status != PSA_SUCCESS )
1414 return( status );
1415 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001416 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001417}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418
Gilles Peskinec5487a82018-12-03 18:08:14 +01001419psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001420 uint8_t *data,
1421 size_t data_size,
1422 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001423{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001424 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001425 psa_status_t status;
1426
1427 /* Set the key to empty now, so that even when there are errors, we always
1428 * set data_length to a value between 0 and data_size. On error, setting
1429 * the key to empty is a good choice because an empty key representation is
1430 * unlikely to be accepted anywhere. */
1431 *data_length = 0;
1432
1433 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001434 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001435 if( status != PSA_SUCCESS )
1436 return( status );
1437 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001438 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001439}
1440
Gilles Peskine91e8c332019-08-02 19:19:39 +02001441#if defined(static_assert)
1442static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1443 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001444static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001445 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001446static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001447 "One or more key attribute flag is listed as both internal-only and external-only" );
1448#endif
1449
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001450/** Validate that a key policy is internally well-formed.
1451 *
1452 * This function only rejects invalid policies. It does not validate the
1453 * consistency of the policy with respect to other attributes of the key
1454 * such as the key type.
1455 */
1456static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001457{
1458 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001459 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 PSA_KEY_USAGE_ENCRYPT |
1461 PSA_KEY_USAGE_DECRYPT |
1462 PSA_KEY_USAGE_SIGN |
1463 PSA_KEY_USAGE_VERIFY |
1464 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1465 return( PSA_ERROR_INVALID_ARGUMENT );
1466
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 return( PSA_SUCCESS );
1468}
1469
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001470/** Validate the internal consistency of key attributes.
1471 *
1472 * This function only rejects invalid attribute values. If does not
1473 * validate the consistency of the attributes with any key data that may
1474 * be involved in the creation of the key.
1475 *
1476 * Call this function early in the key creation process.
1477 *
1478 * \param[in] attributes Key attributes for the new key.
1479 * \param[out] p_drv On any return, the driver for the key, if any.
1480 * NULL for a transparent key.
1481 *
1482 */
1483static psa_status_t psa_validate_key_attributes(
1484 const psa_key_attributes_t *attributes,
1485 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001486{
1487 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001488
1489 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001490 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001491 status = psa_validate_persistent_key_parameters(
1492 attributes->core.lifetime, attributes->core.id,
1493 p_drv, 1 );
1494 if( status != PSA_SUCCESS )
1495 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001496 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001497
1498 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500 return( status );
1501
1502 /* Refuse to create overly large keys.
1503 * Note that this doesn't trigger on import if the attributes don't
1504 * explicitly specify a size (so psa_get_key_bits returns 0), so
1505 * psa_import_key() needs its own checks. */
1506 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1507 return( PSA_ERROR_NOT_SUPPORTED );
1508
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509 /* Reject invalid flags. These should not be reachable through the API. */
1510 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1511 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1512 return( PSA_ERROR_INVALID_ARGUMENT );
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( PSA_SUCCESS );
1515}
1516
Gilles Peskine4747d192019-04-17 15:05:45 +02001517/** Prepare a key slot to receive key material.
1518 *
1519 * This function allocates a key slot and sets its metadata.
1520 *
1521 * If this function fails, call psa_fail_key_creation().
1522 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001523 * This function is intended to be used as follows:
1524 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1525 * it with the specified attributes, and assign it a handle.
1526 * -# Populate the slot with the key material.
1527 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1528 * In case of failure at any step, stop the sequence and call
1529 * psa_fail_key_creation().
1530 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001531 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001532 * \param[in] attributes Key attributes for the new key.
1533 * \param[out] handle On success, a handle for the allocated slot.
1534 * \param[out] p_slot On success, a pointer to the prepared slot.
1535 * \param[out] p_drv On any return, the driver for the key, if any.
1536 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The key slot is ready to receive key material.
1540 * \return If this function fails, the key slot is an invalid state.
1541 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 */
1543static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001544 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 const psa_key_attributes_t *attributes,
1546 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001548 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001549{
1550 psa_status_t status;
1551 psa_key_slot_t *slot;
1552
Gilles Peskinedf179142019-07-15 22:02:14 +02001553 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 *p_drv = NULL;
1555
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001556 status = psa_validate_key_attributes( attributes, p_drv );
1557 if( status != PSA_SUCCESS )
1558 return( status );
1559
Gilles Peskineedbed562019-08-07 18:19:59 +02001560 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 if( status != PSA_SUCCESS )
1562 return( status );
1563 slot = *p_slot;
1564
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001565 /* We're storing the declared bit-size of the key. It's up to each
1566 * creation mechanism to verify that this information is correct.
1567 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001568 * an input (generate, device) but not for those where the bit-size
1569 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001570
1571 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001572
Gilles Peskine91e8c332019-08-02 19:19:39 +02001573 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001574 * external-only flags, query `attributes`. Thanks to the check
1575 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001576 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001577 * may have set. */
1578 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001579
Gilles Peskinecbaff462019-07-12 23:46:04 +02001580#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001581 /* For a key in a secure element, we need to do three things
1582 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001583 * create the key file in internal storage, create the
1584 * key inside the secure element, and update the driver's
1585 * persistent data. Start a transaction that will encompass these
1586 * three actions. */
1587 /* The first thing to do is to find a slot number for the new key.
1588 * We save the slot number in persistent storage as part of the
1589 * transaction data. It will be needed to recover if the power
1590 * fails during the key creation process, to clean up on the secure
1591 * element side after restarting. Obtaining a slot number from the
1592 * secure element driver updates its persistent state, but we do not yet
1593 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001594 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001595 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Darryl Green0c6575a2018-11-07 16:05:30 +00001596 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001597 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001598 &slot->data.se.slot_number );
1599 if( status != PSA_SUCCESS )
1600 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001601 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001602 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001603 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001604 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001605 status = psa_crypto_save_transaction( );
1606 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001607 {
1608 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001609 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1613
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 return( status );
1615}
Gilles Peskine4747d192019-04-17 15:05:45 +02001616
1617/** Finalize the creation of a key once its key material has been set.
1618 *
1619 * This entails writing the key to persistent storage.
1620 *
1621 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001622 * See the documentation of psa_start_key_creation() for the intended use
1623 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001625 * \param[in,out] slot Pointer to the slot with key material.
1626 * \param[in] driver The secure element driver for the key,
1627 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001628 *
1629 * \retval #PSA_SUCCESS
1630 * The key was successfully created. The handle is now valid.
1631 * \return If this function fails, the key slot is an invalid state.
1632 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001634static psa_status_t psa_finish_key_creation(
1635 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001636 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001637{
1638 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001639 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001641
1642#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001643 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1646 if( driver != NULL )
1647 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001648 psa_se_key_data_storage_t data;
1649#if defined(static_assert)
1650 static_assert( sizeof( slot->data.se.slot_number ) ==
1651 sizeof( data.slot_number ),
1652 "Slot number size does not match psa_se_key_data_storage_t" );
1653 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1654 "Bit-size size does not match psa_se_key_data_storage_t" );
1655#endif
1656 memcpy( &data.slot_number, &slot->data.se.slot_number,
1657 sizeof( slot->data.se.slot_number ) );
1658 memcpy( &data.bits, &slot->attr.bits,
1659 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001660 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001661 (uint8_t*) &data,
1662 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001663 }
1664 else
1665#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1666 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001667 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001668 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001669 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001670 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1671 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001672 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001673 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1674 status = psa_internal_export_key( slot,
1675 buffer, buffer_size, &length,
1676 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001677 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001678 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001679 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001680
Gilles Peskinef9168942019-09-12 19:20:29 +02001681 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001682 mbedtls_free( buffer );
1683 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001685#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1686
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001688 /* Finish the transaction for a key creation. This does not
1689 * happen when registering an existing key. Detect this case
1690 * by checking whether a transaction is in progress (actual
1691 * creation of a key in a secure element requires a transaction,
1692 * but registration doesn't use one). */
1693 if( driver != NULL &&
1694 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001695 {
1696 status = psa_save_se_persistent_data( driver );
1697 if( status != PSA_SUCCESS )
1698 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001699 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001700 return( status );
1701 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001702 status = psa_crypto_stop_transaction( );
1703 if( status != PSA_SUCCESS )
1704 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001705 }
1706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1707
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 return( status );
1709}
1710
1711/** Abort the creation of a key.
1712 *
1713 * You may call this function after calling psa_start_key_creation(),
1714 * or after psa_finish_key_creation() fails. In other circumstances, this
1715 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001716 * See the documentation of psa_start_key_creation() for the intended use
1717 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001719 * \param[in,out] slot Pointer to the slot with key material.
1720 * \param[in] driver The secure element driver for the key,
1721 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001723static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001724 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001725{
Gilles Peskine011e4282019-06-26 18:34:38 +02001726 (void) driver;
1727
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 if( slot == NULL )
1729 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001730
1731#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001732 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001733 * element, and the failure happened later (when saving metadata
1734 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001735 * element.
1736 * https://github.com/ARMmbed/mbed-crypto/issues/217
1737 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001738
Gilles Peskined7729582019-08-05 15:55:54 +02001739 /* Abort the ongoing transaction if any (there may not be one if
1740 * the creation process failed before starting one, or if the
1741 * key creation is a registration of a key in a secure element).
1742 * Earlier functions must already have done what it takes to undo any
1743 * partial creation. All that's left is to update the transaction data
1744 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001745 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 psa_wipe_key_slot( slot );
1749}
1750
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001751/** Validate optional attributes during key creation.
1752 *
1753 * Some key attributes are optional during key creation. If they are
1754 * specified in the attributes structure, check that they are consistent
1755 * with the data in the slot.
1756 *
1757 * This function should be called near the end of key creation, after
1758 * the slot in memory is fully populated but before saving persistent data.
1759 */
1760static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001761 const psa_key_slot_t *slot,
1762 const psa_key_attributes_t *attributes )
1763{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001764 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001765 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001766 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001767 return( PSA_ERROR_INVALID_ARGUMENT );
1768 }
1769
1770 if( attributes->domain_parameters_size != 0 )
1771 {
1772#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001773 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001774 {
1775 mbedtls_mpi actual, required;
1776 int ret;
1777 mbedtls_mpi_init( &actual );
1778 mbedtls_mpi_init( &required );
1779 ret = mbedtls_rsa_export( slot->data.rsa,
1780 NULL, NULL, NULL, NULL, &actual );
1781 if( ret != 0 )
1782 goto rsa_exit;
1783 ret = mbedtls_mpi_read_binary( &required,
1784 attributes->domain_parameters,
1785 attributes->domain_parameters_size );
1786 if( ret != 0 )
1787 goto rsa_exit;
1788 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1789 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1790 rsa_exit:
1791 mbedtls_mpi_free( &actual );
1792 mbedtls_mpi_free( &required );
1793 if( ret != 0)
1794 return( mbedtls_to_psa_error( ret ) );
1795 }
1796 else
1797#endif
1798 {
1799 return( PSA_ERROR_INVALID_ARGUMENT );
1800 }
1801 }
1802
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001803 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001805 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001806 return( PSA_ERROR_INVALID_ARGUMENT );
1807 }
1808
1809 return( PSA_SUCCESS );
1810}
1811
Gilles Peskine4747d192019-04-17 15:05:45 +02001812psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001813 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001814 size_t data_length,
1815 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001816{
1817 psa_status_t status;
1818 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001819 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001820
Gilles Peskine0f84d622019-09-12 19:03:13 +02001821 /* Reject zero-length symmetric keys (including raw data key objects).
1822 * This also rejects any key which might be encoded as an empty string,
1823 * which is never valid. */
1824 if( data_length == 0 )
1825 return( PSA_ERROR_INVALID_ARGUMENT );
1826
Gilles Peskinedf179142019-07-15 22:02:14 +02001827 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1828 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829 if( status != PSA_SUCCESS )
1830 goto exit;
1831
Gilles Peskine5d309672019-07-12 23:47:28 +02001832#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1833 if( driver != NULL )
1834 {
1835 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001836 /* The driver should set the number of key bits, however in
1837 * case it doesn't, we initialize bits to an invalid value. */
1838 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001839 if( drv->key_management == NULL ||
1840 drv->key_management->p_import == NULL )
1841 {
1842 status = PSA_ERROR_NOT_SUPPORTED;
1843 goto exit;
1844 }
1845 status = drv->key_management->p_import(
1846 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001847 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001848 &bits );
1849 if( status != PSA_SUCCESS )
1850 goto exit;
1851 if( bits > PSA_MAX_KEY_BITS )
1852 {
1853 status = PSA_ERROR_NOT_SUPPORTED;
1854 goto exit;
1855 }
1856 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001857 }
1858 else
1859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860 {
1861 status = psa_import_key_into_slot( slot, data, data_length );
1862 if( status != PSA_SUCCESS )
1863 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001864 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001865 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001866 if( status != PSA_SUCCESS )
1867 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 if( status != PSA_SUCCESS )
1872 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001873 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 *handle = 0;
1875 }
1876 return( status );
1877}
1878
Gilles Peskined7729582019-08-05 15:55:54 +02001879#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1880psa_status_t mbedtls_psa_register_se_key(
1881 const psa_key_attributes_t *attributes )
1882{
1883 psa_status_t status;
1884 psa_key_slot_t *slot = NULL;
1885 psa_se_drv_table_entry_t *driver = NULL;
1886 const psa_drv_se_t *drv;
1887 psa_key_handle_t handle = 0;
1888
1889 /* Leaving attributes unspecified is not currently supported.
1890 * It could make sense to query the key type and size from the
1891 * secure element, but not all secure elements support this
1892 * and the driver HAL doesn't currently support it. */
1893 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1894 return( PSA_ERROR_NOT_SUPPORTED );
1895 if( psa_get_key_bits( attributes ) == 0 )
1896 return( PSA_ERROR_NOT_SUPPORTED );
1897
1898 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1899 &handle, &slot, &driver );
1900 if( status != PSA_SUCCESS )
1901 goto exit;
1902
1903 if( driver == NULL )
1904 {
1905 status = PSA_ERROR_INVALID_ARGUMENT;
1906 goto exit;
1907 }
1908 drv = psa_get_se_driver_methods( driver );
1909
1910 if ( psa_get_key_slot_number( attributes,
1911 &slot->data.se.slot_number ) != PSA_SUCCESS )
1912 {
1913 /* The application didn't specify a slot number. This doesn't
1914 * make sense when registering a slot. */
1915 status = PSA_ERROR_INVALID_ARGUMENT;
1916 goto exit;
1917 }
1918
Gilles Peskinea5f87492019-08-05 16:46:18 +02001919 /* If the driver has a slot number validation method, call it.
1920 * If it doesn't, it means the secure element is unable to validate
1921 * anything and so we have to trust the application. */
1922 if( drv->key_management != NULL &&
1923 drv->key_management->p_validate_slot_number != NULL )
1924 {
1925 status = drv->key_management->p_validate_slot_number(
1926 psa_get_se_driver_context( driver ),
1927 attributes,
1928 PSA_KEY_CREATION_REGISTER,
1929 slot->data.se.slot_number );
1930 if( status != PSA_SUCCESS )
1931 goto exit;
1932 }
1933
Gilles Peskined7729582019-08-05 15:55:54 +02001934 status = psa_finish_key_creation( slot, driver );
1935
1936exit:
1937 if( status != PSA_SUCCESS )
1938 {
1939 psa_fail_key_creation( slot, driver );
1940 }
1941 /* Registration doesn't keep the key in RAM. */
1942 psa_close_key( handle );
1943 return( status );
1944}
1945#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1946
Gilles Peskinef603c712019-01-19 13:40:11 +01001947static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001948 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001949{
1950 psa_status_t status;
1951 uint8_t *buffer = NULL;
1952 size_t buffer_size = 0;
1953 size_t length;
1954
Gilles Peskine8e338702019-07-30 20:06:31 +02001955 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001956 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001957 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001958 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001959 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001960 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1961 if( status != PSA_SUCCESS )
1962 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001963 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001964 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001965
1966exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001967 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001968 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001969 return( status );
1970}
1971
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001972psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1973 const psa_key_attributes_t *specified_attributes,
1974 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001975{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001976 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001977 psa_key_slot_t *source_slot = NULL;
1978 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001980 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001981
Gilles Peskine28f8f302019-07-24 13:30:31 +02001982 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001983 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001984 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 goto exit;
1986
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001987 status = psa_validate_optional_attributes( source_slot,
1988 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 if( status != PSA_SUCCESS )
1990 goto exit;
1991
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001992 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001993 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 if( status != PSA_SUCCESS )
1995 goto exit;
1996
Gilles Peskinedf179142019-07-15 22:02:14 +02001997 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1998 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001999 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 if( status != PSA_SUCCESS )
2001 goto exit;
2002
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002003#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2004 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002005 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002006 /* Copying to a secure element is not implemented yet. */
2007 status = PSA_ERROR_NOT_SUPPORTED;
2008 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002009 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002010#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002011
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002013 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002015
Gilles Peskine011e4282019-06-26 18:34:38 +02002016 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002017exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 if( status != PSA_SUCCESS )
2019 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002020 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021 *target_handle = 0;
2022 }
2023 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002024}
2025
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002026
2027
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002028/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002029/* Message digests */
2030/****************************************************************/
2031
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002032static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002033{
2034 switch( alg )
2035 {
2036#if defined(MBEDTLS_MD2_C)
2037 case PSA_ALG_MD2:
2038 return( &mbedtls_md2_info );
2039#endif
2040#if defined(MBEDTLS_MD4_C)
2041 case PSA_ALG_MD4:
2042 return( &mbedtls_md4_info );
2043#endif
2044#if defined(MBEDTLS_MD5_C)
2045 case PSA_ALG_MD5:
2046 return( &mbedtls_md5_info );
2047#endif
2048#if defined(MBEDTLS_RIPEMD160_C)
2049 case PSA_ALG_RIPEMD160:
2050 return( &mbedtls_ripemd160_info );
2051#endif
2052#if defined(MBEDTLS_SHA1_C)
2053 case PSA_ALG_SHA_1:
2054 return( &mbedtls_sha1_info );
2055#endif
2056#if defined(MBEDTLS_SHA256_C)
2057 case PSA_ALG_SHA_224:
2058 return( &mbedtls_sha224_info );
2059 case PSA_ALG_SHA_256:
2060 return( &mbedtls_sha256_info );
2061#endif
2062#if defined(MBEDTLS_SHA512_C)
2063 case PSA_ALG_SHA_384:
2064 return( &mbedtls_sha384_info );
2065 case PSA_ALG_SHA_512:
2066 return( &mbedtls_sha512_info );
2067#endif
2068 default:
2069 return( NULL );
2070 }
2071}
2072
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002073psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2074{
2075 switch( operation->alg )
2076 {
Gilles Peskine81736312018-06-26 15:04:31 +02002077 case 0:
2078 /* The object has (apparently) been initialized but it is not
2079 * in use. It's ok to call abort on such an object, and there's
2080 * nothing to do. */
2081 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082#if defined(MBEDTLS_MD2_C)
2083 case PSA_ALG_MD2:
2084 mbedtls_md2_free( &operation->ctx.md2 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_MD4_C)
2088 case PSA_ALG_MD4:
2089 mbedtls_md4_free( &operation->ctx.md4 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_MD5_C)
2093 case PSA_ALG_MD5:
2094 mbedtls_md5_free( &operation->ctx.md5 );
2095 break;
2096#endif
2097#if defined(MBEDTLS_RIPEMD160_C)
2098 case PSA_ALG_RIPEMD160:
2099 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2100 break;
2101#endif
2102#if defined(MBEDTLS_SHA1_C)
2103 case PSA_ALG_SHA_1:
2104 mbedtls_sha1_free( &operation->ctx.sha1 );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA256_C)
2108 case PSA_ALG_SHA_224:
2109 case PSA_ALG_SHA_256:
2110 mbedtls_sha256_free( &operation->ctx.sha256 );
2111 break;
2112#endif
2113#if defined(MBEDTLS_SHA512_C)
2114 case PSA_ALG_SHA_384:
2115 case PSA_ALG_SHA_512:
2116 mbedtls_sha512_free( &operation->ctx.sha512 );
2117 break;
2118#endif
2119 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002120 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 }
2122 operation->alg = 0;
2123 return( PSA_SUCCESS );
2124}
2125
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002126psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127 psa_algorithm_t alg )
2128{
2129 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002130
2131 /* A context must be freshly initialized before it can be set up. */
2132 if( operation->alg != 0 )
2133 {
2134 return( PSA_ERROR_BAD_STATE );
2135 }
2136
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002137 switch( alg )
2138 {
2139#if defined(MBEDTLS_MD2_C)
2140 case PSA_ALG_MD2:
2141 mbedtls_md2_init( &operation->ctx.md2 );
2142 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_MD4_C)
2146 case PSA_ALG_MD4:
2147 mbedtls_md4_init( &operation->ctx.md4 );
2148 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_MD5_C)
2152 case PSA_ALG_MD5:
2153 mbedtls_md5_init( &operation->ctx.md5 );
2154 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2155 break;
2156#endif
2157#if defined(MBEDTLS_RIPEMD160_C)
2158 case PSA_ALG_RIPEMD160:
2159 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2160 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA1_C)
2164 case PSA_ALG_SHA_1:
2165 mbedtls_sha1_init( &operation->ctx.sha1 );
2166 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2167 break;
2168#endif
2169#if defined(MBEDTLS_SHA256_C)
2170 case PSA_ALG_SHA_224:
2171 mbedtls_sha256_init( &operation->ctx.sha256 );
2172 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2173 break;
2174 case PSA_ALG_SHA_256:
2175 mbedtls_sha256_init( &operation->ctx.sha256 );
2176 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2177 break;
2178#endif
2179#if defined(MBEDTLS_SHA512_C)
2180 case PSA_ALG_SHA_384:
2181 mbedtls_sha512_init( &operation->ctx.sha512 );
2182 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2183 break;
2184 case PSA_ALG_SHA_512:
2185 mbedtls_sha512_init( &operation->ctx.sha512 );
2186 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2187 break;
2188#endif
2189 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002190 return( PSA_ALG_IS_HASH( alg ) ?
2191 PSA_ERROR_NOT_SUPPORTED :
2192 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 }
2194 if( ret == 0 )
2195 operation->alg = alg;
2196 else
2197 psa_hash_abort( operation );
2198 return( mbedtls_to_psa_error( ret ) );
2199}
2200
2201psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2202 const uint8_t *input,
2203 size_t input_length )
2204{
2205 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002206
2207 /* Don't require hash implementations to behave correctly on a
2208 * zero-length input, which may have an invalid pointer. */
2209 if( input_length == 0 )
2210 return( PSA_SUCCESS );
2211
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212 switch( operation->alg )
2213 {
2214#if defined(MBEDTLS_MD2_C)
2215 case PSA_ALG_MD2:
2216 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2217 input, input_length );
2218 break;
2219#endif
2220#if defined(MBEDTLS_MD4_C)
2221 case PSA_ALG_MD4:
2222 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2223 input, input_length );
2224 break;
2225#endif
2226#if defined(MBEDTLS_MD5_C)
2227 case PSA_ALG_MD5:
2228 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2229 input, input_length );
2230 break;
2231#endif
2232#if defined(MBEDTLS_RIPEMD160_C)
2233 case PSA_ALG_RIPEMD160:
2234 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2235 input, input_length );
2236 break;
2237#endif
2238#if defined(MBEDTLS_SHA1_C)
2239 case PSA_ALG_SHA_1:
2240 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2241 input, input_length );
2242 break;
2243#endif
2244#if defined(MBEDTLS_SHA256_C)
2245 case PSA_ALG_SHA_224:
2246 case PSA_ALG_SHA_256:
2247 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2248 input, input_length );
2249 break;
2250#endif
2251#if defined(MBEDTLS_SHA512_C)
2252 case PSA_ALG_SHA_384:
2253 case PSA_ALG_SHA_512:
2254 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2255 input, input_length );
2256 break;
2257#endif
2258 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002259 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002261
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 if( ret != 0 )
2263 psa_hash_abort( operation );
2264 return( mbedtls_to_psa_error( ret ) );
2265}
2266
2267psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2268 uint8_t *hash,
2269 size_t hash_size,
2270 size_t *hash_length )
2271{
itayzafrir40835d42018-08-02 13:14:17 +03002272 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002274 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275
2276 /* Fill the output buffer with something that isn't a valid hash
2277 * (barring an attack on the hash and deliberately-crafted input),
2278 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002279 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002280 /* If hash_size is 0 then hash may be NULL and then the
2281 * call to memset would have undefined behavior. */
2282 if( hash_size != 0 )
2283 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284
2285 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002286 {
2287 status = PSA_ERROR_BUFFER_TOO_SMALL;
2288 goto exit;
2289 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290
2291 switch( operation->alg )
2292 {
2293#if defined(MBEDTLS_MD2_C)
2294 case PSA_ALG_MD2:
2295 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_MD4_C)
2299 case PSA_ALG_MD4:
2300 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_MD5_C)
2304 case PSA_ALG_MD5:
2305 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_RIPEMD160_C)
2309 case PSA_ALG_RIPEMD160:
2310 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2311 break;
2312#endif
2313#if defined(MBEDTLS_SHA1_C)
2314 case PSA_ALG_SHA_1:
2315 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2316 break;
2317#endif
2318#if defined(MBEDTLS_SHA256_C)
2319 case PSA_ALG_SHA_224:
2320 case PSA_ALG_SHA_256:
2321 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA512_C)
2325 case PSA_ALG_SHA_384:
2326 case PSA_ALG_SHA_512:
2327 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2328 break;
2329#endif
2330 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002331 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 }
itayzafrir40835d42018-08-02 13:14:17 +03002333 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334
itayzafrir40835d42018-08-02 13:14:17 +03002335exit:
2336 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002338 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 return( psa_hash_abort( operation ) );
2340 }
2341 else
2342 {
2343 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002344 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 }
2346}
2347
Gilles Peskine2d277862018-06-18 15:41:12 +02002348psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2349 const uint8_t *hash,
2350 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351{
2352 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2353 size_t actual_hash_length;
2354 psa_status_t status = psa_hash_finish( operation,
2355 actual_hash, sizeof( actual_hash ),
2356 &actual_hash_length );
2357 if( status != PSA_SUCCESS )
2358 return( status );
2359 if( actual_hash_length != hash_length )
2360 return( PSA_ERROR_INVALID_SIGNATURE );
2361 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2362 return( PSA_ERROR_INVALID_SIGNATURE );
2363 return( PSA_SUCCESS );
2364}
2365
Gilles Peskineeb35d782019-01-22 17:56:16 +01002366psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2367 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002368{
2369 if( target_operation->alg != 0 )
2370 return( PSA_ERROR_BAD_STATE );
2371
2372 switch( source_operation->alg )
2373 {
2374 case 0:
2375 return( PSA_ERROR_BAD_STATE );
2376#if defined(MBEDTLS_MD2_C)
2377 case PSA_ALG_MD2:
2378 mbedtls_md2_clone( &target_operation->ctx.md2,
2379 &source_operation->ctx.md2 );
2380 break;
2381#endif
2382#if defined(MBEDTLS_MD4_C)
2383 case PSA_ALG_MD4:
2384 mbedtls_md4_clone( &target_operation->ctx.md4,
2385 &source_operation->ctx.md4 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_MD5_C)
2389 case PSA_ALG_MD5:
2390 mbedtls_md5_clone( &target_operation->ctx.md5,
2391 &source_operation->ctx.md5 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_RIPEMD160_C)
2395 case PSA_ALG_RIPEMD160:
2396 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2397 &source_operation->ctx.ripemd160 );
2398 break;
2399#endif
2400#if defined(MBEDTLS_SHA1_C)
2401 case PSA_ALG_SHA_1:
2402 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2403 &source_operation->ctx.sha1 );
2404 break;
2405#endif
2406#if defined(MBEDTLS_SHA256_C)
2407 case PSA_ALG_SHA_224:
2408 case PSA_ALG_SHA_256:
2409 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2410 &source_operation->ctx.sha256 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_SHA512_C)
2414 case PSA_ALG_SHA_384:
2415 case PSA_ALG_SHA_512:
2416 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2417 &source_operation->ctx.sha512 );
2418 break;
2419#endif
2420 default:
2421 return( PSA_ERROR_NOT_SUPPORTED );
2422 }
2423
2424 target_operation->alg = source_operation->alg;
2425 return( PSA_SUCCESS );
2426}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002427
2428
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430/* MAC */
2431/****************************************************************/
2432
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002433static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002434 psa_algorithm_t alg,
2435 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002436 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002437 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002440 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002441
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002442 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002443 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002444
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2446 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002447 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002449 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002450 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 mode = MBEDTLS_MODE_STREAM;
2452 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 case PSA_ALG_CTR:
2454 mode = MBEDTLS_MODE_CTR;
2455 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002456 case PSA_ALG_CFB:
2457 mode = MBEDTLS_MODE_CFB;
2458 break;
2459 case PSA_ALG_OFB:
2460 mode = MBEDTLS_MODE_OFB;
2461 break;
2462 case PSA_ALG_CBC_NO_PADDING:
2463 mode = MBEDTLS_MODE_CBC;
2464 break;
2465 case PSA_ALG_CBC_PKCS7:
2466 mode = MBEDTLS_MODE_CBC;
2467 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002468 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002469 mode = MBEDTLS_MODE_CCM;
2470 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002471 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 mode = MBEDTLS_MODE_GCM;
2473 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002474 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2475 mode = MBEDTLS_MODE_CHACHAPOLY;
2476 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 default:
2478 return( NULL );
2479 }
2480 }
2481 else if( alg == PSA_ALG_CMAC )
2482 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483 else
2484 return( NULL );
2485
2486 switch( key_type )
2487 {
2488 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002489 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 break;
2491 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002492 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2493 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002495 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002497 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002498 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2499 * but two-key Triple-DES is functionally three-key Triple-DES
2500 * with K1=K3, so that's how we present it to mbedtls. */
2501 if( key_bits == 128 )
2502 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 break;
2504 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002505 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 break;
2507 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002508 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002510 case PSA_KEY_TYPE_CHACHA20:
2511 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2512 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 default:
2514 return( NULL );
2515 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002516 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002517 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518
Jaeden Amero23bbb752018-06-26 14:16:54 +01002519 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2520 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521}
2522
Gilles Peskinea05219c2018-11-16 16:02:56 +01002523#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002524static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002525{
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002527 {
2528 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002529 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002530 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002531 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002532 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002533 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002534 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002535 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002536 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002537 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002538 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002539 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002540 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002541 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002542 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002543 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002544 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002545 return( 128 );
2546 default:
2547 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002548 }
2549}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002550#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002551
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552/* Initialize the MAC operation structure. Once this function has been
2553 * called, psa_mac_abort can run and will do the right thing. */
2554static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2555 psa_algorithm_t alg )
2556{
2557 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2558
2559 operation->alg = alg;
2560 operation->key_set = 0;
2561 operation->iv_set = 0;
2562 operation->iv_required = 0;
2563 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002564 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002565
2566#if defined(MBEDTLS_CMAC_C)
2567 if( alg == PSA_ALG_CMAC )
2568 {
2569 operation->iv_required = 0;
2570 mbedtls_cipher_init( &operation->ctx.cmac );
2571 status = PSA_SUCCESS;
2572 }
2573 else
2574#endif /* MBEDTLS_CMAC_C */
2575#if defined(MBEDTLS_MD_C)
2576 if( PSA_ALG_IS_HMAC( operation->alg ) )
2577 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002578 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2579 operation->ctx.hmac.hash_ctx.alg = 0;
2580 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581 }
2582 else
2583#endif /* MBEDTLS_MD_C */
2584 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002585 if( ! PSA_ALG_IS_MAC( alg ) )
2586 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002587 }
2588
2589 if( status != PSA_SUCCESS )
2590 memset( operation, 0, sizeof( *operation ) );
2591 return( status );
2592}
2593
Gilles Peskine01126fa2018-07-12 17:04:55 +02002594#if defined(MBEDTLS_MD_C)
2595static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2596{
Gilles Peskine3f108122018-12-07 18:14:53 +01002597 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002598 return( psa_hash_abort( &hmac->hash_ctx ) );
2599}
2600#endif /* MBEDTLS_MD_C */
2601
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2603{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002604 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002606 /* The object has (apparently) been initialized but it is not
2607 * in use. It's ok to call abort on such an object, and there's
2608 * nothing to do. */
2609 return( PSA_SUCCESS );
2610 }
2611 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 if( operation->alg == PSA_ALG_CMAC )
2614 {
2615 mbedtls_cipher_free( &operation->ctx.cmac );
2616 }
2617 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002620 if( PSA_ALG_IS_HMAC( operation->alg ) )
2621 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002622 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 }
2624 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002626 {
2627 /* Sanity check (shouldn't happen: operation->alg should
2628 * always have been initialized to a valid value). */
2629 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630 }
Moran Peker41deec42018-04-04 15:43:05 +03002631
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632 operation->alg = 0;
2633 operation->key_set = 0;
2634 operation->iv_set = 0;
2635 operation->iv_required = 0;
2636 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002637 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002638
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002640
2641bad_state:
2642 /* If abort is called on an uninitialized object, we can't trust
2643 * anything. Wipe the object in case it contains confidential data.
2644 * This may result in a memory leak if a pointer gets overwritten,
2645 * but it's too late to do anything about this. */
2646 memset( operation, 0, sizeof( *operation ) );
2647 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648}
2649
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002650#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002651static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002652 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002653 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002654 const mbedtls_cipher_info_t *cipher_info )
2655{
2656 int ret;
2657
2658 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002659
2660 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2661 if( ret != 0 )
2662 return( ret );
2663
2664 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2665 slot->data.raw.data,
2666 key_bits );
2667 return( ret );
2668}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002669#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002670
Gilles Peskine248051a2018-06-20 16:09:38 +02002671#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002672static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2673 const uint8_t *key,
2674 size_t key_length,
2675 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002676{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002677 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002678 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002679 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002680 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002681 psa_status_t status;
2682
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002683 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2684 * overflow below. This should never trigger if the hash algorithm
2685 * is implemented correctly. */
2686 /* The size checks against the ipad and opad buffers cannot be written
2687 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2688 * because that triggers -Wlogical-op on GCC 7.3. */
2689 if( block_size > sizeof( ipad ) )
2690 return( PSA_ERROR_NOT_SUPPORTED );
2691 if( block_size > sizeof( hmac->opad ) )
2692 return( PSA_ERROR_NOT_SUPPORTED );
2693 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694 return( PSA_ERROR_NOT_SUPPORTED );
2695
Gilles Peskined223b522018-06-11 18:12:58 +02002696 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002697 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002698 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002699 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002700 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002702 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002703 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002705 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002707 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 }
Gilles Peskine96889972018-07-12 17:07:03 +02002709 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2710 * but it is permitted. It is common when HMAC is used in HKDF, for
2711 * example. Don't call `memcpy` in the 0-length because `key` could be
2712 * an invalid pointer which would make the behavior undefined. */
2713 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715
Gilles Peskined223b522018-06-11 18:12:58 +02002716 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2717 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002719 ipad[i] ^= 0x36;
2720 memset( ipad + key_length, 0x36, block_size - key_length );
2721
2722 /* Copy the key material from ipad to opad, flipping the requisite bits,
2723 * and filling the rest of opad with the requisite constant. */
2724 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002725 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2726 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727
Gilles Peskine01126fa2018-07-12 17:04:55 +02002728 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002730 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002731
Gilles Peskine01126fa2018-07-12 17:04:55 +02002732 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002733
2734cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002735 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002736
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737 return( status );
2738}
Gilles Peskine248051a2018-06-20 16:09:38 +02002739#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740
Gilles Peskine89167cb2018-07-08 20:12:23 +02002741static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002742 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002743 psa_algorithm_t alg,
2744 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002747 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002749 psa_key_usage_t usage =
2750 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002751 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002752 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002754 /* A context must be freshly initialized before it can be set up. */
2755 if( operation->alg != 0 )
2756 {
2757 return( PSA_ERROR_BAD_STATE );
2758 }
2759
Gilles Peskined911eb72018-08-14 15:18:45 +02002760 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002761 if( status != PSA_SUCCESS )
2762 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002763 if( is_sign )
2764 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765
Gilles Peskine28f8f302019-07-24 13:30:31 +02002766 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002767 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002768 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002769 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002770
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002772 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 {
2774 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002775 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002776 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002777 int ret;
2778 if( cipher_info == NULL )
2779 {
2780 status = PSA_ERROR_NOT_SUPPORTED;
2781 goto exit;
2782 }
2783 operation->mac_size = cipher_info->block_size;
2784 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2785 status = mbedtls_to_psa_error( ret );
2786 }
2787 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002790 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002791 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002792 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002793 if( hash_alg == 0 )
2794 {
2795 status = PSA_ERROR_NOT_SUPPORTED;
2796 goto exit;
2797 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002798
2799 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2800 /* Sanity check. This shouldn't fail on a valid configuration. */
2801 if( operation->mac_size == 0 ||
2802 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2803 {
2804 status = PSA_ERROR_NOT_SUPPORTED;
2805 goto exit;
2806 }
2807
Gilles Peskine8e338702019-07-30 20:06:31 +02002808 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002809 {
2810 status = PSA_ERROR_INVALID_ARGUMENT;
2811 goto exit;
2812 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002813
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2815 slot->data.raw.data,
2816 slot->data.raw.bytes,
2817 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 }
2819 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002820#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002821 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002822 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002825
Gilles Peskined911eb72018-08-14 15:18:45 +02002826 if( truncated == 0 )
2827 {
2828 /* The "normal" case: untruncated algorithm. Nothing to do. */
2829 }
2830 else if( truncated < 4 )
2831 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002832 /* A very short MAC is too short for security since it can be
2833 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2834 * so we make this our minimum, even though 32 bits is still
2835 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002836 status = PSA_ERROR_NOT_SUPPORTED;
2837 }
2838 else if( truncated > operation->mac_size )
2839 {
2840 /* It's impossible to "truncate" to a larger length. */
2841 status = PSA_ERROR_INVALID_ARGUMENT;
2842 }
2843 else
2844 operation->mac_size = truncated;
2845
Gilles Peskinefbfac682018-07-08 20:51:54 +02002846exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002847 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002849 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002851 else
2852 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002853 operation->key_set = 1;
2854 }
2855 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856}
2857
Gilles Peskine89167cb2018-07-08 20:12:23 +02002858psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002859 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002860 psa_algorithm_t alg )
2861{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002862 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002863}
2864
2865psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002866 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002867 psa_algorithm_t alg )
2868{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002869 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002870}
2871
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2873 const uint8_t *input,
2874 size_t input_length )
2875{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002878 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002880 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881 operation->has_input = 1;
2882
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002884 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002885 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002886 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2887 input, input_length );
2888 status = mbedtls_to_psa_error( ret );
2889 }
2890 else
2891#endif /* MBEDTLS_CMAC_C */
2892#if defined(MBEDTLS_MD_C)
2893 if( PSA_ALG_IS_HMAC( operation->alg ) )
2894 {
2895 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2896 input_length );
2897 }
2898 else
2899#endif /* MBEDTLS_MD_C */
2900 {
2901 /* This shouldn't happen if `operation` was initialized by
2902 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002903 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002904 }
2905
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 if( status != PSA_SUCCESS )
2907 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002908 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909}
2910
Gilles Peskine01126fa2018-07-12 17:04:55 +02002911#if defined(MBEDTLS_MD_C)
2912static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2913 uint8_t *mac,
2914 size_t mac_size )
2915{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002916 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002917 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2918 size_t hash_size = 0;
2919 size_t block_size = psa_get_hash_block_size( hash_alg );
2920 psa_status_t status;
2921
Gilles Peskine01126fa2018-07-12 17:04:55 +02002922 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2923 if( status != PSA_SUCCESS )
2924 return( status );
2925 /* From here on, tmp needs to be wiped. */
2926
2927 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2928 if( status != PSA_SUCCESS )
2929 goto exit;
2930
2931 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2932 if( status != PSA_SUCCESS )
2933 goto exit;
2934
2935 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2936 if( status != PSA_SUCCESS )
2937 goto exit;
2938
Gilles Peskined911eb72018-08-14 15:18:45 +02002939 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2940 if( status != PSA_SUCCESS )
2941 goto exit;
2942
2943 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002944
2945exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002946 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947 return( status );
2948}
2949#endif /* MBEDTLS_MD_C */
2950
mohammad16036df908f2018-04-02 08:34:15 -07002951static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002952 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002953 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002955 if( ! operation->key_set )
2956 return( PSA_ERROR_BAD_STATE );
2957 if( operation->iv_required && ! operation->iv_set )
2958 return( PSA_ERROR_BAD_STATE );
2959
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960 if( mac_size < operation->mac_size )
2961 return( PSA_ERROR_BUFFER_TOO_SMALL );
2962
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002964 if( operation->alg == PSA_ALG_CMAC )
2965 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002966 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2967 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2968 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002969 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002970 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002971 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002973 else
2974#endif /* MBEDTLS_CMAC_C */
2975#if defined(MBEDTLS_MD_C)
2976 if( PSA_ALG_IS_HMAC( operation->alg ) )
2977 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002978 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002979 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002980 }
2981 else
2982#endif /* MBEDTLS_MD_C */
2983 {
2984 /* This shouldn't happen if `operation` was initialized by
2985 * a setup function. */
2986 return( PSA_ERROR_BAD_STATE );
2987 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988}
2989
Gilles Peskineacd4be32018-07-08 19:56:25 +02002990psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2991 uint8_t *mac,
2992 size_t mac_size,
2993 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002994{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002995 psa_status_t status;
2996
Jaeden Amero252ef282019-02-15 14:05:35 +00002997 if( operation->alg == 0 )
2998 {
2999 return( PSA_ERROR_BAD_STATE );
3000 }
3001
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003002 /* Fill the output buffer with something that isn't a valid mac
3003 * (barring an attack on the mac and deliberately-crafted input),
3004 * in case the caller doesn't check the return status properly. */
3005 *mac_length = mac_size;
3006 /* If mac_size is 0 then mac may be NULL and then the
3007 * call to memset would have undefined behavior. */
3008 if( mac_size != 0 )
3009 memset( mac, '!', mac_size );
3010
Gilles Peskine89167cb2018-07-08 20:12:23 +02003011 if( ! operation->is_sign )
3012 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003013 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003014 }
mohammad16036df908f2018-04-02 08:34:15 -07003015
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003016 status = psa_mac_finish_internal( operation, mac, mac_size );
3017
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003018 if( status == PSA_SUCCESS )
3019 {
3020 status = psa_mac_abort( operation );
3021 if( status == PSA_SUCCESS )
3022 *mac_length = operation->mac_size;
3023 else
3024 memset( mac, '!', mac_size );
3025 }
3026 else
3027 psa_mac_abort( operation );
3028 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003029}
3030
Gilles Peskineacd4be32018-07-08 19:56:25 +02003031psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3032 const uint8_t *mac,
3033 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034{
Gilles Peskine828ed142018-06-18 23:25:51 +02003035 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003036 psa_status_t status;
3037
Jaeden Amero252ef282019-02-15 14:05:35 +00003038 if( operation->alg == 0 )
3039 {
3040 return( PSA_ERROR_BAD_STATE );
3041 }
3042
Gilles Peskine89167cb2018-07-08 20:12:23 +02003043 if( operation->is_sign )
3044 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003045 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003046 }
3047 if( operation->mac_size != mac_length )
3048 {
3049 status = PSA_ERROR_INVALID_SIGNATURE;
3050 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003051 }
mohammad16036df908f2018-04-02 08:34:15 -07003052
3053 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003054 actual_mac, sizeof( actual_mac ) );
3055
3056 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3057 status = PSA_ERROR_INVALID_SIGNATURE;
3058
3059cleanup:
3060 if( status == PSA_SUCCESS )
3061 status = psa_mac_abort( operation );
3062 else
3063 psa_mac_abort( operation );
3064
Gilles Peskine3f108122018-12-07 18:14:53 +01003065 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003066
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003067 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068}
3069
3070
Gilles Peskine20035e32018-02-03 22:44:14 +01003071
Gilles Peskine20035e32018-02-03 22:44:14 +01003072/****************************************************************/
3073/* Asymmetric cryptography */
3074/****************************************************************/
3075
Gilles Peskine2b450e32018-06-27 15:42:46 +02003076#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003077/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003078 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003079static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3080 size_t hash_length,
3081 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003082{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003083 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003084 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003085 *md_alg = mbedtls_md_get_type( md_info );
3086
3087 /* The Mbed TLS RSA module uses an unsigned int for hash length
3088 * parameters. Validate that it fits so that we don't risk an
3089 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003090#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003091 if( hash_length > UINT_MAX )
3092 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003093#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003094
3095#if defined(MBEDTLS_PKCS1_V15)
3096 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3097 * must be correct. */
3098 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3099 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003100 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003101 if( md_info == NULL )
3102 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003103 if( mbedtls_md_get_size( md_info ) != hash_length )
3104 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003105 }
3106#endif /* MBEDTLS_PKCS1_V15 */
3107
3108#if defined(MBEDTLS_PKCS1_V21)
3109 /* PSS requires a hash internally. */
3110 if( PSA_ALG_IS_RSA_PSS( alg ) )
3111 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003112 if( md_info == NULL )
3113 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003114 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003115#endif /* MBEDTLS_PKCS1_V21 */
3116
Gilles Peskine61b91d42018-06-08 16:09:36 +02003117 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003118}
3119
Gilles Peskine2b450e32018-06-27 15:42:46 +02003120static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3121 psa_algorithm_t alg,
3122 const uint8_t *hash,
3123 size_t hash_length,
3124 uint8_t *signature,
3125 size_t signature_size,
3126 size_t *signature_length )
3127{
3128 psa_status_t status;
3129 int ret;
3130 mbedtls_md_type_t md_alg;
3131
3132 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3133 if( status != PSA_SUCCESS )
3134 return( status );
3135
Gilles Peskine630a18a2018-06-29 17:49:35 +02003136 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003137 return( PSA_ERROR_BUFFER_TOO_SMALL );
3138
3139#if defined(MBEDTLS_PKCS1_V15)
3140 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3141 {
3142 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3143 MBEDTLS_MD_NONE );
3144 ret = mbedtls_rsa_pkcs1_sign( rsa,
3145 mbedtls_ctr_drbg_random,
3146 &global_data.ctr_drbg,
3147 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003148 md_alg,
3149 (unsigned int) hash_length,
3150 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003151 signature );
3152 }
3153 else
3154#endif /* MBEDTLS_PKCS1_V15 */
3155#if defined(MBEDTLS_PKCS1_V21)
3156 if( PSA_ALG_IS_RSA_PSS( alg ) )
3157 {
3158 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3159 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3160 mbedtls_ctr_drbg_random,
3161 &global_data.ctr_drbg,
3162 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003163 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003164 (unsigned int) hash_length,
3165 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003166 signature );
3167 }
3168 else
3169#endif /* MBEDTLS_PKCS1_V21 */
3170 {
3171 return( PSA_ERROR_INVALID_ARGUMENT );
3172 }
3173
3174 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003175 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003176 return( mbedtls_to_psa_error( ret ) );
3177}
3178
3179static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3180 psa_algorithm_t alg,
3181 const uint8_t *hash,
3182 size_t hash_length,
3183 const uint8_t *signature,
3184 size_t signature_length )
3185{
3186 psa_status_t status;
3187 int ret;
3188 mbedtls_md_type_t md_alg;
3189
3190 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3191 if( status != PSA_SUCCESS )
3192 return( status );
3193
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003194 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3195 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003196
3197#if defined(MBEDTLS_PKCS1_V15)
3198 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3199 {
3200 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3201 MBEDTLS_MD_NONE );
3202 ret = mbedtls_rsa_pkcs1_verify( rsa,
3203 mbedtls_ctr_drbg_random,
3204 &global_data.ctr_drbg,
3205 MBEDTLS_RSA_PUBLIC,
3206 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003207 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003208 hash,
3209 signature );
3210 }
3211 else
3212#endif /* MBEDTLS_PKCS1_V15 */
3213#if defined(MBEDTLS_PKCS1_V21)
3214 if( PSA_ALG_IS_RSA_PSS( alg ) )
3215 {
3216 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3217 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3218 mbedtls_ctr_drbg_random,
3219 &global_data.ctr_drbg,
3220 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003221 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003222 (unsigned int) hash_length,
3223 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003224 signature );
3225 }
3226 else
3227#endif /* MBEDTLS_PKCS1_V21 */
3228 {
3229 return( PSA_ERROR_INVALID_ARGUMENT );
3230 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003231
3232 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3233 * the rest of the signature is invalid". This has little use in
3234 * practice and PSA doesn't report this distinction. */
3235 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3236 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003237 return( mbedtls_to_psa_error( ret ) );
3238}
3239#endif /* MBEDTLS_RSA_C */
3240
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003241#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003242/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3243 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3244 * (even though these functions don't modify it). */
3245static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3246 psa_algorithm_t alg,
3247 const uint8_t *hash,
3248 size_t hash_length,
3249 uint8_t *signature,
3250 size_t signature_size,
3251 size_t *signature_length )
3252{
3253 int ret;
3254 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003255 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003256 mbedtls_mpi_init( &r );
3257 mbedtls_mpi_init( &s );
3258
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003259 if( signature_size < 2 * curve_bytes )
3260 {
3261 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3262 goto cleanup;
3263 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003264
Gilles Peskinea05219c2018-11-16 16:02:56 +01003265#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003266 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3267 {
3268 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3269 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3270 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003271 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3272 &ecp->d, hash,
3273 hash_length, md_alg,
3274 mbedtls_ctr_drbg_random,
3275 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003276 }
3277 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003278#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003279 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003280 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003281 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3282 hash, hash_length,
3283 mbedtls_ctr_drbg_random,
3284 &global_data.ctr_drbg ) );
3285 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003286
3287 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3288 signature,
3289 curve_bytes ) );
3290 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3291 signature + curve_bytes,
3292 curve_bytes ) );
3293
3294cleanup:
3295 mbedtls_mpi_free( &r );
3296 mbedtls_mpi_free( &s );
3297 if( ret == 0 )
3298 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003299 return( mbedtls_to_psa_error( ret ) );
3300}
3301
3302static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3303 const uint8_t *hash,
3304 size_t hash_length,
3305 const uint8_t *signature,
3306 size_t signature_length )
3307{
3308 int ret;
3309 mbedtls_mpi r, s;
3310 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3311 mbedtls_mpi_init( &r );
3312 mbedtls_mpi_init( &s );
3313
3314 if( signature_length != 2 * curve_bytes )
3315 return( PSA_ERROR_INVALID_SIGNATURE );
3316
3317 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3318 signature,
3319 curve_bytes ) );
3320 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3321 signature + curve_bytes,
3322 curve_bytes ) );
3323
3324 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3325 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003326
3327cleanup:
3328 mbedtls_mpi_free( &r );
3329 mbedtls_mpi_free( &s );
3330 return( mbedtls_to_psa_error( ret ) );
3331}
3332#endif /* MBEDTLS_ECDSA_C */
3333
Gilles Peskinec5487a82018-12-03 18:08:14 +01003334psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003335 psa_algorithm_t alg,
3336 const uint8_t *hash,
3337 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003338 uint8_t *signature,
3339 size_t signature_size,
3340 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003341{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003342 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003343 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3345 const psa_drv_se_t *drv;
3346 psa_drv_se_context_t *drv_context;
3347#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003348
3349 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003350 /* Immediately reject a zero-length signature buffer. This guarantees
3351 * that signature must be a valid pointer. (On the other hand, the hash
3352 * buffer can in principle be empty since it doesn't actually have
3353 * to be a hash.) */
3354 if( signature_size == 0 )
3355 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003356
Gilles Peskineedc64242019-08-07 21:05:07 +02003357 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003358 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003359 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003360 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003361 {
3362 status = PSA_ERROR_INVALID_ARGUMENT;
3363 goto exit;
3364 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003365
Gilles Peskineedc64242019-08-07 21:05:07 +02003366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3367 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3368 {
3369 if( drv->asymmetric == NULL ||
3370 drv->asymmetric->p_sign == NULL )
3371 {
3372 status = PSA_ERROR_NOT_SUPPORTED;
3373 goto exit;
3374 }
3375 status = drv->asymmetric->p_sign( drv_context,
3376 slot->data.se.slot_number,
3377 alg,
3378 hash, hash_length,
3379 signature, signature_size,
3380 signature_length );
3381 }
3382 else
3383#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003384#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003385 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003386 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003387 status = psa_rsa_sign( slot->data.rsa,
3388 alg,
3389 hash, hash_length,
3390 signature, signature_size,
3391 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003392 }
3393 else
3394#endif /* defined(MBEDTLS_RSA_C) */
3395#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003396 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003397 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003398#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003399 if(
3400#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3401 PSA_ALG_IS_ECDSA( alg )
3402#else
3403 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3404#endif
3405 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406 status = psa_ecdsa_sign( slot->data.ecp,
3407 alg,
3408 hash, hash_length,
3409 signature, signature_size,
3410 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003411 else
3412#endif /* defined(MBEDTLS_ECDSA_C) */
3413 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003415 }
itayzafrir5c753392018-05-08 11:18:38 +03003416 }
3417 else
3418#endif /* defined(MBEDTLS_ECP_C) */
3419 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003420 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003421 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003422
3423exit:
3424 /* Fill the unused part of the output buffer (the whole buffer on error,
3425 * the trailing part on success) with something that isn't a valid mac
3426 * (barring an attack on the mac and deliberately-crafted input),
3427 * in case the caller doesn't check the return status properly. */
3428 if( status == PSA_SUCCESS )
3429 memset( signature + *signature_length, '!',
3430 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003431 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003432 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003433 /* If signature_size is 0 then we have nothing to do. We must not call
3434 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003435 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003436}
3437
Gilles Peskinec5487a82018-12-03 18:08:14 +01003438psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003439 psa_algorithm_t alg,
3440 const uint8_t *hash,
3441 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003442 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003443 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003444{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003445 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003446 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003447#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3448 const psa_drv_se_t *drv;
3449 psa_drv_se_context_t *drv_context;
3450#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003451
Gilles Peskineedc64242019-08-07 21:05:07 +02003452 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003453 if( status != PSA_SUCCESS )
3454 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003455
Gilles Peskineedc64242019-08-07 21:05:07 +02003456#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3457 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3458 {
3459 if( drv->asymmetric == NULL ||
3460 drv->asymmetric->p_verify == NULL )
3461 return( PSA_ERROR_NOT_SUPPORTED );
3462 return( drv->asymmetric->p_verify( drv_context,
3463 slot->data.se.slot_number,
3464 alg,
3465 hash, hash_length,
3466 signature, signature_length ) );
3467 }
3468 else
3469#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003470#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003471 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003472 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003473 return( psa_rsa_verify( slot->data.rsa,
3474 alg,
3475 hash, hash_length,
3476 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003477 }
3478 else
3479#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003480#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003481 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003482 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003483#if defined(MBEDTLS_ECDSA_C)
3484 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003485 return( psa_ecdsa_verify( slot->data.ecp,
3486 hash, hash_length,
3487 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003488 else
3489#endif /* defined(MBEDTLS_ECDSA_C) */
3490 {
3491 return( PSA_ERROR_INVALID_ARGUMENT );
3492 }
itayzafrir5c753392018-05-08 11:18:38 +03003493 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003494 else
3495#endif /* defined(MBEDTLS_ECP_C) */
3496 {
3497 return( PSA_ERROR_NOT_SUPPORTED );
3498 }
3499}
3500
Gilles Peskine072ac562018-06-30 00:21:29 +02003501#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3502static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3503 mbedtls_rsa_context *rsa )
3504{
3505 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3506 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3507 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3508 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3509}
3510#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3511
Gilles Peskinec5487a82018-12-03 18:08:14 +01003512psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003513 psa_algorithm_t alg,
3514 const uint8_t *input,
3515 size_t input_length,
3516 const uint8_t *salt,
3517 size_t salt_length,
3518 uint8_t *output,
3519 size_t output_size,
3520 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003521{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003522 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003523 psa_status_t status;
3524
Darryl Green5cc689a2018-07-24 15:34:10 +01003525 (void) input;
3526 (void) input_length;
3527 (void) salt;
3528 (void) output;
3529 (void) output_size;
3530
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003531 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003533 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3534 return( PSA_ERROR_INVALID_ARGUMENT );
3535
Gilles Peskine28f8f302019-07-24 13:30:31 +02003536 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003537 if( status != PSA_SUCCESS )
3538 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003539 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3540 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542
3543#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003544 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545 {
3546 mbedtls_rsa_context *rsa = slot->data.rsa;
3547 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003548 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003549 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003551 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003552 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003553 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3554 mbedtls_ctr_drbg_random,
3555 &global_data.ctr_drbg,
3556 MBEDTLS_RSA_PUBLIC,
3557 input_length,
3558 input,
3559 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003560 }
3561 else
3562#endif /* MBEDTLS_PKCS1_V15 */
3563#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003564 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003566 psa_rsa_oaep_set_padding_mode( alg, rsa );
3567 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3568 mbedtls_ctr_drbg_random,
3569 &global_data.ctr_drbg,
3570 MBEDTLS_RSA_PUBLIC,
3571 salt, salt_length,
3572 input_length,
3573 input,
3574 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003575 }
3576 else
3577#endif /* MBEDTLS_PKCS1_V21 */
3578 {
3579 return( PSA_ERROR_INVALID_ARGUMENT );
3580 }
3581 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003582 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583 return( mbedtls_to_psa_error( ret ) );
3584 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003585 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003586#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003587 {
3588 return( PSA_ERROR_NOT_SUPPORTED );
3589 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003590}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591
Gilles Peskinec5487a82018-12-03 18:08:14 +01003592psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003593 psa_algorithm_t alg,
3594 const uint8_t *input,
3595 size_t input_length,
3596 const uint8_t *salt,
3597 size_t salt_length,
3598 uint8_t *output,
3599 size_t output_size,
3600 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003602 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003603 psa_status_t status;
3604
Darryl Green5cc689a2018-07-24 15:34:10 +01003605 (void) input;
3606 (void) input_length;
3607 (void) salt;
3608 (void) output;
3609 (void) output_size;
3610
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003611 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003613 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3614 return( PSA_ERROR_INVALID_ARGUMENT );
3615
Gilles Peskine28f8f302019-07-24 13:30:31 +02003616 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003617 if( status != PSA_SUCCESS )
3618 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003619 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620 return( PSA_ERROR_INVALID_ARGUMENT );
3621
3622#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003623 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624 {
3625 mbedtls_rsa_context *rsa = slot->data.rsa;
3626 int ret;
3627
Gilles Peskine630a18a2018-06-29 17:49:35 +02003628 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629 return( PSA_ERROR_INVALID_ARGUMENT );
3630
3631#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003632 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003634 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3635 mbedtls_ctr_drbg_random,
3636 &global_data.ctr_drbg,
3637 MBEDTLS_RSA_PRIVATE,
3638 output_length,
3639 input,
3640 output,
3641 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 }
3643 else
3644#endif /* MBEDTLS_PKCS1_V15 */
3645#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003646 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003648 psa_rsa_oaep_set_padding_mode( alg, rsa );
3649 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3650 mbedtls_ctr_drbg_random,
3651 &global_data.ctr_drbg,
3652 MBEDTLS_RSA_PRIVATE,
3653 salt, salt_length,
3654 output_length,
3655 input,
3656 output,
3657 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003658 }
3659 else
3660#endif /* MBEDTLS_PKCS1_V21 */
3661 {
3662 return( PSA_ERROR_INVALID_ARGUMENT );
3663 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003664
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003665 return( mbedtls_to_psa_error( ret ) );
3666 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003668#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669 {
3670 return( PSA_ERROR_NOT_SUPPORTED );
3671 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003672}
Gilles Peskine20035e32018-02-03 22:44:14 +01003673
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003674
3675
mohammad1603503973b2018-03-12 15:59:30 +02003676/****************************************************************/
3677/* Symmetric cryptography */
3678/****************************************************************/
3679
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003680/* Initialize the cipher operation structure. Once this function has been
3681 * called, psa_cipher_abort can run and will do the right thing. */
3682static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3683 psa_algorithm_t alg )
3684{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003685 if( ! PSA_ALG_IS_CIPHER( alg ) )
3686 {
3687 memset( operation, 0, sizeof( *operation ) );
3688 return( PSA_ERROR_INVALID_ARGUMENT );
3689 }
3690
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003691 operation->alg = alg;
3692 operation->key_set = 0;
3693 operation->iv_set = 0;
3694 operation->iv_required = 1;
3695 operation->iv_size = 0;
3696 operation->block_size = 0;
3697 mbedtls_cipher_init( &operation->ctx.cipher );
3698 return( PSA_SUCCESS );
3699}
3700
Gilles Peskinee553c652018-06-04 16:22:46 +02003701static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003702 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003703 psa_algorithm_t alg,
3704 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003705{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003706 int ret = 0;
3707 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003708 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003709 size_t key_bits;
3710 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003711 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3712 PSA_KEY_USAGE_ENCRYPT :
3713 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003714
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003715 /* A context must be freshly initialized before it can be set up. */
3716 if( operation->alg != 0 )
3717 {
3718 return( PSA_ERROR_BAD_STATE );
3719 }
3720
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003721 status = psa_cipher_init( operation, alg );
3722 if( status != PSA_SUCCESS )
3723 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003724
Gilles Peskine28f8f302019-07-24 13:30:31 +02003725 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003726 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003727 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003728 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003729
Gilles Peskine8e338702019-07-30 20:06:31 +02003730 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003731 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003732 {
3733 status = PSA_ERROR_NOT_SUPPORTED;
3734 goto exit;
3735 }
mohammad1603503973b2018-03-12 15:59:30 +02003736
mohammad1603503973b2018-03-12 15:59:30 +02003737 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003738 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003739 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003740
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003741#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003742 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003743 {
3744 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003745 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003746 memcpy( keys, slot->data.raw.data, 16 );
3747 memcpy( keys + 16, slot->data.raw.data, 8 );
3748 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3749 keys,
3750 192, cipher_operation );
3751 }
3752 else
3753#endif
3754 {
3755 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3756 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003757 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003758 }
Moran Peker41deec42018-04-04 15:43:05 +03003759 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003760 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003761
mohammad16038481e742018-03-18 13:57:31 +02003762#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003763 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003764 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003765 case PSA_ALG_CBC_NO_PADDING:
3766 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3767 MBEDTLS_PADDING_NONE );
3768 break;
3769 case PSA_ALG_CBC_PKCS7:
3770 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3771 MBEDTLS_PADDING_PKCS7 );
3772 break;
3773 default:
3774 /* The algorithm doesn't involve padding. */
3775 ret = 0;
3776 break;
3777 }
3778 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003779 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003780#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3781
mohammad1603503973b2018-03-12 15:59:30 +02003782 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003783 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003784 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003785 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003786 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003787 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003788 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003789#if defined(MBEDTLS_CHACHA20_C)
3790 else
3791 if( alg == PSA_ALG_CHACHA20 )
3792 operation->iv_size = 12;
3793#endif
mohammad1603503973b2018-03-12 15:59:30 +02003794
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003795exit:
3796 if( status == 0 )
3797 status = mbedtls_to_psa_error( ret );
3798 if( status != 0 )
3799 psa_cipher_abort( operation );
3800 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003801}
3802
Gilles Peskinefe119512018-07-08 21:39:34 +02003803psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003804 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003805 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003806{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003807 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003808}
3809
Gilles Peskinefe119512018-07-08 21:39:34 +02003810psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003811 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003812 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003813{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003814 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003815}
3816
Gilles Peskinefe119512018-07-08 21:39:34 +02003817psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003818 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003819 size_t iv_size,
3820 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003821{
itayzafrir534bd7c2018-08-02 13:56:32 +03003822 psa_status_t status;
3823 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003824 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003825 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003826 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003827 }
Moran Peker41deec42018-04-04 15:43:05 +03003828 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003829 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003830 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003831 goto exit;
3832 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003833 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3834 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003835 if( ret != 0 )
3836 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003837 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003838 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003839 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003840
mohammad16038481e742018-03-18 13:57:31 +02003841 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003842 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003843
Moran Peker395db872018-05-31 14:07:14 +03003844exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003845 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003846 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003847 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003848}
3849
Gilles Peskinefe119512018-07-08 21:39:34 +02003850psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003851 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003852 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003853{
itayzafrir534bd7c2018-08-02 13:56:32 +03003854 psa_status_t status;
3855 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003856 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003857 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003858 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003859 }
Moran Pekera28258c2018-05-29 16:25:04 +03003860 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003861 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003862 status = PSA_ERROR_INVALID_ARGUMENT;
3863 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003864 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003865 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3866 status = mbedtls_to_psa_error( ret );
3867exit:
3868 if( status == PSA_SUCCESS )
3869 operation->iv_set = 1;
3870 else
mohammad1603503973b2018-03-12 15:59:30 +02003871 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003873}
3874
Gilles Peskinee553c652018-06-04 16:22:46 +02003875psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3876 const uint8_t *input,
3877 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003878 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003879 size_t output_size,
3880 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003881{
itayzafrir534bd7c2018-08-02 13:56:32 +03003882 psa_status_t status;
3883 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003884 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003885
3886 if( operation->alg == 0 )
3887 {
3888 return( PSA_ERROR_BAD_STATE );
3889 }
3890
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003891 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003892 {
3893 /* Take the unprocessed partial block left over from previous
3894 * update calls, if any, plus the input to this call. Remove
3895 * the last partial block, if any. You get the data that will be
3896 * output in this call. */
3897 expected_output_size =
3898 ( operation->ctx.cipher.unprocessed_len + input_length )
3899 / operation->block_size * operation->block_size;
3900 }
3901 else
3902 {
3903 expected_output_size = input_length;
3904 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003905
Gilles Peskine89d789c2018-06-04 17:17:16 +02003906 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 {
3908 status = PSA_ERROR_BUFFER_TOO_SMALL;
3909 goto exit;
3910 }
mohammad160382759612018-03-12 18:16:40 +02003911
mohammad1603503973b2018-03-12 15:59:30 +02003912 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003913 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003914 status = mbedtls_to_psa_error( ret );
3915exit:
3916 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003917 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003918 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003919}
3920
Gilles Peskinee553c652018-06-04 16:22:46 +02003921psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3922 uint8_t *output,
3923 size_t output_size,
3924 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003925{
David Saadab4ecc272019-02-14 13:48:10 +02003926 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003927 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003928 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003929
mohammad1603503973b2018-03-12 15:59:30 +02003930 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003931 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003932 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003933 }
3934 if( operation->iv_required && ! operation->iv_set )
3935 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003936 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003937 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003938
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003939 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003940 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3941 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003942 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003943 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003944 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003945 }
3946
Janos Follath315b51c2018-07-09 16:04:51 +01003947 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3948 temp_output_buffer,
3949 output_length );
3950 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003951 {
Janos Follath315b51c2018-07-09 16:04:51 +01003952 status = mbedtls_to_psa_error( cipher_ret );
3953 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003954 }
Janos Follath315b51c2018-07-09 16:04:51 +01003955
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003956 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003957 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003958 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003959 memcpy( output, temp_output_buffer, *output_length );
3960 else
3961 {
Janos Follath315b51c2018-07-09 16:04:51 +01003962 status = PSA_ERROR_BUFFER_TOO_SMALL;
3963 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003964 }
mohammad1603503973b2018-03-12 15:59:30 +02003965
Gilles Peskine3f108122018-12-07 18:14:53 +01003966 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003967 status = psa_cipher_abort( operation );
3968
3969 return( status );
3970
3971error:
3972
3973 *output_length = 0;
3974
Gilles Peskine3f108122018-12-07 18:14:53 +01003975 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003976 (void) psa_cipher_abort( operation );
3977
3978 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003979}
3980
Gilles Peskinee553c652018-06-04 16:22:46 +02003981psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3982{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003983 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003984 {
3985 /* The object has (apparently) been initialized but it is not
3986 * in use. It's ok to call abort on such an object, and there's
3987 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003988 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003989 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003990
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003991 /* Sanity check (shouldn't happen: operation->alg should
3992 * always have been initialized to a valid value). */
3993 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3994 return( PSA_ERROR_BAD_STATE );
3995
mohammad1603503973b2018-03-12 15:59:30 +02003996 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003997
Moran Peker41deec42018-04-04 15:43:05 +03003998 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003999 operation->key_set = 0;
4000 operation->iv_set = 0;
4001 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004002 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004003 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004004
Moran Peker395db872018-05-31 14:07:14 +03004005 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004006}
4007
Gilles Peskinea0655c32018-04-30 17:06:50 +02004008
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004009
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004010
mohammad16035955c982018-04-26 00:53:03 +03004011/****************************************************************/
4012/* AEAD */
4013/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004014
Gilles Peskineedf9a652018-08-17 18:11:56 +02004015typedef struct
4016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004017 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004018 const mbedtls_cipher_info_t *cipher_info;
4019 union
4020 {
4021#if defined(MBEDTLS_CCM_C)
4022 mbedtls_ccm_context ccm;
4023#endif /* MBEDTLS_CCM_C */
4024#if defined(MBEDTLS_GCM_C)
4025 mbedtls_gcm_context gcm;
4026#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004027#if defined(MBEDTLS_CHACHAPOLY_C)
4028 mbedtls_chachapoly_context chachapoly;
4029#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004030 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004031 psa_algorithm_t core_alg;
4032 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004033 uint8_t tag_length;
4034} aead_operation_t;
4035
Gilles Peskine30a9e412019-01-14 18:36:12 +01004036static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004037{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004038 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004039 {
4040#if defined(MBEDTLS_CCM_C)
4041 case PSA_ALG_CCM:
4042 mbedtls_ccm_free( &operation->ctx.ccm );
4043 break;
4044#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004045#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004046 case PSA_ALG_GCM:
4047 mbedtls_gcm_free( &operation->ctx.gcm );
4048 break;
4049#endif /* MBEDTLS_GCM_C */
4050 }
4051}
4052
4053static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004054 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004055 psa_key_usage_t usage,
4056 psa_algorithm_t alg )
4057{
4058 psa_status_t status;
4059 size_t key_bits;
4060 mbedtls_cipher_id_t cipher_id;
4061
Gilles Peskine28f8f302019-07-24 13:30:31 +02004062 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004063 if( status != PSA_SUCCESS )
4064 return( status );
4065
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004066 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004067
4068 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004069 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004070 &cipher_id );
4071 if( operation->cipher_info == NULL )
4072 return( PSA_ERROR_NOT_SUPPORTED );
4073
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004074 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004075 {
4076#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004077 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4078 operation->core_alg = PSA_ALG_CCM;
4079 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004080 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4081 * The call to mbedtls_ccm_encrypt_and_tag or
4082 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004083 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004084 return( PSA_ERROR_INVALID_ARGUMENT );
4085 mbedtls_ccm_init( &operation->ctx.ccm );
4086 status = mbedtls_to_psa_error(
4087 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4088 operation->slot->data.raw.data,
4089 (unsigned int) key_bits ) );
4090 if( status != 0 )
4091 goto cleanup;
4092 break;
4093#endif /* MBEDTLS_CCM_C */
4094
4095#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004096 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4097 operation->core_alg = PSA_ALG_GCM;
4098 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004099 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4100 * The call to mbedtls_gcm_crypt_and_tag or
4101 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004102 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004103 return( PSA_ERROR_INVALID_ARGUMENT );
4104 mbedtls_gcm_init( &operation->ctx.gcm );
4105 status = mbedtls_to_psa_error(
4106 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4107 operation->slot->data.raw.data,
4108 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004109 if( status != 0 )
4110 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004111 break;
4112#endif /* MBEDTLS_GCM_C */
4113
Gilles Peskine26869f22019-05-06 15:25:00 +02004114#if defined(MBEDTLS_CHACHAPOLY_C)
4115 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4116 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4117 operation->full_tag_length = 16;
4118 /* We only support the default tag length. */
4119 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4120 return( PSA_ERROR_NOT_SUPPORTED );
4121 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4122 status = mbedtls_to_psa_error(
4123 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4124 operation->slot->data.raw.data ) );
4125 if( status != 0 )
4126 goto cleanup;
4127 break;
4128#endif /* MBEDTLS_CHACHAPOLY_C */
4129
Gilles Peskineedf9a652018-08-17 18:11:56 +02004130 default:
4131 return( PSA_ERROR_NOT_SUPPORTED );
4132 }
4133
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004134 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4135 {
4136 status = PSA_ERROR_INVALID_ARGUMENT;
4137 goto cleanup;
4138 }
4139 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004140
Gilles Peskineedf9a652018-08-17 18:11:56 +02004141 return( PSA_SUCCESS );
4142
4143cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004144 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004145 return( status );
4146}
4147
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004149 psa_algorithm_t alg,
4150 const uint8_t *nonce,
4151 size_t nonce_length,
4152 const uint8_t *additional_data,
4153 size_t additional_data_length,
4154 const uint8_t *plaintext,
4155 size_t plaintext_length,
4156 uint8_t *ciphertext,
4157 size_t ciphertext_size,
4158 size_t *ciphertext_length )
4159{
mohammad16035955c982018-04-26 00:53:03 +03004160 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004161 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004162 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004163
mohammad1603f08a5502018-06-03 15:05:47 +03004164 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004165
Gilles Peskinec5487a82018-12-03 18:08:14 +01004166 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004167 if( status != PSA_SUCCESS )
4168 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004169
Gilles Peskineedf9a652018-08-17 18:11:56 +02004170 /* For all currently supported modes, the tag is at the end of the
4171 * ciphertext. */
4172 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4173 {
4174 status = PSA_ERROR_BUFFER_TOO_SMALL;
4175 goto exit;
4176 }
4177 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004178
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004179#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004180 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004181 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004182 status = mbedtls_to_psa_error(
4183 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4184 MBEDTLS_GCM_ENCRYPT,
4185 plaintext_length,
4186 nonce, nonce_length,
4187 additional_data, additional_data_length,
4188 plaintext, ciphertext,
4189 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004190 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004191 else
4192#endif /* MBEDTLS_GCM_C */
4193#if defined(MBEDTLS_CCM_C)
4194 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004195 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004196 status = mbedtls_to_psa_error(
4197 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4198 plaintext_length,
4199 nonce, nonce_length,
4200 additional_data,
4201 additional_data_length,
4202 plaintext, ciphertext,
4203 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004204 }
mohammad16035c8845f2018-05-09 05:40:09 -07004205 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004206#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004207#if defined(MBEDTLS_CHACHAPOLY_C)
4208 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4209 {
4210 if( nonce_length != 12 || operation.tag_length != 16 )
4211 {
4212 status = PSA_ERROR_NOT_SUPPORTED;
4213 goto exit;
4214 }
4215 status = mbedtls_to_psa_error(
4216 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4217 plaintext_length,
4218 nonce,
4219 additional_data,
4220 additional_data_length,
4221 plaintext,
4222 ciphertext,
4223 tag ) );
4224 }
4225 else
4226#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004227 {
mohammad1603554faad2018-06-03 15:07:38 +03004228 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004229 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004230
Gilles Peskineedf9a652018-08-17 18:11:56 +02004231 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4232 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004233
Gilles Peskineedf9a652018-08-17 18:11:56 +02004234exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004235 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004236 if( status == PSA_SUCCESS )
4237 *ciphertext_length = plaintext_length + operation.tag_length;
4238 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004239}
4240
Gilles Peskineee652a32018-06-01 19:23:52 +02004241/* Locate the tag in a ciphertext buffer containing the encrypted data
4242 * followed by the tag. Return the length of the part preceding the tag in
4243 * *plaintext_length. This is the size of the plaintext in modes where
4244 * the encrypted data has the same size as the plaintext, such as
4245 * CCM and GCM. */
4246static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4247 const uint8_t *ciphertext,
4248 size_t ciphertext_length,
4249 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004250 const uint8_t **p_tag )
4251{
4252 size_t payload_length;
4253 if( tag_length > ciphertext_length )
4254 return( PSA_ERROR_INVALID_ARGUMENT );
4255 payload_length = ciphertext_length - tag_length;
4256 if( payload_length > plaintext_size )
4257 return( PSA_ERROR_BUFFER_TOO_SMALL );
4258 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004259 return( PSA_SUCCESS );
4260}
4261
Gilles Peskinec5487a82018-12-03 18:08:14 +01004262psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004263 psa_algorithm_t alg,
4264 const uint8_t *nonce,
4265 size_t nonce_length,
4266 const uint8_t *additional_data,
4267 size_t additional_data_length,
4268 const uint8_t *ciphertext,
4269 size_t ciphertext_length,
4270 uint8_t *plaintext,
4271 size_t plaintext_size,
4272 size_t *plaintext_length )
4273{
mohammad16035955c982018-04-26 00:53:03 +03004274 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004275 aead_operation_t operation;
4276 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004277
Gilles Peskineee652a32018-06-01 19:23:52 +02004278 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004279
Gilles Peskinec5487a82018-12-03 18:08:14 +01004280 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004281 if( status != PSA_SUCCESS )
4282 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004283
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004284 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4285 ciphertext, ciphertext_length,
4286 plaintext_size, &tag );
4287 if( status != PSA_SUCCESS )
4288 goto exit;
4289
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004290#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004291 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004292 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004293 status = mbedtls_to_psa_error(
4294 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4295 ciphertext_length - operation.tag_length,
4296 nonce, nonce_length,
4297 additional_data,
4298 additional_data_length,
4299 tag, operation.tag_length,
4300 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004301 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004302 else
4303#endif /* MBEDTLS_GCM_C */
4304#if defined(MBEDTLS_CCM_C)
4305 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004306 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004307 status = mbedtls_to_psa_error(
4308 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4309 ciphertext_length - operation.tag_length,
4310 nonce, nonce_length,
4311 additional_data,
4312 additional_data_length,
4313 ciphertext, plaintext,
4314 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004315 }
mohammad160339574652018-06-01 04:39:53 -07004316 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004317#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004318#if defined(MBEDTLS_CHACHAPOLY_C)
4319 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4320 {
4321 if( nonce_length != 12 || operation.tag_length != 16 )
4322 {
4323 status = PSA_ERROR_NOT_SUPPORTED;
4324 goto exit;
4325 }
4326 status = mbedtls_to_psa_error(
4327 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4328 ciphertext_length - operation.tag_length,
4329 nonce,
4330 additional_data,
4331 additional_data_length,
4332 tag,
4333 ciphertext,
4334 plaintext ) );
4335 }
4336 else
4337#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004338 {
mohammad1603554faad2018-06-03 15:07:38 +03004339 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004340 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004341
Gilles Peskineedf9a652018-08-17 18:11:56 +02004342 if( status != PSA_SUCCESS && plaintext_size != 0 )
4343 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004344
Gilles Peskineedf9a652018-08-17 18:11:56 +02004345exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004346 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004347 if( status == PSA_SUCCESS )
4348 *plaintext_length = ciphertext_length - operation.tag_length;
4349 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004350}
4351
Gilles Peskinea0655c32018-04-30 17:06:50 +02004352
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004353
Gilles Peskine20035e32018-02-03 22:44:14 +01004354/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004355/* Generators */
4356/****************************************************************/
4357
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004358#define HKDF_STATE_INIT 0 /* no input yet */
4359#define HKDF_STATE_STARTED 1 /* got salt */
4360#define HKDF_STATE_KEYED 2 /* got key */
4361#define HKDF_STATE_OUTPUT 3 /* output started */
4362
Gilles Peskinecbe66502019-05-16 16:59:18 +02004363static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004364 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004365{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4367 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004368 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004370}
4371
4372
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004374{
4375 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004377 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004378 {
4379 /* The object has (apparently) been initialized but it is not
4380 * in use. It's ok to call abort on such an object, and there's
4381 * nothing to do. */
4382 }
4383 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004384#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004385 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004386 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004387 mbedtls_free( operation->ctx.hkdf.info );
4388 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004389 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004390 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004391 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004392 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004393 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004394 if( operation->ctx.tls12_prf.seed != NULL )
4395 {
4396 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4397 operation->ctx.tls12_prf.seed_length );
4398 mbedtls_free( operation->ctx.tls12_prf.seed );
4399 }
4400
4401 if( operation->ctx.tls12_prf.label != NULL )
4402 {
4403 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4404 operation->ctx.tls12_prf.label_length );
4405 mbedtls_free( operation->ctx.tls12_prf.label );
4406 }
4407
4408 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4409
4410 /* We leave the fields Ai and output_block to be erased safely by the
4411 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004412 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004413 else
4414#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004415 {
4416 status = PSA_ERROR_BAD_STATE;
4417 }
Janos Follath083036a2019-06-11 10:22:26 +01004418 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419 return( status );
4420}
4421
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004423 size_t *capacity)
4424{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004426 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004428 return PSA_ERROR_BAD_STATE;
4429 }
4430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004432 return( PSA_SUCCESS );
4433}
4434
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004436 size_t capacity )
4437{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004441 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004443 return( PSA_SUCCESS );
4444}
4445
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004448 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004449static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004450 psa_algorithm_t hash_alg,
4451 uint8_t *output,
4452 size_t output_length )
4453{
4454 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4455 psa_status_t status;
4456
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004457 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4458 return( PSA_ERROR_BAD_STATE );
4459 hkdf->state = HKDF_STATE_OUTPUT;
4460
Gilles Peskinebef7f142018-07-12 17:22:21 +02004461 while( output_length != 0 )
4462 {
4463 /* Copy what remains of the current block */
4464 uint8_t n = hash_length - hkdf->offset_in_block;
4465 if( n > output_length )
4466 n = (uint8_t) output_length;
4467 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4468 output += n;
4469 output_length -= n;
4470 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004471 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004472 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004473 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004474 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004476 * object was corrupted or if this function is called directly
4477 * inside the library. */
4478 if( hkdf->block_number == 0xff )
4479 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004480
4481 /* We need a new block */
4482 ++hkdf->block_number;
4483 hkdf->offset_in_block = 0;
4484 status = psa_hmac_setup_internal( &hkdf->hmac,
4485 hkdf->prk, hash_length,
4486 hash_alg );
4487 if( status != PSA_SUCCESS )
4488 return( status );
4489 if( hkdf->block_number != 1 )
4490 {
4491 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4492 hkdf->output_block,
4493 hash_length );
4494 if( status != PSA_SUCCESS )
4495 return( status );
4496 }
4497 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4498 hkdf->info,
4499 hkdf->info_length );
4500 if( status != PSA_SUCCESS )
4501 return( status );
4502 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4503 &hkdf->block_number, 1 );
4504 if( status != PSA_SUCCESS )
4505 return( status );
4506 status = psa_hmac_finish_internal( &hkdf->hmac,
4507 hkdf->output_block,
4508 sizeof( hkdf->output_block ) );
4509 if( status != PSA_SUCCESS )
4510 return( status );
4511 }
4512
4513 return( PSA_SUCCESS );
4514}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004515
Janos Follath7742fee2019-06-17 12:58:10 +01004516static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4517 psa_tls12_prf_key_derivation_t *tls12_prf,
4518 psa_algorithm_t alg )
4519{
4520 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4521 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4523 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004524
Janos Follath7742fee2019-06-17 12:58:10 +01004525 /* We can't be wanting more output after block 0xff, otherwise
4526 * the capacity check in psa_key_derivation_output_bytes() would have
4527 * prevented this call. It could happen only if the operation
4528 * object was corrupted or if this function is called directly
4529 * inside the library. */
4530 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004531 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004532
4533 /* We need a new block */
4534 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004535 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004536
4537 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4538 *
4539 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4540 *
4541 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4542 * HMAC_hash(secret, A(2) + seed) +
4543 * HMAC_hash(secret, A(3) + seed) + ...
4544 *
4545 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004546 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004547 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004548 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004549 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004550 * is currently extracted as `output_block` and where i is
4551 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004552 */
4553
Janos Follathea29bfb2019-06-19 12:21:20 +01004554 /* Save the hash context before using it, to preserve the hash state with
4555 * only the inner padding in it. We need this, because inner padding depends
4556 * on the key (secret in the RFC's terminology). */
4557 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4558 if( status != PSA_SUCCESS )
4559 goto cleanup;
4560
4561 /* Calculate A(i) where i = tls12_prf->block_number. */
4562 if( tls12_prf->block_number == 1 )
4563 {
4564 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4565 * the variable seed and in this instance means it in the context of the
4566 * P_hash function, where seed = label + seed.) */
4567 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4568 tls12_prf->label, tls12_prf->label_length );
4569 if( status != PSA_SUCCESS )
4570 goto cleanup;
4571 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4572 tls12_prf->seed, tls12_prf->seed_length );
4573 if( status != PSA_SUCCESS )
4574 goto cleanup;
4575 }
4576 else
4577 {
4578 /* A(i) = HMAC_hash(secret, A(i-1)) */
4579 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4580 tls12_prf->Ai, hash_length );
4581 if( status != PSA_SUCCESS )
4582 goto cleanup;
4583 }
4584
4585 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4586 tls12_prf->Ai, hash_length );
4587 if( status != PSA_SUCCESS )
4588 goto cleanup;
4589 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4590 if( status != PSA_SUCCESS )
4591 goto cleanup;
4592
4593 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4594 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4595 tls12_prf->Ai, hash_length );
4596 if( status != PSA_SUCCESS )
4597 goto cleanup;
4598 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4599 tls12_prf->label, tls12_prf->label_length );
4600 if( status != PSA_SUCCESS )
4601 goto cleanup;
4602 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4603 tls12_prf->seed, tls12_prf->seed_length );
4604 if( status != PSA_SUCCESS )
4605 goto cleanup;
4606 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4607 tls12_prf->output_block, hash_length );
4608 if( status != PSA_SUCCESS )
4609 goto cleanup;
4610 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4611 if( status != PSA_SUCCESS )
4612 goto cleanup;
4613
Janos Follath7742fee2019-06-17 12:58:10 +01004614
4615cleanup:
4616
Janos Follathea29bfb2019-06-19 12:21:20 +01004617 cleanup_status = psa_hash_abort( &backup );
4618 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4619 status = cleanup_status;
4620
4621 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004622}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004623
Janos Follath844eb0e2019-06-19 12:10:49 +01004624static psa_status_t psa_key_derivation_tls12_prf_read(
4625 psa_tls12_prf_key_derivation_t *tls12_prf,
4626 psa_algorithm_t alg,
4627 uint8_t *output,
4628 size_t output_length )
4629{
4630 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4631 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4632 psa_status_t status;
4633 uint8_t offset, length;
4634
4635 while( output_length != 0 )
4636 {
4637 /* Check if we have fully processed the current block. */
4638 if( tls12_prf->left_in_block == 0 )
4639 {
4640 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4641 alg );
4642 if( status != PSA_SUCCESS )
4643 return( status );
4644
4645 continue;
4646 }
4647
4648 if( tls12_prf->left_in_block > output_length )
4649 length = (uint8_t) output_length;
4650 else
4651 length = tls12_prf->left_in_block;
4652
4653 offset = hash_length - tls12_prf->left_in_block;
4654 memcpy( output, tls12_prf->output_block + offset, length );
4655 output += length;
4656 output_length -= length;
4657 tls12_prf->left_in_block -= length;
4658 }
4659
4660 return( PSA_SUCCESS );
4661}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004662#endif /* MBEDTLS_MD_C */
4663
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004664psa_status_t psa_key_derivation_output_bytes(
4665 psa_key_derivation_operation_t *operation,
4666 uint8_t *output,
4667 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004668{
4669 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004673 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004675 return PSA_ERROR_BAD_STATE;
4676 }
4677
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004679 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004681 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004683 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004684 goto exit;
4685 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004689 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4691 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004692 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004694 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004695 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004697
Gilles Peskinebef7f142018-07-12 17:22:21 +02004698#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004699 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004700 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004701 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004703 output, output_length );
4704 }
Janos Follathadbec812019-06-14 11:05:39 +01004705 else
Janos Follathadbec812019-06-14 11:05:39 +01004706 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004707 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004708 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004710 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004711 output_length );
4712 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004713 else
4714#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004715 {
4716 return( PSA_ERROR_BAD_STATE );
4717 }
4718
4719exit:
4720 if( status != PSA_SUCCESS )
4721 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004722 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 * This allows us to differentiate between exhausted operations and
4724 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4725 * operations. */
4726 psa_algorithm_t alg = operation->alg;
4727 psa_key_derivation_abort( operation );
4728 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729 memset( output, '!', output_length );
4730 }
4731 return( status );
4732}
4733
Gilles Peskine08542d82018-07-19 17:05:42 +02004734#if defined(MBEDTLS_DES_C)
4735static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4736{
4737 if( data_size >= 8 )
4738 mbedtls_des_key_set_parity( data );
4739 if( data_size >= 16 )
4740 mbedtls_des_key_set_parity( data + 8 );
4741 if( data_size >= 24 )
4742 mbedtls_des_key_set_parity( data + 16 );
4743}
4744#endif /* MBEDTLS_DES_C */
4745
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004746static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004747 psa_key_slot_t *slot,
4748 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750{
4751 uint8_t *data = NULL;
4752 size_t bytes = PSA_BITS_TO_BYTES( bits );
4753 psa_status_t status;
4754
Gilles Peskine8e338702019-07-30 20:06:31 +02004755 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756 return( PSA_ERROR_INVALID_ARGUMENT );
4757 if( bits % 8 != 0 )
4758 return( PSA_ERROR_INVALID_ARGUMENT );
4759 data = mbedtls_calloc( 1, bytes );
4760 if( data == NULL )
4761 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4762
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764 if( status != PSA_SUCCESS )
4765 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004766#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004767 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004768 psa_des_set_key_parity( data, bytes );
4769#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004770 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004771
4772exit:
4773 mbedtls_free( data );
4774 return( status );
4775}
4776
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004777psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004779 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004780{
4781 psa_status_t status;
4782 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004783 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004784
4785 /* Reject any attempt to create a zero-length key so that we don't
4786 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4787 if( psa_get_key_bits( attributes ) == 0 )
4788 return( PSA_ERROR_INVALID_ARGUMENT );
4789
Gilles Peskinedf179142019-07-15 22:02:14 +02004790 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4791 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004792#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4793 if( driver != NULL )
4794 {
4795 /* Deriving a key in a secure element is not implemented yet. */
4796 status = PSA_ERROR_NOT_SUPPORTED;
4797 }
4798#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004799 if( status == PSA_SUCCESS )
4800 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004801 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004802 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004804 }
4805 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004806 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004807 if( status != PSA_SUCCESS )
4808 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004809 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004810 *handle = 0;
4811 }
4812 return( status );
4813}
4814
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815
4816
4817/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004818/* Key derivation */
4819/****************************************************************/
4820
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004824{
Janos Follath5fe19732019-06-20 15:09:30 +01004825 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4826 * initialisers for this union leave some bytes unspecified.) */
4827 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4828
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004830#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4832 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4833 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004834 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004835 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4837 if( hash_size == 0 )
4838 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004839 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4840 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004841 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004842 {
4843 return( PSA_ERROR_NOT_SUPPORTED );
4844 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004846 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004847 }
4848#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004849 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004850 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004851}
4852
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004854 psa_algorithm_t alg )
4855{
4856 psa_status_t status;
4857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004859 return( PSA_ERROR_BAD_STATE );
4860
Gilles Peskine6843c292019-01-18 16:44:49 +01004861 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4862 return( PSA_ERROR_INVALID_ARGUMENT );
4863 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004864 {
4865 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004867 }
4868 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4869 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004871 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004872 else
4873 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874
4875 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004876 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004877 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878}
4879
4880#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004881static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004882 psa_algorithm_t hash_alg,
4883 psa_key_derivation_step_t step,
4884 const uint8_t *data,
4885 size_t data_length )
4886{
4887 psa_status_t status;
4888 switch( step )
4889 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004890 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004891 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004893 status = psa_hmac_setup_internal( &hkdf->hmac,
4894 data, data_length,
4895 hash_alg );
4896 if( status != PSA_SUCCESS )
4897 return( status );
4898 hkdf->state = HKDF_STATE_STARTED;
4899 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004900 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 /* If no salt was provided, use an empty salt. */
4902 if( hkdf->state == HKDF_STATE_INIT )
4903 {
4904 status = psa_hmac_setup_internal( &hkdf->hmac,
4905 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004906 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907 if( status != PSA_SUCCESS )
4908 return( status );
4909 hkdf->state = HKDF_STATE_STARTED;
4910 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004911 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004912 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004913 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4914 data, data_length );
4915 if( status != PSA_SUCCESS )
4916 return( status );
4917 status = psa_hmac_finish_internal( &hkdf->hmac,
4918 hkdf->prk,
4919 sizeof( hkdf->prk ) );
4920 if( status != PSA_SUCCESS )
4921 return( status );
4922 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4923 hkdf->block_number = 0;
4924 hkdf->state = HKDF_STATE_KEYED;
4925 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004926 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927 if( hkdf->state == HKDF_STATE_OUTPUT )
4928 return( PSA_ERROR_BAD_STATE );
4929 if( hkdf->info_set )
4930 return( PSA_ERROR_BAD_STATE );
4931 hkdf->info_length = data_length;
4932 if( data_length != 0 )
4933 {
4934 hkdf->info = mbedtls_calloc( 1, data_length );
4935 if( hkdf->info == NULL )
4936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4937 memcpy( hkdf->info, data, data_length );
4938 }
4939 hkdf->info_set = 1;
4940 return( PSA_SUCCESS );
4941 default:
4942 return( PSA_ERROR_INVALID_ARGUMENT );
4943 }
4944}
Janos Follathb03233e2019-06-11 15:30:30 +01004945
Janos Follathf08e2652019-06-13 09:05:41 +01004946static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4947 const uint8_t *data,
4948 size_t data_length )
4949{
4950 if( prf->state != TLS12_PRF_STATE_INIT )
4951 return( PSA_ERROR_BAD_STATE );
4952
Janos Follathd6dce9f2019-07-04 09:11:38 +01004953 if( data_length != 0 )
4954 {
4955 prf->seed = mbedtls_calloc( 1, data_length );
4956 if( prf->seed == NULL )
4957 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004958
Janos Follathd6dce9f2019-07-04 09:11:38 +01004959 memcpy( prf->seed, data, data_length );
4960 prf->seed_length = data_length;
4961 }
Janos Follathf08e2652019-06-13 09:05:41 +01004962
4963 prf->state = TLS12_PRF_STATE_SEED_SET;
4964
4965 return( PSA_SUCCESS );
4966}
4967
Janos Follath81550542019-06-13 14:26:34 +01004968static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4969 psa_algorithm_t hash_alg,
4970 const uint8_t *data,
4971 size_t data_length )
4972{
4973 psa_status_t status;
4974 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4975 return( PSA_ERROR_BAD_STATE );
4976
4977 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4978 if( status != PSA_SUCCESS )
4979 return( status );
4980
4981 prf->state = TLS12_PRF_STATE_KEY_SET;
4982
4983 return( PSA_SUCCESS );
4984}
4985
Janos Follath6660f0e2019-06-17 08:44:03 +01004986static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4987 psa_tls12_prf_key_derivation_t *prf,
4988 psa_algorithm_t hash_alg,
4989 const uint8_t *data,
4990 size_t data_length )
4991{
4992 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004993 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4994 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004995
4996 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4997 return( PSA_ERROR_INVALID_ARGUMENT );
4998
4999 /* Quoting RFC 4279, Section 2:
5000 *
5001 * The premaster secret is formed as follows: if the PSK is N octets
5002 * long, concatenate a uint16 with the value N, N zero octets, a second
5003 * uint16 with the value N, and the PSK itself.
5004 */
5005
Janos Follath40e13932019-06-26 13:22:29 +01005006 *cur++ = ( data_length >> 8 ) & 0xff;
5007 *cur++ = ( data_length >> 0 ) & 0xff;
5008 memset( cur, 0, data_length );
5009 cur += data_length;
5010 *cur++ = pms[0];
5011 *cur++ = pms[1];
5012 memcpy( cur, data, data_length );
5013 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005014
Janos Follath40e13932019-06-26 13:22:29 +01005015 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005016
5017 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5018 return( status );
5019}
5020
Janos Follath63028dd2019-06-13 09:15:47 +01005021static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5022 const uint8_t *data,
5023 size_t data_length )
5024{
5025 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5026 return( PSA_ERROR_BAD_STATE );
5027
Janos Follathd6dce9f2019-07-04 09:11:38 +01005028 if( data_length != 0 )
5029 {
5030 prf->label = mbedtls_calloc( 1, data_length );
5031 if( prf->label == NULL )
5032 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005033
Janos Follathd6dce9f2019-07-04 09:11:38 +01005034 memcpy( prf->label, data, data_length );
5035 prf->label_length = data_length;
5036 }
Janos Follath63028dd2019-06-13 09:15:47 +01005037
5038 prf->state = TLS12_PRF_STATE_LABEL_SET;
5039
5040 return( PSA_SUCCESS );
5041}
5042
Janos Follathb03233e2019-06-11 15:30:30 +01005043static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5044 psa_algorithm_t hash_alg,
5045 psa_key_derivation_step_t step,
5046 const uint8_t *data,
5047 size_t data_length )
5048{
Janos Follathb03233e2019-06-11 15:30:30 +01005049 switch( step )
5050 {
Janos Follathf08e2652019-06-13 09:05:41 +01005051 case PSA_KEY_DERIVATION_INPUT_SEED:
5052 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005053 case PSA_KEY_DERIVATION_INPUT_SECRET:
5054 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005055 case PSA_KEY_DERIVATION_INPUT_LABEL:
5056 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005057 default:
5058 return( PSA_ERROR_INVALID_ARGUMENT );
5059 }
5060}
Janos Follath6660f0e2019-06-17 08:44:03 +01005061
5062static psa_status_t psa_tls12_prf_psk_to_ms_input(
5063 psa_tls12_prf_key_derivation_t *prf,
5064 psa_algorithm_t hash_alg,
5065 psa_key_derivation_step_t step,
5066 const uint8_t *data,
5067 size_t data_length )
5068{
5069 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005070 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005071 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5072 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005073 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005074
5075 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5076}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005077#endif /* MBEDTLS_MD_C */
5078
Janos Follathb80a94e2019-06-12 15:54:46 +01005079static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005081 psa_key_derivation_step_t step,
5082 const uint8_t *data,
5083 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005084{
5085 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087
5088#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005089 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005090 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005092 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005093 step, data, data_length );
5094 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005095 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096 {
Janos Follathb03233e2019-06-11 15:30:30 +01005097 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5098 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5099 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005100 }
5101 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5102 {
5103 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5104 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5105 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005106 }
5107 else
5108#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005111 return( PSA_ERROR_BAD_STATE );
5112 }
5113
5114 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116 return( status );
5117}
5118
Janos Follath51f4a0f2019-06-14 11:35:55 +01005119psa_status_t psa_key_derivation_input_bytes(
5120 psa_key_derivation_operation_t *operation,
5121 psa_key_derivation_step_t step,
5122 const uint8_t *data,
5123 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005124{
Janos Follathc5621512019-06-14 11:27:57 +01005125 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5126 return( PSA_ERROR_INVALID_ARGUMENT );
5127
5128 return( psa_key_derivation_input_internal( operation, step,
5129 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005130}
5131
Janos Follath51f4a0f2019-06-14 11:35:55 +01005132psa_status_t psa_key_derivation_input_key(
5133 psa_key_derivation_operation_t *operation,
5134 psa_key_derivation_step_t step,
5135 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136{
5137 psa_key_slot_t *slot;
5138 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005139 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005140 PSA_KEY_USAGE_DERIVE,
5141 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005142 if( status != PSA_SUCCESS )
5143 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005144 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 return( PSA_ERROR_INVALID_ARGUMENT );
5146 /* Don't allow a key to be used as an input that is usually public.
5147 * This is debatable. It's ok from a cryptographic perspective to
5148 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005149 * the material should be dedicated to a particular input step,
5150 * otherwise this may allow the key to be used in an unintended way
5151 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005152 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005153 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005154 return( psa_key_derivation_input_internal( operation,
5155 step,
5156 slot->data.raw.data,
5157 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005159
5160
5161
5162/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005163/* Key agreement */
5164/****************************************************************/
5165
Gilles Peskinea05219c2018-11-16 16:02:56 +01005166#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005167static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5168 size_t peer_key_length,
5169 const mbedtls_ecp_keypair *our_key,
5170 uint8_t *shared_secret,
5171 size_t shared_secret_size,
5172 size_t *shared_secret_length )
5173{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005174 mbedtls_ecp_keypair *their_key = NULL;
5175 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005176 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005177 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005178
Jaeden Ameroccdce902019-01-10 11:42:27 +00005179 status = psa_import_ec_public_key(
5180 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5181 peer_key, peer_key_length,
5182 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005183 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005184 goto exit;
5185
Jaeden Amero97271b32019-01-10 19:38:51 +00005186 status = mbedtls_to_psa_error(
5187 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5188 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005189 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005190 status = mbedtls_to_psa_error(
5191 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5192 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005193 goto exit;
5194
Jaeden Amero97271b32019-01-10 19:38:51 +00005195 status = mbedtls_to_psa_error(
5196 mbedtls_ecdh_calc_secret( &ecdh,
5197 shared_secret_length,
5198 shared_secret, shared_secret_size,
5199 mbedtls_ctr_drbg_random,
5200 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005201
5202exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005203 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005204 mbedtls_ecp_keypair_free( their_key );
5205 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005206 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005207}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005208#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005209
Gilles Peskine01d718c2018-09-18 12:01:02 +02005210#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5211
Gilles Peskine0216fe12019-04-11 21:23:21 +02005212static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5213 psa_key_slot_t *private_key,
5214 const uint8_t *peer_key,
5215 size_t peer_key_length,
5216 uint8_t *shared_secret,
5217 size_t shared_secret_size,
5218 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005219{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005220 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005221 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005222#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005223 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005224 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005226 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5227 private_key->data.ecp,
5228 shared_secret, shared_secret_size,
5229 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005230#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005231 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005232 (void) private_key;
5233 (void) peer_key;
5234 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005235 (void) shared_secret;
5236 (void) shared_secret_size;
5237 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005238 return( PSA_ERROR_NOT_SUPPORTED );
5239 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005240}
5241
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005242/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005243 * to potentially free embedded data structures and wipe confidential data.
5244 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005246 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005247 psa_key_slot_t *private_key,
5248 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005249 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005250{
5251 psa_status_t status;
5252 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5253 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005255
5256 /* Step 1: run the secret agreement algorithm to generate the shared
5257 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005258 status = psa_key_agreement_raw_internal( ka_alg,
5259 private_key,
5260 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005261 shared_secret,
5262 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005263 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005264 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005265 goto exit;
5266
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005267 /* Step 2: set up the key derivation to generate key material from
5268 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005269 status = psa_key_derivation_input_internal( operation, step,
5270 shared_secret,
5271 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272
Gilles Peskine12313cd2018-06-20 00:20:32 +02005273exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005274 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005275 return( status );
5276}
5277
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005279 psa_key_derivation_step_t step,
5280 psa_key_handle_t private_key,
5281 const uint8_t *peer_key,
5282 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005283{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005284 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005285 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005287 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005288 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005289 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005290 if( status != PSA_SUCCESS )
5291 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005293 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005294 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005295 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005297 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005298}
5299
Gilles Peskinebe697d82019-05-16 18:00:41 +02005300psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5301 psa_key_handle_t private_key,
5302 const uint8_t *peer_key,
5303 size_t peer_key_length,
5304 uint8_t *output,
5305 size_t output_size,
5306 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005307{
5308 psa_key_slot_t *slot;
5309 psa_status_t status;
5310
5311 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5312 {
5313 status = PSA_ERROR_INVALID_ARGUMENT;
5314 goto exit;
5315 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005316 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005317 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005318 if( status != PSA_SUCCESS )
5319 goto exit;
5320
5321 status = psa_key_agreement_raw_internal( alg, slot,
5322 peer_key, peer_key_length,
5323 output, output_size,
5324 output_length );
5325
5326exit:
5327 if( status != PSA_SUCCESS )
5328 {
5329 /* If an error happens and is not handled properly, the output
5330 * may be used as a key to protect sensitive data. Arrange for such
5331 * a key to be random, which is likely to result in decryption or
5332 * verification errors. This is better than filling the buffer with
5333 * some constant data such as zeros, which would result in the data
5334 * being protected with a reproducible, easily knowable key.
5335 */
5336 psa_generate_random( output, output_size );
5337 *output_length = output_size;
5338 }
5339 return( status );
5340}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005341
5342
5343/****************************************************************/
5344/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005345/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005346
Gilles Peskine4c317f42018-07-12 01:24:09 +02005347psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005348 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005349{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005350 int ret;
5351 GUARD_MODULE_INITIALIZED;
5352
Gilles Peskinef181eca2019-08-07 13:49:00 +02005353 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5354 {
5355 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5356 output,
5357 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5358 if( ret != 0 )
5359 return( mbedtls_to_psa_error( ret ) );
5360 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5361 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5362 }
5363
Gilles Peskinee59236f2018-01-27 23:32:46 +01005364 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5365 return( mbedtls_to_psa_error( ret ) );
5366}
5367
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005368#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5369#include "mbedtls/entropy_poll.h"
5370
Gilles Peskine7228da22019-07-15 11:06:15 +02005371psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005372 size_t seed_size )
5373{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005374 if( global_data.initialized )
5375 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005376
5377 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5378 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5379 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5380 return( PSA_ERROR_INVALID_ARGUMENT );
5381
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005382 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005383}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005384#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005385
Gilles Peskinee56e8782019-04-26 17:34:02 +02005386#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5387static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5388 size_t domain_parameters_size,
5389 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005390{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005391 size_t i;
5392 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005393
Gilles Peskinee56e8782019-04-26 17:34:02 +02005394 if( domain_parameters_size == 0 )
5395 {
5396 *exponent = 65537;
5397 return( PSA_SUCCESS );
5398 }
5399
5400 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5401 * support values that fit in a 32-bit integer, which is larger than
5402 * int on just about every platform anyway. */
5403 if( domain_parameters_size > sizeof( acc ) )
5404 return( PSA_ERROR_NOT_SUPPORTED );
5405 for( i = 0; i < domain_parameters_size; i++ )
5406 acc = ( acc << 8 ) | domain_parameters[i];
5407 if( acc > INT_MAX )
5408 return( PSA_ERROR_NOT_SUPPORTED );
5409 *exponent = acc;
5410 return( PSA_SUCCESS );
5411}
5412#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5413
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005414static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005415 psa_key_slot_t *slot, size_t bits,
5416 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005417{
Gilles Peskine8e338702019-07-30 20:06:31 +02005418 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005419
Gilles Peskinee56e8782019-04-26 17:34:02 +02005420 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005421 return( PSA_ERROR_INVALID_ARGUMENT );
5422
Gilles Peskinee59236f2018-01-27 23:32:46 +01005423 if( key_type_is_raw_bytes( type ) )
5424 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005425 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005426 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5427 if( status != PSA_SUCCESS )
5428 return( status );
5429 status = psa_generate_random( slot->data.raw.data,
5430 slot->data.raw.bytes );
5431 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005432 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005433#if defined(MBEDTLS_DES_C)
5434 if( type == PSA_KEY_TYPE_DES )
5435 psa_des_set_key_parity( slot->data.raw.data,
5436 slot->data.raw.bytes );
5437#endif /* MBEDTLS_DES_C */
5438 }
5439 else
5440
5441#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005442 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005443 {
5444 mbedtls_rsa_context *rsa;
5445 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005446 int exponent;
5447 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5449 return( PSA_ERROR_NOT_SUPPORTED );
5450 /* Accept only byte-aligned keys, for the same reasons as
5451 * in psa_import_rsa_key(). */
5452 if( bits % 8 != 0 )
5453 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005454 status = psa_read_rsa_exponent( domain_parameters,
5455 domain_parameters_size,
5456 &exponent );
5457 if( status != PSA_SUCCESS )
5458 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005459 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5460 if( rsa == NULL )
5461 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5462 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5463 ret = mbedtls_rsa_gen_key( rsa,
5464 mbedtls_ctr_drbg_random,
5465 &global_data.ctr_drbg,
5466 (unsigned int) bits,
5467 exponent );
5468 if( ret != 0 )
5469 {
5470 mbedtls_rsa_free( rsa );
5471 mbedtls_free( rsa );
5472 return( mbedtls_to_psa_error( ret ) );
5473 }
5474 slot->data.rsa = rsa;
5475 }
5476 else
5477#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5478
5479#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005480 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481 {
5482 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5483 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5484 const mbedtls_ecp_curve_info *curve_info =
5485 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5486 mbedtls_ecp_keypair *ecp;
5487 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005488 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005489 return( PSA_ERROR_NOT_SUPPORTED );
5490 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5491 return( PSA_ERROR_NOT_SUPPORTED );
5492 if( curve_info->bit_size != bits )
5493 return( PSA_ERROR_INVALID_ARGUMENT );
5494 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5495 if( ecp == NULL )
5496 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5497 mbedtls_ecp_keypair_init( ecp );
5498 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5499 mbedtls_ctr_drbg_random,
5500 &global_data.ctr_drbg );
5501 if( ret != 0 )
5502 {
5503 mbedtls_ecp_keypair_free( ecp );
5504 mbedtls_free( ecp );
5505 return( mbedtls_to_psa_error( ret ) );
5506 }
5507 slot->data.ecp = ecp;
5508 }
5509 else
5510#endif /* MBEDTLS_ECP_C */
5511
5512 return( PSA_ERROR_NOT_SUPPORTED );
5513
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005514 return( PSA_SUCCESS );
5515}
Darryl Green0c6575a2018-11-07 16:05:30 +00005516
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005517psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005518 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005519{
5520 psa_status_t status;
5521 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005522 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005523
Gilles Peskine0f84d622019-09-12 19:03:13 +02005524 /* Reject any attempt to create a zero-length key so that we don't
5525 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5526 if( psa_get_key_bits( attributes ) == 0 )
5527 return( PSA_ERROR_INVALID_ARGUMENT );
5528
Gilles Peskinedf179142019-07-15 22:02:14 +02005529 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5530 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005531 if( status != PSA_SUCCESS )
5532 goto exit;
5533
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005534#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5535 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005536 {
Gilles Peskine11792082019-08-06 18:36:36 +02005537 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5538 size_t pubkey_length = 0; /* We don't support this feature yet */
5539 if( drv->key_management == NULL ||
5540 drv->key_management->p_generate == NULL )
5541 {
5542 status = PSA_ERROR_NOT_SUPPORTED;
5543 goto exit;
5544 }
5545 status = drv->key_management->p_generate(
5546 psa_get_se_driver_context( driver ),
5547 slot->data.se.slot_number, attributes,
5548 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005549 }
Gilles Peskine11792082019-08-06 18:36:36 +02005550 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005551#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005552 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005553 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005554 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005555 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005556 }
Gilles Peskine11792082019-08-06 18:36:36 +02005557
5558exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005559 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005560 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005561 if( status != PSA_SUCCESS )
5562 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005563 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005564 *handle = 0;
5565 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005566 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005567}
5568
5569
Gilles Peskinee59236f2018-01-27 23:32:46 +01005570
5571/****************************************************************/
5572/* Module setup */
5573/****************************************************************/
5574
Gilles Peskine5e769522018-11-20 21:59:56 +01005575psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5576 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5577 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5578{
5579 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5580 return( PSA_ERROR_BAD_STATE );
5581 global_data.entropy_init = entropy_init;
5582 global_data.entropy_free = entropy_free;
5583 return( PSA_SUCCESS );
5584}
5585
Gilles Peskinee59236f2018-01-27 23:32:46 +01005586void mbedtls_psa_crypto_free( void )
5587{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005588 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005589 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5590 {
5591 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005592 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005593 }
5594 /* Wipe all remaining data, including configuration.
5595 * In particular, this sets all state indicator to the value
5596 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005597 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005598#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005599 /* Unregister all secure element drivers, so that we restart from
5600 * a pristine state. */
5601 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005602#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005603}
5604
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005605#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5606/** Recover a transaction that was interrupted by a power failure.
5607 *
5608 * This function is called during initialization, before psa_crypto_init()
5609 * returns. If this function returns a failure status, the initialization
5610 * fails.
5611 */
5612static psa_status_t psa_crypto_recover_transaction(
5613 const psa_crypto_transaction_t *transaction )
5614{
5615 switch( transaction->unknown.type )
5616 {
5617 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5618 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005619 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005620 * is implemented.
5621 * https://github.com/ARMmbed/mbed-crypto/issues/218
5622 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005623 default:
5624 /* We found an unsupported transaction in the storage.
5625 * We don't know what state the storage is in. Give up. */
5626 return( PSA_ERROR_STORAGE_FAILURE );
5627 }
5628}
5629#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5630
Gilles Peskinee59236f2018-01-27 23:32:46 +01005631psa_status_t psa_crypto_init( void )
5632{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005633 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634 const unsigned char drbg_seed[] = "PSA";
5635
Gilles Peskinec6b69072018-11-20 21:42:52 +01005636 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005637 if( global_data.initialized != 0 )
5638 return( PSA_SUCCESS );
5639
Gilles Peskine5e769522018-11-20 21:59:56 +01005640 /* Set default configuration if
5641 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5642 if( global_data.entropy_init == NULL )
5643 global_data.entropy_init = mbedtls_entropy_init;
5644 if( global_data.entropy_free == NULL )
5645 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005646
Gilles Peskinec6b69072018-11-20 21:42:52 +01005647 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005648 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005649#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5650 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5651 /* The PSA entropy injection feature depends on using NV seed as an entropy
5652 * source. Add NV seed as an entropy source for PSA entropy injection. */
5653 mbedtls_entropy_add_source( &global_data.entropy,
5654 mbedtls_nv_seed_poll, NULL,
5655 MBEDTLS_ENTROPY_BLOCK_SIZE,
5656 MBEDTLS_ENTROPY_SOURCE_STRONG );
5657#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005658 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005659 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005660 status = mbedtls_to_psa_error(
5661 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5662 mbedtls_entropy_func,
5663 &global_data.entropy,
5664 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5665 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005666 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005667 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005668
Gilles Peskine66fb1262018-12-10 16:29:04 +01005669 status = psa_initialize_key_slots( );
5670 if( status != PSA_SUCCESS )
5671 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005672
Gilles Peskine4b734222019-07-24 15:56:31 +02005673#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005674 status = psa_crypto_load_transaction( );
5675 if( status == PSA_SUCCESS )
5676 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005677 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5678 if( status != PSA_SUCCESS )
5679 goto exit;
5680 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005681 }
5682 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5683 {
5684 /* There's no transaction to complete. It's all good. */
5685 status = PSA_SUCCESS;
5686 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005687#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005688
Gilles Peskinec6b69072018-11-20 21:42:52 +01005689 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005690 global_data.initialized = 1;
5691
5692exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005693 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005694 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005695 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005696}
5697
5698#endif /* MBEDTLS_PSA_CRYPTO_C */