blob: eefb26116c91d21d636cfa2967ef2b8fe764fbef [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 Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskine28f8f302019-07-24 13:30:31 +0200874/** Retrieve a slot which must contain a transparent key.
875 *
876 * A transparent key is a key for which the key material is directly
877 * available, as opposed to a key in a secure element.
878 *
879 * This is a temporary function until secure element support is
880 * fully implemented.
881 */
882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
883static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
884 psa_key_slot_t **p_slot,
885 psa_key_usage_t usage,
886 psa_algorithm_t alg )
887{
888 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
889 if( status != PSA_SUCCESS )
890 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200891 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200892 {
893 *p_slot = NULL;
894 return( PSA_ERROR_NOT_SUPPORTED );
895 }
896 return( PSA_SUCCESS );
897}
898#else /* MBEDTLS_PSA_CRYPTO_SE_C */
899/* With no secure element support, all keys are transparent. */
900#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
901 psa_get_key_from_slot( handle, p_slot, usage, alg )
902#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
903
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100904/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100905static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000906{
Gilles Peskine73167e12019-07-12 23:44:37 +0200907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
908 if( psa_key_slot_is_external( slot ) )
909 {
910 /* No key material to clean. */
911 }
912 else
913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000914 if( slot->type == PSA_KEY_TYPE_NONE )
915 {
916 /* No key material to clean. */
917 }
918 else if( key_type_is_raw_bytes( slot->type ) )
919 {
920 mbedtls_free( slot->data.raw.data );
921 }
922 else
923#if defined(MBEDTLS_RSA_C)
924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
925 {
926 mbedtls_rsa_free( slot->data.rsa );
927 mbedtls_free( slot->data.rsa );
928 }
929 else
930#endif /* defined(MBEDTLS_RSA_C) */
931#if defined(MBEDTLS_ECP_C)
932 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
933 {
934 mbedtls_ecp_keypair_free( slot->data.ecp );
935 mbedtls_free( slot->data.ecp );
936 }
937 else
938#endif /* defined(MBEDTLS_ECP_C) */
939 {
940 /* Shouldn't happen: the key type is not any type that we
941 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200942 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000943 }
944
945 return( PSA_SUCCESS );
946}
947
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100948static void psa_abort_operations_using_key( psa_key_slot_t *slot )
949{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200950 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951 (void) slot;
952}
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Completely wipe a slot in memory, including its policy.
955 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100956psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957{
958 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100959 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960 /* At this point, key material and other type-specific content has
961 * been wiped. Clear remaining metadata. We can call memset and not
962 * zeroize because the metadata is not particularly sensitive. */
963 memset( slot, 0, sizeof( *slot ) );
964 return( status );
965}
966
Gilles Peskinec5487a82018-12-03 18:08:14 +0100967psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100969 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100970 psa_status_t status = PSA_SUCCESS;
971 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
973 psa_se_drv_table_entry_t *driver;
974#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskinec5487a82018-12-03 18:08:14 +0100976 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200977 if( status != PSA_SUCCESS )
978 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200979
980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
981 driver = psa_get_se_driver_entry( slot->lifetime );
982 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200983 {
984 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
985 psa_crypto_transaction.key.lifetime = slot->lifetime;
986 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
987 psa_crypto_transaction.key.id = slot->persistent_storage_id;
988 status = psa_crypto_save_transaction( );
989 if( status != PSA_SUCCESS )
990 {
991 /* TOnogrepDO: destroy what can be destroyed anyway */
992 return( status );
993 }
994
Gilles Peskine354f7672019-07-12 23:46:38 +0200995 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +0200996 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
998
Darryl Greend49a4992018-06-18 17:27:26 +0100999#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1000 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1001 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001002 storage_status =
1003 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001004 }
1005#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001006
Gilles Peskinefc762652019-07-22 19:30:34 +02001007#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1008 if( driver != NULL )
1009 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001010 psa_status_t status2;
1011 status = psa_save_se_persistent_data( driver );
1012 status2 = psa_crypto_stop_transaction( );
1013 if( status == PSA_SUCCESS )
1014 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001015 if( status != PSA_SUCCESS )
1016 {
1017 /* TOnogrepDO: destroy what can be destroyed anyway */
1018 return( status );
1019 }
1020 }
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1022
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001023 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001024 if( status != PSA_SUCCESS )
1025 return( status );
1026 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027}
1028
Gilles Peskineb870b182018-07-06 16:02:09 +02001029/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001030static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001031{
1032 if( key_type_is_raw_bytes( slot->type ) )
1033 return( slot->data.raw.bytes * 8 );
1034#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001035 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001036 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001037#endif /* defined(MBEDTLS_RSA_C) */
1038#if defined(MBEDTLS_ECP_C)
1039 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1040 return( slot->data.ecp->grp.pbits );
1041#endif /* defined(MBEDTLS_ECP_C) */
1042 /* Shouldn't happen except on an empty slot. */
1043 return( 0 );
1044}
1045
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001046void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1047{
Gilles Peskineb699f072019-04-26 16:06:02 +02001048 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001049 memset( attributes, 0, sizeof( *attributes ) );
1050}
1051
Gilles Peskineb699f072019-04-26 16:06:02 +02001052psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1053 psa_key_type_t type,
1054 const uint8_t *data,
1055 size_t data_length )
1056{
1057 uint8_t *copy = NULL;
1058
1059 if( data_length != 0 )
1060 {
1061 copy = mbedtls_calloc( 1, data_length );
1062 if( copy == NULL )
1063 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1064 memcpy( copy, data, data_length );
1065 }
1066 /* After this point, this function is guaranteed to succeed, so it
1067 * can start modifying `*attributes`. */
1068
1069 if( attributes->domain_parameters != NULL )
1070 {
1071 mbedtls_free( attributes->domain_parameters );
1072 attributes->domain_parameters = NULL;
1073 attributes->domain_parameters_size = 0;
1074 }
1075
1076 attributes->domain_parameters = copy;
1077 attributes->domain_parameters_size = data_length;
1078 attributes->type = type;
1079 return( PSA_SUCCESS );
1080}
1081
1082psa_status_t psa_get_key_domain_parameters(
1083 const psa_key_attributes_t *attributes,
1084 uint8_t *data, size_t data_size, size_t *data_length )
1085{
1086 if( attributes->domain_parameters_size > data_size )
1087 return( PSA_ERROR_BUFFER_TOO_SMALL );
1088 *data_length = attributes->domain_parameters_size;
1089 if( attributes->domain_parameters_size != 0 )
1090 memcpy( data, attributes->domain_parameters,
1091 attributes->domain_parameters_size );
1092 return( PSA_SUCCESS );
1093}
1094
1095#if defined(MBEDTLS_RSA_C)
1096static psa_status_t psa_get_rsa_public_exponent(
1097 const mbedtls_rsa_context *rsa,
1098 psa_key_attributes_t *attributes )
1099{
1100 mbedtls_mpi mpi;
1101 int ret;
1102 uint8_t *buffer = NULL;
1103 size_t buflen;
1104 mbedtls_mpi_init( &mpi );
1105
1106 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1107 if( ret != 0 )
1108 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001109 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1110 {
1111 /* It's the default value, which is reported as an empty string,
1112 * so there's nothing to do. */
1113 goto exit;
1114 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001115
1116 buflen = mbedtls_mpi_size( &mpi );
1117 buffer = mbedtls_calloc( 1, buflen );
1118 if( buffer == NULL )
1119 {
1120 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1121 goto exit;
1122 }
1123 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1124 if( ret != 0 )
1125 goto exit;
1126 attributes->domain_parameters = buffer;
1127 attributes->domain_parameters_size = buflen;
1128
1129exit:
1130 mbedtls_mpi_free( &mpi );
1131 if( ret != 0 )
1132 mbedtls_free( buffer );
1133 return( mbedtls_to_psa_error( ret ) );
1134}
1135#endif /* MBEDTLS_RSA_C */
1136
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001137/** Retrieve the readily-accessible attributes of a key in a slot.
1138 *
1139 * This function does not compute attributes that are not directly
1140 * stored in the slot, such as the bit size of a transparent key.
1141 */
1142static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1143 psa_key_attributes_t *attributes )
1144{
1145 attributes->id = slot->persistent_storage_id;
1146 attributes->lifetime = slot->lifetime;
1147 attributes->policy = slot->policy;
1148 attributes->type = slot->type;
1149}
1150
1151/** Retrieve all the publicly-accessible attributes of a key.
1152 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001153psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1154 psa_key_attributes_t *attributes )
1155{
1156 psa_key_slot_t *slot;
1157 psa_status_t status;
1158
1159 psa_reset_key_attributes( attributes );
1160
Gilles Peskine28f8f302019-07-24 13:30:31 +02001161 status = psa_get_transparent_key( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001162 if( status != PSA_SUCCESS )
1163 return( status );
1164
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001165 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001166 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001167
1168 switch( slot->type )
1169 {
1170#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001171 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001172 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1173 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1174 break;
1175#endif
1176 default:
1177 /* Nothing else to do. */
1178 break;
1179 }
1180
1181 if( status != PSA_SUCCESS )
1182 psa_reset_key_attributes( attributes );
1183 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184}
1185
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001186#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001187static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1188 unsigned char *buf, size_t size )
1189{
1190 int ret;
1191 unsigned char *c;
1192 size_t len = 0;
1193
1194 c = buf + size;
1195
1196 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1197
1198 return( (int) len );
1199}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001200#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001201
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001202static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1203 uint8_t *data,
1204 size_t data_size,
1205 size_t *data_length,
1206 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001207{
Gilles Peskine5d309672019-07-12 23:47:28 +02001208#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1209 const psa_drv_se_t *drv;
1210 psa_drv_se_context_t *drv_context;
1211#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1212
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001213 *data_length = 0;
1214
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001215 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001216 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001217
Gilles Peskine5d309672019-07-12 23:47:28 +02001218#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1219 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1220 {
1221 psa_drv_se_export_key_t method;
1222 if( drv->key_management == NULL )
1223 return( PSA_ERROR_NOT_SUPPORTED );
1224 method = ( export_public_key ?
1225 drv->key_management->p_export_public :
1226 drv->key_management->p_export );
1227 if( method == NULL )
1228 return( PSA_ERROR_NOT_SUPPORTED );
1229 return( ( *method )( drv_context,
1230 slot->data.se.slot_number,
1231 data, data_size, data_length ) );
1232 }
1233#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1234
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001235 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236 {
1237 if( slot->data.raw.bytes > data_size )
1238 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001239 if( data_size != 0 )
1240 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001241 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001242 memset( data + slot->data.raw.bytes, 0,
1243 data_size - slot->data.raw.bytes );
1244 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245 *data_length = slot->data.raw.bytes;
1246 return( PSA_SUCCESS );
1247 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001248#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001249 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001250 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001251 psa_status_t status;
1252
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001253 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001254 if( bytes > data_size )
1255 return( PSA_ERROR_BUFFER_TOO_SMALL );
1256 status = mbedtls_to_psa_error(
1257 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1258 if( status != PSA_SUCCESS )
1259 return( status );
1260 memset( data + bytes, 0, data_size - bytes );
1261 *data_length = bytes;
1262 return( PSA_SUCCESS );
1263 }
1264#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 else
Moran Peker17e36e12018-05-02 12:55:20 +03001266 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001267#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001268 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001269 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001270 {
Moran Pekera998bc62018-04-16 18:16:20 +03001271 mbedtls_pk_context pk;
1272 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001273 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001274 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001275#if defined(MBEDTLS_RSA_C)
1276 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001277 pk.pk_info = &mbedtls_rsa_info;
1278 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001279#else
1280 return( PSA_ERROR_NOT_SUPPORTED );
1281#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001282 }
1283 else
1284 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001285#if defined(MBEDTLS_ECP_C)
1286 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001287 pk.pk_info = &mbedtls_eckey_info;
1288 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001289#else
1290 return( PSA_ERROR_NOT_SUPPORTED );
1291#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001292 }
Moran Pekerd7326592018-05-29 16:56:39 +03001293 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001294 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001295 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001296 }
Moran Peker17e36e12018-05-02 12:55:20 +03001297 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001298 {
Moran Peker17e36e12018-05-02 12:55:20 +03001299 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001300 }
Moran Peker60364322018-04-29 11:34:58 +03001301 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001302 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001303 /* If data_size is 0 then data may be NULL and then the
1304 * call to memset would have undefined behavior. */
1305 if( data_size != 0 )
1306 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001307 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001308 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001309 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1310 * Move the data to the beginning and erase remaining data
1311 * at the original location. */
1312 if( 2 * (size_t) ret <= data_size )
1313 {
1314 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001315 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001316 }
1317 else if( (size_t) ret < data_size )
1318 {
1319 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001320 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001321 }
Moran Pekera998bc62018-04-16 18:16:20 +03001322 *data_length = ret;
1323 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001324 }
1325 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001326#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001327 {
1328 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001329 it is valid for a special-purpose implementation to omit
1330 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001331 return( PSA_ERROR_NOT_SUPPORTED );
1332 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001333 }
1334}
1335
Gilles Peskinec5487a82018-12-03 18:08:14 +01001336psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001337 uint8_t *data,
1338 size_t data_size,
1339 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001340{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001341 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001342 psa_status_t status;
1343
1344 /* Set the key to empty now, so that even when there are errors, we always
1345 * set data_length to a value between 0 and data_size. On error, setting
1346 * the key to empty is a good choice because an empty key representation is
1347 * unlikely to be accepted anywhere. */
1348 *data_length = 0;
1349
1350 /* Export requires the EXPORT flag. There is an exception for public keys,
1351 * which don't require any flag, but psa_get_key_from_slot takes
1352 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001353 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001354 if( status != PSA_SUCCESS )
1355 return( status );
1356 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001357 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359
Gilles Peskinec5487a82018-12-03 18:08:14 +01001360psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001361 uint8_t *data,
1362 size_t data_size,
1363 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001364{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001365 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001366 psa_status_t status;
1367
1368 /* Set the key to empty now, so that even when there are errors, we always
1369 * set data_length to a value between 0 and data_size. On error, setting
1370 * the key to empty is a good choice because an empty key representation is
1371 * unlikely to be accepted anywhere. */
1372 *data_length = 0;
1373
1374 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001375 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001376 if( status != PSA_SUCCESS )
1377 return( status );
1378 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001379 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001380}
1381
Gilles Peskine4747d192019-04-17 15:05:45 +02001382static psa_status_t psa_set_key_policy_internal(
1383 psa_key_slot_t *slot,
1384 const psa_key_policy_t *policy )
1385{
1386 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001387 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001388 PSA_KEY_USAGE_ENCRYPT |
1389 PSA_KEY_USAGE_DECRYPT |
1390 PSA_KEY_USAGE_SIGN |
1391 PSA_KEY_USAGE_VERIFY |
1392 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1393 return( PSA_ERROR_INVALID_ARGUMENT );
1394
1395 slot->policy = *policy;
1396 return( PSA_SUCCESS );
1397}
1398
1399/** Prepare a key slot to receive key material.
1400 *
1401 * This function allocates a key slot and sets its metadata.
1402 *
1403 * If this function fails, call psa_fail_key_creation().
1404 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001405 * This function is intended to be used as follows:
1406 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1407 * it with the specified attributes, and assign it a handle.
1408 * -# Populate the slot with the key material.
1409 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1410 * In case of failure at any step, stop the sequence and call
1411 * psa_fail_key_creation().
1412 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001413 * \param[in] attributes Key attributes for the new key.
1414 * \param[out] handle On success, a handle for the allocated slot.
1415 * \param[out] p_slot On success, a pointer to the prepared slot.
1416 * \param[out] p_drv On any return, the driver for the key, if any.
1417 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001418 *
1419 * \retval #PSA_SUCCESS
1420 * The key slot is ready to receive key material.
1421 * \return If this function fails, the key slot is an invalid state.
1422 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001423 */
1424static psa_status_t psa_start_key_creation(
1425 const psa_key_attributes_t *attributes,
1426 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001427 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001428 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001429{
1430 psa_status_t status;
1431 psa_key_slot_t *slot;
1432
Gilles Peskine011e4282019-06-26 18:34:38 +02001433 *p_drv = NULL;
1434
Gilles Peskine267c6562019-05-27 19:01:54 +02001435 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001436 if( status != PSA_SUCCESS )
1437 return( status );
1438 slot = *p_slot;
1439
1440 status = psa_set_key_policy_internal( slot, &attributes->policy );
1441 if( status != PSA_SUCCESS )
1442 return( status );
1443 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001444
Gilles Peskine4747d192019-04-17 15:05:45 +02001445 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001446 {
1447 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001448 attributes->id,
1449 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001450 if( status != PSA_SUCCESS )
1451 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001453 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001454 slot->type = attributes->type;
1455
Gilles Peskinecbaff462019-07-12 23:46:04 +02001456#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinefc762652019-07-22 19:30:34 +02001457 /* Find a slot number for the new key. Save the slot number in
1458 * persistent storage, but do not yet save the driver's persistent
1459 * state, so that if the power fails during the key creation process,
1460 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001461 if( *p_drv != NULL )
1462 {
1463 status = psa_find_se_slot_for_key( attributes, *p_drv,
1464 &slot->data.se.slot_number );
1465 if( status != PSA_SUCCESS )
1466 return( status );
1467 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001468 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1469 psa_crypto_transaction.key.lifetime = slot->lifetime;
1470 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1471 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1472 status = psa_crypto_save_transaction( );
1473 if( status != PSA_SUCCESS )
1474 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001475#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1476
Gilles Peskine4747d192019-04-17 15:05:45 +02001477 return( status );
1478}
1479
1480/** Finalize the creation of a key once its key material has been set.
1481 *
1482 * This entails writing the key to persistent storage.
1483 *
1484 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001485 * See the documentation of psa_start_key_creation() for the intended use
1486 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001487 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001488 * \param[in,out] slot Pointer to the slot with key material.
1489 * \param[in] driver The secure element driver for the key,
1490 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001491 *
1492 * \retval #PSA_SUCCESS
1493 * The key was successfully created. The handle is now valid.
1494 * \return If this function fails, the key slot is an invalid state.
1495 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001496 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001497static psa_status_t psa_finish_key_creation(
1498 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001499 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001500{
1501 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001502 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001503 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001504
1505#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001506 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001507 {
1508 uint8_t *buffer = NULL;
1509 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001510 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001511
Gilles Peskine1df83d42019-07-23 16:13:14 +02001512#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1513 if( driver != NULL )
1514 {
1515 buffer = (uint8_t*) &slot->data.se.slot_number;
1516 length = sizeof( slot->data.se.slot_number );
1517 }
1518 else
1519#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1520 {
1521 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1522 psa_get_key_slot_bits( slot ) );
1523 buffer = mbedtls_calloc( 1, buffer_size );
1524 if( buffer == NULL && buffer_size != 0 )
1525 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1526 status = psa_internal_export_key( slot,
1527 buffer, buffer_size, &length,
1528 0 );
1529 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001530
1531 if( status == PSA_SUCCESS )
1532 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1534 psa_get_key_slot_attributes( slot, &attributes );
1535 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001536 }
1537
Gilles Peskine1df83d42019-07-23 16:13:14 +02001538#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1539 if( driver == NULL )
1540#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1541 {
1542 if( buffer_size != 0 )
1543 mbedtls_platform_zeroize( buffer, buffer_size );
1544 mbedtls_free( buffer );
1545 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001546 }
1547#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1548
Gilles Peskinecbaff462019-07-12 23:46:04 +02001549#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1550 if( driver != NULL )
1551 {
1552 status = psa_save_se_persistent_data( driver );
1553 if( status != PSA_SUCCESS )
1554 {
1555 psa_destroy_persistent_key( slot->persistent_storage_id );
1556 return( status );
1557 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001558 status = psa_crypto_stop_transaction( );
1559 if( status != PSA_SUCCESS )
1560 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001561 }
1562#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1563
Gilles Peskine4747d192019-04-17 15:05:45 +02001564 return( status );
1565}
1566
1567/** Abort the creation of a key.
1568 *
1569 * You may call this function after calling psa_start_key_creation(),
1570 * or after psa_finish_key_creation() fails. In other circumstances, this
1571 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001572 * See the documentation of psa_start_key_creation() for the intended use
1573 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001575 * \param[in,out] slot Pointer to the slot with key material.
1576 * \param[in] driver The secure element driver for the key,
1577 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001579static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001580 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001581{
Gilles Peskine011e4282019-06-26 18:34:38 +02001582 (void) driver;
1583
Gilles Peskine4747d192019-04-17 15:05:45 +02001584 if( slot == NULL )
1585 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001586
1587#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1588 /* TOnogrepDO: If the key has already been created in the secure
1589 * element, and the failure happened later (when saving metadata
1590 * to internal storage), we need to destroy the key in the secure
1591 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001592
1593 /* Abort the ongoing transaction if any. We already did what it
1594 * takes to undo any partial creation. All that's left is to update
1595 * the transaction data itself. */
1596 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001597#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1598
Gilles Peskine4747d192019-04-17 15:05:45 +02001599 psa_wipe_key_slot( slot );
1600}
1601
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001602static psa_status_t psa_check_key_slot_attributes(
1603 const psa_key_slot_t *slot,
1604 const psa_key_attributes_t *attributes )
1605{
1606 if( attributes->type != 0 )
1607 {
1608 if( attributes->type != slot->type )
1609 return( PSA_ERROR_INVALID_ARGUMENT );
1610 }
1611
1612 if( attributes->domain_parameters_size != 0 )
1613 {
1614#if defined(MBEDTLS_RSA_C)
1615 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1616 {
1617 mbedtls_mpi actual, required;
1618 int ret;
1619 mbedtls_mpi_init( &actual );
1620 mbedtls_mpi_init( &required );
1621 ret = mbedtls_rsa_export( slot->data.rsa,
1622 NULL, NULL, NULL, NULL, &actual );
1623 if( ret != 0 )
1624 goto rsa_exit;
1625 ret = mbedtls_mpi_read_binary( &required,
1626 attributes->domain_parameters,
1627 attributes->domain_parameters_size );
1628 if( ret != 0 )
1629 goto rsa_exit;
1630 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1631 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1632 rsa_exit:
1633 mbedtls_mpi_free( &actual );
1634 mbedtls_mpi_free( &required );
1635 if( ret != 0)
1636 return( mbedtls_to_psa_error( ret ) );
1637 }
1638 else
1639#endif
1640 {
1641 return( PSA_ERROR_INVALID_ARGUMENT );
1642 }
1643 }
1644
1645 if( attributes->bits != 0 )
1646 {
1647 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1648 return( PSA_ERROR_INVALID_ARGUMENT );
1649 }
1650
1651 return( PSA_SUCCESS );
1652}
1653
Gilles Peskine4747d192019-04-17 15:05:45 +02001654psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001656 size_t data_length,
1657 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
1659 psa_status_t status;
1660 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001661 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001662
Gilles Peskine011e4282019-06-26 18:34:38 +02001663 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001664 if( status != PSA_SUCCESS )
1665 goto exit;
1666
Gilles Peskine5d309672019-07-12 23:47:28 +02001667#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1668 if( driver != NULL )
1669 {
1670 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1671 if( drv->key_management == NULL ||
1672 drv->key_management->p_import == NULL )
1673 {
1674 status = PSA_ERROR_NOT_SUPPORTED;
1675 goto exit;
1676 }
1677 status = drv->key_management->p_import(
1678 psa_get_se_driver_context( driver ),
1679 slot->data.se.slot_number,
1680 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1681 data, data_length );
1682 /* TOnogrepDO: psa_check_key_slot_attributes? */
1683 }
1684 else
1685#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1686 {
1687 status = psa_import_key_into_slot( slot, data, data_length );
1688 if( status != PSA_SUCCESS )
1689 goto exit;
1690 status = psa_check_key_slot_attributes( slot, attributes );
1691 if( status != PSA_SUCCESS )
1692 goto exit;
1693 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001694
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001696exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001697 if( status != PSA_SUCCESS )
1698 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001699 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001700 *handle = 0;
1701 }
1702 return( status );
1703}
1704
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001705static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001706 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001707{
1708 psa_status_t status;
1709 uint8_t *buffer = NULL;
1710 size_t buffer_size = 0;
1711 size_t length;
1712
1713 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001714 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001715 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001716 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001717 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001718 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1719 if( status != PSA_SUCCESS )
1720 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001721 target->type = source->type;
1722 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001723
1724exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001725 if( buffer_size != 0 )
1726 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001727 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001728 return( status );
1729}
1730
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001731psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1732 const psa_key_attributes_t *specified_attributes,
1733 psa_key_handle_t *target_handle )
1734{
1735 psa_status_t status;
1736 psa_key_slot_t *source_slot = NULL;
1737 psa_key_slot_t *target_slot = NULL;
1738 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001739 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001740
Gilles Peskine28f8f302019-07-24 13:30:31 +02001741 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001742 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001743 if( status != PSA_SUCCESS )
1744 goto exit;
1745
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001746 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1747 if( status != PSA_SUCCESS )
1748 goto exit;
1749
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001750 status = psa_restrict_key_policy( &actual_attributes.policy,
1751 &source_slot->policy );
1752 if( status != PSA_SUCCESS )
1753 goto exit;
1754
1755 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001756 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001757 if( status != PSA_SUCCESS )
1758 goto exit;
1759
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001760#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1761 if( driver != NULL )
1762 {
1763 /* Copying to a secure element is not implemented yet. */
1764 status = PSA_ERROR_NOT_SUPPORTED;
1765 goto exit;
1766 }
1767#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1768
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001769 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001770 if( status != PSA_SUCCESS )
1771 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001772
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001774exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001775 if( status != PSA_SUCCESS )
1776 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001777 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001778 *target_handle = 0;
1779 }
1780 return( status );
1781}
1782
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001783
1784
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001785/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001786/* Message digests */
1787/****************************************************************/
1788
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001789static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001790{
1791 switch( alg )
1792 {
1793#if defined(MBEDTLS_MD2_C)
1794 case PSA_ALG_MD2:
1795 return( &mbedtls_md2_info );
1796#endif
1797#if defined(MBEDTLS_MD4_C)
1798 case PSA_ALG_MD4:
1799 return( &mbedtls_md4_info );
1800#endif
1801#if defined(MBEDTLS_MD5_C)
1802 case PSA_ALG_MD5:
1803 return( &mbedtls_md5_info );
1804#endif
1805#if defined(MBEDTLS_RIPEMD160_C)
1806 case PSA_ALG_RIPEMD160:
1807 return( &mbedtls_ripemd160_info );
1808#endif
1809#if defined(MBEDTLS_SHA1_C)
1810 case PSA_ALG_SHA_1:
1811 return( &mbedtls_sha1_info );
1812#endif
1813#if defined(MBEDTLS_SHA256_C)
1814 case PSA_ALG_SHA_224:
1815 return( &mbedtls_sha224_info );
1816 case PSA_ALG_SHA_256:
1817 return( &mbedtls_sha256_info );
1818#endif
1819#if defined(MBEDTLS_SHA512_C)
1820 case PSA_ALG_SHA_384:
1821 return( &mbedtls_sha384_info );
1822 case PSA_ALG_SHA_512:
1823 return( &mbedtls_sha512_info );
1824#endif
1825 default:
1826 return( NULL );
1827 }
1828}
1829
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001830psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1831{
1832 switch( operation->alg )
1833 {
Gilles Peskine81736312018-06-26 15:04:31 +02001834 case 0:
1835 /* The object has (apparently) been initialized but it is not
1836 * in use. It's ok to call abort on such an object, and there's
1837 * nothing to do. */
1838 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001839#if defined(MBEDTLS_MD2_C)
1840 case PSA_ALG_MD2:
1841 mbedtls_md2_free( &operation->ctx.md2 );
1842 break;
1843#endif
1844#if defined(MBEDTLS_MD4_C)
1845 case PSA_ALG_MD4:
1846 mbedtls_md4_free( &operation->ctx.md4 );
1847 break;
1848#endif
1849#if defined(MBEDTLS_MD5_C)
1850 case PSA_ALG_MD5:
1851 mbedtls_md5_free( &operation->ctx.md5 );
1852 break;
1853#endif
1854#if defined(MBEDTLS_RIPEMD160_C)
1855 case PSA_ALG_RIPEMD160:
1856 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1857 break;
1858#endif
1859#if defined(MBEDTLS_SHA1_C)
1860 case PSA_ALG_SHA_1:
1861 mbedtls_sha1_free( &operation->ctx.sha1 );
1862 break;
1863#endif
1864#if defined(MBEDTLS_SHA256_C)
1865 case PSA_ALG_SHA_224:
1866 case PSA_ALG_SHA_256:
1867 mbedtls_sha256_free( &operation->ctx.sha256 );
1868 break;
1869#endif
1870#if defined(MBEDTLS_SHA512_C)
1871 case PSA_ALG_SHA_384:
1872 case PSA_ALG_SHA_512:
1873 mbedtls_sha512_free( &operation->ctx.sha512 );
1874 break;
1875#endif
1876 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001877 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001878 }
1879 operation->alg = 0;
1880 return( PSA_SUCCESS );
1881}
1882
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001883psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001884 psa_algorithm_t alg )
1885{
1886 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001887
1888 /* A context must be freshly initialized before it can be set up. */
1889 if( operation->alg != 0 )
1890 {
1891 return( PSA_ERROR_BAD_STATE );
1892 }
1893
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001894 switch( alg )
1895 {
1896#if defined(MBEDTLS_MD2_C)
1897 case PSA_ALG_MD2:
1898 mbedtls_md2_init( &operation->ctx.md2 );
1899 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1900 break;
1901#endif
1902#if defined(MBEDTLS_MD4_C)
1903 case PSA_ALG_MD4:
1904 mbedtls_md4_init( &operation->ctx.md4 );
1905 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1906 break;
1907#endif
1908#if defined(MBEDTLS_MD5_C)
1909 case PSA_ALG_MD5:
1910 mbedtls_md5_init( &operation->ctx.md5 );
1911 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1912 break;
1913#endif
1914#if defined(MBEDTLS_RIPEMD160_C)
1915 case PSA_ALG_RIPEMD160:
1916 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1917 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1918 break;
1919#endif
1920#if defined(MBEDTLS_SHA1_C)
1921 case PSA_ALG_SHA_1:
1922 mbedtls_sha1_init( &operation->ctx.sha1 );
1923 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1924 break;
1925#endif
1926#if defined(MBEDTLS_SHA256_C)
1927 case PSA_ALG_SHA_224:
1928 mbedtls_sha256_init( &operation->ctx.sha256 );
1929 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1930 break;
1931 case PSA_ALG_SHA_256:
1932 mbedtls_sha256_init( &operation->ctx.sha256 );
1933 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1934 break;
1935#endif
1936#if defined(MBEDTLS_SHA512_C)
1937 case PSA_ALG_SHA_384:
1938 mbedtls_sha512_init( &operation->ctx.sha512 );
1939 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1940 break;
1941 case PSA_ALG_SHA_512:
1942 mbedtls_sha512_init( &operation->ctx.sha512 );
1943 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1944 break;
1945#endif
1946 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001947 return( PSA_ALG_IS_HASH( alg ) ?
1948 PSA_ERROR_NOT_SUPPORTED :
1949 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001950 }
1951 if( ret == 0 )
1952 operation->alg = alg;
1953 else
1954 psa_hash_abort( operation );
1955 return( mbedtls_to_psa_error( ret ) );
1956}
1957
1958psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1959 const uint8_t *input,
1960 size_t input_length )
1961{
1962 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001963
1964 /* Don't require hash implementations to behave correctly on a
1965 * zero-length input, which may have an invalid pointer. */
1966 if( input_length == 0 )
1967 return( PSA_SUCCESS );
1968
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001969 switch( operation->alg )
1970 {
1971#if defined(MBEDTLS_MD2_C)
1972 case PSA_ALG_MD2:
1973 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1974 input, input_length );
1975 break;
1976#endif
1977#if defined(MBEDTLS_MD4_C)
1978 case PSA_ALG_MD4:
1979 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1980 input, input_length );
1981 break;
1982#endif
1983#if defined(MBEDTLS_MD5_C)
1984 case PSA_ALG_MD5:
1985 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1986 input, input_length );
1987 break;
1988#endif
1989#if defined(MBEDTLS_RIPEMD160_C)
1990 case PSA_ALG_RIPEMD160:
1991 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1992 input, input_length );
1993 break;
1994#endif
1995#if defined(MBEDTLS_SHA1_C)
1996 case PSA_ALG_SHA_1:
1997 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1998 input, input_length );
1999 break;
2000#endif
2001#if defined(MBEDTLS_SHA256_C)
2002 case PSA_ALG_SHA_224:
2003 case PSA_ALG_SHA_256:
2004 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2005 input, input_length );
2006 break;
2007#endif
2008#if defined(MBEDTLS_SHA512_C)
2009 case PSA_ALG_SHA_384:
2010 case PSA_ALG_SHA_512:
2011 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2012 input, input_length );
2013 break;
2014#endif
2015 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002016 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002017 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002018
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002019 if( ret != 0 )
2020 psa_hash_abort( operation );
2021 return( mbedtls_to_psa_error( ret ) );
2022}
2023
2024psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2025 uint8_t *hash,
2026 size_t hash_size,
2027 size_t *hash_length )
2028{
itayzafrir40835d42018-08-02 13:14:17 +03002029 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002030 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002031 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002032
2033 /* Fill the output buffer with something that isn't a valid hash
2034 * (barring an attack on the hash and deliberately-crafted input),
2035 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002036 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002037 /* If hash_size is 0 then hash may be NULL and then the
2038 * call to memset would have undefined behavior. */
2039 if( hash_size != 0 )
2040 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002041
2042 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002043 {
2044 status = PSA_ERROR_BUFFER_TOO_SMALL;
2045 goto exit;
2046 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002047
2048 switch( operation->alg )
2049 {
2050#if defined(MBEDTLS_MD2_C)
2051 case PSA_ALG_MD2:
2052 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2053 break;
2054#endif
2055#if defined(MBEDTLS_MD4_C)
2056 case PSA_ALG_MD4:
2057 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2058 break;
2059#endif
2060#if defined(MBEDTLS_MD5_C)
2061 case PSA_ALG_MD5:
2062 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2063 break;
2064#endif
2065#if defined(MBEDTLS_RIPEMD160_C)
2066 case PSA_ALG_RIPEMD160:
2067 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2068 break;
2069#endif
2070#if defined(MBEDTLS_SHA1_C)
2071 case PSA_ALG_SHA_1:
2072 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2073 break;
2074#endif
2075#if defined(MBEDTLS_SHA256_C)
2076 case PSA_ALG_SHA_224:
2077 case PSA_ALG_SHA_256:
2078 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2079 break;
2080#endif
2081#if defined(MBEDTLS_SHA512_C)
2082 case PSA_ALG_SHA_384:
2083 case PSA_ALG_SHA_512:
2084 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2085 break;
2086#endif
2087 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002088 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002089 }
itayzafrir40835d42018-08-02 13:14:17 +03002090 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002091
itayzafrir40835d42018-08-02 13:14:17 +03002092exit:
2093 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002094 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002095 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002096 return( psa_hash_abort( operation ) );
2097 }
2098 else
2099 {
2100 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002101 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002102 }
2103}
2104
Gilles Peskine2d277862018-06-18 15:41:12 +02002105psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2106 const uint8_t *hash,
2107 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002108{
2109 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2110 size_t actual_hash_length;
2111 psa_status_t status = psa_hash_finish( operation,
2112 actual_hash, sizeof( actual_hash ),
2113 &actual_hash_length );
2114 if( status != PSA_SUCCESS )
2115 return( status );
2116 if( actual_hash_length != hash_length )
2117 return( PSA_ERROR_INVALID_SIGNATURE );
2118 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2119 return( PSA_ERROR_INVALID_SIGNATURE );
2120 return( PSA_SUCCESS );
2121}
2122
Gilles Peskineeb35d782019-01-22 17:56:16 +01002123psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2124 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002125{
2126 if( target_operation->alg != 0 )
2127 return( PSA_ERROR_BAD_STATE );
2128
2129 switch( source_operation->alg )
2130 {
2131 case 0:
2132 return( PSA_ERROR_BAD_STATE );
2133#if defined(MBEDTLS_MD2_C)
2134 case PSA_ALG_MD2:
2135 mbedtls_md2_clone( &target_operation->ctx.md2,
2136 &source_operation->ctx.md2 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_MD4_C)
2140 case PSA_ALG_MD4:
2141 mbedtls_md4_clone( &target_operation->ctx.md4,
2142 &source_operation->ctx.md4 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_MD5_C)
2146 case PSA_ALG_MD5:
2147 mbedtls_md5_clone( &target_operation->ctx.md5,
2148 &source_operation->ctx.md5 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_RIPEMD160_C)
2152 case PSA_ALG_RIPEMD160:
2153 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2154 &source_operation->ctx.ripemd160 );
2155 break;
2156#endif
2157#if defined(MBEDTLS_SHA1_C)
2158 case PSA_ALG_SHA_1:
2159 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2160 &source_operation->ctx.sha1 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA256_C)
2164 case PSA_ALG_SHA_224:
2165 case PSA_ALG_SHA_256:
2166 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2167 &source_operation->ctx.sha256 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_SHA512_C)
2171 case PSA_ALG_SHA_384:
2172 case PSA_ALG_SHA_512:
2173 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2174 &source_operation->ctx.sha512 );
2175 break;
2176#endif
2177 default:
2178 return( PSA_ERROR_NOT_SUPPORTED );
2179 }
2180
2181 target_operation->alg = source_operation->alg;
2182 return( PSA_SUCCESS );
2183}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184
2185
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002187/* MAC */
2188/****************************************************************/
2189
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002190static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191 psa_algorithm_t alg,
2192 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002193 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002194 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002195{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002196 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002197 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002199 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002200 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002201
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2203 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002204 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002206 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002207 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208 mode = MBEDTLS_MODE_STREAM;
2209 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210 case PSA_ALG_CTR:
2211 mode = MBEDTLS_MODE_CTR;
2212 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002213 case PSA_ALG_CFB:
2214 mode = MBEDTLS_MODE_CFB;
2215 break;
2216 case PSA_ALG_OFB:
2217 mode = MBEDTLS_MODE_OFB;
2218 break;
2219 case PSA_ALG_CBC_NO_PADDING:
2220 mode = MBEDTLS_MODE_CBC;
2221 break;
2222 case PSA_ALG_CBC_PKCS7:
2223 mode = MBEDTLS_MODE_CBC;
2224 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002225 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226 mode = MBEDTLS_MODE_CCM;
2227 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002228 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002229 mode = MBEDTLS_MODE_GCM;
2230 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002231 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2232 mode = MBEDTLS_MODE_CHACHAPOLY;
2233 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002234 default:
2235 return( NULL );
2236 }
2237 }
2238 else if( alg == PSA_ALG_CMAC )
2239 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240 else
2241 return( NULL );
2242
2243 switch( key_type )
2244 {
2245 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002246 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002247 break;
2248 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002249 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2250 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002251 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002252 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002253 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002254 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002255 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2256 * but two-key Triple-DES is functionally three-key Triple-DES
2257 * with K1=K3, so that's how we present it to mbedtls. */
2258 if( key_bits == 128 )
2259 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002260 break;
2261 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002262 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002263 break;
2264 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002265 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002266 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002267 case PSA_KEY_TYPE_CHACHA20:
2268 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2269 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002270 default:
2271 return( NULL );
2272 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002273 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002274 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002275
Jaeden Amero23bbb752018-06-26 14:16:54 +01002276 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2277 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278}
2279
Gilles Peskinea05219c2018-11-16 16:02:56 +01002280#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002281static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002282{
Gilles Peskine2d277862018-06-18 15:41:12 +02002283 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002284 {
2285 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002286 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002287 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002288 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002289 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002290 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002291 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002292 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002293 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002294 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002295 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002296 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002297 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002298 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002299 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002300 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002301 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002302 return( 128 );
2303 default:
2304 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002305 }
2306}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002307#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002308
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309/* Initialize the MAC operation structure. Once this function has been
2310 * called, psa_mac_abort can run and will do the right thing. */
2311static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2312 psa_algorithm_t alg )
2313{
2314 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2315
2316 operation->alg = alg;
2317 operation->key_set = 0;
2318 operation->iv_set = 0;
2319 operation->iv_required = 0;
2320 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002321 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002322
2323#if defined(MBEDTLS_CMAC_C)
2324 if( alg == PSA_ALG_CMAC )
2325 {
2326 operation->iv_required = 0;
2327 mbedtls_cipher_init( &operation->ctx.cmac );
2328 status = PSA_SUCCESS;
2329 }
2330 else
2331#endif /* MBEDTLS_CMAC_C */
2332#if defined(MBEDTLS_MD_C)
2333 if( PSA_ALG_IS_HMAC( operation->alg ) )
2334 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002335 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2336 operation->ctx.hmac.hash_ctx.alg = 0;
2337 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338 }
2339 else
2340#endif /* MBEDTLS_MD_C */
2341 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002342 if( ! PSA_ALG_IS_MAC( alg ) )
2343 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344 }
2345
2346 if( status != PSA_SUCCESS )
2347 memset( operation, 0, sizeof( *operation ) );
2348 return( status );
2349}
2350
Gilles Peskine01126fa2018-07-12 17:04:55 +02002351#if defined(MBEDTLS_MD_C)
2352static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2353{
Gilles Peskine3f108122018-12-07 18:14:53 +01002354 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002355 return( psa_hash_abort( &hmac->hash_ctx ) );
2356}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002357
Janos Follath999f6482019-06-11 12:04:10 +01002358#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002359static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2360{
2361 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2362 memset( hmac, 0, sizeof( *hmac ) );
2363}
Janos Follath999f6482019-06-11 12:04:10 +01002364#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002365#endif /* MBEDTLS_MD_C */
2366
Gilles Peskine8c9def32018-02-08 10:02:12 +01002367psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2368{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002369 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002370 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002371 /* The object has (apparently) been initialized but it is not
2372 * in use. It's ok to call abort on such an object, and there's
2373 * nothing to do. */
2374 return( PSA_SUCCESS );
2375 }
2376 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002378 if( operation->alg == PSA_ALG_CMAC )
2379 {
2380 mbedtls_cipher_free( &operation->ctx.cmac );
2381 }
2382 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002383#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002385 if( PSA_ALG_IS_HMAC( operation->alg ) )
2386 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002387 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002388 }
2389 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002390#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002391 {
2392 /* Sanity check (shouldn't happen: operation->alg should
2393 * always have been initialized to a valid value). */
2394 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395 }
Moran Peker41deec42018-04-04 15:43:05 +03002396
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397 operation->alg = 0;
2398 operation->key_set = 0;
2399 operation->iv_set = 0;
2400 operation->iv_required = 0;
2401 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002402 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002403
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002405
2406bad_state:
2407 /* If abort is called on an uninitialized object, we can't trust
2408 * anything. Wipe the object in case it contains confidential data.
2409 * This may result in a memory leak if a pointer gets overwritten,
2410 * but it's too late to do anything about this. */
2411 memset( operation, 0, sizeof( *operation ) );
2412 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413}
2414
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002415#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002416static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002417 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002418 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002419 const mbedtls_cipher_info_t *cipher_info )
2420{
2421 int ret;
2422
2423 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002424
2425 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2426 if( ret != 0 )
2427 return( ret );
2428
2429 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2430 slot->data.raw.data,
2431 key_bits );
2432 return( ret );
2433}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002434#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002435
Gilles Peskine248051a2018-06-20 16:09:38 +02002436#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002437static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2438 const uint8_t *key,
2439 size_t key_length,
2440 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002441{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002442 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002443 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002444 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002445 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002446 psa_status_t status;
2447
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002448 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2449 * overflow below. This should never trigger if the hash algorithm
2450 * is implemented correctly. */
2451 /* The size checks against the ipad and opad buffers cannot be written
2452 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2453 * because that triggers -Wlogical-op on GCC 7.3. */
2454 if( block_size > sizeof( ipad ) )
2455 return( PSA_ERROR_NOT_SUPPORTED );
2456 if( block_size > sizeof( hmac->opad ) )
2457 return( PSA_ERROR_NOT_SUPPORTED );
2458 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002459 return( PSA_ERROR_NOT_SUPPORTED );
2460
Gilles Peskined223b522018-06-11 18:12:58 +02002461 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002462 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002463 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002464 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002465 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002466 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002467 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002468 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002469 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002470 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002471 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002472 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002473 }
Gilles Peskine96889972018-07-12 17:07:03 +02002474 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2475 * but it is permitted. It is common when HMAC is used in HKDF, for
2476 * example. Don't call `memcpy` in the 0-length because `key` could be
2477 * an invalid pointer which would make the behavior undefined. */
2478 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002479 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002480
Gilles Peskined223b522018-06-11 18:12:58 +02002481 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2482 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002483 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002484 ipad[i] ^= 0x36;
2485 memset( ipad + key_length, 0x36, block_size - key_length );
2486
2487 /* Copy the key material from ipad to opad, flipping the requisite bits,
2488 * and filling the rest of opad with the requisite constant. */
2489 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002490 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2491 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002492
Gilles Peskine01126fa2018-07-12 17:04:55 +02002493 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002494 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002495 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002496
Gilles Peskine01126fa2018-07-12 17:04:55 +02002497 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002498
2499cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002500 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002501
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002502 return( status );
2503}
Gilles Peskine248051a2018-06-20 16:09:38 +02002504#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002505
Gilles Peskine89167cb2018-07-08 20:12:23 +02002506static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002507 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002508 psa_algorithm_t alg,
2509 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002512 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002514 psa_key_usage_t usage =
2515 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002516 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002517 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002519 /* A context must be freshly initialized before it can be set up. */
2520 if( operation->alg != 0 )
2521 {
2522 return( PSA_ERROR_BAD_STATE );
2523 }
2524
Gilles Peskined911eb72018-08-14 15:18:45 +02002525 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002526 if( status != PSA_SUCCESS )
2527 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002528 if( is_sign )
2529 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530
Gilles Peskine28f8f302019-07-24 13:30:31 +02002531 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002532 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002533 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002534 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002535
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002537 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002538 {
2539 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002540 mbedtls_cipher_info_from_psa( full_length_alg,
2541 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002542 int ret;
2543 if( cipher_info == NULL )
2544 {
2545 status = PSA_ERROR_NOT_SUPPORTED;
2546 goto exit;
2547 }
2548 operation->mac_size = cipher_info->block_size;
2549 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2550 status = mbedtls_to_psa_error( ret );
2551 }
2552 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002555 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002556 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002557 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002558 if( hash_alg == 0 )
2559 {
2560 status = PSA_ERROR_NOT_SUPPORTED;
2561 goto exit;
2562 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002563
2564 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2565 /* Sanity check. This shouldn't fail on a valid configuration. */
2566 if( operation->mac_size == 0 ||
2567 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2568 {
2569 status = PSA_ERROR_NOT_SUPPORTED;
2570 goto exit;
2571 }
2572
Gilles Peskine01126fa2018-07-12 17:04:55 +02002573 if( slot->type != PSA_KEY_TYPE_HMAC )
2574 {
2575 status = PSA_ERROR_INVALID_ARGUMENT;
2576 goto exit;
2577 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002578
Gilles Peskine01126fa2018-07-12 17:04:55 +02002579 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2580 slot->data.raw.data,
2581 slot->data.raw.bytes,
2582 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002583 }
2584 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002586 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002587 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002588 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002590
Gilles Peskined911eb72018-08-14 15:18:45 +02002591 if( truncated == 0 )
2592 {
2593 /* The "normal" case: untruncated algorithm. Nothing to do. */
2594 }
2595 else if( truncated < 4 )
2596 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002597 /* A very short MAC is too short for security since it can be
2598 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2599 * so we make this our minimum, even though 32 bits is still
2600 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002601 status = PSA_ERROR_NOT_SUPPORTED;
2602 }
2603 else if( truncated > operation->mac_size )
2604 {
2605 /* It's impossible to "truncate" to a larger length. */
2606 status = PSA_ERROR_INVALID_ARGUMENT;
2607 }
2608 else
2609 operation->mac_size = truncated;
2610
Gilles Peskinefbfac682018-07-08 20:51:54 +02002611exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002612 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002614 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002616 else
2617 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002618 operation->key_set = 1;
2619 }
2620 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002621}
2622
Gilles Peskine89167cb2018-07-08 20:12:23 +02002623psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002624 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002625 psa_algorithm_t alg )
2626{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002627 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002628}
2629
2630psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002631 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632 psa_algorithm_t alg )
2633{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002634 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002635}
2636
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2638 const uint8_t *input,
2639 size_t input_length )
2640{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002641 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002643 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002645 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646 operation->has_input = 1;
2647
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002649 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002650 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002651 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2652 input, input_length );
2653 status = mbedtls_to_psa_error( ret );
2654 }
2655 else
2656#endif /* MBEDTLS_CMAC_C */
2657#if defined(MBEDTLS_MD_C)
2658 if( PSA_ALG_IS_HMAC( operation->alg ) )
2659 {
2660 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2661 input_length );
2662 }
2663 else
2664#endif /* MBEDTLS_MD_C */
2665 {
2666 /* This shouldn't happen if `operation` was initialized by
2667 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002668 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002669 }
2670
Gilles Peskinefbfac682018-07-08 20:51:54 +02002671 if( status != PSA_SUCCESS )
2672 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002673 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674}
2675
Gilles Peskine01126fa2018-07-12 17:04:55 +02002676#if defined(MBEDTLS_MD_C)
2677static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2678 uint8_t *mac,
2679 size_t mac_size )
2680{
2681 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2682 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2683 size_t hash_size = 0;
2684 size_t block_size = psa_get_hash_block_size( hash_alg );
2685 psa_status_t status;
2686
Gilles Peskine01126fa2018-07-12 17:04:55 +02002687 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2688 if( status != PSA_SUCCESS )
2689 return( status );
2690 /* From here on, tmp needs to be wiped. */
2691
2692 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2693 if( status != PSA_SUCCESS )
2694 goto exit;
2695
2696 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2697 if( status != PSA_SUCCESS )
2698 goto exit;
2699
2700 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2701 if( status != PSA_SUCCESS )
2702 goto exit;
2703
Gilles Peskined911eb72018-08-14 15:18:45 +02002704 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2705 if( status != PSA_SUCCESS )
2706 goto exit;
2707
2708 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709
2710exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002711 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002712 return( status );
2713}
2714#endif /* MBEDTLS_MD_C */
2715
mohammad16036df908f2018-04-02 08:34:15 -07002716static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002717 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002718 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002720 if( ! operation->key_set )
2721 return( PSA_ERROR_BAD_STATE );
2722 if( operation->iv_required && ! operation->iv_set )
2723 return( PSA_ERROR_BAD_STATE );
2724
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725 if( mac_size < operation->mac_size )
2726 return( PSA_ERROR_BUFFER_TOO_SMALL );
2727
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002729 if( operation->alg == PSA_ALG_CMAC )
2730 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002731 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2732 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2733 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002734 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002735 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002736 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002738 else
2739#endif /* MBEDTLS_CMAC_C */
2740#if defined(MBEDTLS_MD_C)
2741 if( PSA_ALG_IS_HMAC( operation->alg ) )
2742 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002743 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002744 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002745 }
2746 else
2747#endif /* MBEDTLS_MD_C */
2748 {
2749 /* This shouldn't happen if `operation` was initialized by
2750 * a setup function. */
2751 return( PSA_ERROR_BAD_STATE );
2752 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753}
2754
Gilles Peskineacd4be32018-07-08 19:56:25 +02002755psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2756 uint8_t *mac,
2757 size_t mac_size,
2758 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002759{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002760 psa_status_t status;
2761
Jaeden Amero252ef282019-02-15 14:05:35 +00002762 if( operation->alg == 0 )
2763 {
2764 return( PSA_ERROR_BAD_STATE );
2765 }
2766
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002767 /* Fill the output buffer with something that isn't a valid mac
2768 * (barring an attack on the mac and deliberately-crafted input),
2769 * in case the caller doesn't check the return status properly. */
2770 *mac_length = mac_size;
2771 /* If mac_size is 0 then mac may be NULL and then the
2772 * call to memset would have undefined behavior. */
2773 if( mac_size != 0 )
2774 memset( mac, '!', mac_size );
2775
Gilles Peskine89167cb2018-07-08 20:12:23 +02002776 if( ! operation->is_sign )
2777 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002778 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002779 }
mohammad16036df908f2018-04-02 08:34:15 -07002780
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002781 status = psa_mac_finish_internal( operation, mac, mac_size );
2782
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002783 if( status == PSA_SUCCESS )
2784 {
2785 status = psa_mac_abort( operation );
2786 if( status == PSA_SUCCESS )
2787 *mac_length = operation->mac_size;
2788 else
2789 memset( mac, '!', mac_size );
2790 }
2791 else
2792 psa_mac_abort( operation );
2793 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002794}
2795
Gilles Peskineacd4be32018-07-08 19:56:25 +02002796psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2797 const uint8_t *mac,
2798 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799{
Gilles Peskine828ed142018-06-18 23:25:51 +02002800 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002801 psa_status_t status;
2802
Jaeden Amero252ef282019-02-15 14:05:35 +00002803 if( operation->alg == 0 )
2804 {
2805 return( PSA_ERROR_BAD_STATE );
2806 }
2807
Gilles Peskine89167cb2018-07-08 20:12:23 +02002808 if( operation->is_sign )
2809 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002810 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002811 }
2812 if( operation->mac_size != mac_length )
2813 {
2814 status = PSA_ERROR_INVALID_SIGNATURE;
2815 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002816 }
mohammad16036df908f2018-04-02 08:34:15 -07002817
2818 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002819 actual_mac, sizeof( actual_mac ) );
2820
2821 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2822 status = PSA_ERROR_INVALID_SIGNATURE;
2823
2824cleanup:
2825 if( status == PSA_SUCCESS )
2826 status = psa_mac_abort( operation );
2827 else
2828 psa_mac_abort( operation );
2829
Gilles Peskine3f108122018-12-07 18:14:53 +01002830 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002831
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002832 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833}
2834
2835
Gilles Peskine20035e32018-02-03 22:44:14 +01002836
Gilles Peskine20035e32018-02-03 22:44:14 +01002837/****************************************************************/
2838/* Asymmetric cryptography */
2839/****************************************************************/
2840
Gilles Peskine2b450e32018-06-27 15:42:46 +02002841#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002842/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002843 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002844static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2845 size_t hash_length,
2846 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002847{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002848 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002849 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002850 *md_alg = mbedtls_md_get_type( md_info );
2851
2852 /* The Mbed TLS RSA module uses an unsigned int for hash length
2853 * parameters. Validate that it fits so that we don't risk an
2854 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002855#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002856 if( hash_length > UINT_MAX )
2857 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002858#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002859
2860#if defined(MBEDTLS_PKCS1_V15)
2861 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2862 * must be correct. */
2863 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2864 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002865 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002866 if( md_info == NULL )
2867 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002868 if( mbedtls_md_get_size( md_info ) != hash_length )
2869 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002870 }
2871#endif /* MBEDTLS_PKCS1_V15 */
2872
2873#if defined(MBEDTLS_PKCS1_V21)
2874 /* PSS requires a hash internally. */
2875 if( PSA_ALG_IS_RSA_PSS( alg ) )
2876 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002877 if( md_info == NULL )
2878 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002879 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002880#endif /* MBEDTLS_PKCS1_V21 */
2881
Gilles Peskine61b91d42018-06-08 16:09:36 +02002882 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002883}
2884
Gilles Peskine2b450e32018-06-27 15:42:46 +02002885static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2886 psa_algorithm_t alg,
2887 const uint8_t *hash,
2888 size_t hash_length,
2889 uint8_t *signature,
2890 size_t signature_size,
2891 size_t *signature_length )
2892{
2893 psa_status_t status;
2894 int ret;
2895 mbedtls_md_type_t md_alg;
2896
2897 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2898 if( status != PSA_SUCCESS )
2899 return( status );
2900
Gilles Peskine630a18a2018-06-29 17:49:35 +02002901 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002902 return( PSA_ERROR_BUFFER_TOO_SMALL );
2903
2904#if defined(MBEDTLS_PKCS1_V15)
2905 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2906 {
2907 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2908 MBEDTLS_MD_NONE );
2909 ret = mbedtls_rsa_pkcs1_sign( rsa,
2910 mbedtls_ctr_drbg_random,
2911 &global_data.ctr_drbg,
2912 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002913 md_alg,
2914 (unsigned int) hash_length,
2915 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002916 signature );
2917 }
2918 else
2919#endif /* MBEDTLS_PKCS1_V15 */
2920#if defined(MBEDTLS_PKCS1_V21)
2921 if( PSA_ALG_IS_RSA_PSS( alg ) )
2922 {
2923 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2924 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2925 mbedtls_ctr_drbg_random,
2926 &global_data.ctr_drbg,
2927 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002928 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002929 (unsigned int) hash_length,
2930 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002931 signature );
2932 }
2933 else
2934#endif /* MBEDTLS_PKCS1_V21 */
2935 {
2936 return( PSA_ERROR_INVALID_ARGUMENT );
2937 }
2938
2939 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002940 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002941 return( mbedtls_to_psa_error( ret ) );
2942}
2943
2944static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2945 psa_algorithm_t alg,
2946 const uint8_t *hash,
2947 size_t hash_length,
2948 const uint8_t *signature,
2949 size_t signature_length )
2950{
2951 psa_status_t status;
2952 int ret;
2953 mbedtls_md_type_t md_alg;
2954
2955 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2956 if( status != PSA_SUCCESS )
2957 return( status );
2958
Gilles Peskine630a18a2018-06-29 17:49:35 +02002959 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002960 return( PSA_ERROR_BUFFER_TOO_SMALL );
2961
2962#if defined(MBEDTLS_PKCS1_V15)
2963 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2964 {
2965 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2966 MBEDTLS_MD_NONE );
2967 ret = mbedtls_rsa_pkcs1_verify( rsa,
2968 mbedtls_ctr_drbg_random,
2969 &global_data.ctr_drbg,
2970 MBEDTLS_RSA_PUBLIC,
2971 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002972 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002973 hash,
2974 signature );
2975 }
2976 else
2977#endif /* MBEDTLS_PKCS1_V15 */
2978#if defined(MBEDTLS_PKCS1_V21)
2979 if( PSA_ALG_IS_RSA_PSS( alg ) )
2980 {
2981 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2982 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2983 mbedtls_ctr_drbg_random,
2984 &global_data.ctr_drbg,
2985 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002986 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002987 (unsigned int) hash_length,
2988 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002989 signature );
2990 }
2991 else
2992#endif /* MBEDTLS_PKCS1_V21 */
2993 {
2994 return( PSA_ERROR_INVALID_ARGUMENT );
2995 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002996
2997 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2998 * the rest of the signature is invalid". This has little use in
2999 * practice and PSA doesn't report this distinction. */
3000 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3001 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003002 return( mbedtls_to_psa_error( ret ) );
3003}
3004#endif /* MBEDTLS_RSA_C */
3005
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003006#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003007/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3008 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3009 * (even though these functions don't modify it). */
3010static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3011 psa_algorithm_t alg,
3012 const uint8_t *hash,
3013 size_t hash_length,
3014 uint8_t *signature,
3015 size_t signature_size,
3016 size_t *signature_length )
3017{
3018 int ret;
3019 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003020 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003021 mbedtls_mpi_init( &r );
3022 mbedtls_mpi_init( &s );
3023
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003024 if( signature_size < 2 * curve_bytes )
3025 {
3026 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3027 goto cleanup;
3028 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003029
Gilles Peskinea05219c2018-11-16 16:02:56 +01003030#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003031 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3032 {
3033 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3034 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3035 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3036 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3037 hash, hash_length,
3038 md_alg ) );
3039 }
3040 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003041#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003042 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003043 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003044 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3045 hash, hash_length,
3046 mbedtls_ctr_drbg_random,
3047 &global_data.ctr_drbg ) );
3048 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003049
3050 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3051 signature,
3052 curve_bytes ) );
3053 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3054 signature + curve_bytes,
3055 curve_bytes ) );
3056
3057cleanup:
3058 mbedtls_mpi_free( &r );
3059 mbedtls_mpi_free( &s );
3060 if( ret == 0 )
3061 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003062 return( mbedtls_to_psa_error( ret ) );
3063}
3064
3065static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3066 const uint8_t *hash,
3067 size_t hash_length,
3068 const uint8_t *signature,
3069 size_t signature_length )
3070{
3071 int ret;
3072 mbedtls_mpi r, s;
3073 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3074 mbedtls_mpi_init( &r );
3075 mbedtls_mpi_init( &s );
3076
3077 if( signature_length != 2 * curve_bytes )
3078 return( PSA_ERROR_INVALID_SIGNATURE );
3079
3080 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3081 signature,
3082 curve_bytes ) );
3083 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3084 signature + curve_bytes,
3085 curve_bytes ) );
3086
3087 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3088 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003089
3090cleanup:
3091 mbedtls_mpi_free( &r );
3092 mbedtls_mpi_free( &s );
3093 return( mbedtls_to_psa_error( ret ) );
3094}
3095#endif /* MBEDTLS_ECDSA_C */
3096
Gilles Peskinec5487a82018-12-03 18:08:14 +01003097psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003098 psa_algorithm_t alg,
3099 const uint8_t *hash,
3100 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003101 uint8_t *signature,
3102 size_t signature_size,
3103 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003104{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003105 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003106 psa_status_t status;
3107
3108 *signature_length = signature_size;
3109
Gilles Peskine28f8f302019-07-24 13:30:31 +02003110 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003111 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003112 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003113 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003114 {
3115 status = PSA_ERROR_INVALID_ARGUMENT;
3116 goto exit;
3117 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003118
Gilles Peskine20035e32018-02-03 22:44:14 +01003119#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003120 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003121 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003122 status = psa_rsa_sign( slot->data.rsa,
3123 alg,
3124 hash, hash_length,
3125 signature, signature_size,
3126 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003127 }
3128 else
3129#endif /* defined(MBEDTLS_RSA_C) */
3130#if defined(MBEDTLS_ECP_C)
3131 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3132 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003133#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003134 if(
3135#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3136 PSA_ALG_IS_ECDSA( alg )
3137#else
3138 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3139#endif
3140 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003141 status = psa_ecdsa_sign( slot->data.ecp,
3142 alg,
3143 hash, hash_length,
3144 signature, signature_size,
3145 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003146 else
3147#endif /* defined(MBEDTLS_ECDSA_C) */
3148 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003149 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003150 }
itayzafrir5c753392018-05-08 11:18:38 +03003151 }
3152 else
3153#endif /* defined(MBEDTLS_ECP_C) */
3154 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003155 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003156 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003157
3158exit:
3159 /* Fill the unused part of the output buffer (the whole buffer on error,
3160 * the trailing part on success) with something that isn't a valid mac
3161 * (barring an attack on the mac and deliberately-crafted input),
3162 * in case the caller doesn't check the return status properly. */
3163 if( status == PSA_SUCCESS )
3164 memset( signature + *signature_length, '!',
3165 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003166 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003167 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003168 /* If signature_size is 0 then we have nothing to do. We must not call
3169 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003170 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003171}
3172
Gilles Peskinec5487a82018-12-03 18:08:14 +01003173psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003174 psa_algorithm_t alg,
3175 const uint8_t *hash,
3176 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003177 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003178 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003179{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003180 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003181 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003182
Gilles Peskine28f8f302019-07-24 13:30:31 +02003183 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003184 if( status != PSA_SUCCESS )
3185 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003186
Gilles Peskine61b91d42018-06-08 16:09:36 +02003187#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003188 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003190 return( psa_rsa_verify( slot->data.rsa,
3191 alg,
3192 hash, hash_length,
3193 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194 }
3195 else
3196#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003197#if defined(MBEDTLS_ECP_C)
3198 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3199 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003200#if defined(MBEDTLS_ECDSA_C)
3201 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003202 return( psa_ecdsa_verify( slot->data.ecp,
3203 hash, hash_length,
3204 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003205 else
3206#endif /* defined(MBEDTLS_ECDSA_C) */
3207 {
3208 return( PSA_ERROR_INVALID_ARGUMENT );
3209 }
itayzafrir5c753392018-05-08 11:18:38 +03003210 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003211 else
3212#endif /* defined(MBEDTLS_ECP_C) */
3213 {
3214 return( PSA_ERROR_NOT_SUPPORTED );
3215 }
3216}
3217
Gilles Peskine072ac562018-06-30 00:21:29 +02003218#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3219static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3220 mbedtls_rsa_context *rsa )
3221{
3222 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3223 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3224 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3225 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3226}
3227#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3228
Gilles Peskinec5487a82018-12-03 18:08:14 +01003229psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003230 psa_algorithm_t alg,
3231 const uint8_t *input,
3232 size_t input_length,
3233 const uint8_t *salt,
3234 size_t salt_length,
3235 uint8_t *output,
3236 size_t output_size,
3237 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003239 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003240 psa_status_t status;
3241
Darryl Green5cc689a2018-07-24 15:34:10 +01003242 (void) input;
3243 (void) input_length;
3244 (void) salt;
3245 (void) output;
3246 (void) output_size;
3247
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003248 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003250 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3251 return( PSA_ERROR_INVALID_ARGUMENT );
3252
Gilles Peskine28f8f302019-07-24 13:30:31 +02003253 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 if( status != PSA_SUCCESS )
3255 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003256 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003257 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259
3260#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003261 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 {
3263 mbedtls_rsa_context *rsa = slot->data.rsa;
3264 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003265 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003266 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003268 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003269 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003270 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3271 mbedtls_ctr_drbg_random,
3272 &global_data.ctr_drbg,
3273 MBEDTLS_RSA_PUBLIC,
3274 input_length,
3275 input,
3276 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277 }
3278 else
3279#endif /* MBEDTLS_PKCS1_V15 */
3280#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003281 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003283 psa_rsa_oaep_set_padding_mode( alg, rsa );
3284 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3285 mbedtls_ctr_drbg_random,
3286 &global_data.ctr_drbg,
3287 MBEDTLS_RSA_PUBLIC,
3288 salt, salt_length,
3289 input_length,
3290 input,
3291 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292 }
3293 else
3294#endif /* MBEDTLS_PKCS1_V21 */
3295 {
3296 return( PSA_ERROR_INVALID_ARGUMENT );
3297 }
3298 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003299 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300 return( mbedtls_to_psa_error( ret ) );
3301 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003303#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 {
3305 return( PSA_ERROR_NOT_SUPPORTED );
3306 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003307}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308
Gilles Peskinec5487a82018-12-03 18:08:14 +01003309psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003310 psa_algorithm_t alg,
3311 const uint8_t *input,
3312 size_t input_length,
3313 const uint8_t *salt,
3314 size_t salt_length,
3315 uint8_t *output,
3316 size_t output_size,
3317 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003319 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003320 psa_status_t status;
3321
Darryl Green5cc689a2018-07-24 15:34:10 +01003322 (void) input;
3323 (void) input_length;
3324 (void) salt;
3325 (void) output;
3326 (void) output_size;
3327
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003328 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003330 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3331 return( PSA_ERROR_INVALID_ARGUMENT );
3332
Gilles Peskine28f8f302019-07-24 13:30:31 +02003333 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003334 if( status != PSA_SUCCESS )
3335 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003336 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 return( PSA_ERROR_INVALID_ARGUMENT );
3338
3339#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003340 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341 {
3342 mbedtls_rsa_context *rsa = slot->data.rsa;
3343 int ret;
3344
Gilles Peskine630a18a2018-06-29 17:49:35 +02003345 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003346 return( PSA_ERROR_INVALID_ARGUMENT );
3347
3348#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003349 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003350 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003351 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3352 mbedtls_ctr_drbg_random,
3353 &global_data.ctr_drbg,
3354 MBEDTLS_RSA_PRIVATE,
3355 output_length,
3356 input,
3357 output,
3358 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003359 }
3360 else
3361#endif /* MBEDTLS_PKCS1_V15 */
3362#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003363 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003365 psa_rsa_oaep_set_padding_mode( alg, rsa );
3366 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3367 mbedtls_ctr_drbg_random,
3368 &global_data.ctr_drbg,
3369 MBEDTLS_RSA_PRIVATE,
3370 salt, salt_length,
3371 output_length,
3372 input,
3373 output,
3374 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003375 }
3376 else
3377#endif /* MBEDTLS_PKCS1_V21 */
3378 {
3379 return( PSA_ERROR_INVALID_ARGUMENT );
3380 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003381
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382 return( mbedtls_to_psa_error( ret ) );
3383 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003384 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003385#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003386 {
3387 return( PSA_ERROR_NOT_SUPPORTED );
3388 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003389}
Gilles Peskine20035e32018-02-03 22:44:14 +01003390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003391
3392
mohammad1603503973b2018-03-12 15:59:30 +02003393/****************************************************************/
3394/* Symmetric cryptography */
3395/****************************************************************/
3396
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003397/* Initialize the cipher operation structure. Once this function has been
3398 * called, psa_cipher_abort can run and will do the right thing. */
3399static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3400 psa_algorithm_t alg )
3401{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003402 if( ! PSA_ALG_IS_CIPHER( alg ) )
3403 {
3404 memset( operation, 0, sizeof( *operation ) );
3405 return( PSA_ERROR_INVALID_ARGUMENT );
3406 }
3407
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003408 operation->alg = alg;
3409 operation->key_set = 0;
3410 operation->iv_set = 0;
3411 operation->iv_required = 1;
3412 operation->iv_size = 0;
3413 operation->block_size = 0;
3414 mbedtls_cipher_init( &operation->ctx.cipher );
3415 return( PSA_SUCCESS );
3416}
3417
Gilles Peskinee553c652018-06-04 16:22:46 +02003418static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003419 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003420 psa_algorithm_t alg,
3421 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003422{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003423 int ret = 0;
3424 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003425 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003426 size_t key_bits;
3427 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3429 PSA_KEY_USAGE_ENCRYPT :
3430 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003431
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003432 /* A context must be freshly initialized before it can be set up. */
3433 if( operation->alg != 0 )
3434 {
3435 return( PSA_ERROR_BAD_STATE );
3436 }
3437
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003438 status = psa_cipher_init( operation, alg );
3439 if( status != PSA_SUCCESS )
3440 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003441
Gilles Peskine28f8f302019-07-24 13:30:31 +02003442 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003443 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003444 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003445 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003446
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003447 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003448 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003449 {
3450 status = PSA_ERROR_NOT_SUPPORTED;
3451 goto exit;
3452 }
mohammad1603503973b2018-03-12 15:59:30 +02003453
mohammad1603503973b2018-03-12 15:59:30 +02003454 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003455 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003456 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003457
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003458#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003459 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003460 {
3461 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3462 unsigned char keys[24];
3463 memcpy( keys, slot->data.raw.data, 16 );
3464 memcpy( keys + 16, slot->data.raw.data, 8 );
3465 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3466 keys,
3467 192, cipher_operation );
3468 }
3469 else
3470#endif
3471 {
3472 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3473 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003474 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003475 }
Moran Peker41deec42018-04-04 15:43:05 +03003476 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003477 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003478
mohammad16038481e742018-03-18 13:57:31 +02003479#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003480 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003481 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003482 case PSA_ALG_CBC_NO_PADDING:
3483 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3484 MBEDTLS_PADDING_NONE );
3485 break;
3486 case PSA_ALG_CBC_PKCS7:
3487 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3488 MBEDTLS_PADDING_PKCS7 );
3489 break;
3490 default:
3491 /* The algorithm doesn't involve padding. */
3492 ret = 0;
3493 break;
3494 }
3495 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003496 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003497#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3498
mohammad1603503973b2018-03-12 15:59:30 +02003499 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003500 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3501 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3502 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003503 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003504 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003505 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003506#if defined(MBEDTLS_CHACHA20_C)
3507 else
3508 if( alg == PSA_ALG_CHACHA20 )
3509 operation->iv_size = 12;
3510#endif
mohammad1603503973b2018-03-12 15:59:30 +02003511
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003512exit:
3513 if( status == 0 )
3514 status = mbedtls_to_psa_error( ret );
3515 if( status != 0 )
3516 psa_cipher_abort( operation );
3517 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003518}
3519
Gilles Peskinefe119512018-07-08 21:39:34 +02003520psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003521 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003522 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003523{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003524 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003525}
3526
Gilles Peskinefe119512018-07-08 21:39:34 +02003527psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003528 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003529 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003530{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003531 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003532}
3533
Gilles Peskinefe119512018-07-08 21:39:34 +02003534psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3535 unsigned char *iv,
3536 size_t iv_size,
3537 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003538{
itayzafrir534bd7c2018-08-02 13:56:32 +03003539 psa_status_t status;
3540 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003541 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003542 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003543 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003544 }
Moran Peker41deec42018-04-04 15:43:05 +03003545 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003546 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003547 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003548 goto exit;
3549 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003550 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3551 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003552 if( ret != 0 )
3553 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003554 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003555 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003556 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003557
mohammad16038481e742018-03-18 13:57:31 +02003558 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003559 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003560
Moran Peker395db872018-05-31 14:07:14 +03003561exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003562 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003563 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003564 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003565}
3566
Gilles Peskinefe119512018-07-08 21:39:34 +02003567psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3568 const unsigned char *iv,
3569 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003570{
itayzafrir534bd7c2018-08-02 13:56:32 +03003571 psa_status_t status;
3572 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003573 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003574 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003575 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003576 }
Moran Pekera28258c2018-05-29 16:25:04 +03003577 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003578 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003579 status = PSA_ERROR_INVALID_ARGUMENT;
3580 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003581 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003582 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3583 status = mbedtls_to_psa_error( ret );
3584exit:
3585 if( status == PSA_SUCCESS )
3586 operation->iv_set = 1;
3587 else
mohammad1603503973b2018-03-12 15:59:30 +02003588 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003589 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003590}
3591
Gilles Peskinee553c652018-06-04 16:22:46 +02003592psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3593 const uint8_t *input,
3594 size_t input_length,
3595 unsigned char *output,
3596 size_t output_size,
3597 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003598{
itayzafrir534bd7c2018-08-02 13:56:32 +03003599 psa_status_t status;
3600 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003601 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003602
3603 if( operation->alg == 0 )
3604 {
3605 return( PSA_ERROR_BAD_STATE );
3606 }
3607
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003608 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003609 {
3610 /* Take the unprocessed partial block left over from previous
3611 * update calls, if any, plus the input to this call. Remove
3612 * the last partial block, if any. You get the data that will be
3613 * output in this call. */
3614 expected_output_size =
3615 ( operation->ctx.cipher.unprocessed_len + input_length )
3616 / operation->block_size * operation->block_size;
3617 }
3618 else
3619 {
3620 expected_output_size = input_length;
3621 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003622
Gilles Peskine89d789c2018-06-04 17:17:16 +02003623 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003624 {
3625 status = PSA_ERROR_BUFFER_TOO_SMALL;
3626 goto exit;
3627 }
mohammad160382759612018-03-12 18:16:40 +02003628
mohammad1603503973b2018-03-12 15:59:30 +02003629 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003630 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003631 status = mbedtls_to_psa_error( ret );
3632exit:
3633 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003634 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003635 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003636}
3637
Gilles Peskinee553c652018-06-04 16:22:46 +02003638psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3639 uint8_t *output,
3640 size_t output_size,
3641 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003642{
David Saadab4ecc272019-02-14 13:48:10 +02003643 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003644 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003645 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003646
mohammad1603503973b2018-03-12 15:59:30 +02003647 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003648 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003649 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003650 }
3651 if( operation->iv_required && ! operation->iv_set )
3652 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003653 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003654 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003655
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003656 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003657 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3658 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003659 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003660 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003661 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003662 }
3663
Janos Follath315b51c2018-07-09 16:04:51 +01003664 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3665 temp_output_buffer,
3666 output_length );
3667 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003668 {
Janos Follath315b51c2018-07-09 16:04:51 +01003669 status = mbedtls_to_psa_error( cipher_ret );
3670 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003671 }
Janos Follath315b51c2018-07-09 16:04:51 +01003672
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003673 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003674 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003675 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003676 memcpy( output, temp_output_buffer, *output_length );
3677 else
3678 {
Janos Follath315b51c2018-07-09 16:04:51 +01003679 status = PSA_ERROR_BUFFER_TOO_SMALL;
3680 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003681 }
mohammad1603503973b2018-03-12 15:59:30 +02003682
Gilles Peskine3f108122018-12-07 18:14:53 +01003683 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003684 status = psa_cipher_abort( operation );
3685
3686 return( status );
3687
3688error:
3689
3690 *output_length = 0;
3691
Gilles Peskine3f108122018-12-07 18:14:53 +01003692 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003693 (void) psa_cipher_abort( operation );
3694
3695 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003696}
3697
Gilles Peskinee553c652018-06-04 16:22:46 +02003698psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3699{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003700 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003701 {
3702 /* The object has (apparently) been initialized but it is not
3703 * in use. It's ok to call abort on such an object, and there's
3704 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003705 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003706 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003707
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003708 /* Sanity check (shouldn't happen: operation->alg should
3709 * always have been initialized to a valid value). */
3710 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3711 return( PSA_ERROR_BAD_STATE );
3712
mohammad1603503973b2018-03-12 15:59:30 +02003713 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003714
Moran Peker41deec42018-04-04 15:43:05 +03003715 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003716 operation->key_set = 0;
3717 operation->iv_set = 0;
3718 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003719 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003720 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003721
Moran Peker395db872018-05-31 14:07:14 +03003722 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003723}
3724
Gilles Peskinea0655c32018-04-30 17:06:50 +02003725
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003726
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003727
mohammad16035955c982018-04-26 00:53:03 +03003728/****************************************************************/
3729/* AEAD */
3730/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003731
Gilles Peskineedf9a652018-08-17 18:11:56 +02003732typedef struct
3733{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003734 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003735 const mbedtls_cipher_info_t *cipher_info;
3736 union
3737 {
3738#if defined(MBEDTLS_CCM_C)
3739 mbedtls_ccm_context ccm;
3740#endif /* MBEDTLS_CCM_C */
3741#if defined(MBEDTLS_GCM_C)
3742 mbedtls_gcm_context gcm;
3743#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003744#if defined(MBEDTLS_CHACHAPOLY_C)
3745 mbedtls_chachapoly_context chachapoly;
3746#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003747 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003748 psa_algorithm_t core_alg;
3749 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003750 uint8_t tag_length;
3751} aead_operation_t;
3752
Gilles Peskine30a9e412019-01-14 18:36:12 +01003753static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003754{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003755 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003756 {
3757#if defined(MBEDTLS_CCM_C)
3758 case PSA_ALG_CCM:
3759 mbedtls_ccm_free( &operation->ctx.ccm );
3760 break;
3761#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003762#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003763 case PSA_ALG_GCM:
3764 mbedtls_gcm_free( &operation->ctx.gcm );
3765 break;
3766#endif /* MBEDTLS_GCM_C */
3767 }
3768}
3769
3770static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003771 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003772 psa_key_usage_t usage,
3773 psa_algorithm_t alg )
3774{
3775 psa_status_t status;
3776 size_t key_bits;
3777 mbedtls_cipher_id_t cipher_id;
3778
Gilles Peskine28f8f302019-07-24 13:30:31 +02003779 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780 if( status != PSA_SUCCESS )
3781 return( status );
3782
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003783 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003784
3785 operation->cipher_info =
3786 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3787 &cipher_id );
3788 if( operation->cipher_info == NULL )
3789 return( PSA_ERROR_NOT_SUPPORTED );
3790
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003791 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003792 {
3793#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003794 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3795 operation->core_alg = PSA_ALG_CCM;
3796 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003797 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3798 * The call to mbedtls_ccm_encrypt_and_tag or
3799 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003800 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3801 return( PSA_ERROR_INVALID_ARGUMENT );
3802 mbedtls_ccm_init( &operation->ctx.ccm );
3803 status = mbedtls_to_psa_error(
3804 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3805 operation->slot->data.raw.data,
3806 (unsigned int) key_bits ) );
3807 if( status != 0 )
3808 goto cleanup;
3809 break;
3810#endif /* MBEDTLS_CCM_C */
3811
3812#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003813 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3814 operation->core_alg = PSA_ALG_GCM;
3815 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003816 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3817 * The call to mbedtls_gcm_crypt_and_tag or
3818 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3820 return( PSA_ERROR_INVALID_ARGUMENT );
3821 mbedtls_gcm_init( &operation->ctx.gcm );
3822 status = mbedtls_to_psa_error(
3823 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3824 operation->slot->data.raw.data,
3825 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003826 if( status != 0 )
3827 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828 break;
3829#endif /* MBEDTLS_GCM_C */
3830
Gilles Peskine26869f22019-05-06 15:25:00 +02003831#if defined(MBEDTLS_CHACHAPOLY_C)
3832 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3833 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3834 operation->full_tag_length = 16;
3835 /* We only support the default tag length. */
3836 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3837 return( PSA_ERROR_NOT_SUPPORTED );
3838 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3839 status = mbedtls_to_psa_error(
3840 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3841 operation->slot->data.raw.data ) );
3842 if( status != 0 )
3843 goto cleanup;
3844 break;
3845#endif /* MBEDTLS_CHACHAPOLY_C */
3846
Gilles Peskineedf9a652018-08-17 18:11:56 +02003847 default:
3848 return( PSA_ERROR_NOT_SUPPORTED );
3849 }
3850
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003851 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3852 {
3853 status = PSA_ERROR_INVALID_ARGUMENT;
3854 goto cleanup;
3855 }
3856 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003857
Gilles Peskineedf9a652018-08-17 18:11:56 +02003858 return( PSA_SUCCESS );
3859
3860cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003861 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003862 return( status );
3863}
3864
Gilles Peskinec5487a82018-12-03 18:08:14 +01003865psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003866 psa_algorithm_t alg,
3867 const uint8_t *nonce,
3868 size_t nonce_length,
3869 const uint8_t *additional_data,
3870 size_t additional_data_length,
3871 const uint8_t *plaintext,
3872 size_t plaintext_length,
3873 uint8_t *ciphertext,
3874 size_t ciphertext_size,
3875 size_t *ciphertext_length )
3876{
mohammad16035955c982018-04-26 00:53:03 +03003877 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003878 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003879 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003880
mohammad1603f08a5502018-06-03 15:05:47 +03003881 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003882
Gilles Peskinec5487a82018-12-03 18:08:14 +01003883 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003884 if( status != PSA_SUCCESS )
3885 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003886
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887 /* For all currently supported modes, the tag is at the end of the
3888 * ciphertext. */
3889 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3890 {
3891 status = PSA_ERROR_BUFFER_TOO_SMALL;
3892 goto exit;
3893 }
3894 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003895
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003896#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003897 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003898 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003899 status = mbedtls_to_psa_error(
3900 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3901 MBEDTLS_GCM_ENCRYPT,
3902 plaintext_length,
3903 nonce, nonce_length,
3904 additional_data, additional_data_length,
3905 plaintext, ciphertext,
3906 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003907 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003908 else
3909#endif /* MBEDTLS_GCM_C */
3910#if defined(MBEDTLS_CCM_C)
3911 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003912 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003913 status = mbedtls_to_psa_error(
3914 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3915 plaintext_length,
3916 nonce, nonce_length,
3917 additional_data,
3918 additional_data_length,
3919 plaintext, ciphertext,
3920 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003921 }
mohammad16035c8845f2018-05-09 05:40:09 -07003922 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003923#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003924#if defined(MBEDTLS_CHACHAPOLY_C)
3925 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3926 {
3927 if( nonce_length != 12 || operation.tag_length != 16 )
3928 {
3929 status = PSA_ERROR_NOT_SUPPORTED;
3930 goto exit;
3931 }
3932 status = mbedtls_to_psa_error(
3933 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3934 plaintext_length,
3935 nonce,
3936 additional_data,
3937 additional_data_length,
3938 plaintext,
3939 ciphertext,
3940 tag ) );
3941 }
3942 else
3943#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003944 {
mohammad1603554faad2018-06-03 15:07:38 +03003945 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003946 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003947
Gilles Peskineedf9a652018-08-17 18:11:56 +02003948 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3949 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003950
Gilles Peskineedf9a652018-08-17 18:11:56 +02003951exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003952 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003953 if( status == PSA_SUCCESS )
3954 *ciphertext_length = plaintext_length + operation.tag_length;
3955 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003956}
3957
Gilles Peskineee652a32018-06-01 19:23:52 +02003958/* Locate the tag in a ciphertext buffer containing the encrypted data
3959 * followed by the tag. Return the length of the part preceding the tag in
3960 * *plaintext_length. This is the size of the plaintext in modes where
3961 * the encrypted data has the same size as the plaintext, such as
3962 * CCM and GCM. */
3963static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3964 const uint8_t *ciphertext,
3965 size_t ciphertext_length,
3966 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003967 const uint8_t **p_tag )
3968{
3969 size_t payload_length;
3970 if( tag_length > ciphertext_length )
3971 return( PSA_ERROR_INVALID_ARGUMENT );
3972 payload_length = ciphertext_length - tag_length;
3973 if( payload_length > plaintext_size )
3974 return( PSA_ERROR_BUFFER_TOO_SMALL );
3975 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003976 return( PSA_SUCCESS );
3977}
3978
Gilles Peskinec5487a82018-12-03 18:08:14 +01003979psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003980 psa_algorithm_t alg,
3981 const uint8_t *nonce,
3982 size_t nonce_length,
3983 const uint8_t *additional_data,
3984 size_t additional_data_length,
3985 const uint8_t *ciphertext,
3986 size_t ciphertext_length,
3987 uint8_t *plaintext,
3988 size_t plaintext_size,
3989 size_t *plaintext_length )
3990{
mohammad16035955c982018-04-26 00:53:03 +03003991 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003992 aead_operation_t operation;
3993 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003994
Gilles Peskineee652a32018-06-01 19:23:52 +02003995 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003996
Gilles Peskinec5487a82018-12-03 18:08:14 +01003997 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003998 if( status != PSA_SUCCESS )
3999 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004000
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004001 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4002 ciphertext, ciphertext_length,
4003 plaintext_size, &tag );
4004 if( status != PSA_SUCCESS )
4005 goto exit;
4006
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004007#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004008 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004009 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004010 status = mbedtls_to_psa_error(
4011 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4012 ciphertext_length - operation.tag_length,
4013 nonce, nonce_length,
4014 additional_data,
4015 additional_data_length,
4016 tag, operation.tag_length,
4017 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004018 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004019 else
4020#endif /* MBEDTLS_GCM_C */
4021#if defined(MBEDTLS_CCM_C)
4022 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004023 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004024 status = mbedtls_to_psa_error(
4025 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4026 ciphertext_length - operation.tag_length,
4027 nonce, nonce_length,
4028 additional_data,
4029 additional_data_length,
4030 ciphertext, plaintext,
4031 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004032 }
mohammad160339574652018-06-01 04:39:53 -07004033 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004034#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004035#if defined(MBEDTLS_CHACHAPOLY_C)
4036 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4037 {
4038 if( nonce_length != 12 || operation.tag_length != 16 )
4039 {
4040 status = PSA_ERROR_NOT_SUPPORTED;
4041 goto exit;
4042 }
4043 status = mbedtls_to_psa_error(
4044 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4045 ciphertext_length - operation.tag_length,
4046 nonce,
4047 additional_data,
4048 additional_data_length,
4049 tag,
4050 ciphertext,
4051 plaintext ) );
4052 }
4053 else
4054#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004055 {
mohammad1603554faad2018-06-03 15:07:38 +03004056 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004057 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004058
Gilles Peskineedf9a652018-08-17 18:11:56 +02004059 if( status != PSA_SUCCESS && plaintext_size != 0 )
4060 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004061
Gilles Peskineedf9a652018-08-17 18:11:56 +02004062exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004063 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004064 if( status == PSA_SUCCESS )
4065 *plaintext_length = ciphertext_length - operation.tag_length;
4066 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004067}
4068
Gilles Peskinea0655c32018-04-30 17:06:50 +02004069
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004070
Gilles Peskine20035e32018-02-03 22:44:14 +01004071/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004072/* Generators */
4073/****************************************************************/
4074
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004075#define HKDF_STATE_INIT 0 /* no input yet */
4076#define HKDF_STATE_STARTED 1 /* got salt */
4077#define HKDF_STATE_KEYED 2 /* got key */
4078#define HKDF_STATE_OUTPUT 3 /* output started */
4079
Gilles Peskinecbe66502019-05-16 16:59:18 +02004080static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004081 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004082{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004083 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4084 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004085 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004086 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004087}
4088
4089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004090psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004091{
4092 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004094 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004095 {
4096 /* The object has (apparently) been initialized but it is not
4097 * in use. It's ok to call abort on such an object, and there's
4098 * nothing to do. */
4099 }
4100 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004101#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004102 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004103 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104 mbedtls_free( operation->ctx.hkdf.info );
4105 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004106 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004107 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004109 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004110 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004111#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004112 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004113 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004114 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4115 operation->ctx.tls12_prf.key_len );
4116 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004117 }
Hanno Becker580fba12018-11-13 20:50:45 +00004118
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004119 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004120 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004121 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4122 operation->ctx.tls12_prf.Ai_with_seed_len );
4123 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004124 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004125#else
4126 if( operation->ctx.tls12_prf.seed != NULL )
4127 {
4128 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4129 operation->ctx.tls12_prf.seed_length );
4130 mbedtls_free( operation->ctx.tls12_prf.seed );
4131 }
4132
4133 if( operation->ctx.tls12_prf.label != NULL )
4134 {
4135 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4136 operation->ctx.tls12_prf.label_length );
4137 mbedtls_free( operation->ctx.tls12_prf.label );
4138 }
4139
4140 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4141
4142 /* We leave the fields Ai and output_block to be erased safely by the
4143 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004144#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004145 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004146 else
4147#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004148 {
4149 status = PSA_ERROR_BAD_STATE;
4150 }
Janos Follath083036a2019-06-11 10:22:26 +01004151 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004152 return( status );
4153}
4154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004155psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004156 size_t *capacity)
4157{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004158 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004159 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004161 return PSA_ERROR_BAD_STATE;
4162 }
4163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004165 return( PSA_SUCCESS );
4166}
4167
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004168psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004169 size_t capacity )
4170{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004172 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004173 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004174 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004175 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004176 return( PSA_SUCCESS );
4177}
4178
Gilles Peskinebef7f142018-07-12 17:22:21 +02004179#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004180/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004181 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004182static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004183 psa_algorithm_t hash_alg,
4184 uint8_t *output,
4185 size_t output_length )
4186{
4187 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4188 psa_status_t status;
4189
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004190 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4191 return( PSA_ERROR_BAD_STATE );
4192 hkdf->state = HKDF_STATE_OUTPUT;
4193
Gilles Peskinebef7f142018-07-12 17:22:21 +02004194 while( output_length != 0 )
4195 {
4196 /* Copy what remains of the current block */
4197 uint8_t n = hash_length - hkdf->offset_in_block;
4198 if( n > output_length )
4199 n = (uint8_t) output_length;
4200 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4201 output += n;
4202 output_length -= n;
4203 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004204 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004205 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004206 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004207 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004209 * object was corrupted or if this function is called directly
4210 * inside the library. */
4211 if( hkdf->block_number == 0xff )
4212 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004213
4214 /* We need a new block */
4215 ++hkdf->block_number;
4216 hkdf->offset_in_block = 0;
4217 status = psa_hmac_setup_internal( &hkdf->hmac,
4218 hkdf->prk, hash_length,
4219 hash_alg );
4220 if( status != PSA_SUCCESS )
4221 return( status );
4222 if( hkdf->block_number != 1 )
4223 {
4224 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4225 hkdf->output_block,
4226 hash_length );
4227 if( status != PSA_SUCCESS )
4228 return( status );
4229 }
4230 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4231 hkdf->info,
4232 hkdf->info_length );
4233 if( status != PSA_SUCCESS )
4234 return( status );
4235 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4236 &hkdf->block_number, 1 );
4237 if( status != PSA_SUCCESS )
4238 return( status );
4239 status = psa_hmac_finish_internal( &hkdf->hmac,
4240 hkdf->output_block,
4241 sizeof( hkdf->output_block ) );
4242 if( status != PSA_SUCCESS )
4243 return( status );
4244 }
4245
4246 return( PSA_SUCCESS );
4247}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004248
Janos Follath999f6482019-06-11 12:04:10 +01004249#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004250static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4251 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004252 psa_algorithm_t alg )
4253{
4254 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4255 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4256 psa_hmac_internal_data hmac;
4257 psa_status_t status, cleanup_status;
4258
Hanno Becker3b339e22018-11-13 20:56:14 +00004259 unsigned char *Ai;
4260 size_t Ai_len;
4261
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004262 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004263 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004265 * object was corrupted or if this function is called directly
4266 * inside the library. */
4267 if( tls12_prf->block_number == 0xff )
4268 return( PSA_ERROR_BAD_STATE );
4269
4270 /* We need a new block */
4271 ++tls12_prf->block_number;
4272 tls12_prf->offset_in_block = 0;
4273
4274 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4275 *
4276 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4277 *
4278 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4279 * HMAC_hash(secret, A(2) + seed) +
4280 * HMAC_hash(secret, A(3) + seed) + ...
4281 *
4282 * A(0) = seed
4283 * A(i) = HMAC_hash( secret, A(i-1) )
4284 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004285 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004286 * `HMAC_hash(secret, A(i) + seed)` from which the output
4287 * is currently extracted as `output_block`, while
4288 * `A(i) + seed` is stored in `Ai_with_seed`.
4289 *
4290 * Generating a new block means recalculating `Ai_with_seed`
4291 * from the A(i)-part of it, and afterwards recalculating
4292 * `output_block`.
4293 *
4294 * A(0) is computed at setup time.
4295 *
4296 */
4297
4298 psa_hmac_init_internal( &hmac );
4299
4300 /* We must distinguish the calculation of A(1) from those
4301 * of A(2) and higher, because A(0)=seed has a different
4302 * length than the other A(i). */
4303 if( tls12_prf->block_number == 1 )
4304 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004305 Ai = tls12_prf->Ai_with_seed + hash_length;
4306 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004307 }
4308 else
4309 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004310 Ai = tls12_prf->Ai_with_seed;
4311 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004312 }
4313
Hanno Becker3b339e22018-11-13 20:56:14 +00004314 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4315 status = psa_hmac_setup_internal( &hmac,
4316 tls12_prf->key,
4317 tls12_prf->key_len,
4318 hash_alg );
4319 if( status != PSA_SUCCESS )
4320 goto cleanup;
4321
4322 status = psa_hash_update( &hmac.hash_ctx,
4323 Ai, Ai_len );
4324 if( status != PSA_SUCCESS )
4325 goto cleanup;
4326
4327 status = psa_hmac_finish_internal( &hmac,
4328 tls12_prf->Ai_with_seed,
4329 hash_length );
4330 if( status != PSA_SUCCESS )
4331 goto cleanup;
4332
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004333 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4334 status = psa_hmac_setup_internal( &hmac,
4335 tls12_prf->key,
4336 tls12_prf->key_len,
4337 hash_alg );
4338 if( status != PSA_SUCCESS )
4339 goto cleanup;
4340
4341 status = psa_hash_update( &hmac.hash_ctx,
4342 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004343 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004344 if( status != PSA_SUCCESS )
4345 goto cleanup;
4346
4347 status = psa_hmac_finish_internal( &hmac,
4348 tls12_prf->output_block,
4349 hash_length );
4350 if( status != PSA_SUCCESS )
4351 goto cleanup;
4352
4353cleanup:
4354
4355 cleanup_status = psa_hmac_abort_internal( &hmac );
4356 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4357 status = cleanup_status;
4358
4359 return( status );
4360}
Janos Follath7742fee2019-06-17 12:58:10 +01004361#else
4362static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4363 psa_tls12_prf_key_derivation_t *tls12_prf,
4364 psa_algorithm_t alg )
4365{
4366 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4367 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004368 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4369 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370
Janos Follath7742fee2019-06-17 12:58:10 +01004371 /* We can't be wanting more output after block 0xff, otherwise
4372 * the capacity check in psa_key_derivation_output_bytes() would have
4373 * prevented this call. It could happen only if the operation
4374 * object was corrupted or if this function is called directly
4375 * inside the library. */
4376 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004377 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004378
4379 /* We need a new block */
4380 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004381 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004382
4383 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4384 *
4385 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4386 *
4387 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4388 * HMAC_hash(secret, A(2) + seed) +
4389 * HMAC_hash(secret, A(3) + seed) + ...
4390 *
4391 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004392 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004393 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004394 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004395 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004396 * is currently extracted as `output_block` and where i is
4397 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004398 */
4399
Janos Follathea29bfb2019-06-19 12:21:20 +01004400 /* Save the hash context before using it, to preserve the hash state with
4401 * only the inner padding in it. We need this, because inner padding depends
4402 * on the key (secret in the RFC's terminology). */
4403 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4404 if( status != PSA_SUCCESS )
4405 goto cleanup;
4406
4407 /* Calculate A(i) where i = tls12_prf->block_number. */
4408 if( tls12_prf->block_number == 1 )
4409 {
4410 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4411 * the variable seed and in this instance means it in the context of the
4412 * P_hash function, where seed = label + seed.) */
4413 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4414 tls12_prf->label, tls12_prf->label_length );
4415 if( status != PSA_SUCCESS )
4416 goto cleanup;
4417 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4418 tls12_prf->seed, tls12_prf->seed_length );
4419 if( status != PSA_SUCCESS )
4420 goto cleanup;
4421 }
4422 else
4423 {
4424 /* A(i) = HMAC_hash(secret, A(i-1)) */
4425 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4426 tls12_prf->Ai, hash_length );
4427 if( status != PSA_SUCCESS )
4428 goto cleanup;
4429 }
4430
4431 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4432 tls12_prf->Ai, hash_length );
4433 if( status != PSA_SUCCESS )
4434 goto cleanup;
4435 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4436 if( status != PSA_SUCCESS )
4437 goto cleanup;
4438
4439 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4440 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4441 tls12_prf->Ai, hash_length );
4442 if( status != PSA_SUCCESS )
4443 goto cleanup;
4444 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4445 tls12_prf->label, tls12_prf->label_length );
4446 if( status != PSA_SUCCESS )
4447 goto cleanup;
4448 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4449 tls12_prf->seed, tls12_prf->seed_length );
4450 if( status != PSA_SUCCESS )
4451 goto cleanup;
4452 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4453 tls12_prf->output_block, hash_length );
4454 if( status != PSA_SUCCESS )
4455 goto cleanup;
4456 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4457 if( status != PSA_SUCCESS )
4458 goto cleanup;
4459
Janos Follath7742fee2019-06-17 12:58:10 +01004460
4461cleanup:
4462
Janos Follathea29bfb2019-06-19 12:21:20 +01004463 cleanup_status = psa_hash_abort( &backup );
4464 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4465 status = cleanup_status;
4466
4467 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004468}
Janos Follath999f6482019-06-11 12:04:10 +01004469#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004470
Janos Follath999f6482019-06-11 12:04:10 +01004471#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004473 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004474static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004475 psa_tls12_prf_key_derivation_t *tls12_prf,
4476 psa_algorithm_t alg,
4477 uint8_t *output,
4478 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004479{
4480 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4481 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4482 psa_status_t status;
4483
4484 while( output_length != 0 )
4485 {
4486 /* Copy what remains of the current block */
4487 uint8_t n = hash_length - tls12_prf->offset_in_block;
4488
4489 /* Check if we have fully processed the current block. */
4490 if( n == 0 )
4491 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004492 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004493 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004494 if( status != PSA_SUCCESS )
4495 return( status );
4496
4497 continue;
4498 }
4499
4500 if( n > output_length )
4501 n = (uint8_t) output_length;
4502 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4503 n );
4504 output += n;
4505 output_length -= n;
4506 tls12_prf->offset_in_block += n;
4507 }
4508
4509 return( PSA_SUCCESS );
4510}
Janos Follath844eb0e2019-06-19 12:10:49 +01004511#else
4512static psa_status_t psa_key_derivation_tls12_prf_read(
4513 psa_tls12_prf_key_derivation_t *tls12_prf,
4514 psa_algorithm_t alg,
4515 uint8_t *output,
4516 size_t output_length )
4517{
4518 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4519 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4520 psa_status_t status;
4521 uint8_t offset, length;
4522
4523 while( output_length != 0 )
4524 {
4525 /* Check if we have fully processed the current block. */
4526 if( tls12_prf->left_in_block == 0 )
4527 {
4528 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4529 alg );
4530 if( status != PSA_SUCCESS )
4531 return( status );
4532
4533 continue;
4534 }
4535
4536 if( tls12_prf->left_in_block > output_length )
4537 length = (uint8_t) output_length;
4538 else
4539 length = tls12_prf->left_in_block;
4540
4541 offset = hash_length - tls12_prf->left_in_block;
4542 memcpy( output, tls12_prf->output_block + offset, length );
4543 output += length;
4544 output_length -= length;
4545 tls12_prf->left_in_block -= length;
4546 }
4547
4548 return( PSA_SUCCESS );
4549}
Janos Follath999f6482019-06-11 12:04:10 +01004550#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004551#endif /* MBEDTLS_MD_C */
4552
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004553psa_status_t psa_key_derivation_output_bytes(
4554 psa_key_derivation_operation_t *operation,
4555 uint8_t *output,
4556 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004557{
4558 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004560
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004561 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004562 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004564 return PSA_ERROR_BAD_STATE;
4565 }
4566
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004568 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004570 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004571 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004572 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004573 goto exit;
4574 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004576 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004578 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004579 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4580 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004582 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004583 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004584 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004586
Gilles Peskinebef7f142018-07-12 17:22:21 +02004587#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004588 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004589 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004590 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004591 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004592 output, output_length );
4593 }
Janos Follathadbec812019-06-14 11:05:39 +01004594 else
Janos Follathadbec812019-06-14 11:05:39 +01004595 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004596 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004597 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004599 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004600 output_length );
4601 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004602 else
4603#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004604 {
4605 return( PSA_ERROR_BAD_STATE );
4606 }
4607
4608exit:
4609 if( status != PSA_SUCCESS )
4610 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004611 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 * This allows us to differentiate between exhausted operations and
4613 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4614 * operations. */
4615 psa_algorithm_t alg = operation->alg;
4616 psa_key_derivation_abort( operation );
4617 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004618 memset( output, '!', output_length );
4619 }
4620 return( status );
4621}
4622
Gilles Peskine08542d82018-07-19 17:05:42 +02004623#if defined(MBEDTLS_DES_C)
4624static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4625{
4626 if( data_size >= 8 )
4627 mbedtls_des_key_set_parity( data );
4628 if( data_size >= 16 )
4629 mbedtls_des_key_set_parity( data + 8 );
4630 if( data_size >= 24 )
4631 mbedtls_des_key_set_parity( data + 16 );
4632}
4633#endif /* MBEDTLS_DES_C */
4634
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004635static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004636 psa_key_slot_t *slot,
4637 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004639{
4640 uint8_t *data = NULL;
4641 size_t bytes = PSA_BITS_TO_BYTES( bits );
4642 psa_status_t status;
4643
4644 if( ! key_type_is_raw_bytes( slot->type ) )
4645 return( PSA_ERROR_INVALID_ARGUMENT );
4646 if( bits % 8 != 0 )
4647 return( PSA_ERROR_INVALID_ARGUMENT );
4648 data = mbedtls_calloc( 1, bytes );
4649 if( data == NULL )
4650 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4651
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004653 if( status != PSA_SUCCESS )
4654 goto exit;
4655#if defined(MBEDTLS_DES_C)
4656 if( slot->type == PSA_KEY_TYPE_DES )
4657 psa_des_set_key_parity( data, bytes );
4658#endif /* MBEDTLS_DES_C */
4659 status = psa_import_key_into_slot( slot, data, bytes );
4660
4661exit:
4662 mbedtls_free( data );
4663 return( status );
4664}
4665
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004666psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004668 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004669{
4670 psa_status_t status;
4671 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004672 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004673 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4675 if( driver != NULL )
4676 {
4677 /* Deriving a key in a secure element is not implemented yet. */
4678 status = PSA_ERROR_NOT_SUPPORTED;
4679 }
4680#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 if( status == PSA_SUCCESS )
4682 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004683 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004684 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004686 }
4687 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004688 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004689 if( status != PSA_SUCCESS )
4690 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004691 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004692 *handle = 0;
4693 }
4694 return( status );
4695}
4696
Gilles Peskineeab56e42018-07-12 17:12:33 +02004697
4698
4699/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004700/* Key derivation */
4701/****************************************************************/
4702
Gilles Peskinea05219c2018-11-16 16:02:56 +01004703#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004704#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004706 * of the HKDF algorithm.
4707 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004708 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004709 * to potentially free embedded data structures and wipe confidential data.
4710 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004711static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004712 const uint8_t *secret,
4713 size_t secret_length,
4714 psa_algorithm_t hash_alg,
4715 const uint8_t *salt,
4716 size_t salt_length,
4717 const uint8_t *label,
4718 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004719{
4720 psa_status_t status;
4721 status = psa_hmac_setup_internal( &hkdf->hmac,
4722 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004723 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004724 if( status != PSA_SUCCESS )
4725 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004726 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004727 if( status != PSA_SUCCESS )
4728 return( status );
4729 status = psa_hmac_finish_internal( &hkdf->hmac,
4730 hkdf->prk,
4731 sizeof( hkdf->prk ) );
4732 if( status != PSA_SUCCESS )
4733 return( status );
4734 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4735 hkdf->block_number = 0;
4736 hkdf->info_length = label_length;
4737 if( label_length != 0 )
4738 {
4739 hkdf->info = mbedtls_calloc( 1, label_length );
4740 if( hkdf->info == NULL )
4741 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4742 memcpy( hkdf->info, label, label_length );
4743 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004744 hkdf->state = HKDF_STATE_KEYED;
4745 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004746 return( PSA_SUCCESS );
4747}
Janos Follath71a4c912019-06-11 09:14:47 +01004748#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004749#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004750
Gilles Peskinea05219c2018-11-16 16:02:56 +01004751#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004752#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004754 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004755 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004756 * to potentially free embedded data structures and wipe confidential data.
4757 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004758static psa_status_t psa_key_derivation_tls12_prf_setup(
4759 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004760 const unsigned char *key,
4761 size_t key_len,
4762 psa_algorithm_t hash_alg,
4763 const uint8_t *salt,
4764 size_t salt_length,
4765 const uint8_t *label,
4766 size_t label_length )
4767{
4768 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004769 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4770 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004771
4772 tls12_prf->key = mbedtls_calloc( 1, key_len );
4773 if( tls12_prf->key == NULL )
4774 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4775 tls12_prf->key_len = key_len;
4776 memcpy( tls12_prf->key, key, key_len );
4777
Hanno Becker580fba12018-11-13 20:50:45 +00004778 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004779 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004780 if( overflow )
4781 return( PSA_ERROR_INVALID_ARGUMENT );
4782
4783 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4784 if( tls12_prf->Ai_with_seed == NULL )
4785 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4786 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4787
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004788 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4789 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004790 if( label_length != 0 )
4791 {
4792 memcpy( tls12_prf->Ai_with_seed + hash_length,
4793 label, label_length );
4794 }
4795
4796 if( salt_length != 0 )
4797 {
4798 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4799 salt, salt_length );
4800 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004801
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004802 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004803 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004804 tls12_prf->block_number = 0;
4805 tls12_prf->offset_in_block = hash_length;
4806
4807 return( PSA_SUCCESS );
4808}
Janos Follath71a4c912019-06-11 09:14:47 +01004809#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004810
Janos Follath71a4c912019-06-11 09:14:47 +01004811#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004813static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4814 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004815 const unsigned char *psk,
4816 size_t psk_len,
4817 psa_algorithm_t hash_alg,
4818 const uint8_t *salt,
4819 size_t salt_length,
4820 const uint8_t *label,
4821 size_t label_length )
4822{
4823 psa_status_t status;
4824 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4825
4826 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4827 return( PSA_ERROR_INVALID_ARGUMENT );
4828
4829 /* Quoting RFC 4279, Section 2:
4830 *
4831 * The premaster secret is formed as follows: if the PSK is N octets
4832 * long, concatenate a uint16 with the value N, N zero octets, a second
4833 * uint16 with the value N, and the PSK itself.
4834 */
4835
4836 pms[0] = ( psk_len >> 8 ) & 0xff;
4837 pms[1] = ( psk_len >> 0 ) & 0xff;
4838 memset( pms + 2, 0, psk_len );
4839 pms[2 + psk_len + 0] = pms[0];
4840 pms[2 + psk_len + 1] = pms[1];
4841 memcpy( pms + 4 + psk_len, psk, psk_len );
4842
Gilles Peskinecbe66502019-05-16 16:59:18 +02004843 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004844 pms, 4 + 2 * psk_len,
4845 hash_alg,
4846 salt, salt_length,
4847 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004848
Gilles Peskine3f108122018-12-07 18:14:53 +01004849 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004850 return( status );
4851}
Janos Follath71a4c912019-06-11 09:14:47 +01004852#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004853#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004854
Janos Follath71a4c912019-06-11 09:14:47 +01004855#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004856/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004857 * to potentially free embedded data structures and wipe confidential data.
4858 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004859static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004861 const uint8_t *secret, size_t secret_length,
4862 psa_algorithm_t alg,
4863 const uint8_t *salt, size_t salt_length,
4864 const uint8_t *label, size_t label_length,
4865 size_t capacity )
4866{
4867 psa_status_t status;
4868 size_t max_capacity;
4869
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 /* Set operation->alg even on failure so that abort knows what to do. */
4871 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004872
4873#if defined(MBEDTLS_MD_C)
4874 if( PSA_ALG_IS_HKDF( alg ) )
4875 {
4876 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4877 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4878 if( hash_size == 0 )
4879 return( PSA_ERROR_NOT_SUPPORTED );
4880 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004882 secret, secret_length,
4883 hash_alg,
4884 salt, salt_length,
4885 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004886 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004887 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4888 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4889 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004890 {
4891 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4892 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4893
4894 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4895 if( hash_alg != PSA_ALG_SHA_256 &&
4896 hash_alg != PSA_ALG_SHA_384 )
4897 {
4898 return( PSA_ERROR_NOT_SUPPORTED );
4899 }
4900
4901 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004902
4903 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4904 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004906 secret, secret_length,
4907 hash_alg, salt, salt_length,
4908 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004909 }
4910 else
4911 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004912 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004914 secret, secret_length,
4915 hash_alg, salt, salt_length,
4916 label, label_length );
4917 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004918 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004919 else
4920#endif
4921 {
4922 return( PSA_ERROR_NOT_SUPPORTED );
4923 }
4924
4925 if( status != PSA_SUCCESS )
4926 return( status );
4927
4928 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004930 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004932 else
4933 return( PSA_ERROR_INVALID_ARGUMENT );
4934
4935 return( PSA_SUCCESS );
4936}
Janos Follath71a4c912019-06-11 09:14:47 +01004937#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004938
Janos Follath71a4c912019-06-11 09:14:47 +01004939#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004941 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004942 psa_algorithm_t alg,
4943 const uint8_t *salt,
4944 size_t salt_length,
4945 const uint8_t *label,
4946 size_t label_length,
4947 size_t capacity )
4948{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004949 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004950 psa_status_t status;
4951
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004953 return( PSA_ERROR_BAD_STATE );
4954
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004955 /* Make sure that alg is a key derivation algorithm. This prevents
4956 * key selection algorithms, which psa_key_derivation_internal
4957 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004958 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4959 return( PSA_ERROR_INVALID_ARGUMENT );
4960
Gilles Peskine28f8f302019-07-24 13:30:31 +02004961 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004962 if( status != PSA_SUCCESS )
4963 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004964
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004965 if( slot->type != PSA_KEY_TYPE_DERIVE )
4966 return( PSA_ERROR_INVALID_ARGUMENT );
4967
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004969 slot->data.raw.data,
4970 slot->data.raw.bytes,
4971 alg,
4972 salt, salt_length,
4973 label, label_length,
4974 capacity );
4975 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004977 return( status );
4978}
Janos Follath71a4c912019-06-11 09:14:47 +01004979#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004980
Gilles Peskine969c5d62019-01-16 15:53:06 +01004981static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004983 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004984{
Janos Follath5fe19732019-06-20 15:09:30 +01004985 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4986 * initialisers for this union leave some bytes unspecified.) */
4987 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4988
Gilles Peskine969c5d62019-01-16 15:53:06 +01004989 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004990#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004991 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4992 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4993 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004994 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004996 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4997 if( hash_size == 0 )
4998 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004999 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5000 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005001 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005002 {
5003 return( PSA_ERROR_NOT_SUPPORTED );
5004 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005006 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005007 }
5008#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005009 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005010 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005011}
5012
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005014 psa_algorithm_t alg )
5015{
5016 psa_status_t status;
5017
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005019 return( PSA_ERROR_BAD_STATE );
5020
Gilles Peskine6843c292019-01-18 16:44:49 +01005021 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5022 return( PSA_ERROR_INVALID_ARGUMENT );
5023 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024 {
5025 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005027 }
5028 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5029 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005031 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 else
5033 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034
5035 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005036 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005037 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038}
5039
5040#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005041static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005042 psa_algorithm_t hash_alg,
5043 psa_key_derivation_step_t step,
5044 const uint8_t *data,
5045 size_t data_length )
5046{
5047 psa_status_t status;
5048 switch( step )
5049 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005050 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005051 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005052 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005053 status = psa_hmac_setup_internal( &hkdf->hmac,
5054 data, data_length,
5055 hash_alg );
5056 if( status != PSA_SUCCESS )
5057 return( status );
5058 hkdf->state = HKDF_STATE_STARTED;
5059 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005060 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005061 /* If no salt was provided, use an empty salt. */
5062 if( hkdf->state == HKDF_STATE_INIT )
5063 {
5064 status = psa_hmac_setup_internal( &hkdf->hmac,
5065 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005066 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067 if( status != PSA_SUCCESS )
5068 return( status );
5069 hkdf->state = HKDF_STATE_STARTED;
5070 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005071 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005072 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005073 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5074 data, data_length );
5075 if( status != PSA_SUCCESS )
5076 return( status );
5077 status = psa_hmac_finish_internal( &hkdf->hmac,
5078 hkdf->prk,
5079 sizeof( hkdf->prk ) );
5080 if( status != PSA_SUCCESS )
5081 return( status );
5082 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5083 hkdf->block_number = 0;
5084 hkdf->state = HKDF_STATE_KEYED;
5085 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005086 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087 if( hkdf->state == HKDF_STATE_OUTPUT )
5088 return( PSA_ERROR_BAD_STATE );
5089 if( hkdf->info_set )
5090 return( PSA_ERROR_BAD_STATE );
5091 hkdf->info_length = data_length;
5092 if( data_length != 0 )
5093 {
5094 hkdf->info = mbedtls_calloc( 1, data_length );
5095 if( hkdf->info == NULL )
5096 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5097 memcpy( hkdf->info, data, data_length );
5098 }
5099 hkdf->info_set = 1;
5100 return( PSA_SUCCESS );
5101 default:
5102 return( PSA_ERROR_INVALID_ARGUMENT );
5103 }
5104}
Janos Follathb03233e2019-06-11 15:30:30 +01005105
5106#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5107static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5108 psa_algorithm_t hash_alg,
5109 psa_key_derivation_step_t step,
5110 const uint8_t *data,
5111 size_t data_length )
5112{
5113 (void) prf;
5114 (void) hash_alg;
5115 (void) step;
5116 (void) data;
5117 (void) data_length;
5118
5119 return( PSA_ERROR_INVALID_ARGUMENT );
5120}
Janos Follath6660f0e2019-06-17 08:44:03 +01005121
5122static psa_status_t psa_tls12_prf_psk_to_ms_input(
5123 psa_tls12_prf_key_derivation_t *prf,
5124 psa_algorithm_t hash_alg,
5125 psa_key_derivation_step_t step,
5126 const uint8_t *data,
5127 size_t data_length )
5128{
5129 (void) prf;
5130 (void) hash_alg;
5131 (void) step;
5132 (void) data;
5133 (void) data_length;
5134
5135 return( PSA_ERROR_INVALID_ARGUMENT );
5136}
Janos Follathb03233e2019-06-11 15:30:30 +01005137#else
Janos Follathf08e2652019-06-13 09:05:41 +01005138static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5139 const uint8_t *data,
5140 size_t data_length )
5141{
5142 if( prf->state != TLS12_PRF_STATE_INIT )
5143 return( PSA_ERROR_BAD_STATE );
5144
Janos Follathd6dce9f2019-07-04 09:11:38 +01005145 if( data_length != 0 )
5146 {
5147 prf->seed = mbedtls_calloc( 1, data_length );
5148 if( prf->seed == NULL )
5149 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005150
Janos Follathd6dce9f2019-07-04 09:11:38 +01005151 memcpy( prf->seed, data, data_length );
5152 prf->seed_length = data_length;
5153 }
Janos Follathf08e2652019-06-13 09:05:41 +01005154
5155 prf->state = TLS12_PRF_STATE_SEED_SET;
5156
5157 return( PSA_SUCCESS );
5158}
5159
Janos Follath81550542019-06-13 14:26:34 +01005160static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5161 psa_algorithm_t hash_alg,
5162 const uint8_t *data,
5163 size_t data_length )
5164{
5165 psa_status_t status;
5166 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5167 return( PSA_ERROR_BAD_STATE );
5168
5169 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5170 if( status != PSA_SUCCESS )
5171 return( status );
5172
5173 prf->state = TLS12_PRF_STATE_KEY_SET;
5174
5175 return( PSA_SUCCESS );
5176}
5177
Janos Follath6660f0e2019-06-17 08:44:03 +01005178static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5179 psa_tls12_prf_key_derivation_t *prf,
5180 psa_algorithm_t hash_alg,
5181 const uint8_t *data,
5182 size_t data_length )
5183{
5184 psa_status_t status;
5185 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005186 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005187
5188 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5189 return( PSA_ERROR_INVALID_ARGUMENT );
5190
5191 /* Quoting RFC 4279, Section 2:
5192 *
5193 * The premaster secret is formed as follows: if the PSK is N octets
5194 * long, concatenate a uint16 with the value N, N zero octets, a second
5195 * uint16 with the value N, and the PSK itself.
5196 */
5197
Janos Follath40e13932019-06-26 13:22:29 +01005198 *cur++ = ( data_length >> 8 ) & 0xff;
5199 *cur++ = ( data_length >> 0 ) & 0xff;
5200 memset( cur, 0, data_length );
5201 cur += data_length;
5202 *cur++ = pms[0];
5203 *cur++ = pms[1];
5204 memcpy( cur, data, data_length );
5205 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005206
Janos Follath40e13932019-06-26 13:22:29 +01005207 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005208
5209 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5210 return( status );
5211}
5212
Janos Follath63028dd2019-06-13 09:15:47 +01005213static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5214 const uint8_t *data,
5215 size_t data_length )
5216{
5217 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5218 return( PSA_ERROR_BAD_STATE );
5219
Janos Follathd6dce9f2019-07-04 09:11:38 +01005220 if( data_length != 0 )
5221 {
5222 prf->label = mbedtls_calloc( 1, data_length );
5223 if( prf->label == NULL )
5224 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005225
Janos Follathd6dce9f2019-07-04 09:11:38 +01005226 memcpy( prf->label, data, data_length );
5227 prf->label_length = data_length;
5228 }
Janos Follath63028dd2019-06-13 09:15:47 +01005229
5230 prf->state = TLS12_PRF_STATE_LABEL_SET;
5231
5232 return( PSA_SUCCESS );
5233}
5234
Janos Follathb03233e2019-06-11 15:30:30 +01005235static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5236 psa_algorithm_t hash_alg,
5237 psa_key_derivation_step_t step,
5238 const uint8_t *data,
5239 size_t data_length )
5240{
Janos Follathb03233e2019-06-11 15:30:30 +01005241 switch( step )
5242 {
Janos Follathf08e2652019-06-13 09:05:41 +01005243 case PSA_KEY_DERIVATION_INPUT_SEED:
5244 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005245 case PSA_KEY_DERIVATION_INPUT_SECRET:
5246 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005247 case PSA_KEY_DERIVATION_INPUT_LABEL:
5248 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005249 default:
5250 return( PSA_ERROR_INVALID_ARGUMENT );
5251 }
5252}
Janos Follath6660f0e2019-06-17 08:44:03 +01005253
5254static psa_status_t psa_tls12_prf_psk_to_ms_input(
5255 psa_tls12_prf_key_derivation_t *prf,
5256 psa_algorithm_t hash_alg,
5257 psa_key_derivation_step_t step,
5258 const uint8_t *data,
5259 size_t data_length )
5260{
5261 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005262 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005263 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5264 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005265 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005266
5267 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5268}
Janos Follathb03233e2019-06-11 15:30:30 +01005269#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005270#endif /* MBEDTLS_MD_C */
5271
Janos Follathb80a94e2019-06-12 15:54:46 +01005272static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005274 psa_key_derivation_step_t step,
5275 const uint8_t *data,
5276 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005277{
5278 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005280
5281#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005282 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005285 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286 step, data, data_length );
5287 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005288 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005289#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005290#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005291 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005292 {
Janos Follathb03233e2019-06-11 15:30:30 +01005293 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5294 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5295 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005296 }
5297 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5298 {
5299 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5300 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5301 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005302 }
5303 else
5304#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005307 return( PSA_ERROR_BAD_STATE );
5308 }
5309
5310 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005312 return( status );
5313}
5314
Janos Follath51f4a0f2019-06-14 11:35:55 +01005315psa_status_t psa_key_derivation_input_bytes(
5316 psa_key_derivation_operation_t *operation,
5317 psa_key_derivation_step_t step,
5318 const uint8_t *data,
5319 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005320{
Janos Follathc5621512019-06-14 11:27:57 +01005321 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5322 return( PSA_ERROR_INVALID_ARGUMENT );
5323
5324 return( psa_key_derivation_input_internal( operation, step,
5325 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005326}
5327
Janos Follath51f4a0f2019-06-14 11:35:55 +01005328psa_status_t psa_key_derivation_input_key(
5329 psa_key_derivation_operation_t *operation,
5330 psa_key_derivation_step_t step,
5331 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332{
5333 psa_key_slot_t *slot;
5334 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005335 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005336 PSA_KEY_USAGE_DERIVE,
5337 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 if( status != PSA_SUCCESS )
5339 return( status );
5340 if( slot->type != PSA_KEY_TYPE_DERIVE )
5341 return( PSA_ERROR_INVALID_ARGUMENT );
5342 /* Don't allow a key to be used as an input that is usually public.
5343 * This is debatable. It's ok from a cryptographic perspective to
5344 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005345 * the material should be dedicated to a particular input step,
5346 * otherwise this may allow the key to be used in an unintended way
5347 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005348 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005349 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005350 return( psa_key_derivation_input_internal( operation,
5351 step,
5352 slot->data.raw.data,
5353 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005354}
5355
Gilles Peskineea0fb492018-07-12 17:17:20 +02005356
5357
5358/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005359/* Key agreement */
5360/****************************************************************/
5361
Gilles Peskinea05219c2018-11-16 16:02:56 +01005362#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005363static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5364 size_t peer_key_length,
5365 const mbedtls_ecp_keypair *our_key,
5366 uint8_t *shared_secret,
5367 size_t shared_secret_size,
5368 size_t *shared_secret_length )
5369{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005370 mbedtls_ecp_keypair *their_key = NULL;
5371 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005372 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005373 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005374
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005375 status = psa_import_ec_public_key(
5376 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5377 peer_key, peer_key_length,
5378 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005379 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005380 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005381
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005382 status = mbedtls_to_psa_error(
5383 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5384 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005385 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005386 status = mbedtls_to_psa_error(
5387 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5388 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005389 goto exit;
5390
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005391 status = mbedtls_to_psa_error(
5392 mbedtls_ecdh_calc_secret( &ecdh,
5393 shared_secret_length,
5394 shared_secret, shared_secret_size,
5395 mbedtls_ctr_drbg_random,
5396 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005397
5398exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005399 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005400 mbedtls_ecp_keypair_free( their_key );
5401 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005402 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005403}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005404#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005405
Gilles Peskine01d718c2018-09-18 12:01:02 +02005406#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5407
Gilles Peskine0216fe12019-04-11 21:23:21 +02005408static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5409 psa_key_slot_t *private_key,
5410 const uint8_t *peer_key,
5411 size_t peer_key_length,
5412 uint8_t *shared_secret,
5413 size_t shared_secret_size,
5414 size_t *shared_secret_length )
5415{
5416 switch( alg )
5417 {
5418#if defined(MBEDTLS_ECDH_C)
5419 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005420 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005421 return( PSA_ERROR_INVALID_ARGUMENT );
5422 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5423 private_key->data.ecp,
5424 shared_secret, shared_secret_size,
5425 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005426#endif /* MBEDTLS_ECDH_C */
5427 default:
5428 (void) private_key;
5429 (void) peer_key;
5430 (void) peer_key_length;
5431 (void) shared_secret;
5432 (void) shared_secret_size;
5433 (void) shared_secret_length;
5434 return( PSA_ERROR_NOT_SUPPORTED );
5435 }
5436}
5437
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005438/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005439 * to potentially free embedded data structures and wipe confidential data.
5440 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005442 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005443 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005444 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005445 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005446{
5447 psa_status_t status;
5448 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5449 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005451
5452 /* Step 1: run the secret agreement algorithm to generate the shared
5453 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005454 status = psa_key_agreement_raw_internal( ka_alg,
5455 private_key,
5456 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005457 shared_secret,
5458 sizeof( shared_secret ),
5459 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005460 if( status != PSA_SUCCESS )
5461 goto exit;
5462
5463 /* Step 2: set up the key derivation to generate key material from
5464 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005465 status = psa_key_derivation_input_internal( operation, step,
5466 shared_secret,
5467 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005468
Gilles Peskine01d718c2018-09-18 12:01:02 +02005469exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005470 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005471 return( status );
5472}
5473
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005474psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005475 psa_key_derivation_step_t step,
5476 psa_key_handle_t private_key,
5477 const uint8_t *peer_key,
5478 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005479{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005480 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005481 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005483 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005484 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005485 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005486 if( status != PSA_SUCCESS )
5487 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005489 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005490 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005491 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005493 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005494}
5495
Gilles Peskinebe697d82019-05-16 18:00:41 +02005496psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5497 psa_key_handle_t private_key,
5498 const uint8_t *peer_key,
5499 size_t peer_key_length,
5500 uint8_t *output,
5501 size_t output_size,
5502 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005503{
5504 psa_key_slot_t *slot;
5505 psa_status_t status;
5506
5507 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5508 {
5509 status = PSA_ERROR_INVALID_ARGUMENT;
5510 goto exit;
5511 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005512 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005513 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005514 if( status != PSA_SUCCESS )
5515 goto exit;
5516
5517 status = psa_key_agreement_raw_internal( alg, slot,
5518 peer_key, peer_key_length,
5519 output, output_size,
5520 output_length );
5521
5522exit:
5523 if( status != PSA_SUCCESS )
5524 {
5525 /* If an error happens and is not handled properly, the output
5526 * may be used as a key to protect sensitive data. Arrange for such
5527 * a key to be random, which is likely to result in decryption or
5528 * verification errors. This is better than filling the buffer with
5529 * some constant data such as zeros, which would result in the data
5530 * being protected with a reproducible, easily knowable key.
5531 */
5532 psa_generate_random( output, output_size );
5533 *output_length = output_size;
5534 }
5535 return( status );
5536}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005537
5538
5539/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005540/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005541/****************************************************************/
5542
5543psa_status_t psa_generate_random( uint8_t *output,
5544 size_t output_size )
5545{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005546 int ret;
5547 GUARD_MODULE_INITIALIZED;
5548
5549 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005550 return( mbedtls_to_psa_error( ret ) );
5551}
5552
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005553#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5554#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005555
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005556psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5557 size_t seed_size )
5558{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005559 if( global_data.initialized )
5560 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005561
5562 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5563 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5564 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5565 return( PSA_ERROR_INVALID_ARGUMENT );
5566
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005567 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005568}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005569#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005570
Gilles Peskinee56e8782019-04-26 17:34:02 +02005571#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5572static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5573 size_t domain_parameters_size,
5574 int *exponent )
5575{
5576 size_t i;
5577 uint32_t acc = 0;
5578
5579 if( domain_parameters_size == 0 )
5580 {
5581 *exponent = 65537;
5582 return( PSA_SUCCESS );
5583 }
5584
5585 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5586 * support values that fit in a 32-bit integer, which is larger than
5587 * int on just about every platform anyway. */
5588 if( domain_parameters_size > sizeof( acc ) )
5589 return( PSA_ERROR_NOT_SUPPORTED );
5590 for( i = 0; i < domain_parameters_size; i++ )
5591 acc = ( acc << 8 ) | domain_parameters[i];
5592 if( acc > INT_MAX )
5593 return( PSA_ERROR_NOT_SUPPORTED );
5594 *exponent = acc;
5595 return( PSA_SUCCESS );
5596}
5597#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5598
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005599static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005600 psa_key_slot_t *slot, size_t bits,
5601 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005602{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005603 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005604
Gilles Peskinee56e8782019-04-26 17:34:02 +02005605 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005606 return( PSA_ERROR_INVALID_ARGUMENT );
5607
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005608 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005609 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005610 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005611 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005612 if( status != PSA_SUCCESS )
5613 return( status );
5614 status = psa_generate_random( slot->data.raw.data,
5615 slot->data.raw.bytes );
5616 if( status != PSA_SUCCESS )
5617 {
5618 mbedtls_free( slot->data.raw.data );
5619 return( status );
5620 }
5621#if defined(MBEDTLS_DES_C)
5622 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005623 psa_des_set_key_parity( slot->data.raw.data,
5624 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005625#endif /* MBEDTLS_DES_C */
5626 }
5627 else
5628
5629#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005630 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005631 {
5632 mbedtls_rsa_context *rsa;
5633 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005634 int exponent;
5635 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005636 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5637 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005638 /* Accept only byte-aligned keys, for the same reasons as
5639 * in psa_import_rsa_key(). */
5640 if( bits % 8 != 0 )
5641 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005642 status = psa_read_rsa_exponent( domain_parameters,
5643 domain_parameters_size,
5644 &exponent );
5645 if( status != PSA_SUCCESS )
5646 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005647 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5648 if( rsa == NULL )
5649 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5650 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5651 ret = mbedtls_rsa_gen_key( rsa,
5652 mbedtls_ctr_drbg_random,
5653 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005654 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005655 exponent );
5656 if( ret != 0 )
5657 {
5658 mbedtls_rsa_free( rsa );
5659 mbedtls_free( rsa );
5660 return( mbedtls_to_psa_error( ret ) );
5661 }
5662 slot->data.rsa = rsa;
5663 }
5664 else
5665#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5666
5667#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005668 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005669 {
5670 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5671 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5672 const mbedtls_ecp_curve_info *curve_info =
5673 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5674 mbedtls_ecp_keypair *ecp;
5675 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005676 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005677 return( PSA_ERROR_NOT_SUPPORTED );
5678 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5679 return( PSA_ERROR_NOT_SUPPORTED );
5680 if( curve_info->bit_size != bits )
5681 return( PSA_ERROR_INVALID_ARGUMENT );
5682 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5683 if( ecp == NULL )
5684 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5685 mbedtls_ecp_keypair_init( ecp );
5686 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5687 mbedtls_ctr_drbg_random,
5688 &global_data.ctr_drbg );
5689 if( ret != 0 )
5690 {
5691 mbedtls_ecp_keypair_free( ecp );
5692 mbedtls_free( ecp );
5693 return( mbedtls_to_psa_error( ret ) );
5694 }
5695 slot->data.ecp = ecp;
5696 }
5697 else
5698#endif /* MBEDTLS_ECP_C */
5699
5700 return( PSA_ERROR_NOT_SUPPORTED );
5701
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 return( PSA_SUCCESS );
5703}
5704
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005705psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005706 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707{
5708 psa_status_t status;
5709 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005710 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005711 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5713 if( driver != NULL )
5714 {
5715 /* Generating a key in a secure element is not implemented yet. */
5716 status = PSA_ERROR_NOT_SUPPORTED;
5717 }
5718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005719 if( status == PSA_SUCCESS )
5720 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005721 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005722 slot, attributes->bits,
5723 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005724 }
5725 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005726 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005727 if( status != PSA_SUCCESS )
5728 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005729 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005730 *handle = 0;
5731 }
5732 return( status );
5733}
5734
5735
Gilles Peskine05d69892018-06-19 22:00:52 +02005736
5737/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005738/* Module setup */
5739/****************************************************************/
5740
Gilles Peskine5e769522018-11-20 21:59:56 +01005741psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5742 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5743 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5744{
5745 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5746 return( PSA_ERROR_BAD_STATE );
5747 global_data.entropy_init = entropy_init;
5748 global_data.entropy_free = entropy_free;
5749 return( PSA_SUCCESS );
5750}
5751
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752void mbedtls_psa_crypto_free( void )
5753{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005754 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005755 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5756 {
5757 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005758 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005759 }
5760 /* Wipe all remaining data, including configuration.
5761 * In particular, this sets all state indicator to the value
5762 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005763 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005764#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005765 /* Unregister all secure element drivers, so that we restart from
5766 * a pristine state. */
5767 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005768#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005769}
5770
5771psa_status_t psa_crypto_init( void )
5772{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005773 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005774 const unsigned char drbg_seed[] = "PSA";
5775
Gilles Peskinec6b69072018-11-20 21:42:52 +01005776 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005777 if( global_data.initialized != 0 )
5778 return( PSA_SUCCESS );
5779
Gilles Peskine5e769522018-11-20 21:59:56 +01005780 /* Set default configuration if
5781 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5782 if( global_data.entropy_init == NULL )
5783 global_data.entropy_init = mbedtls_entropy_init;
5784 if( global_data.entropy_free == NULL )
5785 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005786
Gilles Peskinec6b69072018-11-20 21:42:52 +01005787 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005788 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005789 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005790 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005791 status = mbedtls_to_psa_error(
5792 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5793 mbedtls_entropy_func,
5794 &global_data.entropy,
5795 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5796 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005798 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799
Gilles Peskine66fb1262018-12-10 16:29:04 +01005800 status = psa_initialize_key_slots( );
5801 if( status != PSA_SUCCESS )
5802 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005803
Gilles Peskine4b734222019-07-24 15:56:31 +02005804#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005805 status = psa_crypto_load_transaction( );
5806 if( status == PSA_SUCCESS )
5807 {
5808 /*TOnogrepDO: complete or abort the transaction*/
5809 }
5810 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5811 {
5812 /* There's no transaction to complete. It's all good. */
5813 status = PSA_SUCCESS;
5814 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005815#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005816
Gilles Peskinec6b69072018-11-20 21:42:52 +01005817 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005818 global_data.initialized = 1;
5819
Gilles Peskinee59236f2018-01-27 23:32:46 +01005820exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005821 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005822 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005823 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005824}
5825
5826#endif /* MBEDTLS_PSA_CRYPTO_C */