blob: 6041732fd1fbf83e9d482b53c7bf6e667bc9b1de [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454 if( bits == 0 )
455 {
456 raw->bytes = 0;
457 raw->data = NULL;
458 return( PSA_SUCCESS );
459 }
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_MD_C)
462 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200463#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
502 /* Allocate memory for the key */
503 raw->bytes = PSA_BITS_TO_BYTES( bits );
504 raw->data = mbedtls_calloc( 1, raw->bytes );
505 if( raw->data == NULL )
506 {
507 raw->bytes = 0;
508 return( PSA_ERROR_INSUFFICIENT_MEMORY );
509 }
510 return( PSA_SUCCESS );
511}
512
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100514/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
515 * that are not a multiple of 8) well. For example, there is only
516 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
517 * way to return the exact bit size of a key.
518 * To keep things simple, reject non-byte-aligned key sizes. */
519static psa_status_t psa_check_rsa_key_byte_aligned(
520 const mbedtls_rsa_context *rsa )
521{
522 mbedtls_mpi n;
523 psa_status_t status;
524 mbedtls_mpi_init( &n );
525 status = mbedtls_to_psa_error(
526 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
527 if( status == PSA_SUCCESS )
528 {
529 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
530 status = PSA_ERROR_NOT_SUPPORTED;
531 }
532 mbedtls_mpi_free( &n );
533 return( status );
534}
535
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000536static psa_status_t psa_import_rsa_key( psa_key_type_t type,
537 const uint8_t *data,
538 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200539 mbedtls_rsa_context **p_rsa )
540{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 psa_status_t status;
542 mbedtls_pk_context pk;
543 mbedtls_rsa_context *rsa;
544 size_t bits;
545
546 mbedtls_pk_init( &pk );
547
548 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200549 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000550 status = mbedtls_to_psa_error(
551 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000553 status = mbedtls_to_psa_error(
554 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
555 if( status != PSA_SUCCESS )
556 goto exit;
557
558 /* We have something that the pkparse module recognizes. If it is a
559 * valid RSA key, store it. */
560 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000565
566 rsa = mbedtls_pk_rsa( pk );
567 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
568 * supports non-byte-aligned key sizes, but not well. For example,
569 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
570 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
572 {
573 status = PSA_ERROR_NOT_SUPPORTED;
574 goto exit;
575 }
576 status = psa_check_rsa_key_byte_aligned( rsa );
577
578exit:
579 /* Free the content of the pk object only on error. */
580 if( status != PSA_SUCCESS )
581 {
582 mbedtls_pk_free( &pk );
583 return( status );
584 }
585
586 /* On success, store the content of the object in the RSA context. */
587 *p_rsa = rsa;
588
589 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200590}
591#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000593#if defined(MBEDTLS_ECP_C)
594
595/* Import a public key given as the uncompressed representation defined by SEC1
596 * 2.3.3 as the content of an ECPoint. */
597static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
598 const uint8_t *data,
599 size_t data_length,
600 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000603 mbedtls_ecp_keypair *ecp = NULL;
604 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
605
606 *p_ecp = NULL;
607 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
608 if( ecp == NULL )
609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
610 mbedtls_ecp_keypair_init( ecp );
611
612 /* Load the group. */
613 status = mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617 /* Load the public value. */
618 status = mbedtls_to_psa_error(
619 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
620 data, data_length ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
624 /* Check that the point is on the curve. */
625 status = mbedtls_to_psa_error(
626 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000641#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642
Gilles Peskinef76aa772018-10-29 19:24:33 +0100643#if defined(MBEDTLS_ECP_C)
644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
653 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
654
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200655 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
656 return( PSA_ERROR_INVALID_ARGUMENT );
657
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 *p_ecp = NULL;
659 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
660 if( ecp == NULL )
661 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000662 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663
664 /* Load the group. */
665 status = mbedtls_to_psa_error(
666 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Load the secret value. */
670 status = mbedtls_to_psa_error(
671 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Validate the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000772 {
773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000777 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 else
779#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Gilles Peskineb46bef22019-07-30 21:32:04 +0200792
793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000800 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100801}
802
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100815 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
958 {
959 /* No key material to clean. */
960 }
961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997/** Completely wipe a slot in memory, including its policy.
998 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100999psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000{
1001 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001002 /* Multipart operations may still be using the key. This is safe
1003 * because all multipart operation objects are independent from
1004 * the key slot: if they need to access the key after the setup
1005 * phase, they have a copy of the key. Note that this means that
1006 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007 /* At this point, key material and other type-specific content has
1008 * been wiped. Clear remaining metadata. We can call memset and not
1009 * zeroize because the metadata is not particularly sensitive. */
1010 memset( slot, 0, sizeof( *slot ) );
1011 return( status );
1012}
1013
Gilles Peskinec5487a82018-12-03 18:08:14 +01001014psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001016 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001017 psa_status_t status = PSA_SUCCESS;
1018 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1020 psa_se_drv_table_entry_t *driver;
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskinec5487a82018-12-03 18:08:14 +01001023 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001024 if( status != PSA_SUCCESS )
1025 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001026
1027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001030 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001031 /* For a key in a secure element, we need to do three things:
1032 * remove the key file in internal storage, destroy the
1033 * key inside the secure element, and update the driver's
1034 * persistent data. Start a transaction that will encompass these
1035 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 status = psa_crypto_save_transaction( );
1041 if( status != PSA_SUCCESS )
1042 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001043 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 /* TOnogrepDO: destroy what can be destroyed anyway */
1045 return( status );
1046 }
1047
Gilles Peskine354f7672019-07-12 23:46:38 +02001048 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001050#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1051
Darryl Greend49a4992018-06-18 17:27:26 +01001052#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001053 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001054 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001055 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001056 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001057 }
1058#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001059
Gilles Peskinefc762652019-07-22 19:30:34 +02001060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1061 if( driver != NULL )
1062 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001063 psa_status_t status2;
1064 status = psa_save_se_persistent_data( driver );
1065 status2 = psa_crypto_stop_transaction( );
1066 if( status == PSA_SUCCESS )
1067 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001068 if( status != PSA_SUCCESS )
1069 {
1070 /* TOnogrepDO: destroy what can be destroyed anyway */
1071 return( status );
1072 }
1073 }
1074#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1075
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001076 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001077 if( status != PSA_SUCCESS )
1078 return( status );
1079 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080}
1081
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001082void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1083{
Gilles Peskineb699f072019-04-26 16:06:02 +02001084 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001085 memset( attributes, 0, sizeof( *attributes ) );
1086}
1087
Gilles Peskineb699f072019-04-26 16:06:02 +02001088psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1089 psa_key_type_t type,
1090 const uint8_t *data,
1091 size_t data_length )
1092{
1093 uint8_t *copy = NULL;
1094
1095 if( data_length != 0 )
1096 {
1097 copy = mbedtls_calloc( 1, data_length );
1098 if( copy == NULL )
1099 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1100 memcpy( copy, data, data_length );
1101 }
1102 /* After this point, this function is guaranteed to succeed, so it
1103 * can start modifying `*attributes`. */
1104
1105 if( attributes->domain_parameters != NULL )
1106 {
1107 mbedtls_free( attributes->domain_parameters );
1108 attributes->domain_parameters = NULL;
1109 attributes->domain_parameters_size = 0;
1110 }
1111
1112 attributes->domain_parameters = copy;
1113 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001114 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001115 return( PSA_SUCCESS );
1116}
1117
1118psa_status_t psa_get_key_domain_parameters(
1119 const psa_key_attributes_t *attributes,
1120 uint8_t *data, size_t data_size, size_t *data_length )
1121{
1122 if( attributes->domain_parameters_size > data_size )
1123 return( PSA_ERROR_BUFFER_TOO_SMALL );
1124 *data_length = attributes->domain_parameters_size;
1125 if( attributes->domain_parameters_size != 0 )
1126 memcpy( data, attributes->domain_parameters,
1127 attributes->domain_parameters_size );
1128 return( PSA_SUCCESS );
1129}
1130
1131#if defined(MBEDTLS_RSA_C)
1132static psa_status_t psa_get_rsa_public_exponent(
1133 const mbedtls_rsa_context *rsa,
1134 psa_key_attributes_t *attributes )
1135{
1136 mbedtls_mpi mpi;
1137 int ret;
1138 uint8_t *buffer = NULL;
1139 size_t buflen;
1140 mbedtls_mpi_init( &mpi );
1141
1142 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1143 if( ret != 0 )
1144 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001145 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1146 {
1147 /* It's the default value, which is reported as an empty string,
1148 * so there's nothing to do. */
1149 goto exit;
1150 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001151
1152 buflen = mbedtls_mpi_size( &mpi );
1153 buffer = mbedtls_calloc( 1, buflen );
1154 if( buffer == NULL )
1155 {
1156 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1157 goto exit;
1158 }
1159 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1160 if( ret != 0 )
1161 goto exit;
1162 attributes->domain_parameters = buffer;
1163 attributes->domain_parameters_size = buflen;
1164
1165exit:
1166 mbedtls_mpi_free( &mpi );
1167 if( ret != 0 )
1168 mbedtls_free( buffer );
1169 return( mbedtls_to_psa_error( ret ) );
1170}
1171#endif /* MBEDTLS_RSA_C */
1172
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001173/** Retrieve all the publicly-accessible attributes of a key.
1174 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001175psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1176 psa_key_attributes_t *attributes )
1177{
1178 psa_key_slot_t *slot;
1179 psa_status_t status;
1180
1181 psa_reset_key_attributes( attributes );
1182
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001183 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184 if( status != PSA_SUCCESS )
1185 return( status );
1186
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001187 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001188 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1189 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1190
1191#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1192 if( psa_key_slot_is_external( slot ) )
1193 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001195
Gilles Peskine8e338702019-07-30 20:06:31 +02001196 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001197 {
1198#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001199 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001200 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001201#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1202 /* TOnogrepDO: reporting the public exponent for opaque keys
1203 * is not yet implemented. */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001204 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001205 break;
1206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1208 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001209#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 default:
1211 /* Nothing else to do. */
1212 break;
1213 }
1214
1215 if( status != PSA_SUCCESS )
1216 psa_reset_key_attributes( attributes );
1217 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001218}
1219
Gilles Peskinec8000c02019-08-02 20:15:51 +02001220#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1221psa_status_t psa_get_key_slot_number(
1222 const psa_key_attributes_t *attributes,
1223 psa_key_slot_number_t *slot_number )
1224{
1225 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1226 {
1227 *slot_number = attributes->slot_number;
1228 return( PSA_SUCCESS );
1229 }
1230 else
1231 return( PSA_ERROR_INVALID_ARGUMENT );
1232}
1233#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1234
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001235#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001236static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1237 unsigned char *buf, size_t size )
1238{
1239 int ret;
1240 unsigned char *c;
1241 size_t len = 0;
1242
1243 c = buf + size;
1244
1245 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1246
1247 return( (int) len );
1248}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001249#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001250
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001251static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1252 uint8_t *data,
1253 size_t data_size,
1254 size_t *data_length,
1255 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256{
Gilles Peskine5d309672019-07-12 23:47:28 +02001257#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1258 const psa_drv_se_t *drv;
1259 psa_drv_se_context_t *drv_context;
1260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1261
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001262 *data_length = 0;
1263
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001265 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001266
Gilles Peskine5d309672019-07-12 23:47:28 +02001267#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001268 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001269 {
1270 psa_drv_se_export_key_t method;
1271 if( drv->key_management == NULL )
1272 return( PSA_ERROR_NOT_SUPPORTED );
1273 method = ( export_public_key ?
1274 drv->key_management->p_export_public :
1275 drv->key_management->p_export );
1276 if( method == NULL )
1277 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001278 return( method( drv_context,
1279 slot->data.se.slot_number,
1280 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001281 }
1282#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1283
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 {
1286 if( slot->data.raw.bytes > data_size )
1287 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001288 if( data_size != 0 )
1289 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001290 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001291 memset( data + slot->data.raw.bytes, 0,
1292 data_size - slot->data.raw.bytes );
1293 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294 *data_length = slot->data.raw.bytes;
1295 return( PSA_SUCCESS );
1296 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001297#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001299 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001300 psa_status_t status;
1301
Gilles Peskineb46bef22019-07-30 21:32:04 +02001302 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001303 if( bytes > data_size )
1304 return( PSA_ERROR_BUFFER_TOO_SMALL );
1305 status = mbedtls_to_psa_error(
1306 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1307 if( status != PSA_SUCCESS )
1308 return( status );
1309 memset( data + bytes, 0, data_size - bytes );
1310 *data_length = bytes;
1311 return( PSA_SUCCESS );
1312 }
1313#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314 else
Moran Peker17e36e12018-05-02 12:55:20 +03001315 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001316#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001317 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1318 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001319 {
Moran Pekera998bc62018-04-16 18:16:20 +03001320 mbedtls_pk_context pk;
1321 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001323 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001324#if defined(MBEDTLS_RSA_C)
1325 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001326 pk.pk_info = &mbedtls_rsa_info;
1327 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001328#else
1329 return( PSA_ERROR_NOT_SUPPORTED );
1330#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001331 }
1332 else
1333 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001334#if defined(MBEDTLS_ECP_C)
1335 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001336 pk.pk_info = &mbedtls_eckey_info;
1337 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001338#else
1339 return( PSA_ERROR_NOT_SUPPORTED );
1340#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001341 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001342 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001343 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001344 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001345 }
Moran Peker17e36e12018-05-02 12:55:20 +03001346 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001347 {
Moran Peker17e36e12018-05-02 12:55:20 +03001348 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001349 }
Moran Peker60364322018-04-29 11:34:58 +03001350 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001351 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001352 /* If data_size is 0 then data may be NULL and then the
1353 * call to memset would have undefined behavior. */
1354 if( data_size != 0 )
1355 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001356 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001357 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001358 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1359 * Move the data to the beginning and erase remaining data
1360 * at the original location. */
1361 if( 2 * (size_t) ret <= data_size )
1362 {
1363 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001364 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001365 }
1366 else if( (size_t) ret < data_size )
1367 {
1368 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001369 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001370 }
Moran Pekera998bc62018-04-16 18:16:20 +03001371 *data_length = ret;
1372 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001373 }
1374 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001375#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001376 {
1377 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001378 it is valid for a special-purpose implementation to omit
1379 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001380 return( PSA_ERROR_NOT_SUPPORTED );
1381 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382 }
1383}
1384
Gilles Peskinec5487a82018-12-03 18:08:14 +01001385psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001386 uint8_t *data,
1387 size_t data_size,
1388 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001389{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001390 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001391 psa_status_t status;
1392
1393 /* Set the key to empty now, so that even when there are errors, we always
1394 * set data_length to a value between 0 and data_size. On error, setting
1395 * the key to empty is a good choice because an empty key representation is
1396 * unlikely to be accepted anywhere. */
1397 *data_length = 0;
1398
1399 /* Export requires the EXPORT flag. There is an exception for public keys,
1400 * which don't require any flag, but psa_get_key_from_slot takes
1401 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001402 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001403 if( status != PSA_SUCCESS )
1404 return( status );
1405 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001406 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001408
Gilles Peskinec5487a82018-12-03 18:08:14 +01001409psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 uint8_t *data,
1411 size_t data_size,
1412 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001413{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 psa_status_t status;
1416
1417 /* Set the key to empty now, so that even when there are errors, we always
1418 * set data_length to a value between 0 and data_size. On error, setting
1419 * the key to empty is a good choice because an empty key representation is
1420 * unlikely to be accepted anywhere. */
1421 *data_length = 0;
1422
1423 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001424 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001425 if( status != PSA_SUCCESS )
1426 return( status );
1427 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001428 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001429}
1430
Gilles Peskine91e8c332019-08-02 19:19:39 +02001431#if defined(static_assert)
1432static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1433 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001434static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001435 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001436static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001437 "One or more key attribute flag is listed as both internal-only and external-only" );
1438#endif
1439
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001440/** Validate that a key policy is internally well-formed.
1441 *
1442 * This function only rejects invalid policies. It does not validate the
1443 * consistency of the policy with respect to other attributes of the key
1444 * such as the key type.
1445 */
1446static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001447{
1448 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001449 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001450 PSA_KEY_USAGE_ENCRYPT |
1451 PSA_KEY_USAGE_DECRYPT |
1452 PSA_KEY_USAGE_SIGN |
1453 PSA_KEY_USAGE_VERIFY |
1454 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1455 return( PSA_ERROR_INVALID_ARGUMENT );
1456
Gilles Peskine4747d192019-04-17 15:05:45 +02001457 return( PSA_SUCCESS );
1458}
1459
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001460/** Validate the internal consistency of key attributes.
1461 *
1462 * This function only rejects invalid attribute values. If does not
1463 * validate the consistency of the attributes with any key data that may
1464 * be involved in the creation of the key.
1465 *
1466 * Call this function early in the key creation process.
1467 *
1468 * \param[in] attributes Key attributes for the new key.
1469 * \param[out] p_drv On any return, the driver for the key, if any.
1470 * NULL for a transparent key.
1471 *
1472 */
1473static psa_status_t psa_validate_key_attributes(
1474 const psa_key_attributes_t *attributes,
1475 psa_se_drv_table_entry_t **p_drv )
1476{
1477 psa_status_t status;
1478
1479 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
1480 {
1481 status = psa_validate_persistent_key_parameters(
1482 attributes->core.lifetime, attributes->core.id,
1483 p_drv, 1 );
1484 if( status != PSA_SUCCESS )
1485 return( status );
1486 }
1487
1488 status = psa_validate_key_policy( &attributes->core.policy );
1489 if( status != PSA_SUCCESS )
1490 return( status );
1491
1492 /* Refuse to create overly large keys.
1493 * Note that this doesn't trigger on import if the attributes don't
1494 * explicitly specify a size (so psa_get_key_bits returns 0), so
1495 * psa_import_key() needs its own checks. */
1496 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1497 return( PSA_ERROR_NOT_SUPPORTED );
1498
Gilles Peskine91e8c332019-08-02 19:19:39 +02001499 /* Reject invalid flags. These should not be reachable through the API. */
1500 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1501 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1502 return( PSA_ERROR_INVALID_ARGUMENT );
1503
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001504 return( PSA_SUCCESS );
1505}
1506
Gilles Peskine4747d192019-04-17 15:05:45 +02001507/** Prepare a key slot to receive key material.
1508 *
1509 * This function allocates a key slot and sets its metadata.
1510 *
1511 * If this function fails, call psa_fail_key_creation().
1512 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001513 * This function is intended to be used as follows:
1514 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1515 * it with the specified attributes, and assign it a handle.
1516 * -# Populate the slot with the key material.
1517 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1518 * In case of failure at any step, stop the sequence and call
1519 * psa_fail_key_creation().
1520 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001521 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001522 * \param[in] attributes Key attributes for the new key.
1523 * \param[out] handle On success, a handle for the allocated slot.
1524 * \param[out] p_slot On success, a pointer to the prepared slot.
1525 * \param[out] p_drv On any return, the driver for the key, if any.
1526 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001527 *
1528 * \retval #PSA_SUCCESS
1529 * The key slot is ready to receive key material.
1530 * \return If this function fails, the key slot is an invalid state.
1531 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001532 */
1533static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001534 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 const psa_key_attributes_t *attributes,
1536 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001538 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001539{
1540 psa_status_t status;
1541 psa_key_slot_t *slot;
1542
Gilles Peskinedf179142019-07-15 22:02:14 +02001543 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001544 *p_drv = NULL;
1545
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001546 status = psa_validate_key_attributes( attributes, p_drv );
1547 if( status != PSA_SUCCESS )
1548 return( status );
1549
Gilles Peskineedbed562019-08-07 18:19:59 +02001550 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001551 if( status != PSA_SUCCESS )
1552 return( status );
1553 slot = *p_slot;
1554
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001555 /* We're storing the declared bit-size of the key. It's up to each
1556 * creation mechanism to verify that this information is correct.
1557 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001558 * an input (generate, device) but not for those where the bit-size
1559 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001560
1561 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001562
Gilles Peskine91e8c332019-08-02 19:19:39 +02001563 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001564 * external-only flags, query `attributes`. Thanks to the check
1565 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001566 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001567 * may have set. */
1568 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001569
Gilles Peskinecbaff462019-07-12 23:46:04 +02001570#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001571 /* For a key in a secure element, we need to do three things
1572 * when creating a key (but not when registering an existing key):
Gilles Peskine60450a42019-07-25 11:32:45 +02001573 * create the key file in internal storage, create the
1574 * key inside the secure element, and update the driver's
1575 * persistent data. Start a transaction that will encompass these
1576 * three actions. */
1577 /* The first thing to do is to find a slot number for the new key.
1578 * We save the slot number in persistent storage as part of the
1579 * transaction data. It will be needed to recover if the power
1580 * fails during the key creation process, to clean up on the secure
1581 * element side after restarting. Obtaining a slot number from the
1582 * secure element driver updates its persistent state, but we do not yet
1583 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001584 * we can roll back to a state where the key doesn't exist. */
Gilles Peskined7729582019-08-05 15:55:54 +02001585 if( *p_drv != NULL && method != PSA_KEY_CREATION_REGISTER )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001586 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001587 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001588 &slot->data.se.slot_number );
1589 if( status != PSA_SUCCESS )
1590 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001591 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001592 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001593 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001594 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001595 status = psa_crypto_save_transaction( );
1596 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001597 {
1598 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001599 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001600 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001601 }
1602#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1603
Gilles Peskine4747d192019-04-17 15:05:45 +02001604 return( status );
1605}
1606
1607/** Finalize the creation of a key once its key material has been set.
1608 *
1609 * This entails writing the key to persistent storage.
1610 *
1611 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001612 * See the documentation of psa_start_key_creation() for the intended use
1613 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001614 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 * \param[in,out] slot Pointer to the slot with key material.
1616 * \param[in] driver The secure element driver for the key,
1617 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001618 *
1619 * \retval #PSA_SUCCESS
1620 * The key was successfully created. The handle is now valid.
1621 * \return If this function fails, the key slot is an invalid state.
1622 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001623 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001624static psa_status_t psa_finish_key_creation(
1625 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001626 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001627{
1628 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001629 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001631
1632#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001633 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001634 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001635#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1636 if( driver != NULL )
1637 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001638 psa_se_key_data_storage_t data;
1639#if defined(static_assert)
1640 static_assert( sizeof( slot->data.se.slot_number ) ==
1641 sizeof( data.slot_number ),
1642 "Slot number size does not match psa_se_key_data_storage_t" );
1643 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1644 "Bit-size size does not match psa_se_key_data_storage_t" );
1645#endif
1646 memcpy( &data.slot_number, &slot->data.se.slot_number,
1647 sizeof( slot->data.se.slot_number ) );
1648 memcpy( &data.bits, &slot->attr.bits,
1649 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001650 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001651 (uint8_t*) &data,
1652 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001653 }
1654 else
1655#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1656 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001657 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001658 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001659 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001660 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1661 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001662 if( buffer == NULL && buffer_size != 0 )
1663 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1664 status = psa_internal_export_key( slot,
1665 buffer, buffer_size, &length,
1666 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001667 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001668 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001669 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001670
Gilles Peskine1df83d42019-07-23 16:13:14 +02001671 if( buffer_size != 0 )
1672 mbedtls_platform_zeroize( buffer, buffer_size );
1673 mbedtls_free( buffer );
1674 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 }
1676#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1677
Gilles Peskinecbaff462019-07-12 23:46:04 +02001678#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001679 /* Finish the transaction for a key creation. This does not
1680 * happen when registering an existing key. Detect this case
1681 * by checking whether a transaction is in progress (actual
1682 * creation of a key in a secure element requires a transaction,
1683 * but registration doesn't use one). */
1684 if( driver != NULL &&
1685 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001686 {
1687 status = psa_save_se_persistent_data( driver );
1688 if( status != PSA_SUCCESS )
1689 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001690 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001691 return( status );
1692 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001693 status = psa_crypto_stop_transaction( );
1694 if( status != PSA_SUCCESS )
1695 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001696 }
1697#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1698
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 return( status );
1700}
1701
1702/** Abort the creation of a key.
1703 *
1704 * You may call this function after calling psa_start_key_creation(),
1705 * or after psa_finish_key_creation() fails. In other circumstances, this
1706 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001707 * See the documentation of psa_start_key_creation() for the intended use
1708 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 * \param[in,out] slot Pointer to the slot with key material.
1711 * \param[in] driver The secure element driver for the key,
1712 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001713 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001714static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001715 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001716{
Gilles Peskine011e4282019-06-26 18:34:38 +02001717 (void) driver;
1718
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 if( slot == NULL )
1720 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001721
1722#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1723 /* TOnogrepDO: If the key has already been created in the secure
1724 * element, and the failure happened later (when saving metadata
1725 * to internal storage), we need to destroy the key in the secure
1726 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001727
Gilles Peskined7729582019-08-05 15:55:54 +02001728 /* Abort the ongoing transaction if any (there may not be one if
1729 * the creation process failed before starting one, or if the
1730 * key creation is a registration of a key in a secure element).
1731 * Earlier functions must already have done what it takes to undo any
1732 * partial creation. All that's left is to update the transaction data
1733 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001734 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001735#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1736
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 psa_wipe_key_slot( slot );
1738}
1739
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001740/** Validate optional attributes during key creation.
1741 *
1742 * Some key attributes are optional during key creation. If they are
1743 * specified in the attributes structure, check that they are consistent
1744 * with the data in the slot.
1745 *
1746 * This function should be called near the end of key creation, after
1747 * the slot in memory is fully populated but before saving persistent data.
1748 */
1749static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001750 const psa_key_slot_t *slot,
1751 const psa_key_attributes_t *attributes )
1752{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001753 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001754 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001755 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001756 return( PSA_ERROR_INVALID_ARGUMENT );
1757 }
1758
1759 if( attributes->domain_parameters_size != 0 )
1760 {
1761#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001762 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001763 {
1764 mbedtls_mpi actual, required;
1765 int ret;
1766 mbedtls_mpi_init( &actual );
1767 mbedtls_mpi_init( &required );
1768 ret = mbedtls_rsa_export( slot->data.rsa,
1769 NULL, NULL, NULL, NULL, &actual );
1770 if( ret != 0 )
1771 goto rsa_exit;
1772 ret = mbedtls_mpi_read_binary( &required,
1773 attributes->domain_parameters,
1774 attributes->domain_parameters_size );
1775 if( ret != 0 )
1776 goto rsa_exit;
1777 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1778 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1779 rsa_exit:
1780 mbedtls_mpi_free( &actual );
1781 mbedtls_mpi_free( &required );
1782 if( ret != 0)
1783 return( mbedtls_to_psa_error( ret ) );
1784 }
1785 else
1786#endif
1787 {
1788 return( PSA_ERROR_INVALID_ARGUMENT );
1789 }
1790 }
1791
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001792 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001793 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001794 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001795 return( PSA_ERROR_INVALID_ARGUMENT );
1796 }
1797
1798 return( PSA_SUCCESS );
1799}
1800
Gilles Peskine4747d192019-04-17 15:05:45 +02001801psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001802 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001803 size_t data_length,
1804 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001805{
1806 psa_status_t status;
1807 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001808 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809
Gilles Peskinedf179142019-07-15 22:02:14 +02001810 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1811 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 if( status != PSA_SUCCESS )
1813 goto exit;
1814
Gilles Peskine5d309672019-07-12 23:47:28 +02001815#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1816 if( driver != NULL )
1817 {
1818 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001819 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001820 if( drv->key_management == NULL ||
1821 drv->key_management->p_import == NULL )
1822 {
1823 status = PSA_ERROR_NOT_SUPPORTED;
1824 goto exit;
1825 }
1826 status = drv->key_management->p_import(
1827 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001828 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001829 &bits );
1830 if( status != PSA_SUCCESS )
1831 goto exit;
1832 if( bits > PSA_MAX_KEY_BITS )
1833 {
1834 status = PSA_ERROR_NOT_SUPPORTED;
1835 goto exit;
1836 }
1837 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001838 }
1839 else
1840#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1841 {
1842 status = psa_import_key_into_slot( slot, data, data_length );
1843 if( status != PSA_SUCCESS )
1844 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001845 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001846 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001847 if( status != PSA_SUCCESS )
1848 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 if( status != PSA_SUCCESS )
1853 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001854 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 *handle = 0;
1856 }
1857 return( status );
1858}
1859
Gilles Peskined7729582019-08-05 15:55:54 +02001860#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1861psa_status_t mbedtls_psa_register_se_key(
1862 const psa_key_attributes_t *attributes )
1863{
1864 psa_status_t status;
1865 psa_key_slot_t *slot = NULL;
1866 psa_se_drv_table_entry_t *driver = NULL;
1867 const psa_drv_se_t *drv;
1868 psa_key_handle_t handle = 0;
1869
1870 /* Leaving attributes unspecified is not currently supported.
1871 * It could make sense to query the key type and size from the
1872 * secure element, but not all secure elements support this
1873 * and the driver HAL doesn't currently support it. */
1874 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1875 return( PSA_ERROR_NOT_SUPPORTED );
1876 if( psa_get_key_bits( attributes ) == 0 )
1877 return( PSA_ERROR_NOT_SUPPORTED );
1878
1879 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1880 &handle, &slot, &driver );
1881 if( status != PSA_SUCCESS )
1882 goto exit;
1883
1884 if( driver == NULL )
1885 {
1886 status = PSA_ERROR_INVALID_ARGUMENT;
1887 goto exit;
1888 }
1889 drv = psa_get_se_driver_methods( driver );
1890
1891 if ( psa_get_key_slot_number( attributes,
1892 &slot->data.se.slot_number ) != PSA_SUCCESS )
1893 {
1894 /* The application didn't specify a slot number. This doesn't
1895 * make sense when registering a slot. */
1896 status = PSA_ERROR_INVALID_ARGUMENT;
1897 goto exit;
1898 }
1899
Gilles Peskinea5f87492019-08-05 16:46:18 +02001900 /* If the driver has a slot number validation method, call it.
1901 * If it doesn't, it means the secure element is unable to validate
1902 * anything and so we have to trust the application. */
1903 if( drv->key_management != NULL &&
1904 drv->key_management->p_validate_slot_number != NULL )
1905 {
1906 status = drv->key_management->p_validate_slot_number(
1907 psa_get_se_driver_context( driver ),
1908 attributes,
1909 PSA_KEY_CREATION_REGISTER,
1910 slot->data.se.slot_number );
1911 if( status != PSA_SUCCESS )
1912 goto exit;
1913 }
1914
Gilles Peskined7729582019-08-05 15:55:54 +02001915 status = psa_finish_key_creation( slot, driver );
1916
1917exit:
1918 if( status != PSA_SUCCESS )
1919 {
1920 psa_fail_key_creation( slot, driver );
1921 }
1922 /* Registration doesn't keep the key in RAM. */
1923 psa_close_key( handle );
1924 return( status );
1925}
1926#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1927
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001928static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001929 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001930{
1931 psa_status_t status;
1932 uint8_t *buffer = NULL;
1933 size_t buffer_size = 0;
1934 size_t length;
1935
Gilles Peskine8e338702019-07-30 20:06:31 +02001936 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001937 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001938 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001939 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001940 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001941 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1942 if( status != PSA_SUCCESS )
1943 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001944 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001945 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001946
1947exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001948 if( buffer_size != 0 )
1949 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001950 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001951 return( status );
1952}
1953
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001954psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1955 const psa_key_attributes_t *specified_attributes,
1956 psa_key_handle_t *target_handle )
1957{
1958 psa_status_t status;
1959 psa_key_slot_t *source_slot = NULL;
1960 psa_key_slot_t *target_slot = NULL;
1961 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001962 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001963
Gilles Peskine28f8f302019-07-24 13:30:31 +02001964 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001965 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001966 if( status != PSA_SUCCESS )
1967 goto exit;
1968
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001969 status = psa_validate_optional_attributes( source_slot,
1970 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001971 if( status != PSA_SUCCESS )
1972 goto exit;
1973
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001974 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001975 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001976 if( status != PSA_SUCCESS )
1977 goto exit;
1978
Gilles Peskinedf179142019-07-15 22:02:14 +02001979 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1980 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001981 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001985#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1986 if( driver != NULL )
1987 {
1988 /* Copying to a secure element is not implemented yet. */
1989 status = PSA_ERROR_NOT_SUPPORTED;
1990 goto exit;
1991 }
1992#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1993
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 if( status != PSA_SUCCESS )
1996 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997
Gilles Peskine011e4282019-06-26 18:34:38 +02001998 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002000 if( status != PSA_SUCCESS )
2001 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002002 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 *target_handle = 0;
2004 }
2005 return( status );
2006}
2007
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002008
2009
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002010/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002011/* Message digests */
2012/****************************************************************/
2013
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002014static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002015{
2016 switch( alg )
2017 {
2018#if defined(MBEDTLS_MD2_C)
2019 case PSA_ALG_MD2:
2020 return( &mbedtls_md2_info );
2021#endif
2022#if defined(MBEDTLS_MD4_C)
2023 case PSA_ALG_MD4:
2024 return( &mbedtls_md4_info );
2025#endif
2026#if defined(MBEDTLS_MD5_C)
2027 case PSA_ALG_MD5:
2028 return( &mbedtls_md5_info );
2029#endif
2030#if defined(MBEDTLS_RIPEMD160_C)
2031 case PSA_ALG_RIPEMD160:
2032 return( &mbedtls_ripemd160_info );
2033#endif
2034#if defined(MBEDTLS_SHA1_C)
2035 case PSA_ALG_SHA_1:
2036 return( &mbedtls_sha1_info );
2037#endif
2038#if defined(MBEDTLS_SHA256_C)
2039 case PSA_ALG_SHA_224:
2040 return( &mbedtls_sha224_info );
2041 case PSA_ALG_SHA_256:
2042 return( &mbedtls_sha256_info );
2043#endif
2044#if defined(MBEDTLS_SHA512_C)
2045 case PSA_ALG_SHA_384:
2046 return( &mbedtls_sha384_info );
2047 case PSA_ALG_SHA_512:
2048 return( &mbedtls_sha512_info );
2049#endif
2050 default:
2051 return( NULL );
2052 }
2053}
2054
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002055psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2056{
2057 switch( operation->alg )
2058 {
Gilles Peskine81736312018-06-26 15:04:31 +02002059 case 0:
2060 /* The object has (apparently) been initialized but it is not
2061 * in use. It's ok to call abort on such an object, and there's
2062 * nothing to do. */
2063 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002064#if defined(MBEDTLS_MD2_C)
2065 case PSA_ALG_MD2:
2066 mbedtls_md2_free( &operation->ctx.md2 );
2067 break;
2068#endif
2069#if defined(MBEDTLS_MD4_C)
2070 case PSA_ALG_MD4:
2071 mbedtls_md4_free( &operation->ctx.md4 );
2072 break;
2073#endif
2074#if defined(MBEDTLS_MD5_C)
2075 case PSA_ALG_MD5:
2076 mbedtls_md5_free( &operation->ctx.md5 );
2077 break;
2078#endif
2079#if defined(MBEDTLS_RIPEMD160_C)
2080 case PSA_ALG_RIPEMD160:
2081 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_SHA1_C)
2085 case PSA_ALG_SHA_1:
2086 mbedtls_sha1_free( &operation->ctx.sha1 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_SHA256_C)
2090 case PSA_ALG_SHA_224:
2091 case PSA_ALG_SHA_256:
2092 mbedtls_sha256_free( &operation->ctx.sha256 );
2093 break;
2094#endif
2095#if defined(MBEDTLS_SHA512_C)
2096 case PSA_ALG_SHA_384:
2097 case PSA_ALG_SHA_512:
2098 mbedtls_sha512_free( &operation->ctx.sha512 );
2099 break;
2100#endif
2101 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002102 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002103 }
2104 operation->alg = 0;
2105 return( PSA_SUCCESS );
2106}
2107
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002108psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 psa_algorithm_t alg )
2110{
2111 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002112
2113 /* A context must be freshly initialized before it can be set up. */
2114 if( operation->alg != 0 )
2115 {
2116 return( PSA_ERROR_BAD_STATE );
2117 }
2118
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119 switch( alg )
2120 {
2121#if defined(MBEDTLS_MD2_C)
2122 case PSA_ALG_MD2:
2123 mbedtls_md2_init( &operation->ctx.md2 );
2124 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2125 break;
2126#endif
2127#if defined(MBEDTLS_MD4_C)
2128 case PSA_ALG_MD4:
2129 mbedtls_md4_init( &operation->ctx.md4 );
2130 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2131 break;
2132#endif
2133#if defined(MBEDTLS_MD5_C)
2134 case PSA_ALG_MD5:
2135 mbedtls_md5_init( &operation->ctx.md5 );
2136 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_RIPEMD160_C)
2140 case PSA_ALG_RIPEMD160:
2141 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2142 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_SHA1_C)
2146 case PSA_ALG_SHA_1:
2147 mbedtls_sha1_init( &operation->ctx.sha1 );
2148 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_SHA256_C)
2152 case PSA_ALG_SHA_224:
2153 mbedtls_sha256_init( &operation->ctx.sha256 );
2154 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2155 break;
2156 case PSA_ALG_SHA_256:
2157 mbedtls_sha256_init( &operation->ctx.sha256 );
2158 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2159 break;
2160#endif
2161#if defined(MBEDTLS_SHA512_C)
2162 case PSA_ALG_SHA_384:
2163 mbedtls_sha512_init( &operation->ctx.sha512 );
2164 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2165 break;
2166 case PSA_ALG_SHA_512:
2167 mbedtls_sha512_init( &operation->ctx.sha512 );
2168 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2169 break;
2170#endif
2171 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002172 return( PSA_ALG_IS_HASH( alg ) ?
2173 PSA_ERROR_NOT_SUPPORTED :
2174 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175 }
2176 if( ret == 0 )
2177 operation->alg = alg;
2178 else
2179 psa_hash_abort( operation );
2180 return( mbedtls_to_psa_error( ret ) );
2181}
2182
2183psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2184 const uint8_t *input,
2185 size_t input_length )
2186{
2187 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002188
2189 /* Don't require hash implementations to behave correctly on a
2190 * zero-length input, which may have an invalid pointer. */
2191 if( input_length == 0 )
2192 return( PSA_SUCCESS );
2193
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 switch( operation->alg )
2195 {
2196#if defined(MBEDTLS_MD2_C)
2197 case PSA_ALG_MD2:
2198 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2199 input, input_length );
2200 break;
2201#endif
2202#if defined(MBEDTLS_MD4_C)
2203 case PSA_ALG_MD4:
2204 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2205 input, input_length );
2206 break;
2207#endif
2208#if defined(MBEDTLS_MD5_C)
2209 case PSA_ALG_MD5:
2210 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2211 input, input_length );
2212 break;
2213#endif
2214#if defined(MBEDTLS_RIPEMD160_C)
2215 case PSA_ALG_RIPEMD160:
2216 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2217 input, input_length );
2218 break;
2219#endif
2220#if defined(MBEDTLS_SHA1_C)
2221 case PSA_ALG_SHA_1:
2222 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2223 input, input_length );
2224 break;
2225#endif
2226#if defined(MBEDTLS_SHA256_C)
2227 case PSA_ALG_SHA_224:
2228 case PSA_ALG_SHA_256:
2229 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2230 input, input_length );
2231 break;
2232#endif
2233#if defined(MBEDTLS_SHA512_C)
2234 case PSA_ALG_SHA_384:
2235 case PSA_ALG_SHA_512:
2236 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2237 input, input_length );
2238 break;
2239#endif
2240 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002241 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002243
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 if( ret != 0 )
2245 psa_hash_abort( operation );
2246 return( mbedtls_to_psa_error( ret ) );
2247}
2248
2249psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2250 uint8_t *hash,
2251 size_t hash_size,
2252 size_t *hash_length )
2253{
itayzafrir40835d42018-08-02 13:14:17 +03002254 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002256 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257
2258 /* Fill the output buffer with something that isn't a valid hash
2259 * (barring an attack on the hash and deliberately-crafted input),
2260 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002261 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002262 /* If hash_size is 0 then hash may be NULL and then the
2263 * call to memset would have undefined behavior. */
2264 if( hash_size != 0 )
2265 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266
2267 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002268 {
2269 status = PSA_ERROR_BUFFER_TOO_SMALL;
2270 goto exit;
2271 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272
2273 switch( operation->alg )
2274 {
2275#if defined(MBEDTLS_MD2_C)
2276 case PSA_ALG_MD2:
2277 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2278 break;
2279#endif
2280#if defined(MBEDTLS_MD4_C)
2281 case PSA_ALG_MD4:
2282 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2283 break;
2284#endif
2285#if defined(MBEDTLS_MD5_C)
2286 case PSA_ALG_MD5:
2287 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2288 break;
2289#endif
2290#if defined(MBEDTLS_RIPEMD160_C)
2291 case PSA_ALG_RIPEMD160:
2292 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2293 break;
2294#endif
2295#if defined(MBEDTLS_SHA1_C)
2296 case PSA_ALG_SHA_1:
2297 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2298 break;
2299#endif
2300#if defined(MBEDTLS_SHA256_C)
2301 case PSA_ALG_SHA_224:
2302 case PSA_ALG_SHA_256:
2303 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2304 break;
2305#endif
2306#if defined(MBEDTLS_SHA512_C)
2307 case PSA_ALG_SHA_384:
2308 case PSA_ALG_SHA_512:
2309 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2310 break;
2311#endif
2312 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002313 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 }
itayzafrir40835d42018-08-02 13:14:17 +03002315 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316
itayzafrir40835d42018-08-02 13:14:17 +03002317exit:
2318 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002320 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 return( psa_hash_abort( operation ) );
2322 }
2323 else
2324 {
2325 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002326 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 }
2328}
2329
Gilles Peskine2d277862018-06-18 15:41:12 +02002330psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2331 const uint8_t *hash,
2332 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333{
2334 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2335 size_t actual_hash_length;
2336 psa_status_t status = psa_hash_finish( operation,
2337 actual_hash, sizeof( actual_hash ),
2338 &actual_hash_length );
2339 if( status != PSA_SUCCESS )
2340 return( status );
2341 if( actual_hash_length != hash_length )
2342 return( PSA_ERROR_INVALID_SIGNATURE );
2343 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2344 return( PSA_ERROR_INVALID_SIGNATURE );
2345 return( PSA_SUCCESS );
2346}
2347
Gilles Peskineeb35d782019-01-22 17:56:16 +01002348psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2349 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002350{
2351 if( target_operation->alg != 0 )
2352 return( PSA_ERROR_BAD_STATE );
2353
2354 switch( source_operation->alg )
2355 {
2356 case 0:
2357 return( PSA_ERROR_BAD_STATE );
2358#if defined(MBEDTLS_MD2_C)
2359 case PSA_ALG_MD2:
2360 mbedtls_md2_clone( &target_operation->ctx.md2,
2361 &source_operation->ctx.md2 );
2362 break;
2363#endif
2364#if defined(MBEDTLS_MD4_C)
2365 case PSA_ALG_MD4:
2366 mbedtls_md4_clone( &target_operation->ctx.md4,
2367 &source_operation->ctx.md4 );
2368 break;
2369#endif
2370#if defined(MBEDTLS_MD5_C)
2371 case PSA_ALG_MD5:
2372 mbedtls_md5_clone( &target_operation->ctx.md5,
2373 &source_operation->ctx.md5 );
2374 break;
2375#endif
2376#if defined(MBEDTLS_RIPEMD160_C)
2377 case PSA_ALG_RIPEMD160:
2378 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2379 &source_operation->ctx.ripemd160 );
2380 break;
2381#endif
2382#if defined(MBEDTLS_SHA1_C)
2383 case PSA_ALG_SHA_1:
2384 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2385 &source_operation->ctx.sha1 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_SHA256_C)
2389 case PSA_ALG_SHA_224:
2390 case PSA_ALG_SHA_256:
2391 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2392 &source_operation->ctx.sha256 );
2393 break;
2394#endif
2395#if defined(MBEDTLS_SHA512_C)
2396 case PSA_ALG_SHA_384:
2397 case PSA_ALG_SHA_512:
2398 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2399 &source_operation->ctx.sha512 );
2400 break;
2401#endif
2402 default:
2403 return( PSA_ERROR_NOT_SUPPORTED );
2404 }
2405
2406 target_operation->alg = source_operation->alg;
2407 return( PSA_SUCCESS );
2408}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409
2410
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412/* MAC */
2413/****************************************************************/
2414
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002415static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416 psa_algorithm_t alg,
2417 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002418 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002419 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002422 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002424 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002425 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002426
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2428 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002429 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002431 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002432 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 mode = MBEDTLS_MODE_STREAM;
2434 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002435 case PSA_ALG_CTR:
2436 mode = MBEDTLS_MODE_CTR;
2437 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002438 case PSA_ALG_CFB:
2439 mode = MBEDTLS_MODE_CFB;
2440 break;
2441 case PSA_ALG_OFB:
2442 mode = MBEDTLS_MODE_OFB;
2443 break;
2444 case PSA_ALG_CBC_NO_PADDING:
2445 mode = MBEDTLS_MODE_CBC;
2446 break;
2447 case PSA_ALG_CBC_PKCS7:
2448 mode = MBEDTLS_MODE_CBC;
2449 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002450 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 mode = MBEDTLS_MODE_CCM;
2452 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002453 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 mode = MBEDTLS_MODE_GCM;
2455 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002456 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2457 mode = MBEDTLS_MODE_CHACHAPOLY;
2458 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 default:
2460 return( NULL );
2461 }
2462 }
2463 else if( alg == PSA_ALG_CMAC )
2464 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465 else
2466 return( NULL );
2467
2468 switch( key_type )
2469 {
2470 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002471 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472 break;
2473 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002474 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2475 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002477 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002479 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002480 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2481 * but two-key Triple-DES is functionally three-key Triple-DES
2482 * with K1=K3, so that's how we present it to mbedtls. */
2483 if( key_bits == 128 )
2484 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 break;
2486 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002487 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 break;
2489 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002490 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002492 case PSA_KEY_TYPE_CHACHA20:
2493 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2494 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 default:
2496 return( NULL );
2497 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002498 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002499 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500
Jaeden Amero23bbb752018-06-26 14:16:54 +01002501 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2502 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503}
2504
Gilles Peskinea05219c2018-11-16 16:02:56 +01002505#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002506static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002507{
Gilles Peskine2d277862018-06-18 15:41:12 +02002508 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002509 {
2510 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002511 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002512 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002513 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002514 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002515 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002516 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002517 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002518 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002519 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002520 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002521 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002522 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002523 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002524 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002525 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002526 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002527 return( 128 );
2528 default:
2529 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002530 }
2531}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002532#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002533
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002534/* Initialize the MAC operation structure. Once this function has been
2535 * called, psa_mac_abort can run and will do the right thing. */
2536static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2537 psa_algorithm_t alg )
2538{
2539 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2540
2541 operation->alg = alg;
2542 operation->key_set = 0;
2543 operation->iv_set = 0;
2544 operation->iv_required = 0;
2545 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002546 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547
2548#if defined(MBEDTLS_CMAC_C)
2549 if( alg == PSA_ALG_CMAC )
2550 {
2551 operation->iv_required = 0;
2552 mbedtls_cipher_init( &operation->ctx.cmac );
2553 status = PSA_SUCCESS;
2554 }
2555 else
2556#endif /* MBEDTLS_CMAC_C */
2557#if defined(MBEDTLS_MD_C)
2558 if( PSA_ALG_IS_HMAC( operation->alg ) )
2559 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002560 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2561 operation->ctx.hmac.hash_ctx.alg = 0;
2562 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563 }
2564 else
2565#endif /* MBEDTLS_MD_C */
2566 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002567 if( ! PSA_ALG_IS_MAC( alg ) )
2568 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569 }
2570
2571 if( status != PSA_SUCCESS )
2572 memset( operation, 0, sizeof( *operation ) );
2573 return( status );
2574}
2575
Gilles Peskine01126fa2018-07-12 17:04:55 +02002576#if defined(MBEDTLS_MD_C)
2577static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2578{
Gilles Peskine3f108122018-12-07 18:14:53 +01002579 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002580 return( psa_hash_abort( &hmac->hash_ctx ) );
2581}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002582
Janos Follath999f6482019-06-11 12:04:10 +01002583#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002584static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2585{
2586 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2587 memset( hmac, 0, sizeof( *hmac ) );
2588}
Janos Follath999f6482019-06-11 12:04:10 +01002589#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002590#endif /* MBEDTLS_MD_C */
2591
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2593{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002594 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002596 /* The object has (apparently) been initialized but it is not
2597 * in use. It's ok to call abort on such an object, and there's
2598 * nothing to do. */
2599 return( PSA_SUCCESS );
2600 }
2601 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002603 if( operation->alg == PSA_ALG_CMAC )
2604 {
2605 mbedtls_cipher_free( &operation->ctx.cmac );
2606 }
2607 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002610 if( PSA_ALG_IS_HMAC( operation->alg ) )
2611 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002612 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 }
2614 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002616 {
2617 /* Sanity check (shouldn't happen: operation->alg should
2618 * always have been initialized to a valid value). */
2619 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620 }
Moran Peker41deec42018-04-04 15:43:05 +03002621
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622 operation->alg = 0;
2623 operation->key_set = 0;
2624 operation->iv_set = 0;
2625 operation->iv_required = 0;
2626 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002628
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002630
2631bad_state:
2632 /* If abort is called on an uninitialized object, we can't trust
2633 * anything. Wipe the object in case it contains confidential data.
2634 * This may result in a memory leak if a pointer gets overwritten,
2635 * but it's too late to do anything about this. */
2636 memset( operation, 0, sizeof( *operation ) );
2637 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638}
2639
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002640#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002641static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002642 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002643 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002644 const mbedtls_cipher_info_t *cipher_info )
2645{
2646 int ret;
2647
2648 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002649
2650 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2651 if( ret != 0 )
2652 return( ret );
2653
2654 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2655 slot->data.raw.data,
2656 key_bits );
2657 return( ret );
2658}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002659#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002660
Gilles Peskine248051a2018-06-20 16:09:38 +02002661#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002662static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2663 const uint8_t *key,
2664 size_t key_length,
2665 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002666{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002667 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002668 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002669 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002670 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002671 psa_status_t status;
2672
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002673 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2674 * overflow below. This should never trigger if the hash algorithm
2675 * is implemented correctly. */
2676 /* The size checks against the ipad and opad buffers cannot be written
2677 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2678 * because that triggers -Wlogical-op on GCC 7.3. */
2679 if( block_size > sizeof( ipad ) )
2680 return( PSA_ERROR_NOT_SUPPORTED );
2681 if( block_size > sizeof( hmac->opad ) )
2682 return( PSA_ERROR_NOT_SUPPORTED );
2683 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002684 return( PSA_ERROR_NOT_SUPPORTED );
2685
Gilles Peskined223b522018-06-11 18:12:58 +02002686 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002687 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002688 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002689 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002690 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002691 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002692 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002693 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002694 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002695 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002696 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002697 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002698 }
Gilles Peskine96889972018-07-12 17:07:03 +02002699 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2700 * but it is permitted. It is common when HMAC is used in HKDF, for
2701 * example. Don't call `memcpy` in the 0-length because `key` could be
2702 * an invalid pointer which would make the behavior undefined. */
2703 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002704 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002705
Gilles Peskined223b522018-06-11 18:12:58 +02002706 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2707 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002709 ipad[i] ^= 0x36;
2710 memset( ipad + key_length, 0x36, block_size - key_length );
2711
2712 /* Copy the key material from ipad to opad, flipping the requisite bits,
2713 * and filling the rest of opad with the requisite constant. */
2714 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002715 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2716 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717
Gilles Peskine01126fa2018-07-12 17:04:55 +02002718 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002720 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721
Gilles Peskine01126fa2018-07-12 17:04:55 +02002722 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002723
2724cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002725 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002726
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002727 return( status );
2728}
Gilles Peskine248051a2018-06-20 16:09:38 +02002729#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730
Gilles Peskine89167cb2018-07-08 20:12:23 +02002731static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002732 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002733 psa_algorithm_t alg,
2734 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002737 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002739 psa_key_usage_t usage =
2740 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002741 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002742 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002744 /* A context must be freshly initialized before it can be set up. */
2745 if( operation->alg != 0 )
2746 {
2747 return( PSA_ERROR_BAD_STATE );
2748 }
2749
Gilles Peskined911eb72018-08-14 15:18:45 +02002750 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002751 if( status != PSA_SUCCESS )
2752 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002753 if( is_sign )
2754 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755
Gilles Peskine28f8f302019-07-24 13:30:31 +02002756 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002757 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002758 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002759 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002760
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002762 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 {
2764 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002765 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002766 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002767 int ret;
2768 if( cipher_info == NULL )
2769 {
2770 status = PSA_ERROR_NOT_SUPPORTED;
2771 goto exit;
2772 }
2773 operation->mac_size = cipher_info->block_size;
2774 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2775 status = mbedtls_to_psa_error( ret );
2776 }
2777 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002779#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002780 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002781 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002782 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002783 if( hash_alg == 0 )
2784 {
2785 status = PSA_ERROR_NOT_SUPPORTED;
2786 goto exit;
2787 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002788
2789 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2790 /* Sanity check. This shouldn't fail on a valid configuration. */
2791 if( operation->mac_size == 0 ||
2792 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2793 {
2794 status = PSA_ERROR_NOT_SUPPORTED;
2795 goto exit;
2796 }
2797
Gilles Peskine8e338702019-07-30 20:06:31 +02002798 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002799 {
2800 status = PSA_ERROR_INVALID_ARGUMENT;
2801 goto exit;
2802 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002803
Gilles Peskine01126fa2018-07-12 17:04:55 +02002804 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2805 slot->data.raw.data,
2806 slot->data.raw.bytes,
2807 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002808 }
2809 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002811 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002812 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002813 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815
Gilles Peskined911eb72018-08-14 15:18:45 +02002816 if( truncated == 0 )
2817 {
2818 /* The "normal" case: untruncated algorithm. Nothing to do. */
2819 }
2820 else if( truncated < 4 )
2821 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002822 /* A very short MAC is too short for security since it can be
2823 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2824 * so we make this our minimum, even though 32 bits is still
2825 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002826 status = PSA_ERROR_NOT_SUPPORTED;
2827 }
2828 else if( truncated > operation->mac_size )
2829 {
2830 /* It's impossible to "truncate" to a larger length. */
2831 status = PSA_ERROR_INVALID_ARGUMENT;
2832 }
2833 else
2834 operation->mac_size = truncated;
2835
Gilles Peskinefbfac682018-07-08 20:51:54 +02002836exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002837 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002839 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002841 else
2842 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002843 operation->key_set = 1;
2844 }
2845 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846}
2847
Gilles Peskine89167cb2018-07-08 20:12:23 +02002848psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002849 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002850 psa_algorithm_t alg )
2851{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002852 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002853}
2854
2855psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002856 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002857 psa_algorithm_t alg )
2858{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002859 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002860}
2861
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2863 const uint8_t *input,
2864 size_t input_length )
2865{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002867 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002868 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002870 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871 operation->has_input = 1;
2872
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002874 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002875 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2877 input, input_length );
2878 status = mbedtls_to_psa_error( ret );
2879 }
2880 else
2881#endif /* MBEDTLS_CMAC_C */
2882#if defined(MBEDTLS_MD_C)
2883 if( PSA_ALG_IS_HMAC( operation->alg ) )
2884 {
2885 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2886 input_length );
2887 }
2888 else
2889#endif /* MBEDTLS_MD_C */
2890 {
2891 /* This shouldn't happen if `operation` was initialized by
2892 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002893 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002894 }
2895
Gilles Peskinefbfac682018-07-08 20:51:54 +02002896 if( status != PSA_SUCCESS )
2897 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002898 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899}
2900
Gilles Peskine01126fa2018-07-12 17:04:55 +02002901#if defined(MBEDTLS_MD_C)
2902static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2903 uint8_t *mac,
2904 size_t mac_size )
2905{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002906 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002907 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2908 size_t hash_size = 0;
2909 size_t block_size = psa_get_hash_block_size( hash_alg );
2910 psa_status_t status;
2911
Gilles Peskine01126fa2018-07-12 17:04:55 +02002912 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2913 if( status != PSA_SUCCESS )
2914 return( status );
2915 /* From here on, tmp needs to be wiped. */
2916
2917 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2918 if( status != PSA_SUCCESS )
2919 goto exit;
2920
2921 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2922 if( status != PSA_SUCCESS )
2923 goto exit;
2924
2925 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2926 if( status != PSA_SUCCESS )
2927 goto exit;
2928
Gilles Peskined911eb72018-08-14 15:18:45 +02002929 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2930 if( status != PSA_SUCCESS )
2931 goto exit;
2932
2933 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002934
2935exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002936 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002937 return( status );
2938}
2939#endif /* MBEDTLS_MD_C */
2940
mohammad16036df908f2018-04-02 08:34:15 -07002941static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002942 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002943 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002945 if( ! operation->key_set )
2946 return( PSA_ERROR_BAD_STATE );
2947 if( operation->iv_required && ! operation->iv_set )
2948 return( PSA_ERROR_BAD_STATE );
2949
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 if( mac_size < operation->mac_size )
2951 return( PSA_ERROR_BUFFER_TOO_SMALL );
2952
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 if( operation->alg == PSA_ALG_CMAC )
2955 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002956 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2957 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2958 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002959 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002960 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002961 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002963 else
2964#endif /* MBEDTLS_CMAC_C */
2965#if defined(MBEDTLS_MD_C)
2966 if( PSA_ALG_IS_HMAC( operation->alg ) )
2967 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002968 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002969 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002970 }
2971 else
2972#endif /* MBEDTLS_MD_C */
2973 {
2974 /* This shouldn't happen if `operation` was initialized by
2975 * a setup function. */
2976 return( PSA_ERROR_BAD_STATE );
2977 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978}
2979
Gilles Peskineacd4be32018-07-08 19:56:25 +02002980psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2981 uint8_t *mac,
2982 size_t mac_size,
2983 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002984{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002985 psa_status_t status;
2986
Jaeden Amero252ef282019-02-15 14:05:35 +00002987 if( operation->alg == 0 )
2988 {
2989 return( PSA_ERROR_BAD_STATE );
2990 }
2991
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002992 /* Fill the output buffer with something that isn't a valid mac
2993 * (barring an attack on the mac and deliberately-crafted input),
2994 * in case the caller doesn't check the return status properly. */
2995 *mac_length = mac_size;
2996 /* If mac_size is 0 then mac may be NULL and then the
2997 * call to memset would have undefined behavior. */
2998 if( mac_size != 0 )
2999 memset( mac, '!', mac_size );
3000
Gilles Peskine89167cb2018-07-08 20:12:23 +02003001 if( ! operation->is_sign )
3002 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003003 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003004 }
mohammad16036df908f2018-04-02 08:34:15 -07003005
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003006 status = psa_mac_finish_internal( operation, mac, mac_size );
3007
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003008 if( status == PSA_SUCCESS )
3009 {
3010 status = psa_mac_abort( operation );
3011 if( status == PSA_SUCCESS )
3012 *mac_length = operation->mac_size;
3013 else
3014 memset( mac, '!', mac_size );
3015 }
3016 else
3017 psa_mac_abort( operation );
3018 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003019}
3020
Gilles Peskineacd4be32018-07-08 19:56:25 +02003021psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3022 const uint8_t *mac,
3023 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024{
Gilles Peskine828ed142018-06-18 23:25:51 +02003025 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003026 psa_status_t status;
3027
Jaeden Amero252ef282019-02-15 14:05:35 +00003028 if( operation->alg == 0 )
3029 {
3030 return( PSA_ERROR_BAD_STATE );
3031 }
3032
Gilles Peskine89167cb2018-07-08 20:12:23 +02003033 if( operation->is_sign )
3034 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003035 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003036 }
3037 if( operation->mac_size != mac_length )
3038 {
3039 status = PSA_ERROR_INVALID_SIGNATURE;
3040 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003041 }
mohammad16036df908f2018-04-02 08:34:15 -07003042
3043 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003044 actual_mac, sizeof( actual_mac ) );
3045
3046 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3047 status = PSA_ERROR_INVALID_SIGNATURE;
3048
3049cleanup:
3050 if( status == PSA_SUCCESS )
3051 status = psa_mac_abort( operation );
3052 else
3053 psa_mac_abort( operation );
3054
Gilles Peskine3f108122018-12-07 18:14:53 +01003055 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003056
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003057 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058}
3059
3060
Gilles Peskine20035e32018-02-03 22:44:14 +01003061
Gilles Peskine20035e32018-02-03 22:44:14 +01003062/****************************************************************/
3063/* Asymmetric cryptography */
3064/****************************************************************/
3065
Gilles Peskine2b450e32018-06-27 15:42:46 +02003066#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003067/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003068 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003069static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3070 size_t hash_length,
3071 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003072{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003073 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003074 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003075 *md_alg = mbedtls_md_get_type( md_info );
3076
3077 /* The Mbed TLS RSA module uses an unsigned int for hash length
3078 * parameters. Validate that it fits so that we don't risk an
3079 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003080#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003081 if( hash_length > UINT_MAX )
3082 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003083#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003084
3085#if defined(MBEDTLS_PKCS1_V15)
3086 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3087 * must be correct. */
3088 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3089 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003090 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003091 if( md_info == NULL )
3092 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003093 if( mbedtls_md_get_size( md_info ) != hash_length )
3094 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003095 }
3096#endif /* MBEDTLS_PKCS1_V15 */
3097
3098#if defined(MBEDTLS_PKCS1_V21)
3099 /* PSS requires a hash internally. */
3100 if( PSA_ALG_IS_RSA_PSS( alg ) )
3101 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003102 if( md_info == NULL )
3103 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003104 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003105#endif /* MBEDTLS_PKCS1_V21 */
3106
Gilles Peskine61b91d42018-06-08 16:09:36 +02003107 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003108}
3109
Gilles Peskine2b450e32018-06-27 15:42:46 +02003110static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3111 psa_algorithm_t alg,
3112 const uint8_t *hash,
3113 size_t hash_length,
3114 uint8_t *signature,
3115 size_t signature_size,
3116 size_t *signature_length )
3117{
3118 psa_status_t status;
3119 int ret;
3120 mbedtls_md_type_t md_alg;
3121
3122 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3123 if( status != PSA_SUCCESS )
3124 return( status );
3125
Gilles Peskine630a18a2018-06-29 17:49:35 +02003126 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003127 return( PSA_ERROR_BUFFER_TOO_SMALL );
3128
3129#if defined(MBEDTLS_PKCS1_V15)
3130 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3131 {
3132 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3133 MBEDTLS_MD_NONE );
3134 ret = mbedtls_rsa_pkcs1_sign( rsa,
3135 mbedtls_ctr_drbg_random,
3136 &global_data.ctr_drbg,
3137 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003138 md_alg,
3139 (unsigned int) hash_length,
3140 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003141 signature );
3142 }
3143 else
3144#endif /* MBEDTLS_PKCS1_V15 */
3145#if defined(MBEDTLS_PKCS1_V21)
3146 if( PSA_ALG_IS_RSA_PSS( alg ) )
3147 {
3148 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3149 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3150 mbedtls_ctr_drbg_random,
3151 &global_data.ctr_drbg,
3152 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003153 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003154 (unsigned int) hash_length,
3155 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003156 signature );
3157 }
3158 else
3159#endif /* MBEDTLS_PKCS1_V21 */
3160 {
3161 return( PSA_ERROR_INVALID_ARGUMENT );
3162 }
3163
3164 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003165 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003166 return( mbedtls_to_psa_error( ret ) );
3167}
3168
3169static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3170 psa_algorithm_t alg,
3171 const uint8_t *hash,
3172 size_t hash_length,
3173 const uint8_t *signature,
3174 size_t signature_length )
3175{
3176 psa_status_t status;
3177 int ret;
3178 mbedtls_md_type_t md_alg;
3179
3180 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3181 if( status != PSA_SUCCESS )
3182 return( status );
3183
Gilles Peskine630a18a2018-06-29 17:49:35 +02003184 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003185 return( PSA_ERROR_BUFFER_TOO_SMALL );
3186
3187#if defined(MBEDTLS_PKCS1_V15)
3188 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3189 {
3190 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3191 MBEDTLS_MD_NONE );
3192 ret = mbedtls_rsa_pkcs1_verify( rsa,
3193 mbedtls_ctr_drbg_random,
3194 &global_data.ctr_drbg,
3195 MBEDTLS_RSA_PUBLIC,
3196 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003197 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003198 hash,
3199 signature );
3200 }
3201 else
3202#endif /* MBEDTLS_PKCS1_V15 */
3203#if defined(MBEDTLS_PKCS1_V21)
3204 if( PSA_ALG_IS_RSA_PSS( alg ) )
3205 {
3206 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3207 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3208 mbedtls_ctr_drbg_random,
3209 &global_data.ctr_drbg,
3210 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003211 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003212 (unsigned int) hash_length,
3213 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003214 signature );
3215 }
3216 else
3217#endif /* MBEDTLS_PKCS1_V21 */
3218 {
3219 return( PSA_ERROR_INVALID_ARGUMENT );
3220 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003221
3222 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3223 * the rest of the signature is invalid". This has little use in
3224 * practice and PSA doesn't report this distinction. */
3225 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3226 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003227 return( mbedtls_to_psa_error( ret ) );
3228}
3229#endif /* MBEDTLS_RSA_C */
3230
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003231#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003232/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3233 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3234 * (even though these functions don't modify it). */
3235static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3236 psa_algorithm_t alg,
3237 const uint8_t *hash,
3238 size_t hash_length,
3239 uint8_t *signature,
3240 size_t signature_size,
3241 size_t *signature_length )
3242{
3243 int ret;
3244 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003245 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003246 mbedtls_mpi_init( &r );
3247 mbedtls_mpi_init( &s );
3248
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003249 if( signature_size < 2 * curve_bytes )
3250 {
3251 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3252 goto cleanup;
3253 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003254
Gilles Peskinea05219c2018-11-16 16:02:56 +01003255#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003256 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3257 {
3258 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3259 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3260 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3261 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3262 hash, hash_length,
3263 md_alg ) );
3264 }
3265 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003266#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003267 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003268 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003269 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3270 hash, hash_length,
3271 mbedtls_ctr_drbg_random,
3272 &global_data.ctr_drbg ) );
3273 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003274
3275 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3276 signature,
3277 curve_bytes ) );
3278 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3279 signature + curve_bytes,
3280 curve_bytes ) );
3281
3282cleanup:
3283 mbedtls_mpi_free( &r );
3284 mbedtls_mpi_free( &s );
3285 if( ret == 0 )
3286 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003287 return( mbedtls_to_psa_error( ret ) );
3288}
3289
3290static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3291 const uint8_t *hash,
3292 size_t hash_length,
3293 const uint8_t *signature,
3294 size_t signature_length )
3295{
3296 int ret;
3297 mbedtls_mpi r, s;
3298 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3299 mbedtls_mpi_init( &r );
3300 mbedtls_mpi_init( &s );
3301
3302 if( signature_length != 2 * curve_bytes )
3303 return( PSA_ERROR_INVALID_SIGNATURE );
3304
3305 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3306 signature,
3307 curve_bytes ) );
3308 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3309 signature + curve_bytes,
3310 curve_bytes ) );
3311
3312 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3313 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003314
3315cleanup:
3316 mbedtls_mpi_free( &r );
3317 mbedtls_mpi_free( &s );
3318 return( mbedtls_to_psa_error( ret ) );
3319}
3320#endif /* MBEDTLS_ECDSA_C */
3321
Gilles Peskinec5487a82018-12-03 18:08:14 +01003322psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003323 psa_algorithm_t alg,
3324 const uint8_t *hash,
3325 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003326 uint8_t *signature,
3327 size_t signature_size,
3328 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003329{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003330 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003331 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003332#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3333 const psa_drv_se_t *drv;
3334 psa_drv_se_context_t *drv_context;
3335#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003336
3337 *signature_length = signature_size;
3338
Gilles Peskineedc64242019-08-07 21:05:07 +02003339 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003340 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003341 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003342 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003343 {
3344 status = PSA_ERROR_INVALID_ARGUMENT;
3345 goto exit;
3346 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003347
Gilles Peskineedc64242019-08-07 21:05:07 +02003348#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3349 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3350 {
3351 if( drv->asymmetric == NULL ||
3352 drv->asymmetric->p_sign == NULL )
3353 {
3354 status = PSA_ERROR_NOT_SUPPORTED;
3355 goto exit;
3356 }
3357 status = drv->asymmetric->p_sign( drv_context,
3358 slot->data.se.slot_number,
3359 alg,
3360 hash, hash_length,
3361 signature, signature_size,
3362 signature_length );
3363 }
3364 else
3365#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003366#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003367 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003368 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003369 status = psa_rsa_sign( slot->data.rsa,
3370 alg,
3371 hash, hash_length,
3372 signature, signature_size,
3373 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003374 }
3375 else
3376#endif /* defined(MBEDTLS_RSA_C) */
3377#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003378 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003379 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003380#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003381 if(
3382#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3383 PSA_ALG_IS_ECDSA( alg )
3384#else
3385 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3386#endif
3387 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003388 status = psa_ecdsa_sign( slot->data.ecp,
3389 alg,
3390 hash, hash_length,
3391 signature, signature_size,
3392 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003393 else
3394#endif /* defined(MBEDTLS_ECDSA_C) */
3395 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003396 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003397 }
itayzafrir5c753392018-05-08 11:18:38 +03003398 }
3399 else
3400#endif /* defined(MBEDTLS_ECP_C) */
3401 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003402 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003403 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003404
3405exit:
3406 /* Fill the unused part of the output buffer (the whole buffer on error,
3407 * the trailing part on success) with something that isn't a valid mac
3408 * (barring an attack on the mac and deliberately-crafted input),
3409 * in case the caller doesn't check the return status properly. */
3410 if( status == PSA_SUCCESS )
3411 memset( signature + *signature_length, '!',
3412 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003413 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003415 /* If signature_size is 0 then we have nothing to do. We must not call
3416 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003418}
3419
Gilles Peskinec5487a82018-12-03 18:08:14 +01003420psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003421 psa_algorithm_t alg,
3422 const uint8_t *hash,
3423 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003424 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003425 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003427 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3430 const psa_drv_se_t *drv;
3431 psa_drv_se_context_t *drv_context;
3432#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003433
Gilles Peskineedc64242019-08-07 21:05:07 +02003434 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003435 if( status != PSA_SUCCESS )
3436 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003437
Gilles Peskineedc64242019-08-07 21:05:07 +02003438#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3439 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3440 {
3441 if( drv->asymmetric == NULL ||
3442 drv->asymmetric->p_verify == NULL )
3443 return( PSA_ERROR_NOT_SUPPORTED );
3444 return( drv->asymmetric->p_verify( drv_context,
3445 slot->data.se.slot_number,
3446 alg,
3447 hash, hash_length,
3448 signature, signature_length ) );
3449 }
3450 else
3451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003452#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003453 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003454 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003455 return( psa_rsa_verify( slot->data.rsa,
3456 alg,
3457 hash, hash_length,
3458 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459 }
3460 else
3461#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003462#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003463 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003464 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003465#if defined(MBEDTLS_ECDSA_C)
3466 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003467 return( psa_ecdsa_verify( slot->data.ecp,
3468 hash, hash_length,
3469 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003470 else
3471#endif /* defined(MBEDTLS_ECDSA_C) */
3472 {
3473 return( PSA_ERROR_INVALID_ARGUMENT );
3474 }
itayzafrir5c753392018-05-08 11:18:38 +03003475 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003476 else
3477#endif /* defined(MBEDTLS_ECP_C) */
3478 {
3479 return( PSA_ERROR_NOT_SUPPORTED );
3480 }
3481}
3482
Gilles Peskine072ac562018-06-30 00:21:29 +02003483#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3484static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3485 mbedtls_rsa_context *rsa )
3486{
3487 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3488 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3489 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3490 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3491}
3492#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3493
Gilles Peskinec5487a82018-12-03 18:08:14 +01003494psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003495 psa_algorithm_t alg,
3496 const uint8_t *input,
3497 size_t input_length,
3498 const uint8_t *salt,
3499 size_t salt_length,
3500 uint8_t *output,
3501 size_t output_size,
3502 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003503{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003504 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003505 psa_status_t status;
3506
Darryl Green5cc689a2018-07-24 15:34:10 +01003507 (void) input;
3508 (void) input_length;
3509 (void) salt;
3510 (void) output;
3511 (void) output_size;
3512
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003513 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003514
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003515 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3516 return( PSA_ERROR_INVALID_ARGUMENT );
3517
Gilles Peskine28f8f302019-07-24 13:30:31 +02003518 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003519 if( status != PSA_SUCCESS )
3520 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003521 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3522 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003523 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524
3525#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003526 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527 {
3528 mbedtls_rsa_context *rsa = slot->data.rsa;
3529 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003530 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003531 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003533 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003535 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3536 mbedtls_ctr_drbg_random,
3537 &global_data.ctr_drbg,
3538 MBEDTLS_RSA_PUBLIC,
3539 input_length,
3540 input,
3541 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 }
3543 else
3544#endif /* MBEDTLS_PKCS1_V15 */
3545#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003546 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003548 psa_rsa_oaep_set_padding_mode( alg, rsa );
3549 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3550 mbedtls_ctr_drbg_random,
3551 &global_data.ctr_drbg,
3552 MBEDTLS_RSA_PUBLIC,
3553 salt, salt_length,
3554 input_length,
3555 input,
3556 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 }
3558 else
3559#endif /* MBEDTLS_PKCS1_V21 */
3560 {
3561 return( PSA_ERROR_INVALID_ARGUMENT );
3562 }
3563 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003564 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565 return( mbedtls_to_psa_error( ret ) );
3566 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003568#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 {
3570 return( PSA_ERROR_NOT_SUPPORTED );
3571 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003572}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573
Gilles Peskinec5487a82018-12-03 18:08:14 +01003574psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003575 psa_algorithm_t alg,
3576 const uint8_t *input,
3577 size_t input_length,
3578 const uint8_t *salt,
3579 size_t salt_length,
3580 uint8_t *output,
3581 size_t output_size,
3582 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003584 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003585 psa_status_t status;
3586
Darryl Green5cc689a2018-07-24 15:34:10 +01003587 (void) input;
3588 (void) input_length;
3589 (void) salt;
3590 (void) output;
3591 (void) output_size;
3592
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003593 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003595 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3596 return( PSA_ERROR_INVALID_ARGUMENT );
3597
Gilles Peskine28f8f302019-07-24 13:30:31 +02003598 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003599 if( status != PSA_SUCCESS )
3600 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003601 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602 return( PSA_ERROR_INVALID_ARGUMENT );
3603
3604#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003605 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606 {
3607 mbedtls_rsa_context *rsa = slot->data.rsa;
3608 int ret;
3609
Gilles Peskine630a18a2018-06-29 17:49:35 +02003610 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611 return( PSA_ERROR_INVALID_ARGUMENT );
3612
3613#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003614 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003616 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3617 mbedtls_ctr_drbg_random,
3618 &global_data.ctr_drbg,
3619 MBEDTLS_RSA_PRIVATE,
3620 output_length,
3621 input,
3622 output,
3623 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624 }
3625 else
3626#endif /* MBEDTLS_PKCS1_V15 */
3627#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003628 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003630 psa_rsa_oaep_set_padding_mode( alg, rsa );
3631 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3632 mbedtls_ctr_drbg_random,
3633 &global_data.ctr_drbg,
3634 MBEDTLS_RSA_PRIVATE,
3635 salt, salt_length,
3636 output_length,
3637 input,
3638 output,
3639 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640 }
3641 else
3642#endif /* MBEDTLS_PKCS1_V21 */
3643 {
3644 return( PSA_ERROR_INVALID_ARGUMENT );
3645 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003646
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647 return( mbedtls_to_psa_error( ret ) );
3648 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003650#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651 {
3652 return( PSA_ERROR_NOT_SUPPORTED );
3653 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003654}
Gilles Peskine20035e32018-02-03 22:44:14 +01003655
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003656
3657
mohammad1603503973b2018-03-12 15:59:30 +02003658/****************************************************************/
3659/* Symmetric cryptography */
3660/****************************************************************/
3661
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003662/* Initialize the cipher operation structure. Once this function has been
3663 * called, psa_cipher_abort can run and will do the right thing. */
3664static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3665 psa_algorithm_t alg )
3666{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003667 if( ! PSA_ALG_IS_CIPHER( alg ) )
3668 {
3669 memset( operation, 0, sizeof( *operation ) );
3670 return( PSA_ERROR_INVALID_ARGUMENT );
3671 }
3672
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003673 operation->alg = alg;
3674 operation->key_set = 0;
3675 operation->iv_set = 0;
3676 operation->iv_required = 1;
3677 operation->iv_size = 0;
3678 operation->block_size = 0;
3679 mbedtls_cipher_init( &operation->ctx.cipher );
3680 return( PSA_SUCCESS );
3681}
3682
Gilles Peskinee553c652018-06-04 16:22:46 +02003683static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003684 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003685 psa_algorithm_t alg,
3686 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003687{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003688 int ret = 0;
3689 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003690 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003691 size_t key_bits;
3692 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003693 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3694 PSA_KEY_USAGE_ENCRYPT :
3695 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003696
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003697 /* A context must be freshly initialized before it can be set up. */
3698 if( operation->alg != 0 )
3699 {
3700 return( PSA_ERROR_BAD_STATE );
3701 }
3702
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003703 status = psa_cipher_init( operation, alg );
3704 if( status != PSA_SUCCESS )
3705 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003706
Gilles Peskine28f8f302019-07-24 13:30:31 +02003707 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003708 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003709 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003710 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003711
Gilles Peskine8e338702019-07-30 20:06:31 +02003712 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003713 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003714 {
3715 status = PSA_ERROR_NOT_SUPPORTED;
3716 goto exit;
3717 }
mohammad1603503973b2018-03-12 15:59:30 +02003718
mohammad1603503973b2018-03-12 15:59:30 +02003719 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003720 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003721 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003722
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003723#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003724 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003725 {
3726 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003727 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003728 memcpy( keys, slot->data.raw.data, 16 );
3729 memcpy( keys + 16, slot->data.raw.data, 8 );
3730 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3731 keys,
3732 192, cipher_operation );
3733 }
3734 else
3735#endif
3736 {
3737 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3738 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003739 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003740 }
Moran Peker41deec42018-04-04 15:43:05 +03003741 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003742 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003743
mohammad16038481e742018-03-18 13:57:31 +02003744#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003745 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003746 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003747 case PSA_ALG_CBC_NO_PADDING:
3748 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3749 MBEDTLS_PADDING_NONE );
3750 break;
3751 case PSA_ALG_CBC_PKCS7:
3752 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3753 MBEDTLS_PADDING_PKCS7 );
3754 break;
3755 default:
3756 /* The algorithm doesn't involve padding. */
3757 ret = 0;
3758 break;
3759 }
3760 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003761 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003762#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3763
mohammad1603503973b2018-03-12 15:59:30 +02003764 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003765 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003766 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003767 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003768 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003769 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003770 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003771#if defined(MBEDTLS_CHACHA20_C)
3772 else
3773 if( alg == PSA_ALG_CHACHA20 )
3774 operation->iv_size = 12;
3775#endif
mohammad1603503973b2018-03-12 15:59:30 +02003776
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003777exit:
3778 if( status == 0 )
3779 status = mbedtls_to_psa_error( ret );
3780 if( status != 0 )
3781 psa_cipher_abort( operation );
3782 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003783}
3784
Gilles Peskinefe119512018-07-08 21:39:34 +02003785psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003786 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003787 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003788{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003789 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003790}
3791
Gilles Peskinefe119512018-07-08 21:39:34 +02003792psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003793 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003794 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003795{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003796 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003797}
3798
Gilles Peskinefe119512018-07-08 21:39:34 +02003799psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003800 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003801 size_t iv_size,
3802 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003803{
itayzafrir534bd7c2018-08-02 13:56:32 +03003804 psa_status_t status;
3805 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003806 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003807 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003808 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003809 }
Moran Peker41deec42018-04-04 15:43:05 +03003810 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003811 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003812 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003813 goto exit;
3814 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003815 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3816 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003817 if( ret != 0 )
3818 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003819 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003820 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003821 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003822
mohammad16038481e742018-03-18 13:57:31 +02003823 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003824 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003825
Moran Peker395db872018-05-31 14:07:14 +03003826exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003827 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003828 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003829 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003830}
3831
Gilles Peskinefe119512018-07-08 21:39:34 +02003832psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003833 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003834 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003835{
itayzafrir534bd7c2018-08-02 13:56:32 +03003836 psa_status_t status;
3837 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003838 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003839 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003840 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003841 }
Moran Pekera28258c2018-05-29 16:25:04 +03003842 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003843 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003844 status = PSA_ERROR_INVALID_ARGUMENT;
3845 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003846 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003847 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3848 status = mbedtls_to_psa_error( ret );
3849exit:
3850 if( status == PSA_SUCCESS )
3851 operation->iv_set = 1;
3852 else
mohammad1603503973b2018-03-12 15:59:30 +02003853 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003854 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003855}
3856
Gilles Peskinee553c652018-06-04 16:22:46 +02003857psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3858 const uint8_t *input,
3859 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003860 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003861 size_t output_size,
3862 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003863{
itayzafrir534bd7c2018-08-02 13:56:32 +03003864 psa_status_t status;
3865 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003866 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003867
3868 if( operation->alg == 0 )
3869 {
3870 return( PSA_ERROR_BAD_STATE );
3871 }
3872
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003873 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003874 {
3875 /* Take the unprocessed partial block left over from previous
3876 * update calls, if any, plus the input to this call. Remove
3877 * the last partial block, if any. You get the data that will be
3878 * output in this call. */
3879 expected_output_size =
3880 ( operation->ctx.cipher.unprocessed_len + input_length )
3881 / operation->block_size * operation->block_size;
3882 }
3883 else
3884 {
3885 expected_output_size = input_length;
3886 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003887
Gilles Peskine89d789c2018-06-04 17:17:16 +02003888 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 {
3890 status = PSA_ERROR_BUFFER_TOO_SMALL;
3891 goto exit;
3892 }
mohammad160382759612018-03-12 18:16:40 +02003893
mohammad1603503973b2018-03-12 15:59:30 +02003894 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003895 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 status = mbedtls_to_psa_error( ret );
3897exit:
3898 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003899 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003900 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003901}
3902
Gilles Peskinee553c652018-06-04 16:22:46 +02003903psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3904 uint8_t *output,
3905 size_t output_size,
3906 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003907{
David Saadab4ecc272019-02-14 13:48:10 +02003908 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003909 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003910 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003911
mohammad1603503973b2018-03-12 15:59:30 +02003912 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003913 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003914 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003915 }
3916 if( operation->iv_required && ! operation->iv_set )
3917 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003918 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003919 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003920
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003921 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003922 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3923 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003924 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003925 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003926 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003927 }
3928
Janos Follath315b51c2018-07-09 16:04:51 +01003929 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3930 temp_output_buffer,
3931 output_length );
3932 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003933 {
Janos Follath315b51c2018-07-09 16:04:51 +01003934 status = mbedtls_to_psa_error( cipher_ret );
3935 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003936 }
Janos Follath315b51c2018-07-09 16:04:51 +01003937
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003938 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003939 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003940 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003941 memcpy( output, temp_output_buffer, *output_length );
3942 else
3943 {
Janos Follath315b51c2018-07-09 16:04:51 +01003944 status = PSA_ERROR_BUFFER_TOO_SMALL;
3945 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003946 }
mohammad1603503973b2018-03-12 15:59:30 +02003947
Gilles Peskine3f108122018-12-07 18:14:53 +01003948 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003949 status = psa_cipher_abort( operation );
3950
3951 return( status );
3952
3953error:
3954
3955 *output_length = 0;
3956
Gilles Peskine3f108122018-12-07 18:14:53 +01003957 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003958 (void) psa_cipher_abort( operation );
3959
3960 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003961}
3962
Gilles Peskinee553c652018-06-04 16:22:46 +02003963psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3964{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003965 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003966 {
3967 /* The object has (apparently) been initialized but it is not
3968 * in use. It's ok to call abort on such an object, and there's
3969 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003970 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003971 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003972
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003973 /* Sanity check (shouldn't happen: operation->alg should
3974 * always have been initialized to a valid value). */
3975 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3976 return( PSA_ERROR_BAD_STATE );
3977
mohammad1603503973b2018-03-12 15:59:30 +02003978 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003979
Moran Peker41deec42018-04-04 15:43:05 +03003980 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003981 operation->key_set = 0;
3982 operation->iv_set = 0;
3983 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003984 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003985 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003986
Moran Peker395db872018-05-31 14:07:14 +03003987 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003988}
3989
Gilles Peskinea0655c32018-04-30 17:06:50 +02003990
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003991
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003992
mohammad16035955c982018-04-26 00:53:03 +03003993/****************************************************************/
3994/* AEAD */
3995/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003996
Gilles Peskineedf9a652018-08-17 18:11:56 +02003997typedef struct
3998{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003999 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004000 const mbedtls_cipher_info_t *cipher_info;
4001 union
4002 {
4003#if defined(MBEDTLS_CCM_C)
4004 mbedtls_ccm_context ccm;
4005#endif /* MBEDTLS_CCM_C */
4006#if defined(MBEDTLS_GCM_C)
4007 mbedtls_gcm_context gcm;
4008#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004009#if defined(MBEDTLS_CHACHAPOLY_C)
4010 mbedtls_chachapoly_context chachapoly;
4011#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004012 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004013 psa_algorithm_t core_alg;
4014 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004015 uint8_t tag_length;
4016} aead_operation_t;
4017
Gilles Peskine30a9e412019-01-14 18:36:12 +01004018static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004019{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004020 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004021 {
4022#if defined(MBEDTLS_CCM_C)
4023 case PSA_ALG_CCM:
4024 mbedtls_ccm_free( &operation->ctx.ccm );
4025 break;
4026#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004027#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004028 case PSA_ALG_GCM:
4029 mbedtls_gcm_free( &operation->ctx.gcm );
4030 break;
4031#endif /* MBEDTLS_GCM_C */
4032 }
4033}
4034
4035static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004036 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004037 psa_key_usage_t usage,
4038 psa_algorithm_t alg )
4039{
4040 psa_status_t status;
4041 size_t key_bits;
4042 mbedtls_cipher_id_t cipher_id;
4043
Gilles Peskine28f8f302019-07-24 13:30:31 +02004044 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004045 if( status != PSA_SUCCESS )
4046 return( status );
4047
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004048 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049
4050 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004051 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052 &cipher_id );
4053 if( operation->cipher_info == NULL )
4054 return( PSA_ERROR_NOT_SUPPORTED );
4055
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004056 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057 {
4058#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004059 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4060 operation->core_alg = PSA_ALG_CCM;
4061 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004062 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4063 * The call to mbedtls_ccm_encrypt_and_tag or
4064 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004065 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004066 return( PSA_ERROR_INVALID_ARGUMENT );
4067 mbedtls_ccm_init( &operation->ctx.ccm );
4068 status = mbedtls_to_psa_error(
4069 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4070 operation->slot->data.raw.data,
4071 (unsigned int) key_bits ) );
4072 if( status != 0 )
4073 goto cleanup;
4074 break;
4075#endif /* MBEDTLS_CCM_C */
4076
4077#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004078 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4079 operation->core_alg = PSA_ALG_GCM;
4080 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004081 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4082 * The call to mbedtls_gcm_crypt_and_tag or
4083 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004084 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085 return( PSA_ERROR_INVALID_ARGUMENT );
4086 mbedtls_gcm_init( &operation->ctx.gcm );
4087 status = mbedtls_to_psa_error(
4088 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4089 operation->slot->data.raw.data,
4090 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004091 if( status != 0 )
4092 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004093 break;
4094#endif /* MBEDTLS_GCM_C */
4095
Gilles Peskine26869f22019-05-06 15:25:00 +02004096#if defined(MBEDTLS_CHACHAPOLY_C)
4097 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4098 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4099 operation->full_tag_length = 16;
4100 /* We only support the default tag length. */
4101 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4102 return( PSA_ERROR_NOT_SUPPORTED );
4103 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4104 status = mbedtls_to_psa_error(
4105 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4106 operation->slot->data.raw.data ) );
4107 if( status != 0 )
4108 goto cleanup;
4109 break;
4110#endif /* MBEDTLS_CHACHAPOLY_C */
4111
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112 default:
4113 return( PSA_ERROR_NOT_SUPPORTED );
4114 }
4115
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004116 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4117 {
4118 status = PSA_ERROR_INVALID_ARGUMENT;
4119 goto cleanup;
4120 }
4121 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004122
Gilles Peskineedf9a652018-08-17 18:11:56 +02004123 return( PSA_SUCCESS );
4124
4125cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004126 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 return( status );
4128}
4129
Gilles Peskinec5487a82018-12-03 18:08:14 +01004130psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004131 psa_algorithm_t alg,
4132 const uint8_t *nonce,
4133 size_t nonce_length,
4134 const uint8_t *additional_data,
4135 size_t additional_data_length,
4136 const uint8_t *plaintext,
4137 size_t plaintext_length,
4138 uint8_t *ciphertext,
4139 size_t ciphertext_size,
4140 size_t *ciphertext_length )
4141{
mohammad16035955c982018-04-26 00:53:03 +03004142 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004144 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004145
mohammad1603f08a5502018-06-03 15:05:47 +03004146 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004147
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004149 if( status != PSA_SUCCESS )
4150 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004151
Gilles Peskineedf9a652018-08-17 18:11:56 +02004152 /* For all currently supported modes, the tag is at the end of the
4153 * ciphertext. */
4154 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4155 {
4156 status = PSA_ERROR_BUFFER_TOO_SMALL;
4157 goto exit;
4158 }
4159 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004160
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004161#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004162 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004163 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004164 status = mbedtls_to_psa_error(
4165 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4166 MBEDTLS_GCM_ENCRYPT,
4167 plaintext_length,
4168 nonce, nonce_length,
4169 additional_data, additional_data_length,
4170 plaintext, ciphertext,
4171 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004172 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004173 else
4174#endif /* MBEDTLS_GCM_C */
4175#if defined(MBEDTLS_CCM_C)
4176 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004177 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004178 status = mbedtls_to_psa_error(
4179 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4180 plaintext_length,
4181 nonce, nonce_length,
4182 additional_data,
4183 additional_data_length,
4184 plaintext, ciphertext,
4185 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004186 }
mohammad16035c8845f2018-05-09 05:40:09 -07004187 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004188#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004189#if defined(MBEDTLS_CHACHAPOLY_C)
4190 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4191 {
4192 if( nonce_length != 12 || operation.tag_length != 16 )
4193 {
4194 status = PSA_ERROR_NOT_SUPPORTED;
4195 goto exit;
4196 }
4197 status = mbedtls_to_psa_error(
4198 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4199 plaintext_length,
4200 nonce,
4201 additional_data,
4202 additional_data_length,
4203 plaintext,
4204 ciphertext,
4205 tag ) );
4206 }
4207 else
4208#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004209 {
mohammad1603554faad2018-06-03 15:07:38 +03004210 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004211 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004212
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4214 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004215
Gilles Peskineedf9a652018-08-17 18:11:56 +02004216exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004217 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004218 if( status == PSA_SUCCESS )
4219 *ciphertext_length = plaintext_length + operation.tag_length;
4220 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004221}
4222
Gilles Peskineee652a32018-06-01 19:23:52 +02004223/* Locate the tag in a ciphertext buffer containing the encrypted data
4224 * followed by the tag. Return the length of the part preceding the tag in
4225 * *plaintext_length. This is the size of the plaintext in modes where
4226 * the encrypted data has the same size as the plaintext, such as
4227 * CCM and GCM. */
4228static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4229 const uint8_t *ciphertext,
4230 size_t ciphertext_length,
4231 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004232 const uint8_t **p_tag )
4233{
4234 size_t payload_length;
4235 if( tag_length > ciphertext_length )
4236 return( PSA_ERROR_INVALID_ARGUMENT );
4237 payload_length = ciphertext_length - tag_length;
4238 if( payload_length > plaintext_size )
4239 return( PSA_ERROR_BUFFER_TOO_SMALL );
4240 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004241 return( PSA_SUCCESS );
4242}
4243
Gilles Peskinec5487a82018-12-03 18:08:14 +01004244psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004245 psa_algorithm_t alg,
4246 const uint8_t *nonce,
4247 size_t nonce_length,
4248 const uint8_t *additional_data,
4249 size_t additional_data_length,
4250 const uint8_t *ciphertext,
4251 size_t ciphertext_length,
4252 uint8_t *plaintext,
4253 size_t plaintext_size,
4254 size_t *plaintext_length )
4255{
mohammad16035955c982018-04-26 00:53:03 +03004256 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004257 aead_operation_t operation;
4258 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004259
Gilles Peskineee652a32018-06-01 19:23:52 +02004260 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004261
Gilles Peskinec5487a82018-12-03 18:08:14 +01004262 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004263 if( status != PSA_SUCCESS )
4264 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004265
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004266 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4267 ciphertext, ciphertext_length,
4268 plaintext_size, &tag );
4269 if( status != PSA_SUCCESS )
4270 goto exit;
4271
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004272#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004273 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004274 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004275 status = mbedtls_to_psa_error(
4276 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4277 ciphertext_length - operation.tag_length,
4278 nonce, nonce_length,
4279 additional_data,
4280 additional_data_length,
4281 tag, operation.tag_length,
4282 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004283 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004284 else
4285#endif /* MBEDTLS_GCM_C */
4286#if defined(MBEDTLS_CCM_C)
4287 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004288 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004289 status = mbedtls_to_psa_error(
4290 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4291 ciphertext_length - operation.tag_length,
4292 nonce, nonce_length,
4293 additional_data,
4294 additional_data_length,
4295 ciphertext, plaintext,
4296 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004297 }
mohammad160339574652018-06-01 04:39:53 -07004298 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004299#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004300#if defined(MBEDTLS_CHACHAPOLY_C)
4301 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4302 {
4303 if( nonce_length != 12 || operation.tag_length != 16 )
4304 {
4305 status = PSA_ERROR_NOT_SUPPORTED;
4306 goto exit;
4307 }
4308 status = mbedtls_to_psa_error(
4309 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4310 ciphertext_length - operation.tag_length,
4311 nonce,
4312 additional_data,
4313 additional_data_length,
4314 tag,
4315 ciphertext,
4316 plaintext ) );
4317 }
4318 else
4319#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004320 {
mohammad1603554faad2018-06-03 15:07:38 +03004321 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004322 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004323
Gilles Peskineedf9a652018-08-17 18:11:56 +02004324 if( status != PSA_SUCCESS && plaintext_size != 0 )
4325 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004326
Gilles Peskineedf9a652018-08-17 18:11:56 +02004327exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004328 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004329 if( status == PSA_SUCCESS )
4330 *plaintext_length = ciphertext_length - operation.tag_length;
4331 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004332}
4333
Gilles Peskinea0655c32018-04-30 17:06:50 +02004334
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004335
Gilles Peskine20035e32018-02-03 22:44:14 +01004336/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004337/* Generators */
4338/****************************************************************/
4339
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004340#define HKDF_STATE_INIT 0 /* no input yet */
4341#define HKDF_STATE_STARTED 1 /* got salt */
4342#define HKDF_STATE_KEYED 2 /* got key */
4343#define HKDF_STATE_OUTPUT 3 /* output started */
4344
Gilles Peskinecbe66502019-05-16 16:59:18 +02004345static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004346 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004348 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4349 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004350 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004352}
4353
4354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004356{
4357 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004358 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004359 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004360 {
4361 /* The object has (apparently) been initialized but it is not
4362 * in use. It's ok to call abort on such an object, and there's
4363 * nothing to do. */
4364 }
4365 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004366#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004367 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004368 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 mbedtls_free( operation->ctx.hkdf.info );
4370 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004371 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004372 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004374 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004375 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004376#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004378 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4380 operation->ctx.tls12_prf.key_len );
4381 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004382 }
Hanno Becker580fba12018-11-13 20:50:45 +00004383
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004385 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4387 operation->ctx.tls12_prf.Ai_with_seed_len );
4388 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004389 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004390#else
4391 if( operation->ctx.tls12_prf.seed != NULL )
4392 {
4393 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4394 operation->ctx.tls12_prf.seed_length );
4395 mbedtls_free( operation->ctx.tls12_prf.seed );
4396 }
4397
4398 if( operation->ctx.tls12_prf.label != NULL )
4399 {
4400 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4401 operation->ctx.tls12_prf.label_length );
4402 mbedtls_free( operation->ctx.tls12_prf.label );
4403 }
4404
4405 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4406
4407 /* We leave the fields Ai and output_block to be erased safely by the
4408 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004409#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004410 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004411 else
4412#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413 {
4414 status = PSA_ERROR_BAD_STATE;
4415 }
Janos Follath083036a2019-06-11 10:22:26 +01004416 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004417 return( status );
4418}
4419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004421 size_t *capacity)
4422{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004424 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004426 return PSA_ERROR_BAD_STATE;
4427 }
4428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004430 return( PSA_SUCCESS );
4431}
4432
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004434 size_t capacity )
4435{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004437 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004441 return( PSA_SUCCESS );
4442}
4443
Gilles Peskinebef7f142018-07-12 17:22:21 +02004444#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004447static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004448 psa_algorithm_t hash_alg,
4449 uint8_t *output,
4450 size_t output_length )
4451{
4452 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4453 psa_status_t status;
4454
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004455 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4456 return( PSA_ERROR_BAD_STATE );
4457 hkdf->state = HKDF_STATE_OUTPUT;
4458
Gilles Peskinebef7f142018-07-12 17:22:21 +02004459 while( output_length != 0 )
4460 {
4461 /* Copy what remains of the current block */
4462 uint8_t n = hash_length - hkdf->offset_in_block;
4463 if( n > output_length )
4464 n = (uint8_t) output_length;
4465 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4466 output += n;
4467 output_length -= n;
4468 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004469 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004470 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004471 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004472 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004474 * object was corrupted or if this function is called directly
4475 * inside the library. */
4476 if( hkdf->block_number == 0xff )
4477 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004478
4479 /* We need a new block */
4480 ++hkdf->block_number;
4481 hkdf->offset_in_block = 0;
4482 status = psa_hmac_setup_internal( &hkdf->hmac,
4483 hkdf->prk, hash_length,
4484 hash_alg );
4485 if( status != PSA_SUCCESS )
4486 return( status );
4487 if( hkdf->block_number != 1 )
4488 {
4489 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4490 hkdf->output_block,
4491 hash_length );
4492 if( status != PSA_SUCCESS )
4493 return( status );
4494 }
4495 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4496 hkdf->info,
4497 hkdf->info_length );
4498 if( status != PSA_SUCCESS )
4499 return( status );
4500 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4501 &hkdf->block_number, 1 );
4502 if( status != PSA_SUCCESS )
4503 return( status );
4504 status = psa_hmac_finish_internal( &hkdf->hmac,
4505 hkdf->output_block,
4506 sizeof( hkdf->output_block ) );
4507 if( status != PSA_SUCCESS )
4508 return( status );
4509 }
4510
4511 return( PSA_SUCCESS );
4512}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004513
Janos Follath999f6482019-06-11 12:04:10 +01004514#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004515static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4516 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004517 psa_algorithm_t alg )
4518{
4519 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4520 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4521 psa_hmac_internal_data hmac;
4522 psa_status_t status, cleanup_status;
4523
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004524 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004525 size_t Ai_len;
4526
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004527 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004528 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004529 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004530 * object was corrupted or if this function is called directly
4531 * inside the library. */
4532 if( tls12_prf->block_number == 0xff )
4533 return( PSA_ERROR_BAD_STATE );
4534
4535 /* We need a new block */
4536 ++tls12_prf->block_number;
4537 tls12_prf->offset_in_block = 0;
4538
4539 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4540 *
4541 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4542 *
4543 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4544 * HMAC_hash(secret, A(2) + seed) +
4545 * HMAC_hash(secret, A(3) + seed) + ...
4546 *
4547 * A(0) = seed
4548 * A(i) = HMAC_hash( secret, A(i-1) )
4549 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004550 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004551 * `HMAC_hash(secret, A(i) + seed)` from which the output
4552 * is currently extracted as `output_block`, while
4553 * `A(i) + seed` is stored in `Ai_with_seed`.
4554 *
4555 * Generating a new block means recalculating `Ai_with_seed`
4556 * from the A(i)-part of it, and afterwards recalculating
4557 * `output_block`.
4558 *
4559 * A(0) is computed at setup time.
4560 *
4561 */
4562
4563 psa_hmac_init_internal( &hmac );
4564
4565 /* We must distinguish the calculation of A(1) from those
4566 * of A(2) and higher, because A(0)=seed has a different
4567 * length than the other A(i). */
4568 if( tls12_prf->block_number == 1 )
4569 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004570 Ai = tls12_prf->Ai_with_seed + hash_length;
4571 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004572 }
4573 else
4574 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004575 Ai = tls12_prf->Ai_with_seed;
4576 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004577 }
4578
Hanno Becker3b339e22018-11-13 20:56:14 +00004579 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4580 status = psa_hmac_setup_internal( &hmac,
4581 tls12_prf->key,
4582 tls12_prf->key_len,
4583 hash_alg );
4584 if( status != PSA_SUCCESS )
4585 goto cleanup;
4586
4587 status = psa_hash_update( &hmac.hash_ctx,
4588 Ai, Ai_len );
4589 if( status != PSA_SUCCESS )
4590 goto cleanup;
4591
4592 status = psa_hmac_finish_internal( &hmac,
4593 tls12_prf->Ai_with_seed,
4594 hash_length );
4595 if( status != PSA_SUCCESS )
4596 goto cleanup;
4597
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004598 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4599 status = psa_hmac_setup_internal( &hmac,
4600 tls12_prf->key,
4601 tls12_prf->key_len,
4602 hash_alg );
4603 if( status != PSA_SUCCESS )
4604 goto cleanup;
4605
4606 status = psa_hash_update( &hmac.hash_ctx,
4607 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004608 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004609 if( status != PSA_SUCCESS )
4610 goto cleanup;
4611
4612 status = psa_hmac_finish_internal( &hmac,
4613 tls12_prf->output_block,
4614 hash_length );
4615 if( status != PSA_SUCCESS )
4616 goto cleanup;
4617
4618cleanup:
4619
4620 cleanup_status = psa_hmac_abort_internal( &hmac );
4621 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4622 status = cleanup_status;
4623
4624 return( status );
4625}
Janos Follath7742fee2019-06-17 12:58:10 +01004626#else
4627static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4628 psa_tls12_prf_key_derivation_t *tls12_prf,
4629 psa_algorithm_t alg )
4630{
4631 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4632 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004633 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4634 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004635
Janos Follath7742fee2019-06-17 12:58:10 +01004636 /* We can't be wanting more output after block 0xff, otherwise
4637 * the capacity check in psa_key_derivation_output_bytes() would have
4638 * prevented this call. It could happen only if the operation
4639 * object was corrupted or if this function is called directly
4640 * inside the library. */
4641 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004642 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004643
4644 /* We need a new block */
4645 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004646 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004647
4648 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4649 *
4650 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4651 *
4652 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4653 * HMAC_hash(secret, A(2) + seed) +
4654 * HMAC_hash(secret, A(3) + seed) + ...
4655 *
4656 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004657 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004658 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004659 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004660 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004661 * is currently extracted as `output_block` and where i is
4662 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004663 */
4664
Janos Follathea29bfb2019-06-19 12:21:20 +01004665 /* Save the hash context before using it, to preserve the hash state with
4666 * only the inner padding in it. We need this, because inner padding depends
4667 * on the key (secret in the RFC's terminology). */
4668 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4669 if( status != PSA_SUCCESS )
4670 goto cleanup;
4671
4672 /* Calculate A(i) where i = tls12_prf->block_number. */
4673 if( tls12_prf->block_number == 1 )
4674 {
4675 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4676 * the variable seed and in this instance means it in the context of the
4677 * P_hash function, where seed = label + seed.) */
4678 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4679 tls12_prf->label, tls12_prf->label_length );
4680 if( status != PSA_SUCCESS )
4681 goto cleanup;
4682 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4683 tls12_prf->seed, tls12_prf->seed_length );
4684 if( status != PSA_SUCCESS )
4685 goto cleanup;
4686 }
4687 else
4688 {
4689 /* A(i) = HMAC_hash(secret, A(i-1)) */
4690 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4691 tls12_prf->Ai, hash_length );
4692 if( status != PSA_SUCCESS )
4693 goto cleanup;
4694 }
4695
4696 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4697 tls12_prf->Ai, hash_length );
4698 if( status != PSA_SUCCESS )
4699 goto cleanup;
4700 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4701 if( status != PSA_SUCCESS )
4702 goto cleanup;
4703
4704 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4705 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4706 tls12_prf->Ai, hash_length );
4707 if( status != PSA_SUCCESS )
4708 goto cleanup;
4709 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4710 tls12_prf->label, tls12_prf->label_length );
4711 if( status != PSA_SUCCESS )
4712 goto cleanup;
4713 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4714 tls12_prf->seed, tls12_prf->seed_length );
4715 if( status != PSA_SUCCESS )
4716 goto cleanup;
4717 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4718 tls12_prf->output_block, hash_length );
4719 if( status != PSA_SUCCESS )
4720 goto cleanup;
4721 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4722 if( status != PSA_SUCCESS )
4723 goto cleanup;
4724
Janos Follath7742fee2019-06-17 12:58:10 +01004725
4726cleanup:
4727
Janos Follathea29bfb2019-06-19 12:21:20 +01004728 cleanup_status = psa_hash_abort( &backup );
4729 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4730 status = cleanup_status;
4731
4732 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004733}
Janos Follath999f6482019-06-11 12:04:10 +01004734#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004735
Janos Follath999f6482019-06-11 12:04:10 +01004736#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004738 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004739static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004740 psa_tls12_prf_key_derivation_t *tls12_prf,
4741 psa_algorithm_t alg,
4742 uint8_t *output,
4743 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004744{
4745 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4746 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4747 psa_status_t status;
4748
4749 while( output_length != 0 )
4750 {
4751 /* Copy what remains of the current block */
4752 uint8_t n = hash_length - tls12_prf->offset_in_block;
4753
4754 /* Check if we have fully processed the current block. */
4755 if( n == 0 )
4756 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004757 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004758 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004759 if( status != PSA_SUCCESS )
4760 return( status );
4761
4762 continue;
4763 }
4764
4765 if( n > output_length )
4766 n = (uint8_t) output_length;
4767 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4768 n );
4769 output += n;
4770 output_length -= n;
4771 tls12_prf->offset_in_block += n;
4772 }
4773
4774 return( PSA_SUCCESS );
4775}
Janos Follath844eb0e2019-06-19 12:10:49 +01004776#else
4777static psa_status_t psa_key_derivation_tls12_prf_read(
4778 psa_tls12_prf_key_derivation_t *tls12_prf,
4779 psa_algorithm_t alg,
4780 uint8_t *output,
4781 size_t output_length )
4782{
4783 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4784 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4785 psa_status_t status;
4786 uint8_t offset, length;
4787
4788 while( output_length != 0 )
4789 {
4790 /* Check if we have fully processed the current block. */
4791 if( tls12_prf->left_in_block == 0 )
4792 {
4793 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4794 alg );
4795 if( status != PSA_SUCCESS )
4796 return( status );
4797
4798 continue;
4799 }
4800
4801 if( tls12_prf->left_in_block > output_length )
4802 length = (uint8_t) output_length;
4803 else
4804 length = tls12_prf->left_in_block;
4805
4806 offset = hash_length - tls12_prf->left_in_block;
4807 memcpy( output, tls12_prf->output_block + offset, length );
4808 output += length;
4809 output_length -= length;
4810 tls12_prf->left_in_block -= length;
4811 }
4812
4813 return( PSA_SUCCESS );
4814}
Janos Follath999f6482019-06-11 12:04:10 +01004815#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004816#endif /* MBEDTLS_MD_C */
4817
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004818psa_status_t psa_key_derivation_output_bytes(
4819 psa_key_derivation_operation_t *operation,
4820 uint8_t *output,
4821 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004822{
4823 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004825
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004827 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004829 return PSA_ERROR_BAD_STATE;
4830 }
4831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004833 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004835 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004837 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004838 goto exit;
4839 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004841 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004843 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004844 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4845 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004847 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004848 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004849 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004851
Gilles Peskinebef7f142018-07-12 17:22:21 +02004852#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004854 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004855 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004857 output, output_length );
4858 }
Janos Follathadbec812019-06-14 11:05:39 +01004859 else
Janos Follathadbec812019-06-14 11:05:39 +01004860 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004861 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004862 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004864 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004865 output_length );
4866 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004867 else
4868#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004869 {
4870 return( PSA_ERROR_BAD_STATE );
4871 }
4872
4873exit:
4874 if( status != PSA_SUCCESS )
4875 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004876 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 * This allows us to differentiate between exhausted operations and
4878 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4879 * operations. */
4880 psa_algorithm_t alg = operation->alg;
4881 psa_key_derivation_abort( operation );
4882 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004883 memset( output, '!', output_length );
4884 }
4885 return( status );
4886}
4887
Gilles Peskine08542d82018-07-19 17:05:42 +02004888#if defined(MBEDTLS_DES_C)
4889static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4890{
4891 if( data_size >= 8 )
4892 mbedtls_des_key_set_parity( data );
4893 if( data_size >= 16 )
4894 mbedtls_des_key_set_parity( data + 8 );
4895 if( data_size >= 24 )
4896 mbedtls_des_key_set_parity( data + 16 );
4897}
4898#endif /* MBEDTLS_DES_C */
4899
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004900static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004901 psa_key_slot_t *slot,
4902 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004904{
4905 uint8_t *data = NULL;
4906 size_t bytes = PSA_BITS_TO_BYTES( bits );
4907 psa_status_t status;
4908
Gilles Peskine8e338702019-07-30 20:06:31 +02004909 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004910 return( PSA_ERROR_INVALID_ARGUMENT );
4911 if( bits % 8 != 0 )
4912 return( PSA_ERROR_INVALID_ARGUMENT );
4913 data = mbedtls_calloc( 1, bytes );
4914 if( data == NULL )
4915 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004918 if( status != PSA_SUCCESS )
4919 goto exit;
4920#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004921 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004922 psa_des_set_key_parity( data, bytes );
4923#endif /* MBEDTLS_DES_C */
4924 status = psa_import_key_into_slot( slot, data, bytes );
4925
4926exit:
4927 mbedtls_free( data );
4928 return( status );
4929}
4930
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004931psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004933 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004934{
4935 psa_status_t status;
4936 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004937 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinedf179142019-07-15 22:02:14 +02004938 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4939 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004940#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4941 if( driver != NULL )
4942 {
4943 /* Deriving a key in a secure element is not implemented yet. */
4944 status = PSA_ERROR_NOT_SUPPORTED;
4945 }
4946#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004947 if( status == PSA_SUCCESS )
4948 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004949 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004950 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004952 }
4953 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004954 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004955 if( status != PSA_SUCCESS )
4956 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004957 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004958 *handle = 0;
4959 }
4960 return( status );
4961}
4962
Gilles Peskineeab56e42018-07-12 17:12:33 +02004963
4964
4965/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004966/* Key derivation */
4967/****************************************************************/
4968
Gilles Peskinea05219c2018-11-16 16:02:56 +01004969#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004970#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004972 * of the HKDF algorithm.
4973 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004974 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004975 * to potentially free embedded data structures and wipe confidential data.
4976 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004977static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004978 const uint8_t *secret,
4979 size_t secret_length,
4980 psa_algorithm_t hash_alg,
4981 const uint8_t *salt,
4982 size_t salt_length,
4983 const uint8_t *label,
4984 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004985{
4986 psa_status_t status;
4987 status = psa_hmac_setup_internal( &hkdf->hmac,
4988 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004989 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004990 if( status != PSA_SUCCESS )
4991 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004992 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004993 if( status != PSA_SUCCESS )
4994 return( status );
4995 status = psa_hmac_finish_internal( &hkdf->hmac,
4996 hkdf->prk,
4997 sizeof( hkdf->prk ) );
4998 if( status != PSA_SUCCESS )
4999 return( status );
5000 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5001 hkdf->block_number = 0;
5002 hkdf->info_length = label_length;
5003 if( label_length != 0 )
5004 {
5005 hkdf->info = mbedtls_calloc( 1, label_length );
5006 if( hkdf->info == NULL )
5007 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5008 memcpy( hkdf->info, label, label_length );
5009 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005010 hkdf->state = HKDF_STATE_KEYED;
5011 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005012 return( PSA_SUCCESS );
5013}
Janos Follath71a4c912019-06-11 09:14:47 +01005014#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005015#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005016
Gilles Peskinea05219c2018-11-16 16:02:56 +01005017#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01005018#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01005020 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005021 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005022 * to potentially free embedded data structures and wipe confidential data.
5023 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005024static psa_status_t psa_key_derivation_tls12_prf_setup(
5025 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005026 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005027 size_t key_len,
5028 psa_algorithm_t hash_alg,
5029 const uint8_t *salt,
5030 size_t salt_length,
5031 const uint8_t *label,
5032 size_t label_length )
5033{
5034 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00005035 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
5036 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005037
5038 tls12_prf->key = mbedtls_calloc( 1, key_len );
5039 if( tls12_prf->key == NULL )
5040 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5041 tls12_prf->key_len = key_len;
5042 memcpy( tls12_prf->key, key, key_len );
5043
Hanno Becker580fba12018-11-13 20:50:45 +00005044 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005045 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00005046 if( overflow )
5047 return( PSA_ERROR_INVALID_ARGUMENT );
5048
5049 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
5050 if( tls12_prf->Ai_with_seed == NULL )
5051 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5052 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
5053
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005054 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
5055 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00005056 if( label_length != 0 )
5057 {
5058 memcpy( tls12_prf->Ai_with_seed + hash_length,
5059 label, label_length );
5060 }
5061
5062 if( salt_length != 0 )
5063 {
5064 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
5065 salt, salt_length );
5066 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005067
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005068 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005069 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005070 tls12_prf->block_number = 0;
5071 tls12_prf->offset_in_block = hash_length;
5072
5073 return( PSA_SUCCESS );
5074}
Janos Follath71a4c912019-06-11 09:14:47 +01005075#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00005076
Janos Follath71a4c912019-06-11 09:14:47 +01005077#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005079static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
5080 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005081 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005082 size_t psk_len,
5083 psa_algorithm_t hash_alg,
5084 const uint8_t *salt,
5085 size_t salt_length,
5086 const uint8_t *label,
5087 size_t label_length )
5088{
5089 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005090 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00005091
5092 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5093 return( PSA_ERROR_INVALID_ARGUMENT );
5094
5095 /* Quoting RFC 4279, Section 2:
5096 *
5097 * The premaster secret is formed as follows: if the PSK is N octets
5098 * long, concatenate a uint16 with the value N, N zero octets, a second
5099 * uint16 with the value N, and the PSK itself.
5100 */
5101
5102 pms[0] = ( psk_len >> 8 ) & 0xff;
5103 pms[1] = ( psk_len >> 0 ) & 0xff;
5104 memset( pms + 2, 0, psk_len );
5105 pms[2 + psk_len + 0] = pms[0];
5106 pms[2 + psk_len + 1] = pms[1];
5107 memcpy( pms + 4 + psk_len, psk, psk_len );
5108
Gilles Peskinecbe66502019-05-16 16:59:18 +02005109 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005110 pms, 4 + 2 * psk_len,
5111 hash_alg,
5112 salt, salt_length,
5113 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005114
Gilles Peskine3f108122018-12-07 18:14:53 +01005115 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005116 return( status );
5117}
Janos Follath71a4c912019-06-11 09:14:47 +01005118#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01005119#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005120
Janos Follath71a4c912019-06-11 09:14:47 +01005121#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005122/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005123 * to potentially free embedded data structures and wipe confidential data.
5124 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005125static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005127 const uint8_t *secret, size_t secret_length,
5128 psa_algorithm_t alg,
5129 const uint8_t *salt, size_t salt_length,
5130 const uint8_t *label, size_t label_length,
5131 size_t capacity )
5132{
5133 psa_status_t status;
5134 size_t max_capacity;
5135
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 /* Set operation->alg even on failure so that abort knows what to do. */
5137 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005138
5139#if defined(MBEDTLS_MD_C)
5140 if( PSA_ALG_IS_HKDF( alg ) )
5141 {
5142 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5143 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5144 if( hash_size == 0 )
5145 return( PSA_ERROR_NOT_SUPPORTED );
5146 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005148 secret, secret_length,
5149 hash_alg,
5150 salt, salt_length,
5151 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005152 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00005153 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
5154 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
5155 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005156 {
5157 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5158 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5159
5160 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
5161 if( hash_alg != PSA_ALG_SHA_256 &&
5162 hash_alg != PSA_ALG_SHA_384 )
5163 {
5164 return( PSA_ERROR_NOT_SUPPORTED );
5165 }
5166
5167 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00005168
5169 if( PSA_ALG_IS_TLS12_PRF( alg ) )
5170 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005172 secret, secret_length,
5173 hash_alg, salt, salt_length,
5174 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00005175 }
5176 else
5177 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02005178 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00005180 secret, secret_length,
5181 hash_alg, salt, salt_length,
5182 label, label_length );
5183 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005184 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005185 else
5186#endif
5187 {
5188 return( PSA_ERROR_NOT_SUPPORTED );
5189 }
5190
5191 if( status != PSA_SUCCESS )
5192 return( status );
5193
5194 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005195 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005196 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005198 else
5199 return( PSA_ERROR_INVALID_ARGUMENT );
5200
5201 return( PSA_SUCCESS );
5202}
Janos Follath71a4c912019-06-11 09:14:47 +01005203#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005204
Janos Follath71a4c912019-06-11 09:14:47 +01005205#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005207 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02005208 psa_algorithm_t alg,
5209 const uint8_t *salt,
5210 size_t salt_length,
5211 const uint8_t *label,
5212 size_t label_length,
5213 size_t capacity )
5214{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005215 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005216 psa_status_t status;
5217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005219 return( PSA_ERROR_BAD_STATE );
5220
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005221 /* Make sure that alg is a key derivation algorithm. This prevents
5222 * key selection algorithms, which psa_key_derivation_internal
5223 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005224 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
5225 return( PSA_ERROR_INVALID_ARGUMENT );
5226
Gilles Peskine28f8f302019-07-24 13:30:31 +02005227 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005228 if( status != PSA_SUCCESS )
5229 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005230
Gilles Peskine8e338702019-07-30 20:06:31 +02005231 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005232 return( PSA_ERROR_INVALID_ARGUMENT );
5233
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005235 slot->data.raw.data,
5236 slot->data.raw.bytes,
5237 alg,
5238 salt, salt_length,
5239 label, label_length,
5240 capacity );
5241 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005243 return( status );
5244}
Janos Follath71a4c912019-06-11 09:14:47 +01005245#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005246
Gilles Peskine969c5d62019-01-16 15:53:06 +01005247static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005249 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250{
Janos Follath5fe19732019-06-20 15:09:30 +01005251 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5252 * initialisers for this union leave some bytes unspecified.) */
5253 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5254
Gilles Peskine969c5d62019-01-16 15:53:06 +01005255 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005257 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5258 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5259 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005260 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005261 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005262 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5263 if( hash_size == 0 )
5264 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005265 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5266 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005267 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005268 {
5269 return( PSA_ERROR_NOT_SUPPORTED );
5270 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273 }
5274#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005275 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005277}
5278
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005280 psa_algorithm_t alg )
5281{
5282 psa_status_t status;
5283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005285 return( PSA_ERROR_BAD_STATE );
5286
Gilles Peskine6843c292019-01-18 16:44:49 +01005287 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5288 return( PSA_ERROR_INVALID_ARGUMENT );
5289 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005290 {
5291 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005293 }
5294 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5295 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005297 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298 else
5299 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300
5301 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005303 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304}
5305
5306#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005307static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005308 psa_algorithm_t hash_alg,
5309 psa_key_derivation_step_t step,
5310 const uint8_t *data,
5311 size_t data_length )
5312{
5313 psa_status_t status;
5314 switch( step )
5315 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005316 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005317 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005319 status = psa_hmac_setup_internal( &hkdf->hmac,
5320 data, data_length,
5321 hash_alg );
5322 if( status != PSA_SUCCESS )
5323 return( status );
5324 hkdf->state = HKDF_STATE_STARTED;
5325 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005326 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327 /* If no salt was provided, use an empty salt. */
5328 if( hkdf->state == HKDF_STATE_INIT )
5329 {
5330 status = psa_hmac_setup_internal( &hkdf->hmac,
5331 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005332 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005333 if( status != PSA_SUCCESS )
5334 return( status );
5335 hkdf->state = HKDF_STATE_STARTED;
5336 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005337 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005339 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5340 data, data_length );
5341 if( status != PSA_SUCCESS )
5342 return( status );
5343 status = psa_hmac_finish_internal( &hkdf->hmac,
5344 hkdf->prk,
5345 sizeof( hkdf->prk ) );
5346 if( status != PSA_SUCCESS )
5347 return( status );
5348 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5349 hkdf->block_number = 0;
5350 hkdf->state = HKDF_STATE_KEYED;
5351 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005352 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005353 if( hkdf->state == HKDF_STATE_OUTPUT )
5354 return( PSA_ERROR_BAD_STATE );
5355 if( hkdf->info_set )
5356 return( PSA_ERROR_BAD_STATE );
5357 hkdf->info_length = data_length;
5358 if( data_length != 0 )
5359 {
5360 hkdf->info = mbedtls_calloc( 1, data_length );
5361 if( hkdf->info == NULL )
5362 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5363 memcpy( hkdf->info, data, data_length );
5364 }
5365 hkdf->info_set = 1;
5366 return( PSA_SUCCESS );
5367 default:
5368 return( PSA_ERROR_INVALID_ARGUMENT );
5369 }
5370}
Janos Follathb03233e2019-06-11 15:30:30 +01005371
5372#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5373static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5374 psa_algorithm_t hash_alg,
5375 psa_key_derivation_step_t step,
5376 const uint8_t *data,
5377 size_t data_length )
5378{
5379 (void) prf;
5380 (void) hash_alg;
5381 (void) step;
5382 (void) data;
5383 (void) data_length;
5384
5385 return( PSA_ERROR_INVALID_ARGUMENT );
5386}
Janos Follath6660f0e2019-06-17 08:44:03 +01005387
5388static psa_status_t psa_tls12_prf_psk_to_ms_input(
5389 psa_tls12_prf_key_derivation_t *prf,
5390 psa_algorithm_t hash_alg,
5391 psa_key_derivation_step_t step,
5392 const uint8_t *data,
5393 size_t data_length )
5394{
5395 (void) prf;
5396 (void) hash_alg;
5397 (void) step;
5398 (void) data;
5399 (void) data_length;
5400
5401 return( PSA_ERROR_INVALID_ARGUMENT );
5402}
Janos Follathb03233e2019-06-11 15:30:30 +01005403#else
Janos Follathf08e2652019-06-13 09:05:41 +01005404static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5405 const uint8_t *data,
5406 size_t data_length )
5407{
5408 if( prf->state != TLS12_PRF_STATE_INIT )
5409 return( PSA_ERROR_BAD_STATE );
5410
Janos Follathd6dce9f2019-07-04 09:11:38 +01005411 if( data_length != 0 )
5412 {
5413 prf->seed = mbedtls_calloc( 1, data_length );
5414 if( prf->seed == NULL )
5415 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005416
Janos Follathd6dce9f2019-07-04 09:11:38 +01005417 memcpy( prf->seed, data, data_length );
5418 prf->seed_length = data_length;
5419 }
Janos Follathf08e2652019-06-13 09:05:41 +01005420
5421 prf->state = TLS12_PRF_STATE_SEED_SET;
5422
5423 return( PSA_SUCCESS );
5424}
5425
Janos Follath81550542019-06-13 14:26:34 +01005426static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5427 psa_algorithm_t hash_alg,
5428 const uint8_t *data,
5429 size_t data_length )
5430{
5431 psa_status_t status;
5432 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5433 return( PSA_ERROR_BAD_STATE );
5434
5435 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438
5439 prf->state = TLS12_PRF_STATE_KEY_SET;
5440
5441 return( PSA_SUCCESS );
5442}
5443
Janos Follath6660f0e2019-06-17 08:44:03 +01005444static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5445 psa_tls12_prf_key_derivation_t *prf,
5446 psa_algorithm_t hash_alg,
5447 const uint8_t *data,
5448 size_t data_length )
5449{
5450 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005451 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5452 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005453
5454 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5455 return( PSA_ERROR_INVALID_ARGUMENT );
5456
5457 /* Quoting RFC 4279, Section 2:
5458 *
5459 * The premaster secret is formed as follows: if the PSK is N octets
5460 * long, concatenate a uint16 with the value N, N zero octets, a second
5461 * uint16 with the value N, and the PSK itself.
5462 */
5463
Janos Follath40e13932019-06-26 13:22:29 +01005464 *cur++ = ( data_length >> 8 ) & 0xff;
5465 *cur++ = ( data_length >> 0 ) & 0xff;
5466 memset( cur, 0, data_length );
5467 cur += data_length;
5468 *cur++ = pms[0];
5469 *cur++ = pms[1];
5470 memcpy( cur, data, data_length );
5471 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005472
Janos Follath40e13932019-06-26 13:22:29 +01005473 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005474
5475 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5476 return( status );
5477}
5478
Janos Follath63028dd2019-06-13 09:15:47 +01005479static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5480 const uint8_t *data,
5481 size_t data_length )
5482{
5483 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5484 return( PSA_ERROR_BAD_STATE );
5485
Janos Follathd6dce9f2019-07-04 09:11:38 +01005486 if( data_length != 0 )
5487 {
5488 prf->label = mbedtls_calloc( 1, data_length );
5489 if( prf->label == NULL )
5490 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005491
Janos Follathd6dce9f2019-07-04 09:11:38 +01005492 memcpy( prf->label, data, data_length );
5493 prf->label_length = data_length;
5494 }
Janos Follath63028dd2019-06-13 09:15:47 +01005495
5496 prf->state = TLS12_PRF_STATE_LABEL_SET;
5497
5498 return( PSA_SUCCESS );
5499}
5500
Janos Follathb03233e2019-06-11 15:30:30 +01005501static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5502 psa_algorithm_t hash_alg,
5503 psa_key_derivation_step_t step,
5504 const uint8_t *data,
5505 size_t data_length )
5506{
Janos Follathb03233e2019-06-11 15:30:30 +01005507 switch( step )
5508 {
Janos Follathf08e2652019-06-13 09:05:41 +01005509 case PSA_KEY_DERIVATION_INPUT_SEED:
5510 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005511 case PSA_KEY_DERIVATION_INPUT_SECRET:
5512 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005513 case PSA_KEY_DERIVATION_INPUT_LABEL:
5514 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005515 default:
5516 return( PSA_ERROR_INVALID_ARGUMENT );
5517 }
5518}
Janos Follath6660f0e2019-06-17 08:44:03 +01005519
5520static psa_status_t psa_tls12_prf_psk_to_ms_input(
5521 psa_tls12_prf_key_derivation_t *prf,
5522 psa_algorithm_t hash_alg,
5523 psa_key_derivation_step_t step,
5524 const uint8_t *data,
5525 size_t data_length )
5526{
5527 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005528 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005529 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5530 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005531 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005532
5533 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5534}
Janos Follathb03233e2019-06-11 15:30:30 +01005535#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005536#endif /* MBEDTLS_MD_C */
5537
Janos Follathb80a94e2019-06-12 15:54:46 +01005538static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005540 psa_key_derivation_step_t step,
5541 const uint8_t *data,
5542 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005543{
5544 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005546
5547#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005548 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005549 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005551 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005552 step, data, data_length );
5553 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005554 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005555#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005556#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005557 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005558 {
Janos Follathb03233e2019-06-11 15:30:30 +01005559 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5560 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5561 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005562 }
5563 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5564 {
5565 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5566 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5567 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005568 }
5569 else
5570#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573 return( PSA_ERROR_BAD_STATE );
5574 }
5575
5576 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005578 return( status );
5579}
5580
Janos Follath51f4a0f2019-06-14 11:35:55 +01005581psa_status_t psa_key_derivation_input_bytes(
5582 psa_key_derivation_operation_t *operation,
5583 psa_key_derivation_step_t step,
5584 const uint8_t *data,
5585 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005586{
Janos Follathc5621512019-06-14 11:27:57 +01005587 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5588 return( PSA_ERROR_INVALID_ARGUMENT );
5589
5590 return( psa_key_derivation_input_internal( operation, step,
5591 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005592}
5593
Janos Follath51f4a0f2019-06-14 11:35:55 +01005594psa_status_t psa_key_derivation_input_key(
5595 psa_key_derivation_operation_t *operation,
5596 psa_key_derivation_step_t step,
5597 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005598{
5599 psa_key_slot_t *slot;
5600 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005601 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005602 PSA_KEY_USAGE_DERIVE,
5603 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005604 if( status != PSA_SUCCESS )
5605 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005606 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005607 return( PSA_ERROR_INVALID_ARGUMENT );
5608 /* Don't allow a key to be used as an input that is usually public.
5609 * This is debatable. It's ok from a cryptographic perspective to
5610 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005611 * the material should be dedicated to a particular input step,
5612 * otherwise this may allow the key to be used in an unintended way
5613 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005614 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005615 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005616 return( psa_key_derivation_input_internal( operation,
5617 step,
5618 slot->data.raw.data,
5619 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005620}
5621
Gilles Peskineea0fb492018-07-12 17:17:20 +02005622
5623
5624/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005625/* Key agreement */
5626/****************************************************************/
5627
Gilles Peskinea05219c2018-11-16 16:02:56 +01005628#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005629static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5630 size_t peer_key_length,
5631 const mbedtls_ecp_keypair *our_key,
5632 uint8_t *shared_secret,
5633 size_t shared_secret_size,
5634 size_t *shared_secret_length )
5635{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005636 mbedtls_ecp_keypair *their_key = NULL;
5637 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005638 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005639 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005640
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005641 status = psa_import_ec_public_key(
5642 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5643 peer_key, peer_key_length,
5644 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005645 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005646 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005647
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005648 status = mbedtls_to_psa_error(
5649 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5650 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005651 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005652 status = mbedtls_to_psa_error(
5653 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5654 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005655 goto exit;
5656
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005657 status = mbedtls_to_psa_error(
5658 mbedtls_ecdh_calc_secret( &ecdh,
5659 shared_secret_length,
5660 shared_secret, shared_secret_size,
5661 mbedtls_ctr_drbg_random,
5662 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005663
5664exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005665 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005666 mbedtls_ecp_keypair_free( their_key );
5667 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005668 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005669}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005670#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005671
Gilles Peskine01d718c2018-09-18 12:01:02 +02005672#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5673
Gilles Peskine0216fe12019-04-11 21:23:21 +02005674static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5675 psa_key_slot_t *private_key,
5676 const uint8_t *peer_key,
5677 size_t peer_key_length,
5678 uint8_t *shared_secret,
5679 size_t shared_secret_size,
5680 size_t *shared_secret_length )
5681{
5682 switch( alg )
5683 {
5684#if defined(MBEDTLS_ECDH_C)
5685 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005686 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005687 return( PSA_ERROR_INVALID_ARGUMENT );
5688 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5689 private_key->data.ecp,
5690 shared_secret, shared_secret_size,
5691 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005692#endif /* MBEDTLS_ECDH_C */
5693 default:
5694 (void) private_key;
5695 (void) peer_key;
5696 (void) peer_key_length;
5697 (void) shared_secret;
5698 (void) shared_secret_size;
5699 (void) shared_secret_length;
5700 return( PSA_ERROR_NOT_SUPPORTED );
5701 }
5702}
5703
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005704/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005705 * to potentially free embedded data structures and wipe confidential data.
5706 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005707static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005708 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005709 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005710 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005711 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005712{
5713 psa_status_t status;
5714 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5715 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005716 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005717
5718 /* Step 1: run the secret agreement algorithm to generate the shared
5719 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005720 status = psa_key_agreement_raw_internal( ka_alg,
5721 private_key,
5722 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005723 shared_secret,
5724 sizeof( shared_secret ),
5725 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005726 if( status != PSA_SUCCESS )
5727 goto exit;
5728
5729 /* Step 2: set up the key derivation to generate key material from
5730 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005731 status = psa_key_derivation_input_internal( operation, step,
5732 shared_secret,
5733 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005734
Gilles Peskine01d718c2018-09-18 12:01:02 +02005735exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005736 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005737 return( status );
5738}
5739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005741 psa_key_derivation_step_t step,
5742 psa_key_handle_t private_key,
5743 const uint8_t *peer_key,
5744 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005745{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005746 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005747 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005749 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005750 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005751 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005752 if( status != PSA_SUCCESS )
5753 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005755 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005756 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005757 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005759 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005760}
5761
Gilles Peskinebe697d82019-05-16 18:00:41 +02005762psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5763 psa_key_handle_t private_key,
5764 const uint8_t *peer_key,
5765 size_t peer_key_length,
5766 uint8_t *output,
5767 size_t output_size,
5768 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005769{
5770 psa_key_slot_t *slot;
5771 psa_status_t status;
5772
5773 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5774 {
5775 status = PSA_ERROR_INVALID_ARGUMENT;
5776 goto exit;
5777 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005778 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005779 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005780 if( status != PSA_SUCCESS )
5781 goto exit;
5782
5783 status = psa_key_agreement_raw_internal( alg, slot,
5784 peer_key, peer_key_length,
5785 output, output_size,
5786 output_length );
5787
5788exit:
5789 if( status != PSA_SUCCESS )
5790 {
5791 /* If an error happens and is not handled properly, the output
5792 * may be used as a key to protect sensitive data. Arrange for such
5793 * a key to be random, which is likely to result in decryption or
5794 * verification errors. This is better than filling the buffer with
5795 * some constant data such as zeros, which would result in the data
5796 * being protected with a reproducible, easily knowable key.
5797 */
5798 psa_generate_random( output, output_size );
5799 *output_length = output_size;
5800 }
5801 return( status );
5802}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005803
5804
5805/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005806/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005807/****************************************************************/
5808
5809psa_status_t psa_generate_random( uint8_t *output,
5810 size_t output_size )
5811{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005812 int ret;
5813 GUARD_MODULE_INITIALIZED;
5814
Gilles Peskinef181eca2019-08-07 13:49:00 +02005815 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5816 {
5817 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5818 output,
5819 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5820 if( ret != 0 )
5821 return( mbedtls_to_psa_error( ret ) );
5822 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5823 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5824 }
5825
itayzafrir0adf0fc2018-09-06 16:24:41 +03005826 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005827 return( mbedtls_to_psa_error( ret ) );
5828}
5829
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005830#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5831#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005832
Gilles Peskine7228da22019-07-15 11:06:15 +02005833psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005834 size_t seed_size )
5835{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005836 if( global_data.initialized )
5837 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005838
5839 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5840 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5841 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5842 return( PSA_ERROR_INVALID_ARGUMENT );
5843
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005844 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005845}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005846#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005847
Gilles Peskinee56e8782019-04-26 17:34:02 +02005848#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5849static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5850 size_t domain_parameters_size,
5851 int *exponent )
5852{
5853 size_t i;
5854 uint32_t acc = 0;
5855
5856 if( domain_parameters_size == 0 )
5857 {
5858 *exponent = 65537;
5859 return( PSA_SUCCESS );
5860 }
5861
5862 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5863 * support values that fit in a 32-bit integer, which is larger than
5864 * int on just about every platform anyway. */
5865 if( domain_parameters_size > sizeof( acc ) )
5866 return( PSA_ERROR_NOT_SUPPORTED );
5867 for( i = 0; i < domain_parameters_size; i++ )
5868 acc = ( acc << 8 ) | domain_parameters[i];
5869 if( acc > INT_MAX )
5870 return( PSA_ERROR_NOT_SUPPORTED );
5871 *exponent = acc;
5872 return( PSA_SUCCESS );
5873}
5874#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5875
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005876static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005877 psa_key_slot_t *slot, size_t bits,
5878 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005879{
Gilles Peskine8e338702019-07-30 20:06:31 +02005880 psa_key_type_t type = slot->attr.type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005881
Gilles Peskinee56e8782019-04-26 17:34:02 +02005882 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005883 return( PSA_ERROR_INVALID_ARGUMENT );
5884
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005885 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005886 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005887 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005888 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005889 if( status != PSA_SUCCESS )
5890 return( status );
5891 status = psa_generate_random( slot->data.raw.data,
5892 slot->data.raw.bytes );
5893 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005895#if defined(MBEDTLS_DES_C)
5896 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005897 psa_des_set_key_parity( slot->data.raw.data,
5898 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005899#endif /* MBEDTLS_DES_C */
5900 }
5901 else
5902
5903#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005904 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005905 {
5906 mbedtls_rsa_context *rsa;
5907 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005908 int exponent;
5909 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005910 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5911 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005912 /* Accept only byte-aligned keys, for the same reasons as
5913 * in psa_import_rsa_key(). */
5914 if( bits % 8 != 0 )
5915 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005916 status = psa_read_rsa_exponent( domain_parameters,
5917 domain_parameters_size,
5918 &exponent );
5919 if( status != PSA_SUCCESS )
5920 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005921 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5922 if( rsa == NULL )
5923 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5924 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5925 ret = mbedtls_rsa_gen_key( rsa,
5926 mbedtls_ctr_drbg_random,
5927 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005928 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005929 exponent );
5930 if( ret != 0 )
5931 {
5932 mbedtls_rsa_free( rsa );
5933 mbedtls_free( rsa );
5934 return( mbedtls_to_psa_error( ret ) );
5935 }
5936 slot->data.rsa = rsa;
5937 }
5938 else
5939#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5940
5941#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005942 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005943 {
5944 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5945 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5946 const mbedtls_ecp_curve_info *curve_info =
5947 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5948 mbedtls_ecp_keypair *ecp;
5949 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005950 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005951 return( PSA_ERROR_NOT_SUPPORTED );
5952 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5953 return( PSA_ERROR_NOT_SUPPORTED );
5954 if( curve_info->bit_size != bits )
5955 return( PSA_ERROR_INVALID_ARGUMENT );
5956 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5957 if( ecp == NULL )
5958 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5959 mbedtls_ecp_keypair_init( ecp );
5960 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5961 mbedtls_ctr_drbg_random,
5962 &global_data.ctr_drbg );
5963 if( ret != 0 )
5964 {
5965 mbedtls_ecp_keypair_free( ecp );
5966 mbedtls_free( ecp );
5967 return( mbedtls_to_psa_error( ret ) );
5968 }
5969 slot->data.ecp = ecp;
5970 }
5971 else
5972#endif /* MBEDTLS_ECP_C */
5973
5974 return( PSA_ERROR_NOT_SUPPORTED );
5975
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005976 return( PSA_SUCCESS );
5977}
5978
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005979psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005980 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005981{
5982 psa_status_t status;
5983 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005984 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005985
Gilles Peskinedf179142019-07-15 22:02:14 +02005986 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5987 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005988 if( status != PSA_SUCCESS )
5989 goto exit;
5990
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005991#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5992 if( driver != NULL )
5993 {
Gilles Peskine11792082019-08-06 18:36:36 +02005994 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5995 size_t pubkey_length = 0; /* We don't support this feature yet */
5996 if( drv->key_management == NULL ||
5997 drv->key_management->p_generate == NULL )
5998 {
5999 status = PSA_ERROR_NOT_SUPPORTED;
6000 goto exit;
6001 }
6002 status = drv->key_management->p_generate(
6003 psa_get_se_driver_context( driver ),
6004 slot->data.se.slot_number, attributes,
6005 NULL, 0, &pubkey_length );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006006 }
Gilles Peskine11792082019-08-06 18:36:36 +02006007 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006008#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006009 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006010 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006011 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006012 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006013 }
Gilles Peskine11792082019-08-06 18:36:36 +02006014
6015exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006016 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006017 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006018 if( status != PSA_SUCCESS )
6019 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006020 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006021 *handle = 0;
6022 }
6023 return( status );
6024}
6025
6026
Gilles Peskine05d69892018-06-19 22:00:52 +02006027
6028/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01006029/* Module setup */
6030/****************************************************************/
6031
Gilles Peskine5e769522018-11-20 21:59:56 +01006032psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6033 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6034 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6035{
6036 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6037 return( PSA_ERROR_BAD_STATE );
6038 global_data.entropy_init = entropy_init;
6039 global_data.entropy_free = entropy_free;
6040 return( PSA_SUCCESS );
6041}
6042
Gilles Peskinee59236f2018-01-27 23:32:46 +01006043void mbedtls_psa_crypto_free( void )
6044{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006045 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006046 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6047 {
6048 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006049 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006050 }
6051 /* Wipe all remaining data, including configuration.
6052 * In particular, this sets all state indicator to the value
6053 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006054 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006055#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006056 /* Unregister all secure element drivers, so that we restart from
6057 * a pristine state. */
6058 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006060}
6061
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006062#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6063/** Recover a transaction that was interrupted by a power failure.
6064 *
6065 * This function is called during initialization, before psa_crypto_init()
6066 * returns. If this function returns a failure status, the initialization
6067 * fails.
6068 */
6069static psa_status_t psa_crypto_recover_transaction(
6070 const psa_crypto_transaction_t *transaction )
6071{
6072 switch( transaction->unknown.type )
6073 {
6074 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6075 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
6076 /* TOnogrepDO - fall through to the failure case until this
6077 * is implemented */
6078 default:
6079 /* We found an unsupported transaction in the storage.
6080 * We don't know what state the storage is in. Give up. */
6081 return( PSA_ERROR_STORAGE_FAILURE );
6082 }
6083}
6084#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6085
Gilles Peskinee59236f2018-01-27 23:32:46 +01006086psa_status_t psa_crypto_init( void )
6087{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006088 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006089 const unsigned char drbg_seed[] = "PSA";
6090
Gilles Peskinec6b69072018-11-20 21:42:52 +01006091 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006092 if( global_data.initialized != 0 )
6093 return( PSA_SUCCESS );
6094
Gilles Peskine5e769522018-11-20 21:59:56 +01006095 /* Set default configuration if
6096 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6097 if( global_data.entropy_init == NULL )
6098 global_data.entropy_init = mbedtls_entropy_init;
6099 if( global_data.entropy_free == NULL )
6100 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101
Gilles Peskinec6b69072018-11-20 21:42:52 +01006102 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006103 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006104#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6105 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6106 /* The PSA entropy injection feature depends on using NV seed as an entropy
6107 * source. Add NV seed as an entropy source for PSA entropy injection. */
6108 mbedtls_entropy_add_source( &global_data.entropy,
6109 mbedtls_nv_seed_poll, NULL,
6110 MBEDTLS_ENTROPY_BLOCK_SIZE,
6111 MBEDTLS_ENTROPY_SOURCE_STRONG );
6112#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006114 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006115 status = mbedtls_to_psa_error(
6116 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6117 mbedtls_entropy_func,
6118 &global_data.entropy,
6119 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6120 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006121 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006122 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006123
Gilles Peskine66fb1262018-12-10 16:29:04 +01006124 status = psa_initialize_key_slots( );
6125 if( status != PSA_SUCCESS )
6126 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006127
Gilles Peskine4b734222019-07-24 15:56:31 +02006128#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006129 status = psa_crypto_load_transaction( );
6130 if( status == PSA_SUCCESS )
6131 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006132 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6133 if( status != PSA_SUCCESS )
6134 goto exit;
6135 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006136 }
6137 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6138 {
6139 /* There's no transaction to complete. It's all good. */
6140 status = PSA_SUCCESS;
6141 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006142#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006143
Gilles Peskinec6b69072018-11-20 21:42:52 +01006144 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01006145 global_data.initialized = 1;
6146
Gilles Peskinee59236f2018-01-27 23:32:46 +01006147exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006148 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006149 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006150 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151}
6152
6153#endif /* MBEDTLS_PSA_CRYPTO_C */