blob: e043d7000ed8b5acaca4e06fa9227cbf6b93207e [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 Amero6b196002019-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)
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#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)
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#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:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
597static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000772 {
773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000777 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 else
779#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200792
793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000800 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100801}
802
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100815 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
958 {
959 /* No key material to clean. */
960 }
961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100997static void psa_abort_operations_using_key( psa_key_slot_t *slot )
998{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200999 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001000 (void) slot;
1001}
1002
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003/** Completely wipe a slot in memory, including its policy.
1004 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001005psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006{
1007 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001008 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001009 /* At this point, key material and other type-specific content has
1010 * been wiped. Clear remaining metadata. We can call memset and not
1011 * zeroize because the metadata is not particularly sensitive. */
1012 memset( slot, 0, sizeof( *slot ) );
1013 return( status );
1014}
1015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001018 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001019 psa_status_t status = PSA_SUCCESS;
1020 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001021#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1022 psa_se_drv_table_entry_t *driver;
1023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024
Gilles Peskinec5487a82018-12-03 18:08:14 +01001025 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001026 if( status != PSA_SUCCESS )
1027 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
1029#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001031 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001033 /* For a key in a secure element, we need to do three things:
1034 * remove the key file in internal storage, destroy the
1035 * key inside the secure element, and update the driver's
1036 * persistent data. Start a transaction that will encompass these
1037 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001041 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 status = psa_crypto_save_transaction( );
1043 if( status != PSA_SUCCESS )
1044 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001045 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 /* TOnogrepDO: destroy what can be destroyed anyway */
1047 return( status );
1048 }
1049
Gilles Peskine354f7672019-07-12 23:46:38 +02001050 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
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 Peskine8e338702019-07-30 20:06:31 +02001055 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001057 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001058 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001059 }
1060#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001061
Gilles Peskinefc762652019-07-22 19:30:34 +02001062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1063 if( driver != NULL )
1064 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001065 psa_status_t status2;
1066 status = psa_save_se_persistent_data( driver );
1067 status2 = psa_crypto_stop_transaction( );
1068 if( status == PSA_SUCCESS )
1069 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001070 if( status != PSA_SUCCESS )
1071 {
1072 /* TOnogrepDO: destroy what can be destroyed anyway */
1073 return( status );
1074 }
1075 }
1076#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1077
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001079 if( status != PSA_SUCCESS )
1080 return( status );
1081 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082}
1083
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001084void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1085{
Gilles Peskineb699f072019-04-26 16:06:02 +02001086 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087 memset( attributes, 0, sizeof( *attributes ) );
1088}
1089
Gilles Peskineb699f072019-04-26 16:06:02 +02001090psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1091 psa_key_type_t type,
1092 const uint8_t *data,
1093 size_t data_length )
1094{
1095 uint8_t *copy = NULL;
1096
1097 if( data_length != 0 )
1098 {
1099 copy = mbedtls_calloc( 1, data_length );
1100 if( copy == NULL )
1101 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1102 memcpy( copy, data, data_length );
1103 }
1104 /* After this point, this function is guaranteed to succeed, so it
1105 * can start modifying `*attributes`. */
1106
1107 if( attributes->domain_parameters != NULL )
1108 {
1109 mbedtls_free( attributes->domain_parameters );
1110 attributes->domain_parameters = NULL;
1111 attributes->domain_parameters_size = 0;
1112 }
1113
1114 attributes->domain_parameters = copy;
1115 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001116 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001117 return( PSA_SUCCESS );
1118}
1119
1120psa_status_t psa_get_key_domain_parameters(
1121 const psa_key_attributes_t *attributes,
1122 uint8_t *data, size_t data_size, size_t *data_length )
1123{
1124 if( attributes->domain_parameters_size > data_size )
1125 return( PSA_ERROR_BUFFER_TOO_SMALL );
1126 *data_length = attributes->domain_parameters_size;
1127 if( attributes->domain_parameters_size != 0 )
1128 memcpy( data, attributes->domain_parameters,
1129 attributes->domain_parameters_size );
1130 return( PSA_SUCCESS );
1131}
1132
1133#if defined(MBEDTLS_RSA_C)
1134static psa_status_t psa_get_rsa_public_exponent(
1135 const mbedtls_rsa_context *rsa,
1136 psa_key_attributes_t *attributes )
1137{
1138 mbedtls_mpi mpi;
1139 int ret;
1140 uint8_t *buffer = NULL;
1141 size_t buflen;
1142 mbedtls_mpi_init( &mpi );
1143
1144 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1145 if( ret != 0 )
1146 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001147 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1148 {
1149 /* It's the default value, which is reported as an empty string,
1150 * so there's nothing to do. */
1151 goto exit;
1152 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001153
1154 buflen = mbedtls_mpi_size( &mpi );
1155 buffer = mbedtls_calloc( 1, buflen );
1156 if( buffer == NULL )
1157 {
1158 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1159 goto exit;
1160 }
1161 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1162 if( ret != 0 )
1163 goto exit;
1164 attributes->domain_parameters = buffer;
1165 attributes->domain_parameters_size = buflen;
1166
1167exit:
1168 mbedtls_mpi_free( &mpi );
1169 if( ret != 0 )
1170 mbedtls_free( buffer );
1171 return( mbedtls_to_psa_error( ret ) );
1172}
1173#endif /* MBEDTLS_RSA_C */
1174
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001175/** Retrieve all the publicly-accessible attributes of a key.
1176 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1178 psa_key_attributes_t *attributes )
1179{
1180 psa_key_slot_t *slot;
1181 psa_status_t status;
1182
1183 psa_reset_key_attributes( attributes );
1184
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001185 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001186 if( status != PSA_SUCCESS )
1187 return( status );
1188
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001189 attributes->core = slot->attr;
Gilles Peskineb699f072019-04-26 16:06:02 +02001190
Gilles Peskine8e338702019-07-30 20:06:31 +02001191 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 {
1193#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001194 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001195 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001196#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1197 /* TOnogrepDO: reporting the public exponent for opaque keys
1198 * is not yet implemented. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001199 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001200 break;
1201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001202 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1203 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001204#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 default:
1206 /* Nothing else to do. */
1207 break;
1208 }
1209
1210 if( status != PSA_SUCCESS )
1211 psa_reset_key_attributes( attributes );
1212 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001213}
1214
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001215#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001216static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1217 unsigned char *buf, size_t size )
1218{
1219 int ret;
1220 unsigned char *c;
1221 size_t len = 0;
1222
1223 c = buf + size;
1224
1225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1226
1227 return( (int) len );
1228}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001229#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001230
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001231static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1232 uint8_t *data,
1233 size_t data_size,
1234 size_t *data_length,
1235 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236{
Gilles Peskine5d309672019-07-12 23:47:28 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1238 const psa_drv_se_t *drv;
1239 psa_drv_se_context_t *drv_context;
1240#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1241
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001242 *data_length = 0;
1243
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001245 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001246
Gilles Peskine5d309672019-07-12 23:47:28 +02001247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001248 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001249 {
1250 psa_drv_se_export_key_t method;
1251 if( drv->key_management == NULL )
1252 return( PSA_ERROR_NOT_SUPPORTED );
1253 method = ( export_public_key ?
1254 drv->key_management->p_export_public :
1255 drv->key_management->p_export );
1256 if( method == NULL )
1257 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001258 return( method( drv_context,
1259 slot->data.se.slot_number,
1260 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001261 }
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1263
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 {
1266 if( slot->data.raw.bytes > data_size )
1267 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001268 if( data_size != 0 )
1269 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001270 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001271 memset( data + slot->data.raw.bytes, 0,
1272 data_size - slot->data.raw.bytes );
1273 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274 *data_length = slot->data.raw.bytes;
1275 return( PSA_SUCCESS );
1276 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001277#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001278 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001279 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001280 psa_status_t status;
1281
Gilles Peskineb46bef22019-07-30 21:32:04 +02001282 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001283 if( bytes > data_size )
1284 return( PSA_ERROR_BUFFER_TOO_SMALL );
1285 status = mbedtls_to_psa_error(
1286 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1287 if( status != PSA_SUCCESS )
1288 return( status );
1289 memset( data + bytes, 0, data_size - bytes );
1290 *data_length = bytes;
1291 return( PSA_SUCCESS );
1292 }
1293#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294 else
Moran Peker17e36e12018-05-02 12:55:20 +03001295 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001296#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001297 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1298 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001299 {
Moran Pekera998bc62018-04-16 18:16:20 +03001300 mbedtls_pk_context pk;
1301 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001302 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001303 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001304#if defined(MBEDTLS_RSA_C)
1305 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001306 pk.pk_info = &mbedtls_rsa_info;
1307 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001308#else
1309 return( PSA_ERROR_NOT_SUPPORTED );
1310#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001311 }
1312 else
1313 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001314#if defined(MBEDTLS_ECP_C)
1315 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001316 pk.pk_info = &mbedtls_eckey_info;
1317 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001318#else
1319 return( PSA_ERROR_NOT_SUPPORTED );
1320#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001321 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001323 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001324 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001325 }
Moran Peker17e36e12018-05-02 12:55:20 +03001326 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001327 {
Moran Peker17e36e12018-05-02 12:55:20 +03001328 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001329 }
Moran Peker60364322018-04-29 11:34:58 +03001330 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001331 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001332 /* If data_size is 0 then data may be NULL and then the
1333 * call to memset would have undefined behavior. */
1334 if( data_size != 0 )
1335 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001336 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001337 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001338 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1339 * Move the data to the beginning and erase remaining data
1340 * at the original location. */
1341 if( 2 * (size_t) ret <= data_size )
1342 {
1343 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001344 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001345 }
1346 else if( (size_t) ret < data_size )
1347 {
1348 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001349 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001350 }
Moran Pekera998bc62018-04-16 18:16:20 +03001351 *data_length = ret;
1352 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001353 }
1354 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001355#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001356 {
1357 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001358 it is valid for a special-purpose implementation to omit
1359 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001360 return( PSA_ERROR_NOT_SUPPORTED );
1361 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362 }
1363}
1364
Gilles Peskinec5487a82018-12-03 18:08:14 +01001365psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001366 uint8_t *data,
1367 size_t data_size,
1368 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001369{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001370 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001371 psa_status_t status;
1372
1373 /* Set the key to empty now, so that even when there are errors, we always
1374 * set data_length to a value between 0 and data_size. On error, setting
1375 * the key to empty is a good choice because an empty key representation is
1376 * unlikely to be accepted anywhere. */
1377 *data_length = 0;
1378
1379 /* Export requires the EXPORT flag. There is an exception for public keys,
1380 * which don't require any flag, but psa_get_key_from_slot takes
1381 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001382 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001383 if( status != PSA_SUCCESS )
1384 return( status );
1385 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001386 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001387}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388
Gilles Peskinec5487a82018-12-03 18:08:14 +01001389psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001390 uint8_t *data,
1391 size_t data_size,
1392 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001393{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001394 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001395 psa_status_t status;
1396
1397 /* Set the key to empty now, so that even when there are errors, we always
1398 * set data_length to a value between 0 and data_size. On error, setting
1399 * the key to empty is a good choice because an empty key representation is
1400 * unlikely to be accepted anywhere. */
1401 *data_length = 0;
1402
1403 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001404 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001405 if( status != PSA_SUCCESS )
1406 return( status );
1407 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001408 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001409}
1410
Gilles Peskine91e8c332019-08-02 19:19:39 +02001411#if defined(static_assert)
1412static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1413 "One or more key attribute flag is listed as both external-only and dual-use" );
1414static_assert( ( MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1415 "One or more key attribute flag is listed as both external-only and dual-use" );
1416static_assert( ( MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
1417 "One or more key attribute flag is listed as both internal-only and external-only" );
1418#endif
1419
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001420/** Validate that a key policy is internally well-formed.
1421 *
1422 * This function only rejects invalid policies. It does not validate the
1423 * consistency of the policy with respect to other attributes of the key
1424 * such as the key type.
1425 */
1426static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001427{
1428 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001429 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001430 PSA_KEY_USAGE_ENCRYPT |
1431 PSA_KEY_USAGE_DECRYPT |
1432 PSA_KEY_USAGE_SIGN |
1433 PSA_KEY_USAGE_VERIFY |
1434 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1435 return( PSA_ERROR_INVALID_ARGUMENT );
1436
Gilles Peskine4747d192019-04-17 15:05:45 +02001437 return( PSA_SUCCESS );
1438}
1439
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001440/** Validate the internal consistency of key attributes.
1441 *
1442 * This function only rejects invalid attribute values. If does not
1443 * validate the consistency of the attributes with any key data that may
1444 * be involved in the creation of the key.
1445 *
1446 * Call this function early in the key creation process.
1447 *
1448 * \param[in] attributes Key attributes for the new key.
1449 * \param[out] p_drv On any return, the driver for the key, if any.
1450 * NULL for a transparent key.
1451 *
1452 */
1453static psa_status_t psa_validate_key_attributes(
1454 const psa_key_attributes_t *attributes,
1455 psa_se_drv_table_entry_t **p_drv )
1456{
1457 psa_status_t status;
1458
1459 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1460 {
1461 status = psa_validate_persistent_key_parameters(
1462 attributes->core.lifetime, attributes->core.id,
1463 p_drv, 1 );
1464 if( status != PSA_SUCCESS )
1465 return( status );
1466 }
1467
1468 status = psa_validate_key_policy( &attributes->core.policy );
1469 if( status != PSA_SUCCESS )
1470 return( status );
1471
1472 /* Refuse to create overly large keys.
1473 * Note that this doesn't trigger on import if the attributes don't
1474 * explicitly specify a size (so psa_get_key_bits returns 0), so
1475 * psa_import_key() needs its own checks. */
1476 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1477 return( PSA_ERROR_NOT_SUPPORTED );
1478
Gilles Peskine91e8c332019-08-02 19:19:39 +02001479 /* Reject invalid flags. These should not be reachable through the API. */
1480 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1481 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1482 return( PSA_ERROR_INVALID_ARGUMENT );
1483
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001484 return( PSA_SUCCESS );
1485}
1486
Gilles Peskine4747d192019-04-17 15:05:45 +02001487/** Prepare a key slot to receive key material.
1488 *
1489 * This function allocates a key slot and sets its metadata.
1490 *
1491 * If this function fails, call psa_fail_key_creation().
1492 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001493 * This function is intended to be used as follows:
1494 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1495 * it with the specified attributes, and assign it a handle.
1496 * -# Populate the slot with the key material.
1497 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1498 * In case of failure at any step, stop the sequence and call
1499 * psa_fail_key_creation().
1500 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001501 * \param[in] attributes Key attributes for the new key.
1502 * \param[out] handle On success, a handle for the allocated slot.
1503 * \param[out] p_slot On success, a pointer to the prepared slot.
1504 * \param[out] p_drv On any return, the driver for the key, if any.
1505 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001506 *
1507 * \retval #PSA_SUCCESS
1508 * The key slot is ready to receive key material.
1509 * \return If this function fails, the key slot is an invalid state.
1510 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001511 */
1512static psa_status_t psa_start_key_creation(
1513 const psa_key_attributes_t *attributes,
1514 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001515 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001516 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001517{
1518 psa_status_t status;
1519 psa_key_slot_t *slot;
1520
Gilles Peskine011e4282019-06-26 18:34:38 +02001521 *p_drv = NULL;
1522
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001523 status = psa_validate_key_attributes( attributes, p_drv );
1524 if( status != PSA_SUCCESS )
1525 return( status );
1526
Gilles Peskine267c6562019-05-27 19:01:54 +02001527 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001528 if( status != PSA_SUCCESS )
1529 return( status );
1530 slot = *p_slot;
1531
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001532 /* We're storing the declared bit-size of the key. It's up to each
1533 * creation mechanism to verify that this information is correct.
1534 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001535 * an input (generate, device) but not for those where the bit-size
1536 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001537
1538 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001539
Gilles Peskine91e8c332019-08-02 19:19:39 +02001540 /* Erase external-only flags from the internal copy. To access
1541 * external-only flags, query `attributes`. */
1542 slot->attr.flags |= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
1543
Gilles Peskinecbaff462019-07-12 23:46:04 +02001544#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001545 /* For a key in a secure element, we need to do three things:
1546 * create the key file in internal storage, create the
1547 * key inside the secure element, and update the driver's
1548 * persistent data. Start a transaction that will encompass these
1549 * three actions. */
1550 /* The first thing to do is to find a slot number for the new key.
1551 * We save the slot number in persistent storage as part of the
1552 * transaction data. It will be needed to recover if the power
1553 * fails during the key creation process, to clean up on the secure
1554 * element side after restarting. Obtaining a slot number from the
1555 * secure element driver updates its persistent state, but we do not yet
1556 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001557 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001558 if( *p_drv != NULL )
1559 {
1560 status = psa_find_se_slot_for_key( attributes, *p_drv,
1561 &slot->data.se.slot_number );
1562 if( status != PSA_SUCCESS )
1563 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001564 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001565 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001566 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001567 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001568 status = psa_crypto_save_transaction( );
1569 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001570 {
1571 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001572 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001573 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001574 }
1575#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1576
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 return( status );
1578}
1579
1580/** Finalize the creation of a key once its key material has been set.
1581 *
1582 * This entails writing the key to persistent storage.
1583 *
1584 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001585 * See the documentation of psa_start_key_creation() for the intended use
1586 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001587 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001588 * \param[in,out] slot Pointer to the slot with key material.
1589 * \param[in] driver The secure element driver for the key,
1590 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001591 *
1592 * \retval #PSA_SUCCESS
1593 * The key was successfully created. The handle is now valid.
1594 * \return If this function fails, the key slot is an invalid state.
1595 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001596 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001597static psa_status_t psa_finish_key_creation(
1598 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001599 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001600{
1601 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001602 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001603 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001604
1605#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001606 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001607 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001608#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1609 if( driver != NULL )
1610 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001611 psa_se_key_data_storage_t data;
1612#if defined(static_assert)
1613 static_assert( sizeof( slot->data.se.slot_number ) ==
1614 sizeof( data.slot_number ),
1615 "Slot number size does not match psa_se_key_data_storage_t" );
1616 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1617 "Bit-size size does not match psa_se_key_data_storage_t" );
1618#endif
1619 memcpy( &data.slot_number, &slot->data.se.slot_number,
1620 sizeof( slot->data.se.slot_number ) );
1621 memcpy( &data.bits, &slot->attr.bits,
1622 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001623 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001624 (uint8_t*) &data,
1625 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001626 }
1627 else
1628#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1629 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001630 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001631 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001632 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001633 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1634 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001635 if( buffer == NULL && buffer_size != 0 )
1636 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1637 status = psa_internal_export_key( slot,
1638 buffer, buffer_size, &length,
1639 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001640 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001641 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001642 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001643
Gilles Peskine1df83d42019-07-23 16:13:14 +02001644 if( buffer_size != 0 )
1645 mbedtls_platform_zeroize( buffer, buffer_size );
1646 mbedtls_free( buffer );
1647 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 }
1649#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1650
Gilles Peskinecbaff462019-07-12 23:46:04 +02001651#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1652 if( driver != NULL )
1653 {
1654 status = psa_save_se_persistent_data( driver );
1655 if( status != PSA_SUCCESS )
1656 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001657 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001658 return( status );
1659 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001660 status = psa_crypto_stop_transaction( );
1661 if( status != PSA_SUCCESS )
1662 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001663 }
1664#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1665
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 return( status );
1667}
1668
1669/** Abort the creation of a key.
1670 *
1671 * You may call this function after calling psa_start_key_creation(),
1672 * or after psa_finish_key_creation() fails. In other circumstances, this
1673 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001674 * See the documentation of psa_start_key_creation() for the intended use
1675 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001676 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001677 * \param[in,out] slot Pointer to the slot with key material.
1678 * \param[in] driver The secure element driver for the key,
1679 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001681static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001682 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001683{
Gilles Peskine011e4282019-06-26 18:34:38 +02001684 (void) driver;
1685
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 if( slot == NULL )
1687 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001688
1689#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1690 /* TOnogrepDO: If the key has already been created in the secure
1691 * element, and the failure happened later (when saving metadata
1692 * to internal storage), we need to destroy the key in the secure
1693 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001694
1695 /* Abort the ongoing transaction if any. We already did what it
1696 * takes to undo any partial creation. All that's left is to update
1697 * the transaction data itself. */
1698 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001699#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1700
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 psa_wipe_key_slot( slot );
1702}
1703
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001704/** Validate optional attributes during key creation.
1705 *
1706 * Some key attributes are optional during key creation. If they are
1707 * specified in the attributes structure, check that they are consistent
1708 * with the data in the slot.
1709 *
1710 * This function should be called near the end of key creation, after
1711 * the slot in memory is fully populated but before saving persistent data.
1712 */
1713static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001714 const psa_key_slot_t *slot,
1715 const psa_key_attributes_t *attributes )
1716{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001717 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001718 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001719 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001720 return( PSA_ERROR_INVALID_ARGUMENT );
1721 }
1722
1723 if( attributes->domain_parameters_size != 0 )
1724 {
1725#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001726 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001727 {
1728 mbedtls_mpi actual, required;
1729 int ret;
1730 mbedtls_mpi_init( &actual );
1731 mbedtls_mpi_init( &required );
1732 ret = mbedtls_rsa_export( slot->data.rsa,
1733 NULL, NULL, NULL, NULL, &actual );
1734 if( ret != 0 )
1735 goto rsa_exit;
1736 ret = mbedtls_mpi_read_binary( &required,
1737 attributes->domain_parameters,
1738 attributes->domain_parameters_size );
1739 if( ret != 0 )
1740 goto rsa_exit;
1741 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1742 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1743 rsa_exit:
1744 mbedtls_mpi_free( &actual );
1745 mbedtls_mpi_free( &required );
1746 if( ret != 0)
1747 return( mbedtls_to_psa_error( ret ) );
1748 }
1749 else
1750#endif
1751 {
1752 return( PSA_ERROR_INVALID_ARGUMENT );
1753 }
1754 }
1755
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001756 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001757 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001759 return( PSA_ERROR_INVALID_ARGUMENT );
1760 }
1761
1762 return( PSA_SUCCESS );
1763}
1764
Gilles Peskine4747d192019-04-17 15:05:45 +02001765psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001767 size_t data_length,
1768 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001769{
1770 psa_status_t status;
1771 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001772 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001775 if( status != PSA_SUCCESS )
1776 goto exit;
1777
Gilles Peskine5d309672019-07-12 23:47:28 +02001778#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1779 if( driver != NULL )
1780 {
1781 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001782 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001783 if( drv->key_management == NULL ||
1784 drv->key_management->p_import == NULL )
1785 {
1786 status = PSA_ERROR_NOT_SUPPORTED;
1787 goto exit;
1788 }
1789 status = drv->key_management->p_import(
1790 psa_get_se_driver_context( driver ),
1791 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001792 slot->attr.lifetime, slot->attr.type,
1793 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001794 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001795 &bits );
1796 if( status != PSA_SUCCESS )
1797 goto exit;
1798 if( bits > PSA_MAX_KEY_BITS )
1799 {
1800 status = PSA_ERROR_NOT_SUPPORTED;
1801 goto exit;
1802 }
1803 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001804 }
1805 else
1806#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1807 {
1808 status = psa_import_key_into_slot( slot, data, data_length );
1809 if( status != PSA_SUCCESS )
1810 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001811 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001812 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001813 if( status != PSA_SUCCESS )
1814 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001817exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 if( status != PSA_SUCCESS )
1819 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001820 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 *handle = 0;
1822 }
1823 return( status );
1824}
1825
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001826static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001827 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001828{
1829 psa_status_t status;
1830 uint8_t *buffer = NULL;
1831 size_t buffer_size = 0;
1832 size_t length;
1833
Gilles Peskine8e338702019-07-30 20:06:31 +02001834 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001835 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001836 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001837 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001838 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001839 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1840 if( status != PSA_SUCCESS )
1841 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001842 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001843 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001844
1845exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001846 if( buffer_size != 0 )
1847 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001848 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001849 return( status );
1850}
1851
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001852psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1853 const psa_key_attributes_t *specified_attributes,
1854 psa_key_handle_t *target_handle )
1855{
1856 psa_status_t status;
1857 psa_key_slot_t *source_slot = NULL;
1858 psa_key_slot_t *target_slot = NULL;
1859 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001860 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001861
Gilles Peskine28f8f302019-07-24 13:30:31 +02001862 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001863 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001864 if( status != PSA_SUCCESS )
1865 goto exit;
1866
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001867 status = psa_validate_optional_attributes( source_slot,
1868 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001869 if( status != PSA_SUCCESS )
1870 goto exit;
1871
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001872 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001873 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001874 if( status != PSA_SUCCESS )
1875 goto exit;
1876
1877 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001879 if( status != PSA_SUCCESS )
1880 goto exit;
1881
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1883 if( driver != NULL )
1884 {
1885 /* Copying to a secure element is not implemented yet. */
1886 status = PSA_ERROR_NOT_SUPPORTED;
1887 goto exit;
1888 }
1889#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1890
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001891 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892 if( status != PSA_SUCCESS )
1893 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001894
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001896exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001897 if( status != PSA_SUCCESS )
1898 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001899 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001900 *target_handle = 0;
1901 }
1902 return( status );
1903}
1904
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001905
1906
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001907/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001908/* Message digests */
1909/****************************************************************/
1910
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001911static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001912{
1913 switch( alg )
1914 {
1915#if defined(MBEDTLS_MD2_C)
1916 case PSA_ALG_MD2:
1917 return( &mbedtls_md2_info );
1918#endif
1919#if defined(MBEDTLS_MD4_C)
1920 case PSA_ALG_MD4:
1921 return( &mbedtls_md4_info );
1922#endif
1923#if defined(MBEDTLS_MD5_C)
1924 case PSA_ALG_MD5:
1925 return( &mbedtls_md5_info );
1926#endif
1927#if defined(MBEDTLS_RIPEMD160_C)
1928 case PSA_ALG_RIPEMD160:
1929 return( &mbedtls_ripemd160_info );
1930#endif
1931#if defined(MBEDTLS_SHA1_C)
1932 case PSA_ALG_SHA_1:
1933 return( &mbedtls_sha1_info );
1934#endif
1935#if defined(MBEDTLS_SHA256_C)
1936 case PSA_ALG_SHA_224:
1937 return( &mbedtls_sha224_info );
1938 case PSA_ALG_SHA_256:
1939 return( &mbedtls_sha256_info );
1940#endif
1941#if defined(MBEDTLS_SHA512_C)
1942 case PSA_ALG_SHA_384:
1943 return( &mbedtls_sha384_info );
1944 case PSA_ALG_SHA_512:
1945 return( &mbedtls_sha512_info );
1946#endif
1947 default:
1948 return( NULL );
1949 }
1950}
1951
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001952psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1953{
1954 switch( operation->alg )
1955 {
Gilles Peskine81736312018-06-26 15:04:31 +02001956 case 0:
1957 /* The object has (apparently) been initialized but it is not
1958 * in use. It's ok to call abort on such an object, and there's
1959 * nothing to do. */
1960 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001961#if defined(MBEDTLS_MD2_C)
1962 case PSA_ALG_MD2:
1963 mbedtls_md2_free( &operation->ctx.md2 );
1964 break;
1965#endif
1966#if defined(MBEDTLS_MD4_C)
1967 case PSA_ALG_MD4:
1968 mbedtls_md4_free( &operation->ctx.md4 );
1969 break;
1970#endif
1971#if defined(MBEDTLS_MD5_C)
1972 case PSA_ALG_MD5:
1973 mbedtls_md5_free( &operation->ctx.md5 );
1974 break;
1975#endif
1976#if defined(MBEDTLS_RIPEMD160_C)
1977 case PSA_ALG_RIPEMD160:
1978 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1979 break;
1980#endif
1981#if defined(MBEDTLS_SHA1_C)
1982 case PSA_ALG_SHA_1:
1983 mbedtls_sha1_free( &operation->ctx.sha1 );
1984 break;
1985#endif
1986#if defined(MBEDTLS_SHA256_C)
1987 case PSA_ALG_SHA_224:
1988 case PSA_ALG_SHA_256:
1989 mbedtls_sha256_free( &operation->ctx.sha256 );
1990 break;
1991#endif
1992#if defined(MBEDTLS_SHA512_C)
1993 case PSA_ALG_SHA_384:
1994 case PSA_ALG_SHA_512:
1995 mbedtls_sha512_free( &operation->ctx.sha512 );
1996 break;
1997#endif
1998 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001999 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002000 }
2001 operation->alg = 0;
2002 return( PSA_SUCCESS );
2003}
2004
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002005psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002006 psa_algorithm_t alg )
2007{
2008 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002009
2010 /* A context must be freshly initialized before it can be set up. */
2011 if( operation->alg != 0 )
2012 {
2013 return( PSA_ERROR_BAD_STATE );
2014 }
2015
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002016 switch( alg )
2017 {
2018#if defined(MBEDTLS_MD2_C)
2019 case PSA_ALG_MD2:
2020 mbedtls_md2_init( &operation->ctx.md2 );
2021 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2022 break;
2023#endif
2024#if defined(MBEDTLS_MD4_C)
2025 case PSA_ALG_MD4:
2026 mbedtls_md4_init( &operation->ctx.md4 );
2027 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2028 break;
2029#endif
2030#if defined(MBEDTLS_MD5_C)
2031 case PSA_ALG_MD5:
2032 mbedtls_md5_init( &operation->ctx.md5 );
2033 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2034 break;
2035#endif
2036#if defined(MBEDTLS_RIPEMD160_C)
2037 case PSA_ALG_RIPEMD160:
2038 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2039 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2040 break;
2041#endif
2042#if defined(MBEDTLS_SHA1_C)
2043 case PSA_ALG_SHA_1:
2044 mbedtls_sha1_init( &operation->ctx.sha1 );
2045 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2046 break;
2047#endif
2048#if defined(MBEDTLS_SHA256_C)
2049 case PSA_ALG_SHA_224:
2050 mbedtls_sha256_init( &operation->ctx.sha256 );
2051 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2052 break;
2053 case PSA_ALG_SHA_256:
2054 mbedtls_sha256_init( &operation->ctx.sha256 );
2055 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2056 break;
2057#endif
2058#if defined(MBEDTLS_SHA512_C)
2059 case PSA_ALG_SHA_384:
2060 mbedtls_sha512_init( &operation->ctx.sha512 );
2061 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2062 break;
2063 case PSA_ALG_SHA_512:
2064 mbedtls_sha512_init( &operation->ctx.sha512 );
2065 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2066 break;
2067#endif
2068 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002069 return( PSA_ALG_IS_HASH( alg ) ?
2070 PSA_ERROR_NOT_SUPPORTED :
2071 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002072 }
2073 if( ret == 0 )
2074 operation->alg = alg;
2075 else
2076 psa_hash_abort( operation );
2077 return( mbedtls_to_psa_error( ret ) );
2078}
2079
2080psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2081 const uint8_t *input,
2082 size_t input_length )
2083{
2084 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002085
2086 /* Don't require hash implementations to behave correctly on a
2087 * zero-length input, which may have an invalid pointer. */
2088 if( input_length == 0 )
2089 return( PSA_SUCCESS );
2090
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002091 switch( operation->alg )
2092 {
2093#if defined(MBEDTLS_MD2_C)
2094 case PSA_ALG_MD2:
2095 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2096 input, input_length );
2097 break;
2098#endif
2099#if defined(MBEDTLS_MD4_C)
2100 case PSA_ALG_MD4:
2101 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2102 input, input_length );
2103 break;
2104#endif
2105#if defined(MBEDTLS_MD5_C)
2106 case PSA_ALG_MD5:
2107 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2108 input, input_length );
2109 break;
2110#endif
2111#if defined(MBEDTLS_RIPEMD160_C)
2112 case PSA_ALG_RIPEMD160:
2113 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2114 input, input_length );
2115 break;
2116#endif
2117#if defined(MBEDTLS_SHA1_C)
2118 case PSA_ALG_SHA_1:
2119 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2120 input, input_length );
2121 break;
2122#endif
2123#if defined(MBEDTLS_SHA256_C)
2124 case PSA_ALG_SHA_224:
2125 case PSA_ALG_SHA_256:
2126 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2127 input, input_length );
2128 break;
2129#endif
2130#if defined(MBEDTLS_SHA512_C)
2131 case PSA_ALG_SHA_384:
2132 case PSA_ALG_SHA_512:
2133 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2134 input, input_length );
2135 break;
2136#endif
2137 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002138 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002139 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002140
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002141 if( ret != 0 )
2142 psa_hash_abort( operation );
2143 return( mbedtls_to_psa_error( ret ) );
2144}
2145
2146psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2147 uint8_t *hash,
2148 size_t hash_size,
2149 size_t *hash_length )
2150{
itayzafrir40835d42018-08-02 13:14:17 +03002151 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002153 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154
2155 /* Fill the output buffer with something that isn't a valid hash
2156 * (barring an attack on the hash and deliberately-crafted input),
2157 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002158 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002159 /* If hash_size is 0 then hash may be NULL and then the
2160 * call to memset would have undefined behavior. */
2161 if( hash_size != 0 )
2162 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163
2164 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002165 {
2166 status = PSA_ERROR_BUFFER_TOO_SMALL;
2167 goto exit;
2168 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002169
2170 switch( operation->alg )
2171 {
2172#if defined(MBEDTLS_MD2_C)
2173 case PSA_ALG_MD2:
2174 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2175 break;
2176#endif
2177#if defined(MBEDTLS_MD4_C)
2178 case PSA_ALG_MD4:
2179 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2180 break;
2181#endif
2182#if defined(MBEDTLS_MD5_C)
2183 case PSA_ALG_MD5:
2184 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2185 break;
2186#endif
2187#if defined(MBEDTLS_RIPEMD160_C)
2188 case PSA_ALG_RIPEMD160:
2189 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2190 break;
2191#endif
2192#if defined(MBEDTLS_SHA1_C)
2193 case PSA_ALG_SHA_1:
2194 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2195 break;
2196#endif
2197#if defined(MBEDTLS_SHA256_C)
2198 case PSA_ALG_SHA_224:
2199 case PSA_ALG_SHA_256:
2200 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2201 break;
2202#endif
2203#if defined(MBEDTLS_SHA512_C)
2204 case PSA_ALG_SHA_384:
2205 case PSA_ALG_SHA_512:
2206 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2207 break;
2208#endif
2209 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002210 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211 }
itayzafrir40835d42018-08-02 13:14:17 +03002212 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213
itayzafrir40835d42018-08-02 13:14:17 +03002214exit:
2215 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002217 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002218 return( psa_hash_abort( operation ) );
2219 }
2220 else
2221 {
2222 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002223 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 }
2225}
2226
Gilles Peskine2d277862018-06-18 15:41:12 +02002227psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2228 const uint8_t *hash,
2229 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230{
2231 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2232 size_t actual_hash_length;
2233 psa_status_t status = psa_hash_finish( operation,
2234 actual_hash, sizeof( actual_hash ),
2235 &actual_hash_length );
2236 if( status != PSA_SUCCESS )
2237 return( status );
2238 if( actual_hash_length != hash_length )
2239 return( PSA_ERROR_INVALID_SIGNATURE );
2240 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2241 return( PSA_ERROR_INVALID_SIGNATURE );
2242 return( PSA_SUCCESS );
2243}
2244
Gilles Peskineeb35d782019-01-22 17:56:16 +01002245psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2246 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002247{
2248 if( target_operation->alg != 0 )
2249 return( PSA_ERROR_BAD_STATE );
2250
2251 switch( source_operation->alg )
2252 {
2253 case 0:
2254 return( PSA_ERROR_BAD_STATE );
2255#if defined(MBEDTLS_MD2_C)
2256 case PSA_ALG_MD2:
2257 mbedtls_md2_clone( &target_operation->ctx.md2,
2258 &source_operation->ctx.md2 );
2259 break;
2260#endif
2261#if defined(MBEDTLS_MD4_C)
2262 case PSA_ALG_MD4:
2263 mbedtls_md4_clone( &target_operation->ctx.md4,
2264 &source_operation->ctx.md4 );
2265 break;
2266#endif
2267#if defined(MBEDTLS_MD5_C)
2268 case PSA_ALG_MD5:
2269 mbedtls_md5_clone( &target_operation->ctx.md5,
2270 &source_operation->ctx.md5 );
2271 break;
2272#endif
2273#if defined(MBEDTLS_RIPEMD160_C)
2274 case PSA_ALG_RIPEMD160:
2275 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2276 &source_operation->ctx.ripemd160 );
2277 break;
2278#endif
2279#if defined(MBEDTLS_SHA1_C)
2280 case PSA_ALG_SHA_1:
2281 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2282 &source_operation->ctx.sha1 );
2283 break;
2284#endif
2285#if defined(MBEDTLS_SHA256_C)
2286 case PSA_ALG_SHA_224:
2287 case PSA_ALG_SHA_256:
2288 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2289 &source_operation->ctx.sha256 );
2290 break;
2291#endif
2292#if defined(MBEDTLS_SHA512_C)
2293 case PSA_ALG_SHA_384:
2294 case PSA_ALG_SHA_512:
2295 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2296 &source_operation->ctx.sha512 );
2297 break;
2298#endif
2299 default:
2300 return( PSA_ERROR_NOT_SUPPORTED );
2301 }
2302
2303 target_operation->alg = source_operation->alg;
2304 return( PSA_SUCCESS );
2305}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002306
2307
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002309/* MAC */
2310/****************************************************************/
2311
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002312static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002313 psa_algorithm_t alg,
2314 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002315 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002316 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002317{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002318 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002319 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002321 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002322 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002323
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2325 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002326 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002327 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002328 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002329 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330 mode = MBEDTLS_MODE_STREAM;
2331 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332 case PSA_ALG_CTR:
2333 mode = MBEDTLS_MODE_CTR;
2334 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002335 case PSA_ALG_CFB:
2336 mode = MBEDTLS_MODE_CFB;
2337 break;
2338 case PSA_ALG_OFB:
2339 mode = MBEDTLS_MODE_OFB;
2340 break;
2341 case PSA_ALG_CBC_NO_PADDING:
2342 mode = MBEDTLS_MODE_CBC;
2343 break;
2344 case PSA_ALG_CBC_PKCS7:
2345 mode = MBEDTLS_MODE_CBC;
2346 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002347 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002348 mode = MBEDTLS_MODE_CCM;
2349 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002350 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351 mode = MBEDTLS_MODE_GCM;
2352 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002353 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2354 mode = MBEDTLS_MODE_CHACHAPOLY;
2355 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356 default:
2357 return( NULL );
2358 }
2359 }
2360 else if( alg == PSA_ALG_CMAC )
2361 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002362 else
2363 return( NULL );
2364
2365 switch( key_type )
2366 {
2367 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002368 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369 break;
2370 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002371 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2372 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002374 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002375 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002376 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002377 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2378 * but two-key Triple-DES is functionally three-key Triple-DES
2379 * with K1=K3, so that's how we present it to mbedtls. */
2380 if( key_bits == 128 )
2381 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382 break;
2383 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002384 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385 break;
2386 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002387 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002389 case PSA_KEY_TYPE_CHACHA20:
2390 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2391 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002392 default:
2393 return( NULL );
2394 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002395 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002396 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397
Jaeden Amero23bbb752018-06-26 14:16:54 +01002398 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2399 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400}
2401
Gilles Peskinea05219c2018-11-16 16:02:56 +01002402#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002403static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002404{
Gilles Peskine2d277862018-06-18 15:41:12 +02002405 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002406 {
2407 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002408 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002409 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002410 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002411 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002412 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002413 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002414 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002415 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002416 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002417 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002418 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002419 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002420 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002421 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002422 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002423 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002424 return( 128 );
2425 default:
2426 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002427 }
2428}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002429#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002430
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002431/* Initialize the MAC operation structure. Once this function has been
2432 * called, psa_mac_abort can run and will do the right thing. */
2433static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2434 psa_algorithm_t alg )
2435{
2436 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2437
2438 operation->alg = alg;
2439 operation->key_set = 0;
2440 operation->iv_set = 0;
2441 operation->iv_required = 0;
2442 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444
2445#if defined(MBEDTLS_CMAC_C)
2446 if( alg == PSA_ALG_CMAC )
2447 {
2448 operation->iv_required = 0;
2449 mbedtls_cipher_init( &operation->ctx.cmac );
2450 status = PSA_SUCCESS;
2451 }
2452 else
2453#endif /* MBEDTLS_CMAC_C */
2454#if defined(MBEDTLS_MD_C)
2455 if( PSA_ALG_IS_HMAC( operation->alg ) )
2456 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002457 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2458 operation->ctx.hmac.hash_ctx.alg = 0;
2459 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002460 }
2461 else
2462#endif /* MBEDTLS_MD_C */
2463 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002464 if( ! PSA_ALG_IS_MAC( alg ) )
2465 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002466 }
2467
2468 if( status != PSA_SUCCESS )
2469 memset( operation, 0, sizeof( *operation ) );
2470 return( status );
2471}
2472
Gilles Peskine01126fa2018-07-12 17:04:55 +02002473#if defined(MBEDTLS_MD_C)
2474static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2475{
Gilles Peskine3f108122018-12-07 18:14:53 +01002476 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002477 return( psa_hash_abort( &hmac->hash_ctx ) );
2478}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002479
Janos Follath999f6482019-06-11 12:04:10 +01002480#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002481static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2482{
2483 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2484 memset( hmac, 0, sizeof( *hmac ) );
2485}
Janos Follath999f6482019-06-11 12:04:10 +01002486#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002487#endif /* MBEDTLS_MD_C */
2488
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2490{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002491 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002493 /* The object has (apparently) been initialized but it is not
2494 * in use. It's ok to call abort on such an object, and there's
2495 * nothing to do. */
2496 return( PSA_SUCCESS );
2497 }
2498 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002500 if( operation->alg == PSA_ALG_CMAC )
2501 {
2502 mbedtls_cipher_free( &operation->ctx.cmac );
2503 }
2504 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002507 if( PSA_ALG_IS_HMAC( operation->alg ) )
2508 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002509 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002510 }
2511 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002513 {
2514 /* Sanity check (shouldn't happen: operation->alg should
2515 * always have been initialized to a valid value). */
2516 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517 }
Moran Peker41deec42018-04-04 15:43:05 +03002518
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519 operation->alg = 0;
2520 operation->key_set = 0;
2521 operation->iv_set = 0;
2522 operation->iv_required = 0;
2523 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002524 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002525
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002527
2528bad_state:
2529 /* If abort is called on an uninitialized object, we can't trust
2530 * anything. Wipe the object in case it contains confidential data.
2531 * This may result in a memory leak if a pointer gets overwritten,
2532 * but it's too late to do anything about this. */
2533 memset( operation, 0, sizeof( *operation ) );
2534 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535}
2536
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002537#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002538static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002539 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002540 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002541 const mbedtls_cipher_info_t *cipher_info )
2542{
2543 int ret;
2544
2545 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002546
2547 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2548 if( ret != 0 )
2549 return( ret );
2550
2551 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2552 slot->data.raw.data,
2553 key_bits );
2554 return( ret );
2555}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002556#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002557
Gilles Peskine248051a2018-06-20 16:09:38 +02002558#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002559static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2560 const uint8_t *key,
2561 size_t key_length,
2562 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002563{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002564 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002565 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002566 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002567 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002568 psa_status_t status;
2569
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002570 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2571 * overflow below. This should never trigger if the hash algorithm
2572 * is implemented correctly. */
2573 /* The size checks against the ipad and opad buffers cannot be written
2574 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2575 * because that triggers -Wlogical-op on GCC 7.3. */
2576 if( block_size > sizeof( ipad ) )
2577 return( PSA_ERROR_NOT_SUPPORTED );
2578 if( block_size > sizeof( hmac->opad ) )
2579 return( PSA_ERROR_NOT_SUPPORTED );
2580 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002581 return( PSA_ERROR_NOT_SUPPORTED );
2582
Gilles Peskined223b522018-06-11 18:12:58 +02002583 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002584 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002585 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002586 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002587 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002588 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002589 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002590 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002591 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002592 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002593 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002594 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002595 }
Gilles Peskine96889972018-07-12 17:07:03 +02002596 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2597 * but it is permitted. It is common when HMAC is used in HKDF, for
2598 * example. Don't call `memcpy` in the 0-length because `key` could be
2599 * an invalid pointer which would make the behavior undefined. */
2600 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002601 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002602
Gilles Peskined223b522018-06-11 18:12:58 +02002603 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2604 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002605 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002606 ipad[i] ^= 0x36;
2607 memset( ipad + key_length, 0x36, block_size - key_length );
2608
2609 /* Copy the key material from ipad to opad, flipping the requisite bits,
2610 * and filling the rest of opad with the requisite constant. */
2611 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002612 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2613 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002614
Gilles Peskine01126fa2018-07-12 17:04:55 +02002615 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002616 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002617 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002618
Gilles Peskine01126fa2018-07-12 17:04:55 +02002619 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002620
2621cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002622 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002623
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002624 return( status );
2625}
Gilles Peskine248051a2018-06-20 16:09:38 +02002626#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002627
Gilles Peskine89167cb2018-07-08 20:12:23 +02002628static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002629 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002630 psa_algorithm_t alg,
2631 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002634 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002636 psa_key_usage_t usage =
2637 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002638 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002639 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002641 /* A context must be freshly initialized before it can be set up. */
2642 if( operation->alg != 0 )
2643 {
2644 return( PSA_ERROR_BAD_STATE );
2645 }
2646
Gilles Peskined911eb72018-08-14 15:18:45 +02002647 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002648 if( status != PSA_SUCCESS )
2649 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002650 if( is_sign )
2651 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652
Gilles Peskine28f8f302019-07-24 13:30:31 +02002653 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002654 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002655 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002656 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002657
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002659 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002660 {
2661 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002662 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002663 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002664 int ret;
2665 if( cipher_info == NULL )
2666 {
2667 status = PSA_ERROR_NOT_SUPPORTED;
2668 goto exit;
2669 }
2670 operation->mac_size = cipher_info->block_size;
2671 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2672 status = mbedtls_to_psa_error( ret );
2673 }
2674 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002677 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002679 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002680 if( hash_alg == 0 )
2681 {
2682 status = PSA_ERROR_NOT_SUPPORTED;
2683 goto exit;
2684 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002685
2686 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2687 /* Sanity check. This shouldn't fail on a valid configuration. */
2688 if( operation->mac_size == 0 ||
2689 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2690 {
2691 status = PSA_ERROR_NOT_SUPPORTED;
2692 goto exit;
2693 }
2694
Gilles Peskine8e338702019-07-30 20:06:31 +02002695 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002696 {
2697 status = PSA_ERROR_INVALID_ARGUMENT;
2698 goto exit;
2699 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002700
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2702 slot->data.raw.data,
2703 slot->data.raw.bytes,
2704 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002705 }
2706 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002708 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002709 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002710 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002712
Gilles Peskined911eb72018-08-14 15:18:45 +02002713 if( truncated == 0 )
2714 {
2715 /* The "normal" case: untruncated algorithm. Nothing to do. */
2716 }
2717 else if( truncated < 4 )
2718 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002719 /* A very short MAC is too short for security since it can be
2720 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2721 * so we make this our minimum, even though 32 bits is still
2722 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002723 status = PSA_ERROR_NOT_SUPPORTED;
2724 }
2725 else if( truncated > operation->mac_size )
2726 {
2727 /* It's impossible to "truncate" to a larger length. */
2728 status = PSA_ERROR_INVALID_ARGUMENT;
2729 }
2730 else
2731 operation->mac_size = truncated;
2732
Gilles Peskinefbfac682018-07-08 20:51:54 +02002733exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002736 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002738 else
2739 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740 operation->key_set = 1;
2741 }
2742 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743}
2744
Gilles Peskine89167cb2018-07-08 20:12:23 +02002745psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002746 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002747 psa_algorithm_t alg )
2748{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002749 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002750}
2751
2752psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002753 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002754 psa_algorithm_t alg )
2755{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002756 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002757}
2758
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2760 const uint8_t *input,
2761 size_t input_length )
2762{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002765 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002767 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768 operation->has_input = 1;
2769
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002771 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002772 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2774 input, input_length );
2775 status = mbedtls_to_psa_error( ret );
2776 }
2777 else
2778#endif /* MBEDTLS_CMAC_C */
2779#if defined(MBEDTLS_MD_C)
2780 if( PSA_ALG_IS_HMAC( operation->alg ) )
2781 {
2782 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2783 input_length );
2784 }
2785 else
2786#endif /* MBEDTLS_MD_C */
2787 {
2788 /* This shouldn't happen if `operation` was initialized by
2789 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002790 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002791 }
2792
Gilles Peskinefbfac682018-07-08 20:51:54 +02002793 if( status != PSA_SUCCESS )
2794 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002795 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796}
2797
Gilles Peskine01126fa2018-07-12 17:04:55 +02002798#if defined(MBEDTLS_MD_C)
2799static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2800 uint8_t *mac,
2801 size_t mac_size )
2802{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002803 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002804 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2805 size_t hash_size = 0;
2806 size_t block_size = psa_get_hash_block_size( hash_alg );
2807 psa_status_t status;
2808
Gilles Peskine01126fa2018-07-12 17:04:55 +02002809 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2810 if( status != PSA_SUCCESS )
2811 return( status );
2812 /* From here on, tmp needs to be wiped. */
2813
2814 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2815 if( status != PSA_SUCCESS )
2816 goto exit;
2817
2818 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2819 if( status != PSA_SUCCESS )
2820 goto exit;
2821
2822 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2823 if( status != PSA_SUCCESS )
2824 goto exit;
2825
Gilles Peskined911eb72018-08-14 15:18:45 +02002826 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2827 if( status != PSA_SUCCESS )
2828 goto exit;
2829
2830 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002831
2832exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002833 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002834 return( status );
2835}
2836#endif /* MBEDTLS_MD_C */
2837
mohammad16036df908f2018-04-02 08:34:15 -07002838static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002839 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002840 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002842 if( ! operation->key_set )
2843 return( PSA_ERROR_BAD_STATE );
2844 if( operation->iv_required && ! operation->iv_set )
2845 return( PSA_ERROR_BAD_STATE );
2846
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847 if( mac_size < operation->mac_size )
2848 return( PSA_ERROR_BUFFER_TOO_SMALL );
2849
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002851 if( operation->alg == PSA_ALG_CMAC )
2852 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002853 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2854 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2855 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002856 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002857 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002860 else
2861#endif /* MBEDTLS_CMAC_C */
2862#if defined(MBEDTLS_MD_C)
2863 if( PSA_ALG_IS_HMAC( operation->alg ) )
2864 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002865 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002866 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002867 }
2868 else
2869#endif /* MBEDTLS_MD_C */
2870 {
2871 /* This shouldn't happen if `operation` was initialized by
2872 * a setup function. */
2873 return( PSA_ERROR_BAD_STATE );
2874 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875}
2876
Gilles Peskineacd4be32018-07-08 19:56:25 +02002877psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2878 uint8_t *mac,
2879 size_t mac_size,
2880 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002881{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002882 psa_status_t status;
2883
Jaeden Amero252ef282019-02-15 14:05:35 +00002884 if( operation->alg == 0 )
2885 {
2886 return( PSA_ERROR_BAD_STATE );
2887 }
2888
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002889 /* Fill the output buffer with something that isn't a valid mac
2890 * (barring an attack on the mac and deliberately-crafted input),
2891 * in case the caller doesn't check the return status properly. */
2892 *mac_length = mac_size;
2893 /* If mac_size is 0 then mac may be NULL and then the
2894 * call to memset would have undefined behavior. */
2895 if( mac_size != 0 )
2896 memset( mac, '!', mac_size );
2897
Gilles Peskine89167cb2018-07-08 20:12:23 +02002898 if( ! operation->is_sign )
2899 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002900 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002901 }
mohammad16036df908f2018-04-02 08:34:15 -07002902
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002903 status = psa_mac_finish_internal( operation, mac, mac_size );
2904
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002905 if( status == PSA_SUCCESS )
2906 {
2907 status = psa_mac_abort( operation );
2908 if( status == PSA_SUCCESS )
2909 *mac_length = operation->mac_size;
2910 else
2911 memset( mac, '!', mac_size );
2912 }
2913 else
2914 psa_mac_abort( operation );
2915 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002916}
2917
Gilles Peskineacd4be32018-07-08 19:56:25 +02002918psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2919 const uint8_t *mac,
2920 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921{
Gilles Peskine828ed142018-06-18 23:25:51 +02002922 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002923 psa_status_t status;
2924
Jaeden Amero252ef282019-02-15 14:05:35 +00002925 if( operation->alg == 0 )
2926 {
2927 return( PSA_ERROR_BAD_STATE );
2928 }
2929
Gilles Peskine89167cb2018-07-08 20:12:23 +02002930 if( operation->is_sign )
2931 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002932 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002933 }
2934 if( operation->mac_size != mac_length )
2935 {
2936 status = PSA_ERROR_INVALID_SIGNATURE;
2937 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002938 }
mohammad16036df908f2018-04-02 08:34:15 -07002939
2940 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002941 actual_mac, sizeof( actual_mac ) );
2942
2943 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2944 status = PSA_ERROR_INVALID_SIGNATURE;
2945
2946cleanup:
2947 if( status == PSA_SUCCESS )
2948 status = psa_mac_abort( operation );
2949 else
2950 psa_mac_abort( operation );
2951
Gilles Peskine3f108122018-12-07 18:14:53 +01002952 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002953
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002954 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955}
2956
2957
Gilles Peskine20035e32018-02-03 22:44:14 +01002958
Gilles Peskine20035e32018-02-03 22:44:14 +01002959/****************************************************************/
2960/* Asymmetric cryptography */
2961/****************************************************************/
2962
Gilles Peskine2b450e32018-06-27 15:42:46 +02002963#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002964/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002965 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002966static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2967 size_t hash_length,
2968 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002969{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002970 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002971 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002972 *md_alg = mbedtls_md_get_type( md_info );
2973
2974 /* The Mbed TLS RSA module uses an unsigned int for hash length
2975 * parameters. Validate that it fits so that we don't risk an
2976 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002977#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002978 if( hash_length > UINT_MAX )
2979 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002980#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002981
2982#if defined(MBEDTLS_PKCS1_V15)
2983 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2984 * must be correct. */
2985 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2986 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002987 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002988 if( md_info == NULL )
2989 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002990 if( mbedtls_md_get_size( md_info ) != hash_length )
2991 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002992 }
2993#endif /* MBEDTLS_PKCS1_V15 */
2994
2995#if defined(MBEDTLS_PKCS1_V21)
2996 /* PSS requires a hash internally. */
2997 if( PSA_ALG_IS_RSA_PSS( alg ) )
2998 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002999 if( md_info == NULL )
3000 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003001 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003002#endif /* MBEDTLS_PKCS1_V21 */
3003
Gilles Peskine61b91d42018-06-08 16:09:36 +02003004 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003005}
3006
Gilles Peskine2b450e32018-06-27 15:42:46 +02003007static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3008 psa_algorithm_t alg,
3009 const uint8_t *hash,
3010 size_t hash_length,
3011 uint8_t *signature,
3012 size_t signature_size,
3013 size_t *signature_length )
3014{
3015 psa_status_t status;
3016 int ret;
3017 mbedtls_md_type_t md_alg;
3018
3019 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3020 if( status != PSA_SUCCESS )
3021 return( status );
3022
Gilles Peskine630a18a2018-06-29 17:49:35 +02003023 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003024 return( PSA_ERROR_BUFFER_TOO_SMALL );
3025
3026#if defined(MBEDTLS_PKCS1_V15)
3027 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3028 {
3029 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3030 MBEDTLS_MD_NONE );
3031 ret = mbedtls_rsa_pkcs1_sign( rsa,
3032 mbedtls_ctr_drbg_random,
3033 &global_data.ctr_drbg,
3034 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003035 md_alg,
3036 (unsigned int) hash_length,
3037 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003038 signature );
3039 }
3040 else
3041#endif /* MBEDTLS_PKCS1_V15 */
3042#if defined(MBEDTLS_PKCS1_V21)
3043 if( PSA_ALG_IS_RSA_PSS( alg ) )
3044 {
3045 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3046 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3047 mbedtls_ctr_drbg_random,
3048 &global_data.ctr_drbg,
3049 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003050 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003051 (unsigned int) hash_length,
3052 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003053 signature );
3054 }
3055 else
3056#endif /* MBEDTLS_PKCS1_V21 */
3057 {
3058 return( PSA_ERROR_INVALID_ARGUMENT );
3059 }
3060
3061 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003062 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003063 return( mbedtls_to_psa_error( ret ) );
3064}
3065
3066static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3067 psa_algorithm_t alg,
3068 const uint8_t *hash,
3069 size_t hash_length,
3070 const uint8_t *signature,
3071 size_t signature_length )
3072{
3073 psa_status_t status;
3074 int ret;
3075 mbedtls_md_type_t md_alg;
3076
3077 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3078 if( status != PSA_SUCCESS )
3079 return( status );
3080
Gilles Peskine630a18a2018-06-29 17:49:35 +02003081 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003082 return( PSA_ERROR_BUFFER_TOO_SMALL );
3083
3084#if defined(MBEDTLS_PKCS1_V15)
3085 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3086 {
3087 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3088 MBEDTLS_MD_NONE );
3089 ret = mbedtls_rsa_pkcs1_verify( rsa,
3090 mbedtls_ctr_drbg_random,
3091 &global_data.ctr_drbg,
3092 MBEDTLS_RSA_PUBLIC,
3093 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003094 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003095 hash,
3096 signature );
3097 }
3098 else
3099#endif /* MBEDTLS_PKCS1_V15 */
3100#if defined(MBEDTLS_PKCS1_V21)
3101 if( PSA_ALG_IS_RSA_PSS( alg ) )
3102 {
3103 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3104 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3105 mbedtls_ctr_drbg_random,
3106 &global_data.ctr_drbg,
3107 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003108 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003109 (unsigned int) hash_length,
3110 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003111 signature );
3112 }
3113 else
3114#endif /* MBEDTLS_PKCS1_V21 */
3115 {
3116 return( PSA_ERROR_INVALID_ARGUMENT );
3117 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003118
3119 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3120 * the rest of the signature is invalid". This has little use in
3121 * practice and PSA doesn't report this distinction. */
3122 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3123 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003124 return( mbedtls_to_psa_error( ret ) );
3125}
3126#endif /* MBEDTLS_RSA_C */
3127
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003128#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003129/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3130 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3131 * (even though these functions don't modify it). */
3132static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3133 psa_algorithm_t alg,
3134 const uint8_t *hash,
3135 size_t hash_length,
3136 uint8_t *signature,
3137 size_t signature_size,
3138 size_t *signature_length )
3139{
3140 int ret;
3141 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003142 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003143 mbedtls_mpi_init( &r );
3144 mbedtls_mpi_init( &s );
3145
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003146 if( signature_size < 2 * curve_bytes )
3147 {
3148 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3149 goto cleanup;
3150 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003151
Gilles Peskinea05219c2018-11-16 16:02:56 +01003152#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003153 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3154 {
3155 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3156 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3157 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3158 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3159 hash, hash_length,
3160 md_alg ) );
3161 }
3162 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003163#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003164 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003165 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003166 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3167 hash, hash_length,
3168 mbedtls_ctr_drbg_random,
3169 &global_data.ctr_drbg ) );
3170 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003171
3172 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3173 signature,
3174 curve_bytes ) );
3175 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3176 signature + curve_bytes,
3177 curve_bytes ) );
3178
3179cleanup:
3180 mbedtls_mpi_free( &r );
3181 mbedtls_mpi_free( &s );
3182 if( ret == 0 )
3183 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003184 return( mbedtls_to_psa_error( ret ) );
3185}
3186
3187static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3188 const uint8_t *hash,
3189 size_t hash_length,
3190 const uint8_t *signature,
3191 size_t signature_length )
3192{
3193 int ret;
3194 mbedtls_mpi r, s;
3195 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3196 mbedtls_mpi_init( &r );
3197 mbedtls_mpi_init( &s );
3198
3199 if( signature_length != 2 * curve_bytes )
3200 return( PSA_ERROR_INVALID_SIGNATURE );
3201
3202 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3203 signature,
3204 curve_bytes ) );
3205 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3206 signature + curve_bytes,
3207 curve_bytes ) );
3208
3209 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3210 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003211
3212cleanup:
3213 mbedtls_mpi_free( &r );
3214 mbedtls_mpi_free( &s );
3215 return( mbedtls_to_psa_error( ret ) );
3216}
3217#endif /* MBEDTLS_ECDSA_C */
3218
Gilles Peskinec5487a82018-12-03 18:08:14 +01003219psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003220 psa_algorithm_t alg,
3221 const uint8_t *hash,
3222 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003223 uint8_t *signature,
3224 size_t signature_size,
3225 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003226{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003227 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003228 psa_status_t status;
3229
3230 *signature_length = signature_size;
3231
Gilles Peskine28f8f302019-07-24 13:30:31 +02003232 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003233 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003234 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003235 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003236 {
3237 status = PSA_ERROR_INVALID_ARGUMENT;
3238 goto exit;
3239 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003240
Gilles Peskine20035e32018-02-03 22:44:14 +01003241#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003242 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003243 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003244 status = psa_rsa_sign( slot->data.rsa,
3245 alg,
3246 hash, hash_length,
3247 signature, signature_size,
3248 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003249 }
3250 else
3251#endif /* defined(MBEDTLS_RSA_C) */
3252#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003253 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003254 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003255#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003256 if(
3257#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3258 PSA_ALG_IS_ECDSA( alg )
3259#else
3260 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3261#endif
3262 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003263 status = psa_ecdsa_sign( slot->data.ecp,
3264 alg,
3265 hash, hash_length,
3266 signature, signature_size,
3267 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003268 else
3269#endif /* defined(MBEDTLS_ECDSA_C) */
3270 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003271 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003272 }
itayzafrir5c753392018-05-08 11:18:38 +03003273 }
3274 else
3275#endif /* defined(MBEDTLS_ECP_C) */
3276 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003277 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003278 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003279
3280exit:
3281 /* Fill the unused part of the output buffer (the whole buffer on error,
3282 * the trailing part on success) with something that isn't a valid mac
3283 * (barring an attack on the mac and deliberately-crafted input),
3284 * in case the caller doesn't check the return status properly. */
3285 if( status == PSA_SUCCESS )
3286 memset( signature + *signature_length, '!',
3287 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003288 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003289 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003290 /* If signature_size is 0 then we have nothing to do. We must not call
3291 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003292 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003293}
3294
Gilles Peskinec5487a82018-12-03 18:08:14 +01003295psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003296 psa_algorithm_t alg,
3297 const uint8_t *hash,
3298 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003299 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003300 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003301{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003302 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003303 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003304
Gilles Peskine28f8f302019-07-24 13:30:31 +02003305 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003306 if( status != PSA_SUCCESS )
3307 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003308
Gilles Peskine61b91d42018-06-08 16:09:36 +02003309#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003310 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003312 return( psa_rsa_verify( slot->data.rsa,
3313 alg,
3314 hash, hash_length,
3315 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316 }
3317 else
3318#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003319#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003320 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003321 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003322#if defined(MBEDTLS_ECDSA_C)
3323 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003324 return( psa_ecdsa_verify( slot->data.ecp,
3325 hash, hash_length,
3326 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003327 else
3328#endif /* defined(MBEDTLS_ECDSA_C) */
3329 {
3330 return( PSA_ERROR_INVALID_ARGUMENT );
3331 }
itayzafrir5c753392018-05-08 11:18:38 +03003332 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003333 else
3334#endif /* defined(MBEDTLS_ECP_C) */
3335 {
3336 return( PSA_ERROR_NOT_SUPPORTED );
3337 }
3338}
3339
Gilles Peskine072ac562018-06-30 00:21:29 +02003340#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3341static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3342 mbedtls_rsa_context *rsa )
3343{
3344 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3345 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3346 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3347 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3348}
3349#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3350
Gilles Peskinec5487a82018-12-03 18:08:14 +01003351psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003352 psa_algorithm_t alg,
3353 const uint8_t *input,
3354 size_t input_length,
3355 const uint8_t *salt,
3356 size_t salt_length,
3357 uint8_t *output,
3358 size_t output_size,
3359 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003361 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003362 psa_status_t status;
3363
Darryl Green5cc689a2018-07-24 15:34:10 +01003364 (void) input;
3365 (void) input_length;
3366 (void) salt;
3367 (void) output;
3368 (void) output_size;
3369
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003370 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003372 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3373 return( PSA_ERROR_INVALID_ARGUMENT );
3374
Gilles Peskine28f8f302019-07-24 13:30:31 +02003375 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003376 if( status != PSA_SUCCESS )
3377 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003378 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3379 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003380 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003381
3382#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003383 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003384 {
3385 mbedtls_rsa_context *rsa = slot->data.rsa;
3386 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003387 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003388 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003389#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003390 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003391 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003392 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3393 mbedtls_ctr_drbg_random,
3394 &global_data.ctr_drbg,
3395 MBEDTLS_RSA_PUBLIC,
3396 input_length,
3397 input,
3398 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003399 }
3400 else
3401#endif /* MBEDTLS_PKCS1_V15 */
3402#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003403 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003405 psa_rsa_oaep_set_padding_mode( alg, rsa );
3406 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3407 mbedtls_ctr_drbg_random,
3408 &global_data.ctr_drbg,
3409 MBEDTLS_RSA_PUBLIC,
3410 salt, salt_length,
3411 input_length,
3412 input,
3413 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003414 }
3415 else
3416#endif /* MBEDTLS_PKCS1_V21 */
3417 {
3418 return( PSA_ERROR_INVALID_ARGUMENT );
3419 }
3420 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003421 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003422 return( mbedtls_to_psa_error( ret ) );
3423 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003424 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003425#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003426 {
3427 return( PSA_ERROR_NOT_SUPPORTED );
3428 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003429}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003430
Gilles Peskinec5487a82018-12-03 18:08:14 +01003431psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003432 psa_algorithm_t alg,
3433 const uint8_t *input,
3434 size_t input_length,
3435 const uint8_t *salt,
3436 size_t salt_length,
3437 uint8_t *output,
3438 size_t output_size,
3439 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003441 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003442 psa_status_t status;
3443
Darryl Green5cc689a2018-07-24 15:34:10 +01003444 (void) input;
3445 (void) input_length;
3446 (void) salt;
3447 (void) output;
3448 (void) output_size;
3449
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003450 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003452 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3453 return( PSA_ERROR_INVALID_ARGUMENT );
3454
Gilles Peskine28f8f302019-07-24 13:30:31 +02003455 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003456 if( status != PSA_SUCCESS )
3457 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003458 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459 return( PSA_ERROR_INVALID_ARGUMENT );
3460
3461#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003462 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003463 {
3464 mbedtls_rsa_context *rsa = slot->data.rsa;
3465 int ret;
3466
Gilles Peskine630a18a2018-06-29 17:49:35 +02003467 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003468 return( PSA_ERROR_INVALID_ARGUMENT );
3469
3470#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003471 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003472 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003473 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3474 mbedtls_ctr_drbg_random,
3475 &global_data.ctr_drbg,
3476 MBEDTLS_RSA_PRIVATE,
3477 output_length,
3478 input,
3479 output,
3480 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003481 }
3482 else
3483#endif /* MBEDTLS_PKCS1_V15 */
3484#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003485 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003486 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003487 psa_rsa_oaep_set_padding_mode( alg, rsa );
3488 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3489 mbedtls_ctr_drbg_random,
3490 &global_data.ctr_drbg,
3491 MBEDTLS_RSA_PRIVATE,
3492 salt, salt_length,
3493 output_length,
3494 input,
3495 output,
3496 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003497 }
3498 else
3499#endif /* MBEDTLS_PKCS1_V21 */
3500 {
3501 return( PSA_ERROR_INVALID_ARGUMENT );
3502 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003503
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003504 return( mbedtls_to_psa_error( ret ) );
3505 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003506 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003507#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003508 {
3509 return( PSA_ERROR_NOT_SUPPORTED );
3510 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003511}
Gilles Peskine20035e32018-02-03 22:44:14 +01003512
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003513
3514
mohammad1603503973b2018-03-12 15:59:30 +02003515/****************************************************************/
3516/* Symmetric cryptography */
3517/****************************************************************/
3518
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003519/* Initialize the cipher operation structure. Once this function has been
3520 * called, psa_cipher_abort can run and will do the right thing. */
3521static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3522 psa_algorithm_t alg )
3523{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003524 if( ! PSA_ALG_IS_CIPHER( alg ) )
3525 {
3526 memset( operation, 0, sizeof( *operation ) );
3527 return( PSA_ERROR_INVALID_ARGUMENT );
3528 }
3529
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003530 operation->alg = alg;
3531 operation->key_set = 0;
3532 operation->iv_set = 0;
3533 operation->iv_required = 1;
3534 operation->iv_size = 0;
3535 operation->block_size = 0;
3536 mbedtls_cipher_init( &operation->ctx.cipher );
3537 return( PSA_SUCCESS );
3538}
3539
Gilles Peskinee553c652018-06-04 16:22:46 +02003540static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003541 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003542 psa_algorithm_t alg,
3543 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003544{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003545 int ret = 0;
3546 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003547 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003548 size_t key_bits;
3549 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003550 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3551 PSA_KEY_USAGE_ENCRYPT :
3552 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003553
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003554 /* A context must be freshly initialized before it can be set up. */
3555 if( operation->alg != 0 )
3556 {
3557 return( PSA_ERROR_BAD_STATE );
3558 }
3559
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003560 status = psa_cipher_init( operation, alg );
3561 if( status != PSA_SUCCESS )
3562 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003563
Gilles Peskine28f8f302019-07-24 13:30:31 +02003564 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003565 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003566 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003567 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003568
Gilles Peskine8e338702019-07-30 20:06:31 +02003569 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003570 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003571 {
3572 status = PSA_ERROR_NOT_SUPPORTED;
3573 goto exit;
3574 }
mohammad1603503973b2018-03-12 15:59:30 +02003575
mohammad1603503973b2018-03-12 15:59:30 +02003576 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003577 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003578 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003579
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003580#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003581 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003582 {
3583 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003584 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003585 memcpy( keys, slot->data.raw.data, 16 );
3586 memcpy( keys + 16, slot->data.raw.data, 8 );
3587 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3588 keys,
3589 192, cipher_operation );
3590 }
3591 else
3592#endif
3593 {
3594 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3595 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003596 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003597 }
Moran Peker41deec42018-04-04 15:43:05 +03003598 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003599 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003600
mohammad16038481e742018-03-18 13:57:31 +02003601#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003602 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003603 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003604 case PSA_ALG_CBC_NO_PADDING:
3605 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3606 MBEDTLS_PADDING_NONE );
3607 break;
3608 case PSA_ALG_CBC_PKCS7:
3609 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3610 MBEDTLS_PADDING_PKCS7 );
3611 break;
3612 default:
3613 /* The algorithm doesn't involve padding. */
3614 ret = 0;
3615 break;
3616 }
3617 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003618 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003619#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3620
mohammad1603503973b2018-03-12 15:59:30 +02003621 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003622 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003623 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003624 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003625 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003626 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003627 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003628#if defined(MBEDTLS_CHACHA20_C)
3629 else
3630 if( alg == PSA_ALG_CHACHA20 )
3631 operation->iv_size = 12;
3632#endif
mohammad1603503973b2018-03-12 15:59:30 +02003633
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003634exit:
3635 if( status == 0 )
3636 status = mbedtls_to_psa_error( ret );
3637 if( status != 0 )
3638 psa_cipher_abort( operation );
3639 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003640}
3641
Gilles Peskinefe119512018-07-08 21:39:34 +02003642psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003643 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003644 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003645{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003646 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003647}
3648
Gilles Peskinefe119512018-07-08 21:39:34 +02003649psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003650 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003651 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003652{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003653 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003654}
3655
Gilles Peskinefe119512018-07-08 21:39:34 +02003656psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003657 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003658 size_t iv_size,
3659 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003660{
itayzafrir534bd7c2018-08-02 13:56:32 +03003661 psa_status_t status;
3662 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003663 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003664 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003665 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003666 }
Moran Peker41deec42018-04-04 15:43:05 +03003667 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003668 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003669 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003670 goto exit;
3671 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003672 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3673 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003674 if( ret != 0 )
3675 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003676 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003677 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003678 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003679
mohammad16038481e742018-03-18 13:57:31 +02003680 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003681 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003682
Moran Peker395db872018-05-31 14:07:14 +03003683exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003684 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003685 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003686 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003687}
3688
Gilles Peskinefe119512018-07-08 21:39:34 +02003689psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003690 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003691 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003692{
itayzafrir534bd7c2018-08-02 13:56:32 +03003693 psa_status_t status;
3694 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003695 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003696 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003697 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003698 }
Moran Pekera28258c2018-05-29 16:25:04 +03003699 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003700 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003701 status = PSA_ERROR_INVALID_ARGUMENT;
3702 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003703 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003704 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3705 status = mbedtls_to_psa_error( ret );
3706exit:
3707 if( status == PSA_SUCCESS )
3708 operation->iv_set = 1;
3709 else
mohammad1603503973b2018-03-12 15:59:30 +02003710 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003711 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003712}
3713
Gilles Peskinee553c652018-06-04 16:22:46 +02003714psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3715 const uint8_t *input,
3716 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003717 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003718 size_t output_size,
3719 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003720{
itayzafrir534bd7c2018-08-02 13:56:32 +03003721 psa_status_t status;
3722 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003723 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003724
3725 if( operation->alg == 0 )
3726 {
3727 return( PSA_ERROR_BAD_STATE );
3728 }
3729
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003730 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003731 {
3732 /* Take the unprocessed partial block left over from previous
3733 * update calls, if any, plus the input to this call. Remove
3734 * the last partial block, if any. You get the data that will be
3735 * output in this call. */
3736 expected_output_size =
3737 ( operation->ctx.cipher.unprocessed_len + input_length )
3738 / operation->block_size * operation->block_size;
3739 }
3740 else
3741 {
3742 expected_output_size = input_length;
3743 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003744
Gilles Peskine89d789c2018-06-04 17:17:16 +02003745 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003746 {
3747 status = PSA_ERROR_BUFFER_TOO_SMALL;
3748 goto exit;
3749 }
mohammad160382759612018-03-12 18:16:40 +02003750
mohammad1603503973b2018-03-12 15:59:30 +02003751 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003752 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003753 status = mbedtls_to_psa_error( ret );
3754exit:
3755 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003756 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003757 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003758}
3759
Gilles Peskinee553c652018-06-04 16:22:46 +02003760psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3761 uint8_t *output,
3762 size_t output_size,
3763 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003764{
David Saadab4ecc272019-02-14 13:48:10 +02003765 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003766 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003767 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003768
mohammad1603503973b2018-03-12 15:59:30 +02003769 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003770 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003771 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003772 }
3773 if( operation->iv_required && ! operation->iv_set )
3774 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003775 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003776 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003777
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003778 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003779 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3780 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003781 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003782 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003783 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003784 }
3785
Janos Follath315b51c2018-07-09 16:04:51 +01003786 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3787 temp_output_buffer,
3788 output_length );
3789 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003790 {
Janos Follath315b51c2018-07-09 16:04:51 +01003791 status = mbedtls_to_psa_error( cipher_ret );
3792 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003793 }
Janos Follath315b51c2018-07-09 16:04:51 +01003794
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003795 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003796 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003797 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003798 memcpy( output, temp_output_buffer, *output_length );
3799 else
3800 {
Janos Follath315b51c2018-07-09 16:04:51 +01003801 status = PSA_ERROR_BUFFER_TOO_SMALL;
3802 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003803 }
mohammad1603503973b2018-03-12 15:59:30 +02003804
Gilles Peskine3f108122018-12-07 18:14:53 +01003805 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003806 status = psa_cipher_abort( operation );
3807
3808 return( status );
3809
3810error:
3811
3812 *output_length = 0;
3813
Gilles Peskine3f108122018-12-07 18:14:53 +01003814 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003815 (void) psa_cipher_abort( operation );
3816
3817 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003818}
3819
Gilles Peskinee553c652018-06-04 16:22:46 +02003820psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3821{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003822 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003823 {
3824 /* The object has (apparently) been initialized but it is not
3825 * in use. It's ok to call abort on such an object, and there's
3826 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003827 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003828 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003829
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003830 /* Sanity check (shouldn't happen: operation->alg should
3831 * always have been initialized to a valid value). */
3832 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3833 return( PSA_ERROR_BAD_STATE );
3834
mohammad1603503973b2018-03-12 15:59:30 +02003835 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003836
Moran Peker41deec42018-04-04 15:43:05 +03003837 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003838 operation->key_set = 0;
3839 operation->iv_set = 0;
3840 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003841 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003842 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003843
Moran Peker395db872018-05-31 14:07:14 +03003844 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003845}
3846
Gilles Peskinea0655c32018-04-30 17:06:50 +02003847
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003848
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003849
mohammad16035955c982018-04-26 00:53:03 +03003850/****************************************************************/
3851/* AEAD */
3852/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003853
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854typedef struct
3855{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003856 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003857 const mbedtls_cipher_info_t *cipher_info;
3858 union
3859 {
3860#if defined(MBEDTLS_CCM_C)
3861 mbedtls_ccm_context ccm;
3862#endif /* MBEDTLS_CCM_C */
3863#if defined(MBEDTLS_GCM_C)
3864 mbedtls_gcm_context gcm;
3865#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003866#if defined(MBEDTLS_CHACHAPOLY_C)
3867 mbedtls_chachapoly_context chachapoly;
3868#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003869 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003870 psa_algorithm_t core_alg;
3871 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003872 uint8_t tag_length;
3873} aead_operation_t;
3874
Gilles Peskine30a9e412019-01-14 18:36:12 +01003875static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003876{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003877 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003878 {
3879#if defined(MBEDTLS_CCM_C)
3880 case PSA_ALG_CCM:
3881 mbedtls_ccm_free( &operation->ctx.ccm );
3882 break;
3883#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003884#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003885 case PSA_ALG_GCM:
3886 mbedtls_gcm_free( &operation->ctx.gcm );
3887 break;
3888#endif /* MBEDTLS_GCM_C */
3889 }
3890}
3891
3892static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003893 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003894 psa_key_usage_t usage,
3895 psa_algorithm_t alg )
3896{
3897 psa_status_t status;
3898 size_t key_bits;
3899 mbedtls_cipher_id_t cipher_id;
3900
Gilles Peskine28f8f302019-07-24 13:30:31 +02003901 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003902 if( status != PSA_SUCCESS )
3903 return( status );
3904
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003905 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003906
3907 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003908 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003909 &cipher_id );
3910 if( operation->cipher_info == NULL )
3911 return( PSA_ERROR_NOT_SUPPORTED );
3912
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003913 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003914 {
3915#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003916 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3917 operation->core_alg = PSA_ALG_CCM;
3918 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003919 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3920 * The call to mbedtls_ccm_encrypt_and_tag or
3921 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003922 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003923 return( PSA_ERROR_INVALID_ARGUMENT );
3924 mbedtls_ccm_init( &operation->ctx.ccm );
3925 status = mbedtls_to_psa_error(
3926 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3927 operation->slot->data.raw.data,
3928 (unsigned int) key_bits ) );
3929 if( status != 0 )
3930 goto cleanup;
3931 break;
3932#endif /* MBEDTLS_CCM_C */
3933
3934#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003935 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3936 operation->core_alg = PSA_ALG_GCM;
3937 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003938 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3939 * The call to mbedtls_gcm_crypt_and_tag or
3940 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003941 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003942 return( PSA_ERROR_INVALID_ARGUMENT );
3943 mbedtls_gcm_init( &operation->ctx.gcm );
3944 status = mbedtls_to_psa_error(
3945 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3946 operation->slot->data.raw.data,
3947 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003948 if( status != 0 )
3949 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003950 break;
3951#endif /* MBEDTLS_GCM_C */
3952
Gilles Peskine26869f22019-05-06 15:25:00 +02003953#if defined(MBEDTLS_CHACHAPOLY_C)
3954 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3955 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3956 operation->full_tag_length = 16;
3957 /* We only support the default tag length. */
3958 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3959 return( PSA_ERROR_NOT_SUPPORTED );
3960 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3961 status = mbedtls_to_psa_error(
3962 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3963 operation->slot->data.raw.data ) );
3964 if( status != 0 )
3965 goto cleanup;
3966 break;
3967#endif /* MBEDTLS_CHACHAPOLY_C */
3968
Gilles Peskineedf9a652018-08-17 18:11:56 +02003969 default:
3970 return( PSA_ERROR_NOT_SUPPORTED );
3971 }
3972
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003973 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3974 {
3975 status = PSA_ERROR_INVALID_ARGUMENT;
3976 goto cleanup;
3977 }
3978 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003979
Gilles Peskineedf9a652018-08-17 18:11:56 +02003980 return( PSA_SUCCESS );
3981
3982cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003983 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003984 return( status );
3985}
3986
Gilles Peskinec5487a82018-12-03 18:08:14 +01003987psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003988 psa_algorithm_t alg,
3989 const uint8_t *nonce,
3990 size_t nonce_length,
3991 const uint8_t *additional_data,
3992 size_t additional_data_length,
3993 const uint8_t *plaintext,
3994 size_t plaintext_length,
3995 uint8_t *ciphertext,
3996 size_t ciphertext_size,
3997 size_t *ciphertext_length )
3998{
mohammad16035955c982018-04-26 00:53:03 +03003999 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004000 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004001 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004002
mohammad1603f08a5502018-06-03 15:05:47 +03004003 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004004
Gilles Peskinec5487a82018-12-03 18:08:14 +01004005 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004006 if( status != PSA_SUCCESS )
4007 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004008
Gilles Peskineedf9a652018-08-17 18:11:56 +02004009 /* For all currently supported modes, the tag is at the end of the
4010 * ciphertext. */
4011 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4012 {
4013 status = PSA_ERROR_BUFFER_TOO_SMALL;
4014 goto exit;
4015 }
4016 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004017
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004018#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004019 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004020 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004021 status = mbedtls_to_psa_error(
4022 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4023 MBEDTLS_GCM_ENCRYPT,
4024 plaintext_length,
4025 nonce, nonce_length,
4026 additional_data, additional_data_length,
4027 plaintext, ciphertext,
4028 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004029 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004030 else
4031#endif /* MBEDTLS_GCM_C */
4032#if defined(MBEDTLS_CCM_C)
4033 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004034 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004035 status = mbedtls_to_psa_error(
4036 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4037 plaintext_length,
4038 nonce, nonce_length,
4039 additional_data,
4040 additional_data_length,
4041 plaintext, ciphertext,
4042 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004043 }
mohammad16035c8845f2018-05-09 05:40:09 -07004044 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004045#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004046#if defined(MBEDTLS_CHACHAPOLY_C)
4047 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4048 {
4049 if( nonce_length != 12 || operation.tag_length != 16 )
4050 {
4051 status = PSA_ERROR_NOT_SUPPORTED;
4052 goto exit;
4053 }
4054 status = mbedtls_to_psa_error(
4055 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4056 plaintext_length,
4057 nonce,
4058 additional_data,
4059 additional_data_length,
4060 plaintext,
4061 ciphertext,
4062 tag ) );
4063 }
4064 else
4065#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004066 {
mohammad1603554faad2018-06-03 15:07:38 +03004067 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004068 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004069
Gilles Peskineedf9a652018-08-17 18:11:56 +02004070 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4071 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004072
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004074 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004075 if( status == PSA_SUCCESS )
4076 *ciphertext_length = plaintext_length + operation.tag_length;
4077 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004078}
4079
Gilles Peskineee652a32018-06-01 19:23:52 +02004080/* Locate the tag in a ciphertext buffer containing the encrypted data
4081 * followed by the tag. Return the length of the part preceding the tag in
4082 * *plaintext_length. This is the size of the plaintext in modes where
4083 * the encrypted data has the same size as the plaintext, such as
4084 * CCM and GCM. */
4085static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4086 const uint8_t *ciphertext,
4087 size_t ciphertext_length,
4088 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004089 const uint8_t **p_tag )
4090{
4091 size_t payload_length;
4092 if( tag_length > ciphertext_length )
4093 return( PSA_ERROR_INVALID_ARGUMENT );
4094 payload_length = ciphertext_length - tag_length;
4095 if( payload_length > plaintext_size )
4096 return( PSA_ERROR_BUFFER_TOO_SMALL );
4097 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004098 return( PSA_SUCCESS );
4099}
4100
Gilles Peskinec5487a82018-12-03 18:08:14 +01004101psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004102 psa_algorithm_t alg,
4103 const uint8_t *nonce,
4104 size_t nonce_length,
4105 const uint8_t *additional_data,
4106 size_t additional_data_length,
4107 const uint8_t *ciphertext,
4108 size_t ciphertext_length,
4109 uint8_t *plaintext,
4110 size_t plaintext_size,
4111 size_t *plaintext_length )
4112{
mohammad16035955c982018-04-26 00:53:03 +03004113 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114 aead_operation_t operation;
4115 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004116
Gilles Peskineee652a32018-06-01 19:23:52 +02004117 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004118
Gilles Peskinec5487a82018-12-03 18:08:14 +01004119 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004120 if( status != PSA_SUCCESS )
4121 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004122
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004123 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4124 ciphertext, ciphertext_length,
4125 plaintext_size, &tag );
4126 if( status != PSA_SUCCESS )
4127 goto exit;
4128
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004129#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004130 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004131 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004132 status = mbedtls_to_psa_error(
4133 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4134 ciphertext_length - operation.tag_length,
4135 nonce, nonce_length,
4136 additional_data,
4137 additional_data_length,
4138 tag, operation.tag_length,
4139 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004140 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004141 else
4142#endif /* MBEDTLS_GCM_C */
4143#if defined(MBEDTLS_CCM_C)
4144 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004145 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004146 status = mbedtls_to_psa_error(
4147 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4148 ciphertext_length - operation.tag_length,
4149 nonce, nonce_length,
4150 additional_data,
4151 additional_data_length,
4152 ciphertext, plaintext,
4153 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004154 }
mohammad160339574652018-06-01 04:39:53 -07004155 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004156#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004157#if defined(MBEDTLS_CHACHAPOLY_C)
4158 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4159 {
4160 if( nonce_length != 12 || operation.tag_length != 16 )
4161 {
4162 status = PSA_ERROR_NOT_SUPPORTED;
4163 goto exit;
4164 }
4165 status = mbedtls_to_psa_error(
4166 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4167 ciphertext_length - operation.tag_length,
4168 nonce,
4169 additional_data,
4170 additional_data_length,
4171 tag,
4172 ciphertext,
4173 plaintext ) );
4174 }
4175 else
4176#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004177 {
mohammad1603554faad2018-06-03 15:07:38 +03004178 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004179 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004180
Gilles Peskineedf9a652018-08-17 18:11:56 +02004181 if( status != PSA_SUCCESS && plaintext_size != 0 )
4182 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004183
Gilles Peskineedf9a652018-08-17 18:11:56 +02004184exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004185 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004186 if( status == PSA_SUCCESS )
4187 *plaintext_length = ciphertext_length - operation.tag_length;
4188 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004189}
4190
Gilles Peskinea0655c32018-04-30 17:06:50 +02004191
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004192
Gilles Peskine20035e32018-02-03 22:44:14 +01004193/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194/* Generators */
4195/****************************************************************/
4196
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004197#define HKDF_STATE_INIT 0 /* no input yet */
4198#define HKDF_STATE_STARTED 1 /* got salt */
4199#define HKDF_STATE_KEYED 2 /* got key */
4200#define HKDF_STATE_OUTPUT 3 /* output started */
4201
Gilles Peskinecbe66502019-05-16 16:59:18 +02004202static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004203 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004204{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4206 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004207 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004209}
4210
4211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004212psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004213{
4214 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004215 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004216 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004217 {
4218 /* The object has (apparently) been initialized but it is not
4219 * in use. It's ok to call abort on such an object, and there's
4220 * nothing to do. */
4221 }
4222 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004223#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004224 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004225 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004226 mbedtls_free( operation->ctx.hkdf.info );
4227 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004228 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004229 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004230 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004231 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004232 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004233#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004234 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004235 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004236 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4237 operation->ctx.tls12_prf.key_len );
4238 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004239 }
Hanno Becker580fba12018-11-13 20:50:45 +00004240
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004241 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004242 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004243 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4244 operation->ctx.tls12_prf.Ai_with_seed_len );
4245 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004246 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004247#else
4248 if( operation->ctx.tls12_prf.seed != NULL )
4249 {
4250 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4251 operation->ctx.tls12_prf.seed_length );
4252 mbedtls_free( operation->ctx.tls12_prf.seed );
4253 }
4254
4255 if( operation->ctx.tls12_prf.label != NULL )
4256 {
4257 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4258 operation->ctx.tls12_prf.label_length );
4259 mbedtls_free( operation->ctx.tls12_prf.label );
4260 }
4261
4262 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4263
4264 /* We leave the fields Ai and output_block to be erased safely by the
4265 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004266#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004267 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004268 else
4269#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004270 {
4271 status = PSA_ERROR_BAD_STATE;
4272 }
Janos Follath083036a2019-06-11 10:22:26 +01004273 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004274 return( status );
4275}
4276
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004277psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004278 size_t *capacity)
4279{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004280 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004281 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004282 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004283 return PSA_ERROR_BAD_STATE;
4284 }
4285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004286 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004287 return( PSA_SUCCESS );
4288}
4289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004291 size_t capacity )
4292{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004294 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004295 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004296 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004298 return( PSA_SUCCESS );
4299}
4300
Gilles Peskinebef7f142018-07-12 17:22:21 +02004301#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004302/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004303 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004304static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004305 psa_algorithm_t hash_alg,
4306 uint8_t *output,
4307 size_t output_length )
4308{
4309 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4310 psa_status_t status;
4311
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004312 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4313 return( PSA_ERROR_BAD_STATE );
4314 hkdf->state = HKDF_STATE_OUTPUT;
4315
Gilles Peskinebef7f142018-07-12 17:22:21 +02004316 while( output_length != 0 )
4317 {
4318 /* Copy what remains of the current block */
4319 uint8_t n = hash_length - hkdf->offset_in_block;
4320 if( n > output_length )
4321 n = (uint8_t) output_length;
4322 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4323 output += n;
4324 output_length -= n;
4325 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004326 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004327 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004328 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004329 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004330 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004331 * object was corrupted or if this function is called directly
4332 * inside the library. */
4333 if( hkdf->block_number == 0xff )
4334 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004335
4336 /* We need a new block */
4337 ++hkdf->block_number;
4338 hkdf->offset_in_block = 0;
4339 status = psa_hmac_setup_internal( &hkdf->hmac,
4340 hkdf->prk, hash_length,
4341 hash_alg );
4342 if( status != PSA_SUCCESS )
4343 return( status );
4344 if( hkdf->block_number != 1 )
4345 {
4346 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4347 hkdf->output_block,
4348 hash_length );
4349 if( status != PSA_SUCCESS )
4350 return( status );
4351 }
4352 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4353 hkdf->info,
4354 hkdf->info_length );
4355 if( status != PSA_SUCCESS )
4356 return( status );
4357 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4358 &hkdf->block_number, 1 );
4359 if( status != PSA_SUCCESS )
4360 return( status );
4361 status = psa_hmac_finish_internal( &hkdf->hmac,
4362 hkdf->output_block,
4363 sizeof( hkdf->output_block ) );
4364 if( status != PSA_SUCCESS )
4365 return( status );
4366 }
4367
4368 return( PSA_SUCCESS );
4369}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370
Janos Follath999f6482019-06-11 12:04:10 +01004371#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004372static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4373 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004374 psa_algorithm_t alg )
4375{
4376 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4377 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4378 psa_hmac_internal_data hmac;
4379 psa_status_t status, cleanup_status;
4380
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004381 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004382 size_t Ai_len;
4383
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004384 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004385 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004387 * object was corrupted or if this function is called directly
4388 * inside the library. */
4389 if( tls12_prf->block_number == 0xff )
4390 return( PSA_ERROR_BAD_STATE );
4391
4392 /* We need a new block */
4393 ++tls12_prf->block_number;
4394 tls12_prf->offset_in_block = 0;
4395
4396 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4397 *
4398 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4399 *
4400 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4401 * HMAC_hash(secret, A(2) + seed) +
4402 * HMAC_hash(secret, A(3) + seed) + ...
4403 *
4404 * A(0) = seed
4405 * A(i) = HMAC_hash( secret, A(i-1) )
4406 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004407 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004408 * `HMAC_hash(secret, A(i) + seed)` from which the output
4409 * is currently extracted as `output_block`, while
4410 * `A(i) + seed` is stored in `Ai_with_seed`.
4411 *
4412 * Generating a new block means recalculating `Ai_with_seed`
4413 * from the A(i)-part of it, and afterwards recalculating
4414 * `output_block`.
4415 *
4416 * A(0) is computed at setup time.
4417 *
4418 */
4419
4420 psa_hmac_init_internal( &hmac );
4421
4422 /* We must distinguish the calculation of A(1) from those
4423 * of A(2) and higher, because A(0)=seed has a different
4424 * length than the other A(i). */
4425 if( tls12_prf->block_number == 1 )
4426 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004427 Ai = tls12_prf->Ai_with_seed + hash_length;
4428 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004429 }
4430 else
4431 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004432 Ai = tls12_prf->Ai_with_seed;
4433 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004434 }
4435
Hanno Becker3b339e22018-11-13 20:56:14 +00004436 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4437 status = psa_hmac_setup_internal( &hmac,
4438 tls12_prf->key,
4439 tls12_prf->key_len,
4440 hash_alg );
4441 if( status != PSA_SUCCESS )
4442 goto cleanup;
4443
4444 status = psa_hash_update( &hmac.hash_ctx,
4445 Ai, Ai_len );
4446 if( status != PSA_SUCCESS )
4447 goto cleanup;
4448
4449 status = psa_hmac_finish_internal( &hmac,
4450 tls12_prf->Ai_with_seed,
4451 hash_length );
4452 if( status != PSA_SUCCESS )
4453 goto cleanup;
4454
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004455 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4456 status = psa_hmac_setup_internal( &hmac,
4457 tls12_prf->key,
4458 tls12_prf->key_len,
4459 hash_alg );
4460 if( status != PSA_SUCCESS )
4461 goto cleanup;
4462
4463 status = psa_hash_update( &hmac.hash_ctx,
4464 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004465 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004466 if( status != PSA_SUCCESS )
4467 goto cleanup;
4468
4469 status = psa_hmac_finish_internal( &hmac,
4470 tls12_prf->output_block,
4471 hash_length );
4472 if( status != PSA_SUCCESS )
4473 goto cleanup;
4474
4475cleanup:
4476
4477 cleanup_status = psa_hmac_abort_internal( &hmac );
4478 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4479 status = cleanup_status;
4480
4481 return( status );
4482}
Janos Follath7742fee2019-06-17 12:58:10 +01004483#else
4484static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4485 psa_tls12_prf_key_derivation_t *tls12_prf,
4486 psa_algorithm_t alg )
4487{
4488 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4489 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004490 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4491 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004492
Janos Follath7742fee2019-06-17 12:58:10 +01004493 /* We can't be wanting more output after block 0xff, otherwise
4494 * the capacity check in psa_key_derivation_output_bytes() would have
4495 * prevented this call. It could happen only if the operation
4496 * object was corrupted or if this function is called directly
4497 * inside the library. */
4498 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004499 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004500
4501 /* We need a new block */
4502 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004503 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004504
4505 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4506 *
4507 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4508 *
4509 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4510 * HMAC_hash(secret, A(2) + seed) +
4511 * HMAC_hash(secret, A(3) + seed) + ...
4512 *
4513 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004514 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004515 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004516 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004517 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004518 * is currently extracted as `output_block` and where i is
4519 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004520 */
4521
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 /* Save the hash context before using it, to preserve the hash state with
4523 * only the inner padding in it. We need this, because inner padding depends
4524 * on the key (secret in the RFC's terminology). */
4525 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4526 if( status != PSA_SUCCESS )
4527 goto cleanup;
4528
4529 /* Calculate A(i) where i = tls12_prf->block_number. */
4530 if( tls12_prf->block_number == 1 )
4531 {
4532 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4533 * the variable seed and in this instance means it in the context of the
4534 * P_hash function, where seed = label + seed.) */
4535 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4536 tls12_prf->label, tls12_prf->label_length );
4537 if( status != PSA_SUCCESS )
4538 goto cleanup;
4539 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4540 tls12_prf->seed, tls12_prf->seed_length );
4541 if( status != PSA_SUCCESS )
4542 goto cleanup;
4543 }
4544 else
4545 {
4546 /* A(i) = HMAC_hash(secret, A(i-1)) */
4547 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4548 tls12_prf->Ai, hash_length );
4549 if( status != PSA_SUCCESS )
4550 goto cleanup;
4551 }
4552
4553 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4554 tls12_prf->Ai, hash_length );
4555 if( status != PSA_SUCCESS )
4556 goto cleanup;
4557 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4558 if( status != PSA_SUCCESS )
4559 goto cleanup;
4560
4561 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4562 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4563 tls12_prf->Ai, hash_length );
4564 if( status != PSA_SUCCESS )
4565 goto cleanup;
4566 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4567 tls12_prf->label, tls12_prf->label_length );
4568 if( status != PSA_SUCCESS )
4569 goto cleanup;
4570 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4571 tls12_prf->seed, tls12_prf->seed_length );
4572 if( status != PSA_SUCCESS )
4573 goto cleanup;
4574 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4575 tls12_prf->output_block, hash_length );
4576 if( status != PSA_SUCCESS )
4577 goto cleanup;
4578 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4579 if( status != PSA_SUCCESS )
4580 goto cleanup;
4581
Janos Follath7742fee2019-06-17 12:58:10 +01004582
4583cleanup:
4584
Janos Follathea29bfb2019-06-19 12:21:20 +01004585 cleanup_status = psa_hash_abort( &backup );
4586 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4587 status = cleanup_status;
4588
4589 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004590}
Janos Follath999f6482019-06-11 12:04:10 +01004591#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004592
Janos Follath999f6482019-06-11 12:04:10 +01004593#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004595 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004596static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004597 psa_tls12_prf_key_derivation_t *tls12_prf,
4598 psa_algorithm_t alg,
4599 uint8_t *output,
4600 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004601{
4602 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4603 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4604 psa_status_t status;
4605
4606 while( output_length != 0 )
4607 {
4608 /* Copy what remains of the current block */
4609 uint8_t n = hash_length - tls12_prf->offset_in_block;
4610
4611 /* Check if we have fully processed the current block. */
4612 if( n == 0 )
4613 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004614 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004615 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004616 if( status != PSA_SUCCESS )
4617 return( status );
4618
4619 continue;
4620 }
4621
4622 if( n > output_length )
4623 n = (uint8_t) output_length;
4624 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4625 n );
4626 output += n;
4627 output_length -= n;
4628 tls12_prf->offset_in_block += n;
4629 }
4630
4631 return( PSA_SUCCESS );
4632}
Janos Follath844eb0e2019-06-19 12:10:49 +01004633#else
4634static psa_status_t psa_key_derivation_tls12_prf_read(
4635 psa_tls12_prf_key_derivation_t *tls12_prf,
4636 psa_algorithm_t alg,
4637 uint8_t *output,
4638 size_t output_length )
4639{
4640 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4641 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4642 psa_status_t status;
4643 uint8_t offset, length;
4644
4645 while( output_length != 0 )
4646 {
4647 /* Check if we have fully processed the current block. */
4648 if( tls12_prf->left_in_block == 0 )
4649 {
4650 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4651 alg );
4652 if( status != PSA_SUCCESS )
4653 return( status );
4654
4655 continue;
4656 }
4657
4658 if( tls12_prf->left_in_block > output_length )
4659 length = (uint8_t) output_length;
4660 else
4661 length = tls12_prf->left_in_block;
4662
4663 offset = hash_length - tls12_prf->left_in_block;
4664 memcpy( output, tls12_prf->output_block + offset, length );
4665 output += length;
4666 output_length -= length;
4667 tls12_prf->left_in_block -= length;
4668 }
4669
4670 return( PSA_SUCCESS );
4671}
Janos Follath999f6482019-06-11 12:04:10 +01004672#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004673#endif /* MBEDTLS_MD_C */
4674
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004675psa_status_t psa_key_derivation_output_bytes(
4676 psa_key_derivation_operation_t *operation,
4677 uint8_t *output,
4678 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004679{
4680 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004684 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004686 return PSA_ERROR_BAD_STATE;
4687 }
4688
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004692 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004694 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004695 goto exit;
4696 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004698 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004700 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004701 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4702 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004704 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004705 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004706 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708
Gilles Peskinebef7f142018-07-12 17:22:21 +02004709#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004710 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004711 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004712 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004714 output, output_length );
4715 }
Janos Follathadbec812019-06-14 11:05:39 +01004716 else
Janos Follathadbec812019-06-14 11:05:39 +01004717 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004718 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004719 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004721 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004722 output_length );
4723 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004724 else
4725#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726 {
4727 return( PSA_ERROR_BAD_STATE );
4728 }
4729
4730exit:
4731 if( status != PSA_SUCCESS )
4732 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004733 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 * This allows us to differentiate between exhausted operations and
4735 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4736 * operations. */
4737 psa_algorithm_t alg = operation->alg;
4738 psa_key_derivation_abort( operation );
4739 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 memset( output, '!', output_length );
4741 }
4742 return( status );
4743}
4744
Gilles Peskine08542d82018-07-19 17:05:42 +02004745#if defined(MBEDTLS_DES_C)
4746static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4747{
4748 if( data_size >= 8 )
4749 mbedtls_des_key_set_parity( data );
4750 if( data_size >= 16 )
4751 mbedtls_des_key_set_parity( data + 8 );
4752 if( data_size >= 24 )
4753 mbedtls_des_key_set_parity( data + 16 );
4754}
4755#endif /* MBEDTLS_DES_C */
4756
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004757static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004758 psa_key_slot_t *slot,
4759 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004761{
4762 uint8_t *data = NULL;
4763 size_t bytes = PSA_BITS_TO_BYTES( bits );
4764 psa_status_t status;
4765
Gilles Peskine8e338702019-07-30 20:06:31 +02004766 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004767 return( PSA_ERROR_INVALID_ARGUMENT );
4768 if( bits % 8 != 0 )
4769 return( PSA_ERROR_INVALID_ARGUMENT );
4770 data = mbedtls_calloc( 1, bytes );
4771 if( data == NULL )
4772 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4773
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004775 if( status != PSA_SUCCESS )
4776 goto exit;
4777#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004778 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004779 psa_des_set_key_parity( data, bytes );
4780#endif /* MBEDTLS_DES_C */
4781 status = psa_import_key_into_slot( slot, data, bytes );
4782
4783exit:
4784 mbedtls_free( data );
4785 return( status );
4786}
4787
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004788psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004790 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004791{
4792 psa_status_t status;
4793 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004794 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004795 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4797 if( driver != NULL )
4798 {
4799 /* Deriving a key in a secure element is not implemented yet. */
4800 status = PSA_ERROR_NOT_SUPPORTED;
4801 }
4802#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004803 if( status == PSA_SUCCESS )
4804 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004805 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004806 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004808 }
4809 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004810 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004811 if( status != PSA_SUCCESS )
4812 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004813 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004814 *handle = 0;
4815 }
4816 return( status );
4817}
4818
Gilles Peskineeab56e42018-07-12 17:12:33 +02004819
4820
4821/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004822/* Key derivation */
4823/****************************************************************/
4824
Gilles Peskinea05219c2018-11-16 16:02:56 +01004825#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004826#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004828 * of the HKDF algorithm.
4829 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004830 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004831 * to potentially free embedded data structures and wipe confidential data.
4832 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004833static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004834 const uint8_t *secret,
4835 size_t secret_length,
4836 psa_algorithm_t hash_alg,
4837 const uint8_t *salt,
4838 size_t salt_length,
4839 const uint8_t *label,
4840 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004841{
4842 psa_status_t status;
4843 status = psa_hmac_setup_internal( &hkdf->hmac,
4844 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004845 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004846 if( status != PSA_SUCCESS )
4847 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004848 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004849 if( status != PSA_SUCCESS )
4850 return( status );
4851 status = psa_hmac_finish_internal( &hkdf->hmac,
4852 hkdf->prk,
4853 sizeof( hkdf->prk ) );
4854 if( status != PSA_SUCCESS )
4855 return( status );
4856 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4857 hkdf->block_number = 0;
4858 hkdf->info_length = label_length;
4859 if( label_length != 0 )
4860 {
4861 hkdf->info = mbedtls_calloc( 1, label_length );
4862 if( hkdf->info == NULL )
4863 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4864 memcpy( hkdf->info, label, label_length );
4865 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004866 hkdf->state = HKDF_STATE_KEYED;
4867 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004868 return( PSA_SUCCESS );
4869}
Janos Follath71a4c912019-06-11 09:14:47 +01004870#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004871#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004872
Gilles Peskinea05219c2018-11-16 16:02:56 +01004873#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004874#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004876 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004877 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004878 * to potentially free embedded data structures and wipe confidential data.
4879 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004880static psa_status_t psa_key_derivation_tls12_prf_setup(
4881 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004882 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004883 size_t key_len,
4884 psa_algorithm_t hash_alg,
4885 const uint8_t *salt,
4886 size_t salt_length,
4887 const uint8_t *label,
4888 size_t label_length )
4889{
4890 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004891 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4892 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004893
4894 tls12_prf->key = mbedtls_calloc( 1, key_len );
4895 if( tls12_prf->key == NULL )
4896 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4897 tls12_prf->key_len = key_len;
4898 memcpy( tls12_prf->key, key, key_len );
4899
Hanno Becker580fba12018-11-13 20:50:45 +00004900 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004901 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004902 if( overflow )
4903 return( PSA_ERROR_INVALID_ARGUMENT );
4904
4905 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4906 if( tls12_prf->Ai_with_seed == NULL )
4907 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4908 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4909
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004910 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4911 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004912 if( label_length != 0 )
4913 {
4914 memcpy( tls12_prf->Ai_with_seed + hash_length,
4915 label, label_length );
4916 }
4917
4918 if( salt_length != 0 )
4919 {
4920 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4921 salt, salt_length );
4922 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004923
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004924 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004925 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004926 tls12_prf->block_number = 0;
4927 tls12_prf->offset_in_block = hash_length;
4928
4929 return( PSA_SUCCESS );
4930}
Janos Follath71a4c912019-06-11 09:14:47 +01004931#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004932
Janos Follath71a4c912019-06-11 09:14:47 +01004933#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004935static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4936 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004937 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004938 size_t psk_len,
4939 psa_algorithm_t hash_alg,
4940 const uint8_t *salt,
4941 size_t salt_length,
4942 const uint8_t *label,
4943 size_t label_length )
4944{
4945 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004946 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004947
4948 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4949 return( PSA_ERROR_INVALID_ARGUMENT );
4950
4951 /* Quoting RFC 4279, Section 2:
4952 *
4953 * The premaster secret is formed as follows: if the PSK is N octets
4954 * long, concatenate a uint16 with the value N, N zero octets, a second
4955 * uint16 with the value N, and the PSK itself.
4956 */
4957
4958 pms[0] = ( psk_len >> 8 ) & 0xff;
4959 pms[1] = ( psk_len >> 0 ) & 0xff;
4960 memset( pms + 2, 0, psk_len );
4961 pms[2 + psk_len + 0] = pms[0];
4962 pms[2 + psk_len + 1] = pms[1];
4963 memcpy( pms + 4 + psk_len, psk, psk_len );
4964
Gilles Peskinecbe66502019-05-16 16:59:18 +02004965 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004966 pms, 4 + 2 * psk_len,
4967 hash_alg,
4968 salt, salt_length,
4969 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004970
Gilles Peskine3f108122018-12-07 18:14:53 +01004971 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004972 return( status );
4973}
Janos Follath71a4c912019-06-11 09:14:47 +01004974#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004975#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004976
Janos Follath71a4c912019-06-11 09:14:47 +01004977#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004978/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004979 * to potentially free embedded data structures and wipe confidential data.
4980 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004981static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004983 const uint8_t *secret, size_t secret_length,
4984 psa_algorithm_t alg,
4985 const uint8_t *salt, size_t salt_length,
4986 const uint8_t *label, size_t label_length,
4987 size_t capacity )
4988{
4989 psa_status_t status;
4990 size_t max_capacity;
4991
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004992 /* Set operation->alg even on failure so that abort knows what to do. */
4993 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004994
4995#if defined(MBEDTLS_MD_C)
4996 if( PSA_ALG_IS_HKDF( alg ) )
4997 {
4998 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4999 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5000 if( hash_size == 0 )
5001 return( PSA_ERROR_NOT_SUPPORTED );
5002 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005004 secret, secret_length,
5005 hash_alg,
5006 salt, salt_length,
5007 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005008 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005009 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5010 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5011 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005012 {
5013 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5014 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5015
5016 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5017 if( hash_alg != PSA_ALG_SHA_256 &&
5018 hash_alg != PSA_ALG_SHA_384 )
5019 {
5020 return( PSA_ERROR_NOT_SUPPORTED );
5021 }
5022
5023 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005024
5025 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5026 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005028 secret, secret_length,
5029 hash_alg, salt, salt_length,
5030 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005031 }
5032 else
5033 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005034 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005036 secret, secret_length,
5037 hash_alg, salt, salt_length,
5038 label, label_length );
5039 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005040 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005041 else
5042#endif
5043 {
5044 return( PSA_ERROR_NOT_SUPPORTED );
5045 }
5046
5047 if( status != PSA_SUCCESS )
5048 return( status );
5049
5050 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005051 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005052 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005054 else
5055 return( PSA_ERROR_INVALID_ARGUMENT );
5056
5057 return( PSA_SUCCESS );
5058}
Janos Follath71a4c912019-06-11 09:14:47 +01005059#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005060
Janos Follath71a4c912019-06-11 09:14:47 +01005061#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005063 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005064 psa_algorithm_t alg,
5065 const uint8_t *salt,
5066 size_t salt_length,
5067 const uint8_t *label,
5068 size_t label_length,
5069 size_t capacity )
5070{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005071 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005072 psa_status_t status;
5073
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005075 return( PSA_ERROR_BAD_STATE );
5076
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005077 /* Make sure that alg is a key derivation algorithm. This prevents
5078 * key selection algorithms, which psa_key_derivation_internal
5079 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005080 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5081 return( PSA_ERROR_INVALID_ARGUMENT );
5082
Gilles Peskine28f8f302019-07-24 13:30:31 +02005083 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005084 if( status != PSA_SUCCESS )
5085 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005086
Gilles Peskine8e338702019-07-30 20:06:31 +02005087 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005088 return( PSA_ERROR_INVALID_ARGUMENT );
5089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005091 slot->data.raw.data,
5092 slot->data.raw.bytes,
5093 alg,
5094 salt, salt_length,
5095 label, label_length,
5096 capacity );
5097 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005099 return( status );
5100}
Janos Follath71a4c912019-06-11 09:14:47 +01005101#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005102
Gilles Peskine969c5d62019-01-16 15:53:06 +01005103static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005105 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005106{
Janos Follath5fe19732019-06-20 15:09:30 +01005107 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5108 * initialisers for this union leave some bytes unspecified.) */
5109 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5110
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005113 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5114 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5115 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005117 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005118 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5119 if( hash_size == 0 )
5120 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005121 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5122 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005123 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005124 {
5125 return( PSA_ERROR_NOT_SUPPORTED );
5126 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005128 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005129 }
5130#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005131 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005133}
5134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005136 psa_algorithm_t alg )
5137{
5138 psa_status_t status;
5139
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005141 return( PSA_ERROR_BAD_STATE );
5142
Gilles Peskine6843c292019-01-18 16:44:49 +01005143 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5144 return( PSA_ERROR_INVALID_ARGUMENT );
5145 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005146 {
5147 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005149 }
5150 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5151 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005153 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154 else
5155 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005156
5157 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005159 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005160}
5161
5162#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005163static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164 psa_algorithm_t hash_alg,
5165 psa_key_derivation_step_t step,
5166 const uint8_t *data,
5167 size_t data_length )
5168{
5169 psa_status_t status;
5170 switch( step )
5171 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005172 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005173 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005175 status = psa_hmac_setup_internal( &hkdf->hmac,
5176 data, data_length,
5177 hash_alg );
5178 if( status != PSA_SUCCESS )
5179 return( status );
5180 hkdf->state = HKDF_STATE_STARTED;
5181 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005182 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183 /* If no salt was provided, use an empty salt. */
5184 if( hkdf->state == HKDF_STATE_INIT )
5185 {
5186 status = psa_hmac_setup_internal( &hkdf->hmac,
5187 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005188 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189 if( status != PSA_SUCCESS )
5190 return( status );
5191 hkdf->state = HKDF_STATE_STARTED;
5192 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005193 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005195 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5196 data, data_length );
5197 if( status != PSA_SUCCESS )
5198 return( status );
5199 status = psa_hmac_finish_internal( &hkdf->hmac,
5200 hkdf->prk,
5201 sizeof( hkdf->prk ) );
5202 if( status != PSA_SUCCESS )
5203 return( status );
5204 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5205 hkdf->block_number = 0;
5206 hkdf->state = HKDF_STATE_KEYED;
5207 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005208 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 if( hkdf->state == HKDF_STATE_OUTPUT )
5210 return( PSA_ERROR_BAD_STATE );
5211 if( hkdf->info_set )
5212 return( PSA_ERROR_BAD_STATE );
5213 hkdf->info_length = data_length;
5214 if( data_length != 0 )
5215 {
5216 hkdf->info = mbedtls_calloc( 1, data_length );
5217 if( hkdf->info == NULL )
5218 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5219 memcpy( hkdf->info, data, data_length );
5220 }
5221 hkdf->info_set = 1;
5222 return( PSA_SUCCESS );
5223 default:
5224 return( PSA_ERROR_INVALID_ARGUMENT );
5225 }
5226}
Janos Follathb03233e2019-06-11 15:30:30 +01005227
5228#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5229static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5230 psa_algorithm_t hash_alg,
5231 psa_key_derivation_step_t step,
5232 const uint8_t *data,
5233 size_t data_length )
5234{
5235 (void) prf;
5236 (void) hash_alg;
5237 (void) step;
5238 (void) data;
5239 (void) data_length;
5240
5241 return( PSA_ERROR_INVALID_ARGUMENT );
5242}
Janos Follath6660f0e2019-06-17 08:44:03 +01005243
5244static psa_status_t psa_tls12_prf_psk_to_ms_input(
5245 psa_tls12_prf_key_derivation_t *prf,
5246 psa_algorithm_t hash_alg,
5247 psa_key_derivation_step_t step,
5248 const uint8_t *data,
5249 size_t data_length )
5250{
5251 (void) prf;
5252 (void) hash_alg;
5253 (void) step;
5254 (void) data;
5255 (void) data_length;
5256
5257 return( PSA_ERROR_INVALID_ARGUMENT );
5258}
Janos Follathb03233e2019-06-11 15:30:30 +01005259#else
Janos Follathf08e2652019-06-13 09:05:41 +01005260static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5261 const uint8_t *data,
5262 size_t data_length )
5263{
5264 if( prf->state != TLS12_PRF_STATE_INIT )
5265 return( PSA_ERROR_BAD_STATE );
5266
Janos Follathd6dce9f2019-07-04 09:11:38 +01005267 if( data_length != 0 )
5268 {
5269 prf->seed = mbedtls_calloc( 1, data_length );
5270 if( prf->seed == NULL )
5271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005272
Janos Follathd6dce9f2019-07-04 09:11:38 +01005273 memcpy( prf->seed, data, data_length );
5274 prf->seed_length = data_length;
5275 }
Janos Follathf08e2652019-06-13 09:05:41 +01005276
5277 prf->state = TLS12_PRF_STATE_SEED_SET;
5278
5279 return( PSA_SUCCESS );
5280}
5281
Janos Follath81550542019-06-13 14:26:34 +01005282static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5283 psa_algorithm_t hash_alg,
5284 const uint8_t *data,
5285 size_t data_length )
5286{
5287 psa_status_t status;
5288 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5289 return( PSA_ERROR_BAD_STATE );
5290
5291 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5292 if( status != PSA_SUCCESS )
5293 return( status );
5294
5295 prf->state = TLS12_PRF_STATE_KEY_SET;
5296
5297 return( PSA_SUCCESS );
5298}
5299
Janos Follath6660f0e2019-06-17 08:44:03 +01005300static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5301 psa_tls12_prf_key_derivation_t *prf,
5302 psa_algorithm_t hash_alg,
5303 const uint8_t *data,
5304 size_t data_length )
5305{
5306 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005307 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5308 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005309
5310 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5311 return( PSA_ERROR_INVALID_ARGUMENT );
5312
5313 /* Quoting RFC 4279, Section 2:
5314 *
5315 * The premaster secret is formed as follows: if the PSK is N octets
5316 * long, concatenate a uint16 with the value N, N zero octets, a second
5317 * uint16 with the value N, and the PSK itself.
5318 */
5319
Janos Follath40e13932019-06-26 13:22:29 +01005320 *cur++ = ( data_length >> 8 ) & 0xff;
5321 *cur++ = ( data_length >> 0 ) & 0xff;
5322 memset( cur, 0, data_length );
5323 cur += data_length;
5324 *cur++ = pms[0];
5325 *cur++ = pms[1];
5326 memcpy( cur, data, data_length );
5327 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005328
Janos Follath40e13932019-06-26 13:22:29 +01005329 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005330
5331 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5332 return( status );
5333}
5334
Janos Follath63028dd2019-06-13 09:15:47 +01005335static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5336 const uint8_t *data,
5337 size_t data_length )
5338{
5339 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5340 return( PSA_ERROR_BAD_STATE );
5341
Janos Follathd6dce9f2019-07-04 09:11:38 +01005342 if( data_length != 0 )
5343 {
5344 prf->label = mbedtls_calloc( 1, data_length );
5345 if( prf->label == NULL )
5346 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005347
Janos Follathd6dce9f2019-07-04 09:11:38 +01005348 memcpy( prf->label, data, data_length );
5349 prf->label_length = data_length;
5350 }
Janos Follath63028dd2019-06-13 09:15:47 +01005351
5352 prf->state = TLS12_PRF_STATE_LABEL_SET;
5353
5354 return( PSA_SUCCESS );
5355}
5356
Janos Follathb03233e2019-06-11 15:30:30 +01005357static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5358 psa_algorithm_t hash_alg,
5359 psa_key_derivation_step_t step,
5360 const uint8_t *data,
5361 size_t data_length )
5362{
Janos Follathb03233e2019-06-11 15:30:30 +01005363 switch( step )
5364 {
Janos Follathf08e2652019-06-13 09:05:41 +01005365 case PSA_KEY_DERIVATION_INPUT_SEED:
5366 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005367 case PSA_KEY_DERIVATION_INPUT_SECRET:
5368 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005369 case PSA_KEY_DERIVATION_INPUT_LABEL:
5370 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005371 default:
5372 return( PSA_ERROR_INVALID_ARGUMENT );
5373 }
5374}
Janos Follath6660f0e2019-06-17 08:44:03 +01005375
5376static psa_status_t psa_tls12_prf_psk_to_ms_input(
5377 psa_tls12_prf_key_derivation_t *prf,
5378 psa_algorithm_t hash_alg,
5379 psa_key_derivation_step_t step,
5380 const uint8_t *data,
5381 size_t data_length )
5382{
5383 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005384 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005385 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5386 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005387 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005388
5389 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5390}
Janos Follathb03233e2019-06-11 15:30:30 +01005391#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005392#endif /* MBEDTLS_MD_C */
5393
Janos Follathb80a94e2019-06-12 15:54:46 +01005394static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005395 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005396 psa_key_derivation_step_t step,
5397 const uint8_t *data,
5398 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005399{
5400 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005402
5403#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005404 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005405 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005408 step, data, data_length );
5409 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005410 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005411#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005412#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005413 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005414 {
Janos Follathb03233e2019-06-11 15:30:30 +01005415 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5416 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5417 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005418 }
5419 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5420 {
5421 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5422 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5423 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005424 }
5425 else
5426#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005427 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005429 return( PSA_ERROR_BAD_STATE );
5430 }
5431
5432 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005434 return( status );
5435}
5436
Janos Follath51f4a0f2019-06-14 11:35:55 +01005437psa_status_t psa_key_derivation_input_bytes(
5438 psa_key_derivation_operation_t *operation,
5439 psa_key_derivation_step_t step,
5440 const uint8_t *data,
5441 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005442{
Janos Follathc5621512019-06-14 11:27:57 +01005443 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5444 return( PSA_ERROR_INVALID_ARGUMENT );
5445
5446 return( psa_key_derivation_input_internal( operation, step,
5447 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005448}
5449
Janos Follath51f4a0f2019-06-14 11:35:55 +01005450psa_status_t psa_key_derivation_input_key(
5451 psa_key_derivation_operation_t *operation,
5452 psa_key_derivation_step_t step,
5453 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005454{
5455 psa_key_slot_t *slot;
5456 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005457 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005458 PSA_KEY_USAGE_DERIVE,
5459 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005460 if( status != PSA_SUCCESS )
5461 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005462 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005463 return( PSA_ERROR_INVALID_ARGUMENT );
5464 /* Don't allow a key to be used as an input that is usually public.
5465 * This is debatable. It's ok from a cryptographic perspective to
5466 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005467 * the material should be dedicated to a particular input step,
5468 * otherwise this may allow the key to be used in an unintended way
5469 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005470 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005471 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005472 return( psa_key_derivation_input_internal( operation,
5473 step,
5474 slot->data.raw.data,
5475 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005476}
5477
Gilles Peskineea0fb492018-07-12 17:17:20 +02005478
5479
5480/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005481/* Key agreement */
5482/****************************************************************/
5483
Gilles Peskinea05219c2018-11-16 16:02:56 +01005484#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005485static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5486 size_t peer_key_length,
5487 const mbedtls_ecp_keypair *our_key,
5488 uint8_t *shared_secret,
5489 size_t shared_secret_size,
5490 size_t *shared_secret_length )
5491{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005492 mbedtls_ecp_keypair *their_key = NULL;
5493 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005494 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005495 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005496
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005497 status = psa_import_ec_public_key(
5498 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5499 peer_key, peer_key_length,
5500 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005501 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005502 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005503
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005504 status = mbedtls_to_psa_error(
5505 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5506 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005507 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005508 status = mbedtls_to_psa_error(
5509 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5510 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005511 goto exit;
5512
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005513 status = mbedtls_to_psa_error(
5514 mbedtls_ecdh_calc_secret( &ecdh,
5515 shared_secret_length,
5516 shared_secret, shared_secret_size,
5517 mbedtls_ctr_drbg_random,
5518 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005519
5520exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005521 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005522 mbedtls_ecp_keypair_free( their_key );
5523 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005524 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005525}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005526#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005527
Gilles Peskine01d718c2018-09-18 12:01:02 +02005528#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5529
Gilles Peskine0216fe12019-04-11 21:23:21 +02005530static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5531 psa_key_slot_t *private_key,
5532 const uint8_t *peer_key,
5533 size_t peer_key_length,
5534 uint8_t *shared_secret,
5535 size_t shared_secret_size,
5536 size_t *shared_secret_length )
5537{
5538 switch( alg )
5539 {
5540#if defined(MBEDTLS_ECDH_C)
5541 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005542 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005543 return( PSA_ERROR_INVALID_ARGUMENT );
5544 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5545 private_key->data.ecp,
5546 shared_secret, shared_secret_size,
5547 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005548#endif /* MBEDTLS_ECDH_C */
5549 default:
5550 (void) private_key;
5551 (void) peer_key;
5552 (void) peer_key_length;
5553 (void) shared_secret;
5554 (void) shared_secret_size;
5555 (void) shared_secret_length;
5556 return( PSA_ERROR_NOT_SUPPORTED );
5557 }
5558}
5559
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005560/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005561 * to potentially free embedded data structures and wipe confidential data.
5562 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005564 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005565 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005566 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005567 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005568{
5569 psa_status_t status;
5570 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5571 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005573
5574 /* Step 1: run the secret agreement algorithm to generate the shared
5575 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005576 status = psa_key_agreement_raw_internal( ka_alg,
5577 private_key,
5578 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005579 shared_secret,
5580 sizeof( shared_secret ),
5581 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005582 if( status != PSA_SUCCESS )
5583 goto exit;
5584
5585 /* Step 2: set up the key derivation to generate key material from
5586 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005587 status = psa_key_derivation_input_internal( operation, step,
5588 shared_secret,
5589 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005590
Gilles Peskine01d718c2018-09-18 12:01:02 +02005591exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005592 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005593 return( status );
5594}
5595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005597 psa_key_derivation_step_t step,
5598 psa_key_handle_t private_key,
5599 const uint8_t *peer_key,
5600 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005601{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005602 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005603 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005605 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005606 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005607 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005608 if( status != PSA_SUCCESS )
5609 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005610 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005611 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005612 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005613 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005614 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005615 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005616}
5617
Gilles Peskinebe697d82019-05-16 18:00:41 +02005618psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5619 psa_key_handle_t private_key,
5620 const uint8_t *peer_key,
5621 size_t peer_key_length,
5622 uint8_t *output,
5623 size_t output_size,
5624 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005625{
5626 psa_key_slot_t *slot;
5627 psa_status_t status;
5628
5629 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5630 {
5631 status = PSA_ERROR_INVALID_ARGUMENT;
5632 goto exit;
5633 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005634 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005635 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005636 if( status != PSA_SUCCESS )
5637 goto exit;
5638
5639 status = psa_key_agreement_raw_internal( alg, slot,
5640 peer_key, peer_key_length,
5641 output, output_size,
5642 output_length );
5643
5644exit:
5645 if( status != PSA_SUCCESS )
5646 {
5647 /* If an error happens and is not handled properly, the output
5648 * may be used as a key to protect sensitive data. Arrange for such
5649 * a key to be random, which is likely to result in decryption or
5650 * verification errors. This is better than filling the buffer with
5651 * some constant data such as zeros, which would result in the data
5652 * being protected with a reproducible, easily knowable key.
5653 */
5654 psa_generate_random( output, output_size );
5655 *output_length = output_size;
5656 }
5657 return( status );
5658}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005659
5660
5661/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005662/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005663/****************************************************************/
5664
5665psa_status_t psa_generate_random( uint8_t *output,
5666 size_t output_size )
5667{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005668 int ret;
5669 GUARD_MODULE_INITIALIZED;
5670
Gilles Peskinef181eca2019-08-07 13:49:00 +02005671 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5672 {
5673 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5674 output,
5675 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5676 if( ret != 0 )
5677 return( mbedtls_to_psa_error( ret ) );
5678 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5679 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5680 }
5681
itayzafrir0adf0fc2018-09-06 16:24:41 +03005682 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005683 return( mbedtls_to_psa_error( ret ) );
5684}
5685
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005686#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5687#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005688
Gilles Peskine7228da22019-07-15 11:06:15 +02005689psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005690 size_t seed_size )
5691{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005692 if( global_data.initialized )
5693 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005694
5695 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5696 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5697 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5698 return( PSA_ERROR_INVALID_ARGUMENT );
5699
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005700 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005701}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005702#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005703
Gilles Peskinee56e8782019-04-26 17:34:02 +02005704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5705static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5706 size_t domain_parameters_size,
5707 int *exponent )
5708{
5709 size_t i;
5710 uint32_t acc = 0;
5711
5712 if( domain_parameters_size == 0 )
5713 {
5714 *exponent = 65537;
5715 return( PSA_SUCCESS );
5716 }
5717
5718 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5719 * support values that fit in a 32-bit integer, which is larger than
5720 * int on just about every platform anyway. */
5721 if( domain_parameters_size > sizeof( acc ) )
5722 return( PSA_ERROR_NOT_SUPPORTED );
5723 for( i = 0; i < domain_parameters_size; i++ )
5724 acc = ( acc << 8 ) | domain_parameters[i];
5725 if( acc > INT_MAX )
5726 return( PSA_ERROR_NOT_SUPPORTED );
5727 *exponent = acc;
5728 return( PSA_SUCCESS );
5729}
5730#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5731
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005732static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005733 psa_key_slot_t *slot, size_t bits,
5734 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005735{
Gilles Peskine8e338702019-07-30 20:06:31 +02005736 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005737
Gilles Peskinee56e8782019-04-26 17:34:02 +02005738 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005739 return( PSA_ERROR_INVALID_ARGUMENT );
5740
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005741 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005742 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005743 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005744 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005745 if( status != PSA_SUCCESS )
5746 return( status );
5747 status = psa_generate_random( slot->data.raw.data,
5748 slot->data.raw.bytes );
5749 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005750 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005751#if defined(MBEDTLS_DES_C)
5752 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005753 psa_des_set_key_parity( slot->data.raw.data,
5754 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005755#endif /* MBEDTLS_DES_C */
5756 }
5757 else
5758
5759#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005760 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005761 {
5762 mbedtls_rsa_context *rsa;
5763 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005764 int exponent;
5765 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005766 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5767 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005768 /* Accept only byte-aligned keys, for the same reasons as
5769 * in psa_import_rsa_key(). */
5770 if( bits % 8 != 0 )
5771 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005772 status = psa_read_rsa_exponent( domain_parameters,
5773 domain_parameters_size,
5774 &exponent );
5775 if( status != PSA_SUCCESS )
5776 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005777 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5778 if( rsa == NULL )
5779 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5780 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5781 ret = mbedtls_rsa_gen_key( rsa,
5782 mbedtls_ctr_drbg_random,
5783 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005784 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005785 exponent );
5786 if( ret != 0 )
5787 {
5788 mbedtls_rsa_free( rsa );
5789 mbedtls_free( rsa );
5790 return( mbedtls_to_psa_error( ret ) );
5791 }
5792 slot->data.rsa = rsa;
5793 }
5794 else
5795#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5796
5797#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005798 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005799 {
5800 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5801 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5802 const mbedtls_ecp_curve_info *curve_info =
5803 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5804 mbedtls_ecp_keypair *ecp;
5805 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005806 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005807 return( PSA_ERROR_NOT_SUPPORTED );
5808 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5809 return( PSA_ERROR_NOT_SUPPORTED );
5810 if( curve_info->bit_size != bits )
5811 return( PSA_ERROR_INVALID_ARGUMENT );
5812 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5813 if( ecp == NULL )
5814 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5815 mbedtls_ecp_keypair_init( ecp );
5816 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5817 mbedtls_ctr_drbg_random,
5818 &global_data.ctr_drbg );
5819 if( ret != 0 )
5820 {
5821 mbedtls_ecp_keypair_free( ecp );
5822 mbedtls_free( ecp );
5823 return( mbedtls_to_psa_error( ret ) );
5824 }
5825 slot->data.ecp = ecp;
5826 }
5827 else
5828#endif /* MBEDTLS_ECP_C */
5829
5830 return( PSA_ERROR_NOT_SUPPORTED );
5831
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005832 return( PSA_SUCCESS );
5833}
5834
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005835psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005836 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005837{
5838 psa_status_t status;
5839 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005840 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005841 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005842#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5843 if( driver != NULL )
5844 {
5845 /* Generating a key in a secure element is not implemented yet. */
5846 status = PSA_ERROR_NOT_SUPPORTED;
5847 }
5848#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005849 if( status == PSA_SUCCESS )
5850 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005851 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005852 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005853 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005854 }
5855 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005856 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005857 if( status != PSA_SUCCESS )
5858 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005859 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005860 *handle = 0;
5861 }
5862 return( status );
5863}
5864
5865
Gilles Peskine05d69892018-06-19 22:00:52 +02005866
5867/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005868/* Module setup */
5869/****************************************************************/
5870
Gilles Peskine5e769522018-11-20 21:59:56 +01005871psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5872 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5873 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5874{
5875 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5876 return( PSA_ERROR_BAD_STATE );
5877 global_data.entropy_init = entropy_init;
5878 global_data.entropy_free = entropy_free;
5879 return( PSA_SUCCESS );
5880}
5881
Gilles Peskinee59236f2018-01-27 23:32:46 +01005882void mbedtls_psa_crypto_free( void )
5883{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005884 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005885 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5886 {
5887 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005888 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005889 }
5890 /* Wipe all remaining data, including configuration.
5891 * In particular, this sets all state indicator to the value
5892 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005893 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005894#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005895 /* Unregister all secure element drivers, so that we restart from
5896 * a pristine state. */
5897 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005898#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005899}
5900
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005901#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5902/** Recover a transaction that was interrupted by a power failure.
5903 *
5904 * This function is called during initialization, before psa_crypto_init()
5905 * returns. If this function returns a failure status, the initialization
5906 * fails.
5907 */
5908static psa_status_t psa_crypto_recover_transaction(
5909 const psa_crypto_transaction_t *transaction )
5910{
5911 switch( transaction->unknown.type )
5912 {
5913 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5914 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5915 /* TOnogrepDO - fall through to the failure case until this
5916 * is implemented */
5917 default:
5918 /* We found an unsupported transaction in the storage.
5919 * We don't know what state the storage is in. Give up. */
5920 return( PSA_ERROR_STORAGE_FAILURE );
5921 }
5922}
5923#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5924
Gilles Peskinee59236f2018-01-27 23:32:46 +01005925psa_status_t psa_crypto_init( void )
5926{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005927 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005928 const unsigned char drbg_seed[] = "PSA";
5929
Gilles Peskinec6b69072018-11-20 21:42:52 +01005930 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005931 if( global_data.initialized != 0 )
5932 return( PSA_SUCCESS );
5933
Gilles Peskine5e769522018-11-20 21:59:56 +01005934 /* Set default configuration if
5935 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5936 if( global_data.entropy_init == NULL )
5937 global_data.entropy_init = mbedtls_entropy_init;
5938 if( global_data.entropy_free == NULL )
5939 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005940
Gilles Peskinec6b69072018-11-20 21:42:52 +01005941 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005942 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005943#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5944 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5945 /* The PSA entropy injection feature depends on using NV seed as an entropy
5946 * source. Add NV seed as an entropy source for PSA entropy injection. */
5947 mbedtls_entropy_add_source( &global_data.entropy,
5948 mbedtls_nv_seed_poll, NULL,
5949 MBEDTLS_ENTROPY_BLOCK_SIZE,
5950 MBEDTLS_ENTROPY_SOURCE_STRONG );
5951#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005952 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005953 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005954 status = mbedtls_to_psa_error(
5955 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5956 mbedtls_entropy_func,
5957 &global_data.entropy,
5958 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5959 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005960 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005961 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005962
Gilles Peskine66fb1262018-12-10 16:29:04 +01005963 status = psa_initialize_key_slots( );
5964 if( status != PSA_SUCCESS )
5965 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005966
Gilles Peskine4b734222019-07-24 15:56:31 +02005967#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005968 status = psa_crypto_load_transaction( );
5969 if( status == PSA_SUCCESS )
5970 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005971 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5972 if( status != PSA_SUCCESS )
5973 goto exit;
5974 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005975 }
5976 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5977 {
5978 /* There's no transaction to complete. It's all good. */
5979 status = PSA_SUCCESS;
5980 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005981#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005982
Gilles Peskinec6b69072018-11-20 21:42:52 +01005983 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005984 global_data.initialized = 1;
5985
Gilles Peskinee59236f2018-01-27 23:32:46 +01005986exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005987 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005988 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005989 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005990}
5991
5992#endif /* MBEDTLS_PSA_CRYPTO_C */