blob: e048e9f2bc96972f442c36ce16ba98c23167b441 [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 Peskinef77ed1f2018-12-03 11:58:46 +0100874/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100875static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000876{
Gilles Peskine73167e12019-07-12 23:44:37 +0200877#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
878 if( psa_key_slot_is_external( slot ) )
879 {
880 /* No key material to clean. */
881 }
882 else
883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000884 if( slot->type == PSA_KEY_TYPE_NONE )
885 {
886 /* No key material to clean. */
887 }
888 else if( key_type_is_raw_bytes( slot->type ) )
889 {
890 mbedtls_free( slot->data.raw.data );
891 }
892 else
893#if defined(MBEDTLS_RSA_C)
894 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
895 {
896 mbedtls_rsa_free( slot->data.rsa );
897 mbedtls_free( slot->data.rsa );
898 }
899 else
900#endif /* defined(MBEDTLS_RSA_C) */
901#if defined(MBEDTLS_ECP_C)
902 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
903 {
904 mbedtls_ecp_keypair_free( slot->data.ecp );
905 mbedtls_free( slot->data.ecp );
906 }
907 else
908#endif /* defined(MBEDTLS_ECP_C) */
909 {
910 /* Shouldn't happen: the key type is not any type that we
911 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200912 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000913 }
914
915 return( PSA_SUCCESS );
916}
917
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100918static void psa_abort_operations_using_key( psa_key_slot_t *slot )
919{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200920 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100921 (void) slot;
922}
923
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100924/** Completely wipe a slot in memory, including its policy.
925 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100926psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100927{
928 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100929 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100930 /* At this point, key material and other type-specific content has
931 * been wiped. Clear remaining metadata. We can call memset and not
932 * zeroize because the metadata is not particularly sensitive. */
933 memset( slot, 0, sizeof( *slot ) );
934 return( status );
935}
936
Gilles Peskinec5487a82018-12-03 18:08:14 +0100937psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100939 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100940 psa_status_t status = PSA_SUCCESS;
941 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
943 psa_se_drv_table_entry_t *driver;
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Gilles Peskinec5487a82018-12-03 18:08:14 +0100946 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200947 if( status != PSA_SUCCESS )
948 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200949
950#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
951 driver = psa_get_se_driver_entry( slot->lifetime );
952 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200953 {
954 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
955 psa_crypto_transaction.key.lifetime = slot->lifetime;
956 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
957 psa_crypto_transaction.key.id = slot->persistent_storage_id;
958 status = psa_crypto_save_transaction( );
959 if( status != PSA_SUCCESS )
960 {
961 /* TOnogrepDO: destroy what can be destroyed anyway */
962 return( status );
963 }
964
Gilles Peskine354f7672019-07-12 23:46:38 +0200965 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +0200966 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200967#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
968
Darryl Greend49a4992018-06-18 17:27:26 +0100969#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
970 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
971 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100972 storage_status =
973 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100974 }
975#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +0200976
Gilles Peskinefc762652019-07-22 19:30:34 +0200977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
978 if( driver != NULL )
979 {
980 status = psa_crypto_stop_transaction( );
981 if( status != PSA_SUCCESS )
982 {
983 /* TOnogrepDO: destroy what can be destroyed anyway */
984 return( status );
985 }
986 }
987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
988
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100990 if( status != PSA_SUCCESS )
991 return( status );
992 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993}
994
Gilles Peskineb870b182018-07-06 16:02:09 +0200995/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200996static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200997{
998 if( key_type_is_raw_bytes( slot->type ) )
999 return( slot->data.raw.bytes * 8 );
1000#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001001 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001002 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001003#endif /* defined(MBEDTLS_RSA_C) */
1004#if defined(MBEDTLS_ECP_C)
1005 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1006 return( slot->data.ecp->grp.pbits );
1007#endif /* defined(MBEDTLS_ECP_C) */
1008 /* Shouldn't happen except on an empty slot. */
1009 return( 0 );
1010}
1011
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001012void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1013{
Gilles Peskineb699f072019-04-26 16:06:02 +02001014 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001015 memset( attributes, 0, sizeof( *attributes ) );
1016}
1017
Gilles Peskineb699f072019-04-26 16:06:02 +02001018psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1019 psa_key_type_t type,
1020 const uint8_t *data,
1021 size_t data_length )
1022{
1023 uint8_t *copy = NULL;
1024
1025 if( data_length != 0 )
1026 {
1027 copy = mbedtls_calloc( 1, data_length );
1028 if( copy == NULL )
1029 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1030 memcpy( copy, data, data_length );
1031 }
1032 /* After this point, this function is guaranteed to succeed, so it
1033 * can start modifying `*attributes`. */
1034
1035 if( attributes->domain_parameters != NULL )
1036 {
1037 mbedtls_free( attributes->domain_parameters );
1038 attributes->domain_parameters = NULL;
1039 attributes->domain_parameters_size = 0;
1040 }
1041
1042 attributes->domain_parameters = copy;
1043 attributes->domain_parameters_size = data_length;
1044 attributes->type = type;
1045 return( PSA_SUCCESS );
1046}
1047
1048psa_status_t psa_get_key_domain_parameters(
1049 const psa_key_attributes_t *attributes,
1050 uint8_t *data, size_t data_size, size_t *data_length )
1051{
1052 if( attributes->domain_parameters_size > data_size )
1053 return( PSA_ERROR_BUFFER_TOO_SMALL );
1054 *data_length = attributes->domain_parameters_size;
1055 if( attributes->domain_parameters_size != 0 )
1056 memcpy( data, attributes->domain_parameters,
1057 attributes->domain_parameters_size );
1058 return( PSA_SUCCESS );
1059}
1060
1061#if defined(MBEDTLS_RSA_C)
1062static psa_status_t psa_get_rsa_public_exponent(
1063 const mbedtls_rsa_context *rsa,
1064 psa_key_attributes_t *attributes )
1065{
1066 mbedtls_mpi mpi;
1067 int ret;
1068 uint8_t *buffer = NULL;
1069 size_t buflen;
1070 mbedtls_mpi_init( &mpi );
1071
1072 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1073 if( ret != 0 )
1074 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001075 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1076 {
1077 /* It's the default value, which is reported as an empty string,
1078 * so there's nothing to do. */
1079 goto exit;
1080 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001081
1082 buflen = mbedtls_mpi_size( &mpi );
1083 buffer = mbedtls_calloc( 1, buflen );
1084 if( buffer == NULL )
1085 {
1086 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1087 goto exit;
1088 }
1089 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1090 if( ret != 0 )
1091 goto exit;
1092 attributes->domain_parameters = buffer;
1093 attributes->domain_parameters_size = buflen;
1094
1095exit:
1096 mbedtls_mpi_free( &mpi );
1097 if( ret != 0 )
1098 mbedtls_free( buffer );
1099 return( mbedtls_to_psa_error( ret ) );
1100}
1101#endif /* MBEDTLS_RSA_C */
1102
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001103/** Retrieve the readily-accessible attributes of a key in a slot.
1104 *
1105 * This function does not compute attributes that are not directly
1106 * stored in the slot, such as the bit size of a transparent key.
1107 */
1108static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1109 psa_key_attributes_t *attributes )
1110{
1111 attributes->id = slot->persistent_storage_id;
1112 attributes->lifetime = slot->lifetime;
1113 attributes->policy = slot->policy;
1114 attributes->type = slot->type;
1115}
1116
1117/** Retrieve all the publicly-accessible attributes of a key.
1118 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001119psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1120 psa_key_attributes_t *attributes )
1121{
1122 psa_key_slot_t *slot;
1123 psa_status_t status;
1124
1125 psa_reset_key_attributes( attributes );
1126
1127 status = psa_get_key_slot( handle, &slot );
1128 if( status != PSA_SUCCESS )
1129 return( status );
1130
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001131 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001132 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001133
1134 switch( slot->type )
1135 {
1136#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001137 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001138 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1139 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1140 break;
1141#endif
1142 default:
1143 /* Nothing else to do. */
1144 break;
1145 }
1146
1147 if( status != PSA_SUCCESS )
1148 psa_reset_key_attributes( attributes );
1149 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001150}
1151
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001152#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001153static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1154 unsigned char *buf, size_t size )
1155{
1156 int ret;
1157 unsigned char *c;
1158 size_t len = 0;
1159
1160 c = buf + size;
1161
1162 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1163
1164 return( (int) len );
1165}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001166#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001167
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001168static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1169 uint8_t *data,
1170 size_t data_size,
1171 size_t *data_length,
1172 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173{
Gilles Peskine5d309672019-07-12 23:47:28 +02001174#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1175 const psa_drv_se_t *drv;
1176 psa_drv_se_context_t *drv_context;
1177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1178
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001179 *data_length = 0;
1180
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001181 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001182 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001183
Gilles Peskine5d309672019-07-12 23:47:28 +02001184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1185 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1186 {
1187 psa_drv_se_export_key_t method;
1188 if( drv->key_management == NULL )
1189 return( PSA_ERROR_NOT_SUPPORTED );
1190 method = ( export_public_key ?
1191 drv->key_management->p_export_public :
1192 drv->key_management->p_export );
1193 if( method == NULL )
1194 return( PSA_ERROR_NOT_SUPPORTED );
1195 return( ( *method )( drv_context,
1196 slot->data.se.slot_number,
1197 data, data_size, data_length ) );
1198 }
1199#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1200
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001201 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202 {
1203 if( slot->data.raw.bytes > data_size )
1204 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001205 if( data_size != 0 )
1206 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001207 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001208 memset( data + slot->data.raw.bytes, 0,
1209 data_size - slot->data.raw.bytes );
1210 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211 *data_length = slot->data.raw.bytes;
1212 return( PSA_SUCCESS );
1213 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001214#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001215 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001216 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001217 psa_status_t status;
1218
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001219 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001220 if( bytes > data_size )
1221 return( PSA_ERROR_BUFFER_TOO_SMALL );
1222 status = mbedtls_to_psa_error(
1223 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1224 if( status != PSA_SUCCESS )
1225 return( status );
1226 memset( data + bytes, 0, data_size - bytes );
1227 *data_length = bytes;
1228 return( PSA_SUCCESS );
1229 }
1230#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231 else
Moran Peker17e36e12018-05-02 12:55:20 +03001232 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001233#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001234 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001235 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001236 {
Moran Pekera998bc62018-04-16 18:16:20 +03001237 mbedtls_pk_context pk;
1238 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001239 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001240 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001241#if defined(MBEDTLS_RSA_C)
1242 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001243 pk.pk_info = &mbedtls_rsa_info;
1244 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001245#else
1246 return( PSA_ERROR_NOT_SUPPORTED );
1247#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001248 }
1249 else
1250 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001251#if defined(MBEDTLS_ECP_C)
1252 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001253 pk.pk_info = &mbedtls_eckey_info;
1254 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001255#else
1256 return( PSA_ERROR_NOT_SUPPORTED );
1257#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001258 }
Moran Pekerd7326592018-05-29 16:56:39 +03001259 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001260 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001261 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001262 }
Moran Peker17e36e12018-05-02 12:55:20 +03001263 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001264 {
Moran Peker17e36e12018-05-02 12:55:20 +03001265 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001266 }
Moran Peker60364322018-04-29 11:34:58 +03001267 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001268 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001269 /* If data_size is 0 then data may be NULL and then the
1270 * call to memset would have undefined behavior. */
1271 if( data_size != 0 )
1272 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001273 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001274 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001275 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1276 * Move the data to the beginning and erase remaining data
1277 * at the original location. */
1278 if( 2 * (size_t) ret <= data_size )
1279 {
1280 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001281 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001282 }
1283 else if( (size_t) ret < data_size )
1284 {
1285 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001286 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001287 }
Moran Pekera998bc62018-04-16 18:16:20 +03001288 *data_length = ret;
1289 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001290 }
1291 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001292#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001293 {
1294 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001295 it is valid for a special-purpose implementation to omit
1296 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001297 return( PSA_ERROR_NOT_SUPPORTED );
1298 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299 }
1300}
1301
Gilles Peskinec5487a82018-12-03 18:08:14 +01001302psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001303 uint8_t *data,
1304 size_t data_size,
1305 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001306{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001307 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001308 psa_status_t status;
1309
1310 /* Set the key to empty now, so that even when there are errors, we always
1311 * set data_length to a value between 0 and data_size. On error, setting
1312 * the key to empty is a good choice because an empty key representation is
1313 * unlikely to be accepted anywhere. */
1314 *data_length = 0;
1315
1316 /* Export requires the EXPORT flag. There is an exception for public keys,
1317 * which don't require any flag, but psa_get_key_from_slot takes
1318 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001319 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001320 if( status != PSA_SUCCESS )
1321 return( status );
1322 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001323 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001324}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001325
Gilles Peskinec5487a82018-12-03 18:08:14 +01001326psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001327 uint8_t *data,
1328 size_t data_size,
1329 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001330{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001331 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001332 psa_status_t status;
1333
1334 /* Set the key to empty now, so that even when there are errors, we always
1335 * set data_length to a value between 0 and data_size. On error, setting
1336 * the key to empty is a good choice because an empty key representation is
1337 * unlikely to be accepted anywhere. */
1338 *data_length = 0;
1339
1340 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001341 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001342 if( status != PSA_SUCCESS )
1343 return( status );
1344 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001345 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001346}
1347
Gilles Peskine4747d192019-04-17 15:05:45 +02001348static psa_status_t psa_set_key_policy_internal(
1349 psa_key_slot_t *slot,
1350 const psa_key_policy_t *policy )
1351{
1352 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001353 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001354 PSA_KEY_USAGE_ENCRYPT |
1355 PSA_KEY_USAGE_DECRYPT |
1356 PSA_KEY_USAGE_SIGN |
1357 PSA_KEY_USAGE_VERIFY |
1358 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1359 return( PSA_ERROR_INVALID_ARGUMENT );
1360
1361 slot->policy = *policy;
1362 return( PSA_SUCCESS );
1363}
1364
1365/** Prepare a key slot to receive key material.
1366 *
1367 * This function allocates a key slot and sets its metadata.
1368 *
1369 * If this function fails, call psa_fail_key_creation().
1370 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001371 * This function is intended to be used as follows:
1372 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1373 * it with the specified attributes, and assign it a handle.
1374 * -# Populate the slot with the key material.
1375 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1376 * In case of failure at any step, stop the sequence and call
1377 * psa_fail_key_creation().
1378 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001379 * \param[in] attributes Key attributes for the new key.
1380 * \param[out] handle On success, a handle for the allocated slot.
1381 * \param[out] p_slot On success, a pointer to the prepared slot.
1382 * \param[out] p_drv On any return, the driver for the key, if any.
1383 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001384 *
1385 * \retval #PSA_SUCCESS
1386 * The key slot is ready to receive key material.
1387 * \return If this function fails, the key slot is an invalid state.
1388 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001389 */
1390static psa_status_t psa_start_key_creation(
1391 const psa_key_attributes_t *attributes,
1392 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001393 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001394 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001395{
1396 psa_status_t status;
1397 psa_key_slot_t *slot;
1398
Gilles Peskine011e4282019-06-26 18:34:38 +02001399 *p_drv = NULL;
1400
Gilles Peskine267c6562019-05-27 19:01:54 +02001401 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001402 if( status != PSA_SUCCESS )
1403 return( status );
1404 slot = *p_slot;
1405
1406 status = psa_set_key_policy_internal( slot, &attributes->policy );
1407 if( status != PSA_SUCCESS )
1408 return( status );
1409 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001410
Gilles Peskine4747d192019-04-17 15:05:45 +02001411 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001412 {
1413 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001414 attributes->id,
1415 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001416 if( status != PSA_SUCCESS )
1417 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001418 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001419 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001420 slot->type = attributes->type;
1421
Gilles Peskinecbaff462019-07-12 23:46:04 +02001422#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinefc762652019-07-22 19:30:34 +02001423 /* Find a slot number for the new key. Save the slot number in
1424 * persistent storage, but do not yet save the driver's persistent
1425 * state, so that if the power fails during the key creation process,
1426 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001427 if( *p_drv != NULL )
1428 {
1429 status = psa_find_se_slot_for_key( attributes, *p_drv,
1430 &slot->data.se.slot_number );
1431 if( status != PSA_SUCCESS )
1432 return( status );
1433 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001434 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1435 psa_crypto_transaction.key.lifetime = slot->lifetime;
1436 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1437 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1438 status = psa_crypto_save_transaction( );
1439 if( status != PSA_SUCCESS )
1440 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1442
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 return( status );
1444}
1445
1446/** Finalize the creation of a key once its key material has been set.
1447 *
1448 * This entails writing the key to persistent storage.
1449 *
1450 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001451 * See the documentation of psa_start_key_creation() for the intended use
1452 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001454 * \param[in,out] slot Pointer to the slot with key material.
1455 * \param[in] driver The secure element driver for the key,
1456 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001457 *
1458 * \retval #PSA_SUCCESS
1459 * The key was successfully created. The handle is now valid.
1460 * \return If this function fails, the key slot is an invalid state.
1461 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001463static psa_status_t psa_finish_key_creation(
1464 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001465 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001466{
1467 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001468 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001469 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001470
1471#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1472 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1473 {
1474 uint8_t *buffer = NULL;
1475 size_t buffer_size = 0;
1476 size_t length;
1477
1478 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001479 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001480 buffer = mbedtls_calloc( 1, buffer_size );
1481 if( buffer == NULL && buffer_size != 0 )
1482 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1483 status = psa_internal_export_key( slot,
1484 buffer, buffer_size, &length,
1485 0 );
1486
1487 if( status == PSA_SUCCESS )
1488 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1490 psa_get_key_slot_attributes( slot, &attributes );
1491 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001492 }
1493
1494 if( buffer_size != 0 )
1495 mbedtls_platform_zeroize( buffer, buffer_size );
1496 mbedtls_free( buffer );
1497 }
1498#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1499
Gilles Peskinecbaff462019-07-12 23:46:04 +02001500#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1501 if( driver != NULL )
1502 {
1503 status = psa_save_se_persistent_data( driver );
1504 if( status != PSA_SUCCESS )
1505 {
1506 psa_destroy_persistent_key( slot->persistent_storage_id );
1507 return( status );
1508 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001509 status = psa_crypto_stop_transaction( );
1510 if( status != PSA_SUCCESS )
1511 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001512 }
1513#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1514
Gilles Peskine4747d192019-04-17 15:05:45 +02001515 return( status );
1516}
1517
1518/** Abort the creation of a key.
1519 *
1520 * You may call this function after calling psa_start_key_creation(),
1521 * or after psa_finish_key_creation() fails. In other circumstances, this
1522 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001523 * See the documentation of psa_start_key_creation() for the intended use
1524 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001525 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001526 * \param[in,out] slot Pointer to the slot with key material.
1527 * \param[in] driver The secure element driver for the key,
1528 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001529 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001530static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001531 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001532{
Gilles Peskine011e4282019-06-26 18:34:38 +02001533 (void) driver;
1534
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 if( slot == NULL )
1536 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001537
1538#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1539 /* TOnogrepDO: If the key has already been created in the secure
1540 * element, and the failure happened later (when saving metadata
1541 * to internal storage), we need to destroy the key in the secure
1542 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001543
1544 /* Abort the ongoing transaction if any. We already did what it
1545 * takes to undo any partial creation. All that's left is to update
1546 * the transaction data itself. */
1547 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001548#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1549
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 psa_wipe_key_slot( slot );
1551}
1552
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001553static psa_status_t psa_check_key_slot_attributes(
1554 const psa_key_slot_t *slot,
1555 const psa_key_attributes_t *attributes )
1556{
1557 if( attributes->type != 0 )
1558 {
1559 if( attributes->type != slot->type )
1560 return( PSA_ERROR_INVALID_ARGUMENT );
1561 }
1562
1563 if( attributes->domain_parameters_size != 0 )
1564 {
1565#if defined(MBEDTLS_RSA_C)
1566 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1567 {
1568 mbedtls_mpi actual, required;
1569 int ret;
1570 mbedtls_mpi_init( &actual );
1571 mbedtls_mpi_init( &required );
1572 ret = mbedtls_rsa_export( slot->data.rsa,
1573 NULL, NULL, NULL, NULL, &actual );
1574 if( ret != 0 )
1575 goto rsa_exit;
1576 ret = mbedtls_mpi_read_binary( &required,
1577 attributes->domain_parameters,
1578 attributes->domain_parameters_size );
1579 if( ret != 0 )
1580 goto rsa_exit;
1581 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1582 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1583 rsa_exit:
1584 mbedtls_mpi_free( &actual );
1585 mbedtls_mpi_free( &required );
1586 if( ret != 0)
1587 return( mbedtls_to_psa_error( ret ) );
1588 }
1589 else
1590#endif
1591 {
1592 return( PSA_ERROR_INVALID_ARGUMENT );
1593 }
1594 }
1595
1596 if( attributes->bits != 0 )
1597 {
1598 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1599 return( PSA_ERROR_INVALID_ARGUMENT );
1600 }
1601
1602 return( PSA_SUCCESS );
1603}
1604
Gilles Peskine4747d192019-04-17 15:05:45 +02001605psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001607 size_t data_length,
1608 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001609{
1610 psa_status_t status;
1611 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001612 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001613
Gilles Peskine011e4282019-06-26 18:34:38 +02001614 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001615 if( status != PSA_SUCCESS )
1616 goto exit;
1617
Gilles Peskine5d309672019-07-12 23:47:28 +02001618#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1619 if( driver != NULL )
1620 {
1621 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1622 if( drv->key_management == NULL ||
1623 drv->key_management->p_import == NULL )
1624 {
1625 status = PSA_ERROR_NOT_SUPPORTED;
1626 goto exit;
1627 }
1628 status = drv->key_management->p_import(
1629 psa_get_se_driver_context( driver ),
1630 slot->data.se.slot_number,
1631 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1632 data, data_length );
1633 /* TOnogrepDO: psa_check_key_slot_attributes? */
1634 }
1635 else
1636#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1637 {
1638 status = psa_import_key_into_slot( slot, data, data_length );
1639 if( status != PSA_SUCCESS )
1640 goto exit;
1641 status = psa_check_key_slot_attributes( slot, attributes );
1642 if( status != PSA_SUCCESS )
1643 goto exit;
1644 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001645
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001647exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 if( status != PSA_SUCCESS )
1649 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001650 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 *handle = 0;
1652 }
1653 return( status );
1654}
1655
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001656static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001657 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001658{
1659 psa_status_t status;
1660 uint8_t *buffer = NULL;
1661 size_t buffer_size = 0;
1662 size_t length;
1663
1664 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001665 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001666 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001667 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001668 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001669 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1670 if( status != PSA_SUCCESS )
1671 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001672 target->type = source->type;
1673 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001674
1675exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001676 if( buffer_size != 0 )
1677 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001678 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001679 return( status );
1680}
1681
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001682psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1683 const psa_key_attributes_t *specified_attributes,
1684 psa_key_handle_t *target_handle )
1685{
1686 psa_status_t status;
1687 psa_key_slot_t *source_slot = NULL;
1688 psa_key_slot_t *target_slot = NULL;
1689 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001690 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001691
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001692 status = psa_get_key_from_slot( source_handle, &source_slot,
1693 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001694 if( status != PSA_SUCCESS )
1695 goto exit;
1696
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001697 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1698 if( status != PSA_SUCCESS )
1699 goto exit;
1700
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001701 status = psa_restrict_key_policy( &actual_attributes.policy,
1702 &source_slot->policy );
1703 if( status != PSA_SUCCESS )
1704 goto exit;
1705
1706 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001707 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001708 if( status != PSA_SUCCESS )
1709 goto exit;
1710
1711 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001712 if( status != PSA_SUCCESS )
1713 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001714
Gilles Peskine011e4282019-06-26 18:34:38 +02001715 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001716exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001717 if( status != PSA_SUCCESS )
1718 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001719 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 *target_handle = 0;
1721 }
1722 return( status );
1723}
1724
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001725
1726
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001727/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001728/* Message digests */
1729/****************************************************************/
1730
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001731static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001732{
1733 switch( alg )
1734 {
1735#if defined(MBEDTLS_MD2_C)
1736 case PSA_ALG_MD2:
1737 return( &mbedtls_md2_info );
1738#endif
1739#if defined(MBEDTLS_MD4_C)
1740 case PSA_ALG_MD4:
1741 return( &mbedtls_md4_info );
1742#endif
1743#if defined(MBEDTLS_MD5_C)
1744 case PSA_ALG_MD5:
1745 return( &mbedtls_md5_info );
1746#endif
1747#if defined(MBEDTLS_RIPEMD160_C)
1748 case PSA_ALG_RIPEMD160:
1749 return( &mbedtls_ripemd160_info );
1750#endif
1751#if defined(MBEDTLS_SHA1_C)
1752 case PSA_ALG_SHA_1:
1753 return( &mbedtls_sha1_info );
1754#endif
1755#if defined(MBEDTLS_SHA256_C)
1756 case PSA_ALG_SHA_224:
1757 return( &mbedtls_sha224_info );
1758 case PSA_ALG_SHA_256:
1759 return( &mbedtls_sha256_info );
1760#endif
1761#if defined(MBEDTLS_SHA512_C)
1762 case PSA_ALG_SHA_384:
1763 return( &mbedtls_sha384_info );
1764 case PSA_ALG_SHA_512:
1765 return( &mbedtls_sha512_info );
1766#endif
1767 default:
1768 return( NULL );
1769 }
1770}
1771
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001772psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1773{
1774 switch( operation->alg )
1775 {
Gilles Peskine81736312018-06-26 15:04:31 +02001776 case 0:
1777 /* The object has (apparently) been initialized but it is not
1778 * in use. It's ok to call abort on such an object, and there's
1779 * nothing to do. */
1780 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001781#if defined(MBEDTLS_MD2_C)
1782 case PSA_ALG_MD2:
1783 mbedtls_md2_free( &operation->ctx.md2 );
1784 break;
1785#endif
1786#if defined(MBEDTLS_MD4_C)
1787 case PSA_ALG_MD4:
1788 mbedtls_md4_free( &operation->ctx.md4 );
1789 break;
1790#endif
1791#if defined(MBEDTLS_MD5_C)
1792 case PSA_ALG_MD5:
1793 mbedtls_md5_free( &operation->ctx.md5 );
1794 break;
1795#endif
1796#if defined(MBEDTLS_RIPEMD160_C)
1797 case PSA_ALG_RIPEMD160:
1798 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1799 break;
1800#endif
1801#if defined(MBEDTLS_SHA1_C)
1802 case PSA_ALG_SHA_1:
1803 mbedtls_sha1_free( &operation->ctx.sha1 );
1804 break;
1805#endif
1806#if defined(MBEDTLS_SHA256_C)
1807 case PSA_ALG_SHA_224:
1808 case PSA_ALG_SHA_256:
1809 mbedtls_sha256_free( &operation->ctx.sha256 );
1810 break;
1811#endif
1812#if defined(MBEDTLS_SHA512_C)
1813 case PSA_ALG_SHA_384:
1814 case PSA_ALG_SHA_512:
1815 mbedtls_sha512_free( &operation->ctx.sha512 );
1816 break;
1817#endif
1818 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001819 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001820 }
1821 operation->alg = 0;
1822 return( PSA_SUCCESS );
1823}
1824
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001825psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001826 psa_algorithm_t alg )
1827{
1828 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001829
1830 /* A context must be freshly initialized before it can be set up. */
1831 if( operation->alg != 0 )
1832 {
1833 return( PSA_ERROR_BAD_STATE );
1834 }
1835
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001836 switch( alg )
1837 {
1838#if defined(MBEDTLS_MD2_C)
1839 case PSA_ALG_MD2:
1840 mbedtls_md2_init( &operation->ctx.md2 );
1841 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1842 break;
1843#endif
1844#if defined(MBEDTLS_MD4_C)
1845 case PSA_ALG_MD4:
1846 mbedtls_md4_init( &operation->ctx.md4 );
1847 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1848 break;
1849#endif
1850#if defined(MBEDTLS_MD5_C)
1851 case PSA_ALG_MD5:
1852 mbedtls_md5_init( &operation->ctx.md5 );
1853 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1854 break;
1855#endif
1856#if defined(MBEDTLS_RIPEMD160_C)
1857 case PSA_ALG_RIPEMD160:
1858 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1859 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1860 break;
1861#endif
1862#if defined(MBEDTLS_SHA1_C)
1863 case PSA_ALG_SHA_1:
1864 mbedtls_sha1_init( &operation->ctx.sha1 );
1865 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1866 break;
1867#endif
1868#if defined(MBEDTLS_SHA256_C)
1869 case PSA_ALG_SHA_224:
1870 mbedtls_sha256_init( &operation->ctx.sha256 );
1871 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1872 break;
1873 case PSA_ALG_SHA_256:
1874 mbedtls_sha256_init( &operation->ctx.sha256 );
1875 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1876 break;
1877#endif
1878#if defined(MBEDTLS_SHA512_C)
1879 case PSA_ALG_SHA_384:
1880 mbedtls_sha512_init( &operation->ctx.sha512 );
1881 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1882 break;
1883 case PSA_ALG_SHA_512:
1884 mbedtls_sha512_init( &operation->ctx.sha512 );
1885 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1886 break;
1887#endif
1888 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001889 return( PSA_ALG_IS_HASH( alg ) ?
1890 PSA_ERROR_NOT_SUPPORTED :
1891 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001892 }
1893 if( ret == 0 )
1894 operation->alg = alg;
1895 else
1896 psa_hash_abort( operation );
1897 return( mbedtls_to_psa_error( ret ) );
1898}
1899
1900psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1901 const uint8_t *input,
1902 size_t input_length )
1903{
1904 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001905
1906 /* Don't require hash implementations to behave correctly on a
1907 * zero-length input, which may have an invalid pointer. */
1908 if( input_length == 0 )
1909 return( PSA_SUCCESS );
1910
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001911 switch( operation->alg )
1912 {
1913#if defined(MBEDTLS_MD2_C)
1914 case PSA_ALG_MD2:
1915 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1916 input, input_length );
1917 break;
1918#endif
1919#if defined(MBEDTLS_MD4_C)
1920 case PSA_ALG_MD4:
1921 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1922 input, input_length );
1923 break;
1924#endif
1925#if defined(MBEDTLS_MD5_C)
1926 case PSA_ALG_MD5:
1927 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1928 input, input_length );
1929 break;
1930#endif
1931#if defined(MBEDTLS_RIPEMD160_C)
1932 case PSA_ALG_RIPEMD160:
1933 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1934 input, input_length );
1935 break;
1936#endif
1937#if defined(MBEDTLS_SHA1_C)
1938 case PSA_ALG_SHA_1:
1939 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1940 input, input_length );
1941 break;
1942#endif
1943#if defined(MBEDTLS_SHA256_C)
1944 case PSA_ALG_SHA_224:
1945 case PSA_ALG_SHA_256:
1946 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1947 input, input_length );
1948 break;
1949#endif
1950#if defined(MBEDTLS_SHA512_C)
1951 case PSA_ALG_SHA_384:
1952 case PSA_ALG_SHA_512:
1953 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1954 input, input_length );
1955 break;
1956#endif
1957 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001958 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001959 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001960
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001961 if( ret != 0 )
1962 psa_hash_abort( operation );
1963 return( mbedtls_to_psa_error( ret ) );
1964}
1965
1966psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1967 uint8_t *hash,
1968 size_t hash_size,
1969 size_t *hash_length )
1970{
itayzafrir40835d42018-08-02 13:14:17 +03001971 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001972 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001973 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001974
1975 /* Fill the output buffer with something that isn't a valid hash
1976 * (barring an attack on the hash and deliberately-crafted input),
1977 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001978 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001979 /* If hash_size is 0 then hash may be NULL and then the
1980 * call to memset would have undefined behavior. */
1981 if( hash_size != 0 )
1982 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001983
1984 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001985 {
1986 status = PSA_ERROR_BUFFER_TOO_SMALL;
1987 goto exit;
1988 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001989
1990 switch( operation->alg )
1991 {
1992#if defined(MBEDTLS_MD2_C)
1993 case PSA_ALG_MD2:
1994 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1995 break;
1996#endif
1997#if defined(MBEDTLS_MD4_C)
1998 case PSA_ALG_MD4:
1999 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2000 break;
2001#endif
2002#if defined(MBEDTLS_MD5_C)
2003 case PSA_ALG_MD5:
2004 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2005 break;
2006#endif
2007#if defined(MBEDTLS_RIPEMD160_C)
2008 case PSA_ALG_RIPEMD160:
2009 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2010 break;
2011#endif
2012#if defined(MBEDTLS_SHA1_C)
2013 case PSA_ALG_SHA_1:
2014 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2015 break;
2016#endif
2017#if defined(MBEDTLS_SHA256_C)
2018 case PSA_ALG_SHA_224:
2019 case PSA_ALG_SHA_256:
2020 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2021 break;
2022#endif
2023#if defined(MBEDTLS_SHA512_C)
2024 case PSA_ALG_SHA_384:
2025 case PSA_ALG_SHA_512:
2026 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2027 break;
2028#endif
2029 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002030 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002031 }
itayzafrir40835d42018-08-02 13:14:17 +03002032 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002033
itayzafrir40835d42018-08-02 13:14:17 +03002034exit:
2035 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002036 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002037 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002038 return( psa_hash_abort( operation ) );
2039 }
2040 else
2041 {
2042 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002043 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002044 }
2045}
2046
Gilles Peskine2d277862018-06-18 15:41:12 +02002047psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2048 const uint8_t *hash,
2049 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002050{
2051 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2052 size_t actual_hash_length;
2053 psa_status_t status = psa_hash_finish( operation,
2054 actual_hash, sizeof( actual_hash ),
2055 &actual_hash_length );
2056 if( status != PSA_SUCCESS )
2057 return( status );
2058 if( actual_hash_length != hash_length )
2059 return( PSA_ERROR_INVALID_SIGNATURE );
2060 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2061 return( PSA_ERROR_INVALID_SIGNATURE );
2062 return( PSA_SUCCESS );
2063}
2064
Gilles Peskineeb35d782019-01-22 17:56:16 +01002065psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2066 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002067{
2068 if( target_operation->alg != 0 )
2069 return( PSA_ERROR_BAD_STATE );
2070
2071 switch( source_operation->alg )
2072 {
2073 case 0:
2074 return( PSA_ERROR_BAD_STATE );
2075#if defined(MBEDTLS_MD2_C)
2076 case PSA_ALG_MD2:
2077 mbedtls_md2_clone( &target_operation->ctx.md2,
2078 &source_operation->ctx.md2 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_MD4_C)
2082 case PSA_ALG_MD4:
2083 mbedtls_md4_clone( &target_operation->ctx.md4,
2084 &source_operation->ctx.md4 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_MD5_C)
2088 case PSA_ALG_MD5:
2089 mbedtls_md5_clone( &target_operation->ctx.md5,
2090 &source_operation->ctx.md5 );
2091 break;
2092#endif
2093#if defined(MBEDTLS_RIPEMD160_C)
2094 case PSA_ALG_RIPEMD160:
2095 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2096 &source_operation->ctx.ripemd160 );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA1_C)
2100 case PSA_ALG_SHA_1:
2101 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2102 &source_operation->ctx.sha1 );
2103 break;
2104#endif
2105#if defined(MBEDTLS_SHA256_C)
2106 case PSA_ALG_SHA_224:
2107 case PSA_ALG_SHA_256:
2108 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2109 &source_operation->ctx.sha256 );
2110 break;
2111#endif
2112#if defined(MBEDTLS_SHA512_C)
2113 case PSA_ALG_SHA_384:
2114 case PSA_ALG_SHA_512:
2115 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2116 &source_operation->ctx.sha512 );
2117 break;
2118#endif
2119 default:
2120 return( PSA_ERROR_NOT_SUPPORTED );
2121 }
2122
2123 target_operation->alg = source_operation->alg;
2124 return( PSA_SUCCESS );
2125}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126
2127
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002129/* MAC */
2130/****************************************************************/
2131
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002132static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133 psa_algorithm_t alg,
2134 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002135 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002136 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002138 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002139 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002141 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002142 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002143
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2145 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002146 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002147 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002148 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002149 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150 mode = MBEDTLS_MODE_STREAM;
2151 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002152 case PSA_ALG_CTR:
2153 mode = MBEDTLS_MODE_CTR;
2154 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002155 case PSA_ALG_CFB:
2156 mode = MBEDTLS_MODE_CFB;
2157 break;
2158 case PSA_ALG_OFB:
2159 mode = MBEDTLS_MODE_OFB;
2160 break;
2161 case PSA_ALG_CBC_NO_PADDING:
2162 mode = MBEDTLS_MODE_CBC;
2163 break;
2164 case PSA_ALG_CBC_PKCS7:
2165 mode = MBEDTLS_MODE_CBC;
2166 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002167 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002168 mode = MBEDTLS_MODE_CCM;
2169 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002170 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002171 mode = MBEDTLS_MODE_GCM;
2172 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002173 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2174 mode = MBEDTLS_MODE_CHACHAPOLY;
2175 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002176 default:
2177 return( NULL );
2178 }
2179 }
2180 else if( alg == PSA_ALG_CMAC )
2181 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002182 else
2183 return( NULL );
2184
2185 switch( key_type )
2186 {
2187 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002188 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002189 break;
2190 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002191 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2192 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002194 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002195 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002196 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002197 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2198 * but two-key Triple-DES is functionally three-key Triple-DES
2199 * with K1=K3, so that's how we present it to mbedtls. */
2200 if( key_bits == 128 )
2201 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202 break;
2203 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002204 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205 break;
2206 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002207 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002209 case PSA_KEY_TYPE_CHACHA20:
2210 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2211 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002212 default:
2213 return( NULL );
2214 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002215 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002216 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217
Jaeden Amero23bbb752018-06-26 14:16:54 +01002218 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2219 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220}
2221
Gilles Peskinea05219c2018-11-16 16:02:56 +01002222#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002223static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002224{
Gilles Peskine2d277862018-06-18 15:41:12 +02002225 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002226 {
2227 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002228 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002229 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002230 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002231 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002232 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002233 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002234 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002235 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002236 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002237 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002238 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002239 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002240 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002241 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002242 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002243 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002244 return( 128 );
2245 default:
2246 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002247 }
2248}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002249#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002250
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002251/* Initialize the MAC operation structure. Once this function has been
2252 * called, psa_mac_abort can run and will do the right thing. */
2253static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2254 psa_algorithm_t alg )
2255{
2256 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2257
2258 operation->alg = alg;
2259 operation->key_set = 0;
2260 operation->iv_set = 0;
2261 operation->iv_required = 0;
2262 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002263 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264
2265#if defined(MBEDTLS_CMAC_C)
2266 if( alg == PSA_ALG_CMAC )
2267 {
2268 operation->iv_required = 0;
2269 mbedtls_cipher_init( &operation->ctx.cmac );
2270 status = PSA_SUCCESS;
2271 }
2272 else
2273#endif /* MBEDTLS_CMAC_C */
2274#if defined(MBEDTLS_MD_C)
2275 if( PSA_ALG_IS_HMAC( operation->alg ) )
2276 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002277 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2278 operation->ctx.hmac.hash_ctx.alg = 0;
2279 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002280 }
2281 else
2282#endif /* MBEDTLS_MD_C */
2283 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002284 if( ! PSA_ALG_IS_MAC( alg ) )
2285 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286 }
2287
2288 if( status != PSA_SUCCESS )
2289 memset( operation, 0, sizeof( *operation ) );
2290 return( status );
2291}
2292
Gilles Peskine01126fa2018-07-12 17:04:55 +02002293#if defined(MBEDTLS_MD_C)
2294static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2295{
Gilles Peskine3f108122018-12-07 18:14:53 +01002296 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002297 return( psa_hash_abort( &hmac->hash_ctx ) );
2298}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002299
Janos Follath999f6482019-06-11 12:04:10 +01002300#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002301static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2302{
2303 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2304 memset( hmac, 0, sizeof( *hmac ) );
2305}
Janos Follath999f6482019-06-11 12:04:10 +01002306#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002307#endif /* MBEDTLS_MD_C */
2308
Gilles Peskine8c9def32018-02-08 10:02:12 +01002309psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2310{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002311 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002313 /* The object has (apparently) been initialized but it is not
2314 * in use. It's ok to call abort on such an object, and there's
2315 * nothing to do. */
2316 return( PSA_SUCCESS );
2317 }
2318 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002320 if( operation->alg == PSA_ALG_CMAC )
2321 {
2322 mbedtls_cipher_free( &operation->ctx.cmac );
2323 }
2324 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002325#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002326#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002327 if( PSA_ALG_IS_HMAC( operation->alg ) )
2328 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002329 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002330 }
2331 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002333 {
2334 /* Sanity check (shouldn't happen: operation->alg should
2335 * always have been initialized to a valid value). */
2336 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337 }
Moran Peker41deec42018-04-04 15:43:05 +03002338
Gilles Peskine8c9def32018-02-08 10:02:12 +01002339 operation->alg = 0;
2340 operation->key_set = 0;
2341 operation->iv_set = 0;
2342 operation->iv_required = 0;
2343 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002344 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002345
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002347
2348bad_state:
2349 /* If abort is called on an uninitialized object, we can't trust
2350 * anything. Wipe the object in case it contains confidential data.
2351 * This may result in a memory leak if a pointer gets overwritten,
2352 * but it's too late to do anything about this. */
2353 memset( operation, 0, sizeof( *operation ) );
2354 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355}
2356
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002357#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002358static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002359 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002360 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002361 const mbedtls_cipher_info_t *cipher_info )
2362{
2363 int ret;
2364
2365 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002366
2367 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2368 if( ret != 0 )
2369 return( ret );
2370
2371 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2372 slot->data.raw.data,
2373 key_bits );
2374 return( ret );
2375}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002376#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002377
Gilles Peskine248051a2018-06-20 16:09:38 +02002378#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002379static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2380 const uint8_t *key,
2381 size_t key_length,
2382 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002383{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002384 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002385 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002386 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002387 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002388 psa_status_t status;
2389
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002390 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2391 * overflow below. This should never trigger if the hash algorithm
2392 * is implemented correctly. */
2393 /* The size checks against the ipad and opad buffers cannot be written
2394 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2395 * because that triggers -Wlogical-op on GCC 7.3. */
2396 if( block_size > sizeof( ipad ) )
2397 return( PSA_ERROR_NOT_SUPPORTED );
2398 if( block_size > sizeof( hmac->opad ) )
2399 return( PSA_ERROR_NOT_SUPPORTED );
2400 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002401 return( PSA_ERROR_NOT_SUPPORTED );
2402
Gilles Peskined223b522018-06-11 18:12:58 +02002403 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002404 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002405 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002406 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002407 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002408 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002409 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002410 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002411 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002412 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002413 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002414 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002415 }
Gilles Peskine96889972018-07-12 17:07:03 +02002416 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2417 * but it is permitted. It is common when HMAC is used in HKDF, for
2418 * example. Don't call `memcpy` in the 0-length because `key` could be
2419 * an invalid pointer which would make the behavior undefined. */
2420 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002421 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002422
Gilles Peskined223b522018-06-11 18:12:58 +02002423 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2424 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002425 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002426 ipad[i] ^= 0x36;
2427 memset( ipad + key_length, 0x36, block_size - key_length );
2428
2429 /* Copy the key material from ipad to opad, flipping the requisite bits,
2430 * and filling the rest of opad with the requisite constant. */
2431 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002432 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2433 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002434
Gilles Peskine01126fa2018-07-12 17:04:55 +02002435 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002436 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002437 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002438
Gilles Peskine01126fa2018-07-12 17:04:55 +02002439 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002440
2441cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002442 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002443
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002444 return( status );
2445}
Gilles Peskine248051a2018-06-20 16:09:38 +02002446#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002447
Gilles Peskine89167cb2018-07-08 20:12:23 +02002448static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002449 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002450 psa_algorithm_t alg,
2451 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002454 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002456 psa_key_usage_t usage =
2457 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002458 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002459 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002461 /* A context must be freshly initialized before it can be set up. */
2462 if( operation->alg != 0 )
2463 {
2464 return( PSA_ERROR_BAD_STATE );
2465 }
2466
Gilles Peskined911eb72018-08-14 15:18:45 +02002467 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002468 if( status != PSA_SUCCESS )
2469 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002470 if( is_sign )
2471 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472
Gilles Peskinec5487a82018-12-03 18:08:14 +01002473 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002474 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002475 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002476 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002477
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002479 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002480 {
2481 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002482 mbedtls_cipher_info_from_psa( full_length_alg,
2483 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002484 int ret;
2485 if( cipher_info == NULL )
2486 {
2487 status = PSA_ERROR_NOT_SUPPORTED;
2488 goto exit;
2489 }
2490 operation->mac_size = cipher_info->block_size;
2491 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2492 status = mbedtls_to_psa_error( ret );
2493 }
2494 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002497 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002498 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002499 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002500 if( hash_alg == 0 )
2501 {
2502 status = PSA_ERROR_NOT_SUPPORTED;
2503 goto exit;
2504 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002505
2506 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2507 /* Sanity check. This shouldn't fail on a valid configuration. */
2508 if( operation->mac_size == 0 ||
2509 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2510 {
2511 status = PSA_ERROR_NOT_SUPPORTED;
2512 goto exit;
2513 }
2514
Gilles Peskine01126fa2018-07-12 17:04:55 +02002515 if( slot->type != PSA_KEY_TYPE_HMAC )
2516 {
2517 status = PSA_ERROR_INVALID_ARGUMENT;
2518 goto exit;
2519 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002520
Gilles Peskine01126fa2018-07-12 17:04:55 +02002521 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2522 slot->data.raw.data,
2523 slot->data.raw.bytes,
2524 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002525 }
2526 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002528 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002529 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002530 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002532
Gilles Peskined911eb72018-08-14 15:18:45 +02002533 if( truncated == 0 )
2534 {
2535 /* The "normal" case: untruncated algorithm. Nothing to do. */
2536 }
2537 else if( truncated < 4 )
2538 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002539 /* A very short MAC is too short for security since it can be
2540 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2541 * so we make this our minimum, even though 32 bits is still
2542 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002543 status = PSA_ERROR_NOT_SUPPORTED;
2544 }
2545 else if( truncated > operation->mac_size )
2546 {
2547 /* It's impossible to "truncate" to a larger length. */
2548 status = PSA_ERROR_INVALID_ARGUMENT;
2549 }
2550 else
2551 operation->mac_size = truncated;
2552
Gilles Peskinefbfac682018-07-08 20:51:54 +02002553exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002554 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002556 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002558 else
2559 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002560 operation->key_set = 1;
2561 }
2562 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563}
2564
Gilles Peskine89167cb2018-07-08 20:12:23 +02002565psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002566 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002567 psa_algorithm_t alg )
2568{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002569 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002570}
2571
2572psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002573 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002574 psa_algorithm_t alg )
2575{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002576 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002577}
2578
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2580 const uint8_t *input,
2581 size_t input_length )
2582{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002583 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002585 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002587 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 operation->has_input = 1;
2589
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002591 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002592 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002593 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2594 input, input_length );
2595 status = mbedtls_to_psa_error( ret );
2596 }
2597 else
2598#endif /* MBEDTLS_CMAC_C */
2599#if defined(MBEDTLS_MD_C)
2600 if( PSA_ALG_IS_HMAC( operation->alg ) )
2601 {
2602 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2603 input_length );
2604 }
2605 else
2606#endif /* MBEDTLS_MD_C */
2607 {
2608 /* This shouldn't happen if `operation` was initialized by
2609 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002610 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002611 }
2612
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 if( status != PSA_SUCCESS )
2614 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002615 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616}
2617
Gilles Peskine01126fa2018-07-12 17:04:55 +02002618#if defined(MBEDTLS_MD_C)
2619static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2620 uint8_t *mac,
2621 size_t mac_size )
2622{
2623 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2624 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2625 size_t hash_size = 0;
2626 size_t block_size = psa_get_hash_block_size( hash_alg );
2627 psa_status_t status;
2628
Gilles Peskine01126fa2018-07-12 17:04:55 +02002629 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2630 if( status != PSA_SUCCESS )
2631 return( status );
2632 /* From here on, tmp needs to be wiped. */
2633
2634 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2635 if( status != PSA_SUCCESS )
2636 goto exit;
2637
2638 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2639 if( status != PSA_SUCCESS )
2640 goto exit;
2641
2642 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2643 if( status != PSA_SUCCESS )
2644 goto exit;
2645
Gilles Peskined911eb72018-08-14 15:18:45 +02002646 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2647 if( status != PSA_SUCCESS )
2648 goto exit;
2649
2650 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002651
2652exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002653 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002654 return( status );
2655}
2656#endif /* MBEDTLS_MD_C */
2657
mohammad16036df908f2018-04-02 08:34:15 -07002658static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002659 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002660 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002662 if( ! operation->key_set )
2663 return( PSA_ERROR_BAD_STATE );
2664 if( operation->iv_required && ! operation->iv_set )
2665 return( PSA_ERROR_BAD_STATE );
2666
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667 if( mac_size < operation->mac_size )
2668 return( PSA_ERROR_BUFFER_TOO_SMALL );
2669
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002671 if( operation->alg == PSA_ALG_CMAC )
2672 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002673 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2674 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2675 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002676 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002677 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002680 else
2681#endif /* MBEDTLS_CMAC_C */
2682#if defined(MBEDTLS_MD_C)
2683 if( PSA_ALG_IS_HMAC( operation->alg ) )
2684 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002685 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002686 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002687 }
2688 else
2689#endif /* MBEDTLS_MD_C */
2690 {
2691 /* This shouldn't happen if `operation` was initialized by
2692 * a setup function. */
2693 return( PSA_ERROR_BAD_STATE );
2694 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695}
2696
Gilles Peskineacd4be32018-07-08 19:56:25 +02002697psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2698 uint8_t *mac,
2699 size_t mac_size,
2700 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002701{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002702 psa_status_t status;
2703
Jaeden Amero252ef282019-02-15 14:05:35 +00002704 if( operation->alg == 0 )
2705 {
2706 return( PSA_ERROR_BAD_STATE );
2707 }
2708
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002709 /* Fill the output buffer with something that isn't a valid mac
2710 * (barring an attack on the mac and deliberately-crafted input),
2711 * in case the caller doesn't check the return status properly. */
2712 *mac_length = mac_size;
2713 /* If mac_size is 0 then mac may be NULL and then the
2714 * call to memset would have undefined behavior. */
2715 if( mac_size != 0 )
2716 memset( mac, '!', mac_size );
2717
Gilles Peskine89167cb2018-07-08 20:12:23 +02002718 if( ! operation->is_sign )
2719 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002720 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002721 }
mohammad16036df908f2018-04-02 08:34:15 -07002722
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002723 status = psa_mac_finish_internal( operation, mac, mac_size );
2724
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002725 if( status == PSA_SUCCESS )
2726 {
2727 status = psa_mac_abort( operation );
2728 if( status == PSA_SUCCESS )
2729 *mac_length = operation->mac_size;
2730 else
2731 memset( mac, '!', mac_size );
2732 }
2733 else
2734 psa_mac_abort( operation );
2735 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002736}
2737
Gilles Peskineacd4be32018-07-08 19:56:25 +02002738psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2739 const uint8_t *mac,
2740 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741{
Gilles Peskine828ed142018-06-18 23:25:51 +02002742 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002743 psa_status_t status;
2744
Jaeden Amero252ef282019-02-15 14:05:35 +00002745 if( operation->alg == 0 )
2746 {
2747 return( PSA_ERROR_BAD_STATE );
2748 }
2749
Gilles Peskine89167cb2018-07-08 20:12:23 +02002750 if( operation->is_sign )
2751 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002752 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002753 }
2754 if( operation->mac_size != mac_length )
2755 {
2756 status = PSA_ERROR_INVALID_SIGNATURE;
2757 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002758 }
mohammad16036df908f2018-04-02 08:34:15 -07002759
2760 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002761 actual_mac, sizeof( actual_mac ) );
2762
2763 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2764 status = PSA_ERROR_INVALID_SIGNATURE;
2765
2766cleanup:
2767 if( status == PSA_SUCCESS )
2768 status = psa_mac_abort( operation );
2769 else
2770 psa_mac_abort( operation );
2771
Gilles Peskine3f108122018-12-07 18:14:53 +01002772 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002773
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002774 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002775}
2776
2777
Gilles Peskine20035e32018-02-03 22:44:14 +01002778
Gilles Peskine20035e32018-02-03 22:44:14 +01002779/****************************************************************/
2780/* Asymmetric cryptography */
2781/****************************************************************/
2782
Gilles Peskine2b450e32018-06-27 15:42:46 +02002783#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002784/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002785 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002786static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2787 size_t hash_length,
2788 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002789{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002790 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002791 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002792 *md_alg = mbedtls_md_get_type( md_info );
2793
2794 /* The Mbed TLS RSA module uses an unsigned int for hash length
2795 * parameters. Validate that it fits so that we don't risk an
2796 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002797#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002798 if( hash_length > UINT_MAX )
2799 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002800#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002801
2802#if defined(MBEDTLS_PKCS1_V15)
2803 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2804 * must be correct. */
2805 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2806 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002807 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002808 if( md_info == NULL )
2809 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002810 if( mbedtls_md_get_size( md_info ) != hash_length )
2811 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002812 }
2813#endif /* MBEDTLS_PKCS1_V15 */
2814
2815#if defined(MBEDTLS_PKCS1_V21)
2816 /* PSS requires a hash internally. */
2817 if( PSA_ALG_IS_RSA_PSS( alg ) )
2818 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002819 if( md_info == NULL )
2820 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002821 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002822#endif /* MBEDTLS_PKCS1_V21 */
2823
Gilles Peskine61b91d42018-06-08 16:09:36 +02002824 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002825}
2826
Gilles Peskine2b450e32018-06-27 15:42:46 +02002827static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2828 psa_algorithm_t alg,
2829 const uint8_t *hash,
2830 size_t hash_length,
2831 uint8_t *signature,
2832 size_t signature_size,
2833 size_t *signature_length )
2834{
2835 psa_status_t status;
2836 int ret;
2837 mbedtls_md_type_t md_alg;
2838
2839 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2840 if( status != PSA_SUCCESS )
2841 return( status );
2842
Gilles Peskine630a18a2018-06-29 17:49:35 +02002843 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002844 return( PSA_ERROR_BUFFER_TOO_SMALL );
2845
2846#if defined(MBEDTLS_PKCS1_V15)
2847 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2848 {
2849 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2850 MBEDTLS_MD_NONE );
2851 ret = mbedtls_rsa_pkcs1_sign( rsa,
2852 mbedtls_ctr_drbg_random,
2853 &global_data.ctr_drbg,
2854 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002855 md_alg,
2856 (unsigned int) hash_length,
2857 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002858 signature );
2859 }
2860 else
2861#endif /* MBEDTLS_PKCS1_V15 */
2862#if defined(MBEDTLS_PKCS1_V21)
2863 if( PSA_ALG_IS_RSA_PSS( alg ) )
2864 {
2865 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2866 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2867 mbedtls_ctr_drbg_random,
2868 &global_data.ctr_drbg,
2869 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002870 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002871 (unsigned int) hash_length,
2872 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002873 signature );
2874 }
2875 else
2876#endif /* MBEDTLS_PKCS1_V21 */
2877 {
2878 return( PSA_ERROR_INVALID_ARGUMENT );
2879 }
2880
2881 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002882 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002883 return( mbedtls_to_psa_error( ret ) );
2884}
2885
2886static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2887 psa_algorithm_t alg,
2888 const uint8_t *hash,
2889 size_t hash_length,
2890 const uint8_t *signature,
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_length < 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_verify( rsa,
2910 mbedtls_ctr_drbg_random,
2911 &global_data.ctr_drbg,
2912 MBEDTLS_RSA_PUBLIC,
2913 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002914 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002915 hash,
2916 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_verify( rsa,
2925 mbedtls_ctr_drbg_random,
2926 &global_data.ctr_drbg,
2927 MBEDTLS_RSA_PUBLIC,
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 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002938
2939 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2940 * the rest of the signature is invalid". This has little use in
2941 * practice and PSA doesn't report this distinction. */
2942 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2943 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002944 return( mbedtls_to_psa_error( ret ) );
2945}
2946#endif /* MBEDTLS_RSA_C */
2947
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002948#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002949/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2950 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2951 * (even though these functions don't modify it). */
2952static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2953 psa_algorithm_t alg,
2954 const uint8_t *hash,
2955 size_t hash_length,
2956 uint8_t *signature,
2957 size_t signature_size,
2958 size_t *signature_length )
2959{
2960 int ret;
2961 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002962 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002963 mbedtls_mpi_init( &r );
2964 mbedtls_mpi_init( &s );
2965
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002966 if( signature_size < 2 * curve_bytes )
2967 {
2968 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2969 goto cleanup;
2970 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002971
Gilles Peskinea05219c2018-11-16 16:02:56 +01002972#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002973 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2974 {
2975 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2976 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2977 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2978 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2979 hash, hash_length,
2980 md_alg ) );
2981 }
2982 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002983#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002984 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002985 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002986 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2987 hash, hash_length,
2988 mbedtls_ctr_drbg_random,
2989 &global_data.ctr_drbg ) );
2990 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002991
2992 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2993 signature,
2994 curve_bytes ) );
2995 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2996 signature + curve_bytes,
2997 curve_bytes ) );
2998
2999cleanup:
3000 mbedtls_mpi_free( &r );
3001 mbedtls_mpi_free( &s );
3002 if( ret == 0 )
3003 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003004 return( mbedtls_to_psa_error( ret ) );
3005}
3006
3007static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3008 const uint8_t *hash,
3009 size_t hash_length,
3010 const uint8_t *signature,
3011 size_t signature_length )
3012{
3013 int ret;
3014 mbedtls_mpi r, s;
3015 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3016 mbedtls_mpi_init( &r );
3017 mbedtls_mpi_init( &s );
3018
3019 if( signature_length != 2 * curve_bytes )
3020 return( PSA_ERROR_INVALID_SIGNATURE );
3021
3022 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3023 signature,
3024 curve_bytes ) );
3025 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3026 signature + curve_bytes,
3027 curve_bytes ) );
3028
3029 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3030 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003031
3032cleanup:
3033 mbedtls_mpi_free( &r );
3034 mbedtls_mpi_free( &s );
3035 return( mbedtls_to_psa_error( ret ) );
3036}
3037#endif /* MBEDTLS_ECDSA_C */
3038
Gilles Peskinec5487a82018-12-03 18:08:14 +01003039psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003040 psa_algorithm_t alg,
3041 const uint8_t *hash,
3042 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003043 uint8_t *signature,
3044 size_t signature_size,
3045 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003046{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003047 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003048 psa_status_t status;
3049
3050 *signature_length = signature_size;
3051
Gilles Peskinec5487a82018-12-03 18:08:14 +01003052 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003053 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003054 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003055 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003056 {
3057 status = PSA_ERROR_INVALID_ARGUMENT;
3058 goto exit;
3059 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003060
Gilles Peskine20035e32018-02-03 22:44:14 +01003061#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003062 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003063 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003064 status = psa_rsa_sign( slot->data.rsa,
3065 alg,
3066 hash, hash_length,
3067 signature, signature_size,
3068 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003069 }
3070 else
3071#endif /* defined(MBEDTLS_RSA_C) */
3072#if defined(MBEDTLS_ECP_C)
3073 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3074 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003075#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003076 if(
3077#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3078 PSA_ALG_IS_ECDSA( alg )
3079#else
3080 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3081#endif
3082 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003083 status = psa_ecdsa_sign( slot->data.ecp,
3084 alg,
3085 hash, hash_length,
3086 signature, signature_size,
3087 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003088 else
3089#endif /* defined(MBEDTLS_ECDSA_C) */
3090 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003091 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003092 }
itayzafrir5c753392018-05-08 11:18:38 +03003093 }
3094 else
3095#endif /* defined(MBEDTLS_ECP_C) */
3096 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003097 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003098 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003099
3100exit:
3101 /* Fill the unused part of the output buffer (the whole buffer on error,
3102 * the trailing part on success) with something that isn't a valid mac
3103 * (barring an attack on the mac and deliberately-crafted input),
3104 * in case the caller doesn't check the return status properly. */
3105 if( status == PSA_SUCCESS )
3106 memset( signature + *signature_length, '!',
3107 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003108 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003109 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003110 /* If signature_size is 0 then we have nothing to do. We must not call
3111 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003112 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003113}
3114
Gilles Peskinec5487a82018-12-03 18:08:14 +01003115psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003116 psa_algorithm_t alg,
3117 const uint8_t *hash,
3118 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003119 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003120 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003121{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003122 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003123 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003124
Gilles Peskinec5487a82018-12-03 18:08:14 +01003125 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003126 if( status != PSA_SUCCESS )
3127 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003128
Gilles Peskine61b91d42018-06-08 16:09:36 +02003129#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003130 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003132 return( psa_rsa_verify( slot->data.rsa,
3133 alg,
3134 hash, hash_length,
3135 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136 }
3137 else
3138#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003139#if defined(MBEDTLS_ECP_C)
3140 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3141 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003142#if defined(MBEDTLS_ECDSA_C)
3143 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003144 return( psa_ecdsa_verify( slot->data.ecp,
3145 hash, hash_length,
3146 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003147 else
3148#endif /* defined(MBEDTLS_ECDSA_C) */
3149 {
3150 return( PSA_ERROR_INVALID_ARGUMENT );
3151 }
itayzafrir5c753392018-05-08 11:18:38 +03003152 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003153 else
3154#endif /* defined(MBEDTLS_ECP_C) */
3155 {
3156 return( PSA_ERROR_NOT_SUPPORTED );
3157 }
3158}
3159
Gilles Peskine072ac562018-06-30 00:21:29 +02003160#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3161static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3162 mbedtls_rsa_context *rsa )
3163{
3164 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3165 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3166 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3167 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3168}
3169#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3170
Gilles Peskinec5487a82018-12-03 18:08:14 +01003171psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003172 psa_algorithm_t alg,
3173 const uint8_t *input,
3174 size_t input_length,
3175 const uint8_t *salt,
3176 size_t salt_length,
3177 uint8_t *output,
3178 size_t output_size,
3179 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003181 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003182 psa_status_t status;
3183
Darryl Green5cc689a2018-07-24 15:34:10 +01003184 (void) input;
3185 (void) input_length;
3186 (void) salt;
3187 (void) output;
3188 (void) output_size;
3189
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003190 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003192 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3193 return( PSA_ERROR_INVALID_ARGUMENT );
3194
Gilles Peskinec5487a82018-12-03 18:08:14 +01003195 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003196 if( status != PSA_SUCCESS )
3197 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003198 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003199 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201
3202#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003203 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204 {
3205 mbedtls_rsa_context *rsa = slot->data.rsa;
3206 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003207 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003208 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003210 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003212 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3213 mbedtls_ctr_drbg_random,
3214 &global_data.ctr_drbg,
3215 MBEDTLS_RSA_PUBLIC,
3216 input_length,
3217 input,
3218 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 }
3220 else
3221#endif /* MBEDTLS_PKCS1_V15 */
3222#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003223 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003225 psa_rsa_oaep_set_padding_mode( alg, rsa );
3226 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3227 mbedtls_ctr_drbg_random,
3228 &global_data.ctr_drbg,
3229 MBEDTLS_RSA_PUBLIC,
3230 salt, salt_length,
3231 input_length,
3232 input,
3233 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234 }
3235 else
3236#endif /* MBEDTLS_PKCS1_V21 */
3237 {
3238 return( PSA_ERROR_INVALID_ARGUMENT );
3239 }
3240 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003241 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 return( mbedtls_to_psa_error( ret ) );
3243 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003245#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246 {
3247 return( PSA_ERROR_NOT_SUPPORTED );
3248 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003249}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003250
Gilles Peskinec5487a82018-12-03 18:08:14 +01003251psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003252 psa_algorithm_t alg,
3253 const uint8_t *input,
3254 size_t input_length,
3255 const uint8_t *salt,
3256 size_t salt_length,
3257 uint8_t *output,
3258 size_t output_size,
3259 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003261 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003262 psa_status_t status;
3263
Darryl Green5cc689a2018-07-24 15:34:10 +01003264 (void) input;
3265 (void) input_length;
3266 (void) salt;
3267 (void) output;
3268 (void) output_size;
3269
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003270 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003272 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3273 return( PSA_ERROR_INVALID_ARGUMENT );
3274
Gilles Peskinec5487a82018-12-03 18:08:14 +01003275 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003276 if( status != PSA_SUCCESS )
3277 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003278 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279 return( PSA_ERROR_INVALID_ARGUMENT );
3280
3281#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003282 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 {
3284 mbedtls_rsa_context *rsa = slot->data.rsa;
3285 int ret;
3286
Gilles Peskine630a18a2018-06-29 17:49:35 +02003287 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288 return( PSA_ERROR_INVALID_ARGUMENT );
3289
3290#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003291 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003293 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3294 mbedtls_ctr_drbg_random,
3295 &global_data.ctr_drbg,
3296 MBEDTLS_RSA_PRIVATE,
3297 output_length,
3298 input,
3299 output,
3300 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003301 }
3302 else
3303#endif /* MBEDTLS_PKCS1_V15 */
3304#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003305 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003307 psa_rsa_oaep_set_padding_mode( alg, rsa );
3308 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3309 mbedtls_ctr_drbg_random,
3310 &global_data.ctr_drbg,
3311 MBEDTLS_RSA_PRIVATE,
3312 salt, salt_length,
3313 output_length,
3314 input,
3315 output,
3316 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317 }
3318 else
3319#endif /* MBEDTLS_PKCS1_V21 */
3320 {
3321 return( PSA_ERROR_INVALID_ARGUMENT );
3322 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003323
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003324 return( mbedtls_to_psa_error( ret ) );
3325 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003327#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003328 {
3329 return( PSA_ERROR_NOT_SUPPORTED );
3330 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003331}
Gilles Peskine20035e32018-02-03 22:44:14 +01003332
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003333
3334
mohammad1603503973b2018-03-12 15:59:30 +02003335/****************************************************************/
3336/* Symmetric cryptography */
3337/****************************************************************/
3338
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003339/* Initialize the cipher operation structure. Once this function has been
3340 * called, psa_cipher_abort can run and will do the right thing. */
3341static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3342 psa_algorithm_t alg )
3343{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003344 if( ! PSA_ALG_IS_CIPHER( alg ) )
3345 {
3346 memset( operation, 0, sizeof( *operation ) );
3347 return( PSA_ERROR_INVALID_ARGUMENT );
3348 }
3349
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003350 operation->alg = alg;
3351 operation->key_set = 0;
3352 operation->iv_set = 0;
3353 operation->iv_required = 1;
3354 operation->iv_size = 0;
3355 operation->block_size = 0;
3356 mbedtls_cipher_init( &operation->ctx.cipher );
3357 return( PSA_SUCCESS );
3358}
3359
Gilles Peskinee553c652018-06-04 16:22:46 +02003360static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003361 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003362 psa_algorithm_t alg,
3363 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003364{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003365 int ret = 0;
3366 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003367 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003368 size_t key_bits;
3369 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003370 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3371 PSA_KEY_USAGE_ENCRYPT :
3372 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003373
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003374 /* A context must be freshly initialized before it can be set up. */
3375 if( operation->alg != 0 )
3376 {
3377 return( PSA_ERROR_BAD_STATE );
3378 }
3379
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003380 status = psa_cipher_init( operation, alg );
3381 if( status != PSA_SUCCESS )
3382 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003383
Gilles Peskinec5487a82018-12-03 18:08:14 +01003384 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003385 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003386 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003387 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003388
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003389 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003390 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003391 {
3392 status = PSA_ERROR_NOT_SUPPORTED;
3393 goto exit;
3394 }
mohammad1603503973b2018-03-12 15:59:30 +02003395
mohammad1603503973b2018-03-12 15:59:30 +02003396 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003397 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003398 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003399
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003400#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003401 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003402 {
3403 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3404 unsigned char keys[24];
3405 memcpy( keys, slot->data.raw.data, 16 );
3406 memcpy( keys + 16, slot->data.raw.data, 8 );
3407 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3408 keys,
3409 192, cipher_operation );
3410 }
3411 else
3412#endif
3413 {
3414 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3415 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003416 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003417 }
Moran Peker41deec42018-04-04 15:43:05 +03003418 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003419 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003420
mohammad16038481e742018-03-18 13:57:31 +02003421#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003422 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003423 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003424 case PSA_ALG_CBC_NO_PADDING:
3425 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3426 MBEDTLS_PADDING_NONE );
3427 break;
3428 case PSA_ALG_CBC_PKCS7:
3429 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3430 MBEDTLS_PADDING_PKCS7 );
3431 break;
3432 default:
3433 /* The algorithm doesn't involve padding. */
3434 ret = 0;
3435 break;
3436 }
3437 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003438 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003439#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3440
mohammad1603503973b2018-03-12 15:59:30 +02003441 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003442 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3443 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3444 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003445 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003446 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003447 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003448#if defined(MBEDTLS_CHACHA20_C)
3449 else
3450 if( alg == PSA_ALG_CHACHA20 )
3451 operation->iv_size = 12;
3452#endif
mohammad1603503973b2018-03-12 15:59:30 +02003453
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003454exit:
3455 if( status == 0 )
3456 status = mbedtls_to_psa_error( ret );
3457 if( status != 0 )
3458 psa_cipher_abort( operation );
3459 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003460}
3461
Gilles Peskinefe119512018-07-08 21:39:34 +02003462psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003463 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003464 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003465{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003466 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003467}
3468
Gilles Peskinefe119512018-07-08 21:39:34 +02003469psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003470 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003471 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003472{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003473 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003474}
3475
Gilles Peskinefe119512018-07-08 21:39:34 +02003476psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3477 unsigned char *iv,
3478 size_t iv_size,
3479 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003480{
itayzafrir534bd7c2018-08-02 13:56:32 +03003481 psa_status_t status;
3482 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003483 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003484 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003485 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003486 }
Moran Peker41deec42018-04-04 15:43:05 +03003487 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003488 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003489 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003490 goto exit;
3491 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003492 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3493 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003494 if( ret != 0 )
3495 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003496 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003497 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003498 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003499
mohammad16038481e742018-03-18 13:57:31 +02003500 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003501 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003502
Moran Peker395db872018-05-31 14:07:14 +03003503exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003504 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003505 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003506 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003507}
3508
Gilles Peskinefe119512018-07-08 21:39:34 +02003509psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3510 const unsigned char *iv,
3511 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003512{
itayzafrir534bd7c2018-08-02 13:56:32 +03003513 psa_status_t status;
3514 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003515 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003516 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003517 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003518 }
Moran Pekera28258c2018-05-29 16:25:04 +03003519 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003520 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003521 status = PSA_ERROR_INVALID_ARGUMENT;
3522 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003523 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003524 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3525 status = mbedtls_to_psa_error( ret );
3526exit:
3527 if( status == PSA_SUCCESS )
3528 operation->iv_set = 1;
3529 else
mohammad1603503973b2018-03-12 15:59:30 +02003530 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003531 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003532}
3533
Gilles Peskinee553c652018-06-04 16:22:46 +02003534psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3535 const uint8_t *input,
3536 size_t input_length,
3537 unsigned char *output,
3538 size_t output_size,
3539 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003540{
itayzafrir534bd7c2018-08-02 13:56:32 +03003541 psa_status_t status;
3542 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003543 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003544
3545 if( operation->alg == 0 )
3546 {
3547 return( PSA_ERROR_BAD_STATE );
3548 }
3549
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003550 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003551 {
3552 /* Take the unprocessed partial block left over from previous
3553 * update calls, if any, plus the input to this call. Remove
3554 * the last partial block, if any. You get the data that will be
3555 * output in this call. */
3556 expected_output_size =
3557 ( operation->ctx.cipher.unprocessed_len + input_length )
3558 / operation->block_size * operation->block_size;
3559 }
3560 else
3561 {
3562 expected_output_size = input_length;
3563 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003564
Gilles Peskine89d789c2018-06-04 17:17:16 +02003565 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003566 {
3567 status = PSA_ERROR_BUFFER_TOO_SMALL;
3568 goto exit;
3569 }
mohammad160382759612018-03-12 18:16:40 +02003570
mohammad1603503973b2018-03-12 15:59:30 +02003571 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003572 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003573 status = mbedtls_to_psa_error( ret );
3574exit:
3575 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003576 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003577 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003578}
3579
Gilles Peskinee553c652018-06-04 16:22:46 +02003580psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3581 uint8_t *output,
3582 size_t output_size,
3583 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003584{
David Saadab4ecc272019-02-14 13:48:10 +02003585 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003586 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003587 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003588
mohammad1603503973b2018-03-12 15:59:30 +02003589 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003590 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003591 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003592 }
3593 if( operation->iv_required && ! operation->iv_set )
3594 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003595 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003596 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003597
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003598 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003599 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3600 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003601 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003602 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003603 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003604 }
3605
Janos Follath315b51c2018-07-09 16:04:51 +01003606 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3607 temp_output_buffer,
3608 output_length );
3609 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003610 {
Janos Follath315b51c2018-07-09 16:04:51 +01003611 status = mbedtls_to_psa_error( cipher_ret );
3612 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003613 }
Janos Follath315b51c2018-07-09 16:04:51 +01003614
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003615 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003616 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003617 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003618 memcpy( output, temp_output_buffer, *output_length );
3619 else
3620 {
Janos Follath315b51c2018-07-09 16:04:51 +01003621 status = PSA_ERROR_BUFFER_TOO_SMALL;
3622 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003623 }
mohammad1603503973b2018-03-12 15:59:30 +02003624
Gilles Peskine3f108122018-12-07 18:14:53 +01003625 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003626 status = psa_cipher_abort( operation );
3627
3628 return( status );
3629
3630error:
3631
3632 *output_length = 0;
3633
Gilles Peskine3f108122018-12-07 18:14:53 +01003634 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003635 (void) psa_cipher_abort( operation );
3636
3637 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003638}
3639
Gilles Peskinee553c652018-06-04 16:22:46 +02003640psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3641{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003642 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003643 {
3644 /* The object has (apparently) been initialized but it is not
3645 * in use. It's ok to call abort on such an object, and there's
3646 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003647 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003648 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003649
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003650 /* Sanity check (shouldn't happen: operation->alg should
3651 * always have been initialized to a valid value). */
3652 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3653 return( PSA_ERROR_BAD_STATE );
3654
mohammad1603503973b2018-03-12 15:59:30 +02003655 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003656
Moran Peker41deec42018-04-04 15:43:05 +03003657 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003658 operation->key_set = 0;
3659 operation->iv_set = 0;
3660 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003661 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003662 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003663
Moran Peker395db872018-05-31 14:07:14 +03003664 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003665}
3666
Gilles Peskinea0655c32018-04-30 17:06:50 +02003667
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003668
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003669
mohammad16035955c982018-04-26 00:53:03 +03003670/****************************************************************/
3671/* AEAD */
3672/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003673
Gilles Peskineedf9a652018-08-17 18:11:56 +02003674typedef struct
3675{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003676 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003677 const mbedtls_cipher_info_t *cipher_info;
3678 union
3679 {
3680#if defined(MBEDTLS_CCM_C)
3681 mbedtls_ccm_context ccm;
3682#endif /* MBEDTLS_CCM_C */
3683#if defined(MBEDTLS_GCM_C)
3684 mbedtls_gcm_context gcm;
3685#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003686#if defined(MBEDTLS_CHACHAPOLY_C)
3687 mbedtls_chachapoly_context chachapoly;
3688#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003689 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003690 psa_algorithm_t core_alg;
3691 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003692 uint8_t tag_length;
3693} aead_operation_t;
3694
Gilles Peskine30a9e412019-01-14 18:36:12 +01003695static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003696{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003697 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003698 {
3699#if defined(MBEDTLS_CCM_C)
3700 case PSA_ALG_CCM:
3701 mbedtls_ccm_free( &operation->ctx.ccm );
3702 break;
3703#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003704#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003705 case PSA_ALG_GCM:
3706 mbedtls_gcm_free( &operation->ctx.gcm );
3707 break;
3708#endif /* MBEDTLS_GCM_C */
3709 }
3710}
3711
3712static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003713 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003714 psa_key_usage_t usage,
3715 psa_algorithm_t alg )
3716{
3717 psa_status_t status;
3718 size_t key_bits;
3719 mbedtls_cipher_id_t cipher_id;
3720
Gilles Peskinec5487a82018-12-03 18:08:14 +01003721 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003722 if( status != PSA_SUCCESS )
3723 return( status );
3724
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003725 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003726
3727 operation->cipher_info =
3728 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3729 &cipher_id );
3730 if( operation->cipher_info == NULL )
3731 return( PSA_ERROR_NOT_SUPPORTED );
3732
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003733 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003734 {
3735#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003736 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3737 operation->core_alg = PSA_ALG_CCM;
3738 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003739 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3740 * The call to mbedtls_ccm_encrypt_and_tag or
3741 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003742 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3743 return( PSA_ERROR_INVALID_ARGUMENT );
3744 mbedtls_ccm_init( &operation->ctx.ccm );
3745 status = mbedtls_to_psa_error(
3746 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3747 operation->slot->data.raw.data,
3748 (unsigned int) key_bits ) );
3749 if( status != 0 )
3750 goto cleanup;
3751 break;
3752#endif /* MBEDTLS_CCM_C */
3753
3754#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003755 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3756 operation->core_alg = PSA_ALG_GCM;
3757 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003758 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3759 * The call to mbedtls_gcm_crypt_and_tag or
3760 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003761 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3762 return( PSA_ERROR_INVALID_ARGUMENT );
3763 mbedtls_gcm_init( &operation->ctx.gcm );
3764 status = mbedtls_to_psa_error(
3765 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3766 operation->slot->data.raw.data,
3767 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003768 if( status != 0 )
3769 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003770 break;
3771#endif /* MBEDTLS_GCM_C */
3772
Gilles Peskine26869f22019-05-06 15:25:00 +02003773#if defined(MBEDTLS_CHACHAPOLY_C)
3774 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3775 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3776 operation->full_tag_length = 16;
3777 /* We only support the default tag length. */
3778 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3779 return( PSA_ERROR_NOT_SUPPORTED );
3780 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3781 status = mbedtls_to_psa_error(
3782 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3783 operation->slot->data.raw.data ) );
3784 if( status != 0 )
3785 goto cleanup;
3786 break;
3787#endif /* MBEDTLS_CHACHAPOLY_C */
3788
Gilles Peskineedf9a652018-08-17 18:11:56 +02003789 default:
3790 return( PSA_ERROR_NOT_SUPPORTED );
3791 }
3792
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003793 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3794 {
3795 status = PSA_ERROR_INVALID_ARGUMENT;
3796 goto cleanup;
3797 }
3798 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003799
Gilles Peskineedf9a652018-08-17 18:11:56 +02003800 return( PSA_SUCCESS );
3801
3802cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003803 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003804 return( status );
3805}
3806
Gilles Peskinec5487a82018-12-03 18:08:14 +01003807psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003808 psa_algorithm_t alg,
3809 const uint8_t *nonce,
3810 size_t nonce_length,
3811 const uint8_t *additional_data,
3812 size_t additional_data_length,
3813 const uint8_t *plaintext,
3814 size_t plaintext_length,
3815 uint8_t *ciphertext,
3816 size_t ciphertext_size,
3817 size_t *ciphertext_length )
3818{
mohammad16035955c982018-04-26 00:53:03 +03003819 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003820 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003821 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003822
mohammad1603f08a5502018-06-03 15:05:47 +03003823 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003824
Gilles Peskinec5487a82018-12-03 18:08:14 +01003825 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003826 if( status != PSA_SUCCESS )
3827 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003828
Gilles Peskineedf9a652018-08-17 18:11:56 +02003829 /* For all currently supported modes, the tag is at the end of the
3830 * ciphertext. */
3831 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3832 {
3833 status = PSA_ERROR_BUFFER_TOO_SMALL;
3834 goto exit;
3835 }
3836 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003837
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003838#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003839 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003840 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003841 status = mbedtls_to_psa_error(
3842 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3843 MBEDTLS_GCM_ENCRYPT,
3844 plaintext_length,
3845 nonce, nonce_length,
3846 additional_data, additional_data_length,
3847 plaintext, ciphertext,
3848 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003849 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003850 else
3851#endif /* MBEDTLS_GCM_C */
3852#if defined(MBEDTLS_CCM_C)
3853 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003854 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003855 status = mbedtls_to_psa_error(
3856 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3857 plaintext_length,
3858 nonce, nonce_length,
3859 additional_data,
3860 additional_data_length,
3861 plaintext, ciphertext,
3862 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003863 }
mohammad16035c8845f2018-05-09 05:40:09 -07003864 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003865#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003866#if defined(MBEDTLS_CHACHAPOLY_C)
3867 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3868 {
3869 if( nonce_length != 12 || operation.tag_length != 16 )
3870 {
3871 status = PSA_ERROR_NOT_SUPPORTED;
3872 goto exit;
3873 }
3874 status = mbedtls_to_psa_error(
3875 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3876 plaintext_length,
3877 nonce,
3878 additional_data,
3879 additional_data_length,
3880 plaintext,
3881 ciphertext,
3882 tag ) );
3883 }
3884 else
3885#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003886 {
mohammad1603554faad2018-06-03 15:07:38 +03003887 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003888 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003889
Gilles Peskineedf9a652018-08-17 18:11:56 +02003890 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3891 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003892
Gilles Peskineedf9a652018-08-17 18:11:56 +02003893exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003894 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003895 if( status == PSA_SUCCESS )
3896 *ciphertext_length = plaintext_length + operation.tag_length;
3897 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003898}
3899
Gilles Peskineee652a32018-06-01 19:23:52 +02003900/* Locate the tag in a ciphertext buffer containing the encrypted data
3901 * followed by the tag. Return the length of the part preceding the tag in
3902 * *plaintext_length. This is the size of the plaintext in modes where
3903 * the encrypted data has the same size as the plaintext, such as
3904 * CCM and GCM. */
3905static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3906 const uint8_t *ciphertext,
3907 size_t ciphertext_length,
3908 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003909 const uint8_t **p_tag )
3910{
3911 size_t payload_length;
3912 if( tag_length > ciphertext_length )
3913 return( PSA_ERROR_INVALID_ARGUMENT );
3914 payload_length = ciphertext_length - tag_length;
3915 if( payload_length > plaintext_size )
3916 return( PSA_ERROR_BUFFER_TOO_SMALL );
3917 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003918 return( PSA_SUCCESS );
3919}
3920
Gilles Peskinec5487a82018-12-03 18:08:14 +01003921psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003922 psa_algorithm_t alg,
3923 const uint8_t *nonce,
3924 size_t nonce_length,
3925 const uint8_t *additional_data,
3926 size_t additional_data_length,
3927 const uint8_t *ciphertext,
3928 size_t ciphertext_length,
3929 uint8_t *plaintext,
3930 size_t plaintext_size,
3931 size_t *plaintext_length )
3932{
mohammad16035955c982018-04-26 00:53:03 +03003933 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003934 aead_operation_t operation;
3935 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003936
Gilles Peskineee652a32018-06-01 19:23:52 +02003937 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003938
Gilles Peskinec5487a82018-12-03 18:08:14 +01003939 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003940 if( status != PSA_SUCCESS )
3941 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003942
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003943 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3944 ciphertext, ciphertext_length,
3945 plaintext_size, &tag );
3946 if( status != PSA_SUCCESS )
3947 goto exit;
3948
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003949#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003950 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003951 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003952 status = mbedtls_to_psa_error(
3953 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3954 ciphertext_length - operation.tag_length,
3955 nonce, nonce_length,
3956 additional_data,
3957 additional_data_length,
3958 tag, operation.tag_length,
3959 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003960 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003961 else
3962#endif /* MBEDTLS_GCM_C */
3963#if defined(MBEDTLS_CCM_C)
3964 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003965 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003966 status = mbedtls_to_psa_error(
3967 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3968 ciphertext_length - operation.tag_length,
3969 nonce, nonce_length,
3970 additional_data,
3971 additional_data_length,
3972 ciphertext, plaintext,
3973 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003974 }
mohammad160339574652018-06-01 04:39:53 -07003975 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003976#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003977#if defined(MBEDTLS_CHACHAPOLY_C)
3978 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3979 {
3980 if( nonce_length != 12 || operation.tag_length != 16 )
3981 {
3982 status = PSA_ERROR_NOT_SUPPORTED;
3983 goto exit;
3984 }
3985 status = mbedtls_to_psa_error(
3986 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3987 ciphertext_length - operation.tag_length,
3988 nonce,
3989 additional_data,
3990 additional_data_length,
3991 tag,
3992 ciphertext,
3993 plaintext ) );
3994 }
3995 else
3996#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003997 {
mohammad1603554faad2018-06-03 15:07:38 +03003998 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003999 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004000
Gilles Peskineedf9a652018-08-17 18:11:56 +02004001 if( status != PSA_SUCCESS && plaintext_size != 0 )
4002 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004003
Gilles Peskineedf9a652018-08-17 18:11:56 +02004004exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004005 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004006 if( status == PSA_SUCCESS )
4007 *plaintext_length = ciphertext_length - operation.tag_length;
4008 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004009}
4010
Gilles Peskinea0655c32018-04-30 17:06:50 +02004011
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004012
Gilles Peskine20035e32018-02-03 22:44:14 +01004013/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004014/* Generators */
4015/****************************************************************/
4016
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004017#define HKDF_STATE_INIT 0 /* no input yet */
4018#define HKDF_STATE_STARTED 1 /* got salt */
4019#define HKDF_STATE_KEYED 2 /* got key */
4020#define HKDF_STATE_OUTPUT 3 /* output started */
4021
Gilles Peskinecbe66502019-05-16 16:59:18 +02004022static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004023 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004024{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004025 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4026 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004027 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004028 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004029}
4030
4031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004032psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004033{
4034 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004035 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004036 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004037 {
4038 /* The object has (apparently) been initialized but it is not
4039 * in use. It's ok to call abort on such an object, and there's
4040 * nothing to do. */
4041 }
4042 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004043#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004044 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004045 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004046 mbedtls_free( operation->ctx.hkdf.info );
4047 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004048 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004049 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004050 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004051 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004052 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004053#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004054 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004055 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004056 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4057 operation->ctx.tls12_prf.key_len );
4058 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004059 }
Hanno Becker580fba12018-11-13 20:50:45 +00004060
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004061 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004062 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004063 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4064 operation->ctx.tls12_prf.Ai_with_seed_len );
4065 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004066 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004067#else
4068 if( operation->ctx.tls12_prf.seed != NULL )
4069 {
4070 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4071 operation->ctx.tls12_prf.seed_length );
4072 mbedtls_free( operation->ctx.tls12_prf.seed );
4073 }
4074
4075 if( operation->ctx.tls12_prf.label != NULL )
4076 {
4077 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4078 operation->ctx.tls12_prf.label_length );
4079 mbedtls_free( operation->ctx.tls12_prf.label );
4080 }
4081
4082 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4083
4084 /* We leave the fields Ai and output_block to be erased safely by the
4085 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004086#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004087 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004088 else
4089#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004090 {
4091 status = PSA_ERROR_BAD_STATE;
4092 }
Janos Follath083036a2019-06-11 10:22:26 +01004093 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004094 return( status );
4095}
4096
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004097psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004098 size_t *capacity)
4099{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004100 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004101 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004103 return PSA_ERROR_BAD_STATE;
4104 }
4105
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004106 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004107 return( PSA_SUCCESS );
4108}
4109
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004110psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004111 size_t capacity )
4112{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004113 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004114 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004115 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004116 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004117 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004118 return( PSA_SUCCESS );
4119}
4120
Gilles Peskinebef7f142018-07-12 17:22:21 +02004121#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004122/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004123 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004124static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004125 psa_algorithm_t hash_alg,
4126 uint8_t *output,
4127 size_t output_length )
4128{
4129 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4130 psa_status_t status;
4131
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004132 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4133 return( PSA_ERROR_BAD_STATE );
4134 hkdf->state = HKDF_STATE_OUTPUT;
4135
Gilles Peskinebef7f142018-07-12 17:22:21 +02004136 while( output_length != 0 )
4137 {
4138 /* Copy what remains of the current block */
4139 uint8_t n = hash_length - hkdf->offset_in_block;
4140 if( n > output_length )
4141 n = (uint8_t) output_length;
4142 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4143 output += n;
4144 output_length -= n;
4145 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004146 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004147 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004148 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004149 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004150 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004151 * object was corrupted or if this function is called directly
4152 * inside the library. */
4153 if( hkdf->block_number == 0xff )
4154 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004155
4156 /* We need a new block */
4157 ++hkdf->block_number;
4158 hkdf->offset_in_block = 0;
4159 status = psa_hmac_setup_internal( &hkdf->hmac,
4160 hkdf->prk, hash_length,
4161 hash_alg );
4162 if( status != PSA_SUCCESS )
4163 return( status );
4164 if( hkdf->block_number != 1 )
4165 {
4166 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4167 hkdf->output_block,
4168 hash_length );
4169 if( status != PSA_SUCCESS )
4170 return( status );
4171 }
4172 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4173 hkdf->info,
4174 hkdf->info_length );
4175 if( status != PSA_SUCCESS )
4176 return( status );
4177 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4178 &hkdf->block_number, 1 );
4179 if( status != PSA_SUCCESS )
4180 return( status );
4181 status = psa_hmac_finish_internal( &hkdf->hmac,
4182 hkdf->output_block,
4183 sizeof( hkdf->output_block ) );
4184 if( status != PSA_SUCCESS )
4185 return( status );
4186 }
4187
4188 return( PSA_SUCCESS );
4189}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004190
Janos Follath999f6482019-06-11 12:04:10 +01004191#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004192static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4193 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004194 psa_algorithm_t alg )
4195{
4196 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4197 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4198 psa_hmac_internal_data hmac;
4199 psa_status_t status, cleanup_status;
4200
Hanno Becker3b339e22018-11-13 20:56:14 +00004201 unsigned char *Ai;
4202 size_t Ai_len;
4203
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004204 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004205 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004206 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004207 * object was corrupted or if this function is called directly
4208 * inside the library. */
4209 if( tls12_prf->block_number == 0xff )
4210 return( PSA_ERROR_BAD_STATE );
4211
4212 /* We need a new block */
4213 ++tls12_prf->block_number;
4214 tls12_prf->offset_in_block = 0;
4215
4216 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4217 *
4218 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4219 *
4220 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4221 * HMAC_hash(secret, A(2) + seed) +
4222 * HMAC_hash(secret, A(3) + seed) + ...
4223 *
4224 * A(0) = seed
4225 * A(i) = HMAC_hash( secret, A(i-1) )
4226 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004227 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004228 * `HMAC_hash(secret, A(i) + seed)` from which the output
4229 * is currently extracted as `output_block`, while
4230 * `A(i) + seed` is stored in `Ai_with_seed`.
4231 *
4232 * Generating a new block means recalculating `Ai_with_seed`
4233 * from the A(i)-part of it, and afterwards recalculating
4234 * `output_block`.
4235 *
4236 * A(0) is computed at setup time.
4237 *
4238 */
4239
4240 psa_hmac_init_internal( &hmac );
4241
4242 /* We must distinguish the calculation of A(1) from those
4243 * of A(2) and higher, because A(0)=seed has a different
4244 * length than the other A(i). */
4245 if( tls12_prf->block_number == 1 )
4246 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004247 Ai = tls12_prf->Ai_with_seed + hash_length;
4248 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004249 }
4250 else
4251 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004252 Ai = tls12_prf->Ai_with_seed;
4253 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004254 }
4255
Hanno Becker3b339e22018-11-13 20:56:14 +00004256 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4257 status = psa_hmac_setup_internal( &hmac,
4258 tls12_prf->key,
4259 tls12_prf->key_len,
4260 hash_alg );
4261 if( status != PSA_SUCCESS )
4262 goto cleanup;
4263
4264 status = psa_hash_update( &hmac.hash_ctx,
4265 Ai, Ai_len );
4266 if( status != PSA_SUCCESS )
4267 goto cleanup;
4268
4269 status = psa_hmac_finish_internal( &hmac,
4270 tls12_prf->Ai_with_seed,
4271 hash_length );
4272 if( status != PSA_SUCCESS )
4273 goto cleanup;
4274
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004275 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4276 status = psa_hmac_setup_internal( &hmac,
4277 tls12_prf->key,
4278 tls12_prf->key_len,
4279 hash_alg );
4280 if( status != PSA_SUCCESS )
4281 goto cleanup;
4282
4283 status = psa_hash_update( &hmac.hash_ctx,
4284 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004285 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004286 if( status != PSA_SUCCESS )
4287 goto cleanup;
4288
4289 status = psa_hmac_finish_internal( &hmac,
4290 tls12_prf->output_block,
4291 hash_length );
4292 if( status != PSA_SUCCESS )
4293 goto cleanup;
4294
4295cleanup:
4296
4297 cleanup_status = psa_hmac_abort_internal( &hmac );
4298 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4299 status = cleanup_status;
4300
4301 return( status );
4302}
Janos Follath7742fee2019-06-17 12:58:10 +01004303#else
4304static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4305 psa_tls12_prf_key_derivation_t *tls12_prf,
4306 psa_algorithm_t alg )
4307{
4308 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4309 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004310 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4311 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004312
Janos Follath7742fee2019-06-17 12:58:10 +01004313 /* We can't be wanting more output after block 0xff, otherwise
4314 * the capacity check in psa_key_derivation_output_bytes() would have
4315 * prevented this call. It could happen only if the operation
4316 * object was corrupted or if this function is called directly
4317 * inside the library. */
4318 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004319 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004320
4321 /* We need a new block */
4322 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004323 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004324
4325 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4326 *
4327 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4328 *
4329 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4330 * HMAC_hash(secret, A(2) + seed) +
4331 * HMAC_hash(secret, A(3) + seed) + ...
4332 *
4333 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004334 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004335 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004336 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004337 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004338 * is currently extracted as `output_block` and where i is
4339 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004340 */
4341
Janos Follathea29bfb2019-06-19 12:21:20 +01004342 /* Save the hash context before using it, to preserve the hash state with
4343 * only the inner padding in it. We need this, because inner padding depends
4344 * on the key (secret in the RFC's terminology). */
4345 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4346 if( status != PSA_SUCCESS )
4347 goto cleanup;
4348
4349 /* Calculate A(i) where i = tls12_prf->block_number. */
4350 if( tls12_prf->block_number == 1 )
4351 {
4352 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4353 * the variable seed and in this instance means it in the context of the
4354 * P_hash function, where seed = label + seed.) */
4355 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4356 tls12_prf->label, tls12_prf->label_length );
4357 if( status != PSA_SUCCESS )
4358 goto cleanup;
4359 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4360 tls12_prf->seed, tls12_prf->seed_length );
4361 if( status != PSA_SUCCESS )
4362 goto cleanup;
4363 }
4364 else
4365 {
4366 /* A(i) = HMAC_hash(secret, A(i-1)) */
4367 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4368 tls12_prf->Ai, hash_length );
4369 if( status != PSA_SUCCESS )
4370 goto cleanup;
4371 }
4372
4373 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4374 tls12_prf->Ai, hash_length );
4375 if( status != PSA_SUCCESS )
4376 goto cleanup;
4377 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4378 if( status != PSA_SUCCESS )
4379 goto cleanup;
4380
4381 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4382 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4383 tls12_prf->Ai, hash_length );
4384 if( status != PSA_SUCCESS )
4385 goto cleanup;
4386 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4387 tls12_prf->label, tls12_prf->label_length );
4388 if( status != PSA_SUCCESS )
4389 goto cleanup;
4390 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4391 tls12_prf->seed, tls12_prf->seed_length );
4392 if( status != PSA_SUCCESS )
4393 goto cleanup;
4394 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4395 tls12_prf->output_block, hash_length );
4396 if( status != PSA_SUCCESS )
4397 goto cleanup;
4398 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4399 if( status != PSA_SUCCESS )
4400 goto cleanup;
4401
Janos Follath7742fee2019-06-17 12:58:10 +01004402
4403cleanup:
4404
Janos Follathea29bfb2019-06-19 12:21:20 +01004405 cleanup_status = psa_hash_abort( &backup );
4406 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4407 status = cleanup_status;
4408
4409 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004410}
Janos Follath999f6482019-06-11 12:04:10 +01004411#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004412
Janos Follath999f6482019-06-11 12:04:10 +01004413#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004415 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004416static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004417 psa_tls12_prf_key_derivation_t *tls12_prf,
4418 psa_algorithm_t alg,
4419 uint8_t *output,
4420 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004421{
4422 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4423 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4424 psa_status_t status;
4425
4426 while( output_length != 0 )
4427 {
4428 /* Copy what remains of the current block */
4429 uint8_t n = hash_length - tls12_prf->offset_in_block;
4430
4431 /* Check if we have fully processed the current block. */
4432 if( n == 0 )
4433 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004434 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004435 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004436 if( status != PSA_SUCCESS )
4437 return( status );
4438
4439 continue;
4440 }
4441
4442 if( n > output_length )
4443 n = (uint8_t) output_length;
4444 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4445 n );
4446 output += n;
4447 output_length -= n;
4448 tls12_prf->offset_in_block += n;
4449 }
4450
4451 return( PSA_SUCCESS );
4452}
Janos Follath844eb0e2019-06-19 12:10:49 +01004453#else
4454static psa_status_t psa_key_derivation_tls12_prf_read(
4455 psa_tls12_prf_key_derivation_t *tls12_prf,
4456 psa_algorithm_t alg,
4457 uint8_t *output,
4458 size_t output_length )
4459{
4460 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4461 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4462 psa_status_t status;
4463 uint8_t offset, length;
4464
4465 while( output_length != 0 )
4466 {
4467 /* Check if we have fully processed the current block. */
4468 if( tls12_prf->left_in_block == 0 )
4469 {
4470 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4471 alg );
4472 if( status != PSA_SUCCESS )
4473 return( status );
4474
4475 continue;
4476 }
4477
4478 if( tls12_prf->left_in_block > output_length )
4479 length = (uint8_t) output_length;
4480 else
4481 length = tls12_prf->left_in_block;
4482
4483 offset = hash_length - tls12_prf->left_in_block;
4484 memcpy( output, tls12_prf->output_block + offset, length );
4485 output += length;
4486 output_length -= length;
4487 tls12_prf->left_in_block -= length;
4488 }
4489
4490 return( PSA_SUCCESS );
4491}
Janos Follath999f6482019-06-11 12:04:10 +01004492#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004493#endif /* MBEDTLS_MD_C */
4494
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004495psa_status_t psa_key_derivation_output_bytes(
4496 psa_key_derivation_operation_t *operation,
4497 uint8_t *output,
4498 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004499{
4500 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004502
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004504 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004506 return PSA_ERROR_BAD_STATE;
4507 }
4508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004510 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004512 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004514 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004515 goto exit;
4516 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004518 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004520 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004521 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4522 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004524 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004525 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004526 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004528
Gilles Peskinebef7f142018-07-12 17:22:21 +02004529#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004530 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004531 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004532 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004534 output, output_length );
4535 }
Janos Follathadbec812019-06-14 11:05:39 +01004536 else
Janos Follathadbec812019-06-14 11:05:39 +01004537 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004538 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004539 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004541 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004542 output_length );
4543 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004544 else
4545#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004546 {
4547 return( PSA_ERROR_BAD_STATE );
4548 }
4549
4550exit:
4551 if( status != PSA_SUCCESS )
4552 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004553 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 * This allows us to differentiate between exhausted operations and
4555 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4556 * operations. */
4557 psa_algorithm_t alg = operation->alg;
4558 psa_key_derivation_abort( operation );
4559 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004560 memset( output, '!', output_length );
4561 }
4562 return( status );
4563}
4564
Gilles Peskine08542d82018-07-19 17:05:42 +02004565#if defined(MBEDTLS_DES_C)
4566static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4567{
4568 if( data_size >= 8 )
4569 mbedtls_des_key_set_parity( data );
4570 if( data_size >= 16 )
4571 mbedtls_des_key_set_parity( data + 8 );
4572 if( data_size >= 24 )
4573 mbedtls_des_key_set_parity( data + 16 );
4574}
4575#endif /* MBEDTLS_DES_C */
4576
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004577static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004578 psa_key_slot_t *slot,
4579 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004580 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004581{
4582 uint8_t *data = NULL;
4583 size_t bytes = PSA_BITS_TO_BYTES( bits );
4584 psa_status_t status;
4585
4586 if( ! key_type_is_raw_bytes( slot->type ) )
4587 return( PSA_ERROR_INVALID_ARGUMENT );
4588 if( bits % 8 != 0 )
4589 return( PSA_ERROR_INVALID_ARGUMENT );
4590 data = mbedtls_calloc( 1, bytes );
4591 if( data == NULL )
4592 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4593
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004595 if( status != PSA_SUCCESS )
4596 goto exit;
4597#if defined(MBEDTLS_DES_C)
4598 if( slot->type == PSA_KEY_TYPE_DES )
4599 psa_des_set_key_parity( data, bytes );
4600#endif /* MBEDTLS_DES_C */
4601 status = psa_import_key_into_slot( slot, data, bytes );
4602
4603exit:
4604 mbedtls_free( data );
4605 return( status );
4606}
4607
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004608psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004610 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004611{
4612 psa_status_t status;
4613 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004614 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004615 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004616 if( status == PSA_SUCCESS )
4617 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004618 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004619 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004620 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004621 }
4622 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004623 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004624 if( status != PSA_SUCCESS )
4625 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004626 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004627 *handle = 0;
4628 }
4629 return( status );
4630}
4631
Gilles Peskineeab56e42018-07-12 17:12:33 +02004632
4633
4634/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004635/* Key derivation */
4636/****************************************************************/
4637
Gilles Peskinea05219c2018-11-16 16:02:56 +01004638#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004639#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004641 * of the HKDF algorithm.
4642 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004643 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004644 * to potentially free embedded data structures and wipe confidential data.
4645 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004646static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004647 const uint8_t *secret,
4648 size_t secret_length,
4649 psa_algorithm_t hash_alg,
4650 const uint8_t *salt,
4651 size_t salt_length,
4652 const uint8_t *label,
4653 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004654{
4655 psa_status_t status;
4656 status = psa_hmac_setup_internal( &hkdf->hmac,
4657 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004658 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004659 if( status != PSA_SUCCESS )
4660 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004661 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004662 if( status != PSA_SUCCESS )
4663 return( status );
4664 status = psa_hmac_finish_internal( &hkdf->hmac,
4665 hkdf->prk,
4666 sizeof( hkdf->prk ) );
4667 if( status != PSA_SUCCESS )
4668 return( status );
4669 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4670 hkdf->block_number = 0;
4671 hkdf->info_length = label_length;
4672 if( label_length != 0 )
4673 {
4674 hkdf->info = mbedtls_calloc( 1, label_length );
4675 if( hkdf->info == NULL )
4676 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4677 memcpy( hkdf->info, label, label_length );
4678 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004679 hkdf->state = HKDF_STATE_KEYED;
4680 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004681 return( PSA_SUCCESS );
4682}
Janos Follath71a4c912019-06-11 09:14:47 +01004683#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004684#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004685
Gilles Peskinea05219c2018-11-16 16:02:56 +01004686#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004687#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004689 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004690 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004691 * to potentially free embedded data structures and wipe confidential data.
4692 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004693static psa_status_t psa_key_derivation_tls12_prf_setup(
4694 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004695 const unsigned char *key,
4696 size_t key_len,
4697 psa_algorithm_t hash_alg,
4698 const uint8_t *salt,
4699 size_t salt_length,
4700 const uint8_t *label,
4701 size_t label_length )
4702{
4703 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004704 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4705 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004706
4707 tls12_prf->key = mbedtls_calloc( 1, key_len );
4708 if( tls12_prf->key == NULL )
4709 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4710 tls12_prf->key_len = key_len;
4711 memcpy( tls12_prf->key, key, key_len );
4712
Hanno Becker580fba12018-11-13 20:50:45 +00004713 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004714 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004715 if( overflow )
4716 return( PSA_ERROR_INVALID_ARGUMENT );
4717
4718 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4719 if( tls12_prf->Ai_with_seed == NULL )
4720 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4721 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4722
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004723 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4724 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004725 if( label_length != 0 )
4726 {
4727 memcpy( tls12_prf->Ai_with_seed + hash_length,
4728 label, label_length );
4729 }
4730
4731 if( salt_length != 0 )
4732 {
4733 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4734 salt, salt_length );
4735 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004736
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004737 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004738 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004739 tls12_prf->block_number = 0;
4740 tls12_prf->offset_in_block = hash_length;
4741
4742 return( PSA_SUCCESS );
4743}
Janos Follath71a4c912019-06-11 09:14:47 +01004744#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004745
Janos Follath71a4c912019-06-11 09:14:47 +01004746#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004748static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4749 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004750 const unsigned char *psk,
4751 size_t psk_len,
4752 psa_algorithm_t hash_alg,
4753 const uint8_t *salt,
4754 size_t salt_length,
4755 const uint8_t *label,
4756 size_t label_length )
4757{
4758 psa_status_t status;
4759 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4760
4761 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4762 return( PSA_ERROR_INVALID_ARGUMENT );
4763
4764 /* Quoting RFC 4279, Section 2:
4765 *
4766 * The premaster secret is formed as follows: if the PSK is N octets
4767 * long, concatenate a uint16 with the value N, N zero octets, a second
4768 * uint16 with the value N, and the PSK itself.
4769 */
4770
4771 pms[0] = ( psk_len >> 8 ) & 0xff;
4772 pms[1] = ( psk_len >> 0 ) & 0xff;
4773 memset( pms + 2, 0, psk_len );
4774 pms[2 + psk_len + 0] = pms[0];
4775 pms[2 + psk_len + 1] = pms[1];
4776 memcpy( pms + 4 + psk_len, psk, psk_len );
4777
Gilles Peskinecbe66502019-05-16 16:59:18 +02004778 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004779 pms, 4 + 2 * psk_len,
4780 hash_alg,
4781 salt, salt_length,
4782 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004783
Gilles Peskine3f108122018-12-07 18:14:53 +01004784 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004785 return( status );
4786}
Janos Follath71a4c912019-06-11 09:14:47 +01004787#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004788#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004789
Janos Follath71a4c912019-06-11 09:14:47 +01004790#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004791/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004792 * to potentially free embedded data structures and wipe confidential data.
4793 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004794static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004796 const uint8_t *secret, size_t secret_length,
4797 psa_algorithm_t alg,
4798 const uint8_t *salt, size_t salt_length,
4799 const uint8_t *label, size_t label_length,
4800 size_t capacity )
4801{
4802 psa_status_t status;
4803 size_t max_capacity;
4804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 /* Set operation->alg even on failure so that abort knows what to do. */
4806 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004807
4808#if defined(MBEDTLS_MD_C)
4809 if( PSA_ALG_IS_HKDF( alg ) )
4810 {
4811 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4812 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4813 if( hash_size == 0 )
4814 return( PSA_ERROR_NOT_SUPPORTED );
4815 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004817 secret, secret_length,
4818 hash_alg,
4819 salt, salt_length,
4820 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004821 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004822 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4823 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4824 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004825 {
4826 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4827 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4828
4829 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4830 if( hash_alg != PSA_ALG_SHA_256 &&
4831 hash_alg != PSA_ALG_SHA_384 )
4832 {
4833 return( PSA_ERROR_NOT_SUPPORTED );
4834 }
4835
4836 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004837
4838 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4839 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004841 secret, secret_length,
4842 hash_alg, salt, salt_length,
4843 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004844 }
4845 else
4846 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004847 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004849 secret, secret_length,
4850 hash_alg, salt, salt_length,
4851 label, label_length );
4852 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004853 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004854 else
4855#endif
4856 {
4857 return( PSA_ERROR_NOT_SUPPORTED );
4858 }
4859
4860 if( status != PSA_SUCCESS )
4861 return( status );
4862
4863 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004865 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004867 else
4868 return( PSA_ERROR_INVALID_ARGUMENT );
4869
4870 return( PSA_SUCCESS );
4871}
Janos Follath71a4c912019-06-11 09:14:47 +01004872#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004873
Janos Follath71a4c912019-06-11 09:14:47 +01004874#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004876 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004877 psa_algorithm_t alg,
4878 const uint8_t *salt,
4879 size_t salt_length,
4880 const uint8_t *label,
4881 size_t label_length,
4882 size_t capacity )
4883{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004884 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004885 psa_status_t status;
4886
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004888 return( PSA_ERROR_BAD_STATE );
4889
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004890 /* Make sure that alg is a key derivation algorithm. This prevents
4891 * key selection algorithms, which psa_key_derivation_internal
4892 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004893 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4894 return( PSA_ERROR_INVALID_ARGUMENT );
4895
Gilles Peskinec5487a82018-12-03 18:08:14 +01004896 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004897 if( status != PSA_SUCCESS )
4898 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004899
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004900 if( slot->type != PSA_KEY_TYPE_DERIVE )
4901 return( PSA_ERROR_INVALID_ARGUMENT );
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004904 slot->data.raw.data,
4905 slot->data.raw.bytes,
4906 alg,
4907 salt, salt_length,
4908 label, label_length,
4909 capacity );
4910 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004912 return( status );
4913}
Janos Follath71a4c912019-06-11 09:14:47 +01004914#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004915
Gilles Peskine969c5d62019-01-16 15:53:06 +01004916static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004918 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004919{
Janos Follath5fe19732019-06-20 15:09:30 +01004920 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4921 * initialisers for this union leave some bytes unspecified.) */
4922 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4923
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4927 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4928 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004929 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4932 if( hash_size == 0 )
4933 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004934 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4935 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004936 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004937 {
4938 return( PSA_ERROR_NOT_SUPPORTED );
4939 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004941 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004942 }
4943#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004944 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004946}
4947
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 psa_algorithm_t alg )
4950{
4951 psa_status_t status;
4952
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954 return( PSA_ERROR_BAD_STATE );
4955
Gilles Peskine6843c292019-01-18 16:44:49 +01004956 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4957 return( PSA_ERROR_INVALID_ARGUMENT );
4958 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 {
4960 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962 }
4963 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4964 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004966 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 else
4968 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004969
4970 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004972 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973}
4974
4975#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004976static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004977 psa_algorithm_t hash_alg,
4978 psa_key_derivation_step_t step,
4979 const uint8_t *data,
4980 size_t data_length )
4981{
4982 psa_status_t status;
4983 switch( step )
4984 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004985 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004986 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004987 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004988 status = psa_hmac_setup_internal( &hkdf->hmac,
4989 data, data_length,
4990 hash_alg );
4991 if( status != PSA_SUCCESS )
4992 return( status );
4993 hkdf->state = HKDF_STATE_STARTED;
4994 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004995 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004996 /* If no salt was provided, use an empty salt. */
4997 if( hkdf->state == HKDF_STATE_INIT )
4998 {
4999 status = psa_hmac_setup_internal( &hkdf->hmac,
5000 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005001 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005002 if( status != PSA_SUCCESS )
5003 return( status );
5004 hkdf->state = HKDF_STATE_STARTED;
5005 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005006 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005007 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005008 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5009 data, data_length );
5010 if( status != PSA_SUCCESS )
5011 return( status );
5012 status = psa_hmac_finish_internal( &hkdf->hmac,
5013 hkdf->prk,
5014 sizeof( hkdf->prk ) );
5015 if( status != PSA_SUCCESS )
5016 return( status );
5017 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5018 hkdf->block_number = 0;
5019 hkdf->state = HKDF_STATE_KEYED;
5020 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005021 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005022 if( hkdf->state == HKDF_STATE_OUTPUT )
5023 return( PSA_ERROR_BAD_STATE );
5024 if( hkdf->info_set )
5025 return( PSA_ERROR_BAD_STATE );
5026 hkdf->info_length = data_length;
5027 if( data_length != 0 )
5028 {
5029 hkdf->info = mbedtls_calloc( 1, data_length );
5030 if( hkdf->info == NULL )
5031 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5032 memcpy( hkdf->info, data, data_length );
5033 }
5034 hkdf->info_set = 1;
5035 return( PSA_SUCCESS );
5036 default:
5037 return( PSA_ERROR_INVALID_ARGUMENT );
5038 }
5039}
Janos Follathb03233e2019-06-11 15:30:30 +01005040
5041#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5042static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5043 psa_algorithm_t hash_alg,
5044 psa_key_derivation_step_t step,
5045 const uint8_t *data,
5046 size_t data_length )
5047{
5048 (void) prf;
5049 (void) hash_alg;
5050 (void) step;
5051 (void) data;
5052 (void) data_length;
5053
5054 return( PSA_ERROR_INVALID_ARGUMENT );
5055}
Janos Follath6660f0e2019-06-17 08:44:03 +01005056
5057static psa_status_t psa_tls12_prf_psk_to_ms_input(
5058 psa_tls12_prf_key_derivation_t *prf,
5059 psa_algorithm_t hash_alg,
5060 psa_key_derivation_step_t step,
5061 const uint8_t *data,
5062 size_t data_length )
5063{
5064 (void) prf;
5065 (void) hash_alg;
5066 (void) step;
5067 (void) data;
5068 (void) data_length;
5069
5070 return( PSA_ERROR_INVALID_ARGUMENT );
5071}
Janos Follathb03233e2019-06-11 15:30:30 +01005072#else
Janos Follathf08e2652019-06-13 09:05:41 +01005073static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5074 const uint8_t *data,
5075 size_t data_length )
5076{
5077 if( prf->state != TLS12_PRF_STATE_INIT )
5078 return( PSA_ERROR_BAD_STATE );
5079
Janos Follathd6dce9f2019-07-04 09:11:38 +01005080 if( data_length != 0 )
5081 {
5082 prf->seed = mbedtls_calloc( 1, data_length );
5083 if( prf->seed == NULL )
5084 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005085
Janos Follathd6dce9f2019-07-04 09:11:38 +01005086 memcpy( prf->seed, data, data_length );
5087 prf->seed_length = data_length;
5088 }
Janos Follathf08e2652019-06-13 09:05:41 +01005089
5090 prf->state = TLS12_PRF_STATE_SEED_SET;
5091
5092 return( PSA_SUCCESS );
5093}
5094
Janos Follath81550542019-06-13 14:26:34 +01005095static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5096 psa_algorithm_t hash_alg,
5097 const uint8_t *data,
5098 size_t data_length )
5099{
5100 psa_status_t status;
5101 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5102 return( PSA_ERROR_BAD_STATE );
5103
5104 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5105 if( status != PSA_SUCCESS )
5106 return( status );
5107
5108 prf->state = TLS12_PRF_STATE_KEY_SET;
5109
5110 return( PSA_SUCCESS );
5111}
5112
Janos Follath6660f0e2019-06-17 08:44:03 +01005113static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5114 psa_tls12_prf_key_derivation_t *prf,
5115 psa_algorithm_t hash_alg,
5116 const uint8_t *data,
5117 size_t data_length )
5118{
5119 psa_status_t status;
5120 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005121 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005122
5123 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5124 return( PSA_ERROR_INVALID_ARGUMENT );
5125
5126 /* Quoting RFC 4279, Section 2:
5127 *
5128 * The premaster secret is formed as follows: if the PSK is N octets
5129 * long, concatenate a uint16 with the value N, N zero octets, a second
5130 * uint16 with the value N, and the PSK itself.
5131 */
5132
Janos Follath40e13932019-06-26 13:22:29 +01005133 *cur++ = ( data_length >> 8 ) & 0xff;
5134 *cur++ = ( data_length >> 0 ) & 0xff;
5135 memset( cur, 0, data_length );
5136 cur += data_length;
5137 *cur++ = pms[0];
5138 *cur++ = pms[1];
5139 memcpy( cur, data, data_length );
5140 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005141
Janos Follath40e13932019-06-26 13:22:29 +01005142 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005143
5144 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5145 return( status );
5146}
5147
Janos Follath63028dd2019-06-13 09:15:47 +01005148static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5149 const uint8_t *data,
5150 size_t data_length )
5151{
5152 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5153 return( PSA_ERROR_BAD_STATE );
5154
Janos Follathd6dce9f2019-07-04 09:11:38 +01005155 if( data_length != 0 )
5156 {
5157 prf->label = mbedtls_calloc( 1, data_length );
5158 if( prf->label == NULL )
5159 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005160
Janos Follathd6dce9f2019-07-04 09:11:38 +01005161 memcpy( prf->label, data, data_length );
5162 prf->label_length = data_length;
5163 }
Janos Follath63028dd2019-06-13 09:15:47 +01005164
5165 prf->state = TLS12_PRF_STATE_LABEL_SET;
5166
5167 return( PSA_SUCCESS );
5168}
5169
Janos Follathb03233e2019-06-11 15:30:30 +01005170static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5171 psa_algorithm_t hash_alg,
5172 psa_key_derivation_step_t step,
5173 const uint8_t *data,
5174 size_t data_length )
5175{
Janos Follathb03233e2019-06-11 15:30:30 +01005176 switch( step )
5177 {
Janos Follathf08e2652019-06-13 09:05:41 +01005178 case PSA_KEY_DERIVATION_INPUT_SEED:
5179 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005180 case PSA_KEY_DERIVATION_INPUT_SECRET:
5181 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005182 case PSA_KEY_DERIVATION_INPUT_LABEL:
5183 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005184 default:
5185 return( PSA_ERROR_INVALID_ARGUMENT );
5186 }
5187}
Janos Follath6660f0e2019-06-17 08:44:03 +01005188
5189static psa_status_t psa_tls12_prf_psk_to_ms_input(
5190 psa_tls12_prf_key_derivation_t *prf,
5191 psa_algorithm_t hash_alg,
5192 psa_key_derivation_step_t step,
5193 const uint8_t *data,
5194 size_t data_length )
5195{
5196 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005197 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005198 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5199 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005200 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005201
5202 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5203}
Janos Follathb03233e2019-06-11 15:30:30 +01005204#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205#endif /* MBEDTLS_MD_C */
5206
Janos Follathb80a94e2019-06-12 15:54:46 +01005207static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005209 psa_key_derivation_step_t step,
5210 const uint8_t *data,
5211 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212{
5213 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215
5216#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005217 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005218 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221 step, data, data_length );
5222 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005223 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005224#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005226 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227 {
Janos Follathb03233e2019-06-11 15:30:30 +01005228 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5229 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5230 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005231 }
5232 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5233 {
5234 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5235 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5236 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 }
5238 else
5239#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005242 return( PSA_ERROR_BAD_STATE );
5243 }
5244
5245 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247 return( status );
5248}
5249
Janos Follath51f4a0f2019-06-14 11:35:55 +01005250psa_status_t psa_key_derivation_input_bytes(
5251 psa_key_derivation_operation_t *operation,
5252 psa_key_derivation_step_t step,
5253 const uint8_t *data,
5254 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005255{
Janos Follathc5621512019-06-14 11:27:57 +01005256 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5257 return( PSA_ERROR_INVALID_ARGUMENT );
5258
5259 return( psa_key_derivation_input_internal( operation, step,
5260 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005261}
5262
Janos Follath51f4a0f2019-06-14 11:35:55 +01005263psa_status_t psa_key_derivation_input_key(
5264 psa_key_derivation_operation_t *operation,
5265 psa_key_derivation_step_t step,
5266 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005267{
5268 psa_key_slot_t *slot;
5269 psa_status_t status;
5270 status = psa_get_key_from_slot( handle, &slot,
5271 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273 if( status != PSA_SUCCESS )
5274 return( status );
5275 if( slot->type != PSA_KEY_TYPE_DERIVE )
5276 return( PSA_ERROR_INVALID_ARGUMENT );
5277 /* Don't allow a key to be used as an input that is usually public.
5278 * This is debatable. It's ok from a cryptographic perspective to
5279 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005280 * the material should be dedicated to a particular input step,
5281 * otherwise this may allow the key to be used in an unintended way
5282 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005283 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005284 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005285 return( psa_key_derivation_input_internal( operation,
5286 step,
5287 slot->data.raw.data,
5288 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005289}
5290
Gilles Peskineea0fb492018-07-12 17:17:20 +02005291
5292
5293/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005294/* Key agreement */
5295/****************************************************************/
5296
Gilles Peskinea05219c2018-11-16 16:02:56 +01005297#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005298static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5299 size_t peer_key_length,
5300 const mbedtls_ecp_keypair *our_key,
5301 uint8_t *shared_secret,
5302 size_t shared_secret_size,
5303 size_t *shared_secret_length )
5304{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305 mbedtls_ecp_keypair *their_key = NULL;
5306 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005307 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005308 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005309
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005310 status = psa_import_ec_public_key(
5311 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5312 peer_key, peer_key_length,
5313 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005314 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005315 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005316
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005317 status = mbedtls_to_psa_error(
5318 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5319 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005321 status = mbedtls_to_psa_error(
5322 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5323 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005324 goto exit;
5325
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005326 status = mbedtls_to_psa_error(
5327 mbedtls_ecdh_calc_secret( &ecdh,
5328 shared_secret_length,
5329 shared_secret, shared_secret_size,
5330 mbedtls_ctr_drbg_random,
5331 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005332
5333exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005334 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005335 mbedtls_ecp_keypair_free( their_key );
5336 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005337 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005338}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005339#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005340
Gilles Peskine01d718c2018-09-18 12:01:02 +02005341#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5342
Gilles Peskine0216fe12019-04-11 21:23:21 +02005343static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5344 psa_key_slot_t *private_key,
5345 const uint8_t *peer_key,
5346 size_t peer_key_length,
5347 uint8_t *shared_secret,
5348 size_t shared_secret_size,
5349 size_t *shared_secret_length )
5350{
5351 switch( alg )
5352 {
5353#if defined(MBEDTLS_ECDH_C)
5354 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005355 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005356 return( PSA_ERROR_INVALID_ARGUMENT );
5357 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5358 private_key->data.ecp,
5359 shared_secret, shared_secret_size,
5360 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005361#endif /* MBEDTLS_ECDH_C */
5362 default:
5363 (void) private_key;
5364 (void) peer_key;
5365 (void) peer_key_length;
5366 (void) shared_secret;
5367 (void) shared_secret_size;
5368 (void) shared_secret_length;
5369 return( PSA_ERROR_NOT_SUPPORTED );
5370 }
5371}
5372
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005373/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005374 * to potentially free embedded data structures and wipe confidential data.
5375 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005377 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005378 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005379 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005380 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005381{
5382 psa_status_t status;
5383 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5384 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005385 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005386
5387 /* Step 1: run the secret agreement algorithm to generate the shared
5388 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005389 status = psa_key_agreement_raw_internal( ka_alg,
5390 private_key,
5391 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005392 shared_secret,
5393 sizeof( shared_secret ),
5394 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395 if( status != PSA_SUCCESS )
5396 goto exit;
5397
5398 /* Step 2: set up the key derivation to generate key material from
5399 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005400 status = psa_key_derivation_input_internal( operation, step,
5401 shared_secret,
5402 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005403
Gilles Peskine01d718c2018-09-18 12:01:02 +02005404exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005405 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005406 return( status );
5407}
5408
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005410 psa_key_derivation_step_t step,
5411 psa_key_handle_t private_key,
5412 const uint8_t *peer_key,
5413 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005414{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005415 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005416 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005417 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005418 return( PSA_ERROR_INVALID_ARGUMENT );
5419 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005421 if( status != PSA_SUCCESS )
5422 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005423 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005424 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005426 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005427 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005428 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005429}
5430
Gilles Peskinebe697d82019-05-16 18:00:41 +02005431psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5432 psa_key_handle_t private_key,
5433 const uint8_t *peer_key,
5434 size_t peer_key_length,
5435 uint8_t *output,
5436 size_t output_size,
5437 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005438{
5439 psa_key_slot_t *slot;
5440 psa_status_t status;
5441
5442 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5443 {
5444 status = PSA_ERROR_INVALID_ARGUMENT;
5445 goto exit;
5446 }
5447 status = psa_get_key_from_slot( private_key, &slot,
5448 PSA_KEY_USAGE_DERIVE, alg );
5449 if( status != PSA_SUCCESS )
5450 goto exit;
5451
5452 status = psa_key_agreement_raw_internal( alg, slot,
5453 peer_key, peer_key_length,
5454 output, output_size,
5455 output_length );
5456
5457exit:
5458 if( status != PSA_SUCCESS )
5459 {
5460 /* If an error happens and is not handled properly, the output
5461 * may be used as a key to protect sensitive data. Arrange for such
5462 * a key to be random, which is likely to result in decryption or
5463 * verification errors. This is better than filling the buffer with
5464 * some constant data such as zeros, which would result in the data
5465 * being protected with a reproducible, easily knowable key.
5466 */
5467 psa_generate_random( output, output_size );
5468 *output_length = output_size;
5469 }
5470 return( status );
5471}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005472
5473
5474/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005475/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005476/****************************************************************/
5477
5478psa_status_t psa_generate_random( uint8_t *output,
5479 size_t output_size )
5480{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005481 int ret;
5482 GUARD_MODULE_INITIALIZED;
5483
5484 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005485 return( mbedtls_to_psa_error( ret ) );
5486}
5487
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005488#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5489#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005490
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005491psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5492 size_t seed_size )
5493{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005494 if( global_data.initialized )
5495 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005496
5497 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5498 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5499 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5500 return( PSA_ERROR_INVALID_ARGUMENT );
5501
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005502 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005503}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005504#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005505
Gilles Peskinee56e8782019-04-26 17:34:02 +02005506#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5507static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5508 size_t domain_parameters_size,
5509 int *exponent )
5510{
5511 size_t i;
5512 uint32_t acc = 0;
5513
5514 if( domain_parameters_size == 0 )
5515 {
5516 *exponent = 65537;
5517 return( PSA_SUCCESS );
5518 }
5519
5520 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5521 * support values that fit in a 32-bit integer, which is larger than
5522 * int on just about every platform anyway. */
5523 if( domain_parameters_size > sizeof( acc ) )
5524 return( PSA_ERROR_NOT_SUPPORTED );
5525 for( i = 0; i < domain_parameters_size; i++ )
5526 acc = ( acc << 8 ) | domain_parameters[i];
5527 if( acc > INT_MAX )
5528 return( PSA_ERROR_NOT_SUPPORTED );
5529 *exponent = acc;
5530 return( PSA_SUCCESS );
5531}
5532#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5533
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005534static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005535 psa_key_slot_t *slot, size_t bits,
5536 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005537{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005539
Gilles Peskinee56e8782019-04-26 17:34:02 +02005540 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005541 return( PSA_ERROR_INVALID_ARGUMENT );
5542
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005543 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005544 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005545 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005546 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005547 if( status != PSA_SUCCESS )
5548 return( status );
5549 status = psa_generate_random( slot->data.raw.data,
5550 slot->data.raw.bytes );
5551 if( status != PSA_SUCCESS )
5552 {
5553 mbedtls_free( slot->data.raw.data );
5554 return( status );
5555 }
5556#if defined(MBEDTLS_DES_C)
5557 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005558 psa_des_set_key_parity( slot->data.raw.data,
5559 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005560#endif /* MBEDTLS_DES_C */
5561 }
5562 else
5563
5564#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005565 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005566 {
5567 mbedtls_rsa_context *rsa;
5568 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005569 int exponent;
5570 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005571 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5572 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005573 /* Accept only byte-aligned keys, for the same reasons as
5574 * in psa_import_rsa_key(). */
5575 if( bits % 8 != 0 )
5576 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005577 status = psa_read_rsa_exponent( domain_parameters,
5578 domain_parameters_size,
5579 &exponent );
5580 if( status != PSA_SUCCESS )
5581 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005582 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5583 if( rsa == NULL )
5584 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5585 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5586 ret = mbedtls_rsa_gen_key( rsa,
5587 mbedtls_ctr_drbg_random,
5588 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005589 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005590 exponent );
5591 if( ret != 0 )
5592 {
5593 mbedtls_rsa_free( rsa );
5594 mbedtls_free( rsa );
5595 return( mbedtls_to_psa_error( ret ) );
5596 }
5597 slot->data.rsa = rsa;
5598 }
5599 else
5600#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5601
5602#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005603 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005604 {
5605 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5606 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5607 const mbedtls_ecp_curve_info *curve_info =
5608 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5609 mbedtls_ecp_keypair *ecp;
5610 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005611 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005612 return( PSA_ERROR_NOT_SUPPORTED );
5613 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5614 return( PSA_ERROR_NOT_SUPPORTED );
5615 if( curve_info->bit_size != bits )
5616 return( PSA_ERROR_INVALID_ARGUMENT );
5617 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5618 if( ecp == NULL )
5619 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5620 mbedtls_ecp_keypair_init( ecp );
5621 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5622 mbedtls_ctr_drbg_random,
5623 &global_data.ctr_drbg );
5624 if( ret != 0 )
5625 {
5626 mbedtls_ecp_keypair_free( ecp );
5627 mbedtls_free( ecp );
5628 return( mbedtls_to_psa_error( ret ) );
5629 }
5630 slot->data.ecp = ecp;
5631 }
5632 else
5633#endif /* MBEDTLS_ECP_C */
5634
5635 return( PSA_ERROR_NOT_SUPPORTED );
5636
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 return( PSA_SUCCESS );
5638}
5639
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005640psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005641 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005642{
5643 psa_status_t status;
5644 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005645 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005646 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005647 if( status == PSA_SUCCESS )
5648 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005649 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005650 slot, attributes->bits,
5651 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005652 }
5653 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005654 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005655 if( status != PSA_SUCCESS )
5656 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005657 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005658 *handle = 0;
5659 }
5660 return( status );
5661}
5662
5663
Gilles Peskine05d69892018-06-19 22:00:52 +02005664
5665/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005666/* Module setup */
5667/****************************************************************/
5668
Gilles Peskine5e769522018-11-20 21:59:56 +01005669psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5670 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5671 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5672{
5673 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5674 return( PSA_ERROR_BAD_STATE );
5675 global_data.entropy_init = entropy_init;
5676 global_data.entropy_free = entropy_free;
5677 return( PSA_SUCCESS );
5678}
5679
Gilles Peskinee59236f2018-01-27 23:32:46 +01005680void mbedtls_psa_crypto_free( void )
5681{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005682 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005683 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5684 {
5685 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005686 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005687 }
5688 /* Wipe all remaining data, including configuration.
5689 * In particular, this sets all state indicator to the value
5690 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005691 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005693 /* Unregister all secure element drivers, so that we restart from
5694 * a pristine state. */
5695 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005696#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005697}
5698
5699psa_status_t psa_crypto_init( void )
5700{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005701 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005702 const unsigned char drbg_seed[] = "PSA";
5703
Gilles Peskinec6b69072018-11-20 21:42:52 +01005704 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005705 if( global_data.initialized != 0 )
5706 return( PSA_SUCCESS );
5707
Gilles Peskine5e769522018-11-20 21:59:56 +01005708 /* Set default configuration if
5709 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5710 if( global_data.entropy_init == NULL )
5711 global_data.entropy_init = mbedtls_entropy_init;
5712 if( global_data.entropy_free == NULL )
5713 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005714
Gilles Peskinec6b69072018-11-20 21:42:52 +01005715 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005716 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005717 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005718 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005719 status = mbedtls_to_psa_error(
5720 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5721 mbedtls_entropy_func,
5722 &global_data.entropy,
5723 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5724 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005725 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005726 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005727
Gilles Peskine66fb1262018-12-10 16:29:04 +01005728 status = psa_initialize_key_slots( );
5729 if( status != PSA_SUCCESS )
5730 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005731
Gilles Peskinefc762652019-07-22 19:30:34 +02005732#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5733 status = psa_crypto_load_transaction( );
5734 if( status == PSA_SUCCESS )
5735 {
5736 /*TOnogrepDO: complete or abort the transaction*/
5737 }
5738 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5739 {
5740 /* There's no transaction to complete. It's all good. */
5741 status = PSA_SUCCESS;
5742 }
5743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5744
Gilles Peskinec6b69072018-11-20 21:42:52 +01005745 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005746 global_data.initialized = 1;
5747
Gilles Peskinee59236f2018-01-27 23:32:46 +01005748exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005749 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005750 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005751 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752}
5753
5754#endif /* MBEDTLS_PSA_CRYPTO_C */