blob: cbe32612633b5ca8c7909a937a3e0af5b2220bb6 [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 );
749 /* Ensure that the bytes-to-bit conversion doesn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine68cc4332019-07-30 17:42:47 +0200752 /* Ensure that the bit size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200753 if( bit_size > PSA_MAX_KEY_BITS )
754 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200755 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200756 &slot->data.raw );
757 if( status != PSA_SUCCESS )
758 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200759 if( data_length != 0 )
760 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100761 }
762 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200764 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100765 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200766 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100767 data, data_length,
768 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100769 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200770 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000771 {
772 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200773 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000774 data, data_length,
775 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000776 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100777 else
778#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000779#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200780 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200782 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000783 data, data_length,
784 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100785 }
786 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000787#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 {
789 return( PSA_ERROR_NOT_SUPPORTED );
790 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200791
792 if( status == PSA_SUCCESS )
793 {
794 /* Write the actual key size to the slot.
795 * psa_start_key_creation() wrote the size declared by the
796 * caller, which may be 0 (meaning unspecified) or wrong. */
797 slot->attr.bits = psa_calculate_key_bits( slot );
798 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000799 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100800}
801
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100802/** Calculate the intersection of two algorithm usage policies.
803 *
804 * Return 0 (which allows no operation) on incompatibility.
805 */
806static psa_algorithm_t psa_key_policy_algorithm_intersection(
807 psa_algorithm_t alg1,
808 psa_algorithm_t alg2 )
809{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200810 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 if( alg1 == alg2 )
812 return( alg1 );
813 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100814 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100815 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
816 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
817 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
818 {
819 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
820 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100821 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100822 return( alg1 );
823 }
824 /* If the policies are incompatible, allow nothing. */
825 return( 0 );
826}
827
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200828static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
829 psa_algorithm_t requested_alg )
830{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200831 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200832 if( requested_alg == policy_alg )
833 return( 1 );
834 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200835 * and requested_alg is the same hash-and-sign family with any hash,
836 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200837 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
838 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
839 {
840 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
841 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
842 }
843 /* If it isn't permitted, it's forbidden. */
844 return( 0 );
845}
846
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100847/** Test whether a policy permits an algorithm.
848 *
849 * The caller must test usage flags separately.
850 */
851static int psa_key_policy_permits( const psa_key_policy_t *policy,
852 psa_algorithm_t alg )
853{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200854 return( psa_key_algorithm_permits( policy->alg, alg ) ||
855 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100856}
857
Gilles Peskinef603c712019-01-19 13:40:11 +0100858/** Restrict a key policy based on a constraint.
859 *
860 * \param[in,out] policy The policy to restrict.
861 * \param[in] constraint The policy constraint to apply.
862 *
863 * \retval #PSA_SUCCESS
864 * \c *policy contains the intersection of the original value of
865 * \c *policy and \c *constraint.
866 * \retval #PSA_ERROR_INVALID_ARGUMENT
867 * \c *policy and \c *constraint are incompatible.
868 * \c *policy is unchanged.
869 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100870static psa_status_t psa_restrict_key_policy(
871 psa_key_policy_t *policy,
872 const psa_key_policy_t *constraint )
873{
874 psa_algorithm_t intersection_alg =
875 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200876 psa_algorithm_t intersection_alg2 =
877 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100878 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
879 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200880 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
881 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100882 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200884 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100885 return( PSA_SUCCESS );
886}
887
Darryl Green06fd18d2018-07-16 11:21:11 +0100888/** Retrieve a slot which must contain a key. The key must have allow all the
889 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
890 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100891static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100892 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100893 psa_key_usage_t usage,
894 psa_algorithm_t alg )
895{
896 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100897 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100898
899 *p_slot = NULL;
900
Gilles Peskinec5487a82018-12-03 18:08:14 +0100901 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100902 if( status != PSA_SUCCESS )
903 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100904
905 /* Enforce that usage policy for the key slot contains all the flags
906 * required by the usage parameter. There is one exception: public
907 * keys can always be exported, so we treat public key objects as
908 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200911 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100912 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100913
914 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200915 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100916 return( PSA_ERROR_NOT_PERMITTED );
917
918 *p_slot = slot;
919 return( PSA_SUCCESS );
920}
Darryl Green940d72c2018-07-13 13:18:51 +0100921
Gilles Peskine28f8f302019-07-24 13:30:31 +0200922/** Retrieve a slot which must contain a transparent key.
923 *
924 * A transparent key is a key for which the key material is directly
925 * available, as opposed to a key in a secure element.
926 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200927 * This is a temporary function to use instead of psa_get_key_from_slot()
928 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200929 */
930#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
931static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
932 psa_key_slot_t **p_slot,
933 psa_key_usage_t usage,
934 psa_algorithm_t alg )
935{
936 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
937 if( status != PSA_SUCCESS )
938 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200939 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200940 {
941 *p_slot = NULL;
942 return( PSA_ERROR_NOT_SUPPORTED );
943 }
944 return( PSA_SUCCESS );
945}
946#else /* MBEDTLS_PSA_CRYPTO_SE_C */
947/* With no secure element support, all keys are transparent. */
948#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
949 psa_get_key_from_slot( handle, p_slot, usage, alg )
950#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
951
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100952/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100953static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000954{
Gilles Peskine73167e12019-07-12 23:44:37 +0200955#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
956 if( psa_key_slot_is_external( slot ) )
957 {
958 /* No key material to clean. */
959 }
960 else
961#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200962 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000963 {
964 /* No key material to clean. */
965 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000967 {
968 mbedtls_free( slot->data.raw.data );
969 }
970 else
971#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200972 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000973 {
974 mbedtls_rsa_free( slot->data.rsa );
975 mbedtls_free( slot->data.rsa );
976 }
977 else
978#endif /* defined(MBEDTLS_RSA_C) */
979#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200980 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000981 {
982 mbedtls_ecp_keypair_free( slot->data.ecp );
983 mbedtls_free( slot->data.ecp );
984 }
985 else
986#endif /* defined(MBEDTLS_ECP_C) */
987 {
988 /* Shouldn't happen: the key type is not any type that we
989 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200990 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000991 }
992
993 return( PSA_SUCCESS );
994}
995
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100996static void psa_abort_operations_using_key( psa_key_slot_t *slot )
997{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200998 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100999 (void) slot;
1000}
1001
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001002/** Completely wipe a slot in memory, including its policy.
1003 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001004psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001005{
1006 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001007 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001008 /* At this point, key material and other type-specific content has
1009 * been wiped. Clear remaining metadata. We can call memset and not
1010 * zeroize because the metadata is not particularly sensitive. */
1011 memset( slot, 0, sizeof( *slot ) );
1012 return( status );
1013}
1014
Gilles Peskinec5487a82018-12-03 18:08:14 +01001015psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001017 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001018 psa_status_t status = PSA_SUCCESS;
1019 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1021 psa_se_drv_table_entry_t *driver;
1022#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023
Gilles Peskinec5487a82018-12-03 18:08:14 +01001024 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001025 if( status != PSA_SUCCESS )
1026 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001027
1028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001029 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001030 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001032 /* For a key in a secure element, we need to do three things:
1033 * remove the key file in internal storage, destroy the
1034 * key inside the secure element, and update the driver's
1035 * persistent data. Start a transaction that will encompass these
1036 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001038 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001039 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001040 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001041 status = psa_crypto_save_transaction( );
1042 if( status != PSA_SUCCESS )
1043 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001044 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001045 /* TOnogrepDO: destroy what can be destroyed anyway */
1046 return( status );
1047 }
1048
Gilles Peskine354f7672019-07-12 23:46:38 +02001049 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001050 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1052
Darryl Greend49a4992018-06-18 17:27:26 +01001053#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001054 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001055 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001056 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001057 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001058 }
1059#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001060
Gilles Peskinefc762652019-07-22 19:30:34 +02001061#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1062 if( driver != NULL )
1063 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001064 psa_status_t status2;
1065 status = psa_save_se_persistent_data( driver );
1066 status2 = psa_crypto_stop_transaction( );
1067 if( status == PSA_SUCCESS )
1068 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001069 if( status != PSA_SUCCESS )
1070 {
1071 /* TOnogrepDO: destroy what can be destroyed anyway */
1072 return( status );
1073 }
1074 }
1075#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1076
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001077 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001078 if( status != PSA_SUCCESS )
1079 return( status );
1080 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081}
1082
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001083void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1084{
Gilles Peskineb699f072019-04-26 16:06:02 +02001085 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001086 memset( attributes, 0, sizeof( *attributes ) );
1087}
1088
Gilles Peskineb699f072019-04-26 16:06:02 +02001089psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1090 psa_key_type_t type,
1091 const uint8_t *data,
1092 size_t data_length )
1093{
1094 uint8_t *copy = NULL;
1095
1096 if( data_length != 0 )
1097 {
1098 copy = mbedtls_calloc( 1, data_length );
1099 if( copy == NULL )
1100 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1101 memcpy( copy, data, data_length );
1102 }
1103 /* After this point, this function is guaranteed to succeed, so it
1104 * can start modifying `*attributes`. */
1105
1106 if( attributes->domain_parameters != NULL )
1107 {
1108 mbedtls_free( attributes->domain_parameters );
1109 attributes->domain_parameters = NULL;
1110 attributes->domain_parameters_size = 0;
1111 }
1112
1113 attributes->domain_parameters = copy;
1114 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001115 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001116 return( PSA_SUCCESS );
1117}
1118
1119psa_status_t psa_get_key_domain_parameters(
1120 const psa_key_attributes_t *attributes,
1121 uint8_t *data, size_t data_size, size_t *data_length )
1122{
1123 if( attributes->domain_parameters_size > data_size )
1124 return( PSA_ERROR_BUFFER_TOO_SMALL );
1125 *data_length = attributes->domain_parameters_size;
1126 if( attributes->domain_parameters_size != 0 )
1127 memcpy( data, attributes->domain_parameters,
1128 attributes->domain_parameters_size );
1129 return( PSA_SUCCESS );
1130}
1131
1132#if defined(MBEDTLS_RSA_C)
1133static psa_status_t psa_get_rsa_public_exponent(
1134 const mbedtls_rsa_context *rsa,
1135 psa_key_attributes_t *attributes )
1136{
1137 mbedtls_mpi mpi;
1138 int ret;
1139 uint8_t *buffer = NULL;
1140 size_t buflen;
1141 mbedtls_mpi_init( &mpi );
1142
1143 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1144 if( ret != 0 )
1145 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001146 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1147 {
1148 /* It's the default value, which is reported as an empty string,
1149 * so there's nothing to do. */
1150 goto exit;
1151 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001152
1153 buflen = mbedtls_mpi_size( &mpi );
1154 buffer = mbedtls_calloc( 1, buflen );
1155 if( buffer == NULL )
1156 {
1157 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1158 goto exit;
1159 }
1160 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1161 if( ret != 0 )
1162 goto exit;
1163 attributes->domain_parameters = buffer;
1164 attributes->domain_parameters_size = buflen;
1165
1166exit:
1167 mbedtls_mpi_free( &mpi );
1168 if( ret != 0 )
1169 mbedtls_free( buffer );
1170 return( mbedtls_to_psa_error( ret ) );
1171}
1172#endif /* MBEDTLS_RSA_C */
1173
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001174/** Retrieve all the publicly-accessible attributes of a key.
1175 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001176psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1177 psa_key_attributes_t *attributes )
1178{
1179 psa_key_slot_t *slot;
1180 psa_status_t status;
1181
1182 psa_reset_key_attributes( attributes );
1183
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001184 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001185 if( status != PSA_SUCCESS )
1186 return( status );
1187
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001188 attributes->core = slot->attr;
Gilles Peskineb699f072019-04-26 16:06:02 +02001189
Gilles Peskine8e338702019-07-30 20:06:31 +02001190 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001191 {
1192#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001193 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001194 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001195#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1196 /* TOnogrepDO: reporting the public exponent for opaque keys
1197 * is not yet implemented. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001198 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001199 break;
1200#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001201 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1202 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001203#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001204 default:
1205 /* Nothing else to do. */
1206 break;
1207 }
1208
1209 if( status != PSA_SUCCESS )
1210 psa_reset_key_attributes( attributes );
1211 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001212}
1213
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001214#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001215static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1216 unsigned char *buf, size_t size )
1217{
1218 int ret;
1219 unsigned char *c;
1220 size_t len = 0;
1221
1222 c = buf + size;
1223
1224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1225
1226 return( (int) len );
1227}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001228#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001229
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001230static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1231 uint8_t *data,
1232 size_t data_size,
1233 size_t *data_length,
1234 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235{
Gilles Peskine5d309672019-07-12 23:47:28 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237 const psa_drv_se_t *drv;
1238 psa_drv_se_context_t *drv_context;
1239#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1240
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001241 *data_length = 0;
1242
Gilles Peskine8e338702019-07-30 20:06:31 +02001243 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001244 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001245
Gilles Peskine5d309672019-07-12 23:47:28 +02001246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001247 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001248 {
1249 psa_drv_se_export_key_t method;
1250 if( drv->key_management == NULL )
1251 return( PSA_ERROR_NOT_SUPPORTED );
1252 method = ( export_public_key ?
1253 drv->key_management->p_export_public :
1254 drv->key_management->p_export );
1255 if( method == NULL )
1256 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001257 return( method( drv_context,
1258 slot->data.se.slot_number,
1259 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001260 }
1261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1262
Gilles Peskine8e338702019-07-30 20:06:31 +02001263 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264 {
1265 if( slot->data.raw.bytes > data_size )
1266 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001267 if( data_size != 0 )
1268 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001269 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001270 memset( data + slot->data.raw.bytes, 0,
1271 data_size - slot->data.raw.bytes );
1272 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001273 *data_length = slot->data.raw.bytes;
1274 return( PSA_SUCCESS );
1275 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001276#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001277 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001278 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001279 psa_status_t status;
1280
Gilles Peskineb46bef22019-07-30 21:32:04 +02001281 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001282 if( bytes > data_size )
1283 return( PSA_ERROR_BUFFER_TOO_SMALL );
1284 status = mbedtls_to_psa_error(
1285 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1286 if( status != PSA_SUCCESS )
1287 return( status );
1288 memset( data + bytes, 0, data_size - bytes );
1289 *data_length = bytes;
1290 return( PSA_SUCCESS );
1291 }
1292#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001293 else
Moran Peker17e36e12018-05-02 12:55:20 +03001294 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001295#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001296 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1297 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001298 {
Moran Pekera998bc62018-04-16 18:16:20 +03001299 mbedtls_pk_context pk;
1300 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001301 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001302 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001303#if defined(MBEDTLS_RSA_C)
1304 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001305 pk.pk_info = &mbedtls_rsa_info;
1306 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001307#else
1308 return( PSA_ERROR_NOT_SUPPORTED );
1309#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001310 }
1311 else
1312 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001313#if defined(MBEDTLS_ECP_C)
1314 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001315 pk.pk_info = &mbedtls_eckey_info;
1316 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001317#else
1318 return( PSA_ERROR_NOT_SUPPORTED );
1319#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001320 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001321 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001322 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001323 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001324 }
Moran Peker17e36e12018-05-02 12:55:20 +03001325 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001326 {
Moran Peker17e36e12018-05-02 12:55:20 +03001327 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001328 }
Moran Peker60364322018-04-29 11:34:58 +03001329 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001330 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001331 /* If data_size is 0 then data may be NULL and then the
1332 * call to memset would have undefined behavior. */
1333 if( data_size != 0 )
1334 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001335 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001336 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001337 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1338 * Move the data to the beginning and erase remaining data
1339 * at the original location. */
1340 if( 2 * (size_t) ret <= data_size )
1341 {
1342 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001343 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001344 }
1345 else if( (size_t) ret < data_size )
1346 {
1347 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001348 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001349 }
Moran Pekera998bc62018-04-16 18:16:20 +03001350 *data_length = ret;
1351 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001352 }
1353 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001354#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001355 {
1356 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001357 it is valid for a special-purpose implementation to omit
1358 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001359 return( PSA_ERROR_NOT_SUPPORTED );
1360 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361 }
1362}
1363
Gilles Peskinec5487a82018-12-03 18:08:14 +01001364psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001365 uint8_t *data,
1366 size_t data_size,
1367 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001368{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001369 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001370 psa_status_t status;
1371
1372 /* Set the key to empty now, so that even when there are errors, we always
1373 * set data_length to a value between 0 and data_size. On error, setting
1374 * the key to empty is a good choice because an empty key representation is
1375 * unlikely to be accepted anywhere. */
1376 *data_length = 0;
1377
1378 /* Export requires the EXPORT flag. There is an exception for public keys,
1379 * which don't require any flag, but psa_get_key_from_slot takes
1380 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001381 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001382 if( status != PSA_SUCCESS )
1383 return( status );
1384 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001385 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001386}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001387
Gilles Peskinec5487a82018-12-03 18:08:14 +01001388psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001389 uint8_t *data,
1390 size_t data_size,
1391 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001392{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001393 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001394 psa_status_t status;
1395
1396 /* Set the key to empty now, so that even when there are errors, we always
1397 * set data_length to a value between 0 and data_size. On error, setting
1398 * the key to empty is a good choice because an empty key representation is
1399 * unlikely to be accepted anywhere. */
1400 *data_length = 0;
1401
1402 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001403 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001404 if( status != PSA_SUCCESS )
1405 return( status );
1406 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001407 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001408}
1409
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001410/** Validate that a key policy is internally well-formed.
1411 *
1412 * This function only rejects invalid policies. It does not validate the
1413 * consistency of the policy with respect to other attributes of the key
1414 * such as the key type.
1415 */
1416static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001417{
1418 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001419 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001420 PSA_KEY_USAGE_ENCRYPT |
1421 PSA_KEY_USAGE_DECRYPT |
1422 PSA_KEY_USAGE_SIGN |
1423 PSA_KEY_USAGE_VERIFY |
1424 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1425 return( PSA_ERROR_INVALID_ARGUMENT );
1426
Gilles Peskine4747d192019-04-17 15:05:45 +02001427 return( PSA_SUCCESS );
1428}
1429
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001430/** Validate the internal consistency of key attributes.
1431 *
1432 * This function only rejects invalid attribute values. If does not
1433 * validate the consistency of the attributes with any key data that may
1434 * be involved in the creation of the key.
1435 *
1436 * Call this function early in the key creation process.
1437 *
1438 * \param[in] attributes Key attributes for the new key.
1439 * \param[out] p_drv On any return, the driver for the key, if any.
1440 * NULL for a transparent key.
1441 *
1442 */
1443static psa_status_t psa_validate_key_attributes(
1444 const psa_key_attributes_t *attributes,
1445 psa_se_drv_table_entry_t **p_drv )
1446{
1447 psa_status_t status;
1448
1449 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1450 {
1451 status = psa_validate_persistent_key_parameters(
1452 attributes->core.lifetime, attributes->core.id,
1453 p_drv, 1 );
1454 if( status != PSA_SUCCESS )
1455 return( status );
1456 }
1457
1458 status = psa_validate_key_policy( &attributes->core.policy );
1459 if( status != PSA_SUCCESS )
1460 return( status );
1461
1462 /* Refuse to create overly large keys.
1463 * Note that this doesn't trigger on import if the attributes don't
1464 * explicitly specify a size (so psa_get_key_bits returns 0), so
1465 * psa_import_key() needs its own checks. */
1466 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1467 return( PSA_ERROR_NOT_SUPPORTED );
1468
1469 return( PSA_SUCCESS );
1470}
1471
Gilles Peskine4747d192019-04-17 15:05:45 +02001472/** Prepare a key slot to receive key material.
1473 *
1474 * This function allocates a key slot and sets its metadata.
1475 *
1476 * If this function fails, call psa_fail_key_creation().
1477 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001478 * This function is intended to be used as follows:
1479 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1480 * it with the specified attributes, and assign it a handle.
1481 * -# Populate the slot with the key material.
1482 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1483 * In case of failure at any step, stop the sequence and call
1484 * psa_fail_key_creation().
1485 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001486 * \param[in] attributes Key attributes for the new key.
1487 * \param[out] handle On success, a handle for the allocated slot.
1488 * \param[out] p_slot On success, a pointer to the prepared slot.
1489 * \param[out] p_drv On any return, the driver for the key, if any.
1490 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001491 *
1492 * \retval #PSA_SUCCESS
1493 * The key slot is ready to receive key material.
1494 * \return If this function fails, the key slot is an invalid state.
1495 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001496 */
1497static psa_status_t psa_start_key_creation(
1498 const psa_key_attributes_t *attributes,
1499 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001500 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001501 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001502{
1503 psa_status_t status;
1504 psa_key_slot_t *slot;
1505
Gilles Peskine011e4282019-06-26 18:34:38 +02001506 *p_drv = NULL;
1507
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001508 status = psa_validate_key_attributes( attributes, p_drv );
1509 if( status != PSA_SUCCESS )
1510 return( status );
1511
Gilles Peskine267c6562019-05-27 19:01:54 +02001512 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001513 if( status != PSA_SUCCESS )
1514 return( status );
1515 slot = *p_slot;
1516
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001517 /* We're storing the declared bit-size of the key. It's up to each
1518 * creation mechanism to verify that this information is correct.
1519 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001520 * an input (generate, device) but not for those where the bit-size
1521 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001522
1523 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001524
Gilles Peskinecbaff462019-07-12 23:46:04 +02001525#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001526 /* For a key in a secure element, we need to do three things:
1527 * create the key file in internal storage, create the
1528 * key inside the secure element, and update the driver's
1529 * persistent data. Start a transaction that will encompass these
1530 * three actions. */
1531 /* The first thing to do is to find a slot number for the new key.
1532 * We save the slot number in persistent storage as part of the
1533 * transaction data. It will be needed to recover if the power
1534 * fails during the key creation process, to clean up on the secure
1535 * element side after restarting. Obtaining a slot number from the
1536 * secure element driver updates its persistent state, but we do not yet
1537 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001538 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001539 if( *p_drv != NULL )
1540 {
1541 status = psa_find_se_slot_for_key( attributes, *p_drv,
1542 &slot->data.se.slot_number );
1543 if( status != PSA_SUCCESS )
1544 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001545 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001546 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001547 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001548 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001549 status = psa_crypto_save_transaction( );
1550 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001551 {
1552 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001553 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001554 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001555 }
1556#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1557
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 return( status );
1559}
1560
1561/** Finalize the creation of a key once its key material has been set.
1562 *
1563 * This entails writing the key to persistent storage.
1564 *
1565 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001566 * See the documentation of psa_start_key_creation() for the intended use
1567 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001569 * \param[in,out] slot Pointer to the slot with key material.
1570 * \param[in] driver The secure element driver for the key,
1571 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001572 *
1573 * \retval #PSA_SUCCESS
1574 * The key was successfully created. The handle is now valid.
1575 * \return If this function fails, the key slot is an invalid state.
1576 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001578static psa_status_t psa_finish_key_creation(
1579 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001580 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001581{
1582 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001583 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001584 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001585
1586#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001587 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001588 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001589#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1590 if( driver != NULL )
1591 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001592 psa_se_key_data_storage_t data;
1593#if defined(static_assert)
1594 static_assert( sizeof( slot->data.se.slot_number ) ==
1595 sizeof( data.slot_number ),
1596 "Slot number size does not match psa_se_key_data_storage_t" );
1597 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1598 "Bit-size size does not match psa_se_key_data_storage_t" );
1599#endif
1600 memcpy( &data.slot_number, &slot->data.se.slot_number,
1601 sizeof( slot->data.se.slot_number ) );
1602 memcpy( &data.bits, &slot->attr.bits,
1603 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001604 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001605 (uint8_t*) &data,
1606 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001607 }
1608 else
1609#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1610 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001611 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001612 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001613 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001614 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1615 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001616 if( buffer == NULL && buffer_size != 0 )
1617 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1618 status = psa_internal_export_key( slot,
1619 buffer, buffer_size, &length,
1620 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001621 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001622 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001623 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001624
Gilles Peskine1df83d42019-07-23 16:13:14 +02001625 if( buffer_size != 0 )
1626 mbedtls_platform_zeroize( buffer, buffer_size );
1627 mbedtls_free( buffer );
1628 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 }
1630#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1631
Gilles Peskinecbaff462019-07-12 23:46:04 +02001632#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1633 if( driver != NULL )
1634 {
1635 status = psa_save_se_persistent_data( driver );
1636 if( status != PSA_SUCCESS )
1637 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001638 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001639 return( status );
1640 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001641 status = psa_crypto_stop_transaction( );
1642 if( status != PSA_SUCCESS )
1643 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001644 }
1645#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1646
Gilles Peskine4747d192019-04-17 15:05:45 +02001647 return( status );
1648}
1649
1650/** Abort the creation of a key.
1651 *
1652 * You may call this function after calling psa_start_key_creation(),
1653 * or after psa_finish_key_creation() fails. In other circumstances, this
1654 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 * See the documentation of psa_start_key_creation() for the intended use
1656 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001658 * \param[in,out] slot Pointer to the slot with key material.
1659 * \param[in] driver The secure element driver for the key,
1660 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001662static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001663 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001664{
Gilles Peskine011e4282019-06-26 18:34:38 +02001665 (void) driver;
1666
Gilles Peskine4747d192019-04-17 15:05:45 +02001667 if( slot == NULL )
1668 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001669
1670#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1671 /* TOnogrepDO: If the key has already been created in the secure
1672 * element, and the failure happened later (when saving metadata
1673 * to internal storage), we need to destroy the key in the secure
1674 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001675
1676 /* Abort the ongoing transaction if any. We already did what it
1677 * takes to undo any partial creation. All that's left is to update
1678 * the transaction data itself. */
1679 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001680#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1681
Gilles Peskine4747d192019-04-17 15:05:45 +02001682 psa_wipe_key_slot( slot );
1683}
1684
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001685/** Validate optional attributes during key creation.
1686 *
1687 * Some key attributes are optional during key creation. If they are
1688 * specified in the attributes structure, check that they are consistent
1689 * with the data in the slot.
1690 *
1691 * This function should be called near the end of key creation, after
1692 * the slot in memory is fully populated but before saving persistent data.
1693 */
1694static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001695 const psa_key_slot_t *slot,
1696 const psa_key_attributes_t *attributes )
1697{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001698 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001699 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001700 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001701 return( PSA_ERROR_INVALID_ARGUMENT );
1702 }
1703
1704 if( attributes->domain_parameters_size != 0 )
1705 {
1706#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001707 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001708 {
1709 mbedtls_mpi actual, required;
1710 int ret;
1711 mbedtls_mpi_init( &actual );
1712 mbedtls_mpi_init( &required );
1713 ret = mbedtls_rsa_export( slot->data.rsa,
1714 NULL, NULL, NULL, NULL, &actual );
1715 if( ret != 0 )
1716 goto rsa_exit;
1717 ret = mbedtls_mpi_read_binary( &required,
1718 attributes->domain_parameters,
1719 attributes->domain_parameters_size );
1720 if( ret != 0 )
1721 goto rsa_exit;
1722 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1723 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1724 rsa_exit:
1725 mbedtls_mpi_free( &actual );
1726 mbedtls_mpi_free( &required );
1727 if( ret != 0)
1728 return( mbedtls_to_psa_error( ret ) );
1729 }
1730 else
1731#endif
1732 {
1733 return( PSA_ERROR_INVALID_ARGUMENT );
1734 }
1735 }
1736
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001737 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001738 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001739 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001740 return( PSA_ERROR_INVALID_ARGUMENT );
1741 }
1742
1743 return( PSA_SUCCESS );
1744}
1745
Gilles Peskine4747d192019-04-17 15:05:45 +02001746psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001748 size_t data_length,
1749 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001750{
1751 psa_status_t status;
1752 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001753 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001754
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001756 if( status != PSA_SUCCESS )
1757 goto exit;
1758
Gilles Peskine5d309672019-07-12 23:47:28 +02001759#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1760 if( driver != NULL )
1761 {
1762 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001763 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001764 if( drv->key_management == NULL ||
1765 drv->key_management->p_import == NULL )
1766 {
1767 status = PSA_ERROR_NOT_SUPPORTED;
1768 goto exit;
1769 }
1770 status = drv->key_management->p_import(
1771 psa_get_se_driver_context( driver ),
1772 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001773 slot->attr.lifetime, slot->attr.type,
1774 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001775 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001776 &bits );
1777 if( status != PSA_SUCCESS )
1778 goto exit;
1779 if( bits > PSA_MAX_KEY_BITS )
1780 {
1781 status = PSA_ERROR_NOT_SUPPORTED;
1782 goto exit;
1783 }
1784 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001785 }
1786 else
1787#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1788 {
1789 status = psa_import_key_into_slot( slot, data, data_length );
1790 if( status != PSA_SUCCESS )
1791 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001792 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001793 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001794 if( status != PSA_SUCCESS )
1795 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001796
Gilles Peskine011e4282019-06-26 18:34:38 +02001797 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001798exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 if( status != PSA_SUCCESS )
1800 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001801 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001802 *handle = 0;
1803 }
1804 return( status );
1805}
1806
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001807static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001808 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001809{
1810 psa_status_t status;
1811 uint8_t *buffer = NULL;
1812 size_t buffer_size = 0;
1813 size_t length;
1814
Gilles Peskine8e338702019-07-30 20:06:31 +02001815 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001816 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001817 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001818 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001819 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001820 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1821 if( status != PSA_SUCCESS )
1822 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001823 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001824 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001825
1826exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001827 if( buffer_size != 0 )
1828 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001829 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001830 return( status );
1831}
1832
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001833psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1834 const psa_key_attributes_t *specified_attributes,
1835 psa_key_handle_t *target_handle )
1836{
1837 psa_status_t status;
1838 psa_key_slot_t *source_slot = NULL;
1839 psa_key_slot_t *target_slot = NULL;
1840 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001841 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001842
Gilles Peskine28f8f302019-07-24 13:30:31 +02001843 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001844 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001845 if( status != PSA_SUCCESS )
1846 goto exit;
1847
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001848 status = psa_validate_optional_attributes( source_slot,
1849 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001850 if( status != PSA_SUCCESS )
1851 goto exit;
1852
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001853 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001854 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001855 if( status != PSA_SUCCESS )
1856 goto exit;
1857
1858 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001859 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001860 if( status != PSA_SUCCESS )
1861 goto exit;
1862
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001863#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1864 if( driver != NULL )
1865 {
1866 /* Copying to a secure element is not implemented yet. */
1867 status = PSA_ERROR_NOT_SUPPORTED;
1868 goto exit;
1869 }
1870#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1871
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001872 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001873 if( status != PSA_SUCCESS )
1874 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001875
Gilles Peskine011e4282019-06-26 18:34:38 +02001876 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001877exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001878 if( status != PSA_SUCCESS )
1879 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001880 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001881 *target_handle = 0;
1882 }
1883 return( status );
1884}
1885
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001886
1887
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001888/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001889/* Message digests */
1890/****************************************************************/
1891
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001892static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001893{
1894 switch( alg )
1895 {
1896#if defined(MBEDTLS_MD2_C)
1897 case PSA_ALG_MD2:
1898 return( &mbedtls_md2_info );
1899#endif
1900#if defined(MBEDTLS_MD4_C)
1901 case PSA_ALG_MD4:
1902 return( &mbedtls_md4_info );
1903#endif
1904#if defined(MBEDTLS_MD5_C)
1905 case PSA_ALG_MD5:
1906 return( &mbedtls_md5_info );
1907#endif
1908#if defined(MBEDTLS_RIPEMD160_C)
1909 case PSA_ALG_RIPEMD160:
1910 return( &mbedtls_ripemd160_info );
1911#endif
1912#if defined(MBEDTLS_SHA1_C)
1913 case PSA_ALG_SHA_1:
1914 return( &mbedtls_sha1_info );
1915#endif
1916#if defined(MBEDTLS_SHA256_C)
1917 case PSA_ALG_SHA_224:
1918 return( &mbedtls_sha224_info );
1919 case PSA_ALG_SHA_256:
1920 return( &mbedtls_sha256_info );
1921#endif
1922#if defined(MBEDTLS_SHA512_C)
1923 case PSA_ALG_SHA_384:
1924 return( &mbedtls_sha384_info );
1925 case PSA_ALG_SHA_512:
1926 return( &mbedtls_sha512_info );
1927#endif
1928 default:
1929 return( NULL );
1930 }
1931}
1932
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001933psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1934{
1935 switch( operation->alg )
1936 {
Gilles Peskine81736312018-06-26 15:04:31 +02001937 case 0:
1938 /* The object has (apparently) been initialized but it is not
1939 * in use. It's ok to call abort on such an object, and there's
1940 * nothing to do. */
1941 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001942#if defined(MBEDTLS_MD2_C)
1943 case PSA_ALG_MD2:
1944 mbedtls_md2_free( &operation->ctx.md2 );
1945 break;
1946#endif
1947#if defined(MBEDTLS_MD4_C)
1948 case PSA_ALG_MD4:
1949 mbedtls_md4_free( &operation->ctx.md4 );
1950 break;
1951#endif
1952#if defined(MBEDTLS_MD5_C)
1953 case PSA_ALG_MD5:
1954 mbedtls_md5_free( &operation->ctx.md5 );
1955 break;
1956#endif
1957#if defined(MBEDTLS_RIPEMD160_C)
1958 case PSA_ALG_RIPEMD160:
1959 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1960 break;
1961#endif
1962#if defined(MBEDTLS_SHA1_C)
1963 case PSA_ALG_SHA_1:
1964 mbedtls_sha1_free( &operation->ctx.sha1 );
1965 break;
1966#endif
1967#if defined(MBEDTLS_SHA256_C)
1968 case PSA_ALG_SHA_224:
1969 case PSA_ALG_SHA_256:
1970 mbedtls_sha256_free( &operation->ctx.sha256 );
1971 break;
1972#endif
1973#if defined(MBEDTLS_SHA512_C)
1974 case PSA_ALG_SHA_384:
1975 case PSA_ALG_SHA_512:
1976 mbedtls_sha512_free( &operation->ctx.sha512 );
1977 break;
1978#endif
1979 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001980 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001981 }
1982 operation->alg = 0;
1983 return( PSA_SUCCESS );
1984}
1985
Gilles Peskineda8191d2018-07-08 19:46:38 +02001986psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001987 psa_algorithm_t alg )
1988{
1989 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001990
1991 /* A context must be freshly initialized before it can be set up. */
1992 if( operation->alg != 0 )
1993 {
1994 return( PSA_ERROR_BAD_STATE );
1995 }
1996
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001997 switch( alg )
1998 {
1999#if defined(MBEDTLS_MD2_C)
2000 case PSA_ALG_MD2:
2001 mbedtls_md2_init( &operation->ctx.md2 );
2002 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2003 break;
2004#endif
2005#if defined(MBEDTLS_MD4_C)
2006 case PSA_ALG_MD4:
2007 mbedtls_md4_init( &operation->ctx.md4 );
2008 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2009 break;
2010#endif
2011#if defined(MBEDTLS_MD5_C)
2012 case PSA_ALG_MD5:
2013 mbedtls_md5_init( &operation->ctx.md5 );
2014 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2015 break;
2016#endif
2017#if defined(MBEDTLS_RIPEMD160_C)
2018 case PSA_ALG_RIPEMD160:
2019 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2020 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2021 break;
2022#endif
2023#if defined(MBEDTLS_SHA1_C)
2024 case PSA_ALG_SHA_1:
2025 mbedtls_sha1_init( &operation->ctx.sha1 );
2026 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2027 break;
2028#endif
2029#if defined(MBEDTLS_SHA256_C)
2030 case PSA_ALG_SHA_224:
2031 mbedtls_sha256_init( &operation->ctx.sha256 );
2032 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2033 break;
2034 case PSA_ALG_SHA_256:
2035 mbedtls_sha256_init( &operation->ctx.sha256 );
2036 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2037 break;
2038#endif
2039#if defined(MBEDTLS_SHA512_C)
2040 case PSA_ALG_SHA_384:
2041 mbedtls_sha512_init( &operation->ctx.sha512 );
2042 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2043 break;
2044 case PSA_ALG_SHA_512:
2045 mbedtls_sha512_init( &operation->ctx.sha512 );
2046 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2047 break;
2048#endif
2049 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002050 return( PSA_ALG_IS_HASH( alg ) ?
2051 PSA_ERROR_NOT_SUPPORTED :
2052 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002053 }
2054 if( ret == 0 )
2055 operation->alg = alg;
2056 else
2057 psa_hash_abort( operation );
2058 return( mbedtls_to_psa_error( ret ) );
2059}
2060
2061psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2062 const uint8_t *input,
2063 size_t input_length )
2064{
2065 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002066
2067 /* Don't require hash implementations to behave correctly on a
2068 * zero-length input, which may have an invalid pointer. */
2069 if( input_length == 0 )
2070 return( PSA_SUCCESS );
2071
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002072 switch( operation->alg )
2073 {
2074#if defined(MBEDTLS_MD2_C)
2075 case PSA_ALG_MD2:
2076 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2077 input, input_length );
2078 break;
2079#endif
2080#if defined(MBEDTLS_MD4_C)
2081 case PSA_ALG_MD4:
2082 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2083 input, input_length );
2084 break;
2085#endif
2086#if defined(MBEDTLS_MD5_C)
2087 case PSA_ALG_MD5:
2088 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2089 input, input_length );
2090 break;
2091#endif
2092#if defined(MBEDTLS_RIPEMD160_C)
2093 case PSA_ALG_RIPEMD160:
2094 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2095 input, input_length );
2096 break;
2097#endif
2098#if defined(MBEDTLS_SHA1_C)
2099 case PSA_ALG_SHA_1:
2100 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2101 input, input_length );
2102 break;
2103#endif
2104#if defined(MBEDTLS_SHA256_C)
2105 case PSA_ALG_SHA_224:
2106 case PSA_ALG_SHA_256:
2107 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2108 input, input_length );
2109 break;
2110#endif
2111#if defined(MBEDTLS_SHA512_C)
2112 case PSA_ALG_SHA_384:
2113 case PSA_ALG_SHA_512:
2114 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2115 input, input_length );
2116 break;
2117#endif
2118 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002119 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002121
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 if( ret != 0 )
2123 psa_hash_abort( operation );
2124 return( mbedtls_to_psa_error( ret ) );
2125}
2126
2127psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2128 uint8_t *hash,
2129 size_t hash_size,
2130 size_t *hash_length )
2131{
itayzafrir40835d42018-08-02 13:14:17 +03002132 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002134 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002135
2136 /* Fill the output buffer with something that isn't a valid hash
2137 * (barring an attack on the hash and deliberately-crafted input),
2138 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002139 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002140 /* If hash_size is 0 then hash may be NULL and then the
2141 * call to memset would have undefined behavior. */
2142 if( hash_size != 0 )
2143 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144
2145 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002146 {
2147 status = PSA_ERROR_BUFFER_TOO_SMALL;
2148 goto exit;
2149 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002150
2151 switch( operation->alg )
2152 {
2153#if defined(MBEDTLS_MD2_C)
2154 case PSA_ALG_MD2:
2155 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2156 break;
2157#endif
2158#if defined(MBEDTLS_MD4_C)
2159 case PSA_ALG_MD4:
2160 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2161 break;
2162#endif
2163#if defined(MBEDTLS_MD5_C)
2164 case PSA_ALG_MD5:
2165 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2166 break;
2167#endif
2168#if defined(MBEDTLS_RIPEMD160_C)
2169 case PSA_ALG_RIPEMD160:
2170 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2171 break;
2172#endif
2173#if defined(MBEDTLS_SHA1_C)
2174 case PSA_ALG_SHA_1:
2175 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2176 break;
2177#endif
2178#if defined(MBEDTLS_SHA256_C)
2179 case PSA_ALG_SHA_224:
2180 case PSA_ALG_SHA_256:
2181 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2182 break;
2183#endif
2184#if defined(MBEDTLS_SHA512_C)
2185 case PSA_ALG_SHA_384:
2186 case PSA_ALG_SHA_512:
2187 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2188 break;
2189#endif
2190 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002191 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192 }
itayzafrir40835d42018-08-02 13:14:17 +03002193 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194
itayzafrir40835d42018-08-02 13:14:17 +03002195exit:
2196 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002198 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199 return( psa_hash_abort( operation ) );
2200 }
2201 else
2202 {
2203 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002204 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205 }
2206}
2207
Gilles Peskine2d277862018-06-18 15:41:12 +02002208psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2209 const uint8_t *hash,
2210 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211{
2212 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2213 size_t actual_hash_length;
2214 psa_status_t status = psa_hash_finish( operation,
2215 actual_hash, sizeof( actual_hash ),
2216 &actual_hash_length );
2217 if( status != PSA_SUCCESS )
2218 return( status );
2219 if( actual_hash_length != hash_length )
2220 return( PSA_ERROR_INVALID_SIGNATURE );
2221 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2222 return( PSA_ERROR_INVALID_SIGNATURE );
2223 return( PSA_SUCCESS );
2224}
2225
Gilles Peskineeb35d782019-01-22 17:56:16 +01002226psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2227 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002228{
2229 if( target_operation->alg != 0 )
2230 return( PSA_ERROR_BAD_STATE );
2231
2232 switch( source_operation->alg )
2233 {
2234 case 0:
2235 return( PSA_ERROR_BAD_STATE );
2236#if defined(MBEDTLS_MD2_C)
2237 case PSA_ALG_MD2:
2238 mbedtls_md2_clone( &target_operation->ctx.md2,
2239 &source_operation->ctx.md2 );
2240 break;
2241#endif
2242#if defined(MBEDTLS_MD4_C)
2243 case PSA_ALG_MD4:
2244 mbedtls_md4_clone( &target_operation->ctx.md4,
2245 &source_operation->ctx.md4 );
2246 break;
2247#endif
2248#if defined(MBEDTLS_MD5_C)
2249 case PSA_ALG_MD5:
2250 mbedtls_md5_clone( &target_operation->ctx.md5,
2251 &source_operation->ctx.md5 );
2252 break;
2253#endif
2254#if defined(MBEDTLS_RIPEMD160_C)
2255 case PSA_ALG_RIPEMD160:
2256 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2257 &source_operation->ctx.ripemd160 );
2258 break;
2259#endif
2260#if defined(MBEDTLS_SHA1_C)
2261 case PSA_ALG_SHA_1:
2262 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2263 &source_operation->ctx.sha1 );
2264 break;
2265#endif
2266#if defined(MBEDTLS_SHA256_C)
2267 case PSA_ALG_SHA_224:
2268 case PSA_ALG_SHA_256:
2269 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2270 &source_operation->ctx.sha256 );
2271 break;
2272#endif
2273#if defined(MBEDTLS_SHA512_C)
2274 case PSA_ALG_SHA_384:
2275 case PSA_ALG_SHA_512:
2276 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2277 &source_operation->ctx.sha512 );
2278 break;
2279#endif
2280 default:
2281 return( PSA_ERROR_NOT_SUPPORTED );
2282 }
2283
2284 target_operation->alg = source_operation->alg;
2285 return( PSA_SUCCESS );
2286}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287
2288
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002290/* MAC */
2291/****************************************************************/
2292
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002293static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002294 psa_algorithm_t alg,
2295 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002296 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002297 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002298{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002300 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002301
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002302 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002303 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002304
Gilles Peskine8c9def32018-02-08 10:02:12 +01002305 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2306 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002307 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002308 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002309 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002310 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311 mode = MBEDTLS_MODE_STREAM;
2312 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002313 case PSA_ALG_CTR:
2314 mode = MBEDTLS_MODE_CTR;
2315 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002316 case PSA_ALG_CFB:
2317 mode = MBEDTLS_MODE_CFB;
2318 break;
2319 case PSA_ALG_OFB:
2320 mode = MBEDTLS_MODE_OFB;
2321 break;
2322 case PSA_ALG_CBC_NO_PADDING:
2323 mode = MBEDTLS_MODE_CBC;
2324 break;
2325 case PSA_ALG_CBC_PKCS7:
2326 mode = MBEDTLS_MODE_CBC;
2327 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002328 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329 mode = MBEDTLS_MODE_CCM;
2330 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002331 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332 mode = MBEDTLS_MODE_GCM;
2333 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002334 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2335 mode = MBEDTLS_MODE_CHACHAPOLY;
2336 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337 default:
2338 return( NULL );
2339 }
2340 }
2341 else if( alg == PSA_ALG_CMAC )
2342 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343 else
2344 return( NULL );
2345
2346 switch( key_type )
2347 {
2348 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002349 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350 break;
2351 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002352 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2353 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002355 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002357 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002358 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2359 * but two-key Triple-DES is functionally three-key Triple-DES
2360 * with K1=K3, so that's how we present it to mbedtls. */
2361 if( key_bits == 128 )
2362 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002363 break;
2364 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002365 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002366 break;
2367 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002368 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002370 case PSA_KEY_TYPE_CHACHA20:
2371 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2372 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373 default:
2374 return( NULL );
2375 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002376 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002377 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378
Jaeden Amero23bbb752018-06-26 14:16:54 +01002379 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2380 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002381}
2382
Gilles Peskinea05219c2018-11-16 16:02:56 +01002383#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002384static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002385{
Gilles Peskine2d277862018-06-18 15:41:12 +02002386 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002387 {
2388 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002389 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002390 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002391 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002392 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002393 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002394 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002395 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002396 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002397 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002398 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002399 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002400 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002401 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002402 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002403 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002404 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002405 return( 128 );
2406 default:
2407 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002408 }
2409}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002410#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002411
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002412/* Initialize the MAC operation structure. Once this function has been
2413 * called, psa_mac_abort can run and will do the right thing. */
2414static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2415 psa_algorithm_t alg )
2416{
2417 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2418
2419 operation->alg = alg;
2420 operation->key_set = 0;
2421 operation->iv_set = 0;
2422 operation->iv_required = 0;
2423 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002424 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002425
2426#if defined(MBEDTLS_CMAC_C)
2427 if( alg == PSA_ALG_CMAC )
2428 {
2429 operation->iv_required = 0;
2430 mbedtls_cipher_init( &operation->ctx.cmac );
2431 status = PSA_SUCCESS;
2432 }
2433 else
2434#endif /* MBEDTLS_CMAC_C */
2435#if defined(MBEDTLS_MD_C)
2436 if( PSA_ALG_IS_HMAC( operation->alg ) )
2437 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002438 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2439 operation->ctx.hmac.hash_ctx.alg = 0;
2440 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002441 }
2442 else
2443#endif /* MBEDTLS_MD_C */
2444 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002445 if( ! PSA_ALG_IS_MAC( alg ) )
2446 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447 }
2448
2449 if( status != PSA_SUCCESS )
2450 memset( operation, 0, sizeof( *operation ) );
2451 return( status );
2452}
2453
Gilles Peskine01126fa2018-07-12 17:04:55 +02002454#if defined(MBEDTLS_MD_C)
2455static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2456{
Gilles Peskine3f108122018-12-07 18:14:53 +01002457 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002458 return( psa_hash_abort( &hmac->hash_ctx ) );
2459}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002460
Janos Follath999f6482019-06-11 12:04:10 +01002461#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002462static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2463{
2464 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2465 memset( hmac, 0, sizeof( *hmac ) );
2466}
Janos Follath999f6482019-06-11 12:04:10 +01002467#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002468#endif /* MBEDTLS_MD_C */
2469
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2471{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002472 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002474 /* The object has (apparently) been initialized but it is not
2475 * in use. It's ok to call abort on such an object, and there's
2476 * nothing to do. */
2477 return( PSA_SUCCESS );
2478 }
2479 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002481 if( operation->alg == PSA_ALG_CMAC )
2482 {
2483 mbedtls_cipher_free( &operation->ctx.cmac );
2484 }
2485 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002488 if( PSA_ALG_IS_HMAC( operation->alg ) )
2489 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002490 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002491 }
2492 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002494 {
2495 /* Sanity check (shouldn't happen: operation->alg should
2496 * always have been initialized to a valid value). */
2497 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498 }
Moran Peker41deec42018-04-04 15:43:05 +03002499
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 operation->alg = 0;
2501 operation->key_set = 0;
2502 operation->iv_set = 0;
2503 operation->iv_required = 0;
2504 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002505 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002506
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002508
2509bad_state:
2510 /* If abort is called on an uninitialized object, we can't trust
2511 * anything. Wipe the object in case it contains confidential data.
2512 * This may result in a memory leak if a pointer gets overwritten,
2513 * but it's too late to do anything about this. */
2514 memset( operation, 0, sizeof( *operation ) );
2515 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516}
2517
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002518#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002519static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002520 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002521 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002522 const mbedtls_cipher_info_t *cipher_info )
2523{
2524 int ret;
2525
2526 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002527
2528 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2529 if( ret != 0 )
2530 return( ret );
2531
2532 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2533 slot->data.raw.data,
2534 key_bits );
2535 return( ret );
2536}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002537#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002538
Gilles Peskine248051a2018-06-20 16:09:38 +02002539#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002540static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2541 const uint8_t *key,
2542 size_t key_length,
2543 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002544{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002545 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002546 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002547 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002548 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002549 psa_status_t status;
2550
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002551 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2552 * overflow below. This should never trigger if the hash algorithm
2553 * is implemented correctly. */
2554 /* The size checks against the ipad and opad buffers cannot be written
2555 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2556 * because that triggers -Wlogical-op on GCC 7.3. */
2557 if( block_size > sizeof( ipad ) )
2558 return( PSA_ERROR_NOT_SUPPORTED );
2559 if( block_size > sizeof( hmac->opad ) )
2560 return( PSA_ERROR_NOT_SUPPORTED );
2561 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002562 return( PSA_ERROR_NOT_SUPPORTED );
2563
Gilles Peskined223b522018-06-11 18:12:58 +02002564 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002565 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002566 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002567 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002568 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002569 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002570 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002571 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002572 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002573 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002574 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002575 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002576 }
Gilles Peskine96889972018-07-12 17:07:03 +02002577 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2578 * but it is permitted. It is common when HMAC is used in HKDF, for
2579 * example. Don't call `memcpy` in the 0-length because `key` could be
2580 * an invalid pointer which would make the behavior undefined. */
2581 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002582 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002583
Gilles Peskined223b522018-06-11 18:12:58 +02002584 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2585 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002586 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002587 ipad[i] ^= 0x36;
2588 memset( ipad + key_length, 0x36, block_size - key_length );
2589
2590 /* Copy the key material from ipad to opad, flipping the requisite bits,
2591 * and filling the rest of opad with the requisite constant. */
2592 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002593 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2594 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002595
Gilles Peskine01126fa2018-07-12 17:04:55 +02002596 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002597 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002598 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002599
Gilles Peskine01126fa2018-07-12 17:04:55 +02002600 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002601
2602cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002603 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002604
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002605 return( status );
2606}
Gilles Peskine248051a2018-06-20 16:09:38 +02002607#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002608
Gilles Peskine89167cb2018-07-08 20:12:23 +02002609static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002610 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002611 psa_algorithm_t alg,
2612 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002615 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002617 psa_key_usage_t usage =
2618 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002619 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002620 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002622 /* A context must be freshly initialized before it can be set up. */
2623 if( operation->alg != 0 )
2624 {
2625 return( PSA_ERROR_BAD_STATE );
2626 }
2627
Gilles Peskined911eb72018-08-14 15:18:45 +02002628 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629 if( status != PSA_SUCCESS )
2630 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002631 if( is_sign )
2632 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633
Gilles Peskine28f8f302019-07-24 13:30:31 +02002634 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002635 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002636 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002637 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002638
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002640 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002641 {
2642 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002643 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002644 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002645 int ret;
2646 if( cipher_info == NULL )
2647 {
2648 status = PSA_ERROR_NOT_SUPPORTED;
2649 goto exit;
2650 }
2651 operation->mac_size = cipher_info->block_size;
2652 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2653 status = mbedtls_to_psa_error( ret );
2654 }
2655 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002658 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002659 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002660 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002661 if( hash_alg == 0 )
2662 {
2663 status = PSA_ERROR_NOT_SUPPORTED;
2664 goto exit;
2665 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002666
2667 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2668 /* Sanity check. This shouldn't fail on a valid configuration. */
2669 if( operation->mac_size == 0 ||
2670 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2671 {
2672 status = PSA_ERROR_NOT_SUPPORTED;
2673 goto exit;
2674 }
2675
Gilles Peskine8e338702019-07-30 20:06:31 +02002676 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002677 {
2678 status = PSA_ERROR_INVALID_ARGUMENT;
2679 goto exit;
2680 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002681
Gilles Peskine01126fa2018-07-12 17:04:55 +02002682 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2683 slot->data.raw.data,
2684 slot->data.raw.bytes,
2685 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002686 }
2687 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002689 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002690 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002691 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002693
Gilles Peskined911eb72018-08-14 15:18:45 +02002694 if( truncated == 0 )
2695 {
2696 /* The "normal" case: untruncated algorithm. Nothing to do. */
2697 }
2698 else if( truncated < 4 )
2699 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002700 /* A very short MAC is too short for security since it can be
2701 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2702 * so we make this our minimum, even though 32 bits is still
2703 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002704 status = PSA_ERROR_NOT_SUPPORTED;
2705 }
2706 else if( truncated > operation->mac_size )
2707 {
2708 /* It's impossible to "truncate" to a larger length. */
2709 status = PSA_ERROR_INVALID_ARGUMENT;
2710 }
2711 else
2712 operation->mac_size = truncated;
2713
Gilles Peskinefbfac682018-07-08 20:51:54 +02002714exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002717 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719 else
2720 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721 operation->key_set = 1;
2722 }
2723 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724}
2725
Gilles Peskine89167cb2018-07-08 20:12:23 +02002726psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002727 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002728 psa_algorithm_t alg )
2729{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002730 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002731}
2732
2733psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002734 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002735 psa_algorithm_t alg )
2736{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002737 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002738}
2739
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2741 const uint8_t *input,
2742 size_t input_length )
2743{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002744 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002746 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002748 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749 operation->has_input = 1;
2750
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002752 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002753 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002754 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2755 input, input_length );
2756 status = mbedtls_to_psa_error( ret );
2757 }
2758 else
2759#endif /* MBEDTLS_CMAC_C */
2760#if defined(MBEDTLS_MD_C)
2761 if( PSA_ALG_IS_HMAC( operation->alg ) )
2762 {
2763 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2764 input_length );
2765 }
2766 else
2767#endif /* MBEDTLS_MD_C */
2768 {
2769 /* This shouldn't happen if `operation` was initialized by
2770 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002771 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002772 }
2773
Gilles Peskinefbfac682018-07-08 20:51:54 +02002774 if( status != PSA_SUCCESS )
2775 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002776 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777}
2778
Gilles Peskine01126fa2018-07-12 17:04:55 +02002779#if defined(MBEDTLS_MD_C)
2780static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2781 uint8_t *mac,
2782 size_t mac_size )
2783{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002784 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002785 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2786 size_t hash_size = 0;
2787 size_t block_size = psa_get_hash_block_size( hash_alg );
2788 psa_status_t status;
2789
Gilles Peskine01126fa2018-07-12 17:04:55 +02002790 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2791 if( status != PSA_SUCCESS )
2792 return( status );
2793 /* From here on, tmp needs to be wiped. */
2794
2795 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2796 if( status != PSA_SUCCESS )
2797 goto exit;
2798
2799 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2800 if( status != PSA_SUCCESS )
2801 goto exit;
2802
2803 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2804 if( status != PSA_SUCCESS )
2805 goto exit;
2806
Gilles Peskined911eb72018-08-14 15:18:45 +02002807 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2808 if( status != PSA_SUCCESS )
2809 goto exit;
2810
2811 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002812
2813exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002814 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002815 return( status );
2816}
2817#endif /* MBEDTLS_MD_C */
2818
mohammad16036df908f2018-04-02 08:34:15 -07002819static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002820 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002821 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002823 if( ! operation->key_set )
2824 return( PSA_ERROR_BAD_STATE );
2825 if( operation->iv_required && ! operation->iv_set )
2826 return( PSA_ERROR_BAD_STATE );
2827
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828 if( mac_size < operation->mac_size )
2829 return( PSA_ERROR_BUFFER_TOO_SMALL );
2830
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002832 if( operation->alg == PSA_ALG_CMAC )
2833 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002834 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2835 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2836 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002837 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002838 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002839 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002841 else
2842#endif /* MBEDTLS_CMAC_C */
2843#if defined(MBEDTLS_MD_C)
2844 if( PSA_ALG_IS_HMAC( operation->alg ) )
2845 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002846 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002847 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002848 }
2849 else
2850#endif /* MBEDTLS_MD_C */
2851 {
2852 /* This shouldn't happen if `operation` was initialized by
2853 * a setup function. */
2854 return( PSA_ERROR_BAD_STATE );
2855 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856}
2857
Gilles Peskineacd4be32018-07-08 19:56:25 +02002858psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2859 uint8_t *mac,
2860 size_t mac_size,
2861 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002862{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002863 psa_status_t status;
2864
Jaeden Amero252ef282019-02-15 14:05:35 +00002865 if( operation->alg == 0 )
2866 {
2867 return( PSA_ERROR_BAD_STATE );
2868 }
2869
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002870 /* Fill the output buffer with something that isn't a valid mac
2871 * (barring an attack on the mac and deliberately-crafted input),
2872 * in case the caller doesn't check the return status properly. */
2873 *mac_length = mac_size;
2874 /* If mac_size is 0 then mac may be NULL and then the
2875 * call to memset would have undefined behavior. */
2876 if( mac_size != 0 )
2877 memset( mac, '!', mac_size );
2878
Gilles Peskine89167cb2018-07-08 20:12:23 +02002879 if( ! operation->is_sign )
2880 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002881 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002882 }
mohammad16036df908f2018-04-02 08:34:15 -07002883
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002884 status = psa_mac_finish_internal( operation, mac, mac_size );
2885
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002886 if( status == PSA_SUCCESS )
2887 {
2888 status = psa_mac_abort( operation );
2889 if( status == PSA_SUCCESS )
2890 *mac_length = operation->mac_size;
2891 else
2892 memset( mac, '!', mac_size );
2893 }
2894 else
2895 psa_mac_abort( operation );
2896 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002897}
2898
Gilles Peskineacd4be32018-07-08 19:56:25 +02002899psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2900 const uint8_t *mac,
2901 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902{
Gilles Peskine828ed142018-06-18 23:25:51 +02002903 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002904 psa_status_t status;
2905
Jaeden Amero252ef282019-02-15 14:05:35 +00002906 if( operation->alg == 0 )
2907 {
2908 return( PSA_ERROR_BAD_STATE );
2909 }
2910
Gilles Peskine89167cb2018-07-08 20:12:23 +02002911 if( operation->is_sign )
2912 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002913 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002914 }
2915 if( operation->mac_size != mac_length )
2916 {
2917 status = PSA_ERROR_INVALID_SIGNATURE;
2918 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002919 }
mohammad16036df908f2018-04-02 08:34:15 -07002920
2921 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002922 actual_mac, sizeof( actual_mac ) );
2923
2924 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2925 status = PSA_ERROR_INVALID_SIGNATURE;
2926
2927cleanup:
2928 if( status == PSA_SUCCESS )
2929 status = psa_mac_abort( operation );
2930 else
2931 psa_mac_abort( operation );
2932
Gilles Peskine3f108122018-12-07 18:14:53 +01002933 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002934
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002935 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936}
2937
2938
Gilles Peskine20035e32018-02-03 22:44:14 +01002939
Gilles Peskine20035e32018-02-03 22:44:14 +01002940/****************************************************************/
2941/* Asymmetric cryptography */
2942/****************************************************************/
2943
Gilles Peskine2b450e32018-06-27 15:42:46 +02002944#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002945/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002946 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002947static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2948 size_t hash_length,
2949 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002950{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002951 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002952 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002953 *md_alg = mbedtls_md_get_type( md_info );
2954
2955 /* The Mbed TLS RSA module uses an unsigned int for hash length
2956 * parameters. Validate that it fits so that we don't risk an
2957 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002958#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002959 if( hash_length > UINT_MAX )
2960 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002961#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002962
2963#if defined(MBEDTLS_PKCS1_V15)
2964 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2965 * must be correct. */
2966 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2967 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002968 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002969 if( md_info == NULL )
2970 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002971 if( mbedtls_md_get_size( md_info ) != hash_length )
2972 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002973 }
2974#endif /* MBEDTLS_PKCS1_V15 */
2975
2976#if defined(MBEDTLS_PKCS1_V21)
2977 /* PSS requires a hash internally. */
2978 if( PSA_ALG_IS_RSA_PSS( alg ) )
2979 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002980 if( md_info == NULL )
2981 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002982 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002983#endif /* MBEDTLS_PKCS1_V21 */
2984
Gilles Peskine61b91d42018-06-08 16:09:36 +02002985 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002986}
2987
Gilles Peskine2b450e32018-06-27 15:42:46 +02002988static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2989 psa_algorithm_t alg,
2990 const uint8_t *hash,
2991 size_t hash_length,
2992 uint8_t *signature,
2993 size_t signature_size,
2994 size_t *signature_length )
2995{
2996 psa_status_t status;
2997 int ret;
2998 mbedtls_md_type_t md_alg;
2999
3000 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3001 if( status != PSA_SUCCESS )
3002 return( status );
3003
Gilles Peskine630a18a2018-06-29 17:49:35 +02003004 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003005 return( PSA_ERROR_BUFFER_TOO_SMALL );
3006
3007#if defined(MBEDTLS_PKCS1_V15)
3008 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3009 {
3010 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3011 MBEDTLS_MD_NONE );
3012 ret = mbedtls_rsa_pkcs1_sign( rsa,
3013 mbedtls_ctr_drbg_random,
3014 &global_data.ctr_drbg,
3015 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003016 md_alg,
3017 (unsigned int) hash_length,
3018 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003019 signature );
3020 }
3021 else
3022#endif /* MBEDTLS_PKCS1_V15 */
3023#if defined(MBEDTLS_PKCS1_V21)
3024 if( PSA_ALG_IS_RSA_PSS( alg ) )
3025 {
3026 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3027 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3028 mbedtls_ctr_drbg_random,
3029 &global_data.ctr_drbg,
3030 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003031 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003032 (unsigned int) hash_length,
3033 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003034 signature );
3035 }
3036 else
3037#endif /* MBEDTLS_PKCS1_V21 */
3038 {
3039 return( PSA_ERROR_INVALID_ARGUMENT );
3040 }
3041
3042 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003043 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003044 return( mbedtls_to_psa_error( ret ) );
3045}
3046
3047static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3048 psa_algorithm_t alg,
3049 const uint8_t *hash,
3050 size_t hash_length,
3051 const uint8_t *signature,
3052 size_t signature_length )
3053{
3054 psa_status_t status;
3055 int ret;
3056 mbedtls_md_type_t md_alg;
3057
3058 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3059 if( status != PSA_SUCCESS )
3060 return( status );
3061
Gilles Peskine630a18a2018-06-29 17:49:35 +02003062 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003063 return( PSA_ERROR_BUFFER_TOO_SMALL );
3064
3065#if defined(MBEDTLS_PKCS1_V15)
3066 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3067 {
3068 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3069 MBEDTLS_MD_NONE );
3070 ret = mbedtls_rsa_pkcs1_verify( rsa,
3071 mbedtls_ctr_drbg_random,
3072 &global_data.ctr_drbg,
3073 MBEDTLS_RSA_PUBLIC,
3074 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003075 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003076 hash,
3077 signature );
3078 }
3079 else
3080#endif /* MBEDTLS_PKCS1_V15 */
3081#if defined(MBEDTLS_PKCS1_V21)
3082 if( PSA_ALG_IS_RSA_PSS( alg ) )
3083 {
3084 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3085 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3086 mbedtls_ctr_drbg_random,
3087 &global_data.ctr_drbg,
3088 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003089 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003090 (unsigned int) hash_length,
3091 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003092 signature );
3093 }
3094 else
3095#endif /* MBEDTLS_PKCS1_V21 */
3096 {
3097 return( PSA_ERROR_INVALID_ARGUMENT );
3098 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003099
3100 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3101 * the rest of the signature is invalid". This has little use in
3102 * practice and PSA doesn't report this distinction. */
3103 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3104 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003105 return( mbedtls_to_psa_error( ret ) );
3106}
3107#endif /* MBEDTLS_RSA_C */
3108
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003109#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003110/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3111 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3112 * (even though these functions don't modify it). */
3113static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3114 psa_algorithm_t alg,
3115 const uint8_t *hash,
3116 size_t hash_length,
3117 uint8_t *signature,
3118 size_t signature_size,
3119 size_t *signature_length )
3120{
3121 int ret;
3122 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003123 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003124 mbedtls_mpi_init( &r );
3125 mbedtls_mpi_init( &s );
3126
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003127 if( signature_size < 2 * curve_bytes )
3128 {
3129 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3130 goto cleanup;
3131 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003132
Gilles Peskinea05219c2018-11-16 16:02:56 +01003133#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003134 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3135 {
3136 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3137 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3138 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3139 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3140 hash, hash_length,
3141 md_alg ) );
3142 }
3143 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003144#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003145 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003146 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003147 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3148 hash, hash_length,
3149 mbedtls_ctr_drbg_random,
3150 &global_data.ctr_drbg ) );
3151 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003152
3153 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3154 signature,
3155 curve_bytes ) );
3156 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3157 signature + curve_bytes,
3158 curve_bytes ) );
3159
3160cleanup:
3161 mbedtls_mpi_free( &r );
3162 mbedtls_mpi_free( &s );
3163 if( ret == 0 )
3164 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003165 return( mbedtls_to_psa_error( ret ) );
3166}
3167
3168static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3169 const uint8_t *hash,
3170 size_t hash_length,
3171 const uint8_t *signature,
3172 size_t signature_length )
3173{
3174 int ret;
3175 mbedtls_mpi r, s;
3176 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3177 mbedtls_mpi_init( &r );
3178 mbedtls_mpi_init( &s );
3179
3180 if( signature_length != 2 * curve_bytes )
3181 return( PSA_ERROR_INVALID_SIGNATURE );
3182
3183 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3184 signature,
3185 curve_bytes ) );
3186 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3187 signature + curve_bytes,
3188 curve_bytes ) );
3189
3190 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3191 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003192
3193cleanup:
3194 mbedtls_mpi_free( &r );
3195 mbedtls_mpi_free( &s );
3196 return( mbedtls_to_psa_error( ret ) );
3197}
3198#endif /* MBEDTLS_ECDSA_C */
3199
Gilles Peskinec5487a82018-12-03 18:08:14 +01003200psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003201 psa_algorithm_t alg,
3202 const uint8_t *hash,
3203 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003204 uint8_t *signature,
3205 size_t signature_size,
3206 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003207{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003208 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003209 psa_status_t status;
3210
3211 *signature_length = signature_size;
3212
Gilles Peskine28f8f302019-07-24 13:30:31 +02003213 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003214 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003215 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003216 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003217 {
3218 status = PSA_ERROR_INVALID_ARGUMENT;
3219 goto exit;
3220 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003221
Gilles Peskine20035e32018-02-03 22:44:14 +01003222#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003223 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003224 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003225 status = psa_rsa_sign( slot->data.rsa,
3226 alg,
3227 hash, hash_length,
3228 signature, signature_size,
3229 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003230 }
3231 else
3232#endif /* defined(MBEDTLS_RSA_C) */
3233#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003234 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003235 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003236#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003237 if(
3238#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3239 PSA_ALG_IS_ECDSA( alg )
3240#else
3241 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3242#endif
3243 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003244 status = psa_ecdsa_sign( slot->data.ecp,
3245 alg,
3246 hash, hash_length,
3247 signature, signature_size,
3248 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003249 else
3250#endif /* defined(MBEDTLS_ECDSA_C) */
3251 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003252 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003253 }
itayzafrir5c753392018-05-08 11:18:38 +03003254 }
3255 else
3256#endif /* defined(MBEDTLS_ECP_C) */
3257 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003258 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003259 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003260
3261exit:
3262 /* Fill the unused part of the output buffer (the whole buffer on error,
3263 * the trailing part on success) with something that isn't a valid mac
3264 * (barring an attack on the mac and deliberately-crafted input),
3265 * in case the caller doesn't check the return status properly. */
3266 if( status == PSA_SUCCESS )
3267 memset( signature + *signature_length, '!',
3268 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003269 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003270 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003271 /* If signature_size is 0 then we have nothing to do. We must not call
3272 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003273 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003274}
3275
Gilles Peskinec5487a82018-12-03 18:08:14 +01003276psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003277 psa_algorithm_t alg,
3278 const uint8_t *hash,
3279 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003280 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003281 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003282{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003283 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003284 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003285
Gilles Peskine28f8f302019-07-24 13:30:31 +02003286 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003287 if( status != PSA_SUCCESS )
3288 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003289
Gilles Peskine61b91d42018-06-08 16:09:36 +02003290#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003291 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003293 return( psa_rsa_verify( slot->data.rsa,
3294 alg,
3295 hash, hash_length,
3296 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297 }
3298 else
3299#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003300#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003301 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003302 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003303#if defined(MBEDTLS_ECDSA_C)
3304 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003305 return( psa_ecdsa_verify( slot->data.ecp,
3306 hash, hash_length,
3307 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003308 else
3309#endif /* defined(MBEDTLS_ECDSA_C) */
3310 {
3311 return( PSA_ERROR_INVALID_ARGUMENT );
3312 }
itayzafrir5c753392018-05-08 11:18:38 +03003313 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003314 else
3315#endif /* defined(MBEDTLS_ECP_C) */
3316 {
3317 return( PSA_ERROR_NOT_SUPPORTED );
3318 }
3319}
3320
Gilles Peskine072ac562018-06-30 00:21:29 +02003321#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3322static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3323 mbedtls_rsa_context *rsa )
3324{
3325 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3326 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3327 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3328 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3329}
3330#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3331
Gilles Peskinec5487a82018-12-03 18:08:14 +01003332psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003333 psa_algorithm_t alg,
3334 const uint8_t *input,
3335 size_t input_length,
3336 const uint8_t *salt,
3337 size_t salt_length,
3338 uint8_t *output,
3339 size_t output_size,
3340 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003342 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003343 psa_status_t status;
3344
Darryl Green5cc689a2018-07-24 15:34:10 +01003345 (void) input;
3346 (void) input_length;
3347 (void) salt;
3348 (void) output;
3349 (void) output_size;
3350
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003351 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003353 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3354 return( PSA_ERROR_INVALID_ARGUMENT );
3355
Gilles Peskine28f8f302019-07-24 13:30:31 +02003356 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003357 if( status != PSA_SUCCESS )
3358 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003359 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3360 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003361 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362
3363#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003364 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365 {
3366 mbedtls_rsa_context *rsa = slot->data.rsa;
3367 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003368 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003369 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003370#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003371 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003372 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003373 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3374 mbedtls_ctr_drbg_random,
3375 &global_data.ctr_drbg,
3376 MBEDTLS_RSA_PUBLIC,
3377 input_length,
3378 input,
3379 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003380 }
3381 else
3382#endif /* MBEDTLS_PKCS1_V15 */
3383#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003384 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003385 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003386 psa_rsa_oaep_set_padding_mode( alg, rsa );
3387 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3388 mbedtls_ctr_drbg_random,
3389 &global_data.ctr_drbg,
3390 MBEDTLS_RSA_PUBLIC,
3391 salt, salt_length,
3392 input_length,
3393 input,
3394 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003395 }
3396 else
3397#endif /* MBEDTLS_PKCS1_V21 */
3398 {
3399 return( PSA_ERROR_INVALID_ARGUMENT );
3400 }
3401 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003402 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003403 return( mbedtls_to_psa_error( ret ) );
3404 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003405 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003406#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003407 {
3408 return( PSA_ERROR_NOT_SUPPORTED );
3409 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003410}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003411
Gilles Peskinec5487a82018-12-03 18:08:14 +01003412psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003413 psa_algorithm_t alg,
3414 const uint8_t *input,
3415 size_t input_length,
3416 const uint8_t *salt,
3417 size_t salt_length,
3418 uint8_t *output,
3419 size_t output_size,
3420 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003421{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003422 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003423 psa_status_t status;
3424
Darryl Green5cc689a2018-07-24 15:34:10 +01003425 (void) input;
3426 (void) input_length;
3427 (void) salt;
3428 (void) output;
3429 (void) output_size;
3430
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003431 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003432
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003433 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3434 return( PSA_ERROR_INVALID_ARGUMENT );
3435
Gilles Peskine28f8f302019-07-24 13:30:31 +02003436 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003437 if( status != PSA_SUCCESS )
3438 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003439 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440 return( PSA_ERROR_INVALID_ARGUMENT );
3441
3442#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003443 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003444 {
3445 mbedtls_rsa_context *rsa = slot->data.rsa;
3446 int ret;
3447
Gilles Peskine630a18a2018-06-29 17:49:35 +02003448 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003449 return( PSA_ERROR_INVALID_ARGUMENT );
3450
3451#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003452 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003454 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3455 mbedtls_ctr_drbg_random,
3456 &global_data.ctr_drbg,
3457 MBEDTLS_RSA_PRIVATE,
3458 output_length,
3459 input,
3460 output,
3461 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003462 }
3463 else
3464#endif /* MBEDTLS_PKCS1_V15 */
3465#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003466 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003467 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003468 psa_rsa_oaep_set_padding_mode( alg, rsa );
3469 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3470 mbedtls_ctr_drbg_random,
3471 &global_data.ctr_drbg,
3472 MBEDTLS_RSA_PRIVATE,
3473 salt, salt_length,
3474 output_length,
3475 input,
3476 output,
3477 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003478 }
3479 else
3480#endif /* MBEDTLS_PKCS1_V21 */
3481 {
3482 return( PSA_ERROR_INVALID_ARGUMENT );
3483 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003484
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003485 return( mbedtls_to_psa_error( ret ) );
3486 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003487 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003488#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003489 {
3490 return( PSA_ERROR_NOT_SUPPORTED );
3491 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003492}
Gilles Peskine20035e32018-02-03 22:44:14 +01003493
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003494
3495
mohammad1603503973b2018-03-12 15:59:30 +02003496/****************************************************************/
3497/* Symmetric cryptography */
3498/****************************************************************/
3499
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003500/* Initialize the cipher operation structure. Once this function has been
3501 * called, psa_cipher_abort can run and will do the right thing. */
3502static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3503 psa_algorithm_t alg )
3504{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003505 if( ! PSA_ALG_IS_CIPHER( alg ) )
3506 {
3507 memset( operation, 0, sizeof( *operation ) );
3508 return( PSA_ERROR_INVALID_ARGUMENT );
3509 }
3510
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003511 operation->alg = alg;
3512 operation->key_set = 0;
3513 operation->iv_set = 0;
3514 operation->iv_required = 1;
3515 operation->iv_size = 0;
3516 operation->block_size = 0;
3517 mbedtls_cipher_init( &operation->ctx.cipher );
3518 return( PSA_SUCCESS );
3519}
3520
Gilles Peskinee553c652018-06-04 16:22:46 +02003521static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003522 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003523 psa_algorithm_t alg,
3524 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003525{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003526 int ret = 0;
3527 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003528 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003529 size_t key_bits;
3530 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003531 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3532 PSA_KEY_USAGE_ENCRYPT :
3533 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003534
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003535 /* A context must be freshly initialized before it can be set up. */
3536 if( operation->alg != 0 )
3537 {
3538 return( PSA_ERROR_BAD_STATE );
3539 }
3540
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003541 status = psa_cipher_init( operation, alg );
3542 if( status != PSA_SUCCESS )
3543 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003544
Gilles Peskine28f8f302019-07-24 13:30:31 +02003545 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003546 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003547 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003548 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003549
Gilles Peskine8e338702019-07-30 20:06:31 +02003550 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003551 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003552 {
3553 status = PSA_ERROR_NOT_SUPPORTED;
3554 goto exit;
3555 }
mohammad1603503973b2018-03-12 15:59:30 +02003556
mohammad1603503973b2018-03-12 15:59:30 +02003557 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003558 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003559 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003560
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003561#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003562 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003563 {
3564 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003565 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003566 memcpy( keys, slot->data.raw.data, 16 );
3567 memcpy( keys + 16, slot->data.raw.data, 8 );
3568 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3569 keys,
3570 192, cipher_operation );
3571 }
3572 else
3573#endif
3574 {
3575 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3576 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003577 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003578 }
Moran Peker41deec42018-04-04 15:43:05 +03003579 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003580 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003581
mohammad16038481e742018-03-18 13:57:31 +02003582#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003583 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003584 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003585 case PSA_ALG_CBC_NO_PADDING:
3586 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3587 MBEDTLS_PADDING_NONE );
3588 break;
3589 case PSA_ALG_CBC_PKCS7:
3590 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3591 MBEDTLS_PADDING_PKCS7 );
3592 break;
3593 default:
3594 /* The algorithm doesn't involve padding. */
3595 ret = 0;
3596 break;
3597 }
3598 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003599 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003600#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3601
mohammad1603503973b2018-03-12 15:59:30 +02003602 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003603 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003604 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003605 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003606 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003607 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003608 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003609#if defined(MBEDTLS_CHACHA20_C)
3610 else
3611 if( alg == PSA_ALG_CHACHA20 )
3612 operation->iv_size = 12;
3613#endif
mohammad1603503973b2018-03-12 15:59:30 +02003614
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003615exit:
3616 if( status == 0 )
3617 status = mbedtls_to_psa_error( ret );
3618 if( status != 0 )
3619 psa_cipher_abort( operation );
3620 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003621}
3622
Gilles Peskinefe119512018-07-08 21:39:34 +02003623psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003624 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003625 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003626{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003627 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003628}
3629
Gilles Peskinefe119512018-07-08 21:39:34 +02003630psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003631 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003632 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003633{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003634 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003635}
3636
Gilles Peskinefe119512018-07-08 21:39:34 +02003637psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003638 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003639 size_t iv_size,
3640 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003641{
itayzafrir534bd7c2018-08-02 13:56:32 +03003642 psa_status_t status;
3643 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003644 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003645 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003646 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003647 }
Moran Peker41deec42018-04-04 15:43:05 +03003648 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003649 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003650 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003651 goto exit;
3652 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003653 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3654 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003655 if( ret != 0 )
3656 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003657 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003658 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003659 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003660
mohammad16038481e742018-03-18 13:57:31 +02003661 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003662 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003663
Moran Peker395db872018-05-31 14:07:14 +03003664exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003665 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003666 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003667 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003668}
3669
Gilles Peskinefe119512018-07-08 21:39:34 +02003670psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003671 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003672 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003673{
itayzafrir534bd7c2018-08-02 13:56:32 +03003674 psa_status_t status;
3675 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003676 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003677 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003678 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003679 }
Moran Pekera28258c2018-05-29 16:25:04 +03003680 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003681 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003682 status = PSA_ERROR_INVALID_ARGUMENT;
3683 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003684 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003685 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3686 status = mbedtls_to_psa_error( ret );
3687exit:
3688 if( status == PSA_SUCCESS )
3689 operation->iv_set = 1;
3690 else
mohammad1603503973b2018-03-12 15:59:30 +02003691 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003692 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003693}
3694
Gilles Peskinee553c652018-06-04 16:22:46 +02003695psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3696 const uint8_t *input,
3697 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003698 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003699 size_t output_size,
3700 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003701{
itayzafrir534bd7c2018-08-02 13:56:32 +03003702 psa_status_t status;
3703 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003704 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003705
3706 if( operation->alg == 0 )
3707 {
3708 return( PSA_ERROR_BAD_STATE );
3709 }
3710
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003711 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003712 {
3713 /* Take the unprocessed partial block left over from previous
3714 * update calls, if any, plus the input to this call. Remove
3715 * the last partial block, if any. You get the data that will be
3716 * output in this call. */
3717 expected_output_size =
3718 ( operation->ctx.cipher.unprocessed_len + input_length )
3719 / operation->block_size * operation->block_size;
3720 }
3721 else
3722 {
3723 expected_output_size = input_length;
3724 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003725
Gilles Peskine89d789c2018-06-04 17:17:16 +02003726 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003727 {
3728 status = PSA_ERROR_BUFFER_TOO_SMALL;
3729 goto exit;
3730 }
mohammad160382759612018-03-12 18:16:40 +02003731
mohammad1603503973b2018-03-12 15:59:30 +02003732 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003733 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003734 status = mbedtls_to_psa_error( ret );
3735exit:
3736 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003737 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003738 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003739}
3740
Gilles Peskinee553c652018-06-04 16:22:46 +02003741psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3742 uint8_t *output,
3743 size_t output_size,
3744 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003745{
David Saadab4ecc272019-02-14 13:48:10 +02003746 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003747 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003748 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003749
mohammad1603503973b2018-03-12 15:59:30 +02003750 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003751 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003752 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003753 }
3754 if( operation->iv_required && ! operation->iv_set )
3755 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003756 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003757 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003758
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003759 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003760 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3761 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003762 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003763 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003764 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003765 }
3766
Janos Follath315b51c2018-07-09 16:04:51 +01003767 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3768 temp_output_buffer,
3769 output_length );
3770 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003771 {
Janos Follath315b51c2018-07-09 16:04:51 +01003772 status = mbedtls_to_psa_error( cipher_ret );
3773 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003774 }
Janos Follath315b51c2018-07-09 16:04:51 +01003775
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003776 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003777 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003778 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003779 memcpy( output, temp_output_buffer, *output_length );
3780 else
3781 {
Janos Follath315b51c2018-07-09 16:04:51 +01003782 status = PSA_ERROR_BUFFER_TOO_SMALL;
3783 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003784 }
mohammad1603503973b2018-03-12 15:59:30 +02003785
Gilles Peskine3f108122018-12-07 18:14:53 +01003786 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003787 status = psa_cipher_abort( operation );
3788
3789 return( status );
3790
3791error:
3792
3793 *output_length = 0;
3794
Gilles Peskine3f108122018-12-07 18:14:53 +01003795 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003796 (void) psa_cipher_abort( operation );
3797
3798 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003799}
3800
Gilles Peskinee553c652018-06-04 16:22:46 +02003801psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3802{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003803 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003804 {
3805 /* The object has (apparently) been initialized but it is not
3806 * in use. It's ok to call abort on such an object, and there's
3807 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003808 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003809 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003810
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003811 /* Sanity check (shouldn't happen: operation->alg should
3812 * always have been initialized to a valid value). */
3813 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3814 return( PSA_ERROR_BAD_STATE );
3815
mohammad1603503973b2018-03-12 15:59:30 +02003816 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003817
Moran Peker41deec42018-04-04 15:43:05 +03003818 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003819 operation->key_set = 0;
3820 operation->iv_set = 0;
3821 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003822 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003823 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003824
Moran Peker395db872018-05-31 14:07:14 +03003825 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003826}
3827
Gilles Peskinea0655c32018-04-30 17:06:50 +02003828
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003829
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003830
mohammad16035955c982018-04-26 00:53:03 +03003831/****************************************************************/
3832/* AEAD */
3833/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003834
Gilles Peskineedf9a652018-08-17 18:11:56 +02003835typedef struct
3836{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003837 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003838 const mbedtls_cipher_info_t *cipher_info;
3839 union
3840 {
3841#if defined(MBEDTLS_CCM_C)
3842 mbedtls_ccm_context ccm;
3843#endif /* MBEDTLS_CCM_C */
3844#if defined(MBEDTLS_GCM_C)
3845 mbedtls_gcm_context gcm;
3846#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003847#if defined(MBEDTLS_CHACHAPOLY_C)
3848 mbedtls_chachapoly_context chachapoly;
3849#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003851 psa_algorithm_t core_alg;
3852 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003853 uint8_t tag_length;
3854} aead_operation_t;
3855
Gilles Peskine30a9e412019-01-14 18:36:12 +01003856static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003857{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003858 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003859 {
3860#if defined(MBEDTLS_CCM_C)
3861 case PSA_ALG_CCM:
3862 mbedtls_ccm_free( &operation->ctx.ccm );
3863 break;
3864#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003865#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003866 case PSA_ALG_GCM:
3867 mbedtls_gcm_free( &operation->ctx.gcm );
3868 break;
3869#endif /* MBEDTLS_GCM_C */
3870 }
3871}
3872
3873static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003874 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003875 psa_key_usage_t usage,
3876 psa_algorithm_t alg )
3877{
3878 psa_status_t status;
3879 size_t key_bits;
3880 mbedtls_cipher_id_t cipher_id;
3881
Gilles Peskine28f8f302019-07-24 13:30:31 +02003882 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003883 if( status != PSA_SUCCESS )
3884 return( status );
3885
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003886 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887
3888 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003889 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003890 &cipher_id );
3891 if( operation->cipher_info == NULL )
3892 return( PSA_ERROR_NOT_SUPPORTED );
3893
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003894 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003895 {
3896#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003897 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3898 operation->core_alg = PSA_ALG_CCM;
3899 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003900 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3901 * The call to mbedtls_ccm_encrypt_and_tag or
3902 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003903 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003904 return( PSA_ERROR_INVALID_ARGUMENT );
3905 mbedtls_ccm_init( &operation->ctx.ccm );
3906 status = mbedtls_to_psa_error(
3907 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3908 operation->slot->data.raw.data,
3909 (unsigned int) key_bits ) );
3910 if( status != 0 )
3911 goto cleanup;
3912 break;
3913#endif /* MBEDTLS_CCM_C */
3914
3915#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003916 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3917 operation->core_alg = PSA_ALG_GCM;
3918 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003919 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3920 * The call to mbedtls_gcm_crypt_and_tag or
3921 * mbedtls_gcm_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_gcm_init( &operation->ctx.gcm );
3925 status = mbedtls_to_psa_error(
3926 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3927 operation->slot->data.raw.data,
3928 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003929 if( status != 0 )
3930 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003931 break;
3932#endif /* MBEDTLS_GCM_C */
3933
Gilles Peskine26869f22019-05-06 15:25:00 +02003934#if defined(MBEDTLS_CHACHAPOLY_C)
3935 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3936 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3937 operation->full_tag_length = 16;
3938 /* We only support the default tag length. */
3939 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3940 return( PSA_ERROR_NOT_SUPPORTED );
3941 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3942 status = mbedtls_to_psa_error(
3943 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3944 operation->slot->data.raw.data ) );
3945 if( status != 0 )
3946 goto cleanup;
3947 break;
3948#endif /* MBEDTLS_CHACHAPOLY_C */
3949
Gilles Peskineedf9a652018-08-17 18:11:56 +02003950 default:
3951 return( PSA_ERROR_NOT_SUPPORTED );
3952 }
3953
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003954 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3955 {
3956 status = PSA_ERROR_INVALID_ARGUMENT;
3957 goto cleanup;
3958 }
3959 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003960
Gilles Peskineedf9a652018-08-17 18:11:56 +02003961 return( PSA_SUCCESS );
3962
3963cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003964 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003965 return( status );
3966}
3967
Gilles Peskinec5487a82018-12-03 18:08:14 +01003968psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003969 psa_algorithm_t alg,
3970 const uint8_t *nonce,
3971 size_t nonce_length,
3972 const uint8_t *additional_data,
3973 size_t additional_data_length,
3974 const uint8_t *plaintext,
3975 size_t plaintext_length,
3976 uint8_t *ciphertext,
3977 size_t ciphertext_size,
3978 size_t *ciphertext_length )
3979{
mohammad16035955c982018-04-26 00:53:03 +03003980 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003981 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003982 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003983
mohammad1603f08a5502018-06-03 15:05:47 +03003984 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003985
Gilles Peskinec5487a82018-12-03 18:08:14 +01003986 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003987 if( status != PSA_SUCCESS )
3988 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003989
Gilles Peskineedf9a652018-08-17 18:11:56 +02003990 /* For all currently supported modes, the tag is at the end of the
3991 * ciphertext. */
3992 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3993 {
3994 status = PSA_ERROR_BUFFER_TOO_SMALL;
3995 goto exit;
3996 }
3997 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003998
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003999#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004000 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004001 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004002 status = mbedtls_to_psa_error(
4003 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4004 MBEDTLS_GCM_ENCRYPT,
4005 plaintext_length,
4006 nonce, nonce_length,
4007 additional_data, additional_data_length,
4008 plaintext, ciphertext,
4009 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004010 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004011 else
4012#endif /* MBEDTLS_GCM_C */
4013#if defined(MBEDTLS_CCM_C)
4014 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004015 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004016 status = mbedtls_to_psa_error(
4017 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4018 plaintext_length,
4019 nonce, nonce_length,
4020 additional_data,
4021 additional_data_length,
4022 plaintext, ciphertext,
4023 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004024 }
mohammad16035c8845f2018-05-09 05:40:09 -07004025 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004026#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004027#if defined(MBEDTLS_CHACHAPOLY_C)
4028 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4029 {
4030 if( nonce_length != 12 || operation.tag_length != 16 )
4031 {
4032 status = PSA_ERROR_NOT_SUPPORTED;
4033 goto exit;
4034 }
4035 status = mbedtls_to_psa_error(
4036 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4037 plaintext_length,
4038 nonce,
4039 additional_data,
4040 additional_data_length,
4041 plaintext,
4042 ciphertext,
4043 tag ) );
4044 }
4045 else
4046#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004047 {
mohammad1603554faad2018-06-03 15:07:38 +03004048 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004049 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004050
Gilles Peskineedf9a652018-08-17 18:11:56 +02004051 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4052 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004053
Gilles Peskineedf9a652018-08-17 18:11:56 +02004054exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004055 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004056 if( status == PSA_SUCCESS )
4057 *ciphertext_length = plaintext_length + operation.tag_length;
4058 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004059}
4060
Gilles Peskineee652a32018-06-01 19:23:52 +02004061/* Locate the tag in a ciphertext buffer containing the encrypted data
4062 * followed by the tag. Return the length of the part preceding the tag in
4063 * *plaintext_length. This is the size of the plaintext in modes where
4064 * the encrypted data has the same size as the plaintext, such as
4065 * CCM and GCM. */
4066static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4067 const uint8_t *ciphertext,
4068 size_t ciphertext_length,
4069 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004070 const uint8_t **p_tag )
4071{
4072 size_t payload_length;
4073 if( tag_length > ciphertext_length )
4074 return( PSA_ERROR_INVALID_ARGUMENT );
4075 payload_length = ciphertext_length - tag_length;
4076 if( payload_length > plaintext_size )
4077 return( PSA_ERROR_BUFFER_TOO_SMALL );
4078 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004079 return( PSA_SUCCESS );
4080}
4081
Gilles Peskinec5487a82018-12-03 18:08:14 +01004082psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004083 psa_algorithm_t alg,
4084 const uint8_t *nonce,
4085 size_t nonce_length,
4086 const uint8_t *additional_data,
4087 size_t additional_data_length,
4088 const uint8_t *ciphertext,
4089 size_t ciphertext_length,
4090 uint8_t *plaintext,
4091 size_t plaintext_size,
4092 size_t *plaintext_length )
4093{
mohammad16035955c982018-04-26 00:53:03 +03004094 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004095 aead_operation_t operation;
4096 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004097
Gilles Peskineee652a32018-06-01 19:23:52 +02004098 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004099
Gilles Peskinec5487a82018-12-03 18:08:14 +01004100 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004101 if( status != PSA_SUCCESS )
4102 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004103
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004104 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4105 ciphertext, ciphertext_length,
4106 plaintext_size, &tag );
4107 if( status != PSA_SUCCESS )
4108 goto exit;
4109
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004110#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004111 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004112 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004113 status = mbedtls_to_psa_error(
4114 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4115 ciphertext_length - operation.tag_length,
4116 nonce, nonce_length,
4117 additional_data,
4118 additional_data_length,
4119 tag, operation.tag_length,
4120 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004121 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004122 else
4123#endif /* MBEDTLS_GCM_C */
4124#if defined(MBEDTLS_CCM_C)
4125 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004126 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 status = mbedtls_to_psa_error(
4128 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4129 ciphertext_length - operation.tag_length,
4130 nonce, nonce_length,
4131 additional_data,
4132 additional_data_length,
4133 ciphertext, plaintext,
4134 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004135 }
mohammad160339574652018-06-01 04:39:53 -07004136 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004137#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004138#if defined(MBEDTLS_CHACHAPOLY_C)
4139 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4140 {
4141 if( nonce_length != 12 || operation.tag_length != 16 )
4142 {
4143 status = PSA_ERROR_NOT_SUPPORTED;
4144 goto exit;
4145 }
4146 status = mbedtls_to_psa_error(
4147 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4148 ciphertext_length - operation.tag_length,
4149 nonce,
4150 additional_data,
4151 additional_data_length,
4152 tag,
4153 ciphertext,
4154 plaintext ) );
4155 }
4156 else
4157#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004158 {
mohammad1603554faad2018-06-03 15:07:38 +03004159 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004160 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004161
Gilles Peskineedf9a652018-08-17 18:11:56 +02004162 if( status != PSA_SUCCESS && plaintext_size != 0 )
4163 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004164
Gilles Peskineedf9a652018-08-17 18:11:56 +02004165exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004166 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 if( status == PSA_SUCCESS )
4168 *plaintext_length = ciphertext_length - operation.tag_length;
4169 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004170}
4171
Gilles Peskinea0655c32018-04-30 17:06:50 +02004172
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004173
Gilles Peskine20035e32018-02-03 22:44:14 +01004174/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175/* Generators */
4176/****************************************************************/
4177
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004178#define HKDF_STATE_INIT 0 /* no input yet */
4179#define HKDF_STATE_STARTED 1 /* got salt */
4180#define HKDF_STATE_KEYED 2 /* got key */
4181#define HKDF_STATE_OUTPUT 3 /* output started */
4182
Gilles Peskinecbe66502019-05-16 16:59:18 +02004183static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004184 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004185{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004186 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4187 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004188 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004190}
4191
4192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194{
4195 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004197 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004198 {
4199 /* The object has (apparently) been initialized but it is not
4200 * in use. It's ok to call abort on such an object, and there's
4201 * nothing to do. */
4202 }
4203 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004204#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004205 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004206 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004207 mbedtls_free( operation->ctx.hkdf.info );
4208 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004209 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004210 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004211 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004212 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004213 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004214#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004215 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004216 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004217 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4218 operation->ctx.tls12_prf.key_len );
4219 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004220 }
Hanno Becker580fba12018-11-13 20:50:45 +00004221
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004222 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004223 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4225 operation->ctx.tls12_prf.Ai_with_seed_len );
4226 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004227 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004228#else
4229 if( operation->ctx.tls12_prf.seed != NULL )
4230 {
4231 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4232 operation->ctx.tls12_prf.seed_length );
4233 mbedtls_free( operation->ctx.tls12_prf.seed );
4234 }
4235
4236 if( operation->ctx.tls12_prf.label != NULL )
4237 {
4238 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4239 operation->ctx.tls12_prf.label_length );
4240 mbedtls_free( operation->ctx.tls12_prf.label );
4241 }
4242
4243 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4244
4245 /* We leave the fields Ai and output_block to be erased safely by the
4246 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004247#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004248 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004249 else
4250#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004251 {
4252 status = PSA_ERROR_BAD_STATE;
4253 }
Janos Follath083036a2019-06-11 10:22:26 +01004254 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004255 return( status );
4256}
4257
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004258psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004259 size_t *capacity)
4260{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004262 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004264 return PSA_ERROR_BAD_STATE;
4265 }
4266
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004267 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004268 return( PSA_SUCCESS );
4269}
4270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004272 size_t capacity )
4273{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004274 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004275 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004276 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004277 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004278 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004279 return( PSA_SUCCESS );
4280}
4281
Gilles Peskinebef7f142018-07-12 17:22:21 +02004282#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004283/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004284 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004285static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004286 psa_algorithm_t hash_alg,
4287 uint8_t *output,
4288 size_t output_length )
4289{
4290 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4291 psa_status_t status;
4292
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004293 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4294 return( PSA_ERROR_BAD_STATE );
4295 hkdf->state = HKDF_STATE_OUTPUT;
4296
Gilles Peskinebef7f142018-07-12 17:22:21 +02004297 while( output_length != 0 )
4298 {
4299 /* Copy what remains of the current block */
4300 uint8_t n = hash_length - hkdf->offset_in_block;
4301 if( n > output_length )
4302 n = (uint8_t) output_length;
4303 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4304 output += n;
4305 output_length -= n;
4306 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004307 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004308 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004309 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004310 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004312 * object was corrupted or if this function is called directly
4313 * inside the library. */
4314 if( hkdf->block_number == 0xff )
4315 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004316
4317 /* We need a new block */
4318 ++hkdf->block_number;
4319 hkdf->offset_in_block = 0;
4320 status = psa_hmac_setup_internal( &hkdf->hmac,
4321 hkdf->prk, hash_length,
4322 hash_alg );
4323 if( status != PSA_SUCCESS )
4324 return( status );
4325 if( hkdf->block_number != 1 )
4326 {
4327 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4328 hkdf->output_block,
4329 hash_length );
4330 if( status != PSA_SUCCESS )
4331 return( status );
4332 }
4333 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4334 hkdf->info,
4335 hkdf->info_length );
4336 if( status != PSA_SUCCESS )
4337 return( status );
4338 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4339 &hkdf->block_number, 1 );
4340 if( status != PSA_SUCCESS )
4341 return( status );
4342 status = psa_hmac_finish_internal( &hkdf->hmac,
4343 hkdf->output_block,
4344 sizeof( hkdf->output_block ) );
4345 if( status != PSA_SUCCESS )
4346 return( status );
4347 }
4348
4349 return( PSA_SUCCESS );
4350}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004351
Janos Follath999f6482019-06-11 12:04:10 +01004352#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004353static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4354 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004355 psa_algorithm_t alg )
4356{
4357 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4358 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4359 psa_hmac_internal_data hmac;
4360 psa_status_t status, cleanup_status;
4361
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004362 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004363 size_t Ai_len;
4364
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004365 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004366 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004368 * object was corrupted or if this function is called directly
4369 * inside the library. */
4370 if( tls12_prf->block_number == 0xff )
4371 return( PSA_ERROR_BAD_STATE );
4372
4373 /* We need a new block */
4374 ++tls12_prf->block_number;
4375 tls12_prf->offset_in_block = 0;
4376
4377 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4378 *
4379 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4380 *
4381 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4382 * HMAC_hash(secret, A(2) + seed) +
4383 * HMAC_hash(secret, A(3) + seed) + ...
4384 *
4385 * A(0) = seed
4386 * A(i) = HMAC_hash( secret, A(i-1) )
4387 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004388 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004389 * `HMAC_hash(secret, A(i) + seed)` from which the output
4390 * is currently extracted as `output_block`, while
4391 * `A(i) + seed` is stored in `Ai_with_seed`.
4392 *
4393 * Generating a new block means recalculating `Ai_with_seed`
4394 * from the A(i)-part of it, and afterwards recalculating
4395 * `output_block`.
4396 *
4397 * A(0) is computed at setup time.
4398 *
4399 */
4400
4401 psa_hmac_init_internal( &hmac );
4402
4403 /* We must distinguish the calculation of A(1) from those
4404 * of A(2) and higher, because A(0)=seed has a different
4405 * length than the other A(i). */
4406 if( tls12_prf->block_number == 1 )
4407 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004408 Ai = tls12_prf->Ai_with_seed + hash_length;
4409 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004410 }
4411 else
4412 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004413 Ai = tls12_prf->Ai_with_seed;
4414 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004415 }
4416
Hanno Becker3b339e22018-11-13 20:56:14 +00004417 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4418 status = psa_hmac_setup_internal( &hmac,
4419 tls12_prf->key,
4420 tls12_prf->key_len,
4421 hash_alg );
4422 if( status != PSA_SUCCESS )
4423 goto cleanup;
4424
4425 status = psa_hash_update( &hmac.hash_ctx,
4426 Ai, Ai_len );
4427 if( status != PSA_SUCCESS )
4428 goto cleanup;
4429
4430 status = psa_hmac_finish_internal( &hmac,
4431 tls12_prf->Ai_with_seed,
4432 hash_length );
4433 if( status != PSA_SUCCESS )
4434 goto cleanup;
4435
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004436 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
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 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004446 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004447 if( status != PSA_SUCCESS )
4448 goto cleanup;
4449
4450 status = psa_hmac_finish_internal( &hmac,
4451 tls12_prf->output_block,
4452 hash_length );
4453 if( status != PSA_SUCCESS )
4454 goto cleanup;
4455
4456cleanup:
4457
4458 cleanup_status = psa_hmac_abort_internal( &hmac );
4459 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4460 status = cleanup_status;
4461
4462 return( status );
4463}
Janos Follath7742fee2019-06-17 12:58:10 +01004464#else
4465static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4466 psa_tls12_prf_key_derivation_t *tls12_prf,
4467 psa_algorithm_t alg )
4468{
4469 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4470 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004471 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4472 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004473
Janos Follath7742fee2019-06-17 12:58:10 +01004474 /* We can't be wanting more output after block 0xff, otherwise
4475 * the capacity check in psa_key_derivation_output_bytes() would have
4476 * prevented this call. It could happen only if the operation
4477 * object was corrupted or if this function is called directly
4478 * inside the library. */
4479 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004480 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004481
4482 /* We need a new block */
4483 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004484 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004485
4486 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4487 *
4488 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4489 *
4490 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4491 * HMAC_hash(secret, A(2) + seed) +
4492 * HMAC_hash(secret, A(3) + seed) + ...
4493 *
4494 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004495 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004496 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004497 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004498 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004499 * is currently extracted as `output_block` and where i is
4500 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004501 */
4502
Janos Follathea29bfb2019-06-19 12:21:20 +01004503 /* Save the hash context before using it, to preserve the hash state with
4504 * only the inner padding in it. We need this, because inner padding depends
4505 * on the key (secret in the RFC's terminology). */
4506 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4507 if( status != PSA_SUCCESS )
4508 goto cleanup;
4509
4510 /* Calculate A(i) where i = tls12_prf->block_number. */
4511 if( tls12_prf->block_number == 1 )
4512 {
4513 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4514 * the variable seed and in this instance means it in the context of the
4515 * P_hash function, where seed = label + seed.) */
4516 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4517 tls12_prf->label, tls12_prf->label_length );
4518 if( status != PSA_SUCCESS )
4519 goto cleanup;
4520 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4521 tls12_prf->seed, tls12_prf->seed_length );
4522 if( status != PSA_SUCCESS )
4523 goto cleanup;
4524 }
4525 else
4526 {
4527 /* A(i) = HMAC_hash(secret, A(i-1)) */
4528 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4529 tls12_prf->Ai, hash_length );
4530 if( status != PSA_SUCCESS )
4531 goto cleanup;
4532 }
4533
4534 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4535 tls12_prf->Ai, hash_length );
4536 if( status != PSA_SUCCESS )
4537 goto cleanup;
4538 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4539 if( status != PSA_SUCCESS )
4540 goto cleanup;
4541
4542 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4543 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4544 tls12_prf->Ai, hash_length );
4545 if( status != PSA_SUCCESS )
4546 goto cleanup;
4547 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4548 tls12_prf->label, tls12_prf->label_length );
4549 if( status != PSA_SUCCESS )
4550 goto cleanup;
4551 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4552 tls12_prf->seed, tls12_prf->seed_length );
4553 if( status != PSA_SUCCESS )
4554 goto cleanup;
4555 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4556 tls12_prf->output_block, hash_length );
4557 if( status != PSA_SUCCESS )
4558 goto cleanup;
4559 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4560 if( status != PSA_SUCCESS )
4561 goto cleanup;
4562
Janos Follath7742fee2019-06-17 12:58:10 +01004563
4564cleanup:
4565
Janos Follathea29bfb2019-06-19 12:21:20 +01004566 cleanup_status = psa_hash_abort( &backup );
4567 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4568 status = cleanup_status;
4569
4570 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004571}
Janos Follath999f6482019-06-11 12:04:10 +01004572#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004573
Janos Follath999f6482019-06-11 12:04:10 +01004574#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004576 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004577static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004578 psa_tls12_prf_key_derivation_t *tls12_prf,
4579 psa_algorithm_t alg,
4580 uint8_t *output,
4581 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004582{
4583 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4584 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4585 psa_status_t status;
4586
4587 while( output_length != 0 )
4588 {
4589 /* Copy what remains of the current block */
4590 uint8_t n = hash_length - tls12_prf->offset_in_block;
4591
4592 /* Check if we have fully processed the current block. */
4593 if( n == 0 )
4594 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004595 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004596 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004597 if( status != PSA_SUCCESS )
4598 return( status );
4599
4600 continue;
4601 }
4602
4603 if( n > output_length )
4604 n = (uint8_t) output_length;
4605 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4606 n );
4607 output += n;
4608 output_length -= n;
4609 tls12_prf->offset_in_block += n;
4610 }
4611
4612 return( PSA_SUCCESS );
4613}
Janos Follath844eb0e2019-06-19 12:10:49 +01004614#else
4615static psa_status_t psa_key_derivation_tls12_prf_read(
4616 psa_tls12_prf_key_derivation_t *tls12_prf,
4617 psa_algorithm_t alg,
4618 uint8_t *output,
4619 size_t output_length )
4620{
4621 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4622 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4623 psa_status_t status;
4624 uint8_t offset, length;
4625
4626 while( output_length != 0 )
4627 {
4628 /* Check if we have fully processed the current block. */
4629 if( tls12_prf->left_in_block == 0 )
4630 {
4631 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4632 alg );
4633 if( status != PSA_SUCCESS )
4634 return( status );
4635
4636 continue;
4637 }
4638
4639 if( tls12_prf->left_in_block > output_length )
4640 length = (uint8_t) output_length;
4641 else
4642 length = tls12_prf->left_in_block;
4643
4644 offset = hash_length - tls12_prf->left_in_block;
4645 memcpy( output, tls12_prf->output_block + offset, length );
4646 output += length;
4647 output_length -= length;
4648 tls12_prf->left_in_block -= length;
4649 }
4650
4651 return( PSA_SUCCESS );
4652}
Janos Follath999f6482019-06-11 12:04:10 +01004653#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004654#endif /* MBEDTLS_MD_C */
4655
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004656psa_status_t psa_key_derivation_output_bytes(
4657 psa_key_derivation_operation_t *operation,
4658 uint8_t *output,
4659 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004660{
4661 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004665 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004667 return PSA_ERROR_BAD_STATE;
4668 }
4669
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004673 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004675 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004676 goto exit;
4677 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004679 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004681 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4683 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004685 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004686 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004689
Gilles Peskinebef7f142018-07-12 17:22:21 +02004690#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004691 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004692 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004693 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004694 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004695 output, output_length );
4696 }
Janos Follathadbec812019-06-14 11:05:39 +01004697 else
Janos Follathadbec812019-06-14 11:05:39 +01004698 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004699 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004700 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004702 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004703 output_length );
4704 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004705 else
4706#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004707 {
4708 return( PSA_ERROR_BAD_STATE );
4709 }
4710
4711exit:
4712 if( status != PSA_SUCCESS )
4713 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004714 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 * This allows us to differentiate between exhausted operations and
4716 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4717 * operations. */
4718 psa_algorithm_t alg = operation->alg;
4719 psa_key_derivation_abort( operation );
4720 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721 memset( output, '!', output_length );
4722 }
4723 return( status );
4724}
4725
Gilles Peskine08542d82018-07-19 17:05:42 +02004726#if defined(MBEDTLS_DES_C)
4727static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4728{
4729 if( data_size >= 8 )
4730 mbedtls_des_key_set_parity( data );
4731 if( data_size >= 16 )
4732 mbedtls_des_key_set_parity( data + 8 );
4733 if( data_size >= 24 )
4734 mbedtls_des_key_set_parity( data + 16 );
4735}
4736#endif /* MBEDTLS_DES_C */
4737
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004738static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004739 psa_key_slot_t *slot,
4740 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004742{
4743 uint8_t *data = NULL;
4744 size_t bytes = PSA_BITS_TO_BYTES( bits );
4745 psa_status_t status;
4746
Gilles Peskine8e338702019-07-30 20:06:31 +02004747 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004748 return( PSA_ERROR_INVALID_ARGUMENT );
4749 if( bits % 8 != 0 )
4750 return( PSA_ERROR_INVALID_ARGUMENT );
4751 data = mbedtls_calloc( 1, bytes );
4752 if( data == NULL )
4753 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4754
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004756 if( status != PSA_SUCCESS )
4757 goto exit;
4758#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004759 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004760 psa_des_set_key_parity( data, bytes );
4761#endif /* MBEDTLS_DES_C */
4762 status = psa_import_key_into_slot( slot, data, bytes );
4763
4764exit:
4765 mbedtls_free( data );
4766 return( status );
4767}
4768
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004769psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004771 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004772{
4773 psa_status_t status;
4774 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004775 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004776 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4778 if( driver != NULL )
4779 {
4780 /* Deriving a key in a secure element is not implemented yet. */
4781 status = PSA_ERROR_NOT_SUPPORTED;
4782 }
4783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 if( status == PSA_SUCCESS )
4785 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004786 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004787 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 }
4790 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004791 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004792 if( status != PSA_SUCCESS )
4793 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004794 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004795 *handle = 0;
4796 }
4797 return( status );
4798}
4799
Gilles Peskineeab56e42018-07-12 17:12:33 +02004800
4801
4802/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004803/* Key derivation */
4804/****************************************************************/
4805
Gilles Peskinea05219c2018-11-16 16:02:56 +01004806#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004807#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004809 * of the HKDF algorithm.
4810 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004811 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004812 * to potentially free embedded data structures and wipe confidential data.
4813 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004814static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004815 const uint8_t *secret,
4816 size_t secret_length,
4817 psa_algorithm_t hash_alg,
4818 const uint8_t *salt,
4819 size_t salt_length,
4820 const uint8_t *label,
4821 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004822{
4823 psa_status_t status;
4824 status = psa_hmac_setup_internal( &hkdf->hmac,
4825 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004826 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004827 if( status != PSA_SUCCESS )
4828 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004829 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004830 if( status != PSA_SUCCESS )
4831 return( status );
4832 status = psa_hmac_finish_internal( &hkdf->hmac,
4833 hkdf->prk,
4834 sizeof( hkdf->prk ) );
4835 if( status != PSA_SUCCESS )
4836 return( status );
4837 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4838 hkdf->block_number = 0;
4839 hkdf->info_length = label_length;
4840 if( label_length != 0 )
4841 {
4842 hkdf->info = mbedtls_calloc( 1, label_length );
4843 if( hkdf->info == NULL )
4844 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4845 memcpy( hkdf->info, label, label_length );
4846 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004847 hkdf->state = HKDF_STATE_KEYED;
4848 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004849 return( PSA_SUCCESS );
4850}
Janos Follath71a4c912019-06-11 09:14:47 +01004851#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004852#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004853
Gilles Peskinea05219c2018-11-16 16:02:56 +01004854#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004855#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004857 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004858 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004859 * to potentially free embedded data structures and wipe confidential data.
4860 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004861static psa_status_t psa_key_derivation_tls12_prf_setup(
4862 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004863 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004864 size_t key_len,
4865 psa_algorithm_t hash_alg,
4866 const uint8_t *salt,
4867 size_t salt_length,
4868 const uint8_t *label,
4869 size_t label_length )
4870{
4871 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004872 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4873 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004874
4875 tls12_prf->key = mbedtls_calloc( 1, key_len );
4876 if( tls12_prf->key == NULL )
4877 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4878 tls12_prf->key_len = key_len;
4879 memcpy( tls12_prf->key, key, key_len );
4880
Hanno Becker580fba12018-11-13 20:50:45 +00004881 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004882 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004883 if( overflow )
4884 return( PSA_ERROR_INVALID_ARGUMENT );
4885
4886 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4887 if( tls12_prf->Ai_with_seed == NULL )
4888 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4889 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4890
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004891 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4892 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004893 if( label_length != 0 )
4894 {
4895 memcpy( tls12_prf->Ai_with_seed + hash_length,
4896 label, label_length );
4897 }
4898
4899 if( salt_length != 0 )
4900 {
4901 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4902 salt, salt_length );
4903 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004904
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004905 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004906 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004907 tls12_prf->block_number = 0;
4908 tls12_prf->offset_in_block = hash_length;
4909
4910 return( PSA_SUCCESS );
4911}
Janos Follath71a4c912019-06-11 09:14:47 +01004912#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004913
Janos Follath71a4c912019-06-11 09:14:47 +01004914#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004916static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4917 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004918 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004919 size_t psk_len,
4920 psa_algorithm_t hash_alg,
4921 const uint8_t *salt,
4922 size_t salt_length,
4923 const uint8_t *label,
4924 size_t label_length )
4925{
4926 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004927 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004928
4929 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4930 return( PSA_ERROR_INVALID_ARGUMENT );
4931
4932 /* Quoting RFC 4279, Section 2:
4933 *
4934 * The premaster secret is formed as follows: if the PSK is N octets
4935 * long, concatenate a uint16 with the value N, N zero octets, a second
4936 * uint16 with the value N, and the PSK itself.
4937 */
4938
4939 pms[0] = ( psk_len >> 8 ) & 0xff;
4940 pms[1] = ( psk_len >> 0 ) & 0xff;
4941 memset( pms + 2, 0, psk_len );
4942 pms[2 + psk_len + 0] = pms[0];
4943 pms[2 + psk_len + 1] = pms[1];
4944 memcpy( pms + 4 + psk_len, psk, psk_len );
4945
Gilles Peskinecbe66502019-05-16 16:59:18 +02004946 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004947 pms, 4 + 2 * psk_len,
4948 hash_alg,
4949 salt, salt_length,
4950 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004951
Gilles Peskine3f108122018-12-07 18:14:53 +01004952 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004953 return( status );
4954}
Janos Follath71a4c912019-06-11 09:14:47 +01004955#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004956#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004957
Janos Follath71a4c912019-06-11 09:14:47 +01004958#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004959/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004960 * to potentially free embedded data structures and wipe confidential data.
4961 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004962static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004964 const uint8_t *secret, size_t secret_length,
4965 psa_algorithm_t alg,
4966 const uint8_t *salt, size_t salt_length,
4967 const uint8_t *label, size_t label_length,
4968 size_t capacity )
4969{
4970 psa_status_t status;
4971 size_t max_capacity;
4972
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 /* Set operation->alg even on failure so that abort knows what to do. */
4974 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004975
4976#if defined(MBEDTLS_MD_C)
4977 if( PSA_ALG_IS_HKDF( alg ) )
4978 {
4979 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4980 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4981 if( hash_size == 0 )
4982 return( PSA_ERROR_NOT_SUPPORTED );
4983 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004985 secret, secret_length,
4986 hash_alg,
4987 salt, salt_length,
4988 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004989 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004990 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4991 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4992 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004993 {
4994 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4995 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4996
4997 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4998 if( hash_alg != PSA_ALG_SHA_256 &&
4999 hash_alg != PSA_ALG_SHA_384 )
5000 {
5001 return( PSA_ERROR_NOT_SUPPORTED );
5002 }
5003
5004 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005005
5006 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5007 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005009 secret, secret_length,
5010 hash_alg, salt, salt_length,
5011 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005012 }
5013 else
5014 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005015 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005017 secret, secret_length,
5018 hash_alg, salt, salt_length,
5019 label, label_length );
5020 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005021 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005022 else
5023#endif
5024 {
5025 return( PSA_ERROR_NOT_SUPPORTED );
5026 }
5027
5028 if( status != PSA_SUCCESS )
5029 return( status );
5030
5031 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005033 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005035 else
5036 return( PSA_ERROR_INVALID_ARGUMENT );
5037
5038 return( PSA_SUCCESS );
5039}
Janos Follath71a4c912019-06-11 09:14:47 +01005040#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005041
Janos Follath71a4c912019-06-11 09:14:47 +01005042#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005044 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005045 psa_algorithm_t alg,
5046 const uint8_t *salt,
5047 size_t salt_length,
5048 const uint8_t *label,
5049 size_t label_length,
5050 size_t capacity )
5051{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005052 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005053 psa_status_t status;
5054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005056 return( PSA_ERROR_BAD_STATE );
5057
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005058 /* Make sure that alg is a key derivation algorithm. This prevents
5059 * key selection algorithms, which psa_key_derivation_internal
5060 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005061 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5062 return( PSA_ERROR_INVALID_ARGUMENT );
5063
Gilles Peskine28f8f302019-07-24 13:30:31 +02005064 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005065 if( status != PSA_SUCCESS )
5066 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005067
Gilles Peskine8e338702019-07-30 20:06:31 +02005068 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005069 return( PSA_ERROR_INVALID_ARGUMENT );
5070
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005072 slot->data.raw.data,
5073 slot->data.raw.bytes,
5074 alg,
5075 salt, salt_length,
5076 label, label_length,
5077 capacity );
5078 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005079 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005080 return( status );
5081}
Janos Follath71a4c912019-06-11 09:14:47 +01005082#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005083
Gilles Peskine969c5d62019-01-16 15:53:06 +01005084static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005086 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087{
Janos Follath5fe19732019-06-20 15:09:30 +01005088 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5089 * initialisers for this union leave some bytes unspecified.) */
5090 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5091
Gilles Peskine969c5d62019-01-16 15:53:06 +01005092 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005093#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005094 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5095 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5096 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005097 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005098 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5100 if( hash_size == 0 )
5101 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005102 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5103 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005104 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105 {
5106 return( PSA_ERROR_NOT_SUPPORTED );
5107 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005109 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005110 }
5111#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005112 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005113 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005114}
5115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005117 psa_algorithm_t alg )
5118{
5119 psa_status_t status;
5120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005122 return( PSA_ERROR_BAD_STATE );
5123
Gilles Peskine6843c292019-01-18 16:44:49 +01005124 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5125 return( PSA_ERROR_INVALID_ARGUMENT );
5126 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 {
5128 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005130 }
5131 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5132 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005134 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135 else
5136 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005137
5138 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005140 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005141}
5142
5143#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005144static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 psa_algorithm_t hash_alg,
5146 psa_key_derivation_step_t step,
5147 const uint8_t *data,
5148 size_t data_length )
5149{
5150 psa_status_t status;
5151 switch( step )
5152 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005153 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005154 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005155 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005156 status = psa_hmac_setup_internal( &hkdf->hmac,
5157 data, data_length,
5158 hash_alg );
5159 if( status != PSA_SUCCESS )
5160 return( status );
5161 hkdf->state = HKDF_STATE_STARTED;
5162 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005163 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164 /* If no salt was provided, use an empty salt. */
5165 if( hkdf->state == HKDF_STATE_INIT )
5166 {
5167 status = psa_hmac_setup_internal( &hkdf->hmac,
5168 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005169 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170 if( status != PSA_SUCCESS )
5171 return( status );
5172 hkdf->state = HKDF_STATE_STARTED;
5173 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005174 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005176 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5177 data, data_length );
5178 if( status != PSA_SUCCESS )
5179 return( status );
5180 status = psa_hmac_finish_internal( &hkdf->hmac,
5181 hkdf->prk,
5182 sizeof( hkdf->prk ) );
5183 if( status != PSA_SUCCESS )
5184 return( status );
5185 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5186 hkdf->block_number = 0;
5187 hkdf->state = HKDF_STATE_KEYED;
5188 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005189 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 if( hkdf->state == HKDF_STATE_OUTPUT )
5191 return( PSA_ERROR_BAD_STATE );
5192 if( hkdf->info_set )
5193 return( PSA_ERROR_BAD_STATE );
5194 hkdf->info_length = data_length;
5195 if( data_length != 0 )
5196 {
5197 hkdf->info = mbedtls_calloc( 1, data_length );
5198 if( hkdf->info == NULL )
5199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5200 memcpy( hkdf->info, data, data_length );
5201 }
5202 hkdf->info_set = 1;
5203 return( PSA_SUCCESS );
5204 default:
5205 return( PSA_ERROR_INVALID_ARGUMENT );
5206 }
5207}
Janos Follathb03233e2019-06-11 15:30:30 +01005208
5209#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5210static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5211 psa_algorithm_t hash_alg,
5212 psa_key_derivation_step_t step,
5213 const uint8_t *data,
5214 size_t data_length )
5215{
5216 (void) prf;
5217 (void) hash_alg;
5218 (void) step;
5219 (void) data;
5220 (void) data_length;
5221
5222 return( PSA_ERROR_INVALID_ARGUMENT );
5223}
Janos Follath6660f0e2019-06-17 08:44:03 +01005224
5225static psa_status_t psa_tls12_prf_psk_to_ms_input(
5226 psa_tls12_prf_key_derivation_t *prf,
5227 psa_algorithm_t hash_alg,
5228 psa_key_derivation_step_t step,
5229 const uint8_t *data,
5230 size_t data_length )
5231{
5232 (void) prf;
5233 (void) hash_alg;
5234 (void) step;
5235 (void) data;
5236 (void) data_length;
5237
5238 return( PSA_ERROR_INVALID_ARGUMENT );
5239}
Janos Follathb03233e2019-06-11 15:30:30 +01005240#else
Janos Follathf08e2652019-06-13 09:05:41 +01005241static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5242 const uint8_t *data,
5243 size_t data_length )
5244{
5245 if( prf->state != TLS12_PRF_STATE_INIT )
5246 return( PSA_ERROR_BAD_STATE );
5247
Janos Follathd6dce9f2019-07-04 09:11:38 +01005248 if( data_length != 0 )
5249 {
5250 prf->seed = mbedtls_calloc( 1, data_length );
5251 if( prf->seed == NULL )
5252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005253
Janos Follathd6dce9f2019-07-04 09:11:38 +01005254 memcpy( prf->seed, data, data_length );
5255 prf->seed_length = data_length;
5256 }
Janos Follathf08e2652019-06-13 09:05:41 +01005257
5258 prf->state = TLS12_PRF_STATE_SEED_SET;
5259
5260 return( PSA_SUCCESS );
5261}
5262
Janos Follath81550542019-06-13 14:26:34 +01005263static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5264 psa_algorithm_t hash_alg,
5265 const uint8_t *data,
5266 size_t data_length )
5267{
5268 psa_status_t status;
5269 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5270 return( PSA_ERROR_BAD_STATE );
5271
5272 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5273 if( status != PSA_SUCCESS )
5274 return( status );
5275
5276 prf->state = TLS12_PRF_STATE_KEY_SET;
5277
5278 return( PSA_SUCCESS );
5279}
5280
Janos Follath6660f0e2019-06-17 08:44:03 +01005281static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5282 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;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005288 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5289 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005290
5291 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5292 return( PSA_ERROR_INVALID_ARGUMENT );
5293
5294 /* Quoting RFC 4279, Section 2:
5295 *
5296 * The premaster secret is formed as follows: if the PSK is N octets
5297 * long, concatenate a uint16 with the value N, N zero octets, a second
5298 * uint16 with the value N, and the PSK itself.
5299 */
5300
Janos Follath40e13932019-06-26 13:22:29 +01005301 *cur++ = ( data_length >> 8 ) & 0xff;
5302 *cur++ = ( data_length >> 0 ) & 0xff;
5303 memset( cur, 0, data_length );
5304 cur += data_length;
5305 *cur++ = pms[0];
5306 *cur++ = pms[1];
5307 memcpy( cur, data, data_length );
5308 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005309
Janos Follath40e13932019-06-26 13:22:29 +01005310 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005311
5312 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5313 return( status );
5314}
5315
Janos Follath63028dd2019-06-13 09:15:47 +01005316static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5317 const uint8_t *data,
5318 size_t data_length )
5319{
5320 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5321 return( PSA_ERROR_BAD_STATE );
5322
Janos Follathd6dce9f2019-07-04 09:11:38 +01005323 if( data_length != 0 )
5324 {
5325 prf->label = mbedtls_calloc( 1, data_length );
5326 if( prf->label == NULL )
5327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005328
Janos Follathd6dce9f2019-07-04 09:11:38 +01005329 memcpy( prf->label, data, data_length );
5330 prf->label_length = data_length;
5331 }
Janos Follath63028dd2019-06-13 09:15:47 +01005332
5333 prf->state = TLS12_PRF_STATE_LABEL_SET;
5334
5335 return( PSA_SUCCESS );
5336}
5337
Janos Follathb03233e2019-06-11 15:30:30 +01005338static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5339 psa_algorithm_t hash_alg,
5340 psa_key_derivation_step_t step,
5341 const uint8_t *data,
5342 size_t data_length )
5343{
Janos Follathb03233e2019-06-11 15:30:30 +01005344 switch( step )
5345 {
Janos Follathf08e2652019-06-13 09:05:41 +01005346 case PSA_KEY_DERIVATION_INPUT_SEED:
5347 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005348 case PSA_KEY_DERIVATION_INPUT_SECRET:
5349 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005350 case PSA_KEY_DERIVATION_INPUT_LABEL:
5351 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005352 default:
5353 return( PSA_ERROR_INVALID_ARGUMENT );
5354 }
5355}
Janos Follath6660f0e2019-06-17 08:44:03 +01005356
5357static psa_status_t psa_tls12_prf_psk_to_ms_input(
5358 psa_tls12_prf_key_derivation_t *prf,
5359 psa_algorithm_t hash_alg,
5360 psa_key_derivation_step_t step,
5361 const uint8_t *data,
5362 size_t data_length )
5363{
5364 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005365 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005366 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5367 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005368 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005369
5370 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5371}
Janos Follathb03233e2019-06-11 15:30:30 +01005372#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005373#endif /* MBEDTLS_MD_C */
5374
Janos Follathb80a94e2019-06-12 15:54:46 +01005375static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005377 psa_key_derivation_step_t step,
5378 const uint8_t *data,
5379 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005380{
5381 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005383
5384#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005385 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005386 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005388 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005389 step, data, data_length );
5390 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005391 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005392#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005393#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005394 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005395 {
Janos Follathb03233e2019-06-11 15:30:30 +01005396 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5397 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5398 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005399 }
5400 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5401 {
5402 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5403 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5404 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005405 }
5406 else
5407#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005408 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005410 return( PSA_ERROR_BAD_STATE );
5411 }
5412
5413 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005415 return( status );
5416}
5417
Janos Follath51f4a0f2019-06-14 11:35:55 +01005418psa_status_t psa_key_derivation_input_bytes(
5419 psa_key_derivation_operation_t *operation,
5420 psa_key_derivation_step_t step,
5421 const uint8_t *data,
5422 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005423{
Janos Follathc5621512019-06-14 11:27:57 +01005424 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5425 return( PSA_ERROR_INVALID_ARGUMENT );
5426
5427 return( psa_key_derivation_input_internal( operation, step,
5428 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005429}
5430
Janos Follath51f4a0f2019-06-14 11:35:55 +01005431psa_status_t psa_key_derivation_input_key(
5432 psa_key_derivation_operation_t *operation,
5433 psa_key_derivation_step_t step,
5434 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005435{
5436 psa_key_slot_t *slot;
5437 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005438 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005439 PSA_KEY_USAGE_DERIVE,
5440 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 if( status != PSA_SUCCESS )
5442 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005443 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005444 return( PSA_ERROR_INVALID_ARGUMENT );
5445 /* Don't allow a key to be used as an input that is usually public.
5446 * This is debatable. It's ok from a cryptographic perspective to
5447 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005448 * the material should be dedicated to a particular input step,
5449 * otherwise this may allow the key to be used in an unintended way
5450 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005451 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005452 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005453 return( psa_key_derivation_input_internal( operation,
5454 step,
5455 slot->data.raw.data,
5456 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005457}
5458
Gilles Peskineea0fb492018-07-12 17:17:20 +02005459
5460
5461/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005462/* Key agreement */
5463/****************************************************************/
5464
Gilles Peskinea05219c2018-11-16 16:02:56 +01005465#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005466static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5467 size_t peer_key_length,
5468 const mbedtls_ecp_keypair *our_key,
5469 uint8_t *shared_secret,
5470 size_t shared_secret_size,
5471 size_t *shared_secret_length )
5472{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005473 mbedtls_ecp_keypair *their_key = NULL;
5474 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005475 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005476 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005477
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005478 status = psa_import_ec_public_key(
5479 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5480 peer_key, peer_key_length,
5481 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005482 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005483 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005484
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005485 status = mbedtls_to_psa_error(
5486 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5487 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005488 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005489 status = mbedtls_to_psa_error(
5490 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5491 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005492 goto exit;
5493
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005494 status = mbedtls_to_psa_error(
5495 mbedtls_ecdh_calc_secret( &ecdh,
5496 shared_secret_length,
5497 shared_secret, shared_secret_size,
5498 mbedtls_ctr_drbg_random,
5499 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005500
5501exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005502 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005503 mbedtls_ecp_keypair_free( their_key );
5504 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005505 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005506}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005507#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005508
Gilles Peskine01d718c2018-09-18 12:01:02 +02005509#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5510
Gilles Peskine0216fe12019-04-11 21:23:21 +02005511static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5512 psa_key_slot_t *private_key,
5513 const uint8_t *peer_key,
5514 size_t peer_key_length,
5515 uint8_t *shared_secret,
5516 size_t shared_secret_size,
5517 size_t *shared_secret_length )
5518{
5519 switch( alg )
5520 {
5521#if defined(MBEDTLS_ECDH_C)
5522 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005523 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005524 return( PSA_ERROR_INVALID_ARGUMENT );
5525 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5526 private_key->data.ecp,
5527 shared_secret, shared_secret_size,
5528 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005529#endif /* MBEDTLS_ECDH_C */
5530 default:
5531 (void) private_key;
5532 (void) peer_key;
5533 (void) peer_key_length;
5534 (void) shared_secret;
5535 (void) shared_secret_size;
5536 (void) shared_secret_length;
5537 return( PSA_ERROR_NOT_SUPPORTED );
5538 }
5539}
5540
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005541/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005542 * to potentially free embedded data structures and wipe confidential data.
5543 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005545 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005546 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005547 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005548 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005549{
5550 psa_status_t status;
5551 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5552 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005553 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005554
5555 /* Step 1: run the secret agreement algorithm to generate the shared
5556 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005557 status = psa_key_agreement_raw_internal( ka_alg,
5558 private_key,
5559 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005560 shared_secret,
5561 sizeof( shared_secret ),
5562 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005563 if( status != PSA_SUCCESS )
5564 goto exit;
5565
5566 /* Step 2: set up the key derivation to generate key material from
5567 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005568 status = psa_key_derivation_input_internal( operation, step,
5569 shared_secret,
5570 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005571
Gilles Peskine01d718c2018-09-18 12:01:02 +02005572exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005573 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005574 return( status );
5575}
5576
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005578 psa_key_derivation_step_t step,
5579 psa_key_handle_t private_key,
5580 const uint8_t *peer_key,
5581 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005582{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005583 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005584 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005586 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005587 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005588 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005589 if( status != PSA_SUCCESS )
5590 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005592 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005593 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005594 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005595 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005596 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005597}
5598
Gilles Peskinebe697d82019-05-16 18:00:41 +02005599psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5600 psa_key_handle_t private_key,
5601 const uint8_t *peer_key,
5602 size_t peer_key_length,
5603 uint8_t *output,
5604 size_t output_size,
5605 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005606{
5607 psa_key_slot_t *slot;
5608 psa_status_t status;
5609
5610 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5611 {
5612 status = PSA_ERROR_INVALID_ARGUMENT;
5613 goto exit;
5614 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005615 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005616 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005617 if( status != PSA_SUCCESS )
5618 goto exit;
5619
5620 status = psa_key_agreement_raw_internal( alg, slot,
5621 peer_key, peer_key_length,
5622 output, output_size,
5623 output_length );
5624
5625exit:
5626 if( status != PSA_SUCCESS )
5627 {
5628 /* If an error happens and is not handled properly, the output
5629 * may be used as a key to protect sensitive data. Arrange for such
5630 * a key to be random, which is likely to result in decryption or
5631 * verification errors. This is better than filling the buffer with
5632 * some constant data such as zeros, which would result in the data
5633 * being protected with a reproducible, easily knowable key.
5634 */
5635 psa_generate_random( output, output_size );
5636 *output_length = output_size;
5637 }
5638 return( status );
5639}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005640
5641
5642/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005643/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005644/****************************************************************/
5645
5646psa_status_t psa_generate_random( uint8_t *output,
5647 size_t output_size )
5648{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005649 int ret;
5650 GUARD_MODULE_INITIALIZED;
5651
5652 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005653 return( mbedtls_to_psa_error( ret ) );
5654}
5655
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005656#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5657#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005658
Gilles Peskine7228da22019-07-15 11:06:15 +02005659psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005660 size_t seed_size )
5661{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005662 if( global_data.initialized )
5663 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005664
5665 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5666 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5667 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5668 return( PSA_ERROR_INVALID_ARGUMENT );
5669
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005670 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005671}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005672#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005673
Gilles Peskinee56e8782019-04-26 17:34:02 +02005674#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5675static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5676 size_t domain_parameters_size,
5677 int *exponent )
5678{
5679 size_t i;
5680 uint32_t acc = 0;
5681
5682 if( domain_parameters_size == 0 )
5683 {
5684 *exponent = 65537;
5685 return( PSA_SUCCESS );
5686 }
5687
5688 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5689 * support values that fit in a 32-bit integer, which is larger than
5690 * int on just about every platform anyway. */
5691 if( domain_parameters_size > sizeof( acc ) )
5692 return( PSA_ERROR_NOT_SUPPORTED );
5693 for( i = 0; i < domain_parameters_size; i++ )
5694 acc = ( acc << 8 ) | domain_parameters[i];
5695 if( acc > INT_MAX )
5696 return( PSA_ERROR_NOT_SUPPORTED );
5697 *exponent = acc;
5698 return( PSA_SUCCESS );
5699}
5700#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5701
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005702static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005703 psa_key_slot_t *slot, size_t bits,
5704 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005705{
Gilles Peskine8e338702019-07-30 20:06:31 +02005706 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005707
Gilles Peskinee56e8782019-04-26 17:34:02 +02005708 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005709 return( PSA_ERROR_INVALID_ARGUMENT );
5710
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005711 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005712 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005713 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005714 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005715 if( status != PSA_SUCCESS )
5716 return( status );
5717 status = psa_generate_random( slot->data.raw.data,
5718 slot->data.raw.bytes );
5719 if( status != PSA_SUCCESS )
5720 {
5721 mbedtls_free( slot->data.raw.data );
5722 return( status );
5723 }
5724#if defined(MBEDTLS_DES_C)
5725 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005726 psa_des_set_key_parity( slot->data.raw.data,
5727 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005728#endif /* MBEDTLS_DES_C */
5729 }
5730 else
5731
5732#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005733 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005734 {
5735 mbedtls_rsa_context *rsa;
5736 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005737 int exponent;
5738 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005739 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5740 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005741 /* Accept only byte-aligned keys, for the same reasons as
5742 * in psa_import_rsa_key(). */
5743 if( bits % 8 != 0 )
5744 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005745 status = psa_read_rsa_exponent( domain_parameters,
5746 domain_parameters_size,
5747 &exponent );
5748 if( status != PSA_SUCCESS )
5749 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005750 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5751 if( rsa == NULL )
5752 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5753 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5754 ret = mbedtls_rsa_gen_key( rsa,
5755 mbedtls_ctr_drbg_random,
5756 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005757 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005758 exponent );
5759 if( ret != 0 )
5760 {
5761 mbedtls_rsa_free( rsa );
5762 mbedtls_free( rsa );
5763 return( mbedtls_to_psa_error( ret ) );
5764 }
5765 slot->data.rsa = rsa;
5766 }
5767 else
5768#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5769
5770#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005771 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005772 {
5773 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5774 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5775 const mbedtls_ecp_curve_info *curve_info =
5776 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5777 mbedtls_ecp_keypair *ecp;
5778 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005779 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005780 return( PSA_ERROR_NOT_SUPPORTED );
5781 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5782 return( PSA_ERROR_NOT_SUPPORTED );
5783 if( curve_info->bit_size != bits )
5784 return( PSA_ERROR_INVALID_ARGUMENT );
5785 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5786 if( ecp == NULL )
5787 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5788 mbedtls_ecp_keypair_init( ecp );
5789 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5790 mbedtls_ctr_drbg_random,
5791 &global_data.ctr_drbg );
5792 if( ret != 0 )
5793 {
5794 mbedtls_ecp_keypair_free( ecp );
5795 mbedtls_free( ecp );
5796 return( mbedtls_to_psa_error( ret ) );
5797 }
5798 slot->data.ecp = ecp;
5799 }
5800 else
5801#endif /* MBEDTLS_ECP_C */
5802
5803 return( PSA_ERROR_NOT_SUPPORTED );
5804
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005805 return( PSA_SUCCESS );
5806}
5807
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005808psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005809 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005810{
5811 psa_status_t status;
5812 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005813 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005814 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005815#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5816 if( driver != NULL )
5817 {
5818 /* Generating a key in a secure element is not implemented yet. */
5819 status = PSA_ERROR_NOT_SUPPORTED;
5820 }
5821#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005822 if( status == PSA_SUCCESS )
5823 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005824 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005825 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005826 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005827 }
5828 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005829 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005830 if( status != PSA_SUCCESS )
5831 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005832 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005833 *handle = 0;
5834 }
5835 return( status );
5836}
5837
5838
Gilles Peskine05d69892018-06-19 22:00:52 +02005839
5840/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005841/* Module setup */
5842/****************************************************************/
5843
Gilles Peskine5e769522018-11-20 21:59:56 +01005844psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5845 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5846 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5847{
5848 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5849 return( PSA_ERROR_BAD_STATE );
5850 global_data.entropy_init = entropy_init;
5851 global_data.entropy_free = entropy_free;
5852 return( PSA_SUCCESS );
5853}
5854
Gilles Peskinee59236f2018-01-27 23:32:46 +01005855void mbedtls_psa_crypto_free( void )
5856{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005857 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005858 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5859 {
5860 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005861 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005862 }
5863 /* Wipe all remaining data, including configuration.
5864 * In particular, this sets all state indicator to the value
5865 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005866 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005867#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005868 /* Unregister all secure element drivers, so that we restart from
5869 * a pristine state. */
5870 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005871#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005872}
5873
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005874#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5875/** Recover a transaction that was interrupted by a power failure.
5876 *
5877 * This function is called during initialization, before psa_crypto_init()
5878 * returns. If this function returns a failure status, the initialization
5879 * fails.
5880 */
5881static psa_status_t psa_crypto_recover_transaction(
5882 const psa_crypto_transaction_t *transaction )
5883{
5884 switch( transaction->unknown.type )
5885 {
5886 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5887 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5888 /* TOnogrepDO - fall through to the failure case until this
5889 * is implemented */
5890 default:
5891 /* We found an unsupported transaction in the storage.
5892 * We don't know what state the storage is in. Give up. */
5893 return( PSA_ERROR_STORAGE_FAILURE );
5894 }
5895}
5896#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5897
Gilles Peskinee59236f2018-01-27 23:32:46 +01005898psa_status_t psa_crypto_init( void )
5899{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005900 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005901 const unsigned char drbg_seed[] = "PSA";
5902
Gilles Peskinec6b69072018-11-20 21:42:52 +01005903 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005904 if( global_data.initialized != 0 )
5905 return( PSA_SUCCESS );
5906
Gilles Peskine5e769522018-11-20 21:59:56 +01005907 /* Set default configuration if
5908 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5909 if( global_data.entropy_init == NULL )
5910 global_data.entropy_init = mbedtls_entropy_init;
5911 if( global_data.entropy_free == NULL )
5912 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005913
Gilles Peskinec6b69072018-11-20 21:42:52 +01005914 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005915 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005916 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005917 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005918 status = mbedtls_to_psa_error(
5919 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5920 mbedtls_entropy_func,
5921 &global_data.entropy,
5922 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5923 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005924 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005925 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005926
Gilles Peskine66fb1262018-12-10 16:29:04 +01005927 status = psa_initialize_key_slots( );
5928 if( status != PSA_SUCCESS )
5929 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005930
Gilles Peskine4b734222019-07-24 15:56:31 +02005931#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005932 status = psa_crypto_load_transaction( );
5933 if( status == PSA_SUCCESS )
5934 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005935 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5936 if( status != PSA_SUCCESS )
5937 goto exit;
5938 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005939 }
5940 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5941 {
5942 /* There's no transaction to complete. It's all good. */
5943 status = PSA_SUCCESS;
5944 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005945#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005946
Gilles Peskinec6b69072018-11-20 21:42:52 +01005947 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005948 global_data.initialized = 1;
5949
Gilles Peskinee59236f2018-01-27 23:32:46 +01005950exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005951 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005952 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005953 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005954}
5955
5956#endif /* MBEDTLS_PSA_CRYPTO_C */