blob: 92364ca4cdd90621d1776ab91ef583589944e250 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskine28f8f302019-07-24 13:30:31 +0200874/** Retrieve a slot which must contain a transparent key.
875 *
876 * A transparent key is a key for which the key material is directly
877 * available, as opposed to a key in a secure element.
878 *
879 * This is a temporary function until secure element support is
880 * fully implemented.
881 */
882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
883static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
884 psa_key_slot_t **p_slot,
885 psa_key_usage_t usage,
886 psa_algorithm_t alg )
887{
888 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
889 if( status != PSA_SUCCESS )
890 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200891 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200892 {
893 *p_slot = NULL;
894 return( PSA_ERROR_NOT_SUPPORTED );
895 }
896 return( PSA_SUCCESS );
897}
898#else /* MBEDTLS_PSA_CRYPTO_SE_C */
899/* With no secure element support, all keys are transparent. */
900#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
901 psa_get_key_from_slot( handle, p_slot, usage, alg )
902#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
903
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100904/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100905static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000906{
Gilles Peskine73167e12019-07-12 23:44:37 +0200907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
908 if( psa_key_slot_is_external( slot ) )
909 {
910 /* No key material to clean. */
911 }
912 else
913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000914 if( slot->type == PSA_KEY_TYPE_NONE )
915 {
916 /* No key material to clean. */
917 }
918 else if( key_type_is_raw_bytes( slot->type ) )
919 {
920 mbedtls_free( slot->data.raw.data );
921 }
922 else
923#if defined(MBEDTLS_RSA_C)
924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
925 {
926 mbedtls_rsa_free( slot->data.rsa );
927 mbedtls_free( slot->data.rsa );
928 }
929 else
930#endif /* defined(MBEDTLS_RSA_C) */
931#if defined(MBEDTLS_ECP_C)
932 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
933 {
934 mbedtls_ecp_keypair_free( slot->data.ecp );
935 mbedtls_free( slot->data.ecp );
936 }
937 else
938#endif /* defined(MBEDTLS_ECP_C) */
939 {
940 /* Shouldn't happen: the key type is not any type that we
941 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200942 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000943 }
944
945 return( PSA_SUCCESS );
946}
947
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100948static void psa_abort_operations_using_key( psa_key_slot_t *slot )
949{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200950 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951 (void) slot;
952}
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Completely wipe a slot in memory, including its policy.
955 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100956psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957{
958 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100959 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960 /* At this point, key material and other type-specific content has
961 * been wiped. Clear remaining metadata. We can call memset and not
962 * zeroize because the metadata is not particularly sensitive. */
963 memset( slot, 0, sizeof( *slot ) );
964 return( status );
965}
966
Gilles Peskinec5487a82018-12-03 18:08:14 +0100967psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100969 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100970 psa_status_t status = PSA_SUCCESS;
971 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
973 psa_se_drv_table_entry_t *driver;
974#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskinec5487a82018-12-03 18:08:14 +0100976 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200977 if( status != PSA_SUCCESS )
978 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200979
980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
981 driver = psa_get_se_driver_entry( slot->lifetime );
982 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200983 {
984 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
985 psa_crypto_transaction.key.lifetime = slot->lifetime;
986 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
987 psa_crypto_transaction.key.id = slot->persistent_storage_id;
988 status = psa_crypto_save_transaction( );
989 if( status != PSA_SUCCESS )
990 {
991 /* TOnogrepDO: destroy what can be destroyed anyway */
992 return( status );
993 }
994
Gilles Peskine354f7672019-07-12 23:46:38 +0200995 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +0200996 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
998
Darryl Greend49a4992018-06-18 17:27:26 +0100999#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1000 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1001 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001002 storage_status =
1003 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001004 }
1005#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001006
Gilles Peskinefc762652019-07-22 19:30:34 +02001007#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1008 if( driver != NULL )
1009 {
1010 status = psa_crypto_stop_transaction( );
1011 if( status != PSA_SUCCESS )
1012 {
1013 /* TOnogrepDO: destroy what can be destroyed anyway */
1014 return( status );
1015 }
1016 }
1017#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1018
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001019 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001020 if( status != PSA_SUCCESS )
1021 return( status );
1022 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023}
1024
Gilles Peskineb870b182018-07-06 16:02:09 +02001025/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001026static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001027{
1028 if( key_type_is_raw_bytes( slot->type ) )
1029 return( slot->data.raw.bytes * 8 );
1030#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001031 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001032 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001033#endif /* defined(MBEDTLS_RSA_C) */
1034#if defined(MBEDTLS_ECP_C)
1035 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1036 return( slot->data.ecp->grp.pbits );
1037#endif /* defined(MBEDTLS_ECP_C) */
1038 /* Shouldn't happen except on an empty slot. */
1039 return( 0 );
1040}
1041
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001042void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1043{
Gilles Peskineb699f072019-04-26 16:06:02 +02001044 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001045 memset( attributes, 0, sizeof( *attributes ) );
1046}
1047
Gilles Peskineb699f072019-04-26 16:06:02 +02001048psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1049 psa_key_type_t type,
1050 const uint8_t *data,
1051 size_t data_length )
1052{
1053 uint8_t *copy = NULL;
1054
1055 if( data_length != 0 )
1056 {
1057 copy = mbedtls_calloc( 1, data_length );
1058 if( copy == NULL )
1059 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1060 memcpy( copy, data, data_length );
1061 }
1062 /* After this point, this function is guaranteed to succeed, so it
1063 * can start modifying `*attributes`. */
1064
1065 if( attributes->domain_parameters != NULL )
1066 {
1067 mbedtls_free( attributes->domain_parameters );
1068 attributes->domain_parameters = NULL;
1069 attributes->domain_parameters_size = 0;
1070 }
1071
1072 attributes->domain_parameters = copy;
1073 attributes->domain_parameters_size = data_length;
1074 attributes->type = type;
1075 return( PSA_SUCCESS );
1076}
1077
1078psa_status_t psa_get_key_domain_parameters(
1079 const psa_key_attributes_t *attributes,
1080 uint8_t *data, size_t data_size, size_t *data_length )
1081{
1082 if( attributes->domain_parameters_size > data_size )
1083 return( PSA_ERROR_BUFFER_TOO_SMALL );
1084 *data_length = attributes->domain_parameters_size;
1085 if( attributes->domain_parameters_size != 0 )
1086 memcpy( data, attributes->domain_parameters,
1087 attributes->domain_parameters_size );
1088 return( PSA_SUCCESS );
1089}
1090
1091#if defined(MBEDTLS_RSA_C)
1092static psa_status_t psa_get_rsa_public_exponent(
1093 const mbedtls_rsa_context *rsa,
1094 psa_key_attributes_t *attributes )
1095{
1096 mbedtls_mpi mpi;
1097 int ret;
1098 uint8_t *buffer = NULL;
1099 size_t buflen;
1100 mbedtls_mpi_init( &mpi );
1101
1102 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1103 if( ret != 0 )
1104 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001105 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1106 {
1107 /* It's the default value, which is reported as an empty string,
1108 * so there's nothing to do. */
1109 goto exit;
1110 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001111
1112 buflen = mbedtls_mpi_size( &mpi );
1113 buffer = mbedtls_calloc( 1, buflen );
1114 if( buffer == NULL )
1115 {
1116 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1117 goto exit;
1118 }
1119 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1120 if( ret != 0 )
1121 goto exit;
1122 attributes->domain_parameters = buffer;
1123 attributes->domain_parameters_size = buflen;
1124
1125exit:
1126 mbedtls_mpi_free( &mpi );
1127 if( ret != 0 )
1128 mbedtls_free( buffer );
1129 return( mbedtls_to_psa_error( ret ) );
1130}
1131#endif /* MBEDTLS_RSA_C */
1132
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001133/** Retrieve the readily-accessible attributes of a key in a slot.
1134 *
1135 * This function does not compute attributes that are not directly
1136 * stored in the slot, such as the bit size of a transparent key.
1137 */
1138static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1139 psa_key_attributes_t *attributes )
1140{
1141 attributes->id = slot->persistent_storage_id;
1142 attributes->lifetime = slot->lifetime;
1143 attributes->policy = slot->policy;
1144 attributes->type = slot->type;
1145}
1146
1147/** Retrieve all the publicly-accessible attributes of a key.
1148 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001149psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1150 psa_key_attributes_t *attributes )
1151{
1152 psa_key_slot_t *slot;
1153 psa_status_t status;
1154
1155 psa_reset_key_attributes( attributes );
1156
Gilles Peskine28f8f302019-07-24 13:30:31 +02001157 status = psa_get_transparent_key( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001158 if( status != PSA_SUCCESS )
1159 return( status );
1160
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001161 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001162 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001163
1164 switch( slot->type )
1165 {
1166#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001167 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001168 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1169 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1170 break;
1171#endif
1172 default:
1173 /* Nothing else to do. */
1174 break;
1175 }
1176
1177 if( status != PSA_SUCCESS )
1178 psa_reset_key_attributes( attributes );
1179 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001180}
1181
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001182#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001183static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1184 unsigned char *buf, size_t size )
1185{
1186 int ret;
1187 unsigned char *c;
1188 size_t len = 0;
1189
1190 c = buf + size;
1191
1192 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1193
1194 return( (int) len );
1195}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001196#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001197
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001198static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1199 uint8_t *data,
1200 size_t data_size,
1201 size_t *data_length,
1202 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203{
Gilles Peskine5d309672019-07-12 23:47:28 +02001204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205 const psa_drv_se_t *drv;
1206 psa_drv_se_context_t *drv_context;
1207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1208
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001209 *data_length = 0;
1210
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001211 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001212 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001213
Gilles Peskine5d309672019-07-12 23:47:28 +02001214#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1215 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1216 {
1217 psa_drv_se_export_key_t method;
1218 if( drv->key_management == NULL )
1219 return( PSA_ERROR_NOT_SUPPORTED );
1220 method = ( export_public_key ?
1221 drv->key_management->p_export_public :
1222 drv->key_management->p_export );
1223 if( method == NULL )
1224 return( PSA_ERROR_NOT_SUPPORTED );
1225 return( ( *method )( drv_context,
1226 slot->data.se.slot_number,
1227 data, data_size, data_length ) );
1228 }
1229#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1230
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001231 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232 {
1233 if( slot->data.raw.bytes > data_size )
1234 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001235 if( data_size != 0 )
1236 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001237 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001238 memset( data + slot->data.raw.bytes, 0,
1239 data_size - slot->data.raw.bytes );
1240 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001241 *data_length = slot->data.raw.bytes;
1242 return( PSA_SUCCESS );
1243 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001244#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001245 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001246 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001247 psa_status_t status;
1248
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001249 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001250 if( bytes > data_size )
1251 return( PSA_ERROR_BUFFER_TOO_SMALL );
1252 status = mbedtls_to_psa_error(
1253 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1254 if( status != PSA_SUCCESS )
1255 return( status );
1256 memset( data + bytes, 0, data_size - bytes );
1257 *data_length = bytes;
1258 return( PSA_SUCCESS );
1259 }
1260#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001261 else
Moran Peker17e36e12018-05-02 12:55:20 +03001262 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001263#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001264 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001265 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001266 {
Moran Pekera998bc62018-04-16 18:16:20 +03001267 mbedtls_pk_context pk;
1268 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001269 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001270 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001271#if defined(MBEDTLS_RSA_C)
1272 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001273 pk.pk_info = &mbedtls_rsa_info;
1274 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001275#else
1276 return( PSA_ERROR_NOT_SUPPORTED );
1277#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001278 }
1279 else
1280 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001281#if defined(MBEDTLS_ECP_C)
1282 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001283 pk.pk_info = &mbedtls_eckey_info;
1284 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001285#else
1286 return( PSA_ERROR_NOT_SUPPORTED );
1287#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001288 }
Moran Pekerd7326592018-05-29 16:56:39 +03001289 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001290 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001291 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001292 }
Moran Peker17e36e12018-05-02 12:55:20 +03001293 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001294 {
Moran Peker17e36e12018-05-02 12:55:20 +03001295 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001296 }
Moran Peker60364322018-04-29 11:34:58 +03001297 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001298 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001299 /* If data_size is 0 then data may be NULL and then the
1300 * call to memset would have undefined behavior. */
1301 if( data_size != 0 )
1302 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001303 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001304 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001305 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1306 * Move the data to the beginning and erase remaining data
1307 * at the original location. */
1308 if( 2 * (size_t) ret <= data_size )
1309 {
1310 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001311 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001312 }
1313 else if( (size_t) ret < data_size )
1314 {
1315 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001316 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001317 }
Moran Pekera998bc62018-04-16 18:16:20 +03001318 *data_length = ret;
1319 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001320 }
1321 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001322#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001323 {
1324 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001325 it is valid for a special-purpose implementation to omit
1326 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001327 return( PSA_ERROR_NOT_SUPPORTED );
1328 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 }
1330}
1331
Gilles Peskinec5487a82018-12-03 18:08:14 +01001332psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001333 uint8_t *data,
1334 size_t data_size,
1335 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001336{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001337 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001338 psa_status_t status;
1339
1340 /* Set the key to empty now, so that even when there are errors, we always
1341 * set data_length to a value between 0 and data_size. On error, setting
1342 * the key to empty is a good choice because an empty key representation is
1343 * unlikely to be accepted anywhere. */
1344 *data_length = 0;
1345
1346 /* Export requires the EXPORT flag. There is an exception for public keys,
1347 * which don't require any flag, but psa_get_key_from_slot takes
1348 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001349 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001350 if( status != PSA_SUCCESS )
1351 return( status );
1352 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001353 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskinec5487a82018-12-03 18:08:14 +01001356psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001357 uint8_t *data,
1358 size_t data_size,
1359 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001360{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001361 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001362 psa_status_t status;
1363
1364 /* Set the key to empty now, so that even when there are errors, we always
1365 * set data_length to a value between 0 and data_size. On error, setting
1366 * the key to empty is a good choice because an empty key representation is
1367 * unlikely to be accepted anywhere. */
1368 *data_length = 0;
1369
1370 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001371 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001372 if( status != PSA_SUCCESS )
1373 return( status );
1374 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001375 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001376}
1377
Gilles Peskine4747d192019-04-17 15:05:45 +02001378static psa_status_t psa_set_key_policy_internal(
1379 psa_key_slot_t *slot,
1380 const psa_key_policy_t *policy )
1381{
1382 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001383 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001384 PSA_KEY_USAGE_ENCRYPT |
1385 PSA_KEY_USAGE_DECRYPT |
1386 PSA_KEY_USAGE_SIGN |
1387 PSA_KEY_USAGE_VERIFY |
1388 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1389 return( PSA_ERROR_INVALID_ARGUMENT );
1390
1391 slot->policy = *policy;
1392 return( PSA_SUCCESS );
1393}
1394
1395/** Prepare a key slot to receive key material.
1396 *
1397 * This function allocates a key slot and sets its metadata.
1398 *
1399 * If this function fails, call psa_fail_key_creation().
1400 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001401 * This function is intended to be used as follows:
1402 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1403 * it with the specified attributes, and assign it a handle.
1404 * -# Populate the slot with the key material.
1405 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1406 * In case of failure at any step, stop the sequence and call
1407 * psa_fail_key_creation().
1408 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001409 * \param[in] attributes Key attributes for the new key.
1410 * \param[out] handle On success, a handle for the allocated slot.
1411 * \param[out] p_slot On success, a pointer to the prepared slot.
1412 * \param[out] p_drv On any return, the driver for the key, if any.
1413 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001414 *
1415 * \retval #PSA_SUCCESS
1416 * The key slot is ready to receive key material.
1417 * \return If this function fails, the key slot is an invalid state.
1418 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001419 */
1420static psa_status_t psa_start_key_creation(
1421 const psa_key_attributes_t *attributes,
1422 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001423 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001424 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001425{
1426 psa_status_t status;
1427 psa_key_slot_t *slot;
1428
Gilles Peskine011e4282019-06-26 18:34:38 +02001429 *p_drv = NULL;
1430
Gilles Peskine267c6562019-05-27 19:01:54 +02001431 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001432 if( status != PSA_SUCCESS )
1433 return( status );
1434 slot = *p_slot;
1435
1436 status = psa_set_key_policy_internal( slot, &attributes->policy );
1437 if( status != PSA_SUCCESS )
1438 return( status );
1439 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001440
Gilles Peskine4747d192019-04-17 15:05:45 +02001441 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001442 {
1443 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001444 attributes->id,
1445 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001446 if( status != PSA_SUCCESS )
1447 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001448 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001449 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001450 slot->type = attributes->type;
1451
Gilles Peskinecbaff462019-07-12 23:46:04 +02001452#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinefc762652019-07-22 19:30:34 +02001453 /* Find a slot number for the new key. Save the slot number in
1454 * persistent storage, but do not yet save the driver's persistent
1455 * state, so that if the power fails during the key creation process,
1456 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001457 if( *p_drv != NULL )
1458 {
1459 status = psa_find_se_slot_for_key( attributes, *p_drv,
1460 &slot->data.se.slot_number );
1461 if( status != PSA_SUCCESS )
1462 return( status );
1463 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001464 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1465 psa_crypto_transaction.key.lifetime = slot->lifetime;
1466 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1467 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1468 status = psa_crypto_save_transaction( );
1469 if( status != PSA_SUCCESS )
1470 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001471#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1472
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 return( status );
1474}
1475
1476/** Finalize the creation of a key once its key material has been set.
1477 *
1478 * This entails writing the key to persistent storage.
1479 *
1480 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001481 * See the documentation of psa_start_key_creation() for the intended use
1482 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001483 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001484 * \param[in,out] slot Pointer to the slot with key material.
1485 * \param[in] driver The secure element driver for the key,
1486 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001487 *
1488 * \retval #PSA_SUCCESS
1489 * The key was successfully created. The handle is now valid.
1490 * \return If this function fails, the key slot is an invalid state.
1491 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001492 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001493static psa_status_t psa_finish_key_creation(
1494 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001495 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001496{
1497 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001498 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001499 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001500
1501#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001502 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001503 {
1504 uint8_t *buffer = NULL;
1505 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001506 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001507
Gilles Peskine1df83d42019-07-23 16:13:14 +02001508#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1509 if( driver != NULL )
1510 {
1511 buffer = (uint8_t*) &slot->data.se.slot_number;
1512 length = sizeof( slot->data.se.slot_number );
1513 }
1514 else
1515#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1516 {
1517 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1518 psa_get_key_slot_bits( slot ) );
1519 buffer = mbedtls_calloc( 1, buffer_size );
1520 if( buffer == NULL && buffer_size != 0 )
1521 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1522 status = psa_internal_export_key( slot,
1523 buffer, buffer_size, &length,
1524 0 );
1525 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001526
1527 if( status == PSA_SUCCESS )
1528 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1530 psa_get_key_slot_attributes( slot, &attributes );
1531 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001532 }
1533
Gilles Peskine1df83d42019-07-23 16:13:14 +02001534#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1535 if( driver == NULL )
1536#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1537 {
1538 if( buffer_size != 0 )
1539 mbedtls_platform_zeroize( buffer, buffer_size );
1540 mbedtls_free( buffer );
1541 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 }
1543#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1544
Gilles Peskinecbaff462019-07-12 23:46:04 +02001545#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1546 if( driver != NULL )
1547 {
1548 status = psa_save_se_persistent_data( driver );
1549 if( status != PSA_SUCCESS )
1550 {
1551 psa_destroy_persistent_key( slot->persistent_storage_id );
1552 return( status );
1553 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001554 status = psa_crypto_stop_transaction( );
1555 if( status != PSA_SUCCESS )
1556 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001557 }
1558#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1559
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 return( status );
1561}
1562
1563/** Abort the creation of a key.
1564 *
1565 * You may call this function after calling psa_start_key_creation(),
1566 * or after psa_finish_key_creation() fails. In other circumstances, this
1567 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001568 * See the documentation of psa_start_key_creation() for the intended use
1569 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001570 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001571 * \param[in,out] slot Pointer to the slot with key material.
1572 * \param[in] driver The secure element driver for the key,
1573 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001575static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001576 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001577{
Gilles Peskine011e4282019-06-26 18:34:38 +02001578 (void) driver;
1579
Gilles Peskine4747d192019-04-17 15:05:45 +02001580 if( slot == NULL )
1581 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001582
1583#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1584 /* TOnogrepDO: If the key has already been created in the secure
1585 * element, and the failure happened later (when saving metadata
1586 * to internal storage), we need to destroy the key in the secure
1587 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001588
1589 /* Abort the ongoing transaction if any. We already did what it
1590 * takes to undo any partial creation. All that's left is to update
1591 * the transaction data itself. */
1592 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001593#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1594
Gilles Peskine4747d192019-04-17 15:05:45 +02001595 psa_wipe_key_slot( slot );
1596}
1597
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001598static psa_status_t psa_check_key_slot_attributes(
1599 const psa_key_slot_t *slot,
1600 const psa_key_attributes_t *attributes )
1601{
1602 if( attributes->type != 0 )
1603 {
1604 if( attributes->type != slot->type )
1605 return( PSA_ERROR_INVALID_ARGUMENT );
1606 }
1607
1608 if( attributes->domain_parameters_size != 0 )
1609 {
1610#if defined(MBEDTLS_RSA_C)
1611 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1612 {
1613 mbedtls_mpi actual, required;
1614 int ret;
1615 mbedtls_mpi_init( &actual );
1616 mbedtls_mpi_init( &required );
1617 ret = mbedtls_rsa_export( slot->data.rsa,
1618 NULL, NULL, NULL, NULL, &actual );
1619 if( ret != 0 )
1620 goto rsa_exit;
1621 ret = mbedtls_mpi_read_binary( &required,
1622 attributes->domain_parameters,
1623 attributes->domain_parameters_size );
1624 if( ret != 0 )
1625 goto rsa_exit;
1626 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1627 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1628 rsa_exit:
1629 mbedtls_mpi_free( &actual );
1630 mbedtls_mpi_free( &required );
1631 if( ret != 0)
1632 return( mbedtls_to_psa_error( ret ) );
1633 }
1634 else
1635#endif
1636 {
1637 return( PSA_ERROR_INVALID_ARGUMENT );
1638 }
1639 }
1640
1641 if( attributes->bits != 0 )
1642 {
1643 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1644 return( PSA_ERROR_INVALID_ARGUMENT );
1645 }
1646
1647 return( PSA_SUCCESS );
1648}
1649
Gilles Peskine4747d192019-04-17 15:05:45 +02001650psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001652 size_t data_length,
1653 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001654{
1655 psa_status_t status;
1656 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001657 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001658
Gilles Peskine011e4282019-06-26 18:34:38 +02001659 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001660 if( status != PSA_SUCCESS )
1661 goto exit;
1662
Gilles Peskine5d309672019-07-12 23:47:28 +02001663#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1664 if( driver != NULL )
1665 {
1666 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1667 if( drv->key_management == NULL ||
1668 drv->key_management->p_import == NULL )
1669 {
1670 status = PSA_ERROR_NOT_SUPPORTED;
1671 goto exit;
1672 }
1673 status = drv->key_management->p_import(
1674 psa_get_se_driver_context( driver ),
1675 slot->data.se.slot_number,
1676 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1677 data, data_length );
1678 /* TOnogrepDO: psa_check_key_slot_attributes? */
1679 }
1680 else
1681#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1682 {
1683 status = psa_import_key_into_slot( slot, data, data_length );
1684 if( status != PSA_SUCCESS )
1685 goto exit;
1686 status = psa_check_key_slot_attributes( slot, attributes );
1687 if( status != PSA_SUCCESS )
1688 goto exit;
1689 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001690
Gilles Peskine011e4282019-06-26 18:34:38 +02001691 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001692exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 if( status != PSA_SUCCESS )
1694 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 *handle = 0;
1697 }
1698 return( status );
1699}
1700
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001701static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001702 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001703{
1704 psa_status_t status;
1705 uint8_t *buffer = NULL;
1706 size_t buffer_size = 0;
1707 size_t length;
1708
1709 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001710 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001711 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001712 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001713 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001714 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1715 if( status != PSA_SUCCESS )
1716 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001717 target->type = source->type;
1718 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001719
1720exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001721 if( buffer_size != 0 )
1722 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001723 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001724 return( status );
1725}
1726
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001727psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1728 const psa_key_attributes_t *specified_attributes,
1729 psa_key_handle_t *target_handle )
1730{
1731 psa_status_t status;
1732 psa_key_slot_t *source_slot = NULL;
1733 psa_key_slot_t *target_slot = NULL;
1734 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001735 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001736
Gilles Peskine28f8f302019-07-24 13:30:31 +02001737 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001738 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001739 if( status != PSA_SUCCESS )
1740 goto exit;
1741
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001742 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1743 if( status != PSA_SUCCESS )
1744 goto exit;
1745
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001746 status = psa_restrict_key_policy( &actual_attributes.policy,
1747 &source_slot->policy );
1748 if( status != PSA_SUCCESS )
1749 goto exit;
1750
1751 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001753 if( status != PSA_SUCCESS )
1754 goto exit;
1755
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1757 if( driver != NULL )
1758 {
1759 /* Copying to a secure element is not implemented yet. */
1760 status = PSA_ERROR_NOT_SUPPORTED;
1761 goto exit;
1762 }
1763#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1764
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001765 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001766 if( status != PSA_SUCCESS )
1767 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001768
Gilles Peskine011e4282019-06-26 18:34:38 +02001769 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001770exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001771 if( status != PSA_SUCCESS )
1772 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001774 *target_handle = 0;
1775 }
1776 return( status );
1777}
1778
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001779
1780
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001781/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001782/* Message digests */
1783/****************************************************************/
1784
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001785static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001786{
1787 switch( alg )
1788 {
1789#if defined(MBEDTLS_MD2_C)
1790 case PSA_ALG_MD2:
1791 return( &mbedtls_md2_info );
1792#endif
1793#if defined(MBEDTLS_MD4_C)
1794 case PSA_ALG_MD4:
1795 return( &mbedtls_md4_info );
1796#endif
1797#if defined(MBEDTLS_MD5_C)
1798 case PSA_ALG_MD5:
1799 return( &mbedtls_md5_info );
1800#endif
1801#if defined(MBEDTLS_RIPEMD160_C)
1802 case PSA_ALG_RIPEMD160:
1803 return( &mbedtls_ripemd160_info );
1804#endif
1805#if defined(MBEDTLS_SHA1_C)
1806 case PSA_ALG_SHA_1:
1807 return( &mbedtls_sha1_info );
1808#endif
1809#if defined(MBEDTLS_SHA256_C)
1810 case PSA_ALG_SHA_224:
1811 return( &mbedtls_sha224_info );
1812 case PSA_ALG_SHA_256:
1813 return( &mbedtls_sha256_info );
1814#endif
1815#if defined(MBEDTLS_SHA512_C)
1816 case PSA_ALG_SHA_384:
1817 return( &mbedtls_sha384_info );
1818 case PSA_ALG_SHA_512:
1819 return( &mbedtls_sha512_info );
1820#endif
1821 default:
1822 return( NULL );
1823 }
1824}
1825
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001826psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1827{
1828 switch( operation->alg )
1829 {
Gilles Peskine81736312018-06-26 15:04:31 +02001830 case 0:
1831 /* The object has (apparently) been initialized but it is not
1832 * in use. It's ok to call abort on such an object, and there's
1833 * nothing to do. */
1834 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001835#if defined(MBEDTLS_MD2_C)
1836 case PSA_ALG_MD2:
1837 mbedtls_md2_free( &operation->ctx.md2 );
1838 break;
1839#endif
1840#if defined(MBEDTLS_MD4_C)
1841 case PSA_ALG_MD4:
1842 mbedtls_md4_free( &operation->ctx.md4 );
1843 break;
1844#endif
1845#if defined(MBEDTLS_MD5_C)
1846 case PSA_ALG_MD5:
1847 mbedtls_md5_free( &operation->ctx.md5 );
1848 break;
1849#endif
1850#if defined(MBEDTLS_RIPEMD160_C)
1851 case PSA_ALG_RIPEMD160:
1852 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1853 break;
1854#endif
1855#if defined(MBEDTLS_SHA1_C)
1856 case PSA_ALG_SHA_1:
1857 mbedtls_sha1_free( &operation->ctx.sha1 );
1858 break;
1859#endif
1860#if defined(MBEDTLS_SHA256_C)
1861 case PSA_ALG_SHA_224:
1862 case PSA_ALG_SHA_256:
1863 mbedtls_sha256_free( &operation->ctx.sha256 );
1864 break;
1865#endif
1866#if defined(MBEDTLS_SHA512_C)
1867 case PSA_ALG_SHA_384:
1868 case PSA_ALG_SHA_512:
1869 mbedtls_sha512_free( &operation->ctx.sha512 );
1870 break;
1871#endif
1872 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001873 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001874 }
1875 operation->alg = 0;
1876 return( PSA_SUCCESS );
1877}
1878
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001879psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001880 psa_algorithm_t alg )
1881{
1882 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001883
1884 /* A context must be freshly initialized before it can be set up. */
1885 if( operation->alg != 0 )
1886 {
1887 return( PSA_ERROR_BAD_STATE );
1888 }
1889
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001890 switch( alg )
1891 {
1892#if defined(MBEDTLS_MD2_C)
1893 case PSA_ALG_MD2:
1894 mbedtls_md2_init( &operation->ctx.md2 );
1895 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1896 break;
1897#endif
1898#if defined(MBEDTLS_MD4_C)
1899 case PSA_ALG_MD4:
1900 mbedtls_md4_init( &operation->ctx.md4 );
1901 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1902 break;
1903#endif
1904#if defined(MBEDTLS_MD5_C)
1905 case PSA_ALG_MD5:
1906 mbedtls_md5_init( &operation->ctx.md5 );
1907 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1908 break;
1909#endif
1910#if defined(MBEDTLS_RIPEMD160_C)
1911 case PSA_ALG_RIPEMD160:
1912 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1913 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1914 break;
1915#endif
1916#if defined(MBEDTLS_SHA1_C)
1917 case PSA_ALG_SHA_1:
1918 mbedtls_sha1_init( &operation->ctx.sha1 );
1919 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1920 break;
1921#endif
1922#if defined(MBEDTLS_SHA256_C)
1923 case PSA_ALG_SHA_224:
1924 mbedtls_sha256_init( &operation->ctx.sha256 );
1925 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1926 break;
1927 case PSA_ALG_SHA_256:
1928 mbedtls_sha256_init( &operation->ctx.sha256 );
1929 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1930 break;
1931#endif
1932#if defined(MBEDTLS_SHA512_C)
1933 case PSA_ALG_SHA_384:
1934 mbedtls_sha512_init( &operation->ctx.sha512 );
1935 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1936 break;
1937 case PSA_ALG_SHA_512:
1938 mbedtls_sha512_init( &operation->ctx.sha512 );
1939 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1940 break;
1941#endif
1942 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001943 return( PSA_ALG_IS_HASH( alg ) ?
1944 PSA_ERROR_NOT_SUPPORTED :
1945 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001946 }
1947 if( ret == 0 )
1948 operation->alg = alg;
1949 else
1950 psa_hash_abort( operation );
1951 return( mbedtls_to_psa_error( ret ) );
1952}
1953
1954psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1955 const uint8_t *input,
1956 size_t input_length )
1957{
1958 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001959
1960 /* Don't require hash implementations to behave correctly on a
1961 * zero-length input, which may have an invalid pointer. */
1962 if( input_length == 0 )
1963 return( PSA_SUCCESS );
1964
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001965 switch( operation->alg )
1966 {
1967#if defined(MBEDTLS_MD2_C)
1968 case PSA_ALG_MD2:
1969 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1970 input, input_length );
1971 break;
1972#endif
1973#if defined(MBEDTLS_MD4_C)
1974 case PSA_ALG_MD4:
1975 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1976 input, input_length );
1977 break;
1978#endif
1979#if defined(MBEDTLS_MD5_C)
1980 case PSA_ALG_MD5:
1981 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1982 input, input_length );
1983 break;
1984#endif
1985#if defined(MBEDTLS_RIPEMD160_C)
1986 case PSA_ALG_RIPEMD160:
1987 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1988 input, input_length );
1989 break;
1990#endif
1991#if defined(MBEDTLS_SHA1_C)
1992 case PSA_ALG_SHA_1:
1993 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1994 input, input_length );
1995 break;
1996#endif
1997#if defined(MBEDTLS_SHA256_C)
1998 case PSA_ALG_SHA_224:
1999 case PSA_ALG_SHA_256:
2000 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2001 input, input_length );
2002 break;
2003#endif
2004#if defined(MBEDTLS_SHA512_C)
2005 case PSA_ALG_SHA_384:
2006 case PSA_ALG_SHA_512:
2007 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2008 input, input_length );
2009 break;
2010#endif
2011 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002012 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002013 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002014
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002015 if( ret != 0 )
2016 psa_hash_abort( operation );
2017 return( mbedtls_to_psa_error( ret ) );
2018}
2019
2020psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2021 uint8_t *hash,
2022 size_t hash_size,
2023 size_t *hash_length )
2024{
itayzafrir40835d42018-08-02 13:14:17 +03002025 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002026 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002027 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002028
2029 /* Fill the output buffer with something that isn't a valid hash
2030 * (barring an attack on the hash and deliberately-crafted input),
2031 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002032 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002033 /* If hash_size is 0 then hash may be NULL and then the
2034 * call to memset would have undefined behavior. */
2035 if( hash_size != 0 )
2036 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002037
2038 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002039 {
2040 status = PSA_ERROR_BUFFER_TOO_SMALL;
2041 goto exit;
2042 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002043
2044 switch( operation->alg )
2045 {
2046#if defined(MBEDTLS_MD2_C)
2047 case PSA_ALG_MD2:
2048 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2049 break;
2050#endif
2051#if defined(MBEDTLS_MD4_C)
2052 case PSA_ALG_MD4:
2053 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2054 break;
2055#endif
2056#if defined(MBEDTLS_MD5_C)
2057 case PSA_ALG_MD5:
2058 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2059 break;
2060#endif
2061#if defined(MBEDTLS_RIPEMD160_C)
2062 case PSA_ALG_RIPEMD160:
2063 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2064 break;
2065#endif
2066#if defined(MBEDTLS_SHA1_C)
2067 case PSA_ALG_SHA_1:
2068 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2069 break;
2070#endif
2071#if defined(MBEDTLS_SHA256_C)
2072 case PSA_ALG_SHA_224:
2073 case PSA_ALG_SHA_256:
2074 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2075 break;
2076#endif
2077#if defined(MBEDTLS_SHA512_C)
2078 case PSA_ALG_SHA_384:
2079 case PSA_ALG_SHA_512:
2080 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2081 break;
2082#endif
2083 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002084 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002085 }
itayzafrir40835d42018-08-02 13:14:17 +03002086 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002087
itayzafrir40835d42018-08-02 13:14:17 +03002088exit:
2089 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002090 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002091 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002092 return( psa_hash_abort( operation ) );
2093 }
2094 else
2095 {
2096 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002097 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002098 }
2099}
2100
Gilles Peskine2d277862018-06-18 15:41:12 +02002101psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2102 const uint8_t *hash,
2103 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002104{
2105 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2106 size_t actual_hash_length;
2107 psa_status_t status = psa_hash_finish( operation,
2108 actual_hash, sizeof( actual_hash ),
2109 &actual_hash_length );
2110 if( status != PSA_SUCCESS )
2111 return( status );
2112 if( actual_hash_length != hash_length )
2113 return( PSA_ERROR_INVALID_SIGNATURE );
2114 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2115 return( PSA_ERROR_INVALID_SIGNATURE );
2116 return( PSA_SUCCESS );
2117}
2118
Gilles Peskineeb35d782019-01-22 17:56:16 +01002119psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2120 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002121{
2122 if( target_operation->alg != 0 )
2123 return( PSA_ERROR_BAD_STATE );
2124
2125 switch( source_operation->alg )
2126 {
2127 case 0:
2128 return( PSA_ERROR_BAD_STATE );
2129#if defined(MBEDTLS_MD2_C)
2130 case PSA_ALG_MD2:
2131 mbedtls_md2_clone( &target_operation->ctx.md2,
2132 &source_operation->ctx.md2 );
2133 break;
2134#endif
2135#if defined(MBEDTLS_MD4_C)
2136 case PSA_ALG_MD4:
2137 mbedtls_md4_clone( &target_operation->ctx.md4,
2138 &source_operation->ctx.md4 );
2139 break;
2140#endif
2141#if defined(MBEDTLS_MD5_C)
2142 case PSA_ALG_MD5:
2143 mbedtls_md5_clone( &target_operation->ctx.md5,
2144 &source_operation->ctx.md5 );
2145 break;
2146#endif
2147#if defined(MBEDTLS_RIPEMD160_C)
2148 case PSA_ALG_RIPEMD160:
2149 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2150 &source_operation->ctx.ripemd160 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_SHA1_C)
2154 case PSA_ALG_SHA_1:
2155 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2156 &source_operation->ctx.sha1 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_SHA256_C)
2160 case PSA_ALG_SHA_224:
2161 case PSA_ALG_SHA_256:
2162 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2163 &source_operation->ctx.sha256 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_SHA512_C)
2167 case PSA_ALG_SHA_384:
2168 case PSA_ALG_SHA_512:
2169 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2170 &source_operation->ctx.sha512 );
2171 break;
2172#endif
2173 default:
2174 return( PSA_ERROR_NOT_SUPPORTED );
2175 }
2176
2177 target_operation->alg = source_operation->alg;
2178 return( PSA_SUCCESS );
2179}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180
2181
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183/* MAC */
2184/****************************************************************/
2185
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002186static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002187 psa_algorithm_t alg,
2188 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002189 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002190 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002193 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002194
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002195 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002196 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002197
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2199 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002200 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002201 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002202 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002203 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204 mode = MBEDTLS_MODE_STREAM;
2205 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206 case PSA_ALG_CTR:
2207 mode = MBEDTLS_MODE_CTR;
2208 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002209 case PSA_ALG_CFB:
2210 mode = MBEDTLS_MODE_CFB;
2211 break;
2212 case PSA_ALG_OFB:
2213 mode = MBEDTLS_MODE_OFB;
2214 break;
2215 case PSA_ALG_CBC_NO_PADDING:
2216 mode = MBEDTLS_MODE_CBC;
2217 break;
2218 case PSA_ALG_CBC_PKCS7:
2219 mode = MBEDTLS_MODE_CBC;
2220 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002221 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222 mode = MBEDTLS_MODE_CCM;
2223 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002224 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002225 mode = MBEDTLS_MODE_GCM;
2226 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002227 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2228 mode = MBEDTLS_MODE_CHACHAPOLY;
2229 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002230 default:
2231 return( NULL );
2232 }
2233 }
2234 else if( alg == PSA_ALG_CMAC )
2235 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002236 else
2237 return( NULL );
2238
2239 switch( key_type )
2240 {
2241 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002242 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243 break;
2244 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002245 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2246 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002247 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002248 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002249 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002250 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002251 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2252 * but two-key Triple-DES is functionally three-key Triple-DES
2253 * with K1=K3, so that's how we present it to mbedtls. */
2254 if( key_bits == 128 )
2255 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002256 break;
2257 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002258 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259 break;
2260 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002261 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002262 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002263 case PSA_KEY_TYPE_CHACHA20:
2264 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2265 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002266 default:
2267 return( NULL );
2268 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002269 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002270 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271
Jaeden Amero23bbb752018-06-26 14:16:54 +01002272 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2273 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002274}
2275
Gilles Peskinea05219c2018-11-16 16:02:56 +01002276#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002277static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002278{
Gilles Peskine2d277862018-06-18 15:41:12 +02002279 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002280 {
2281 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002282 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002283 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002284 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002285 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002286 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002287 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002288 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002289 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002290 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002291 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002292 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002293 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002294 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002295 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002296 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002297 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002298 return( 128 );
2299 default:
2300 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002301 }
2302}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002303#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002304
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305/* Initialize the MAC operation structure. Once this function has been
2306 * called, psa_mac_abort can run and will do the right thing. */
2307static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2308 psa_algorithm_t alg )
2309{
2310 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2311
2312 operation->alg = alg;
2313 operation->key_set = 0;
2314 operation->iv_set = 0;
2315 operation->iv_required = 0;
2316 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002317 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002318
2319#if defined(MBEDTLS_CMAC_C)
2320 if( alg == PSA_ALG_CMAC )
2321 {
2322 operation->iv_required = 0;
2323 mbedtls_cipher_init( &operation->ctx.cmac );
2324 status = PSA_SUCCESS;
2325 }
2326 else
2327#endif /* MBEDTLS_CMAC_C */
2328#if defined(MBEDTLS_MD_C)
2329 if( PSA_ALG_IS_HMAC( operation->alg ) )
2330 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002331 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2332 operation->ctx.hmac.hash_ctx.alg = 0;
2333 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334 }
2335 else
2336#endif /* MBEDTLS_MD_C */
2337 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002338 if( ! PSA_ALG_IS_MAC( alg ) )
2339 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340 }
2341
2342 if( status != PSA_SUCCESS )
2343 memset( operation, 0, sizeof( *operation ) );
2344 return( status );
2345}
2346
Gilles Peskine01126fa2018-07-12 17:04:55 +02002347#if defined(MBEDTLS_MD_C)
2348static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2349{
Gilles Peskine3f108122018-12-07 18:14:53 +01002350 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002351 return( psa_hash_abort( &hmac->hash_ctx ) );
2352}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002353
Janos Follath999f6482019-06-11 12:04:10 +01002354#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002355static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2356{
2357 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2358 memset( hmac, 0, sizeof( *hmac ) );
2359}
Janos Follath999f6482019-06-11 12:04:10 +01002360#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002361#endif /* MBEDTLS_MD_C */
2362
Gilles Peskine8c9def32018-02-08 10:02:12 +01002363psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2364{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002365 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002366 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002367 /* The object has (apparently) been initialized but it is not
2368 * in use. It's ok to call abort on such an object, and there's
2369 * nothing to do. */
2370 return( PSA_SUCCESS );
2371 }
2372 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002374 if( operation->alg == PSA_ALG_CMAC )
2375 {
2376 mbedtls_cipher_free( &operation->ctx.cmac );
2377 }
2378 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002379#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002381 if( PSA_ALG_IS_HMAC( operation->alg ) )
2382 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002383 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002384 }
2385 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002387 {
2388 /* Sanity check (shouldn't happen: operation->alg should
2389 * always have been initialized to a valid value). */
2390 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391 }
Moran Peker41deec42018-04-04 15:43:05 +03002392
Gilles Peskine8c9def32018-02-08 10:02:12 +01002393 operation->alg = 0;
2394 operation->key_set = 0;
2395 operation->iv_set = 0;
2396 operation->iv_required = 0;
2397 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002398 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002399
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002401
2402bad_state:
2403 /* If abort is called on an uninitialized object, we can't trust
2404 * anything. Wipe the object in case it contains confidential data.
2405 * This may result in a memory leak if a pointer gets overwritten,
2406 * but it's too late to do anything about this. */
2407 memset( operation, 0, sizeof( *operation ) );
2408 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409}
2410
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002411#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002412static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002413 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002414 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002415 const mbedtls_cipher_info_t *cipher_info )
2416{
2417 int ret;
2418
2419 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002420
2421 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2422 if( ret != 0 )
2423 return( ret );
2424
2425 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2426 slot->data.raw.data,
2427 key_bits );
2428 return( ret );
2429}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002430#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002431
Gilles Peskine248051a2018-06-20 16:09:38 +02002432#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002433static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2434 const uint8_t *key,
2435 size_t key_length,
2436 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002437{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002438 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002439 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002440 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002441 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002442 psa_status_t status;
2443
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002444 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2445 * overflow below. This should never trigger if the hash algorithm
2446 * is implemented correctly. */
2447 /* The size checks against the ipad and opad buffers cannot be written
2448 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2449 * because that triggers -Wlogical-op on GCC 7.3. */
2450 if( block_size > sizeof( ipad ) )
2451 return( PSA_ERROR_NOT_SUPPORTED );
2452 if( block_size > sizeof( hmac->opad ) )
2453 return( PSA_ERROR_NOT_SUPPORTED );
2454 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002455 return( PSA_ERROR_NOT_SUPPORTED );
2456
Gilles Peskined223b522018-06-11 18:12:58 +02002457 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002458 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002459 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002460 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002461 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002462 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002463 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002464 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002465 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002466 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002467 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002468 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002469 }
Gilles Peskine96889972018-07-12 17:07:03 +02002470 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2471 * but it is permitted. It is common when HMAC is used in HKDF, for
2472 * example. Don't call `memcpy` in the 0-length because `key` could be
2473 * an invalid pointer which would make the behavior undefined. */
2474 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002475 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002476
Gilles Peskined223b522018-06-11 18:12:58 +02002477 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2478 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002479 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002480 ipad[i] ^= 0x36;
2481 memset( ipad + key_length, 0x36, block_size - key_length );
2482
2483 /* Copy the key material from ipad to opad, flipping the requisite bits,
2484 * and filling the rest of opad with the requisite constant. */
2485 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002486 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2487 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002488
Gilles Peskine01126fa2018-07-12 17:04:55 +02002489 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002490 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002491 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002492
Gilles Peskine01126fa2018-07-12 17:04:55 +02002493 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002494
2495cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002496 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002497
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498 return( status );
2499}
Gilles Peskine248051a2018-06-20 16:09:38 +02002500#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002501
Gilles Peskine89167cb2018-07-08 20:12:23 +02002502static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002503 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002504 psa_algorithm_t alg,
2505 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002508 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002510 psa_key_usage_t usage =
2511 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002512 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002513 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002515 /* A context must be freshly initialized before it can be set up. */
2516 if( operation->alg != 0 )
2517 {
2518 return( PSA_ERROR_BAD_STATE );
2519 }
2520
Gilles Peskined911eb72018-08-14 15:18:45 +02002521 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002522 if( status != PSA_SUCCESS )
2523 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002524 if( is_sign )
2525 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526
Gilles Peskine28f8f302019-07-24 13:30:31 +02002527 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002528 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002529 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002530 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002531
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002533 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002534 {
2535 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002536 mbedtls_cipher_info_from_psa( full_length_alg,
2537 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002538 int ret;
2539 if( cipher_info == NULL )
2540 {
2541 status = PSA_ERROR_NOT_SUPPORTED;
2542 goto exit;
2543 }
2544 operation->mac_size = cipher_info->block_size;
2545 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2546 status = mbedtls_to_psa_error( ret );
2547 }
2548 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002551 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002552 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002553 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002554 if( hash_alg == 0 )
2555 {
2556 status = PSA_ERROR_NOT_SUPPORTED;
2557 goto exit;
2558 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002559
2560 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2561 /* Sanity check. This shouldn't fail on a valid configuration. */
2562 if( operation->mac_size == 0 ||
2563 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2564 {
2565 status = PSA_ERROR_NOT_SUPPORTED;
2566 goto exit;
2567 }
2568
Gilles Peskine01126fa2018-07-12 17:04:55 +02002569 if( slot->type != PSA_KEY_TYPE_HMAC )
2570 {
2571 status = PSA_ERROR_INVALID_ARGUMENT;
2572 goto exit;
2573 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002574
Gilles Peskine01126fa2018-07-12 17:04:55 +02002575 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2576 slot->data.raw.data,
2577 slot->data.raw.bytes,
2578 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002579 }
2580 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002582 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002583 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002584 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002586
Gilles Peskined911eb72018-08-14 15:18:45 +02002587 if( truncated == 0 )
2588 {
2589 /* The "normal" case: untruncated algorithm. Nothing to do. */
2590 }
2591 else if( truncated < 4 )
2592 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002593 /* A very short MAC is too short for security since it can be
2594 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2595 * so we make this our minimum, even though 32 bits is still
2596 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002597 status = PSA_ERROR_NOT_SUPPORTED;
2598 }
2599 else if( truncated > operation->mac_size )
2600 {
2601 /* It's impossible to "truncate" to a larger length. */
2602 status = PSA_ERROR_INVALID_ARGUMENT;
2603 }
2604 else
2605 operation->mac_size = truncated;
2606
Gilles Peskinefbfac682018-07-08 20:51:54 +02002607exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002608 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002610 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002612 else
2613 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002614 operation->key_set = 1;
2615 }
2616 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617}
2618
Gilles Peskine89167cb2018-07-08 20:12:23 +02002619psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002620 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002621 psa_algorithm_t alg )
2622{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002623 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002624}
2625
2626psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002627 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002628 psa_algorithm_t alg )
2629{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002630 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002631}
2632
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2634 const uint8_t *input,
2635 size_t input_length )
2636{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002637 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002639 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002641 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642 operation->has_input = 1;
2643
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002645 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002646 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002647 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2648 input, input_length );
2649 status = mbedtls_to_psa_error( ret );
2650 }
2651 else
2652#endif /* MBEDTLS_CMAC_C */
2653#if defined(MBEDTLS_MD_C)
2654 if( PSA_ALG_IS_HMAC( operation->alg ) )
2655 {
2656 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2657 input_length );
2658 }
2659 else
2660#endif /* MBEDTLS_MD_C */
2661 {
2662 /* This shouldn't happen if `operation` was initialized by
2663 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002664 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002665 }
2666
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667 if( status != PSA_SUCCESS )
2668 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002669 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670}
2671
Gilles Peskine01126fa2018-07-12 17:04:55 +02002672#if defined(MBEDTLS_MD_C)
2673static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2674 uint8_t *mac,
2675 size_t mac_size )
2676{
2677 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2678 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2679 size_t hash_size = 0;
2680 size_t block_size = psa_get_hash_block_size( hash_alg );
2681 psa_status_t status;
2682
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2684 if( status != PSA_SUCCESS )
2685 return( status );
2686 /* From here on, tmp needs to be wiped. */
2687
2688 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2689 if( status != PSA_SUCCESS )
2690 goto exit;
2691
2692 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2693 if( status != PSA_SUCCESS )
2694 goto exit;
2695
2696 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2697 if( status != PSA_SUCCESS )
2698 goto exit;
2699
Gilles Peskined911eb72018-08-14 15:18:45 +02002700 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2701 if( status != PSA_SUCCESS )
2702 goto exit;
2703
2704 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002705
2706exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002707 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002708 return( status );
2709}
2710#endif /* MBEDTLS_MD_C */
2711
mohammad16036df908f2018-04-02 08:34:15 -07002712static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002713 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002714 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002716 if( ! operation->key_set )
2717 return( PSA_ERROR_BAD_STATE );
2718 if( operation->iv_required && ! operation->iv_set )
2719 return( PSA_ERROR_BAD_STATE );
2720
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721 if( mac_size < operation->mac_size )
2722 return( PSA_ERROR_BUFFER_TOO_SMALL );
2723
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002725 if( operation->alg == PSA_ALG_CMAC )
2726 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002727 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2728 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2729 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002730 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002731 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002732 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002734 else
2735#endif /* MBEDTLS_CMAC_C */
2736#if defined(MBEDTLS_MD_C)
2737 if( PSA_ALG_IS_HMAC( operation->alg ) )
2738 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002739 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002740 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002741 }
2742 else
2743#endif /* MBEDTLS_MD_C */
2744 {
2745 /* This shouldn't happen if `operation` was initialized by
2746 * a setup function. */
2747 return( PSA_ERROR_BAD_STATE );
2748 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749}
2750
Gilles Peskineacd4be32018-07-08 19:56:25 +02002751psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2752 uint8_t *mac,
2753 size_t mac_size,
2754 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002755{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002756 psa_status_t status;
2757
Jaeden Amero252ef282019-02-15 14:05:35 +00002758 if( operation->alg == 0 )
2759 {
2760 return( PSA_ERROR_BAD_STATE );
2761 }
2762
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002763 /* Fill the output buffer with something that isn't a valid mac
2764 * (barring an attack on the mac and deliberately-crafted input),
2765 * in case the caller doesn't check the return status properly. */
2766 *mac_length = mac_size;
2767 /* If mac_size is 0 then mac may be NULL and then the
2768 * call to memset would have undefined behavior. */
2769 if( mac_size != 0 )
2770 memset( mac, '!', mac_size );
2771
Gilles Peskine89167cb2018-07-08 20:12:23 +02002772 if( ! operation->is_sign )
2773 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002774 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002775 }
mohammad16036df908f2018-04-02 08:34:15 -07002776
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002777 status = psa_mac_finish_internal( operation, mac, mac_size );
2778
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002779 if( status == PSA_SUCCESS )
2780 {
2781 status = psa_mac_abort( operation );
2782 if( status == PSA_SUCCESS )
2783 *mac_length = operation->mac_size;
2784 else
2785 memset( mac, '!', mac_size );
2786 }
2787 else
2788 psa_mac_abort( operation );
2789 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002790}
2791
Gilles Peskineacd4be32018-07-08 19:56:25 +02002792psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2793 const uint8_t *mac,
2794 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795{
Gilles Peskine828ed142018-06-18 23:25:51 +02002796 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002797 psa_status_t status;
2798
Jaeden Amero252ef282019-02-15 14:05:35 +00002799 if( operation->alg == 0 )
2800 {
2801 return( PSA_ERROR_BAD_STATE );
2802 }
2803
Gilles Peskine89167cb2018-07-08 20:12:23 +02002804 if( operation->is_sign )
2805 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002806 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002807 }
2808 if( operation->mac_size != mac_length )
2809 {
2810 status = PSA_ERROR_INVALID_SIGNATURE;
2811 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002812 }
mohammad16036df908f2018-04-02 08:34:15 -07002813
2814 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002815 actual_mac, sizeof( actual_mac ) );
2816
2817 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2818 status = PSA_ERROR_INVALID_SIGNATURE;
2819
2820cleanup:
2821 if( status == PSA_SUCCESS )
2822 status = psa_mac_abort( operation );
2823 else
2824 psa_mac_abort( operation );
2825
Gilles Peskine3f108122018-12-07 18:14:53 +01002826 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002827
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002828 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829}
2830
2831
Gilles Peskine20035e32018-02-03 22:44:14 +01002832
Gilles Peskine20035e32018-02-03 22:44:14 +01002833/****************************************************************/
2834/* Asymmetric cryptography */
2835/****************************************************************/
2836
Gilles Peskine2b450e32018-06-27 15:42:46 +02002837#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002838/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002839 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002840static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2841 size_t hash_length,
2842 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002843{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002844 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002845 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002846 *md_alg = mbedtls_md_get_type( md_info );
2847
2848 /* The Mbed TLS RSA module uses an unsigned int for hash length
2849 * parameters. Validate that it fits so that we don't risk an
2850 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002851#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002852 if( hash_length > UINT_MAX )
2853 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002854#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002855
2856#if defined(MBEDTLS_PKCS1_V15)
2857 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2858 * must be correct. */
2859 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2860 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002861 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002862 if( md_info == NULL )
2863 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002864 if( mbedtls_md_get_size( md_info ) != hash_length )
2865 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002866 }
2867#endif /* MBEDTLS_PKCS1_V15 */
2868
2869#if defined(MBEDTLS_PKCS1_V21)
2870 /* PSS requires a hash internally. */
2871 if( PSA_ALG_IS_RSA_PSS( alg ) )
2872 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002873 if( md_info == NULL )
2874 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002875 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002876#endif /* MBEDTLS_PKCS1_V21 */
2877
Gilles Peskine61b91d42018-06-08 16:09:36 +02002878 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002879}
2880
Gilles Peskine2b450e32018-06-27 15:42:46 +02002881static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2882 psa_algorithm_t alg,
2883 const uint8_t *hash,
2884 size_t hash_length,
2885 uint8_t *signature,
2886 size_t signature_size,
2887 size_t *signature_length )
2888{
2889 psa_status_t status;
2890 int ret;
2891 mbedtls_md_type_t md_alg;
2892
2893 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2894 if( status != PSA_SUCCESS )
2895 return( status );
2896
Gilles Peskine630a18a2018-06-29 17:49:35 +02002897 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002898 return( PSA_ERROR_BUFFER_TOO_SMALL );
2899
2900#if defined(MBEDTLS_PKCS1_V15)
2901 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2902 {
2903 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2904 MBEDTLS_MD_NONE );
2905 ret = mbedtls_rsa_pkcs1_sign( rsa,
2906 mbedtls_ctr_drbg_random,
2907 &global_data.ctr_drbg,
2908 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002909 md_alg,
2910 (unsigned int) hash_length,
2911 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002912 signature );
2913 }
2914 else
2915#endif /* MBEDTLS_PKCS1_V15 */
2916#if defined(MBEDTLS_PKCS1_V21)
2917 if( PSA_ALG_IS_RSA_PSS( alg ) )
2918 {
2919 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2920 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2921 mbedtls_ctr_drbg_random,
2922 &global_data.ctr_drbg,
2923 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002924 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002925 (unsigned int) hash_length,
2926 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002927 signature );
2928 }
2929 else
2930#endif /* MBEDTLS_PKCS1_V21 */
2931 {
2932 return( PSA_ERROR_INVALID_ARGUMENT );
2933 }
2934
2935 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002936 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002937 return( mbedtls_to_psa_error( ret ) );
2938}
2939
2940static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2941 psa_algorithm_t alg,
2942 const uint8_t *hash,
2943 size_t hash_length,
2944 const uint8_t *signature,
2945 size_t signature_length )
2946{
2947 psa_status_t status;
2948 int ret;
2949 mbedtls_md_type_t md_alg;
2950
2951 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2952 if( status != PSA_SUCCESS )
2953 return( status );
2954
Gilles Peskine630a18a2018-06-29 17:49:35 +02002955 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002956 return( PSA_ERROR_BUFFER_TOO_SMALL );
2957
2958#if defined(MBEDTLS_PKCS1_V15)
2959 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2960 {
2961 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2962 MBEDTLS_MD_NONE );
2963 ret = mbedtls_rsa_pkcs1_verify( rsa,
2964 mbedtls_ctr_drbg_random,
2965 &global_data.ctr_drbg,
2966 MBEDTLS_RSA_PUBLIC,
2967 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002968 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002969 hash,
2970 signature );
2971 }
2972 else
2973#endif /* MBEDTLS_PKCS1_V15 */
2974#if defined(MBEDTLS_PKCS1_V21)
2975 if( PSA_ALG_IS_RSA_PSS( alg ) )
2976 {
2977 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2978 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2979 mbedtls_ctr_drbg_random,
2980 &global_data.ctr_drbg,
2981 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002982 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002983 (unsigned int) hash_length,
2984 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002985 signature );
2986 }
2987 else
2988#endif /* MBEDTLS_PKCS1_V21 */
2989 {
2990 return( PSA_ERROR_INVALID_ARGUMENT );
2991 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002992
2993 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2994 * the rest of the signature is invalid". This has little use in
2995 * practice and PSA doesn't report this distinction. */
2996 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2997 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002998 return( mbedtls_to_psa_error( ret ) );
2999}
3000#endif /* MBEDTLS_RSA_C */
3001
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003002#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003003/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3004 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3005 * (even though these functions don't modify it). */
3006static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3007 psa_algorithm_t alg,
3008 const uint8_t *hash,
3009 size_t hash_length,
3010 uint8_t *signature,
3011 size_t signature_size,
3012 size_t *signature_length )
3013{
3014 int ret;
3015 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003016 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003017 mbedtls_mpi_init( &r );
3018 mbedtls_mpi_init( &s );
3019
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003020 if( signature_size < 2 * curve_bytes )
3021 {
3022 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3023 goto cleanup;
3024 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003025
Gilles Peskinea05219c2018-11-16 16:02:56 +01003026#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003027 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3028 {
3029 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3030 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3031 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3032 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3033 hash, hash_length,
3034 md_alg ) );
3035 }
3036 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003037#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003038 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003039 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003040 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3041 hash, hash_length,
3042 mbedtls_ctr_drbg_random,
3043 &global_data.ctr_drbg ) );
3044 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003045
3046 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3047 signature,
3048 curve_bytes ) );
3049 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3050 signature + curve_bytes,
3051 curve_bytes ) );
3052
3053cleanup:
3054 mbedtls_mpi_free( &r );
3055 mbedtls_mpi_free( &s );
3056 if( ret == 0 )
3057 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003058 return( mbedtls_to_psa_error( ret ) );
3059}
3060
3061static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3062 const uint8_t *hash,
3063 size_t hash_length,
3064 const uint8_t *signature,
3065 size_t signature_length )
3066{
3067 int ret;
3068 mbedtls_mpi r, s;
3069 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3070 mbedtls_mpi_init( &r );
3071 mbedtls_mpi_init( &s );
3072
3073 if( signature_length != 2 * curve_bytes )
3074 return( PSA_ERROR_INVALID_SIGNATURE );
3075
3076 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3077 signature,
3078 curve_bytes ) );
3079 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3080 signature + curve_bytes,
3081 curve_bytes ) );
3082
3083 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3084 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003085
3086cleanup:
3087 mbedtls_mpi_free( &r );
3088 mbedtls_mpi_free( &s );
3089 return( mbedtls_to_psa_error( ret ) );
3090}
3091#endif /* MBEDTLS_ECDSA_C */
3092
Gilles Peskinec5487a82018-12-03 18:08:14 +01003093psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003094 psa_algorithm_t alg,
3095 const uint8_t *hash,
3096 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003097 uint8_t *signature,
3098 size_t signature_size,
3099 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003100{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003101 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003102 psa_status_t status;
3103
3104 *signature_length = signature_size;
3105
Gilles Peskine28f8f302019-07-24 13:30:31 +02003106 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003107 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003108 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003109 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003110 {
3111 status = PSA_ERROR_INVALID_ARGUMENT;
3112 goto exit;
3113 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003114
Gilles Peskine20035e32018-02-03 22:44:14 +01003115#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003116 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003117 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003118 status = psa_rsa_sign( slot->data.rsa,
3119 alg,
3120 hash, hash_length,
3121 signature, signature_size,
3122 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003123 }
3124 else
3125#endif /* defined(MBEDTLS_RSA_C) */
3126#if defined(MBEDTLS_ECP_C)
3127 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3128 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003129#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003130 if(
3131#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3132 PSA_ALG_IS_ECDSA( alg )
3133#else
3134 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3135#endif
3136 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003137 status = psa_ecdsa_sign( slot->data.ecp,
3138 alg,
3139 hash, hash_length,
3140 signature, signature_size,
3141 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003142 else
3143#endif /* defined(MBEDTLS_ECDSA_C) */
3144 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003145 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003146 }
itayzafrir5c753392018-05-08 11:18:38 +03003147 }
3148 else
3149#endif /* defined(MBEDTLS_ECP_C) */
3150 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003151 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003152 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003153
3154exit:
3155 /* Fill the unused part of the output buffer (the whole buffer on error,
3156 * the trailing part on success) with something that isn't a valid mac
3157 * (barring an attack on the mac and deliberately-crafted input),
3158 * in case the caller doesn't check the return status properly. */
3159 if( status == PSA_SUCCESS )
3160 memset( signature + *signature_length, '!',
3161 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003162 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003163 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003164 /* If signature_size is 0 then we have nothing to do. We must not call
3165 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003166 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003167}
3168
Gilles Peskinec5487a82018-12-03 18:08:14 +01003169psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003170 psa_algorithm_t alg,
3171 const uint8_t *hash,
3172 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003173 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003174 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003175{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003176 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003177 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003178
Gilles Peskine28f8f302019-07-24 13:30:31 +02003179 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003180 if( status != PSA_SUCCESS )
3181 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003182
Gilles Peskine61b91d42018-06-08 16:09:36 +02003183#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003184 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003186 return( psa_rsa_verify( slot->data.rsa,
3187 alg,
3188 hash, hash_length,
3189 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 }
3191 else
3192#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003193#if defined(MBEDTLS_ECP_C)
3194 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3195 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003196#if defined(MBEDTLS_ECDSA_C)
3197 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003198 return( psa_ecdsa_verify( slot->data.ecp,
3199 hash, hash_length,
3200 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003201 else
3202#endif /* defined(MBEDTLS_ECDSA_C) */
3203 {
3204 return( PSA_ERROR_INVALID_ARGUMENT );
3205 }
itayzafrir5c753392018-05-08 11:18:38 +03003206 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003207 else
3208#endif /* defined(MBEDTLS_ECP_C) */
3209 {
3210 return( PSA_ERROR_NOT_SUPPORTED );
3211 }
3212}
3213
Gilles Peskine072ac562018-06-30 00:21:29 +02003214#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3215static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3216 mbedtls_rsa_context *rsa )
3217{
3218 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3219 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3220 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3221 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3222}
3223#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3224
Gilles Peskinec5487a82018-12-03 18:08:14 +01003225psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003226 psa_algorithm_t alg,
3227 const uint8_t *input,
3228 size_t input_length,
3229 const uint8_t *salt,
3230 size_t salt_length,
3231 uint8_t *output,
3232 size_t output_size,
3233 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003235 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003236 psa_status_t status;
3237
Darryl Green5cc689a2018-07-24 15:34:10 +01003238 (void) input;
3239 (void) input_length;
3240 (void) salt;
3241 (void) output;
3242 (void) output_size;
3243
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003244 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003246 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3247 return( PSA_ERROR_INVALID_ARGUMENT );
3248
Gilles Peskine28f8f302019-07-24 13:30:31 +02003249 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003250 if( status != PSA_SUCCESS )
3251 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003252 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003253 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255
3256#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003257 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 {
3259 mbedtls_rsa_context *rsa = slot->data.rsa;
3260 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003261 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003262 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003264 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003266 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3267 mbedtls_ctr_drbg_random,
3268 &global_data.ctr_drbg,
3269 MBEDTLS_RSA_PUBLIC,
3270 input_length,
3271 input,
3272 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003273 }
3274 else
3275#endif /* MBEDTLS_PKCS1_V15 */
3276#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003277 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003279 psa_rsa_oaep_set_padding_mode( alg, rsa );
3280 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3281 mbedtls_ctr_drbg_random,
3282 &global_data.ctr_drbg,
3283 MBEDTLS_RSA_PUBLIC,
3284 salt, salt_length,
3285 input_length,
3286 input,
3287 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288 }
3289 else
3290#endif /* MBEDTLS_PKCS1_V21 */
3291 {
3292 return( PSA_ERROR_INVALID_ARGUMENT );
3293 }
3294 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003295 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296 return( mbedtls_to_psa_error( ret ) );
3297 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003299#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300 {
3301 return( PSA_ERROR_NOT_SUPPORTED );
3302 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003303}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304
Gilles Peskinec5487a82018-12-03 18:08:14 +01003305psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003306 psa_algorithm_t alg,
3307 const uint8_t *input,
3308 size_t input_length,
3309 const uint8_t *salt,
3310 size_t salt_length,
3311 uint8_t *output,
3312 size_t output_size,
3313 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003315 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003316 psa_status_t status;
3317
Darryl Green5cc689a2018-07-24 15:34:10 +01003318 (void) input;
3319 (void) input_length;
3320 (void) salt;
3321 (void) output;
3322 (void) output_size;
3323
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003324 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003326 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3327 return( PSA_ERROR_INVALID_ARGUMENT );
3328
Gilles Peskine28f8f302019-07-24 13:30:31 +02003329 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003330 if( status != PSA_SUCCESS )
3331 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003332 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333 return( PSA_ERROR_INVALID_ARGUMENT );
3334
3335#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003336 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 {
3338 mbedtls_rsa_context *rsa = slot->data.rsa;
3339 int ret;
3340
Gilles Peskine630a18a2018-06-29 17:49:35 +02003341 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003342 return( PSA_ERROR_INVALID_ARGUMENT );
3343
3344#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003345 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003346 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003347 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3348 mbedtls_ctr_drbg_random,
3349 &global_data.ctr_drbg,
3350 MBEDTLS_RSA_PRIVATE,
3351 output_length,
3352 input,
3353 output,
3354 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003355 }
3356 else
3357#endif /* MBEDTLS_PKCS1_V15 */
3358#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003359 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003361 psa_rsa_oaep_set_padding_mode( alg, rsa );
3362 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3363 mbedtls_ctr_drbg_random,
3364 &global_data.ctr_drbg,
3365 MBEDTLS_RSA_PRIVATE,
3366 salt, salt_length,
3367 output_length,
3368 input,
3369 output,
3370 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371 }
3372 else
3373#endif /* MBEDTLS_PKCS1_V21 */
3374 {
3375 return( PSA_ERROR_INVALID_ARGUMENT );
3376 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003377
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378 return( mbedtls_to_psa_error( ret ) );
3379 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003380 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003381#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382 {
3383 return( PSA_ERROR_NOT_SUPPORTED );
3384 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003385}
Gilles Peskine20035e32018-02-03 22:44:14 +01003386
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003387
3388
mohammad1603503973b2018-03-12 15:59:30 +02003389/****************************************************************/
3390/* Symmetric cryptography */
3391/****************************************************************/
3392
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003393/* Initialize the cipher operation structure. Once this function has been
3394 * called, psa_cipher_abort can run and will do the right thing. */
3395static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3396 psa_algorithm_t alg )
3397{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003398 if( ! PSA_ALG_IS_CIPHER( alg ) )
3399 {
3400 memset( operation, 0, sizeof( *operation ) );
3401 return( PSA_ERROR_INVALID_ARGUMENT );
3402 }
3403
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003404 operation->alg = alg;
3405 operation->key_set = 0;
3406 operation->iv_set = 0;
3407 operation->iv_required = 1;
3408 operation->iv_size = 0;
3409 operation->block_size = 0;
3410 mbedtls_cipher_init( &operation->ctx.cipher );
3411 return( PSA_SUCCESS );
3412}
3413
Gilles Peskinee553c652018-06-04 16:22:46 +02003414static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003415 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003416 psa_algorithm_t alg,
3417 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003418{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003419 int ret = 0;
3420 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003421 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003422 size_t key_bits;
3423 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003424 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3425 PSA_KEY_USAGE_ENCRYPT :
3426 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003427
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003428 /* A context must be freshly initialized before it can be set up. */
3429 if( operation->alg != 0 )
3430 {
3431 return( PSA_ERROR_BAD_STATE );
3432 }
3433
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003434 status = psa_cipher_init( operation, alg );
3435 if( status != PSA_SUCCESS )
3436 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003437
Gilles Peskine28f8f302019-07-24 13:30:31 +02003438 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003439 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003440 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003441 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003442
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003443 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003444 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003445 {
3446 status = PSA_ERROR_NOT_SUPPORTED;
3447 goto exit;
3448 }
mohammad1603503973b2018-03-12 15:59:30 +02003449
mohammad1603503973b2018-03-12 15:59:30 +02003450 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003451 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003452 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003453
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003454#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003455 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003456 {
3457 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3458 unsigned char keys[24];
3459 memcpy( keys, slot->data.raw.data, 16 );
3460 memcpy( keys + 16, slot->data.raw.data, 8 );
3461 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3462 keys,
3463 192, cipher_operation );
3464 }
3465 else
3466#endif
3467 {
3468 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3469 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003470 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003471 }
Moran Peker41deec42018-04-04 15:43:05 +03003472 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003473 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003474
mohammad16038481e742018-03-18 13:57:31 +02003475#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003476 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003477 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003478 case PSA_ALG_CBC_NO_PADDING:
3479 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3480 MBEDTLS_PADDING_NONE );
3481 break;
3482 case PSA_ALG_CBC_PKCS7:
3483 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3484 MBEDTLS_PADDING_PKCS7 );
3485 break;
3486 default:
3487 /* The algorithm doesn't involve padding. */
3488 ret = 0;
3489 break;
3490 }
3491 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003492 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003493#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3494
mohammad1603503973b2018-03-12 15:59:30 +02003495 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003496 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3497 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3498 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003499 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003500 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003501 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003502#if defined(MBEDTLS_CHACHA20_C)
3503 else
3504 if( alg == PSA_ALG_CHACHA20 )
3505 operation->iv_size = 12;
3506#endif
mohammad1603503973b2018-03-12 15:59:30 +02003507
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003508exit:
3509 if( status == 0 )
3510 status = mbedtls_to_psa_error( ret );
3511 if( status != 0 )
3512 psa_cipher_abort( operation );
3513 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003514}
3515
Gilles Peskinefe119512018-07-08 21:39:34 +02003516psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003517 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003518 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003519{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003520 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003521}
3522
Gilles Peskinefe119512018-07-08 21:39:34 +02003523psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003524 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003525 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003526{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003527 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003528}
3529
Gilles Peskinefe119512018-07-08 21:39:34 +02003530psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3531 unsigned char *iv,
3532 size_t iv_size,
3533 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003534{
itayzafrir534bd7c2018-08-02 13:56:32 +03003535 psa_status_t status;
3536 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003537 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003538 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003539 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003540 }
Moran Peker41deec42018-04-04 15:43:05 +03003541 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003542 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003543 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003544 goto exit;
3545 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003546 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3547 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003548 if( ret != 0 )
3549 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003550 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003551 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003552 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003553
mohammad16038481e742018-03-18 13:57:31 +02003554 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003555 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003556
Moran Peker395db872018-05-31 14:07:14 +03003557exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003558 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003559 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003560 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003561}
3562
Gilles Peskinefe119512018-07-08 21:39:34 +02003563psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3564 const unsigned char *iv,
3565 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003566{
itayzafrir534bd7c2018-08-02 13:56:32 +03003567 psa_status_t status;
3568 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003569 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003570 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003571 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003572 }
Moran Pekera28258c2018-05-29 16:25:04 +03003573 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003574 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003575 status = PSA_ERROR_INVALID_ARGUMENT;
3576 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003577 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003578 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3579 status = mbedtls_to_psa_error( ret );
3580exit:
3581 if( status == PSA_SUCCESS )
3582 operation->iv_set = 1;
3583 else
mohammad1603503973b2018-03-12 15:59:30 +02003584 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003585 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003586}
3587
Gilles Peskinee553c652018-06-04 16:22:46 +02003588psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3589 const uint8_t *input,
3590 size_t input_length,
3591 unsigned char *output,
3592 size_t output_size,
3593 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003594{
itayzafrir534bd7c2018-08-02 13:56:32 +03003595 psa_status_t status;
3596 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003597 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003598
3599 if( operation->alg == 0 )
3600 {
3601 return( PSA_ERROR_BAD_STATE );
3602 }
3603
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003604 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003605 {
3606 /* Take the unprocessed partial block left over from previous
3607 * update calls, if any, plus the input to this call. Remove
3608 * the last partial block, if any. You get the data that will be
3609 * output in this call. */
3610 expected_output_size =
3611 ( operation->ctx.cipher.unprocessed_len + input_length )
3612 / operation->block_size * operation->block_size;
3613 }
3614 else
3615 {
3616 expected_output_size = input_length;
3617 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003618
Gilles Peskine89d789c2018-06-04 17:17:16 +02003619 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003620 {
3621 status = PSA_ERROR_BUFFER_TOO_SMALL;
3622 goto exit;
3623 }
mohammad160382759612018-03-12 18:16:40 +02003624
mohammad1603503973b2018-03-12 15:59:30 +02003625 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003626 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003627 status = mbedtls_to_psa_error( ret );
3628exit:
3629 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003630 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003631 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003632}
3633
Gilles Peskinee553c652018-06-04 16:22:46 +02003634psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3635 uint8_t *output,
3636 size_t output_size,
3637 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003638{
David Saadab4ecc272019-02-14 13:48:10 +02003639 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003640 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003641 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003642
mohammad1603503973b2018-03-12 15:59:30 +02003643 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003644 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003645 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003646 }
3647 if( operation->iv_required && ! operation->iv_set )
3648 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003649 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003650 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003651
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003652 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003653 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3654 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003655 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003656 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003657 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003658 }
3659
Janos Follath315b51c2018-07-09 16:04:51 +01003660 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3661 temp_output_buffer,
3662 output_length );
3663 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003664 {
Janos Follath315b51c2018-07-09 16:04:51 +01003665 status = mbedtls_to_psa_error( cipher_ret );
3666 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003667 }
Janos Follath315b51c2018-07-09 16:04:51 +01003668
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003669 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003670 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003671 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003672 memcpy( output, temp_output_buffer, *output_length );
3673 else
3674 {
Janos Follath315b51c2018-07-09 16:04:51 +01003675 status = PSA_ERROR_BUFFER_TOO_SMALL;
3676 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003677 }
mohammad1603503973b2018-03-12 15:59:30 +02003678
Gilles Peskine3f108122018-12-07 18:14:53 +01003679 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003680 status = psa_cipher_abort( operation );
3681
3682 return( status );
3683
3684error:
3685
3686 *output_length = 0;
3687
Gilles Peskine3f108122018-12-07 18:14:53 +01003688 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003689 (void) psa_cipher_abort( operation );
3690
3691 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003692}
3693
Gilles Peskinee553c652018-06-04 16:22:46 +02003694psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3695{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003696 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003697 {
3698 /* The object has (apparently) been initialized but it is not
3699 * in use. It's ok to call abort on such an object, and there's
3700 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003701 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003702 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003703
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003704 /* Sanity check (shouldn't happen: operation->alg should
3705 * always have been initialized to a valid value). */
3706 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3707 return( PSA_ERROR_BAD_STATE );
3708
mohammad1603503973b2018-03-12 15:59:30 +02003709 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003710
Moran Peker41deec42018-04-04 15:43:05 +03003711 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003712 operation->key_set = 0;
3713 operation->iv_set = 0;
3714 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003715 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003716 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003717
Moran Peker395db872018-05-31 14:07:14 +03003718 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003719}
3720
Gilles Peskinea0655c32018-04-30 17:06:50 +02003721
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003722
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003723
mohammad16035955c982018-04-26 00:53:03 +03003724/****************************************************************/
3725/* AEAD */
3726/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003727
Gilles Peskineedf9a652018-08-17 18:11:56 +02003728typedef struct
3729{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003730 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003731 const mbedtls_cipher_info_t *cipher_info;
3732 union
3733 {
3734#if defined(MBEDTLS_CCM_C)
3735 mbedtls_ccm_context ccm;
3736#endif /* MBEDTLS_CCM_C */
3737#if defined(MBEDTLS_GCM_C)
3738 mbedtls_gcm_context gcm;
3739#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003740#if defined(MBEDTLS_CHACHAPOLY_C)
3741 mbedtls_chachapoly_context chachapoly;
3742#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003743 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003744 psa_algorithm_t core_alg;
3745 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003746 uint8_t tag_length;
3747} aead_operation_t;
3748
Gilles Peskine30a9e412019-01-14 18:36:12 +01003749static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003750{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003751 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003752 {
3753#if defined(MBEDTLS_CCM_C)
3754 case PSA_ALG_CCM:
3755 mbedtls_ccm_free( &operation->ctx.ccm );
3756 break;
3757#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003758#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003759 case PSA_ALG_GCM:
3760 mbedtls_gcm_free( &operation->ctx.gcm );
3761 break;
3762#endif /* MBEDTLS_GCM_C */
3763 }
3764}
3765
3766static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003767 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768 psa_key_usage_t usage,
3769 psa_algorithm_t alg )
3770{
3771 psa_status_t status;
3772 size_t key_bits;
3773 mbedtls_cipher_id_t cipher_id;
3774
Gilles Peskine28f8f302019-07-24 13:30:31 +02003775 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003776 if( status != PSA_SUCCESS )
3777 return( status );
3778
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003779 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780
3781 operation->cipher_info =
3782 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3783 &cipher_id );
3784 if( operation->cipher_info == NULL )
3785 return( PSA_ERROR_NOT_SUPPORTED );
3786
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003787 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003788 {
3789#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003790 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3791 operation->core_alg = PSA_ALG_CCM;
3792 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003793 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3794 * The call to mbedtls_ccm_encrypt_and_tag or
3795 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003796 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3797 return( PSA_ERROR_INVALID_ARGUMENT );
3798 mbedtls_ccm_init( &operation->ctx.ccm );
3799 status = mbedtls_to_psa_error(
3800 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3801 operation->slot->data.raw.data,
3802 (unsigned int) key_bits ) );
3803 if( status != 0 )
3804 goto cleanup;
3805 break;
3806#endif /* MBEDTLS_CCM_C */
3807
3808#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003809 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3810 operation->core_alg = PSA_ALG_GCM;
3811 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003812 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3813 * The call to mbedtls_gcm_crypt_and_tag or
3814 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003815 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3816 return( PSA_ERROR_INVALID_ARGUMENT );
3817 mbedtls_gcm_init( &operation->ctx.gcm );
3818 status = mbedtls_to_psa_error(
3819 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3820 operation->slot->data.raw.data,
3821 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003822 if( status != 0 )
3823 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003824 break;
3825#endif /* MBEDTLS_GCM_C */
3826
Gilles Peskine26869f22019-05-06 15:25:00 +02003827#if defined(MBEDTLS_CHACHAPOLY_C)
3828 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3829 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3830 operation->full_tag_length = 16;
3831 /* We only support the default tag length. */
3832 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3833 return( PSA_ERROR_NOT_SUPPORTED );
3834 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3835 status = mbedtls_to_psa_error(
3836 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3837 operation->slot->data.raw.data ) );
3838 if( status != 0 )
3839 goto cleanup;
3840 break;
3841#endif /* MBEDTLS_CHACHAPOLY_C */
3842
Gilles Peskineedf9a652018-08-17 18:11:56 +02003843 default:
3844 return( PSA_ERROR_NOT_SUPPORTED );
3845 }
3846
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003847 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3848 {
3849 status = PSA_ERROR_INVALID_ARGUMENT;
3850 goto cleanup;
3851 }
3852 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003853
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854 return( PSA_SUCCESS );
3855
3856cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003857 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003858 return( status );
3859}
3860
Gilles Peskinec5487a82018-12-03 18:08:14 +01003861psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003862 psa_algorithm_t alg,
3863 const uint8_t *nonce,
3864 size_t nonce_length,
3865 const uint8_t *additional_data,
3866 size_t additional_data_length,
3867 const uint8_t *plaintext,
3868 size_t plaintext_length,
3869 uint8_t *ciphertext,
3870 size_t ciphertext_size,
3871 size_t *ciphertext_length )
3872{
mohammad16035955c982018-04-26 00:53:03 +03003873 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003874 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003875 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003876
mohammad1603f08a5502018-06-03 15:05:47 +03003877 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003878
Gilles Peskinec5487a82018-12-03 18:08:14 +01003879 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003880 if( status != PSA_SUCCESS )
3881 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003882
Gilles Peskineedf9a652018-08-17 18:11:56 +02003883 /* For all currently supported modes, the tag is at the end of the
3884 * ciphertext. */
3885 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3886 {
3887 status = PSA_ERROR_BUFFER_TOO_SMALL;
3888 goto exit;
3889 }
3890 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003891
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003892#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003893 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003894 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003895 status = mbedtls_to_psa_error(
3896 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3897 MBEDTLS_GCM_ENCRYPT,
3898 plaintext_length,
3899 nonce, nonce_length,
3900 additional_data, additional_data_length,
3901 plaintext, ciphertext,
3902 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003903 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003904 else
3905#endif /* MBEDTLS_GCM_C */
3906#if defined(MBEDTLS_CCM_C)
3907 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003908 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003909 status = mbedtls_to_psa_error(
3910 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3911 plaintext_length,
3912 nonce, nonce_length,
3913 additional_data,
3914 additional_data_length,
3915 plaintext, ciphertext,
3916 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003917 }
mohammad16035c8845f2018-05-09 05:40:09 -07003918 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003919#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003920#if defined(MBEDTLS_CHACHAPOLY_C)
3921 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3922 {
3923 if( nonce_length != 12 || operation.tag_length != 16 )
3924 {
3925 status = PSA_ERROR_NOT_SUPPORTED;
3926 goto exit;
3927 }
3928 status = mbedtls_to_psa_error(
3929 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3930 plaintext_length,
3931 nonce,
3932 additional_data,
3933 additional_data_length,
3934 plaintext,
3935 ciphertext,
3936 tag ) );
3937 }
3938 else
3939#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003940 {
mohammad1603554faad2018-06-03 15:07:38 +03003941 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003942 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003943
Gilles Peskineedf9a652018-08-17 18:11:56 +02003944 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3945 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003946
Gilles Peskineedf9a652018-08-17 18:11:56 +02003947exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003948 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003949 if( status == PSA_SUCCESS )
3950 *ciphertext_length = plaintext_length + operation.tag_length;
3951 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003952}
3953
Gilles Peskineee652a32018-06-01 19:23:52 +02003954/* Locate the tag in a ciphertext buffer containing the encrypted data
3955 * followed by the tag. Return the length of the part preceding the tag in
3956 * *plaintext_length. This is the size of the plaintext in modes where
3957 * the encrypted data has the same size as the plaintext, such as
3958 * CCM and GCM. */
3959static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3960 const uint8_t *ciphertext,
3961 size_t ciphertext_length,
3962 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003963 const uint8_t **p_tag )
3964{
3965 size_t payload_length;
3966 if( tag_length > ciphertext_length )
3967 return( PSA_ERROR_INVALID_ARGUMENT );
3968 payload_length = ciphertext_length - tag_length;
3969 if( payload_length > plaintext_size )
3970 return( PSA_ERROR_BUFFER_TOO_SMALL );
3971 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003972 return( PSA_SUCCESS );
3973}
3974
Gilles Peskinec5487a82018-12-03 18:08:14 +01003975psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003976 psa_algorithm_t alg,
3977 const uint8_t *nonce,
3978 size_t nonce_length,
3979 const uint8_t *additional_data,
3980 size_t additional_data_length,
3981 const uint8_t *ciphertext,
3982 size_t ciphertext_length,
3983 uint8_t *plaintext,
3984 size_t plaintext_size,
3985 size_t *plaintext_length )
3986{
mohammad16035955c982018-04-26 00:53:03 +03003987 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003988 aead_operation_t operation;
3989 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003990
Gilles Peskineee652a32018-06-01 19:23:52 +02003991 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003992
Gilles Peskinec5487a82018-12-03 18:08:14 +01003993 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003994 if( status != PSA_SUCCESS )
3995 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003996
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003997 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3998 ciphertext, ciphertext_length,
3999 plaintext_size, &tag );
4000 if( status != PSA_SUCCESS )
4001 goto exit;
4002
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004003#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004004 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004005 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004006 status = mbedtls_to_psa_error(
4007 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4008 ciphertext_length - operation.tag_length,
4009 nonce, nonce_length,
4010 additional_data,
4011 additional_data_length,
4012 tag, operation.tag_length,
4013 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004014 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004015 else
4016#endif /* MBEDTLS_GCM_C */
4017#if defined(MBEDTLS_CCM_C)
4018 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004019 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004020 status = mbedtls_to_psa_error(
4021 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4022 ciphertext_length - operation.tag_length,
4023 nonce, nonce_length,
4024 additional_data,
4025 additional_data_length,
4026 ciphertext, plaintext,
4027 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004028 }
mohammad160339574652018-06-01 04:39:53 -07004029 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004030#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004031#if defined(MBEDTLS_CHACHAPOLY_C)
4032 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4033 {
4034 if( nonce_length != 12 || operation.tag_length != 16 )
4035 {
4036 status = PSA_ERROR_NOT_SUPPORTED;
4037 goto exit;
4038 }
4039 status = mbedtls_to_psa_error(
4040 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4041 ciphertext_length - operation.tag_length,
4042 nonce,
4043 additional_data,
4044 additional_data_length,
4045 tag,
4046 ciphertext,
4047 plaintext ) );
4048 }
4049 else
4050#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004051 {
mohammad1603554faad2018-06-03 15:07:38 +03004052 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004053 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004054
Gilles Peskineedf9a652018-08-17 18:11:56 +02004055 if( status != PSA_SUCCESS && plaintext_size != 0 )
4056 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004057
Gilles Peskineedf9a652018-08-17 18:11:56 +02004058exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004059 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 if( status == PSA_SUCCESS )
4061 *plaintext_length = ciphertext_length - operation.tag_length;
4062 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004063}
4064
Gilles Peskinea0655c32018-04-30 17:06:50 +02004065
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004066
Gilles Peskine20035e32018-02-03 22:44:14 +01004067/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004068/* Generators */
4069/****************************************************************/
4070
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004071#define HKDF_STATE_INIT 0 /* no input yet */
4072#define HKDF_STATE_STARTED 1 /* got salt */
4073#define HKDF_STATE_KEYED 2 /* got key */
4074#define HKDF_STATE_OUTPUT 3 /* output started */
4075
Gilles Peskinecbe66502019-05-16 16:59:18 +02004076static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004077 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004078{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004079 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4080 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004081 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004082 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004083}
4084
4085
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004086psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004087{
4088 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004089 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004090 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004091 {
4092 /* The object has (apparently) been initialized but it is not
4093 * in use. It's ok to call abort on such an object, and there's
4094 * nothing to do. */
4095 }
4096 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004097#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004098 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004099 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004100 mbedtls_free( operation->ctx.hkdf.info );
4101 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004102 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004103 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004105 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004106 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004107#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004109 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004110 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4111 operation->ctx.tls12_prf.key_len );
4112 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004113 }
Hanno Becker580fba12018-11-13 20:50:45 +00004114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004115 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004116 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004117 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4118 operation->ctx.tls12_prf.Ai_with_seed_len );
4119 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004120 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004121#else
4122 if( operation->ctx.tls12_prf.seed != NULL )
4123 {
4124 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4125 operation->ctx.tls12_prf.seed_length );
4126 mbedtls_free( operation->ctx.tls12_prf.seed );
4127 }
4128
4129 if( operation->ctx.tls12_prf.label != NULL )
4130 {
4131 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4132 operation->ctx.tls12_prf.label_length );
4133 mbedtls_free( operation->ctx.tls12_prf.label );
4134 }
4135
4136 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4137
4138 /* We leave the fields Ai and output_block to be erased safely by the
4139 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004140#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004141 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004142 else
4143#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004144 {
4145 status = PSA_ERROR_BAD_STATE;
4146 }
Janos Follath083036a2019-06-11 10:22:26 +01004147 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004148 return( status );
4149}
4150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004152 size_t *capacity)
4153{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004154 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004155 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004157 return PSA_ERROR_BAD_STATE;
4158 }
4159
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004161 return( PSA_SUCCESS );
4162}
4163
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004165 size_t capacity )
4166{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004167 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004168 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004169 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004170 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004172 return( PSA_SUCCESS );
4173}
4174
Gilles Peskinebef7f142018-07-12 17:22:21 +02004175#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004176/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004177 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004178static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004179 psa_algorithm_t hash_alg,
4180 uint8_t *output,
4181 size_t output_length )
4182{
4183 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4184 psa_status_t status;
4185
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004186 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4187 return( PSA_ERROR_BAD_STATE );
4188 hkdf->state = HKDF_STATE_OUTPUT;
4189
Gilles Peskinebef7f142018-07-12 17:22:21 +02004190 while( output_length != 0 )
4191 {
4192 /* Copy what remains of the current block */
4193 uint8_t n = hash_length - hkdf->offset_in_block;
4194 if( n > output_length )
4195 n = (uint8_t) output_length;
4196 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4197 output += n;
4198 output_length -= n;
4199 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004200 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004201 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004202 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004203 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004204 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004205 * object was corrupted or if this function is called directly
4206 * inside the library. */
4207 if( hkdf->block_number == 0xff )
4208 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004209
4210 /* We need a new block */
4211 ++hkdf->block_number;
4212 hkdf->offset_in_block = 0;
4213 status = psa_hmac_setup_internal( &hkdf->hmac,
4214 hkdf->prk, hash_length,
4215 hash_alg );
4216 if( status != PSA_SUCCESS )
4217 return( status );
4218 if( hkdf->block_number != 1 )
4219 {
4220 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4221 hkdf->output_block,
4222 hash_length );
4223 if( status != PSA_SUCCESS )
4224 return( status );
4225 }
4226 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4227 hkdf->info,
4228 hkdf->info_length );
4229 if( status != PSA_SUCCESS )
4230 return( status );
4231 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4232 &hkdf->block_number, 1 );
4233 if( status != PSA_SUCCESS )
4234 return( status );
4235 status = psa_hmac_finish_internal( &hkdf->hmac,
4236 hkdf->output_block,
4237 sizeof( hkdf->output_block ) );
4238 if( status != PSA_SUCCESS )
4239 return( status );
4240 }
4241
4242 return( PSA_SUCCESS );
4243}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004244
Janos Follath999f6482019-06-11 12:04:10 +01004245#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004246static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4247 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004248 psa_algorithm_t alg )
4249{
4250 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4251 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4252 psa_hmac_internal_data hmac;
4253 psa_status_t status, cleanup_status;
4254
Hanno Becker3b339e22018-11-13 20:56:14 +00004255 unsigned char *Ai;
4256 size_t Ai_len;
4257
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004258 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004259 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004260 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004261 * object was corrupted or if this function is called directly
4262 * inside the library. */
4263 if( tls12_prf->block_number == 0xff )
4264 return( PSA_ERROR_BAD_STATE );
4265
4266 /* We need a new block */
4267 ++tls12_prf->block_number;
4268 tls12_prf->offset_in_block = 0;
4269
4270 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4271 *
4272 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4273 *
4274 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4275 * HMAC_hash(secret, A(2) + seed) +
4276 * HMAC_hash(secret, A(3) + seed) + ...
4277 *
4278 * A(0) = seed
4279 * A(i) = HMAC_hash( secret, A(i-1) )
4280 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004281 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004282 * `HMAC_hash(secret, A(i) + seed)` from which the output
4283 * is currently extracted as `output_block`, while
4284 * `A(i) + seed` is stored in `Ai_with_seed`.
4285 *
4286 * Generating a new block means recalculating `Ai_with_seed`
4287 * from the A(i)-part of it, and afterwards recalculating
4288 * `output_block`.
4289 *
4290 * A(0) is computed at setup time.
4291 *
4292 */
4293
4294 psa_hmac_init_internal( &hmac );
4295
4296 /* We must distinguish the calculation of A(1) from those
4297 * of A(2) and higher, because A(0)=seed has a different
4298 * length than the other A(i). */
4299 if( tls12_prf->block_number == 1 )
4300 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004301 Ai = tls12_prf->Ai_with_seed + hash_length;
4302 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004303 }
4304 else
4305 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004306 Ai = tls12_prf->Ai_with_seed;
4307 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004308 }
4309
Hanno Becker3b339e22018-11-13 20:56:14 +00004310 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4311 status = psa_hmac_setup_internal( &hmac,
4312 tls12_prf->key,
4313 tls12_prf->key_len,
4314 hash_alg );
4315 if( status != PSA_SUCCESS )
4316 goto cleanup;
4317
4318 status = psa_hash_update( &hmac.hash_ctx,
4319 Ai, Ai_len );
4320 if( status != PSA_SUCCESS )
4321 goto cleanup;
4322
4323 status = psa_hmac_finish_internal( &hmac,
4324 tls12_prf->Ai_with_seed,
4325 hash_length );
4326 if( status != PSA_SUCCESS )
4327 goto cleanup;
4328
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004329 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4330 status = psa_hmac_setup_internal( &hmac,
4331 tls12_prf->key,
4332 tls12_prf->key_len,
4333 hash_alg );
4334 if( status != PSA_SUCCESS )
4335 goto cleanup;
4336
4337 status = psa_hash_update( &hmac.hash_ctx,
4338 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004339 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004340 if( status != PSA_SUCCESS )
4341 goto cleanup;
4342
4343 status = psa_hmac_finish_internal( &hmac,
4344 tls12_prf->output_block,
4345 hash_length );
4346 if( status != PSA_SUCCESS )
4347 goto cleanup;
4348
4349cleanup:
4350
4351 cleanup_status = psa_hmac_abort_internal( &hmac );
4352 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4353 status = cleanup_status;
4354
4355 return( status );
4356}
Janos Follath7742fee2019-06-17 12:58:10 +01004357#else
4358static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4359 psa_tls12_prf_key_derivation_t *tls12_prf,
4360 psa_algorithm_t alg )
4361{
4362 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4363 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004364 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4365 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004366
Janos Follath7742fee2019-06-17 12:58:10 +01004367 /* We can't be wanting more output after block 0xff, otherwise
4368 * the capacity check in psa_key_derivation_output_bytes() would have
4369 * prevented this call. It could happen only if the operation
4370 * object was corrupted or if this function is called directly
4371 * inside the library. */
4372 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004373 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004374
4375 /* We need a new block */
4376 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004377 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004378
4379 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4380 *
4381 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4382 *
4383 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4384 * HMAC_hash(secret, A(2) + seed) +
4385 * HMAC_hash(secret, A(3) + seed) + ...
4386 *
4387 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004388 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004389 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004390 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004391 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004392 * is currently extracted as `output_block` and where i is
4393 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004394 */
4395
Janos Follathea29bfb2019-06-19 12:21:20 +01004396 /* Save the hash context before using it, to preserve the hash state with
4397 * only the inner padding in it. We need this, because inner padding depends
4398 * on the key (secret in the RFC's terminology). */
4399 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4400 if( status != PSA_SUCCESS )
4401 goto cleanup;
4402
4403 /* Calculate A(i) where i = tls12_prf->block_number. */
4404 if( tls12_prf->block_number == 1 )
4405 {
4406 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4407 * the variable seed and in this instance means it in the context of the
4408 * P_hash function, where seed = label + seed.) */
4409 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4410 tls12_prf->label, tls12_prf->label_length );
4411 if( status != PSA_SUCCESS )
4412 goto cleanup;
4413 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4414 tls12_prf->seed, tls12_prf->seed_length );
4415 if( status != PSA_SUCCESS )
4416 goto cleanup;
4417 }
4418 else
4419 {
4420 /* A(i) = HMAC_hash(secret, A(i-1)) */
4421 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4422 tls12_prf->Ai, hash_length );
4423 if( status != PSA_SUCCESS )
4424 goto cleanup;
4425 }
4426
4427 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4428 tls12_prf->Ai, hash_length );
4429 if( status != PSA_SUCCESS )
4430 goto cleanup;
4431 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4432 if( status != PSA_SUCCESS )
4433 goto cleanup;
4434
4435 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4436 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4437 tls12_prf->Ai, hash_length );
4438 if( status != PSA_SUCCESS )
4439 goto cleanup;
4440 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4441 tls12_prf->label, tls12_prf->label_length );
4442 if( status != PSA_SUCCESS )
4443 goto cleanup;
4444 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4445 tls12_prf->seed, tls12_prf->seed_length );
4446 if( status != PSA_SUCCESS )
4447 goto cleanup;
4448 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4449 tls12_prf->output_block, hash_length );
4450 if( status != PSA_SUCCESS )
4451 goto cleanup;
4452 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4453 if( status != PSA_SUCCESS )
4454 goto cleanup;
4455
Janos Follath7742fee2019-06-17 12:58:10 +01004456
4457cleanup:
4458
Janos Follathea29bfb2019-06-19 12:21:20 +01004459 cleanup_status = psa_hash_abort( &backup );
4460 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4461 status = cleanup_status;
4462
4463 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004464}
Janos Follath999f6482019-06-11 12:04:10 +01004465#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004466
Janos Follath999f6482019-06-11 12:04:10 +01004467#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004468/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004469 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004470static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004471 psa_tls12_prf_key_derivation_t *tls12_prf,
4472 psa_algorithm_t alg,
4473 uint8_t *output,
4474 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004475{
4476 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4477 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4478 psa_status_t status;
4479
4480 while( output_length != 0 )
4481 {
4482 /* Copy what remains of the current block */
4483 uint8_t n = hash_length - tls12_prf->offset_in_block;
4484
4485 /* Check if we have fully processed the current block. */
4486 if( n == 0 )
4487 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004488 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004489 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004490 if( status != PSA_SUCCESS )
4491 return( status );
4492
4493 continue;
4494 }
4495
4496 if( n > output_length )
4497 n = (uint8_t) output_length;
4498 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4499 n );
4500 output += n;
4501 output_length -= n;
4502 tls12_prf->offset_in_block += n;
4503 }
4504
4505 return( PSA_SUCCESS );
4506}
Janos Follath844eb0e2019-06-19 12:10:49 +01004507#else
4508static psa_status_t psa_key_derivation_tls12_prf_read(
4509 psa_tls12_prf_key_derivation_t *tls12_prf,
4510 psa_algorithm_t alg,
4511 uint8_t *output,
4512 size_t output_length )
4513{
4514 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4515 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4516 psa_status_t status;
4517 uint8_t offset, length;
4518
4519 while( output_length != 0 )
4520 {
4521 /* Check if we have fully processed the current block. */
4522 if( tls12_prf->left_in_block == 0 )
4523 {
4524 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4525 alg );
4526 if( status != PSA_SUCCESS )
4527 return( status );
4528
4529 continue;
4530 }
4531
4532 if( tls12_prf->left_in_block > output_length )
4533 length = (uint8_t) output_length;
4534 else
4535 length = tls12_prf->left_in_block;
4536
4537 offset = hash_length - tls12_prf->left_in_block;
4538 memcpy( output, tls12_prf->output_block + offset, length );
4539 output += length;
4540 output_length -= length;
4541 tls12_prf->left_in_block -= length;
4542 }
4543
4544 return( PSA_SUCCESS );
4545}
Janos Follath999f6482019-06-11 12:04:10 +01004546#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004547#endif /* MBEDTLS_MD_C */
4548
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004549psa_status_t psa_key_derivation_output_bytes(
4550 psa_key_derivation_operation_t *operation,
4551 uint8_t *output,
4552 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004553{
4554 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004558 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004560 return PSA_ERROR_BAD_STATE;
4561 }
4562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004564 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004565 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004566 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004568 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004569 goto exit;
4570 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004571 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004572 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004573 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004574 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004575 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4576 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004578 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004579 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004580 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004582
Gilles Peskinebef7f142018-07-12 17:22:21 +02004583#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004584 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004585 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004586 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004588 output, output_length );
4589 }
Janos Follathadbec812019-06-14 11:05:39 +01004590 else
Janos Follathadbec812019-06-14 11:05:39 +01004591 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004592 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004593 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004595 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004596 output_length );
4597 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004598 else
4599#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004600 {
4601 return( PSA_ERROR_BAD_STATE );
4602 }
4603
4604exit:
4605 if( status != PSA_SUCCESS )
4606 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004607 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 * This allows us to differentiate between exhausted operations and
4609 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4610 * operations. */
4611 psa_algorithm_t alg = operation->alg;
4612 psa_key_derivation_abort( operation );
4613 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004614 memset( output, '!', output_length );
4615 }
4616 return( status );
4617}
4618
Gilles Peskine08542d82018-07-19 17:05:42 +02004619#if defined(MBEDTLS_DES_C)
4620static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4621{
4622 if( data_size >= 8 )
4623 mbedtls_des_key_set_parity( data );
4624 if( data_size >= 16 )
4625 mbedtls_des_key_set_parity( data + 8 );
4626 if( data_size >= 24 )
4627 mbedtls_des_key_set_parity( data + 16 );
4628}
4629#endif /* MBEDTLS_DES_C */
4630
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004631static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004632 psa_key_slot_t *slot,
4633 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004635{
4636 uint8_t *data = NULL;
4637 size_t bytes = PSA_BITS_TO_BYTES( bits );
4638 psa_status_t status;
4639
4640 if( ! key_type_is_raw_bytes( slot->type ) )
4641 return( PSA_ERROR_INVALID_ARGUMENT );
4642 if( bits % 8 != 0 )
4643 return( PSA_ERROR_INVALID_ARGUMENT );
4644 data = mbedtls_calloc( 1, bytes );
4645 if( data == NULL )
4646 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004649 if( status != PSA_SUCCESS )
4650 goto exit;
4651#if defined(MBEDTLS_DES_C)
4652 if( slot->type == PSA_KEY_TYPE_DES )
4653 psa_des_set_key_parity( data, bytes );
4654#endif /* MBEDTLS_DES_C */
4655 status = psa_import_key_into_slot( slot, data, bytes );
4656
4657exit:
4658 mbedtls_free( data );
4659 return( status );
4660}
4661
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004662psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004664 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004665{
4666 psa_status_t status;
4667 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004668 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004669 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004670#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4671 if( driver != NULL )
4672 {
4673 /* Deriving a key in a secure element is not implemented yet. */
4674 status = PSA_ERROR_NOT_SUPPORTED;
4675 }
4676#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 if( status == PSA_SUCCESS )
4678 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004679 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004680 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004682 }
4683 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004684 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 if( status != PSA_SUCCESS )
4686 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004687 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004688 *handle = 0;
4689 }
4690 return( status );
4691}
4692
Gilles Peskineeab56e42018-07-12 17:12:33 +02004693
4694
4695/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004696/* Key derivation */
4697/****************************************************************/
4698
Gilles Peskinea05219c2018-11-16 16:02:56 +01004699#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004700#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004702 * of the HKDF algorithm.
4703 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004704 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004705 * to potentially free embedded data structures and wipe confidential data.
4706 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004707static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004708 const uint8_t *secret,
4709 size_t secret_length,
4710 psa_algorithm_t hash_alg,
4711 const uint8_t *salt,
4712 size_t salt_length,
4713 const uint8_t *label,
4714 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004715{
4716 psa_status_t status;
4717 status = psa_hmac_setup_internal( &hkdf->hmac,
4718 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004719 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004720 if( status != PSA_SUCCESS )
4721 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004722 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004723 if( status != PSA_SUCCESS )
4724 return( status );
4725 status = psa_hmac_finish_internal( &hkdf->hmac,
4726 hkdf->prk,
4727 sizeof( hkdf->prk ) );
4728 if( status != PSA_SUCCESS )
4729 return( status );
4730 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4731 hkdf->block_number = 0;
4732 hkdf->info_length = label_length;
4733 if( label_length != 0 )
4734 {
4735 hkdf->info = mbedtls_calloc( 1, label_length );
4736 if( hkdf->info == NULL )
4737 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4738 memcpy( hkdf->info, label, label_length );
4739 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004740 hkdf->state = HKDF_STATE_KEYED;
4741 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004742 return( PSA_SUCCESS );
4743}
Janos Follath71a4c912019-06-11 09:14:47 +01004744#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004745#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004746
Gilles Peskinea05219c2018-11-16 16:02:56 +01004747#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004748#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004750 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004751 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004752 * to potentially free embedded data structures and wipe confidential data.
4753 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004754static psa_status_t psa_key_derivation_tls12_prf_setup(
4755 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004756 const unsigned char *key,
4757 size_t key_len,
4758 psa_algorithm_t hash_alg,
4759 const uint8_t *salt,
4760 size_t salt_length,
4761 const uint8_t *label,
4762 size_t label_length )
4763{
4764 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004765 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4766 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004767
4768 tls12_prf->key = mbedtls_calloc( 1, key_len );
4769 if( tls12_prf->key == NULL )
4770 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4771 tls12_prf->key_len = key_len;
4772 memcpy( tls12_prf->key, key, key_len );
4773
Hanno Becker580fba12018-11-13 20:50:45 +00004774 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004775 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004776 if( overflow )
4777 return( PSA_ERROR_INVALID_ARGUMENT );
4778
4779 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4780 if( tls12_prf->Ai_with_seed == NULL )
4781 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4782 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4783
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004784 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4785 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004786 if( label_length != 0 )
4787 {
4788 memcpy( tls12_prf->Ai_with_seed + hash_length,
4789 label, label_length );
4790 }
4791
4792 if( salt_length != 0 )
4793 {
4794 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4795 salt, salt_length );
4796 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004797
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004798 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004799 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004800 tls12_prf->block_number = 0;
4801 tls12_prf->offset_in_block = hash_length;
4802
4803 return( PSA_SUCCESS );
4804}
Janos Follath71a4c912019-06-11 09:14:47 +01004805#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004806
Janos Follath71a4c912019-06-11 09:14:47 +01004807#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004809static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4810 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004811 const unsigned char *psk,
4812 size_t psk_len,
4813 psa_algorithm_t hash_alg,
4814 const uint8_t *salt,
4815 size_t salt_length,
4816 const uint8_t *label,
4817 size_t label_length )
4818{
4819 psa_status_t status;
4820 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4821
4822 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4823 return( PSA_ERROR_INVALID_ARGUMENT );
4824
4825 /* Quoting RFC 4279, Section 2:
4826 *
4827 * The premaster secret is formed as follows: if the PSK is N octets
4828 * long, concatenate a uint16 with the value N, N zero octets, a second
4829 * uint16 with the value N, and the PSK itself.
4830 */
4831
4832 pms[0] = ( psk_len >> 8 ) & 0xff;
4833 pms[1] = ( psk_len >> 0 ) & 0xff;
4834 memset( pms + 2, 0, psk_len );
4835 pms[2 + psk_len + 0] = pms[0];
4836 pms[2 + psk_len + 1] = pms[1];
4837 memcpy( pms + 4 + psk_len, psk, psk_len );
4838
Gilles Peskinecbe66502019-05-16 16:59:18 +02004839 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004840 pms, 4 + 2 * psk_len,
4841 hash_alg,
4842 salt, salt_length,
4843 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004844
Gilles Peskine3f108122018-12-07 18:14:53 +01004845 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004846 return( status );
4847}
Janos Follath71a4c912019-06-11 09:14:47 +01004848#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004849#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004850
Janos Follath71a4c912019-06-11 09:14:47 +01004851#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004852/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004853 * to potentially free embedded data structures and wipe confidential data.
4854 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004855static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004857 const uint8_t *secret, size_t secret_length,
4858 psa_algorithm_t alg,
4859 const uint8_t *salt, size_t salt_length,
4860 const uint8_t *label, size_t label_length,
4861 size_t capacity )
4862{
4863 psa_status_t status;
4864 size_t max_capacity;
4865
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 /* Set operation->alg even on failure so that abort knows what to do. */
4867 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004868
4869#if defined(MBEDTLS_MD_C)
4870 if( PSA_ALG_IS_HKDF( alg ) )
4871 {
4872 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4873 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4874 if( hash_size == 0 )
4875 return( PSA_ERROR_NOT_SUPPORTED );
4876 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004878 secret, secret_length,
4879 hash_alg,
4880 salt, salt_length,
4881 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004882 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004883 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4884 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4885 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004886 {
4887 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4888 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4889
4890 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4891 if( hash_alg != PSA_ALG_SHA_256 &&
4892 hash_alg != PSA_ALG_SHA_384 )
4893 {
4894 return( PSA_ERROR_NOT_SUPPORTED );
4895 }
4896
4897 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004898
4899 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4900 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004902 secret, secret_length,
4903 hash_alg, salt, salt_length,
4904 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004905 }
4906 else
4907 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004908 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004910 secret, secret_length,
4911 hash_alg, salt, salt_length,
4912 label, label_length );
4913 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004914 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004915 else
4916#endif
4917 {
4918 return( PSA_ERROR_NOT_SUPPORTED );
4919 }
4920
4921 if( status != PSA_SUCCESS )
4922 return( status );
4923
4924 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004926 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004928 else
4929 return( PSA_ERROR_INVALID_ARGUMENT );
4930
4931 return( PSA_SUCCESS );
4932}
Janos Follath71a4c912019-06-11 09:14:47 +01004933#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004934
Janos Follath71a4c912019-06-11 09:14:47 +01004935#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004937 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004938 psa_algorithm_t alg,
4939 const uint8_t *salt,
4940 size_t salt_length,
4941 const uint8_t *label,
4942 size_t label_length,
4943 size_t capacity )
4944{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004945 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004946 psa_status_t status;
4947
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004949 return( PSA_ERROR_BAD_STATE );
4950
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004951 /* Make sure that alg is a key derivation algorithm. This prevents
4952 * key selection algorithms, which psa_key_derivation_internal
4953 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004954 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4955 return( PSA_ERROR_INVALID_ARGUMENT );
4956
Gilles Peskine28f8f302019-07-24 13:30:31 +02004957 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004958 if( status != PSA_SUCCESS )
4959 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004960
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004961 if( slot->type != PSA_KEY_TYPE_DERIVE )
4962 return( PSA_ERROR_INVALID_ARGUMENT );
4963
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004965 slot->data.raw.data,
4966 slot->data.raw.bytes,
4967 alg,
4968 salt, salt_length,
4969 label, label_length,
4970 capacity );
4971 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004973 return( status );
4974}
Janos Follath71a4c912019-06-11 09:14:47 +01004975#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004976
Gilles Peskine969c5d62019-01-16 15:53:06 +01004977static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004979 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980{
Janos Follath5fe19732019-06-20 15:09:30 +01004981 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4982 * initialisers for this union leave some bytes unspecified.) */
4983 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4984
Gilles Peskine969c5d62019-01-16 15:53:06 +01004985 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004986#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004987 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4988 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4989 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004990 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004991 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004992 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4993 if( hash_size == 0 )
4994 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4996 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004997 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004998 {
4999 return( PSA_ERROR_NOT_SUPPORTED );
5000 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005002 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005003 }
5004#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005005 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005006 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005007}
5008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005010 psa_algorithm_t alg )
5011{
5012 psa_status_t status;
5013
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005015 return( PSA_ERROR_BAD_STATE );
5016
Gilles Peskine6843c292019-01-18 16:44:49 +01005017 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5018 return( PSA_ERROR_INVALID_ARGUMENT );
5019 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005020 {
5021 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005023 }
5024 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5025 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005027 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005028 else
5029 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005030
5031 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005033 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005034}
5035
5036#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005037static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038 psa_algorithm_t hash_alg,
5039 psa_key_derivation_step_t step,
5040 const uint8_t *data,
5041 size_t data_length )
5042{
5043 psa_status_t status;
5044 switch( step )
5045 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005046 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005047 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005048 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005049 status = psa_hmac_setup_internal( &hkdf->hmac,
5050 data, data_length,
5051 hash_alg );
5052 if( status != PSA_SUCCESS )
5053 return( status );
5054 hkdf->state = HKDF_STATE_STARTED;
5055 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005056 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005057 /* If no salt was provided, use an empty salt. */
5058 if( hkdf->state == HKDF_STATE_INIT )
5059 {
5060 status = psa_hmac_setup_internal( &hkdf->hmac,
5061 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005062 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005063 if( status != PSA_SUCCESS )
5064 return( status );
5065 hkdf->state = HKDF_STATE_STARTED;
5066 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005067 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005068 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005069 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5070 data, data_length );
5071 if( status != PSA_SUCCESS )
5072 return( status );
5073 status = psa_hmac_finish_internal( &hkdf->hmac,
5074 hkdf->prk,
5075 sizeof( hkdf->prk ) );
5076 if( status != PSA_SUCCESS )
5077 return( status );
5078 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5079 hkdf->block_number = 0;
5080 hkdf->state = HKDF_STATE_KEYED;
5081 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005082 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005083 if( hkdf->state == HKDF_STATE_OUTPUT )
5084 return( PSA_ERROR_BAD_STATE );
5085 if( hkdf->info_set )
5086 return( PSA_ERROR_BAD_STATE );
5087 hkdf->info_length = data_length;
5088 if( data_length != 0 )
5089 {
5090 hkdf->info = mbedtls_calloc( 1, data_length );
5091 if( hkdf->info == NULL )
5092 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5093 memcpy( hkdf->info, data, data_length );
5094 }
5095 hkdf->info_set = 1;
5096 return( PSA_SUCCESS );
5097 default:
5098 return( PSA_ERROR_INVALID_ARGUMENT );
5099 }
5100}
Janos Follathb03233e2019-06-11 15:30:30 +01005101
5102#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5103static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5104 psa_algorithm_t hash_alg,
5105 psa_key_derivation_step_t step,
5106 const uint8_t *data,
5107 size_t data_length )
5108{
5109 (void) prf;
5110 (void) hash_alg;
5111 (void) step;
5112 (void) data;
5113 (void) data_length;
5114
5115 return( PSA_ERROR_INVALID_ARGUMENT );
5116}
Janos Follath6660f0e2019-06-17 08:44:03 +01005117
5118static psa_status_t psa_tls12_prf_psk_to_ms_input(
5119 psa_tls12_prf_key_derivation_t *prf,
5120 psa_algorithm_t hash_alg,
5121 psa_key_derivation_step_t step,
5122 const uint8_t *data,
5123 size_t data_length )
5124{
5125 (void) prf;
5126 (void) hash_alg;
5127 (void) step;
5128 (void) data;
5129 (void) data_length;
5130
5131 return( PSA_ERROR_INVALID_ARGUMENT );
5132}
Janos Follathb03233e2019-06-11 15:30:30 +01005133#else
Janos Follathf08e2652019-06-13 09:05:41 +01005134static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5135 const uint8_t *data,
5136 size_t data_length )
5137{
5138 if( prf->state != TLS12_PRF_STATE_INIT )
5139 return( PSA_ERROR_BAD_STATE );
5140
Janos Follathd6dce9f2019-07-04 09:11:38 +01005141 if( data_length != 0 )
5142 {
5143 prf->seed = mbedtls_calloc( 1, data_length );
5144 if( prf->seed == NULL )
5145 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005146
Janos Follathd6dce9f2019-07-04 09:11:38 +01005147 memcpy( prf->seed, data, data_length );
5148 prf->seed_length = data_length;
5149 }
Janos Follathf08e2652019-06-13 09:05:41 +01005150
5151 prf->state = TLS12_PRF_STATE_SEED_SET;
5152
5153 return( PSA_SUCCESS );
5154}
5155
Janos Follath81550542019-06-13 14:26:34 +01005156static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5157 psa_algorithm_t hash_alg,
5158 const uint8_t *data,
5159 size_t data_length )
5160{
5161 psa_status_t status;
5162 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5163 return( PSA_ERROR_BAD_STATE );
5164
5165 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5166 if( status != PSA_SUCCESS )
5167 return( status );
5168
5169 prf->state = TLS12_PRF_STATE_KEY_SET;
5170
5171 return( PSA_SUCCESS );
5172}
5173
Janos Follath6660f0e2019-06-17 08:44:03 +01005174static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5175 psa_tls12_prf_key_derivation_t *prf,
5176 psa_algorithm_t hash_alg,
5177 const uint8_t *data,
5178 size_t data_length )
5179{
5180 psa_status_t status;
5181 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005182 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005183
5184 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5185 return( PSA_ERROR_INVALID_ARGUMENT );
5186
5187 /* Quoting RFC 4279, Section 2:
5188 *
5189 * The premaster secret is formed as follows: if the PSK is N octets
5190 * long, concatenate a uint16 with the value N, N zero octets, a second
5191 * uint16 with the value N, and the PSK itself.
5192 */
5193
Janos Follath40e13932019-06-26 13:22:29 +01005194 *cur++ = ( data_length >> 8 ) & 0xff;
5195 *cur++ = ( data_length >> 0 ) & 0xff;
5196 memset( cur, 0, data_length );
5197 cur += data_length;
5198 *cur++ = pms[0];
5199 *cur++ = pms[1];
5200 memcpy( cur, data, data_length );
5201 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005202
Janos Follath40e13932019-06-26 13:22:29 +01005203 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005204
5205 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5206 return( status );
5207}
5208
Janos Follath63028dd2019-06-13 09:15:47 +01005209static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5210 const uint8_t *data,
5211 size_t data_length )
5212{
5213 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5214 return( PSA_ERROR_BAD_STATE );
5215
Janos Follathd6dce9f2019-07-04 09:11:38 +01005216 if( data_length != 0 )
5217 {
5218 prf->label = mbedtls_calloc( 1, data_length );
5219 if( prf->label == NULL )
5220 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005221
Janos Follathd6dce9f2019-07-04 09:11:38 +01005222 memcpy( prf->label, data, data_length );
5223 prf->label_length = data_length;
5224 }
Janos Follath63028dd2019-06-13 09:15:47 +01005225
5226 prf->state = TLS12_PRF_STATE_LABEL_SET;
5227
5228 return( PSA_SUCCESS );
5229}
5230
Janos Follathb03233e2019-06-11 15:30:30 +01005231static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5232 psa_algorithm_t hash_alg,
5233 psa_key_derivation_step_t step,
5234 const uint8_t *data,
5235 size_t data_length )
5236{
Janos Follathb03233e2019-06-11 15:30:30 +01005237 switch( step )
5238 {
Janos Follathf08e2652019-06-13 09:05:41 +01005239 case PSA_KEY_DERIVATION_INPUT_SEED:
5240 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005241 case PSA_KEY_DERIVATION_INPUT_SECRET:
5242 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005243 case PSA_KEY_DERIVATION_INPUT_LABEL:
5244 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005245 default:
5246 return( PSA_ERROR_INVALID_ARGUMENT );
5247 }
5248}
Janos Follath6660f0e2019-06-17 08:44:03 +01005249
5250static psa_status_t psa_tls12_prf_psk_to_ms_input(
5251 psa_tls12_prf_key_derivation_t *prf,
5252 psa_algorithm_t hash_alg,
5253 psa_key_derivation_step_t step,
5254 const uint8_t *data,
5255 size_t data_length )
5256{
5257 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005258 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005259 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5260 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005261 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005262
5263 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5264}
Janos Follathb03233e2019-06-11 15:30:30 +01005265#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005266#endif /* MBEDTLS_MD_C */
5267
Janos Follathb80a94e2019-06-12 15:54:46 +01005268static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005270 psa_key_derivation_step_t step,
5271 const uint8_t *data,
5272 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273{
5274 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276
5277#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005278 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005281 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005282 step, data, data_length );
5283 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005284 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005285#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005287 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 {
Janos Follathb03233e2019-06-11 15:30:30 +01005289 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5290 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5291 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005292 }
5293 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5294 {
5295 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5296 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5297 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298 }
5299 else
5300#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005303 return( PSA_ERROR_BAD_STATE );
5304 }
5305
5306 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005308 return( status );
5309}
5310
Janos Follath51f4a0f2019-06-14 11:35:55 +01005311psa_status_t psa_key_derivation_input_bytes(
5312 psa_key_derivation_operation_t *operation,
5313 psa_key_derivation_step_t step,
5314 const uint8_t *data,
5315 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005316{
Janos Follathc5621512019-06-14 11:27:57 +01005317 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5318 return( PSA_ERROR_INVALID_ARGUMENT );
5319
5320 return( psa_key_derivation_input_internal( operation, step,
5321 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005322}
5323
Janos Follath51f4a0f2019-06-14 11:35:55 +01005324psa_status_t psa_key_derivation_input_key(
5325 psa_key_derivation_operation_t *operation,
5326 psa_key_derivation_step_t step,
5327 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005328{
5329 psa_key_slot_t *slot;
5330 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005331 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005332 PSA_KEY_USAGE_DERIVE,
5333 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005334 if( status != PSA_SUCCESS )
5335 return( status );
5336 if( slot->type != PSA_KEY_TYPE_DERIVE )
5337 return( PSA_ERROR_INVALID_ARGUMENT );
5338 /* Don't allow a key to be used as an input that is usually public.
5339 * This is debatable. It's ok from a cryptographic perspective to
5340 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005341 * the material should be dedicated to a particular input step,
5342 * otherwise this may allow the key to be used in an unintended way
5343 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005344 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005345 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005346 return( psa_key_derivation_input_internal( operation,
5347 step,
5348 slot->data.raw.data,
5349 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350}
5351
Gilles Peskineea0fb492018-07-12 17:17:20 +02005352
5353
5354/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005355/* Key agreement */
5356/****************************************************************/
5357
Gilles Peskinea05219c2018-11-16 16:02:56 +01005358#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005359static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5360 size_t peer_key_length,
5361 const mbedtls_ecp_keypair *our_key,
5362 uint8_t *shared_secret,
5363 size_t shared_secret_size,
5364 size_t *shared_secret_length )
5365{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005366 mbedtls_ecp_keypair *their_key = NULL;
5367 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005368 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005369 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005370
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005371 status = psa_import_ec_public_key(
5372 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5373 peer_key, peer_key_length,
5374 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005375 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005376 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005377
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005378 status = mbedtls_to_psa_error(
5379 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5380 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005381 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005382 status = mbedtls_to_psa_error(
5383 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5384 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005385 goto exit;
5386
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005387 status = mbedtls_to_psa_error(
5388 mbedtls_ecdh_calc_secret( &ecdh,
5389 shared_secret_length,
5390 shared_secret, shared_secret_size,
5391 mbedtls_ctr_drbg_random,
5392 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005393
5394exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005395 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005396 mbedtls_ecp_keypair_free( their_key );
5397 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005398 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005399}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005400#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005401
Gilles Peskine01d718c2018-09-18 12:01:02 +02005402#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5403
Gilles Peskine0216fe12019-04-11 21:23:21 +02005404static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5405 psa_key_slot_t *private_key,
5406 const uint8_t *peer_key,
5407 size_t peer_key_length,
5408 uint8_t *shared_secret,
5409 size_t shared_secret_size,
5410 size_t *shared_secret_length )
5411{
5412 switch( alg )
5413 {
5414#if defined(MBEDTLS_ECDH_C)
5415 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005416 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005417 return( PSA_ERROR_INVALID_ARGUMENT );
5418 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5419 private_key->data.ecp,
5420 shared_secret, shared_secret_size,
5421 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005422#endif /* MBEDTLS_ECDH_C */
5423 default:
5424 (void) private_key;
5425 (void) peer_key;
5426 (void) peer_key_length;
5427 (void) shared_secret;
5428 (void) shared_secret_size;
5429 (void) shared_secret_length;
5430 return( PSA_ERROR_NOT_SUPPORTED );
5431 }
5432}
5433
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005434/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005435 * to potentially free embedded data structures and wipe confidential data.
5436 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005438 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005439 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005440 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005441 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005442{
5443 psa_status_t status;
5444 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5445 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005447
5448 /* Step 1: run the secret agreement algorithm to generate the shared
5449 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005450 status = psa_key_agreement_raw_internal( ka_alg,
5451 private_key,
5452 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005453 shared_secret,
5454 sizeof( shared_secret ),
5455 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005456 if( status != PSA_SUCCESS )
5457 goto exit;
5458
5459 /* Step 2: set up the key derivation to generate key material from
5460 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005461 status = psa_key_derivation_input_internal( operation, step,
5462 shared_secret,
5463 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005464
Gilles Peskine01d718c2018-09-18 12:01:02 +02005465exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005466 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005467 return( status );
5468}
5469
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005471 psa_key_derivation_step_t step,
5472 psa_key_handle_t private_key,
5473 const uint8_t *peer_key,
5474 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005475{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005476 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005477 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005479 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005480 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005481 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005482 if( status != PSA_SUCCESS )
5483 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005485 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005486 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005487 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005489 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005490}
5491
Gilles Peskinebe697d82019-05-16 18:00:41 +02005492psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5493 psa_key_handle_t private_key,
5494 const uint8_t *peer_key,
5495 size_t peer_key_length,
5496 uint8_t *output,
5497 size_t output_size,
5498 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005499{
5500 psa_key_slot_t *slot;
5501 psa_status_t status;
5502
5503 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5504 {
5505 status = PSA_ERROR_INVALID_ARGUMENT;
5506 goto exit;
5507 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005508 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005509 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005510 if( status != PSA_SUCCESS )
5511 goto exit;
5512
5513 status = psa_key_agreement_raw_internal( alg, slot,
5514 peer_key, peer_key_length,
5515 output, output_size,
5516 output_length );
5517
5518exit:
5519 if( status != PSA_SUCCESS )
5520 {
5521 /* If an error happens and is not handled properly, the output
5522 * may be used as a key to protect sensitive data. Arrange for such
5523 * a key to be random, which is likely to result in decryption or
5524 * verification errors. This is better than filling the buffer with
5525 * some constant data such as zeros, which would result in the data
5526 * being protected with a reproducible, easily knowable key.
5527 */
5528 psa_generate_random( output, output_size );
5529 *output_length = output_size;
5530 }
5531 return( status );
5532}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005533
5534
5535/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005536/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005537/****************************************************************/
5538
5539psa_status_t psa_generate_random( uint8_t *output,
5540 size_t output_size )
5541{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005542 int ret;
5543 GUARD_MODULE_INITIALIZED;
5544
5545 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005546 return( mbedtls_to_psa_error( ret ) );
5547}
5548
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005549#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5550#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005551
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005552psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5553 size_t seed_size )
5554{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005555 if( global_data.initialized )
5556 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005557
5558 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5559 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5560 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5561 return( PSA_ERROR_INVALID_ARGUMENT );
5562
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005563 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005564}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005565#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005566
Gilles Peskinee56e8782019-04-26 17:34:02 +02005567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5568static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5569 size_t domain_parameters_size,
5570 int *exponent )
5571{
5572 size_t i;
5573 uint32_t acc = 0;
5574
5575 if( domain_parameters_size == 0 )
5576 {
5577 *exponent = 65537;
5578 return( PSA_SUCCESS );
5579 }
5580
5581 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5582 * support values that fit in a 32-bit integer, which is larger than
5583 * int on just about every platform anyway. */
5584 if( domain_parameters_size > sizeof( acc ) )
5585 return( PSA_ERROR_NOT_SUPPORTED );
5586 for( i = 0; i < domain_parameters_size; i++ )
5587 acc = ( acc << 8 ) | domain_parameters[i];
5588 if( acc > INT_MAX )
5589 return( PSA_ERROR_NOT_SUPPORTED );
5590 *exponent = acc;
5591 return( PSA_SUCCESS );
5592}
5593#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5594
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005595static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005596 psa_key_slot_t *slot, size_t bits,
5597 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005598{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005599 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005600
Gilles Peskinee56e8782019-04-26 17:34:02 +02005601 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005602 return( PSA_ERROR_INVALID_ARGUMENT );
5603
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005604 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005605 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005606 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005607 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005608 if( status != PSA_SUCCESS )
5609 return( status );
5610 status = psa_generate_random( slot->data.raw.data,
5611 slot->data.raw.bytes );
5612 if( status != PSA_SUCCESS )
5613 {
5614 mbedtls_free( slot->data.raw.data );
5615 return( status );
5616 }
5617#if defined(MBEDTLS_DES_C)
5618 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005619 psa_des_set_key_parity( slot->data.raw.data,
5620 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005621#endif /* MBEDTLS_DES_C */
5622 }
5623 else
5624
5625#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005626 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627 {
5628 mbedtls_rsa_context *rsa;
5629 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005630 int exponent;
5631 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005632 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5633 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005634 /* Accept only byte-aligned keys, for the same reasons as
5635 * in psa_import_rsa_key(). */
5636 if( bits % 8 != 0 )
5637 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005638 status = psa_read_rsa_exponent( domain_parameters,
5639 domain_parameters_size,
5640 &exponent );
5641 if( status != PSA_SUCCESS )
5642 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005643 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5644 if( rsa == NULL )
5645 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5646 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5647 ret = mbedtls_rsa_gen_key( rsa,
5648 mbedtls_ctr_drbg_random,
5649 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005650 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005651 exponent );
5652 if( ret != 0 )
5653 {
5654 mbedtls_rsa_free( rsa );
5655 mbedtls_free( rsa );
5656 return( mbedtls_to_psa_error( ret ) );
5657 }
5658 slot->data.rsa = rsa;
5659 }
5660 else
5661#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5662
5663#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005664 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005665 {
5666 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5667 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5668 const mbedtls_ecp_curve_info *curve_info =
5669 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5670 mbedtls_ecp_keypair *ecp;
5671 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005672 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005673 return( PSA_ERROR_NOT_SUPPORTED );
5674 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5675 return( PSA_ERROR_NOT_SUPPORTED );
5676 if( curve_info->bit_size != bits )
5677 return( PSA_ERROR_INVALID_ARGUMENT );
5678 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5679 if( ecp == NULL )
5680 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5681 mbedtls_ecp_keypair_init( ecp );
5682 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5683 mbedtls_ctr_drbg_random,
5684 &global_data.ctr_drbg );
5685 if( ret != 0 )
5686 {
5687 mbedtls_ecp_keypair_free( ecp );
5688 mbedtls_free( ecp );
5689 return( mbedtls_to_psa_error( ret ) );
5690 }
5691 slot->data.ecp = ecp;
5692 }
5693 else
5694#endif /* MBEDTLS_ECP_C */
5695
5696 return( PSA_ERROR_NOT_SUPPORTED );
5697
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005698 return( PSA_SUCCESS );
5699}
5700
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005701psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005702 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005703{
5704 psa_status_t status;
5705 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005706 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005707 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005708#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5709 if( driver != NULL )
5710 {
5711 /* Generating a key in a secure element is not implemented yet. */
5712 status = PSA_ERROR_NOT_SUPPORTED;
5713 }
5714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005715 if( status == PSA_SUCCESS )
5716 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005717 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005718 slot, attributes->bits,
5719 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005720 }
5721 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005722 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005723 if( status != PSA_SUCCESS )
5724 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005725 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005726 *handle = 0;
5727 }
5728 return( status );
5729}
5730
5731
Gilles Peskine05d69892018-06-19 22:00:52 +02005732
5733/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005734/* Module setup */
5735/****************************************************************/
5736
Gilles Peskine5e769522018-11-20 21:59:56 +01005737psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5738 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5739 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5740{
5741 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5742 return( PSA_ERROR_BAD_STATE );
5743 global_data.entropy_init = entropy_init;
5744 global_data.entropy_free = entropy_free;
5745 return( PSA_SUCCESS );
5746}
5747
Gilles Peskinee59236f2018-01-27 23:32:46 +01005748void mbedtls_psa_crypto_free( void )
5749{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005750 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005751 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5752 {
5753 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005754 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005755 }
5756 /* Wipe all remaining data, including configuration.
5757 * In particular, this sets all state indicator to the value
5758 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005759 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005760#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005761 /* Unregister all secure element drivers, so that we restart from
5762 * a pristine state. */
5763 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005764#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765}
5766
5767psa_status_t psa_crypto_init( void )
5768{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005769 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005770 const unsigned char drbg_seed[] = "PSA";
5771
Gilles Peskinec6b69072018-11-20 21:42:52 +01005772 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005773 if( global_data.initialized != 0 )
5774 return( PSA_SUCCESS );
5775
Gilles Peskine5e769522018-11-20 21:59:56 +01005776 /* Set default configuration if
5777 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5778 if( global_data.entropy_init == NULL )
5779 global_data.entropy_init = mbedtls_entropy_init;
5780 if( global_data.entropy_free == NULL )
5781 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005782
Gilles Peskinec6b69072018-11-20 21:42:52 +01005783 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005784 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005786 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005787 status = mbedtls_to_psa_error(
5788 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5789 mbedtls_entropy_func,
5790 &global_data.entropy,
5791 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5792 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005793 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005794 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005795
Gilles Peskine66fb1262018-12-10 16:29:04 +01005796 status = psa_initialize_key_slots( );
5797 if( status != PSA_SUCCESS )
5798 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005799
Gilles Peskine4b734222019-07-24 15:56:31 +02005800#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005801 status = psa_crypto_load_transaction( );
5802 if( status == PSA_SUCCESS )
5803 {
5804 /*TOnogrepDO: complete or abort the transaction*/
5805 }
5806 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5807 {
5808 /* There's no transaction to complete. It's all good. */
5809 status = PSA_SUCCESS;
5810 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005811#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005812
Gilles Peskinec6b69072018-11-20 21:42:52 +01005813 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005814 global_data.initialized = 1;
5815
Gilles Peskinee59236f2018-01-27 23:32:46 +01005816exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005817 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005819 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005820}
5821
5822#endif /* MBEDTLS_PSA_CRYPTO_C */