blob: 787b5a737d3a067c0b91d8389823a5c958a0d0c0 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/xtea.h"
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
205 return( PSA_ERROR_BAD_STATE );
206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
216 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
218 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
219 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
220 return( PSA_ERROR_NOT_SUPPORTED );
221 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
223
224 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_GCM_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
246 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
247 return( PSA_ERROR_NOT_SUPPORTED );
248 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MD_ALLOC_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
252 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
253 return( PSA_ERROR_STORAGE_FAILURE );
254 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
258 return( PSA_ERROR_STORAGE_FAILURE );
259 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
264 return( PSA_ERROR_BUFFER_TOO_SMALL );
265 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 case MBEDTLS_ERR_PK_ALLOC_FAILED:
275 return( PSA_ERROR_INSUFFICIENT_MEMORY );
276 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
277 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
282 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
287 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
288 return( PSA_ERROR_NOT_PERMITTED );
289 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_PK_INVALID_ALG:
292 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
293 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
296 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100297 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
303 return( PSA_ERROR_NOT_SUPPORTED );
304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
308 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_RSA_INVALID_PADDING:
311 return( PSA_ERROR_INVALID_PADDING );
312 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
313 return( PSA_ERROR_HARDWARE_FAILURE );
314 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
317 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200318 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
320 return( PSA_ERROR_INVALID_SIGNATURE );
321 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
322 return( PSA_ERROR_BUFFER_TOO_SMALL );
323 case MBEDTLS_ERR_RSA_RNG_FAILED:
324 return( PSA_ERROR_INSUFFICIENT_MEMORY );
325 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329
330 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339
itayzafrir5c753392018-05-08 11:18:38 +0300340 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300342 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
344 return( PSA_ERROR_BUFFER_TOO_SMALL );
345 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
348 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
352 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200375static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
376{
377 switch( grpid )
378 {
379 case MBEDTLS_ECP_DP_SECP192R1:
380 return( PSA_ECC_CURVE_SECP192R1 );
381 case MBEDTLS_ECP_DP_SECP224R1:
382 return( PSA_ECC_CURVE_SECP224R1 );
383 case MBEDTLS_ECP_DP_SECP256R1:
384 return( PSA_ECC_CURVE_SECP256R1 );
385 case MBEDTLS_ECP_DP_SECP384R1:
386 return( PSA_ECC_CURVE_SECP384R1 );
387 case MBEDTLS_ECP_DP_SECP521R1:
388 return( PSA_ECC_CURVE_SECP521R1 );
389 case MBEDTLS_ECP_DP_BP256R1:
390 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
391 case MBEDTLS_ECP_DP_BP384R1:
392 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
393 case MBEDTLS_ECP_DP_BP512R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
395 case MBEDTLS_ECP_DP_CURVE25519:
396 return( PSA_ECC_CURVE_CURVE25519 );
397 case MBEDTLS_ECP_DP_SECP192K1:
398 return( PSA_ECC_CURVE_SECP192K1 );
399 case MBEDTLS_ECP_DP_SECP224K1:
400 return( PSA_ECC_CURVE_SECP224K1 );
401 case MBEDTLS_ECP_DP_SECP256K1:
402 return( PSA_ECC_CURVE_SECP256K1 );
403 case MBEDTLS_ECP_DP_CURVE448:
404 return( PSA_ECC_CURVE_CURVE448 );
405 default:
406 return( 0 );
407 }
408}
409
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
411{
412 switch( curve )
413 {
414 case PSA_ECC_CURVE_SECP192R1:
415 return( MBEDTLS_ECP_DP_SECP192R1 );
416 case PSA_ECC_CURVE_SECP224R1:
417 return( MBEDTLS_ECP_DP_SECP224R1 );
418 case PSA_ECC_CURVE_SECP256R1:
419 return( MBEDTLS_ECP_DP_SECP256R1 );
420 case PSA_ECC_CURVE_SECP384R1:
421 return( MBEDTLS_ECP_DP_SECP384R1 );
422 case PSA_ECC_CURVE_SECP521R1:
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
425 return( MBEDTLS_ECP_DP_BP256R1 );
426 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
427 return( MBEDTLS_ECP_DP_BP384R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
429 return( MBEDTLS_ECP_DP_BP512R1 );
430 case PSA_ECC_CURVE_CURVE25519:
431 return( MBEDTLS_ECP_DP_CURVE25519 );
432 case PSA_ECC_CURVE_SECP192K1:
433 return( MBEDTLS_ECP_DP_SECP192K1 );
434 case PSA_ECC_CURVE_SECP224K1:
435 return( MBEDTLS_ECP_DP_SECP224K1 );
436 case PSA_ECC_CURVE_SECP256K1:
437 return( MBEDTLS_ECP_DP_SECP256K1 );
438 case PSA_ECC_CURVE_CURVE448:
439 return( MBEDTLS_ECP_DP_CURVE448 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100444#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200445
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
447 size_t bits,
448 struct raw_data *raw )
449{
450 /* Check that the bit size is acceptable for the key type */
451 switch( type )
452 {
453 case PSA_KEY_TYPE_RAW_DATA:
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 Amerocd09d8c2019-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 Amerocd09d8c2019-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 Amerocd09d8c2019-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 Amerocd09d8c2019-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 Amerocd09d8c2019-01-11 17:53:05 +0000562 status = PSA_ERROR_INVALID_ARGUMENT;
563 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564 }
Jaeden Amerocd09d8c2019-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 Ameroccdce902019-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 Ameroccdce902019-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 Ameroccdce902019-01-10 11:42:27 +0000636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000639 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640}
Jaeden Ameroccdce902019-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 Peskine2c86ebc2019-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 ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 }
778 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100779#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-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 Amerocd09d8c2019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Ameroccdce902019-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 }
Darryl Green940d72c2018-07-13 13:18:51 +0100792
Gilles Peskineb46bef22019-07-30 21:32:04 +0200793 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 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100800 return( status );
801}
802
Gilles Peskinef603c712019-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 Peskine549ea862019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-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
815 * if one is a wildcard. If so the other has the specific algorithm. */
816 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 );
822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskined6f371b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskine549ea862019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-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 Peskine549ea862019-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 Peskined6f371b2019-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 Peskined6f371b2019-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 */
871static 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 Peskined6f371b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
884 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-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 ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000958 {
959 /* No key material to clean. */
960 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100997static void psa_abort_operations_using_key( psa_key_slot_t *slot )
998{
Janos Follath1d57a202019-08-13 12:15:34 +0100999 /*TODO how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001000 (void) slot;
1001}
1002
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001003/** Completely wipe a slot in memory, including its policy.
1004 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001005psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001006{
1007 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001008 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001009 /* At this point, key material and other type-specific content has
1010 * been wiped. Clear remaining metadata. We can call memset and not
1011 * zeroize because the metadata is not particularly sensitive. */
1012 memset( slot, 0, sizeof( *slot ) );
1013 return( status );
1014}
1015
Gilles Peskinec5487a82018-12-03 18:08:14 +01001016psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001018 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001019 psa_status_t status = PSA_SUCCESS;
1020 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001021#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1022 psa_se_drv_table_entry_t *driver;
1023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024
Gilles Peskinec5487a82018-12-03 18:08:14 +01001025 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026 if( status != PSA_SUCCESS )
1027 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001028
1029#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001031 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001032 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001033 /* For a key in a secure element, we need to do three things:
1034 * remove the key file in internal storage, destroy the
1035 * key inside the secure element, and update the driver's
1036 * persistent data. Start a transaction that will encompass these
1037 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001041 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 status = psa_crypto_save_transaction( );
1043 if( status != PSA_SUCCESS )
1044 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001045 (void) psa_crypto_stop_transaction( );
Janos Follath1d57a202019-08-13 12:15:34 +01001046 /* TODO: destroy what can be destroyed anyway */
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 return( status );
1048 }
1049
Gilles Peskine354f7672019-07-12 23:46:38 +02001050 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1053
Darryl Greend49a4992018-06-18 17:27:26 +01001054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001055 if( slot->attr.lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +01001056 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001057 storage_status =
Gilles Peskine8e338702019-07-30 20:06:31 +02001058 psa_destroy_persistent_key( slot->attr.id );
Darryl Greend49a4992018-06-18 17:27:26 +01001059 }
1060#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001061
Gilles Peskinefc762652019-07-22 19:30:34 +02001062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1063 if( driver != NULL )
1064 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001065 psa_status_t status2;
1066 status = psa_save_se_persistent_data( driver );
1067 status2 = psa_crypto_stop_transaction( );
1068 if( status == PSA_SUCCESS )
1069 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001070 if( status != PSA_SUCCESS )
1071 {
Janos Follath1d57a202019-08-13 12:15:34 +01001072 /* TODO: destroy what can be destroyed anyway */
Gilles Peskinefc762652019-07-22 19:30:34 +02001073 return( status );
1074 }
1075 }
1076#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1077
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001078 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001079 if( status != PSA_SUCCESS )
1080 return( status );
1081 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082}
1083
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001084void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001085{
Gilles Peskineb699f072019-04-26 16:06:02 +02001086 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001088}
1089
Gilles Peskineb699f072019-04-26 16:06:02 +02001090psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1091 psa_key_type_t type,
1092 const uint8_t *data,
1093 size_t data_length )
1094{
1095 uint8_t *copy = NULL;
1096
1097 if( data_length != 0 )
1098 {
1099 copy = mbedtls_calloc( 1, data_length );
1100 if( copy == NULL )
1101 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1102 memcpy( copy, data, data_length );
1103 }
1104 /* After this point, this function is guaranteed to succeed, so it
1105 * can start modifying `*attributes`. */
1106
1107 if( attributes->domain_parameters != NULL )
1108 {
1109 mbedtls_free( attributes->domain_parameters );
1110 attributes->domain_parameters = NULL;
1111 attributes->domain_parameters_size = 0;
1112 }
1113
1114 attributes->domain_parameters = copy;
1115 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001116 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001117 return( PSA_SUCCESS );
1118}
1119
1120psa_status_t psa_get_key_domain_parameters(
1121 const psa_key_attributes_t *attributes,
1122 uint8_t *data, size_t data_size, size_t *data_length )
1123{
1124 if( attributes->domain_parameters_size > data_size )
1125 return( PSA_ERROR_BUFFER_TOO_SMALL );
1126 *data_length = attributes->domain_parameters_size;
1127 if( attributes->domain_parameters_size != 0 )
1128 memcpy( data, attributes->domain_parameters,
1129 attributes->domain_parameters_size );
1130 return( PSA_SUCCESS );
1131}
1132
1133#if defined(MBEDTLS_RSA_C)
1134static psa_status_t psa_get_rsa_public_exponent(
1135 const mbedtls_rsa_context *rsa,
1136 psa_key_attributes_t *attributes )
1137{
1138 mbedtls_mpi mpi;
1139 int ret;
1140 uint8_t *buffer = NULL;
1141 size_t buflen;
1142 mbedtls_mpi_init( &mpi );
1143
1144 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1145 if( ret != 0 )
1146 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001147 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1148 {
1149 /* It's the default value, which is reported as an empty string,
1150 * so there's nothing to do. */
1151 goto exit;
1152 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001153
1154 buflen = mbedtls_mpi_size( &mpi );
1155 buffer = mbedtls_calloc( 1, buflen );
1156 if( buffer == NULL )
1157 {
1158 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1159 goto exit;
1160 }
1161 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1162 if( ret != 0 )
1163 goto exit;
1164 attributes->domain_parameters = buffer;
1165 attributes->domain_parameters_size = buflen;
1166
1167exit:
1168 mbedtls_mpi_free( &mpi );
1169 if( ret != 0 )
1170 mbedtls_free( buffer );
1171 return( mbedtls_to_psa_error( ret ) );
1172}
1173#endif /* MBEDTLS_RSA_C */
1174
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001175/** Retrieve all the publicly-accessible attributes of a key.
1176 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1178 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001179{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001180 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001181 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001182
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 psa_reset_key_attributes( attributes );
1184
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001185 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001186 if( status != PSA_SUCCESS )
1187 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001188
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001189 attributes->core = slot->attr;
Gilles Peskineb699f072019-04-26 16:06:02 +02001190
Gilles Peskine8e338702019-07-30 20:06:31 +02001191 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001192 {
1193#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001194 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001195 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001196#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001197 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001198 * is not yet implemented. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001199 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001200 break;
1201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001202 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1203 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001204#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 default:
1206 /* Nothing else to do. */
1207 break;
1208 }
1209
1210 if( status != PSA_SUCCESS )
1211 psa_reset_key_attributes( attributes );
1212 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213}
1214
Jaeden Ameroccdce902019-01-10 11:42:27 +00001215#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001216static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1217 unsigned char *buf, size_t size )
1218{
1219 int ret;
1220 unsigned char *c;
1221 size_t len = 0;
1222
1223 c = buf + size;
1224
1225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1226
1227 return( (int) len );
1228}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001229#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001230
Gilles Peskinef603c712019-01-19 13:40:11 +01001231static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1232 uint8_t *data,
1233 size_t data_size,
1234 size_t *data_length,
1235 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236{
Gilles Peskine5d309672019-07-12 23:47:28 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1238 const psa_drv_se_t *drv;
1239 psa_drv_se_context_t *drv_context;
1240#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1241
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001242 *data_length = 0;
1243
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001245 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001246
Gilles Peskine5d309672019-07-12 23:47:28 +02001247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001248 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001249 {
1250 psa_drv_se_export_key_t method;
1251 if( drv->key_management == NULL )
1252 return( PSA_ERROR_NOT_SUPPORTED );
1253 method = ( export_public_key ?
1254 drv->key_management->p_export_public :
1255 drv->key_management->p_export );
1256 if( method == NULL )
1257 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001258 return( method( drv_context,
1259 slot->data.se.slot_number,
1260 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001261 }
1262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1263
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 {
1266 if( slot->data.raw.bytes > data_size )
1267 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001268 if( data_size != 0 )
1269 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001270 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001271 memset( data + slot->data.raw.bytes, 0,
1272 data_size - slot->data.raw.bytes );
1273 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274 *data_length = slot->data.raw.bytes;
1275 return( PSA_SUCCESS );
1276 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001277#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001278 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001279 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001280 psa_status_t status;
1281
Gilles Peskineb46bef22019-07-30 21:32:04 +02001282 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001283 if( bytes > data_size )
1284 return( PSA_ERROR_BUFFER_TOO_SMALL );
1285 status = mbedtls_to_psa_error(
1286 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1287 if( status != PSA_SUCCESS )
1288 return( status );
1289 memset( data + bytes, 0, data_size - bytes );
1290 *data_length = bytes;
1291 return( PSA_SUCCESS );
1292 }
1293#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294 else
Moran Peker17e36e12018-05-02 12:55:20 +03001295 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001296#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001297 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1298 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001299 {
Moran Pekera998bc62018-04-16 18:16:20 +03001300 mbedtls_pk_context pk;
1301 int ret;
Gilles Peskine8e338702019-07-30 20:06:31 +02001302 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001303 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001304#if defined(MBEDTLS_RSA_C)
1305 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001306 pk.pk_info = &mbedtls_rsa_info;
1307 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001308#else
1309 return( PSA_ERROR_NOT_SUPPORTED );
1310#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001311 }
1312 else
1313 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001314#if defined(MBEDTLS_ECP_C)
1315 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001316 pk.pk_info = &mbedtls_eckey_info;
1317 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001318#else
1319 return( PSA_ERROR_NOT_SUPPORTED );
1320#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001321 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001323 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001324 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001325 }
Moran Peker17e36e12018-05-02 12:55:20 +03001326 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001327 {
Moran Peker17e36e12018-05-02 12:55:20 +03001328 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001329 }
Moran Peker60364322018-04-29 11:34:58 +03001330 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001331 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001332 /* If data_size is 0 then data may be NULL and then the
1333 * call to memset would have undefined behavior. */
1334 if( data_size != 0 )
1335 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001336 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001337 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001338 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1339 * Move the data to the beginning and erase remaining data
1340 * at the original location. */
1341 if( 2 * (size_t) ret <= data_size )
1342 {
1343 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001344 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001345 }
1346 else if( (size_t) ret < data_size )
1347 {
1348 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001349 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001350 }
Moran Pekera998bc62018-04-16 18:16:20 +03001351 *data_length = ret;
1352 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001353 }
1354 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001355#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001356 {
1357 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001358 it is valid for a special-purpose implementation to omit
1359 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001360 return( PSA_ERROR_NOT_SUPPORTED );
1361 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362 }
1363}
1364
Gilles Peskinec5487a82018-12-03 18:08:14 +01001365psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001366 uint8_t *data,
1367 size_t data_size,
1368 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001369{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001370 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001371 psa_status_t status;
1372
1373 /* Set the key to empty now, so that even when there are errors, we always
1374 * set data_length to a value between 0 and data_size. On error, setting
1375 * the key to empty is a good choice because an empty key representation is
1376 * unlikely to be accepted anywhere. */
1377 *data_length = 0;
1378
1379 /* Export requires the EXPORT flag. There is an exception for public keys,
1380 * which don't require any flag, but psa_get_key_from_slot takes
1381 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001382 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001383 if( status != PSA_SUCCESS )
1384 return( status );
1385 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001386 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001387}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388
Gilles Peskinec5487a82018-12-03 18:08:14 +01001389psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001390 uint8_t *data,
1391 size_t data_size,
1392 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001393{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001394 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001395 psa_status_t status;
1396
1397 /* Set the key to empty now, so that even when there are errors, we always
1398 * set data_length to a value between 0 and data_size. On error, setting
1399 * the key to empty is a good choice because an empty key representation is
1400 * unlikely to be accepted anywhere. */
1401 *data_length = 0;
1402
1403 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001404 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001405 if( status != PSA_SUCCESS )
1406 return( status );
1407 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001408 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001409}
1410
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001411/** Validate that a key policy is internally well-formed.
1412 *
1413 * This function only rejects invalid policies. It does not validate the
1414 * consistency of the policy with respect to other attributes of the key
1415 * such as the key type.
1416 */
1417static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001418{
1419 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001420 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001421 PSA_KEY_USAGE_ENCRYPT |
1422 PSA_KEY_USAGE_DECRYPT |
1423 PSA_KEY_USAGE_SIGN |
1424 PSA_KEY_USAGE_VERIFY |
1425 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1426 return( PSA_ERROR_INVALID_ARGUMENT );
1427
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 return( PSA_SUCCESS );
1429}
1430
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001431/** Validate the internal consistency of key attributes.
1432 *
1433 * This function only rejects invalid attribute values. If does not
1434 * validate the consistency of the attributes with any key data that may
1435 * be involved in the creation of the key.
1436 *
1437 * Call this function early in the key creation process.
1438 *
1439 * \param[in] attributes Key attributes for the new key.
1440 * \param[out] p_drv On any return, the driver for the key, if any.
1441 * NULL for a transparent key.
1442 *
1443 */
1444static psa_status_t psa_validate_key_attributes(
1445 const psa_key_attributes_t *attributes,
1446 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001447{
1448 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001449
1450 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001451 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001452 status = psa_validate_persistent_key_parameters(
1453 attributes->core.lifetime, attributes->core.id,
1454 p_drv, 1 );
1455 if( status != PSA_SUCCESS )
1456 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001457 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001458
1459 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001460 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001461 return( status );
1462
1463 /* Refuse to create overly large keys.
1464 * Note that this doesn't trigger on import if the attributes don't
1465 * explicitly specify a size (so psa_get_key_bits returns 0), so
1466 * psa_import_key() needs its own checks. */
1467 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1468 return( PSA_ERROR_NOT_SUPPORTED );
1469
1470 return( PSA_SUCCESS );
1471}
1472
Gilles Peskine4747d192019-04-17 15:05:45 +02001473/** Prepare a key slot to receive key material.
1474 *
1475 * This function allocates a key slot and sets its metadata.
1476 *
1477 * If this function fails, call psa_fail_key_creation().
1478 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001479 * This function is intended to be used as follows:
1480 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1481 * it with the specified attributes, and assign it a handle.
1482 * -# Populate the slot with the key material.
1483 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1484 * In case of failure at any step, stop the sequence and call
1485 * psa_fail_key_creation().
1486 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001487 * \param[in] attributes Key attributes for the new key.
1488 * \param[out] handle On success, a handle for the allocated slot.
1489 * \param[out] p_slot On success, a pointer to the prepared slot.
1490 * \param[out] p_drv On any return, the driver for the key, if any.
1491 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001492 *
1493 * \retval #PSA_SUCCESS
1494 * The key slot is ready to receive key material.
1495 * \return If this function fails, the key slot is an invalid state.
1496 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001497 */
1498static psa_status_t psa_start_key_creation(
1499 const psa_key_attributes_t *attributes,
1500 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001501 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001502 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001503{
1504 psa_status_t status;
1505 psa_key_slot_t *slot;
1506
Gilles Peskine011e4282019-06-26 18:34:38 +02001507 *p_drv = NULL;
1508
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509 status = psa_validate_key_attributes( attributes, p_drv );
1510 if( status != PSA_SUCCESS )
1511 return( status );
1512
Gilles Peskine267c6562019-05-27 19:01:54 +02001513 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001514 if( status != PSA_SUCCESS )
1515 return( status );
1516 slot = *p_slot;
1517
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001518 /* We're storing the declared bit-size of the key. It's up to each
1519 * creation mechanism to verify that this information is correct.
1520 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001521 * an input (generate, device) but not for those where the bit-size
1522 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001523
1524 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001525
Gilles Peskinecbaff462019-07-12 23:46:04 +02001526#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001527 /* For a key in a secure element, we need to do three things:
1528 * create the key file in internal storage, create the
1529 * key inside the secure element, and update the driver's
1530 * persistent data. Start a transaction that will encompass these
1531 * three actions. */
1532 /* The first thing to do is to find a slot number for the new key.
1533 * We save the slot number in persistent storage as part of the
1534 * transaction data. It will be needed to recover if the power
1535 * fails during the key creation process, to clean up on the secure
1536 * element side after restarting. Obtaining a slot number from the
1537 * secure element driver updates its persistent state, but we do not yet
1538 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001539 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001540 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001541 {
Gilles Peskinecbaff462019-07-12 23:46:04 +02001542 status = psa_find_se_slot_for_key( attributes, *p_drv,
1543 &slot->data.se.slot_number );
1544 if( status != PSA_SUCCESS )
1545 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001546 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001547 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001548 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001549 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001550 status = psa_crypto_save_transaction( );
1551 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001552 {
1553 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001554 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001555 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001556 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001557#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1558
Darryl Green0c6575a2018-11-07 16:05:30 +00001559 return( status );
1560}
Gilles Peskine4747d192019-04-17 15:05:45 +02001561
1562/** Finalize the creation of a key once its key material has been set.
1563 *
1564 * This entails writing the key to persistent storage.
1565 *
1566 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001567 * See the documentation of psa_start_key_creation() for the intended use
1568 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001570 * \param[in,out] slot Pointer to the slot with key material.
1571 * \param[in] driver The secure element driver for the key,
1572 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001573 *
1574 * \retval #PSA_SUCCESS
1575 * The key was successfully created. The handle is now valid.
1576 * \return If this function fails, the key slot is an invalid state.
1577 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001579static psa_status_t psa_finish_key_creation(
1580 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001581 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001582{
1583 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001584 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001585 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001586
1587#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001588 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001589 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001590#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1591 if( driver != NULL )
1592 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001593 psa_se_key_data_storage_t data;
1594#if defined(static_assert)
1595 static_assert( sizeof( slot->data.se.slot_number ) ==
1596 sizeof( data.slot_number ),
1597 "Slot number size does not match psa_se_key_data_storage_t" );
1598 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1599 "Bit-size size does not match psa_se_key_data_storage_t" );
1600#endif
1601 memcpy( &data.slot_number, &slot->data.se.slot_number,
1602 sizeof( slot->data.se.slot_number ) );
1603 memcpy( &data.bits, &slot->attr.bits,
1604 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001605 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001606 (uint8_t*) &data,
1607 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001608 }
1609 else
1610#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1611 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001612 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001613 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001614 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001615 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1616 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001617 if( buffer == NULL && buffer_size != 0 )
1618 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1619 status = psa_internal_export_key( slot,
1620 buffer, buffer_size, &length,
1621 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001622 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001623 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001624 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001625
Gilles Peskine1df83d42019-07-23 16:13:14 +02001626 if( buffer_size != 0 )
1627 mbedtls_platform_zeroize( buffer, buffer_size );
1628 mbedtls_free( buffer );
1629 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001630 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001631#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1632
Gilles Peskinecbaff462019-07-12 23:46:04 +02001633#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1634 if( driver != NULL )
1635 {
1636 status = psa_save_se_persistent_data( driver );
1637 if( status != PSA_SUCCESS )
1638 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001639 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001640 return( status );
1641 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001642 status = psa_crypto_stop_transaction( );
1643 if( status != PSA_SUCCESS )
1644 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001645 }
1646#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1647
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 return( status );
1649}
1650
1651/** Abort the creation of a key.
1652 *
1653 * You may call this function after calling psa_start_key_creation(),
1654 * or after psa_finish_key_creation() fails. In other circumstances, this
1655 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001656 * See the documentation of psa_start_key_creation() for the intended use
1657 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001659 * \param[in,out] slot Pointer to the slot with key material.
1660 * \param[in] driver The secure element driver for the key,
1661 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001662 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001663static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001664 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001665{
Gilles Peskine011e4282019-06-26 18:34:38 +02001666 (void) driver;
1667
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 if( slot == NULL )
1669 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001670
1671#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001672 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001673 * element, and the failure happened later (when saving metadata
1674 * to internal storage), we need to destroy the key in the secure
1675 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001676
1677 /* Abort the ongoing transaction if any. We already did what it
1678 * takes to undo any partial creation. All that's left is to update
1679 * the transaction data itself. */
1680 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001681#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1682
Gilles Peskine4747d192019-04-17 15:05:45 +02001683 psa_wipe_key_slot( slot );
1684}
1685
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001686/** Validate optional attributes during key creation.
1687 *
1688 * Some key attributes are optional during key creation. If they are
1689 * specified in the attributes structure, check that they are consistent
1690 * with the data in the slot.
1691 *
1692 * This function should be called near the end of key creation, after
1693 * the slot in memory is fully populated but before saving persistent data.
1694 */
1695static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001696 const psa_key_slot_t *slot,
1697 const psa_key_attributes_t *attributes )
1698{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001699 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001700 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001701 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001702 return( PSA_ERROR_INVALID_ARGUMENT );
1703 }
1704
1705 if( attributes->domain_parameters_size != 0 )
1706 {
1707#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001708 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001709 {
1710 mbedtls_mpi actual, required;
1711 int ret;
1712 mbedtls_mpi_init( &actual );
1713 mbedtls_mpi_init( &required );
1714 ret = mbedtls_rsa_export( slot->data.rsa,
1715 NULL, NULL, NULL, NULL, &actual );
1716 if( ret != 0 )
1717 goto rsa_exit;
1718 ret = mbedtls_mpi_read_binary( &required,
1719 attributes->domain_parameters,
1720 attributes->domain_parameters_size );
1721 if( ret != 0 )
1722 goto rsa_exit;
1723 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1724 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1725 rsa_exit:
1726 mbedtls_mpi_free( &actual );
1727 mbedtls_mpi_free( &required );
1728 if( ret != 0)
1729 return( mbedtls_to_psa_error( ret ) );
1730 }
1731 else
1732#endif
1733 {
1734 return( PSA_ERROR_INVALID_ARGUMENT );
1735 }
1736 }
1737
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001738 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001739 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001740 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001741 return( PSA_ERROR_INVALID_ARGUMENT );
1742 }
1743
1744 return( PSA_SUCCESS );
1745}
1746
Gilles Peskine4747d192019-04-17 15:05:45 +02001747psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001749 size_t data_length,
1750 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001751{
1752 psa_status_t status;
1753 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001754 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001755
Gilles Peskine011e4282019-06-26 18:34:38 +02001756 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001757 if( status != PSA_SUCCESS )
1758 goto exit;
1759
Gilles Peskine5d309672019-07-12 23:47:28 +02001760#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1761 if( driver != NULL )
1762 {
1763 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001764 size_t bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001765 if( drv->key_management == NULL ||
1766 drv->key_management->p_import == NULL )
1767 {
1768 status = PSA_ERROR_NOT_SUPPORTED;
1769 goto exit;
1770 }
1771 status = drv->key_management->p_import(
1772 psa_get_se_driver_context( driver ),
1773 slot->data.se.slot_number,
Gilles Peskine8e338702019-07-30 20:06:31 +02001774 slot->attr.lifetime, slot->attr.type,
1775 slot->attr.policy.alg, slot->attr.policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001776 data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001777 &bits );
1778 if( status != PSA_SUCCESS )
1779 goto exit;
1780 if( bits > PSA_MAX_KEY_BITS )
1781 {
1782 status = PSA_ERROR_NOT_SUPPORTED;
1783 goto exit;
1784 }
1785 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001786 }
1787 else
1788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1789 {
1790 status = psa_import_key_into_slot( slot, data, data_length );
1791 if( status != PSA_SUCCESS )
1792 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001793 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001794 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001795 if( status != PSA_SUCCESS )
1796 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001797
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001799exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 if( status != PSA_SUCCESS )
1801 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001802 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 *handle = 0;
1804 }
1805 return( status );
1806}
1807
Gilles Peskinef603c712019-01-19 13:40:11 +01001808static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001809 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001810{
1811 psa_status_t status;
1812 uint8_t *buffer = NULL;
1813 size_t buffer_size = 0;
1814 size_t length;
1815
Gilles Peskine8e338702019-07-30 20:06:31 +02001816 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001817 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001818 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001819 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001821 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1822 if( status != PSA_SUCCESS )
1823 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001824 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001825 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001826
1827exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001828 if( buffer_size != 0 )
1829 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001830 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001831 return( status );
1832}
1833
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001834psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1835 const psa_key_attributes_t *specified_attributes,
1836 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001837{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001838 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001839 psa_key_slot_t *source_slot = NULL;
1840 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001841 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001842 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001843
Gilles Peskine28f8f302019-07-24 13:30:31 +02001844 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001845 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001846 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001847 goto exit;
1848
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001849 status = psa_validate_optional_attributes( source_slot,
1850 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 if( status != PSA_SUCCESS )
1852 goto exit;
1853
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001854 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001855 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001856 if( status != PSA_SUCCESS )
1857 goto exit;
1858
1859 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001861 if( status != PSA_SUCCESS )
1862 goto exit;
1863
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001864#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1865 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001866 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001867 /* Copying to a secure element is not implemented yet. */
1868 status = PSA_ERROR_NOT_SUPPORTED;
1869 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001870 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001871#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001872
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001873 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001874 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001876
Gilles Peskine011e4282019-06-26 18:34:38 +02001877 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001878exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001879 if( status != PSA_SUCCESS )
1880 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001881 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001882 *target_handle = 0;
1883 }
1884 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001885}
1886
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001887
1888
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001889/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001890/* Message digests */
1891/****************************************************************/
1892
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001893static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001894{
1895 switch( alg )
1896 {
1897#if defined(MBEDTLS_MD2_C)
1898 case PSA_ALG_MD2:
1899 return( &mbedtls_md2_info );
1900#endif
1901#if defined(MBEDTLS_MD4_C)
1902 case PSA_ALG_MD4:
1903 return( &mbedtls_md4_info );
1904#endif
1905#if defined(MBEDTLS_MD5_C)
1906 case PSA_ALG_MD5:
1907 return( &mbedtls_md5_info );
1908#endif
1909#if defined(MBEDTLS_RIPEMD160_C)
1910 case PSA_ALG_RIPEMD160:
1911 return( &mbedtls_ripemd160_info );
1912#endif
1913#if defined(MBEDTLS_SHA1_C)
1914 case PSA_ALG_SHA_1:
1915 return( &mbedtls_sha1_info );
1916#endif
1917#if defined(MBEDTLS_SHA256_C)
1918 case PSA_ALG_SHA_224:
1919 return( &mbedtls_sha224_info );
1920 case PSA_ALG_SHA_256:
1921 return( &mbedtls_sha256_info );
1922#endif
1923#if defined(MBEDTLS_SHA512_C)
1924 case PSA_ALG_SHA_384:
1925 return( &mbedtls_sha384_info );
1926 case PSA_ALG_SHA_512:
1927 return( &mbedtls_sha512_info );
1928#endif
1929 default:
1930 return( NULL );
1931 }
1932}
1933
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001934psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1935{
1936 switch( operation->alg )
1937 {
Gilles Peskine81736312018-06-26 15:04:31 +02001938 case 0:
1939 /* The object has (apparently) been initialized but it is not
1940 * in use. It's ok to call abort on such an object, and there's
1941 * nothing to do. */
1942 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001943#if defined(MBEDTLS_MD2_C)
1944 case PSA_ALG_MD2:
1945 mbedtls_md2_free( &operation->ctx.md2 );
1946 break;
1947#endif
1948#if defined(MBEDTLS_MD4_C)
1949 case PSA_ALG_MD4:
1950 mbedtls_md4_free( &operation->ctx.md4 );
1951 break;
1952#endif
1953#if defined(MBEDTLS_MD5_C)
1954 case PSA_ALG_MD5:
1955 mbedtls_md5_free( &operation->ctx.md5 );
1956 break;
1957#endif
1958#if defined(MBEDTLS_RIPEMD160_C)
1959 case PSA_ALG_RIPEMD160:
1960 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1961 break;
1962#endif
1963#if defined(MBEDTLS_SHA1_C)
1964 case PSA_ALG_SHA_1:
1965 mbedtls_sha1_free( &operation->ctx.sha1 );
1966 break;
1967#endif
1968#if defined(MBEDTLS_SHA256_C)
1969 case PSA_ALG_SHA_224:
1970 case PSA_ALG_SHA_256:
1971 mbedtls_sha256_free( &operation->ctx.sha256 );
1972 break;
1973#endif
1974#if defined(MBEDTLS_SHA512_C)
1975 case PSA_ALG_SHA_384:
1976 case PSA_ALG_SHA_512:
1977 mbedtls_sha512_free( &operation->ctx.sha512 );
1978 break;
1979#endif
1980 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001981 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001982 }
1983 operation->alg = 0;
1984 return( PSA_SUCCESS );
1985}
1986
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001987psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001988 psa_algorithm_t alg )
1989{
1990 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001991
1992 /* A context must be freshly initialized before it can be set up. */
1993 if( operation->alg != 0 )
1994 {
1995 return( PSA_ERROR_BAD_STATE );
1996 }
1997
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001998 switch( alg )
1999 {
2000#if defined(MBEDTLS_MD2_C)
2001 case PSA_ALG_MD2:
2002 mbedtls_md2_init( &operation->ctx.md2 );
2003 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2004 break;
2005#endif
2006#if defined(MBEDTLS_MD4_C)
2007 case PSA_ALG_MD4:
2008 mbedtls_md4_init( &operation->ctx.md4 );
2009 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2010 break;
2011#endif
2012#if defined(MBEDTLS_MD5_C)
2013 case PSA_ALG_MD5:
2014 mbedtls_md5_init( &operation->ctx.md5 );
2015 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2016 break;
2017#endif
2018#if defined(MBEDTLS_RIPEMD160_C)
2019 case PSA_ALG_RIPEMD160:
2020 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2021 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2022 break;
2023#endif
2024#if defined(MBEDTLS_SHA1_C)
2025 case PSA_ALG_SHA_1:
2026 mbedtls_sha1_init( &operation->ctx.sha1 );
2027 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2028 break;
2029#endif
2030#if defined(MBEDTLS_SHA256_C)
2031 case PSA_ALG_SHA_224:
2032 mbedtls_sha256_init( &operation->ctx.sha256 );
2033 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2034 break;
2035 case PSA_ALG_SHA_256:
2036 mbedtls_sha256_init( &operation->ctx.sha256 );
2037 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2038 break;
2039#endif
2040#if defined(MBEDTLS_SHA512_C)
2041 case PSA_ALG_SHA_384:
2042 mbedtls_sha512_init( &operation->ctx.sha512 );
2043 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2044 break;
2045 case PSA_ALG_SHA_512:
2046 mbedtls_sha512_init( &operation->ctx.sha512 );
2047 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2048 break;
2049#endif
2050 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002051 return( PSA_ALG_IS_HASH( alg ) ?
2052 PSA_ERROR_NOT_SUPPORTED :
2053 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002054 }
2055 if( ret == 0 )
2056 operation->alg = alg;
2057 else
2058 psa_hash_abort( operation );
2059 return( mbedtls_to_psa_error( ret ) );
2060}
2061
2062psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2063 const uint8_t *input,
2064 size_t input_length )
2065{
2066 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02002067
2068 /* Don't require hash implementations to behave correctly on a
2069 * zero-length input, which may have an invalid pointer. */
2070 if( input_length == 0 )
2071 return( PSA_SUCCESS );
2072
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002073 switch( operation->alg )
2074 {
2075#if defined(MBEDTLS_MD2_C)
2076 case PSA_ALG_MD2:
2077 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2078 input, input_length );
2079 break;
2080#endif
2081#if defined(MBEDTLS_MD4_C)
2082 case PSA_ALG_MD4:
2083 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2084 input, input_length );
2085 break;
2086#endif
2087#if defined(MBEDTLS_MD5_C)
2088 case PSA_ALG_MD5:
2089 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2090 input, input_length );
2091 break;
2092#endif
2093#if defined(MBEDTLS_RIPEMD160_C)
2094 case PSA_ALG_RIPEMD160:
2095 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2096 input, input_length );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA1_C)
2100 case PSA_ALG_SHA_1:
2101 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2102 input, input_length );
2103 break;
2104#endif
2105#if defined(MBEDTLS_SHA256_C)
2106 case PSA_ALG_SHA_224:
2107 case PSA_ALG_SHA_256:
2108 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2109 input, input_length );
2110 break;
2111#endif
2112#if defined(MBEDTLS_SHA512_C)
2113 case PSA_ALG_SHA_384:
2114 case PSA_ALG_SHA_512:
2115 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2116 input, input_length );
2117 break;
2118#endif
2119 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002120 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002122
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 if( ret != 0 )
2124 psa_hash_abort( operation );
2125 return( mbedtls_to_psa_error( ret ) );
2126}
2127
2128psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2129 uint8_t *hash,
2130 size_t hash_size,
2131 size_t *hash_length )
2132{
itayzafrir40835d42018-08-02 13:14:17 +03002133 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002135 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002136
2137 /* Fill the output buffer with something that isn't a valid hash
2138 * (barring an attack on the hash and deliberately-crafted input),
2139 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002140 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002141 /* If hash_size is 0 then hash may be NULL and then the
2142 * call to memset would have undefined behavior. */
2143 if( hash_size != 0 )
2144 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145
2146 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002147 {
2148 status = PSA_ERROR_BUFFER_TOO_SMALL;
2149 goto exit;
2150 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151
2152 switch( operation->alg )
2153 {
2154#if defined(MBEDTLS_MD2_C)
2155 case PSA_ALG_MD2:
2156 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2157 break;
2158#endif
2159#if defined(MBEDTLS_MD4_C)
2160 case PSA_ALG_MD4:
2161 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2162 break;
2163#endif
2164#if defined(MBEDTLS_MD5_C)
2165 case PSA_ALG_MD5:
2166 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2167 break;
2168#endif
2169#if defined(MBEDTLS_RIPEMD160_C)
2170 case PSA_ALG_RIPEMD160:
2171 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2172 break;
2173#endif
2174#if defined(MBEDTLS_SHA1_C)
2175 case PSA_ALG_SHA_1:
2176 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2177 break;
2178#endif
2179#if defined(MBEDTLS_SHA256_C)
2180 case PSA_ALG_SHA_224:
2181 case PSA_ALG_SHA_256:
2182 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2183 break;
2184#endif
2185#if defined(MBEDTLS_SHA512_C)
2186 case PSA_ALG_SHA_384:
2187 case PSA_ALG_SHA_512:
2188 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2189 break;
2190#endif
2191 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002192 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 }
itayzafrir40835d42018-08-02 13:14:17 +03002194 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195
itayzafrir40835d42018-08-02 13:14:17 +03002196exit:
2197 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002199 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002200 return( psa_hash_abort( operation ) );
2201 }
2202 else
2203 {
2204 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002205 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206 }
2207}
2208
Gilles Peskine2d277862018-06-18 15:41:12 +02002209psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2210 const uint8_t *hash,
2211 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212{
2213 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2214 size_t actual_hash_length;
2215 psa_status_t status = psa_hash_finish( operation,
2216 actual_hash, sizeof( actual_hash ),
2217 &actual_hash_length );
2218 if( status != PSA_SUCCESS )
2219 return( status );
2220 if( actual_hash_length != hash_length )
2221 return( PSA_ERROR_INVALID_SIGNATURE );
2222 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2223 return( PSA_ERROR_INVALID_SIGNATURE );
2224 return( PSA_SUCCESS );
2225}
2226
Gilles Peskineeb35d782019-01-22 17:56:16 +01002227psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2228 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002229{
2230 if( target_operation->alg != 0 )
2231 return( PSA_ERROR_BAD_STATE );
2232
2233 switch( source_operation->alg )
2234 {
2235 case 0:
2236 return( PSA_ERROR_BAD_STATE );
2237#if defined(MBEDTLS_MD2_C)
2238 case PSA_ALG_MD2:
2239 mbedtls_md2_clone( &target_operation->ctx.md2,
2240 &source_operation->ctx.md2 );
2241 break;
2242#endif
2243#if defined(MBEDTLS_MD4_C)
2244 case PSA_ALG_MD4:
2245 mbedtls_md4_clone( &target_operation->ctx.md4,
2246 &source_operation->ctx.md4 );
2247 break;
2248#endif
2249#if defined(MBEDTLS_MD5_C)
2250 case PSA_ALG_MD5:
2251 mbedtls_md5_clone( &target_operation->ctx.md5,
2252 &source_operation->ctx.md5 );
2253 break;
2254#endif
2255#if defined(MBEDTLS_RIPEMD160_C)
2256 case PSA_ALG_RIPEMD160:
2257 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2258 &source_operation->ctx.ripemd160 );
2259 break;
2260#endif
2261#if defined(MBEDTLS_SHA1_C)
2262 case PSA_ALG_SHA_1:
2263 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2264 &source_operation->ctx.sha1 );
2265 break;
2266#endif
2267#if defined(MBEDTLS_SHA256_C)
2268 case PSA_ALG_SHA_224:
2269 case PSA_ALG_SHA_256:
2270 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2271 &source_operation->ctx.sha256 );
2272 break;
2273#endif
2274#if defined(MBEDTLS_SHA512_C)
2275 case PSA_ALG_SHA_384:
2276 case PSA_ALG_SHA_512:
2277 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2278 &source_operation->ctx.sha512 );
2279 break;
2280#endif
2281 default:
2282 return( PSA_ERROR_NOT_SUPPORTED );
2283 }
2284
2285 target_operation->alg = source_operation->alg;
2286 return( PSA_SUCCESS );
2287}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288
2289
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291/* MAC */
2292/****************************************************************/
2293
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002294static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002295 psa_algorithm_t alg,
2296 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002297 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002298 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002300 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002301 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002302
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002303 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002304 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002305
Gilles Peskine8c9def32018-02-08 10:02:12 +01002306 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2307 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002308 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002309 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002310 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002311 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312 mode = MBEDTLS_MODE_STREAM;
2313 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002314 case PSA_ALG_CTR:
2315 mode = MBEDTLS_MODE_CTR;
2316 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002317 case PSA_ALG_CFB:
2318 mode = MBEDTLS_MODE_CFB;
2319 break;
2320 case PSA_ALG_OFB:
2321 mode = MBEDTLS_MODE_OFB;
2322 break;
2323 case PSA_ALG_CBC_NO_PADDING:
2324 mode = MBEDTLS_MODE_CBC;
2325 break;
2326 case PSA_ALG_CBC_PKCS7:
2327 mode = MBEDTLS_MODE_CBC;
2328 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002329 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330 mode = MBEDTLS_MODE_CCM;
2331 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002332 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333 mode = MBEDTLS_MODE_GCM;
2334 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002335 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2336 mode = MBEDTLS_MODE_CHACHAPOLY;
2337 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338 default:
2339 return( NULL );
2340 }
2341 }
2342 else if( alg == PSA_ALG_CMAC )
2343 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344 else
2345 return( NULL );
2346
2347 switch( key_type )
2348 {
2349 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002350 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351 break;
2352 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002353 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2354 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002356 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002357 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002358 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002359 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2360 * but two-key Triple-DES is functionally three-key Triple-DES
2361 * with K1=K3, so that's how we present it to mbedtls. */
2362 if( key_bits == 128 )
2363 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364 break;
2365 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002366 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002367 break;
2368 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002369 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002370 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002371 case PSA_KEY_TYPE_CHACHA20:
2372 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2373 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374 default:
2375 return( NULL );
2376 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002377 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002378 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002379
Jaeden Amero23bbb752018-06-26 14:16:54 +01002380 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2381 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002382}
2383
Gilles Peskinea05219c2018-11-16 16:02:56 +01002384#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002385static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002386{
Gilles Peskine2d277862018-06-18 15:41:12 +02002387 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002388 {
2389 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002390 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002391 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002392 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002393 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002394 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002395 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002396 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002397 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002398 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002399 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002400 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002401 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002402 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002403 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002404 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002405 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002406 return( 128 );
2407 default:
2408 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002409 }
2410}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002411#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002412
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002413/* Initialize the MAC operation structure. Once this function has been
2414 * called, psa_mac_abort can run and will do the right thing. */
2415static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2416 psa_algorithm_t alg )
2417{
2418 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2419
2420 operation->alg = alg;
2421 operation->key_set = 0;
2422 operation->iv_set = 0;
2423 operation->iv_required = 0;
2424 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002425 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002426
2427#if defined(MBEDTLS_CMAC_C)
2428 if( alg == PSA_ALG_CMAC )
2429 {
2430 operation->iv_required = 0;
2431 mbedtls_cipher_init( &operation->ctx.cmac );
2432 status = PSA_SUCCESS;
2433 }
2434 else
2435#endif /* MBEDTLS_CMAC_C */
2436#if defined(MBEDTLS_MD_C)
2437 if( PSA_ALG_IS_HMAC( operation->alg ) )
2438 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002439 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2440 operation->ctx.hmac.hash_ctx.alg = 0;
2441 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002442 }
2443 else
2444#endif /* MBEDTLS_MD_C */
2445 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002446 if( ! PSA_ALG_IS_MAC( alg ) )
2447 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002448 }
2449
2450 if( status != PSA_SUCCESS )
2451 memset( operation, 0, sizeof( *operation ) );
2452 return( status );
2453}
2454
Gilles Peskine01126fa2018-07-12 17:04:55 +02002455#if defined(MBEDTLS_MD_C)
2456static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2457{
Gilles Peskine3f108122018-12-07 18:14:53 +01002458 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002459 return( psa_hash_abort( &hmac->hash_ctx ) );
2460}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002461
Janos Follath999f6482019-06-11 12:04:10 +01002462#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002463static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2464{
2465 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2466 memset( hmac, 0, sizeof( *hmac ) );
2467}
Janos Follath999f6482019-06-11 12:04:10 +01002468#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002469#endif /* MBEDTLS_MD_C */
2470
Gilles Peskine8c9def32018-02-08 10:02:12 +01002471psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2472{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002473 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002474 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002475 /* The object has (apparently) been initialized but it is not
2476 * in use. It's ok to call abort on such an object, and there's
2477 * nothing to do. */
2478 return( PSA_SUCCESS );
2479 }
2480 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002482 if( operation->alg == PSA_ALG_CMAC )
2483 {
2484 mbedtls_cipher_free( &operation->ctx.cmac );
2485 }
2486 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002489 if( PSA_ALG_IS_HMAC( operation->alg ) )
2490 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002491 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002492 }
2493 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002495 {
2496 /* Sanity check (shouldn't happen: operation->alg should
2497 * always have been initialized to a valid value). */
2498 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499 }
Moran Peker41deec42018-04-04 15:43:05 +03002500
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 operation->alg = 0;
2502 operation->key_set = 0;
2503 operation->iv_set = 0;
2504 operation->iv_required = 0;
2505 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002506 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002507
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002509
2510bad_state:
2511 /* If abort is called on an uninitialized object, we can't trust
2512 * anything. Wipe the object in case it contains confidential data.
2513 * This may result in a memory leak if a pointer gets overwritten,
2514 * but it's too late to do anything about this. */
2515 memset( operation, 0, sizeof( *operation ) );
2516 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517}
2518
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002519#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002521 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002522 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002523 const mbedtls_cipher_info_t *cipher_info )
2524{
2525 int ret;
2526
2527 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002528
2529 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2530 if( ret != 0 )
2531 return( ret );
2532
2533 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2534 slot->data.raw.data,
2535 key_bits );
2536 return( ret );
2537}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002538#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002539
Gilles Peskine248051a2018-06-20 16:09:38 +02002540#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002541static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2542 const uint8_t *key,
2543 size_t key_length,
2544 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002545{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002546 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002547 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002548 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002549 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002550 psa_status_t status;
2551
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002552 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2553 * overflow below. This should never trigger if the hash algorithm
2554 * is implemented correctly. */
2555 /* The size checks against the ipad and opad buffers cannot be written
2556 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2557 * because that triggers -Wlogical-op on GCC 7.3. */
2558 if( block_size > sizeof( ipad ) )
2559 return( PSA_ERROR_NOT_SUPPORTED );
2560 if( block_size > sizeof( hmac->opad ) )
2561 return( PSA_ERROR_NOT_SUPPORTED );
2562 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002563 return( PSA_ERROR_NOT_SUPPORTED );
2564
Gilles Peskined223b522018-06-11 18:12:58 +02002565 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002566 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002567 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002568 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002569 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002570 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002571 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002572 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002573 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002574 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002575 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002576 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002577 }
Gilles Peskine96889972018-07-12 17:07:03 +02002578 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2579 * but it is permitted. It is common when HMAC is used in HKDF, for
2580 * example. Don't call `memcpy` in the 0-length because `key` could be
2581 * an invalid pointer which would make the behavior undefined. */
2582 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002583 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002584
Gilles Peskined223b522018-06-11 18:12:58 +02002585 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2586 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002587 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002588 ipad[i] ^= 0x36;
2589 memset( ipad + key_length, 0x36, block_size - key_length );
2590
2591 /* Copy the key material from ipad to opad, flipping the requisite bits,
2592 * and filling the rest of opad with the requisite constant. */
2593 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002594 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2595 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002596
Gilles Peskine01126fa2018-07-12 17:04:55 +02002597 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002598 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002599 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002600
Gilles Peskine01126fa2018-07-12 17:04:55 +02002601 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002602
2603cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002604 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002605
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002606 return( status );
2607}
Gilles Peskine248051a2018-06-20 16:09:38 +02002608#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002609
Gilles Peskine89167cb2018-07-08 20:12:23 +02002610static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002611 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002612 psa_algorithm_t alg,
2613 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002616 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002618 psa_key_usage_t usage =
2619 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002620 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002621 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002623 /* A context must be freshly initialized before it can be set up. */
2624 if( operation->alg != 0 )
2625 {
2626 return( PSA_ERROR_BAD_STATE );
2627 }
2628
Gilles Peskined911eb72018-08-14 15:18:45 +02002629 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630 if( status != PSA_SUCCESS )
2631 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632 if( is_sign )
2633 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634
Gilles Peskine28f8f302019-07-24 13:30:31 +02002635 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002636 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002637 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002638 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002639
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002641 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002642 {
2643 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002644 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002645 slot->attr.type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646 int ret;
2647 if( cipher_info == NULL )
2648 {
2649 status = PSA_ERROR_NOT_SUPPORTED;
2650 goto exit;
2651 }
2652 operation->mac_size = cipher_info->block_size;
2653 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2654 status = mbedtls_to_psa_error( ret );
2655 }
2656 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002659 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002660 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002661 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002662 if( hash_alg == 0 )
2663 {
2664 status = PSA_ERROR_NOT_SUPPORTED;
2665 goto exit;
2666 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002667
2668 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2669 /* Sanity check. This shouldn't fail on a valid configuration. */
2670 if( operation->mac_size == 0 ||
2671 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2672 {
2673 status = PSA_ERROR_NOT_SUPPORTED;
2674 goto exit;
2675 }
2676
Gilles Peskine8e338702019-07-30 20:06:31 +02002677 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002678 {
2679 status = PSA_ERROR_INVALID_ARGUMENT;
2680 goto exit;
2681 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002682
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2684 slot->data.raw.data,
2685 slot->data.raw.bytes,
2686 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002687 }
2688 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002690 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002691 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002692 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694
Gilles Peskined911eb72018-08-14 15:18:45 +02002695 if( truncated == 0 )
2696 {
2697 /* The "normal" case: untruncated algorithm. Nothing to do. */
2698 }
2699 else if( truncated < 4 )
2700 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002701 /* A very short MAC is too short for security since it can be
2702 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2703 * so we make this our minimum, even though 32 bits is still
2704 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002705 status = PSA_ERROR_NOT_SUPPORTED;
2706 }
2707 else if( truncated > operation->mac_size )
2708 {
2709 /* It's impossible to "truncate" to a larger length. */
2710 status = PSA_ERROR_INVALID_ARGUMENT;
2711 }
2712 else
2713 operation->mac_size = truncated;
2714
Gilles Peskinefbfac682018-07-08 20:51:54 +02002715exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002718 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002720 else
2721 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722 operation->key_set = 1;
2723 }
2724 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725}
2726
Gilles Peskine89167cb2018-07-08 20:12:23 +02002727psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002728 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002729 psa_algorithm_t alg )
2730{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002731 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002732}
2733
2734psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002735 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002736 psa_algorithm_t alg )
2737{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002738 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002739}
2740
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2742 const uint8_t *input,
2743 size_t input_length )
2744{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002745 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002747 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002749 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750 operation->has_input = 1;
2751
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002753 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002754 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002755 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2756 input, input_length );
2757 status = mbedtls_to_psa_error( ret );
2758 }
2759 else
2760#endif /* MBEDTLS_CMAC_C */
2761#if defined(MBEDTLS_MD_C)
2762 if( PSA_ALG_IS_HMAC( operation->alg ) )
2763 {
2764 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2765 input_length );
2766 }
2767 else
2768#endif /* MBEDTLS_MD_C */
2769 {
2770 /* This shouldn't happen if `operation` was initialized by
2771 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002772 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002773 }
2774
Gilles Peskinefbfac682018-07-08 20:51:54 +02002775 if( status != PSA_SUCCESS )
2776 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002777 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778}
2779
Gilles Peskine01126fa2018-07-12 17:04:55 +02002780#if defined(MBEDTLS_MD_C)
2781static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2782 uint8_t *mac,
2783 size_t mac_size )
2784{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002785 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002786 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2787 size_t hash_size = 0;
2788 size_t block_size = psa_get_hash_block_size( hash_alg );
2789 psa_status_t status;
2790
Gilles Peskine01126fa2018-07-12 17:04:55 +02002791 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2792 if( status != PSA_SUCCESS )
2793 return( status );
2794 /* From here on, tmp needs to be wiped. */
2795
2796 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2797 if( status != PSA_SUCCESS )
2798 goto exit;
2799
2800 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2801 if( status != PSA_SUCCESS )
2802 goto exit;
2803
2804 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2805 if( status != PSA_SUCCESS )
2806 goto exit;
2807
Gilles Peskined911eb72018-08-14 15:18:45 +02002808 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2809 if( status != PSA_SUCCESS )
2810 goto exit;
2811
2812 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002813
2814exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002815 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002816 return( status );
2817}
2818#endif /* MBEDTLS_MD_C */
2819
mohammad16036df908f2018-04-02 08:34:15 -07002820static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002821 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002822 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002824 if( ! operation->key_set )
2825 return( PSA_ERROR_BAD_STATE );
2826 if( operation->iv_required && ! operation->iv_set )
2827 return( PSA_ERROR_BAD_STATE );
2828
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 if( mac_size < operation->mac_size )
2830 return( PSA_ERROR_BUFFER_TOO_SMALL );
2831
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002833 if( operation->alg == PSA_ALG_CMAC )
2834 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002835 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2836 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2837 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002838 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002839 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002840 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 else
2843#endif /* MBEDTLS_CMAC_C */
2844#if defined(MBEDTLS_MD_C)
2845 if( PSA_ALG_IS_HMAC( operation->alg ) )
2846 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002847 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002848 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002849 }
2850 else
2851#endif /* MBEDTLS_MD_C */
2852 {
2853 /* This shouldn't happen if `operation` was initialized by
2854 * a setup function. */
2855 return( PSA_ERROR_BAD_STATE );
2856 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857}
2858
Gilles Peskineacd4be32018-07-08 19:56:25 +02002859psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2860 uint8_t *mac,
2861 size_t mac_size,
2862 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002863{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002864 psa_status_t status;
2865
Jaeden Amero252ef282019-02-15 14:05:35 +00002866 if( operation->alg == 0 )
2867 {
2868 return( PSA_ERROR_BAD_STATE );
2869 }
2870
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002871 /* Fill the output buffer with something that isn't a valid mac
2872 * (barring an attack on the mac and deliberately-crafted input),
2873 * in case the caller doesn't check the return status properly. */
2874 *mac_length = mac_size;
2875 /* If mac_size is 0 then mac may be NULL and then the
2876 * call to memset would have undefined behavior. */
2877 if( mac_size != 0 )
2878 memset( mac, '!', mac_size );
2879
Gilles Peskine89167cb2018-07-08 20:12:23 +02002880 if( ! operation->is_sign )
2881 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002882 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002883 }
mohammad16036df908f2018-04-02 08:34:15 -07002884
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002885 status = psa_mac_finish_internal( operation, mac, mac_size );
2886
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002887 if( status == PSA_SUCCESS )
2888 {
2889 status = psa_mac_abort( operation );
2890 if( status == PSA_SUCCESS )
2891 *mac_length = operation->mac_size;
2892 else
2893 memset( mac, '!', mac_size );
2894 }
2895 else
2896 psa_mac_abort( operation );
2897 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002898}
2899
Gilles Peskineacd4be32018-07-08 19:56:25 +02002900psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2901 const uint8_t *mac,
2902 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903{
Gilles Peskine828ed142018-06-18 23:25:51 +02002904 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002905 psa_status_t status;
2906
Jaeden Amero252ef282019-02-15 14:05:35 +00002907 if( operation->alg == 0 )
2908 {
2909 return( PSA_ERROR_BAD_STATE );
2910 }
2911
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912 if( operation->is_sign )
2913 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002914 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002915 }
2916 if( operation->mac_size != mac_length )
2917 {
2918 status = PSA_ERROR_INVALID_SIGNATURE;
2919 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920 }
mohammad16036df908f2018-04-02 08:34:15 -07002921
2922 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002923 actual_mac, sizeof( actual_mac ) );
2924
2925 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2926 status = PSA_ERROR_INVALID_SIGNATURE;
2927
2928cleanup:
2929 if( status == PSA_SUCCESS )
2930 status = psa_mac_abort( operation );
2931 else
2932 psa_mac_abort( operation );
2933
Gilles Peskine3f108122018-12-07 18:14:53 +01002934 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002935
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002936 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937}
2938
2939
Gilles Peskine20035e32018-02-03 22:44:14 +01002940
Gilles Peskine20035e32018-02-03 22:44:14 +01002941/****************************************************************/
2942/* Asymmetric cryptography */
2943/****************************************************************/
2944
Gilles Peskine2b450e32018-06-27 15:42:46 +02002945#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002946/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002947 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002948static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2949 size_t hash_length,
2950 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002951{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002952 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002953 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002954 *md_alg = mbedtls_md_get_type( md_info );
2955
2956 /* The Mbed TLS RSA module uses an unsigned int for hash length
2957 * parameters. Validate that it fits so that we don't risk an
2958 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002959#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002960 if( hash_length > UINT_MAX )
2961 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002962#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002963
2964#if defined(MBEDTLS_PKCS1_V15)
2965 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2966 * must be correct. */
2967 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2968 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002969 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002970 if( md_info == NULL )
2971 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002972 if( mbedtls_md_get_size( md_info ) != hash_length )
2973 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002974 }
2975#endif /* MBEDTLS_PKCS1_V15 */
2976
2977#if defined(MBEDTLS_PKCS1_V21)
2978 /* PSS requires a hash internally. */
2979 if( PSA_ALG_IS_RSA_PSS( alg ) )
2980 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002981 if( md_info == NULL )
2982 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002983 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002984#endif /* MBEDTLS_PKCS1_V21 */
2985
Gilles Peskine61b91d42018-06-08 16:09:36 +02002986 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002987}
2988
Gilles Peskine2b450e32018-06-27 15:42:46 +02002989static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2990 psa_algorithm_t alg,
2991 const uint8_t *hash,
2992 size_t hash_length,
2993 uint8_t *signature,
2994 size_t signature_size,
2995 size_t *signature_length )
2996{
2997 psa_status_t status;
2998 int ret;
2999 mbedtls_md_type_t md_alg;
3000
3001 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3002 if( status != PSA_SUCCESS )
3003 return( status );
3004
Gilles Peskine630a18a2018-06-29 17:49:35 +02003005 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003006 return( PSA_ERROR_BUFFER_TOO_SMALL );
3007
3008#if defined(MBEDTLS_PKCS1_V15)
3009 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3010 {
3011 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3012 MBEDTLS_MD_NONE );
3013 ret = mbedtls_rsa_pkcs1_sign( rsa,
3014 mbedtls_ctr_drbg_random,
3015 &global_data.ctr_drbg,
3016 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003017 md_alg,
3018 (unsigned int) hash_length,
3019 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003020 signature );
3021 }
3022 else
3023#endif /* MBEDTLS_PKCS1_V15 */
3024#if defined(MBEDTLS_PKCS1_V21)
3025 if( PSA_ALG_IS_RSA_PSS( alg ) )
3026 {
3027 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3028 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3029 mbedtls_ctr_drbg_random,
3030 &global_data.ctr_drbg,
3031 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003032 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003033 (unsigned int) hash_length,
3034 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003035 signature );
3036 }
3037 else
3038#endif /* MBEDTLS_PKCS1_V21 */
3039 {
3040 return( PSA_ERROR_INVALID_ARGUMENT );
3041 }
3042
3043 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003044 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003045 return( mbedtls_to_psa_error( ret ) );
3046}
3047
3048static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3049 psa_algorithm_t alg,
3050 const uint8_t *hash,
3051 size_t hash_length,
3052 const uint8_t *signature,
3053 size_t signature_length )
3054{
3055 psa_status_t status;
3056 int ret;
3057 mbedtls_md_type_t md_alg;
3058
3059 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3060 if( status != PSA_SUCCESS )
3061 return( status );
3062
Gilles Peskine630a18a2018-06-29 17:49:35 +02003063 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003064 return( PSA_ERROR_BUFFER_TOO_SMALL );
3065
3066#if defined(MBEDTLS_PKCS1_V15)
3067 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3068 {
3069 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3070 MBEDTLS_MD_NONE );
3071 ret = mbedtls_rsa_pkcs1_verify( rsa,
3072 mbedtls_ctr_drbg_random,
3073 &global_data.ctr_drbg,
3074 MBEDTLS_RSA_PUBLIC,
3075 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003076 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003077 hash,
3078 signature );
3079 }
3080 else
3081#endif /* MBEDTLS_PKCS1_V15 */
3082#if defined(MBEDTLS_PKCS1_V21)
3083 if( PSA_ALG_IS_RSA_PSS( alg ) )
3084 {
3085 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3086 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3087 mbedtls_ctr_drbg_random,
3088 &global_data.ctr_drbg,
3089 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003090 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003091 (unsigned int) hash_length,
3092 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003093 signature );
3094 }
3095 else
3096#endif /* MBEDTLS_PKCS1_V21 */
3097 {
3098 return( PSA_ERROR_INVALID_ARGUMENT );
3099 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003100
3101 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3102 * the rest of the signature is invalid". This has little use in
3103 * practice and PSA doesn't report this distinction. */
3104 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3105 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003106 return( mbedtls_to_psa_error( ret ) );
3107}
3108#endif /* MBEDTLS_RSA_C */
3109
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003110#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003111/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3112 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3113 * (even though these functions don't modify it). */
3114static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3115 psa_algorithm_t alg,
3116 const uint8_t *hash,
3117 size_t hash_length,
3118 uint8_t *signature,
3119 size_t signature_size,
3120 size_t *signature_length )
3121{
3122 int ret;
3123 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003124 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003125 mbedtls_mpi_init( &r );
3126 mbedtls_mpi_init( &s );
3127
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003128 if( signature_size < 2 * curve_bytes )
3129 {
3130 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3131 goto cleanup;
3132 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003133
Gilles Peskinea05219c2018-11-16 16:02:56 +01003134#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003135 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3136 {
3137 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3138 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3139 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3140 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3141 hash, hash_length,
3142 md_alg ) );
3143 }
3144 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003145#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003146 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003147 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003148 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3149 hash, hash_length,
3150 mbedtls_ctr_drbg_random,
3151 &global_data.ctr_drbg ) );
3152 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003153
3154 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3155 signature,
3156 curve_bytes ) );
3157 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3158 signature + curve_bytes,
3159 curve_bytes ) );
3160
3161cleanup:
3162 mbedtls_mpi_free( &r );
3163 mbedtls_mpi_free( &s );
3164 if( ret == 0 )
3165 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003166 return( mbedtls_to_psa_error( ret ) );
3167}
3168
3169static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3170 const uint8_t *hash,
3171 size_t hash_length,
3172 const uint8_t *signature,
3173 size_t signature_length )
3174{
3175 int ret;
3176 mbedtls_mpi r, s;
3177 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3178 mbedtls_mpi_init( &r );
3179 mbedtls_mpi_init( &s );
3180
3181 if( signature_length != 2 * curve_bytes )
3182 return( PSA_ERROR_INVALID_SIGNATURE );
3183
3184 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3185 signature,
3186 curve_bytes ) );
3187 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3188 signature + curve_bytes,
3189 curve_bytes ) );
3190
3191 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3192 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003193
3194cleanup:
3195 mbedtls_mpi_free( &r );
3196 mbedtls_mpi_free( &s );
3197 return( mbedtls_to_psa_error( ret ) );
3198}
3199#endif /* MBEDTLS_ECDSA_C */
3200
Gilles Peskinec5487a82018-12-03 18:08:14 +01003201psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003202 psa_algorithm_t alg,
3203 const uint8_t *hash,
3204 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003205 uint8_t *signature,
3206 size_t signature_size,
3207 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003208{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003209 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003210 psa_status_t status;
3211
3212 *signature_length = signature_size;
3213
Gilles Peskine28f8f302019-07-24 13:30:31 +02003214 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003215 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003216 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003217 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003218 {
3219 status = PSA_ERROR_INVALID_ARGUMENT;
3220 goto exit;
3221 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003222
Gilles Peskine20035e32018-02-03 22:44:14 +01003223#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003224 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003225 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003226 status = psa_rsa_sign( slot->data.rsa,
3227 alg,
3228 hash, hash_length,
3229 signature, signature_size,
3230 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003231 }
3232 else
3233#endif /* defined(MBEDTLS_RSA_C) */
3234#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003235 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003236 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003237#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003238 if(
3239#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3240 PSA_ALG_IS_ECDSA( alg )
3241#else
3242 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3243#endif
3244 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003245 status = psa_ecdsa_sign( slot->data.ecp,
3246 alg,
3247 hash, hash_length,
3248 signature, signature_size,
3249 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003250 else
3251#endif /* defined(MBEDTLS_ECDSA_C) */
3252 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003253 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003254 }
itayzafrir5c753392018-05-08 11:18:38 +03003255 }
3256 else
3257#endif /* defined(MBEDTLS_ECP_C) */
3258 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003259 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003260 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003261
3262exit:
3263 /* Fill the unused part of the output buffer (the whole buffer on error,
3264 * the trailing part on success) with something that isn't a valid mac
3265 * (barring an attack on the mac and deliberately-crafted input),
3266 * in case the caller doesn't check the return status properly. */
3267 if( status == PSA_SUCCESS )
3268 memset( signature + *signature_length, '!',
3269 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003270 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003271 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003272 /* If signature_size is 0 then we have nothing to do. We must not call
3273 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003274 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003275}
3276
Gilles Peskinec5487a82018-12-03 18:08:14 +01003277psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003278 psa_algorithm_t alg,
3279 const uint8_t *hash,
3280 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003281 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003282 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003283{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003284 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003285 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003286
Gilles Peskine28f8f302019-07-24 13:30:31 +02003287 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003288 if( status != PSA_SUCCESS )
3289 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003290
Gilles Peskine61b91d42018-06-08 16:09:36 +02003291#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003292 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003294 return( psa_rsa_verify( slot->data.rsa,
3295 alg,
3296 hash, hash_length,
3297 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 }
3299 else
3300#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003301#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003302 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003303 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003304#if defined(MBEDTLS_ECDSA_C)
3305 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003306 return( psa_ecdsa_verify( slot->data.ecp,
3307 hash, hash_length,
3308 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003309 else
3310#endif /* defined(MBEDTLS_ECDSA_C) */
3311 {
3312 return( PSA_ERROR_INVALID_ARGUMENT );
3313 }
itayzafrir5c753392018-05-08 11:18:38 +03003314 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003315 else
3316#endif /* defined(MBEDTLS_ECP_C) */
3317 {
3318 return( PSA_ERROR_NOT_SUPPORTED );
3319 }
3320}
3321
Gilles Peskine072ac562018-06-30 00:21:29 +02003322#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3323static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3324 mbedtls_rsa_context *rsa )
3325{
3326 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3327 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3328 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3329 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3330}
3331#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3332
Gilles Peskinec5487a82018-12-03 18:08:14 +01003333psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003334 psa_algorithm_t alg,
3335 const uint8_t *input,
3336 size_t input_length,
3337 const uint8_t *salt,
3338 size_t salt_length,
3339 uint8_t *output,
3340 size_t output_size,
3341 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003342{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003343 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003344 psa_status_t status;
3345
Darryl Green5cc689a2018-07-24 15:34:10 +01003346 (void) input;
3347 (void) input_length;
3348 (void) salt;
3349 (void) output;
3350 (void) output_size;
3351
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003352 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003353
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003354 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3355 return( PSA_ERROR_INVALID_ARGUMENT );
3356
Gilles Peskine28f8f302019-07-24 13:30:31 +02003357 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003358 if( status != PSA_SUCCESS )
3359 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003360 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3361 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003363
3364#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003365 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003366 {
3367 mbedtls_rsa_context *rsa = slot->data.rsa;
3368 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003369 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003370 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003372 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003373 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003374 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3375 mbedtls_ctr_drbg_random,
3376 &global_data.ctr_drbg,
3377 MBEDTLS_RSA_PUBLIC,
3378 input_length,
3379 input,
3380 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003381 }
3382 else
3383#endif /* MBEDTLS_PKCS1_V15 */
3384#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003385 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003386 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003387 psa_rsa_oaep_set_padding_mode( alg, rsa );
3388 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3389 mbedtls_ctr_drbg_random,
3390 &global_data.ctr_drbg,
3391 MBEDTLS_RSA_PUBLIC,
3392 salt, salt_length,
3393 input_length,
3394 input,
3395 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003396 }
3397 else
3398#endif /* MBEDTLS_PKCS1_V21 */
3399 {
3400 return( PSA_ERROR_INVALID_ARGUMENT );
3401 }
3402 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003403 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404 return( mbedtls_to_psa_error( ret ) );
3405 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003406 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003407#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003408 {
3409 return( PSA_ERROR_NOT_SUPPORTED );
3410 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003411}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003412
Gilles Peskinec5487a82018-12-03 18:08:14 +01003413psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003414 psa_algorithm_t alg,
3415 const uint8_t *input,
3416 size_t input_length,
3417 const uint8_t *salt,
3418 size_t salt_length,
3419 uint8_t *output,
3420 size_t output_size,
3421 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003422{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003423 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003424 psa_status_t status;
3425
Darryl Green5cc689a2018-07-24 15:34:10 +01003426 (void) input;
3427 (void) input_length;
3428 (void) salt;
3429 (void) output;
3430 (void) output_size;
3431
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003432 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003433
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003434 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3435 return( PSA_ERROR_INVALID_ARGUMENT );
3436
Gilles Peskine28f8f302019-07-24 13:30:31 +02003437 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003438 if( status != PSA_SUCCESS )
3439 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003440 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003441 return( PSA_ERROR_INVALID_ARGUMENT );
3442
3443#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003444 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003445 {
3446 mbedtls_rsa_context *rsa = slot->data.rsa;
3447 int ret;
3448
Gilles Peskine630a18a2018-06-29 17:49:35 +02003449 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003450 return( PSA_ERROR_INVALID_ARGUMENT );
3451
3452#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003453 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003454 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003455 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3456 mbedtls_ctr_drbg_random,
3457 &global_data.ctr_drbg,
3458 MBEDTLS_RSA_PRIVATE,
3459 output_length,
3460 input,
3461 output,
3462 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003463 }
3464 else
3465#endif /* MBEDTLS_PKCS1_V15 */
3466#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003467 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003468 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003469 psa_rsa_oaep_set_padding_mode( alg, rsa );
3470 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3471 mbedtls_ctr_drbg_random,
3472 &global_data.ctr_drbg,
3473 MBEDTLS_RSA_PRIVATE,
3474 salt, salt_length,
3475 output_length,
3476 input,
3477 output,
3478 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003479 }
3480 else
3481#endif /* MBEDTLS_PKCS1_V21 */
3482 {
3483 return( PSA_ERROR_INVALID_ARGUMENT );
3484 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003485
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003486 return( mbedtls_to_psa_error( ret ) );
3487 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003489#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003490 {
3491 return( PSA_ERROR_NOT_SUPPORTED );
3492 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003493}
Gilles Peskine20035e32018-02-03 22:44:14 +01003494
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003495
3496
mohammad1603503973b2018-03-12 15:59:30 +02003497/****************************************************************/
3498/* Symmetric cryptography */
3499/****************************************************************/
3500
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003501/* Initialize the cipher operation structure. Once this function has been
3502 * called, psa_cipher_abort can run and will do the right thing. */
3503static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3504 psa_algorithm_t alg )
3505{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003506 if( ! PSA_ALG_IS_CIPHER( alg ) )
3507 {
3508 memset( operation, 0, sizeof( *operation ) );
3509 return( PSA_ERROR_INVALID_ARGUMENT );
3510 }
3511
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003512 operation->alg = alg;
3513 operation->key_set = 0;
3514 operation->iv_set = 0;
3515 operation->iv_required = 1;
3516 operation->iv_size = 0;
3517 operation->block_size = 0;
3518 mbedtls_cipher_init( &operation->ctx.cipher );
3519 return( PSA_SUCCESS );
3520}
3521
Gilles Peskinee553c652018-06-04 16:22:46 +02003522static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003523 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003524 psa_algorithm_t alg,
3525 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003526{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003527 int ret = 0;
3528 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003529 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003530 size_t key_bits;
3531 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003532 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3533 PSA_KEY_USAGE_ENCRYPT :
3534 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003535
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003536 /* A context must be freshly initialized before it can be set up. */
3537 if( operation->alg != 0 )
3538 {
3539 return( PSA_ERROR_BAD_STATE );
3540 }
3541
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003542 status = psa_cipher_init( operation, alg );
3543 if( status != PSA_SUCCESS )
3544 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003545
Gilles Peskine28f8f302019-07-24 13:30:31 +02003546 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003547 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003548 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003549 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003550
Gilles Peskine8e338702019-07-30 20:06:31 +02003551 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003552 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003553 {
3554 status = PSA_ERROR_NOT_SUPPORTED;
3555 goto exit;
3556 }
mohammad1603503973b2018-03-12 15:59:30 +02003557
mohammad1603503973b2018-03-12 15:59:30 +02003558 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003559 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003560 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003561
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003562#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003563 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003564 {
3565 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003566 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003567 memcpy( keys, slot->data.raw.data, 16 );
3568 memcpy( keys + 16, slot->data.raw.data, 8 );
3569 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3570 keys,
3571 192, cipher_operation );
3572 }
3573 else
3574#endif
3575 {
3576 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3577 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003578 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003579 }
Moran Peker41deec42018-04-04 15:43:05 +03003580 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003581 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003582
mohammad16038481e742018-03-18 13:57:31 +02003583#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003584 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003585 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003586 case PSA_ALG_CBC_NO_PADDING:
3587 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3588 MBEDTLS_PADDING_NONE );
3589 break;
3590 case PSA_ALG_CBC_PKCS7:
3591 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3592 MBEDTLS_PADDING_PKCS7 );
3593 break;
3594 default:
3595 /* The algorithm doesn't involve padding. */
3596 ret = 0;
3597 break;
3598 }
3599 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003600 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003601#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3602
mohammad1603503973b2018-03-12 15:59:30 +02003603 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003604 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003605 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003606 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003607 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003608 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003609 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003610#if defined(MBEDTLS_CHACHA20_C)
3611 else
3612 if( alg == PSA_ALG_CHACHA20 )
3613 operation->iv_size = 12;
3614#endif
mohammad1603503973b2018-03-12 15:59:30 +02003615
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003616exit:
3617 if( status == 0 )
3618 status = mbedtls_to_psa_error( ret );
3619 if( status != 0 )
3620 psa_cipher_abort( operation );
3621 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003622}
3623
Gilles Peskinefe119512018-07-08 21:39:34 +02003624psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003625 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003626 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003627{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003628 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003629}
3630
Gilles Peskinefe119512018-07-08 21:39:34 +02003631psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003632 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003633 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003634{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003635 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003636}
3637
Gilles Peskinefe119512018-07-08 21:39:34 +02003638psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003639 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003640 size_t iv_size,
3641 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003642{
itayzafrir534bd7c2018-08-02 13:56:32 +03003643 psa_status_t status;
3644 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003645 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003646 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003647 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003648 }
Moran Peker41deec42018-04-04 15:43:05 +03003649 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003650 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003651 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003652 goto exit;
3653 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003654 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3655 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003656 if( ret != 0 )
3657 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003658 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003659 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003660 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003661
mohammad16038481e742018-03-18 13:57:31 +02003662 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003663 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003664
Moran Peker395db872018-05-31 14:07:14 +03003665exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003666 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003667 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003668 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003669}
3670
Gilles Peskinefe119512018-07-08 21:39:34 +02003671psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003672 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003673 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003674{
itayzafrir534bd7c2018-08-02 13:56:32 +03003675 psa_status_t status;
3676 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003677 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003678 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003679 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003680 }
Moran Pekera28258c2018-05-29 16:25:04 +03003681 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003682 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003683 status = PSA_ERROR_INVALID_ARGUMENT;
3684 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003685 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003686 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3687 status = mbedtls_to_psa_error( ret );
3688exit:
3689 if( status == PSA_SUCCESS )
3690 operation->iv_set = 1;
3691 else
mohammad1603503973b2018-03-12 15:59:30 +02003692 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003693 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003694}
3695
Gilles Peskinee553c652018-06-04 16:22:46 +02003696psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3697 const uint8_t *input,
3698 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003699 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003700 size_t output_size,
3701 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003702{
itayzafrir534bd7c2018-08-02 13:56:32 +03003703 psa_status_t status;
3704 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003705 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003706
3707 if( operation->alg == 0 )
3708 {
3709 return( PSA_ERROR_BAD_STATE );
3710 }
3711
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003712 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003713 {
3714 /* Take the unprocessed partial block left over from previous
3715 * update calls, if any, plus the input to this call. Remove
3716 * the last partial block, if any. You get the data that will be
3717 * output in this call. */
3718 expected_output_size =
3719 ( operation->ctx.cipher.unprocessed_len + input_length )
3720 / operation->block_size * operation->block_size;
3721 }
3722 else
3723 {
3724 expected_output_size = input_length;
3725 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003726
Gilles Peskine89d789c2018-06-04 17:17:16 +02003727 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003728 {
3729 status = PSA_ERROR_BUFFER_TOO_SMALL;
3730 goto exit;
3731 }
mohammad160382759612018-03-12 18:16:40 +02003732
mohammad1603503973b2018-03-12 15:59:30 +02003733 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003734 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003735 status = mbedtls_to_psa_error( ret );
3736exit:
3737 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003738 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003739 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003740}
3741
Gilles Peskinee553c652018-06-04 16:22:46 +02003742psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3743 uint8_t *output,
3744 size_t output_size,
3745 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003746{
David Saadab4ecc272019-02-14 13:48:10 +02003747 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003748 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003749 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003750
mohammad1603503973b2018-03-12 15:59:30 +02003751 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003752 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003753 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003754 }
3755 if( operation->iv_required && ! operation->iv_set )
3756 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003757 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003758 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003759
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003760 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003761 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3762 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003763 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003764 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003765 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003766 }
3767
Janos Follath315b51c2018-07-09 16:04:51 +01003768 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3769 temp_output_buffer,
3770 output_length );
3771 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003772 {
Janos Follath315b51c2018-07-09 16:04:51 +01003773 status = mbedtls_to_psa_error( cipher_ret );
3774 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003775 }
Janos Follath315b51c2018-07-09 16:04:51 +01003776
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003777 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003778 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003779 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003780 memcpy( output, temp_output_buffer, *output_length );
3781 else
3782 {
Janos Follath315b51c2018-07-09 16:04:51 +01003783 status = PSA_ERROR_BUFFER_TOO_SMALL;
3784 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003785 }
mohammad1603503973b2018-03-12 15:59:30 +02003786
Gilles Peskine3f108122018-12-07 18:14:53 +01003787 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003788 status = psa_cipher_abort( operation );
3789
3790 return( status );
3791
3792error:
3793
3794 *output_length = 0;
3795
Gilles Peskine3f108122018-12-07 18:14:53 +01003796 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003797 (void) psa_cipher_abort( operation );
3798
3799 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003800}
3801
Gilles Peskinee553c652018-06-04 16:22:46 +02003802psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3803{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003804 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003805 {
3806 /* The object has (apparently) been initialized but it is not
3807 * in use. It's ok to call abort on such an object, and there's
3808 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003809 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003810 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003811
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003812 /* Sanity check (shouldn't happen: operation->alg should
3813 * always have been initialized to a valid value). */
3814 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3815 return( PSA_ERROR_BAD_STATE );
3816
mohammad1603503973b2018-03-12 15:59:30 +02003817 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003818
Moran Peker41deec42018-04-04 15:43:05 +03003819 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003820 operation->key_set = 0;
3821 operation->iv_set = 0;
3822 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003823 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003824 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003825
Moran Peker395db872018-05-31 14:07:14 +03003826 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003827}
3828
Gilles Peskinea0655c32018-04-30 17:06:50 +02003829
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003830
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003831
mohammad16035955c982018-04-26 00:53:03 +03003832/****************************************************************/
3833/* AEAD */
3834/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003835
Gilles Peskineedf9a652018-08-17 18:11:56 +02003836typedef struct
3837{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003838 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003839 const mbedtls_cipher_info_t *cipher_info;
3840 union
3841 {
3842#if defined(MBEDTLS_CCM_C)
3843 mbedtls_ccm_context ccm;
3844#endif /* MBEDTLS_CCM_C */
3845#if defined(MBEDTLS_GCM_C)
3846 mbedtls_gcm_context gcm;
3847#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003848#if defined(MBEDTLS_CHACHAPOLY_C)
3849 mbedtls_chachapoly_context chachapoly;
3850#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003851 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003852 psa_algorithm_t core_alg;
3853 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854 uint8_t tag_length;
3855} aead_operation_t;
3856
Gilles Peskine30a9e412019-01-14 18:36:12 +01003857static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003858{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003859 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003860 {
3861#if defined(MBEDTLS_CCM_C)
3862 case PSA_ALG_CCM:
3863 mbedtls_ccm_free( &operation->ctx.ccm );
3864 break;
3865#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003866#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003867 case PSA_ALG_GCM:
3868 mbedtls_gcm_free( &operation->ctx.gcm );
3869 break;
3870#endif /* MBEDTLS_GCM_C */
3871 }
3872}
3873
3874static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003875 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003876 psa_key_usage_t usage,
3877 psa_algorithm_t alg )
3878{
3879 psa_status_t status;
3880 size_t key_bits;
3881 mbedtls_cipher_id_t cipher_id;
3882
Gilles Peskine28f8f302019-07-24 13:30:31 +02003883 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003884 if( status != PSA_SUCCESS )
3885 return( status );
3886
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003887 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003888
3889 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003890 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003891 &cipher_id );
3892 if( operation->cipher_info == NULL )
3893 return( PSA_ERROR_NOT_SUPPORTED );
3894
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003895 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003896 {
3897#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003898 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3899 operation->core_alg = PSA_ALG_CCM;
3900 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003901 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3902 * The call to mbedtls_ccm_encrypt_and_tag or
3903 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003904 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003905 return( PSA_ERROR_INVALID_ARGUMENT );
3906 mbedtls_ccm_init( &operation->ctx.ccm );
3907 status = mbedtls_to_psa_error(
3908 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3909 operation->slot->data.raw.data,
3910 (unsigned int) key_bits ) );
3911 if( status != 0 )
3912 goto cleanup;
3913 break;
3914#endif /* MBEDTLS_CCM_C */
3915
3916#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003917 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3918 operation->core_alg = PSA_ALG_GCM;
3919 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003920 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3921 * The call to mbedtls_gcm_crypt_and_tag or
3922 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02003923 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003924 return( PSA_ERROR_INVALID_ARGUMENT );
3925 mbedtls_gcm_init( &operation->ctx.gcm );
3926 status = mbedtls_to_psa_error(
3927 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3928 operation->slot->data.raw.data,
3929 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003930 if( status != 0 )
3931 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003932 break;
3933#endif /* MBEDTLS_GCM_C */
3934
Gilles Peskine26869f22019-05-06 15:25:00 +02003935#if defined(MBEDTLS_CHACHAPOLY_C)
3936 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3937 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3938 operation->full_tag_length = 16;
3939 /* We only support the default tag length. */
3940 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3941 return( PSA_ERROR_NOT_SUPPORTED );
3942 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3943 status = mbedtls_to_psa_error(
3944 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3945 operation->slot->data.raw.data ) );
3946 if( status != 0 )
3947 goto cleanup;
3948 break;
3949#endif /* MBEDTLS_CHACHAPOLY_C */
3950
Gilles Peskineedf9a652018-08-17 18:11:56 +02003951 default:
3952 return( PSA_ERROR_NOT_SUPPORTED );
3953 }
3954
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003955 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3956 {
3957 status = PSA_ERROR_INVALID_ARGUMENT;
3958 goto cleanup;
3959 }
3960 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003961
Gilles Peskineedf9a652018-08-17 18:11:56 +02003962 return( PSA_SUCCESS );
3963
3964cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003965 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003966 return( status );
3967}
3968
Gilles Peskinec5487a82018-12-03 18:08:14 +01003969psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003970 psa_algorithm_t alg,
3971 const uint8_t *nonce,
3972 size_t nonce_length,
3973 const uint8_t *additional_data,
3974 size_t additional_data_length,
3975 const uint8_t *plaintext,
3976 size_t plaintext_length,
3977 uint8_t *ciphertext,
3978 size_t ciphertext_size,
3979 size_t *ciphertext_length )
3980{
mohammad16035955c982018-04-26 00:53:03 +03003981 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003982 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003983 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003984
mohammad1603f08a5502018-06-03 15:05:47 +03003985 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003986
Gilles Peskinec5487a82018-12-03 18:08:14 +01003987 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003988 if( status != PSA_SUCCESS )
3989 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003990
Gilles Peskineedf9a652018-08-17 18:11:56 +02003991 /* For all currently supported modes, the tag is at the end of the
3992 * ciphertext. */
3993 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3994 {
3995 status = PSA_ERROR_BUFFER_TOO_SMALL;
3996 goto exit;
3997 }
3998 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003999
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004000#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004001 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004002 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004003 status = mbedtls_to_psa_error(
4004 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4005 MBEDTLS_GCM_ENCRYPT,
4006 plaintext_length,
4007 nonce, nonce_length,
4008 additional_data, additional_data_length,
4009 plaintext, ciphertext,
4010 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004011 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004012 else
4013#endif /* MBEDTLS_GCM_C */
4014#if defined(MBEDTLS_CCM_C)
4015 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004016 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004017 status = mbedtls_to_psa_error(
4018 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4019 plaintext_length,
4020 nonce, nonce_length,
4021 additional_data,
4022 additional_data_length,
4023 plaintext, ciphertext,
4024 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004025 }
mohammad16035c8845f2018-05-09 05:40:09 -07004026 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004027#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004028#if defined(MBEDTLS_CHACHAPOLY_C)
4029 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4030 {
4031 if( nonce_length != 12 || operation.tag_length != 16 )
4032 {
4033 status = PSA_ERROR_NOT_SUPPORTED;
4034 goto exit;
4035 }
4036 status = mbedtls_to_psa_error(
4037 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4038 plaintext_length,
4039 nonce,
4040 additional_data,
4041 additional_data_length,
4042 plaintext,
4043 ciphertext,
4044 tag ) );
4045 }
4046 else
4047#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004048 {
mohammad1603554faad2018-06-03 15:07:38 +03004049 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004050 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004051
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4053 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004054
Gilles Peskineedf9a652018-08-17 18:11:56 +02004055exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004056 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057 if( status == PSA_SUCCESS )
4058 *ciphertext_length = plaintext_length + operation.tag_length;
4059 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004060}
4061
Gilles Peskineee652a32018-06-01 19:23:52 +02004062/* Locate the tag in a ciphertext buffer containing the encrypted data
4063 * followed by the tag. Return the length of the part preceding the tag in
4064 * *plaintext_length. This is the size of the plaintext in modes where
4065 * the encrypted data has the same size as the plaintext, such as
4066 * CCM and GCM. */
4067static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4068 const uint8_t *ciphertext,
4069 size_t ciphertext_length,
4070 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004071 const uint8_t **p_tag )
4072{
4073 size_t payload_length;
4074 if( tag_length > ciphertext_length )
4075 return( PSA_ERROR_INVALID_ARGUMENT );
4076 payload_length = ciphertext_length - tag_length;
4077 if( payload_length > plaintext_size )
4078 return( PSA_ERROR_BUFFER_TOO_SMALL );
4079 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004080 return( PSA_SUCCESS );
4081}
4082
Gilles Peskinec5487a82018-12-03 18:08:14 +01004083psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004084 psa_algorithm_t alg,
4085 const uint8_t *nonce,
4086 size_t nonce_length,
4087 const uint8_t *additional_data,
4088 size_t additional_data_length,
4089 const uint8_t *ciphertext,
4090 size_t ciphertext_length,
4091 uint8_t *plaintext,
4092 size_t plaintext_size,
4093 size_t *plaintext_length )
4094{
mohammad16035955c982018-04-26 00:53:03 +03004095 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 aead_operation_t operation;
4097 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004098
Gilles Peskineee652a32018-06-01 19:23:52 +02004099 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004100
Gilles Peskinec5487a82018-12-03 18:08:14 +01004101 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004102 if( status != PSA_SUCCESS )
4103 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004104
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004105 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4106 ciphertext, ciphertext_length,
4107 plaintext_size, &tag );
4108 if( status != PSA_SUCCESS )
4109 goto exit;
4110
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004111#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004112 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004113 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114 status = mbedtls_to_psa_error(
4115 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4116 ciphertext_length - operation.tag_length,
4117 nonce, nonce_length,
4118 additional_data,
4119 additional_data_length,
4120 tag, operation.tag_length,
4121 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004122 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004123 else
4124#endif /* MBEDTLS_GCM_C */
4125#if defined(MBEDTLS_CCM_C)
4126 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004127 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004128 status = mbedtls_to_psa_error(
4129 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4130 ciphertext_length - operation.tag_length,
4131 nonce, nonce_length,
4132 additional_data,
4133 additional_data_length,
4134 ciphertext, plaintext,
4135 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004136 }
mohammad160339574652018-06-01 04:39:53 -07004137 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004138#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004139#if defined(MBEDTLS_CHACHAPOLY_C)
4140 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4141 {
4142 if( nonce_length != 12 || operation.tag_length != 16 )
4143 {
4144 status = PSA_ERROR_NOT_SUPPORTED;
4145 goto exit;
4146 }
4147 status = mbedtls_to_psa_error(
4148 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4149 ciphertext_length - operation.tag_length,
4150 nonce,
4151 additional_data,
4152 additional_data_length,
4153 tag,
4154 ciphertext,
4155 plaintext ) );
4156 }
4157 else
4158#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004159 {
mohammad1603554faad2018-06-03 15:07:38 +03004160 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004161 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004162
Gilles Peskineedf9a652018-08-17 18:11:56 +02004163 if( status != PSA_SUCCESS && plaintext_size != 0 )
4164 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004165
Gilles Peskineedf9a652018-08-17 18:11:56 +02004166exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004167 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004168 if( status == PSA_SUCCESS )
4169 *plaintext_length = ciphertext_length - operation.tag_length;
4170 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004171}
4172
Gilles Peskinea0655c32018-04-30 17:06:50 +02004173
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004174
Gilles Peskine20035e32018-02-03 22:44:14 +01004175/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004176/* Generators */
4177/****************************************************************/
4178
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004179#define HKDF_STATE_INIT 0 /* no input yet */
4180#define HKDF_STATE_STARTED 1 /* got salt */
4181#define HKDF_STATE_KEYED 2 /* got key */
4182#define HKDF_STATE_OUTPUT 3 /* output started */
4183
Gilles Peskinecbe66502019-05-16 16:59:18 +02004184static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004186{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4188 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004189 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004190 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004191}
4192
4193
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004194psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004195{
4196 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004198 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004199 {
4200 /* The object has (apparently) been initialized but it is not
4201 * in use. It's ok to call abort on such an object, and there's
4202 * nothing to do. */
4203 }
4204 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004205#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004206 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004207 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 mbedtls_free( operation->ctx.hkdf.info );
4209 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004210 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004211 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004212 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004213 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004214 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004215#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004216 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004217 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4219 operation->ctx.tls12_prf.key_len );
4220 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004221 }
Hanno Becker580fba12018-11-13 20:50:45 +00004222
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004223 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004224 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004225 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4226 operation->ctx.tls12_prf.Ai_with_seed_len );
4227 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004228 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004229#else
4230 if( operation->ctx.tls12_prf.seed != NULL )
4231 {
4232 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4233 operation->ctx.tls12_prf.seed_length );
4234 mbedtls_free( operation->ctx.tls12_prf.seed );
4235 }
4236
4237 if( operation->ctx.tls12_prf.label != NULL )
4238 {
4239 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4240 operation->ctx.tls12_prf.label_length );
4241 mbedtls_free( operation->ctx.tls12_prf.label );
4242 }
4243
4244 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4245
4246 /* We leave the fields Ai and output_block to be erased safely by the
4247 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004248#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004249 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004250 else
4251#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004252 {
4253 status = PSA_ERROR_BAD_STATE;
4254 }
Janos Follath083036a2019-06-11 10:22:26 +01004255 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004256 return( status );
4257}
4258
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004259psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004260 size_t *capacity)
4261{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004262 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004263 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004265 return PSA_ERROR_BAD_STATE;
4266 }
4267
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004268 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004269 return( PSA_SUCCESS );
4270}
4271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004272psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004273 size_t capacity )
4274{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004275 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004276 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004277 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004278 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004280 return( PSA_SUCCESS );
4281}
4282
Gilles Peskinebef7f142018-07-12 17:22:21 +02004283#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004285 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004286static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004287 psa_algorithm_t hash_alg,
4288 uint8_t *output,
4289 size_t output_length )
4290{
4291 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4292 psa_status_t status;
4293
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004294 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4295 return( PSA_ERROR_BAD_STATE );
4296 hkdf->state = HKDF_STATE_OUTPUT;
4297
Gilles Peskinebef7f142018-07-12 17:22:21 +02004298 while( output_length != 0 )
4299 {
4300 /* Copy what remains of the current block */
4301 uint8_t n = hash_length - hkdf->offset_in_block;
4302 if( n > output_length )
4303 n = (uint8_t) output_length;
4304 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4305 output += n;
4306 output_length -= n;
4307 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004308 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004309 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004310 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004311 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004312 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004313 * object was corrupted or if this function is called directly
4314 * inside the library. */
4315 if( hkdf->block_number == 0xff )
4316 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004317
4318 /* We need a new block */
4319 ++hkdf->block_number;
4320 hkdf->offset_in_block = 0;
4321 status = psa_hmac_setup_internal( &hkdf->hmac,
4322 hkdf->prk, hash_length,
4323 hash_alg );
4324 if( status != PSA_SUCCESS )
4325 return( status );
4326 if( hkdf->block_number != 1 )
4327 {
4328 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4329 hkdf->output_block,
4330 hash_length );
4331 if( status != PSA_SUCCESS )
4332 return( status );
4333 }
4334 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4335 hkdf->info,
4336 hkdf->info_length );
4337 if( status != PSA_SUCCESS )
4338 return( status );
4339 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4340 &hkdf->block_number, 1 );
4341 if( status != PSA_SUCCESS )
4342 return( status );
4343 status = psa_hmac_finish_internal( &hkdf->hmac,
4344 hkdf->output_block,
4345 sizeof( hkdf->output_block ) );
4346 if( status != PSA_SUCCESS )
4347 return( status );
4348 }
4349
4350 return( PSA_SUCCESS );
4351}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004352
Janos Follath999f6482019-06-11 12:04:10 +01004353#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004354static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4355 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004356 psa_algorithm_t alg )
4357{
4358 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4359 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4360 psa_hmac_internal_data hmac;
4361 psa_status_t status, cleanup_status;
4362
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004363 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004364 size_t Ai_len;
4365
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004366 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004367 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004369 * object was corrupted or if this function is called directly
4370 * inside the library. */
4371 if( tls12_prf->block_number == 0xff )
4372 return( PSA_ERROR_BAD_STATE );
4373
4374 /* We need a new block */
4375 ++tls12_prf->block_number;
4376 tls12_prf->offset_in_block = 0;
4377
4378 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4379 *
4380 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4381 *
4382 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4383 * HMAC_hash(secret, A(2) + seed) +
4384 * HMAC_hash(secret, A(3) + seed) + ...
4385 *
4386 * A(0) = seed
4387 * A(i) = HMAC_hash( secret, A(i-1) )
4388 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004389 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004390 * `HMAC_hash(secret, A(i) + seed)` from which the output
4391 * is currently extracted as `output_block`, while
4392 * `A(i) + seed` is stored in `Ai_with_seed`.
4393 *
4394 * Generating a new block means recalculating `Ai_with_seed`
4395 * from the A(i)-part of it, and afterwards recalculating
4396 * `output_block`.
4397 *
4398 * A(0) is computed at setup time.
4399 *
4400 */
4401
4402 psa_hmac_init_internal( &hmac );
4403
4404 /* We must distinguish the calculation of A(1) from those
4405 * of A(2) and higher, because A(0)=seed has a different
4406 * length than the other A(i). */
4407 if( tls12_prf->block_number == 1 )
4408 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004409 Ai = tls12_prf->Ai_with_seed + hash_length;
4410 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004411 }
4412 else
4413 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004414 Ai = tls12_prf->Ai_with_seed;
4415 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004416 }
4417
Hanno Becker3b339e22018-11-13 20:56:14 +00004418 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4419 status = psa_hmac_setup_internal( &hmac,
4420 tls12_prf->key,
4421 tls12_prf->key_len,
4422 hash_alg );
4423 if( status != PSA_SUCCESS )
4424 goto cleanup;
4425
4426 status = psa_hash_update( &hmac.hash_ctx,
4427 Ai, Ai_len );
4428 if( status != PSA_SUCCESS )
4429 goto cleanup;
4430
4431 status = psa_hmac_finish_internal( &hmac,
4432 tls12_prf->Ai_with_seed,
4433 hash_length );
4434 if( status != PSA_SUCCESS )
4435 goto cleanup;
4436
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004437 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4438 status = psa_hmac_setup_internal( &hmac,
4439 tls12_prf->key,
4440 tls12_prf->key_len,
4441 hash_alg );
4442 if( status != PSA_SUCCESS )
4443 goto cleanup;
4444
4445 status = psa_hash_update( &hmac.hash_ctx,
4446 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004447 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004448 if( status != PSA_SUCCESS )
4449 goto cleanup;
4450
4451 status = psa_hmac_finish_internal( &hmac,
4452 tls12_prf->output_block,
4453 hash_length );
4454 if( status != PSA_SUCCESS )
4455 goto cleanup;
4456
4457cleanup:
4458
4459 cleanup_status = psa_hmac_abort_internal( &hmac );
4460 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4461 status = cleanup_status;
4462
4463 return( status );
4464}
Janos Follath7742fee2019-06-17 12:58:10 +01004465#else
4466static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4467 psa_tls12_prf_key_derivation_t *tls12_prf,
4468 psa_algorithm_t alg )
4469{
4470 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4471 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004472 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4473 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004474
Janos Follath7742fee2019-06-17 12:58:10 +01004475 /* We can't be wanting more output after block 0xff, otherwise
4476 * the capacity check in psa_key_derivation_output_bytes() would have
4477 * prevented this call. It could happen only if the operation
4478 * object was corrupted or if this function is called directly
4479 * inside the library. */
4480 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004481 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004482
4483 /* We need a new block */
4484 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004485 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004486
4487 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4488 *
4489 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4490 *
4491 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4492 * HMAC_hash(secret, A(2) + seed) +
4493 * HMAC_hash(secret, A(3) + seed) + ...
4494 *
4495 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004496 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004497 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004498 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004499 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004500 * is currently extracted as `output_block` and where i is
4501 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004502 */
4503
Janos Follathea29bfb2019-06-19 12:21:20 +01004504 /* Save the hash context before using it, to preserve the hash state with
4505 * only the inner padding in it. We need this, because inner padding depends
4506 * on the key (secret in the RFC's terminology). */
4507 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4508 if( status != PSA_SUCCESS )
4509 goto cleanup;
4510
4511 /* Calculate A(i) where i = tls12_prf->block_number. */
4512 if( tls12_prf->block_number == 1 )
4513 {
4514 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4515 * the variable seed and in this instance means it in the context of the
4516 * P_hash function, where seed = label + seed.) */
4517 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4518 tls12_prf->label, tls12_prf->label_length );
4519 if( status != PSA_SUCCESS )
4520 goto cleanup;
4521 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4522 tls12_prf->seed, tls12_prf->seed_length );
4523 if( status != PSA_SUCCESS )
4524 goto cleanup;
4525 }
4526 else
4527 {
4528 /* A(i) = HMAC_hash(secret, A(i-1)) */
4529 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4530 tls12_prf->Ai, hash_length );
4531 if( status != PSA_SUCCESS )
4532 goto cleanup;
4533 }
4534
4535 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4536 tls12_prf->Ai, hash_length );
4537 if( status != PSA_SUCCESS )
4538 goto cleanup;
4539 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4540 if( status != PSA_SUCCESS )
4541 goto cleanup;
4542
4543 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4544 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4545 tls12_prf->Ai, hash_length );
4546 if( status != PSA_SUCCESS )
4547 goto cleanup;
4548 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4549 tls12_prf->label, tls12_prf->label_length );
4550 if( status != PSA_SUCCESS )
4551 goto cleanup;
4552 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4553 tls12_prf->seed, tls12_prf->seed_length );
4554 if( status != PSA_SUCCESS )
4555 goto cleanup;
4556 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4557 tls12_prf->output_block, hash_length );
4558 if( status != PSA_SUCCESS )
4559 goto cleanup;
4560 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4561 if( status != PSA_SUCCESS )
4562 goto cleanup;
4563
Janos Follath7742fee2019-06-17 12:58:10 +01004564
4565cleanup:
4566
Janos Follathea29bfb2019-06-19 12:21:20 +01004567 cleanup_status = psa_hash_abort( &backup );
4568 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4569 status = cleanup_status;
4570
4571 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004572}
Janos Follath999f6482019-06-11 12:04:10 +01004573#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004574
Janos Follath999f6482019-06-11 12:04:10 +01004575#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004576/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004577 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004578static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004579 psa_tls12_prf_key_derivation_t *tls12_prf,
4580 psa_algorithm_t alg,
4581 uint8_t *output,
4582 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004583{
4584 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4585 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4586 psa_status_t status;
4587
4588 while( output_length != 0 )
4589 {
4590 /* Copy what remains of the current block */
4591 uint8_t n = hash_length - tls12_prf->offset_in_block;
4592
4593 /* Check if we have fully processed the current block. */
4594 if( n == 0 )
4595 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004596 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004597 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004598 if( status != PSA_SUCCESS )
4599 return( status );
4600
4601 continue;
4602 }
4603
4604 if( n > output_length )
4605 n = (uint8_t) output_length;
4606 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4607 n );
4608 output += n;
4609 output_length -= n;
4610 tls12_prf->offset_in_block += n;
4611 }
4612
4613 return( PSA_SUCCESS );
4614}
Janos Follath844eb0e2019-06-19 12:10:49 +01004615#else
4616static psa_status_t psa_key_derivation_tls12_prf_read(
4617 psa_tls12_prf_key_derivation_t *tls12_prf,
4618 psa_algorithm_t alg,
4619 uint8_t *output,
4620 size_t output_length )
4621{
4622 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4623 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4624 psa_status_t status;
4625 uint8_t offset, length;
4626
4627 while( output_length != 0 )
4628 {
4629 /* Check if we have fully processed the current block. */
4630 if( tls12_prf->left_in_block == 0 )
4631 {
4632 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4633 alg );
4634 if( status != PSA_SUCCESS )
4635 return( status );
4636
4637 continue;
4638 }
4639
4640 if( tls12_prf->left_in_block > output_length )
4641 length = (uint8_t) output_length;
4642 else
4643 length = tls12_prf->left_in_block;
4644
4645 offset = hash_length - tls12_prf->left_in_block;
4646 memcpy( output, tls12_prf->output_block + offset, length );
4647 output += length;
4648 output_length -= length;
4649 tls12_prf->left_in_block -= length;
4650 }
4651
4652 return( PSA_SUCCESS );
4653}
Janos Follath999f6482019-06-11 12:04:10 +01004654#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004655#endif /* MBEDTLS_MD_C */
4656
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004657psa_status_t psa_key_derivation_output_bytes(
4658 psa_key_derivation_operation_t *operation,
4659 uint8_t *output,
4660 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004661{
4662 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004664
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004666 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004668 return PSA_ERROR_BAD_STATE;
4669 }
4670
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004672 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004674 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004676 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004677 goto exit;
4678 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004680 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004682 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004683 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4684 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004686 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004687 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004688 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690
Gilles Peskinebef7f142018-07-12 17:22:21 +02004691#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004692 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004693 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004694 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004696 output, output_length );
4697 }
Janos Follathadbec812019-06-14 11:05:39 +01004698 else
Janos Follathadbec812019-06-14 11:05:39 +01004699 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004700 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004701 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004703 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004704 output_length );
4705 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004706 else
4707#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708 {
4709 return( PSA_ERROR_BAD_STATE );
4710 }
4711
4712exit:
4713 if( status != PSA_SUCCESS )
4714 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004715 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 * This allows us to differentiate between exhausted operations and
4717 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4718 * operations. */
4719 psa_algorithm_t alg = operation->alg;
4720 psa_key_derivation_abort( operation );
4721 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004722 memset( output, '!', output_length );
4723 }
4724 return( status );
4725}
4726
Gilles Peskine08542d82018-07-19 17:05:42 +02004727#if defined(MBEDTLS_DES_C)
4728static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4729{
4730 if( data_size >= 8 )
4731 mbedtls_des_key_set_parity( data );
4732 if( data_size >= 16 )
4733 mbedtls_des_key_set_parity( data + 8 );
4734 if( data_size >= 24 )
4735 mbedtls_des_key_set_parity( data + 16 );
4736}
4737#endif /* MBEDTLS_DES_C */
4738
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004739static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004740 psa_key_slot_t *slot,
4741 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743{
4744 uint8_t *data = NULL;
4745 size_t bytes = PSA_BITS_TO_BYTES( bits );
4746 psa_status_t status;
4747
Gilles Peskine8e338702019-07-30 20:06:31 +02004748 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749 return( PSA_ERROR_INVALID_ARGUMENT );
4750 if( bits % 8 != 0 )
4751 return( PSA_ERROR_INVALID_ARGUMENT );
4752 data = mbedtls_calloc( 1, bytes );
4753 if( data == NULL )
4754 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4755
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757 if( status != PSA_SUCCESS )
4758 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004759#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004760 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004761 psa_des_set_key_parity( data, bytes );
4762#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004763 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764
4765exit:
4766 mbedtls_free( data );
4767 return( status );
4768}
4769
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004770psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004772 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004773{
4774 psa_status_t status;
4775 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004776 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004777 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004778#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4779 if( driver != NULL )
4780 {
4781 /* Deriving a key in a secure element is not implemented yet. */
4782 status = PSA_ERROR_NOT_SUPPORTED;
4783 }
4784#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004785 if( status == PSA_SUCCESS )
4786 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004787 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004788 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004790 }
4791 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004792 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004793 if( status != PSA_SUCCESS )
4794 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004795 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004796 *handle = 0;
4797 }
4798 return( status );
4799}
4800
Gilles Peskineeab56e42018-07-12 17:12:33 +02004801
4802
4803/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004804/* Key derivation */
4805/****************************************************************/
4806
Gilles Peskine969c5d62019-01-16 15:53:06 +01004807static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810{
Janos Follath5fe19732019-06-20 15:09:30 +01004811 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4812 * initialisers for this union leave some bytes unspecified.) */
4813 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4814
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004816#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004817 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4818 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4819 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004820 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4823 if( hash_size == 0 )
4824 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4826 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004827 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004828 {
4829 return( PSA_ERROR_NOT_SUPPORTED );
4830 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004832 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004833 }
4834#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004835 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004837}
4838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004840 psa_algorithm_t alg )
4841{
4842 psa_status_t status;
4843
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004845 return( PSA_ERROR_BAD_STATE );
4846
Gilles Peskine6843c292019-01-18 16:44:49 +01004847 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4848 return( PSA_ERROR_INVALID_ARGUMENT );
4849 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004850 {
4851 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853 }
4854 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4855 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004857 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004858 else
4859 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004860
4861 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004863 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004864}
4865
4866#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004867static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004868 psa_algorithm_t hash_alg,
4869 psa_key_derivation_step_t step,
4870 const uint8_t *data,
4871 size_t data_length )
4872{
4873 psa_status_t status;
4874 switch( step )
4875 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004876 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004877 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004879 status = psa_hmac_setup_internal( &hkdf->hmac,
4880 data, data_length,
4881 hash_alg );
4882 if( status != PSA_SUCCESS )
4883 return( status );
4884 hkdf->state = HKDF_STATE_STARTED;
4885 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004886 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 /* If no salt was provided, use an empty salt. */
4888 if( hkdf->state == HKDF_STATE_INIT )
4889 {
4890 status = psa_hmac_setup_internal( &hkdf->hmac,
4891 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004892 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004893 if( status != PSA_SUCCESS )
4894 return( status );
4895 hkdf->state = HKDF_STATE_STARTED;
4896 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004897 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004899 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4900 data, data_length );
4901 if( status != PSA_SUCCESS )
4902 return( status );
4903 status = psa_hmac_finish_internal( &hkdf->hmac,
4904 hkdf->prk,
4905 sizeof( hkdf->prk ) );
4906 if( status != PSA_SUCCESS )
4907 return( status );
4908 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4909 hkdf->block_number = 0;
4910 hkdf->state = HKDF_STATE_KEYED;
4911 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004912 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913 if( hkdf->state == HKDF_STATE_OUTPUT )
4914 return( PSA_ERROR_BAD_STATE );
4915 if( hkdf->info_set )
4916 return( PSA_ERROR_BAD_STATE );
4917 hkdf->info_length = data_length;
4918 if( data_length != 0 )
4919 {
4920 hkdf->info = mbedtls_calloc( 1, data_length );
4921 if( hkdf->info == NULL )
4922 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4923 memcpy( hkdf->info, data, data_length );
4924 }
4925 hkdf->info_set = 1;
4926 return( PSA_SUCCESS );
4927 default:
4928 return( PSA_ERROR_INVALID_ARGUMENT );
4929 }
4930}
Janos Follathb03233e2019-06-11 15:30:30 +01004931
4932#if defined(PSA_PRE_1_0_KEY_DERIVATION)
4933static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4934 psa_algorithm_t hash_alg,
4935 psa_key_derivation_step_t step,
4936 const uint8_t *data,
4937 size_t data_length )
4938{
4939 (void) prf;
4940 (void) hash_alg;
4941 (void) step;
4942 (void) data;
4943 (void) data_length;
4944
4945 return( PSA_ERROR_INVALID_ARGUMENT );
4946}
Janos Follath6660f0e2019-06-17 08:44:03 +01004947
4948static psa_status_t psa_tls12_prf_psk_to_ms_input(
4949 psa_tls12_prf_key_derivation_t *prf,
4950 psa_algorithm_t hash_alg,
4951 psa_key_derivation_step_t step,
4952 const uint8_t *data,
4953 size_t data_length )
4954{
4955 (void) prf;
4956 (void) hash_alg;
4957 (void) step;
4958 (void) data;
4959 (void) data_length;
4960
4961 return( PSA_ERROR_INVALID_ARGUMENT );
4962}
Janos Follathb03233e2019-06-11 15:30:30 +01004963#else
Janos Follathf08e2652019-06-13 09:05:41 +01004964static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4965 const uint8_t *data,
4966 size_t data_length )
4967{
4968 if( prf->state != TLS12_PRF_STATE_INIT )
4969 return( PSA_ERROR_BAD_STATE );
4970
Janos Follathd6dce9f2019-07-04 09:11:38 +01004971 if( data_length != 0 )
4972 {
4973 prf->seed = mbedtls_calloc( 1, data_length );
4974 if( prf->seed == NULL )
4975 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004976
Janos Follathd6dce9f2019-07-04 09:11:38 +01004977 memcpy( prf->seed, data, data_length );
4978 prf->seed_length = data_length;
4979 }
Janos Follathf08e2652019-06-13 09:05:41 +01004980
4981 prf->state = TLS12_PRF_STATE_SEED_SET;
4982
4983 return( PSA_SUCCESS );
4984}
4985
Janos Follath81550542019-06-13 14:26:34 +01004986static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4987 psa_algorithm_t hash_alg,
4988 const uint8_t *data,
4989 size_t data_length )
4990{
4991 psa_status_t status;
4992 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4993 return( PSA_ERROR_BAD_STATE );
4994
4995 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4996 if( status != PSA_SUCCESS )
4997 return( status );
4998
4999 prf->state = TLS12_PRF_STATE_KEY_SET;
5000
5001 return( PSA_SUCCESS );
5002}
5003
Janos Follath6660f0e2019-06-17 08:44:03 +01005004static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5005 psa_tls12_prf_key_derivation_t *prf,
5006 psa_algorithm_t hash_alg,
5007 const uint8_t *data,
5008 size_t data_length )
5009{
5010 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005011 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5012 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005013
5014 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5015 return( PSA_ERROR_INVALID_ARGUMENT );
5016
5017 /* Quoting RFC 4279, Section 2:
5018 *
5019 * The premaster secret is formed as follows: if the PSK is N octets
5020 * long, concatenate a uint16 with the value N, N zero octets, a second
5021 * uint16 with the value N, and the PSK itself.
5022 */
5023
Janos Follath40e13932019-06-26 13:22:29 +01005024 *cur++ = ( data_length >> 8 ) & 0xff;
5025 *cur++ = ( data_length >> 0 ) & 0xff;
5026 memset( cur, 0, data_length );
5027 cur += data_length;
5028 *cur++ = pms[0];
5029 *cur++ = pms[1];
5030 memcpy( cur, data, data_length );
5031 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005032
Janos Follath40e13932019-06-26 13:22:29 +01005033 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005034
5035 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5036 return( status );
5037}
5038
Janos Follath63028dd2019-06-13 09:15:47 +01005039static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5040 const uint8_t *data,
5041 size_t data_length )
5042{
5043 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5044 return( PSA_ERROR_BAD_STATE );
5045
Janos Follathd6dce9f2019-07-04 09:11:38 +01005046 if( data_length != 0 )
5047 {
5048 prf->label = mbedtls_calloc( 1, data_length );
5049 if( prf->label == NULL )
5050 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005051
Janos Follathd6dce9f2019-07-04 09:11:38 +01005052 memcpy( prf->label, data, data_length );
5053 prf->label_length = data_length;
5054 }
Janos Follath63028dd2019-06-13 09:15:47 +01005055
5056 prf->state = TLS12_PRF_STATE_LABEL_SET;
5057
5058 return( PSA_SUCCESS );
5059}
5060
Janos Follathb03233e2019-06-11 15:30:30 +01005061static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5062 psa_algorithm_t hash_alg,
5063 psa_key_derivation_step_t step,
5064 const uint8_t *data,
5065 size_t data_length )
5066{
Janos Follathb03233e2019-06-11 15:30:30 +01005067 switch( step )
5068 {
Janos Follathf08e2652019-06-13 09:05:41 +01005069 case PSA_KEY_DERIVATION_INPUT_SEED:
5070 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005071 case PSA_KEY_DERIVATION_INPUT_SECRET:
5072 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005073 case PSA_KEY_DERIVATION_INPUT_LABEL:
5074 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005075 default:
5076 return( PSA_ERROR_INVALID_ARGUMENT );
5077 }
5078}
Janos Follath6660f0e2019-06-17 08:44:03 +01005079
5080static psa_status_t psa_tls12_prf_psk_to_ms_input(
5081 psa_tls12_prf_key_derivation_t *prf,
5082 psa_algorithm_t hash_alg,
5083 psa_key_derivation_step_t step,
5084 const uint8_t *data,
5085 size_t data_length )
5086{
5087 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005088 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005089 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5090 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005091 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005092
5093 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5094}
Janos Follathb03233e2019-06-11 15:30:30 +01005095#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096#endif /* MBEDTLS_MD_C */
5097
Janos Follathb80a94e2019-06-12 15:54:46 +01005098static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005100 psa_key_derivation_step_t step,
5101 const uint8_t *data,
5102 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005103{
5104 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005105 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005106
5107#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 step, data, data_length );
5113 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005114 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005117 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005118 {
Janos Follathb03233e2019-06-11 15:30:30 +01005119 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5120 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5121 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005122 }
5123 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5124 {
5125 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5126 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5127 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128 }
5129 else
5130#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005131 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133 return( PSA_ERROR_BAD_STATE );
5134 }
5135
5136 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138 return( status );
5139}
5140
Janos Follath51f4a0f2019-06-14 11:35:55 +01005141psa_status_t psa_key_derivation_input_bytes(
5142 psa_key_derivation_operation_t *operation,
5143 psa_key_derivation_step_t step,
5144 const uint8_t *data,
5145 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005146{
Janos Follathc5621512019-06-14 11:27:57 +01005147 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5148 return( PSA_ERROR_INVALID_ARGUMENT );
5149
5150 return( psa_key_derivation_input_internal( operation, step,
5151 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005152}
5153
Janos Follath51f4a0f2019-06-14 11:35:55 +01005154psa_status_t psa_key_derivation_input_key(
5155 psa_key_derivation_operation_t *operation,
5156 psa_key_derivation_step_t step,
5157 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158{
5159 psa_key_slot_t *slot;
5160 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005161 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005162 PSA_KEY_USAGE_DERIVE,
5163 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164 if( status != PSA_SUCCESS )
5165 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02005166 if( slot->attr.type != PSA_KEY_TYPE_DERIVE )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005167 return( PSA_ERROR_INVALID_ARGUMENT );
5168 /* Don't allow a key to be used as an input that is usually public.
5169 * This is debatable. It's ok from a cryptographic perspective to
5170 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005171 * the material should be dedicated to a particular input step,
5172 * otherwise this may allow the key to be used in an unintended way
5173 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005174 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005176 return( psa_key_derivation_input_internal( operation,
5177 step,
5178 slot->data.raw.data,
5179 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005181
5182
5183
5184/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005185/* Key agreement */
5186/****************************************************************/
5187
Gilles Peskinea05219c2018-11-16 16:02:56 +01005188#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005189static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5190 size_t peer_key_length,
5191 const mbedtls_ecp_keypair *our_key,
5192 uint8_t *shared_secret,
5193 size_t shared_secret_size,
5194 size_t *shared_secret_length )
5195{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005196 mbedtls_ecp_keypair *their_key = NULL;
5197 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005198 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005199 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005200
Jaeden Ameroccdce902019-01-10 11:42:27 +00005201 status = psa_import_ec_public_key(
5202 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5203 peer_key, peer_key_length,
5204 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005205 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005206 goto exit;
5207
Jaeden Amero97271b32019-01-10 19:38:51 +00005208 status = mbedtls_to_psa_error(
5209 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5210 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005211 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005212 status = mbedtls_to_psa_error(
5213 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5214 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005215 goto exit;
5216
Jaeden Amero97271b32019-01-10 19:38:51 +00005217 status = mbedtls_to_psa_error(
5218 mbedtls_ecdh_calc_secret( &ecdh,
5219 shared_secret_length,
5220 shared_secret, shared_secret_size,
5221 mbedtls_ctr_drbg_random,
5222 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005223
5224exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005226 mbedtls_ecp_keypair_free( their_key );
5227 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005228 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005229}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005230#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005231
Gilles Peskine01d718c2018-09-18 12:01:02 +02005232#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5233
Gilles Peskine0216fe12019-04-11 21:23:21 +02005234static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5235 psa_key_slot_t *private_key,
5236 const uint8_t *peer_key,
5237 size_t peer_key_length,
5238 uint8_t *shared_secret,
5239 size_t shared_secret_size,
5240 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005241{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005242 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005243 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005244#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005245 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005246 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005247 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005248 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5249 private_key->data.ecp,
5250 shared_secret, shared_secret_size,
5251 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005252#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005254 (void) private_key;
5255 (void) peer_key;
5256 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005257 (void) shared_secret;
5258 (void) shared_secret_size;
5259 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260 return( PSA_ERROR_NOT_SUPPORTED );
5261 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005262}
5263
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005264/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265 * to potentially free embedded data structures and wipe confidential data.
5266 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005269 psa_key_slot_t *private_key,
5270 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005271 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005272{
5273 psa_status_t status;
5274 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5275 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005277
5278 /* Step 1: run the secret agreement algorithm to generate the shared
5279 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005280 status = psa_key_agreement_raw_internal( ka_alg,
5281 private_key,
5282 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005283 shared_secret,
5284 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005285 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005286 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005287 goto exit;
5288
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005289 /* Step 2: set up the key derivation to generate key material from
5290 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005291 status = psa_key_derivation_input_internal( operation, step,
5292 shared_secret,
5293 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005294
Gilles Peskine12313cd2018-06-20 00:20:32 +02005295exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005296 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005297 return( status );
5298}
5299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005301 psa_key_derivation_step_t step,
5302 psa_key_handle_t private_key,
5303 const uint8_t *peer_key,
5304 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005305{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005306 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005307 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005309 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005310 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005311 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005312 if( status != PSA_SUCCESS )
5313 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005315 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005316 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005317 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005319 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005320}
5321
Gilles Peskinebe697d82019-05-16 18:00:41 +02005322psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5323 psa_key_handle_t private_key,
5324 const uint8_t *peer_key,
5325 size_t peer_key_length,
5326 uint8_t *output,
5327 size_t output_size,
5328 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005329{
5330 psa_key_slot_t *slot;
5331 psa_status_t status;
5332
5333 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5334 {
5335 status = PSA_ERROR_INVALID_ARGUMENT;
5336 goto exit;
5337 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005338 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005339 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005340 if( status != PSA_SUCCESS )
5341 goto exit;
5342
5343 status = psa_key_agreement_raw_internal( alg, slot,
5344 peer_key, peer_key_length,
5345 output, output_size,
5346 output_length );
5347
5348exit:
5349 if( status != PSA_SUCCESS )
5350 {
5351 /* If an error happens and is not handled properly, the output
5352 * may be used as a key to protect sensitive data. Arrange for such
5353 * a key to be random, which is likely to result in decryption or
5354 * verification errors. This is better than filling the buffer with
5355 * some constant data such as zeros, which would result in the data
5356 * being protected with a reproducible, easily knowable key.
5357 */
5358 psa_generate_random( output, output_size );
5359 *output_length = output_size;
5360 }
5361 return( status );
5362}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005363
5364
5365/****************************************************************/
5366/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005367/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368
Gilles Peskine4c317f42018-07-12 01:24:09 +02005369psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005370 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371{
Gilles Peskine4c317f42018-07-12 01:24:09 +02005372 int ret;
5373 GUARD_MODULE_INITIALIZED;
5374
Gilles Peskinef181eca2019-08-07 13:49:00 +02005375 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5376 {
5377 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5378 output,
5379 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5380 if( ret != 0 )
5381 return( mbedtls_to_psa_error( ret ) );
5382 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5383 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5384 }
5385
Gilles Peskinee59236f2018-01-27 23:32:46 +01005386 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5387 return( mbedtls_to_psa_error( ret ) );
5388}
5389
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005390#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5391#include "mbedtls/entropy_poll.h"
5392
Gilles Peskine7228da22019-07-15 11:06:15 +02005393psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005394 size_t seed_size )
5395{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005396 if( global_data.initialized )
5397 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005398
5399 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5400 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5401 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5402 return( PSA_ERROR_INVALID_ARGUMENT );
5403
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005404 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005405}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005406#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005407
Gilles Peskinee56e8782019-04-26 17:34:02 +02005408#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5409static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5410 size_t domain_parameters_size,
5411 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005412{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005413 size_t i;
5414 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005415
Gilles Peskinee56e8782019-04-26 17:34:02 +02005416 if( domain_parameters_size == 0 )
5417 {
5418 *exponent = 65537;
5419 return( PSA_SUCCESS );
5420 }
5421
5422 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5423 * support values that fit in a 32-bit integer, which is larger than
5424 * int on just about every platform anyway. */
5425 if( domain_parameters_size > sizeof( acc ) )
5426 return( PSA_ERROR_NOT_SUPPORTED );
5427 for( i = 0; i < domain_parameters_size; i++ )
5428 acc = ( acc << 8 ) | domain_parameters[i];
5429 if( acc > INT_MAX )
5430 return( PSA_ERROR_NOT_SUPPORTED );
5431 *exponent = acc;
5432 return( PSA_SUCCESS );
5433}
5434#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5435
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005436static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 psa_key_slot_t *slot, size_t bits,
5438 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005439{
Gilles Peskine8e338702019-07-30 20:06:31 +02005440 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005441
Gilles Peskinee56e8782019-04-26 17:34:02 +02005442 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005443 return( PSA_ERROR_INVALID_ARGUMENT );
5444
Gilles Peskinee59236f2018-01-27 23:32:46 +01005445 if( key_type_is_raw_bytes( type ) )
5446 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005447 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5449 if( status != PSA_SUCCESS )
5450 return( status );
5451 status = psa_generate_random( slot->data.raw.data,
5452 slot->data.raw.bytes );
5453 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005454 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005455#if defined(MBEDTLS_DES_C)
5456 if( type == PSA_KEY_TYPE_DES )
5457 psa_des_set_key_parity( slot->data.raw.data,
5458 slot->data.raw.bytes );
5459#endif /* MBEDTLS_DES_C */
5460 }
5461 else
5462
5463#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005464 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 {
5466 mbedtls_rsa_context *rsa;
5467 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005468 int exponent;
5469 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005470 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5471 return( PSA_ERROR_NOT_SUPPORTED );
5472 /* Accept only byte-aligned keys, for the same reasons as
5473 * in psa_import_rsa_key(). */
5474 if( bits % 8 != 0 )
5475 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005476 status = psa_read_rsa_exponent( domain_parameters,
5477 domain_parameters_size,
5478 &exponent );
5479 if( status != PSA_SUCCESS )
5480 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5482 if( rsa == NULL )
5483 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5484 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5485 ret = mbedtls_rsa_gen_key( rsa,
5486 mbedtls_ctr_drbg_random,
5487 &global_data.ctr_drbg,
5488 (unsigned int) bits,
5489 exponent );
5490 if( ret != 0 )
5491 {
5492 mbedtls_rsa_free( rsa );
5493 mbedtls_free( rsa );
5494 return( mbedtls_to_psa_error( ret ) );
5495 }
5496 slot->data.rsa = rsa;
5497 }
5498 else
5499#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5500
5501#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005502 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005503 {
5504 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5505 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5506 const mbedtls_ecp_curve_info *curve_info =
5507 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5508 mbedtls_ecp_keypair *ecp;
5509 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005510 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511 return( PSA_ERROR_NOT_SUPPORTED );
5512 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5513 return( PSA_ERROR_NOT_SUPPORTED );
5514 if( curve_info->bit_size != bits )
5515 return( PSA_ERROR_INVALID_ARGUMENT );
5516 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5517 if( ecp == NULL )
5518 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5519 mbedtls_ecp_keypair_init( ecp );
5520 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5521 mbedtls_ctr_drbg_random,
5522 &global_data.ctr_drbg );
5523 if( ret != 0 )
5524 {
5525 mbedtls_ecp_keypair_free( ecp );
5526 mbedtls_free( ecp );
5527 return( mbedtls_to_psa_error( ret ) );
5528 }
5529 slot->data.ecp = ecp;
5530 }
5531 else
5532#endif /* MBEDTLS_ECP_C */
5533
5534 return( PSA_ERROR_NOT_SUPPORTED );
5535
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005536 return( PSA_SUCCESS );
5537}
Darryl Green0c6575a2018-11-07 16:05:30 +00005538
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005539psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005540 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005541{
5542 psa_status_t status;
5543 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005544 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005545 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005546#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5547 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005548 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005549 /* Generating a key in a secure element is not implemented yet. */
5550 status = PSA_ERROR_NOT_SUPPORTED;
Darryl Green0c6575a2018-11-07 16:05:30 +00005551 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005553 if( status == PSA_SUCCESS )
5554 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005555 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005556 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005557 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005558 }
5559 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005560 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005561 if( status != PSA_SUCCESS )
5562 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005563 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005564 *handle = 0;
5565 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005566 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005567}
5568
5569
Gilles Peskinee59236f2018-01-27 23:32:46 +01005570
5571/****************************************************************/
5572/* Module setup */
5573/****************************************************************/
5574
Gilles Peskine5e769522018-11-20 21:59:56 +01005575psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5576 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5577 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5578{
5579 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5580 return( PSA_ERROR_BAD_STATE );
5581 global_data.entropy_init = entropy_init;
5582 global_data.entropy_free = entropy_free;
5583 return( PSA_SUCCESS );
5584}
5585
Gilles Peskinee59236f2018-01-27 23:32:46 +01005586void mbedtls_psa_crypto_free( void )
5587{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005588 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005589 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5590 {
5591 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005592 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005593 }
5594 /* Wipe all remaining data, including configuration.
5595 * In particular, this sets all state indicator to the value
5596 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005597 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005598#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005599 /* Unregister all secure element drivers, so that we restart from
5600 * a pristine state. */
5601 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005602#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005603}
5604
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005605#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5606/** Recover a transaction that was interrupted by a power failure.
5607 *
5608 * This function is called during initialization, before psa_crypto_init()
5609 * returns. If this function returns a failure status, the initialization
5610 * fails.
5611 */
5612static psa_status_t psa_crypto_recover_transaction(
5613 const psa_crypto_transaction_t *transaction )
5614{
5615 switch( transaction->unknown.type )
5616 {
5617 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5618 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005619 /* TODO - fall through to the failure case until this
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005620 * is implemented */
5621 default:
5622 /* We found an unsupported transaction in the storage.
5623 * We don't know what state the storage is in. Give up. */
5624 return( PSA_ERROR_STORAGE_FAILURE );
5625 }
5626}
5627#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5628
Gilles Peskinee59236f2018-01-27 23:32:46 +01005629psa_status_t psa_crypto_init( void )
5630{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005631 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005632 const unsigned char drbg_seed[] = "PSA";
5633
Gilles Peskinec6b69072018-11-20 21:42:52 +01005634 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005635 if( global_data.initialized != 0 )
5636 return( PSA_SUCCESS );
5637
Gilles Peskine5e769522018-11-20 21:59:56 +01005638 /* Set default configuration if
5639 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5640 if( global_data.entropy_init == NULL )
5641 global_data.entropy_init = mbedtls_entropy_init;
5642 if( global_data.entropy_free == NULL )
5643 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005644
Gilles Peskinec6b69072018-11-20 21:42:52 +01005645 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005646 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005647#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5648 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5649 /* The PSA entropy injection feature depends on using NV seed as an entropy
5650 * source. Add NV seed as an entropy source for PSA entropy injection. */
5651 mbedtls_entropy_add_source( &global_data.entropy,
5652 mbedtls_nv_seed_poll, NULL,
5653 MBEDTLS_ENTROPY_BLOCK_SIZE,
5654 MBEDTLS_ENTROPY_SOURCE_STRONG );
5655#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005656 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005657 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005658 status = mbedtls_to_psa_error(
5659 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5660 mbedtls_entropy_func,
5661 &global_data.entropy,
5662 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5663 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005664 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005665 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005666
Gilles Peskine66fb1262018-12-10 16:29:04 +01005667 status = psa_initialize_key_slots( );
5668 if( status != PSA_SUCCESS )
5669 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005670
Gilles Peskine4b734222019-07-24 15:56:31 +02005671#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005672 status = psa_crypto_load_transaction( );
5673 if( status == PSA_SUCCESS )
5674 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005675 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5676 if( status != PSA_SUCCESS )
5677 goto exit;
5678 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005679 }
5680 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5681 {
5682 /* There's no transaction to complete. It's all good. */
5683 status = PSA_SUCCESS;
5684 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005685#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005686
Gilles Peskinec6b69072018-11-20 21:42:52 +01005687 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688 global_data.initialized = 1;
5689
5690exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005691 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005692 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005693 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005694}
5695
5696#endif /* MBEDTLS_PSA_CRYPTO_C */