blob: 2285694ee75a0ee54a0fa2615c3f8cc749b999ba [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
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100366#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200367static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
368{
369 switch( grpid )
370 {
371 case MBEDTLS_ECP_DP_SECP192R1:
372 return( PSA_ECC_CURVE_SECP192R1 );
373 case MBEDTLS_ECP_DP_SECP224R1:
374 return( PSA_ECC_CURVE_SECP224R1 );
375 case MBEDTLS_ECP_DP_SECP256R1:
376 return( PSA_ECC_CURVE_SECP256R1 );
377 case MBEDTLS_ECP_DP_SECP384R1:
378 return( PSA_ECC_CURVE_SECP384R1 );
379 case MBEDTLS_ECP_DP_SECP521R1:
380 return( PSA_ECC_CURVE_SECP521R1 );
381 case MBEDTLS_ECP_DP_BP256R1:
382 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
383 case MBEDTLS_ECP_DP_BP384R1:
384 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
385 case MBEDTLS_ECP_DP_BP512R1:
386 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
387 case MBEDTLS_ECP_DP_CURVE25519:
388 return( PSA_ECC_CURVE_CURVE25519 );
389 case MBEDTLS_ECP_DP_SECP192K1:
390 return( PSA_ECC_CURVE_SECP192K1 );
391 case MBEDTLS_ECP_DP_SECP224K1:
392 return( PSA_ECC_CURVE_SECP224K1 );
393 case MBEDTLS_ECP_DP_SECP256K1:
394 return( PSA_ECC_CURVE_SECP256K1 );
395 case MBEDTLS_ECP_DP_CURVE448:
396 return( PSA_ECC_CURVE_CURVE448 );
397 default:
398 return( 0 );
399 }
400}
401
Gilles Peskine12313cd2018-06-20 00:20:32 +0200402static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
403{
404 switch( curve )
405 {
406 case PSA_ECC_CURVE_SECP192R1:
407 return( MBEDTLS_ECP_DP_SECP192R1 );
408 case PSA_ECC_CURVE_SECP224R1:
409 return( MBEDTLS_ECP_DP_SECP224R1 );
410 case PSA_ECC_CURVE_SECP256R1:
411 return( MBEDTLS_ECP_DP_SECP256R1 );
412 case PSA_ECC_CURVE_SECP384R1:
413 return( MBEDTLS_ECP_DP_SECP384R1 );
414 case PSA_ECC_CURVE_SECP521R1:
415 return( MBEDTLS_ECP_DP_SECP521R1 );
416 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
417 return( MBEDTLS_ECP_DP_BP256R1 );
418 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
419 return( MBEDTLS_ECP_DP_BP384R1 );
420 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
421 return( MBEDTLS_ECP_DP_BP512R1 );
422 case PSA_ECC_CURVE_CURVE25519:
423 return( MBEDTLS_ECP_DP_CURVE25519 );
424 case PSA_ECC_CURVE_SECP192K1:
425 return( MBEDTLS_ECP_DP_SECP192K1 );
426 case PSA_ECC_CURVE_SECP224K1:
427 return( MBEDTLS_ECP_DP_SECP224K1 );
428 case PSA_ECC_CURVE_SECP256K1:
429 return( MBEDTLS_ECP_DP_SECP256K1 );
430 case PSA_ECC_CURVE_CURVE448:
431 return( MBEDTLS_ECP_DP_CURVE448 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100436#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200438static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
439 size_t bits,
440 struct raw_data *raw )
441{
442 /* Check that the bit size is acceptable for the key type */
443 switch( type )
444 {
445 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200446 if( bits == 0 )
447 {
448 raw->bytes = 0;
449 raw->data = NULL;
450 return( PSA_SUCCESS );
451 }
452 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200453#if defined(MBEDTLS_MD_C)
454 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200455#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200456 case PSA_KEY_TYPE_DERIVE:
457 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200458#if defined(MBEDTLS_AES_C)
459 case PSA_KEY_TYPE_AES:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_CAMELLIA_C)
465 case PSA_KEY_TYPE_CAMELLIA:
466 if( bits != 128 && bits != 192 && bits != 256 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470#if defined(MBEDTLS_DES_C)
471 case PSA_KEY_TYPE_DES:
472 if( bits != 64 && bits != 128 && bits != 192 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476#if defined(MBEDTLS_ARC4_C)
477 case PSA_KEY_TYPE_ARC4:
478 if( bits < 8 || bits > 2048 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
480 break;
481#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200482#if defined(MBEDTLS_CHACHA20_C)
483 case PSA_KEY_TYPE_CHACHA20:
484 if( bits != 256 )
485 return( PSA_ERROR_INVALID_ARGUMENT );
486 break;
487#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 default:
489 return( PSA_ERROR_NOT_SUPPORTED );
490 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200491 if( bits % 8 != 0 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493
494 /* Allocate memory for the key */
495 raw->bytes = PSA_BITS_TO_BYTES( bits );
496 raw->data = mbedtls_calloc( 1, raw->bytes );
497 if( raw->data == NULL )
498 {
499 raw->bytes = 0;
500 return( PSA_ERROR_INSUFFICIENT_MEMORY );
501 }
502 return( PSA_SUCCESS );
503}
504
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200505#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100506/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
507 * that are not a multiple of 8) well. For example, there is only
508 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
509 * way to return the exact bit size of a key.
510 * To keep things simple, reject non-byte-aligned key sizes. */
511static psa_status_t psa_check_rsa_key_byte_aligned(
512 const mbedtls_rsa_context *rsa )
513{
514 mbedtls_mpi n;
515 psa_status_t status;
516 mbedtls_mpi_init( &n );
517 status = mbedtls_to_psa_error(
518 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
519 if( status == PSA_SUCCESS )
520 {
521 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
522 status = PSA_ERROR_NOT_SUPPORTED;
523 }
524 mbedtls_mpi_free( &n );
525 return( status );
526}
527
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000528static psa_status_t psa_import_rsa_key( psa_key_type_t type,
529 const uint8_t *data,
530 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531 mbedtls_rsa_context **p_rsa )
532{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000533 psa_status_t status;
534 mbedtls_pk_context pk;
535 mbedtls_rsa_context *rsa;
536 size_t bits;
537
538 mbedtls_pk_init( &pk );
539
540 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200541 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000542 status = mbedtls_to_psa_error(
543 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000545 status = mbedtls_to_psa_error(
546 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
547 if( status != PSA_SUCCESS )
548 goto exit;
549
550 /* We have something that the pkparse module recognizes. If it is a
551 * valid RSA key, store it. */
552 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200553 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000554 status = PSA_ERROR_INVALID_ARGUMENT;
555 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000557
558 rsa = mbedtls_pk_rsa( pk );
559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
568 status = psa_check_rsa_key_byte_aligned( rsa );
569
570exit:
571 /* Free the content of the pk object only on error. */
572 if( status != PSA_SUCCESS )
573 {
574 mbedtls_pk_free( &pk );
575 return( status );
576 }
577
578 /* On success, store the content of the object in the RSA context. */
579 *p_rsa = rsa;
580
581 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582}
583#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
584
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000585#if defined(MBEDTLS_ECP_C)
586
587/* Import a public key given as the uncompressed representation defined by SEC1
588 * 2.3.3 as the content of an ECPoint. */
589static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
590 const uint8_t *data,
591 size_t data_length,
592 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200593{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000595 mbedtls_ecp_keypair *ecp = NULL;
596 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
597
598 *p_ecp = NULL;
599 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
600 if( ecp == NULL )
601 return( PSA_ERROR_INSUFFICIENT_MEMORY );
602 mbedtls_ecp_keypair_init( ecp );
603
604 /* Load the group. */
605 status = mbedtls_to_psa_error(
606 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
607 if( status != PSA_SUCCESS )
608 goto exit;
609 /* Load the public value. */
610 status = mbedtls_to_psa_error(
611 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
612 data, data_length ) );
613 if( status != PSA_SUCCESS )
614 goto exit;
615
616 /* Check that the point is on the curve. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
619 if( status != PSA_SUCCESS )
620 goto exit;
621
622 *p_ecp = ecp;
623 return( PSA_SUCCESS );
624
625exit:
626 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200627 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000628 mbedtls_ecp_keypair_free( ecp );
629 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200630 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000631 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200632}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000633#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634
Gilles Peskinef76aa772018-10-29 19:24:33 +0100635#if defined(MBEDTLS_ECP_C)
636/* Import a private key given as a byte string which is the private value
637 * in big-endian order. */
638static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
639 const uint8_t *data,
640 size_t data_length,
641 mbedtls_ecp_keypair **p_ecp )
642{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100644 mbedtls_ecp_keypair *ecp = NULL;
645 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
646
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200647 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
648 return( PSA_ERROR_INVALID_ARGUMENT );
649
Gilles Peskinef76aa772018-10-29 19:24:33 +0100650 *p_ecp = NULL;
651 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
652 if( ecp == NULL )
653 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000654 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100655
656 /* Load the group. */
657 status = mbedtls_to_psa_error(
658 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
659 if( status != PSA_SUCCESS )
660 goto exit;
661 /* Load the secret value. */
662 status = mbedtls_to_psa_error(
663 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
664 if( status != PSA_SUCCESS )
665 goto exit;
666 /* Validate the private key. */
667 status = mbedtls_to_psa_error(
668 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
669 if( status != PSA_SUCCESS )
670 goto exit;
671 /* Calculate the public key from the private key. */
672 status = mbedtls_to_psa_error(
673 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
674 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
675 if( status != PSA_SUCCESS )
676 goto exit;
677
678 *p_ecp = ecp;
679 return( PSA_SUCCESS );
680
681exit:
682 if( ecp != NULL )
683 {
684 mbedtls_ecp_keypair_free( ecp );
685 mbedtls_free( ecp );
686 }
687 return( status );
688}
689#endif /* defined(MBEDTLS_ECP_C) */
690
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100691/** Import key data into a slot. `slot->type` must have been set
692 * previously. This function assumes that the slot does not contain
693 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100694psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
695 const uint8_t *data,
696 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200698 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100699
Darryl Green940d72c2018-07-13 13:18:51 +0100700 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100702 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 if( data_length > SIZE_MAX / 8 )
704 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100705 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200706 PSA_BYTES_TO_BITS( data_length ),
707 &slot->data.raw );
708 if( status != PSA_SUCCESS )
709 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200710 if( data_length != 0 )
711 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100714#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200715 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100716 {
Darryl Green940d72c2018-07-13 13:18:51 +0100717 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100718 data, data_length,
719 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100720 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000721 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
722 {
723 status = psa_import_ec_public_key(
724 PSA_KEY_TYPE_GET_CURVE( slot->type ),
725 data, data_length,
726 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000727 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100728 else
729#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000730#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
731 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000733 status = psa_import_rsa_key( slot->type,
734 data, data_length,
735 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 }
737 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000738#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
740 return( PSA_ERROR_NOT_SUPPORTED );
741 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000742 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100743}
744
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100745/** Calculate the intersection of two algorithm usage policies.
746 *
747 * Return 0 (which allows no operation) on incompatibility.
748 */
749static psa_algorithm_t psa_key_policy_algorithm_intersection(
750 psa_algorithm_t alg1,
751 psa_algorithm_t alg2 )
752{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200753 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100754 if( alg1 == alg2 )
755 return( alg1 );
756 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100757 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100758 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
759 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
760 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
761 {
762 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
763 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 return( alg1 );
766 }
767 /* If the policies are incompatible, allow nothing. */
768 return( 0 );
769}
770
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200771static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
772 psa_algorithm_t requested_alg )
773{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200774 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200775 if( requested_alg == policy_alg )
776 return( 1 );
777 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200778 * and requested_alg is the same hash-and-sign family with any hash,
779 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200780 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
781 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
782 {
783 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
784 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
785 }
786 /* If it isn't permitted, it's forbidden. */
787 return( 0 );
788}
789
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100790/** Test whether a policy permits an algorithm.
791 *
792 * The caller must test usage flags separately.
793 */
794static int psa_key_policy_permits( const psa_key_policy_t *policy,
795 psa_algorithm_t alg )
796{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200797 return( psa_key_algorithm_permits( policy->alg, alg ) ||
798 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100799}
800
Gilles Peskinef603c712019-01-19 13:40:11 +0100801/** Restrict a key policy based on a constraint.
802 *
803 * \param[in,out] policy The policy to restrict.
804 * \param[in] constraint The policy constraint to apply.
805 *
806 * \retval #PSA_SUCCESS
807 * \c *policy contains the intersection of the original value of
808 * \c *policy and \c *constraint.
809 * \retval #PSA_ERROR_INVALID_ARGUMENT
810 * \c *policy and \c *constraint are incompatible.
811 * \c *policy is unchanged.
812 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100813static psa_status_t psa_restrict_key_policy(
814 psa_key_policy_t *policy,
815 const psa_key_policy_t *constraint )
816{
817 psa_algorithm_t intersection_alg =
818 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200819 psa_algorithm_t intersection_alg2 =
820 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100821 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
822 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200823 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
824 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100825 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100826 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200827 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 return( PSA_SUCCESS );
829}
830
Darryl Green06fd18d2018-07-16 11:21:11 +0100831/** Retrieve a slot which must contain a key. The key must have allow all the
832 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
833 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100834static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100835 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100836 psa_key_usage_t usage,
837 psa_algorithm_t alg )
838{
839 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100840 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100841
842 *p_slot = NULL;
843
Gilles Peskinec5487a82018-12-03 18:08:14 +0100844 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100845 if( status != PSA_SUCCESS )
846 return( status );
847 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200848 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100849
850 /* Enforce that usage policy for the key slot contains all the flags
851 * required by the usage parameter. There is one exception: public
852 * keys can always be exported, so we treat public key objects as
853 * if they had the export flag. */
854 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
855 usage &= ~PSA_KEY_USAGE_EXPORT;
856 if( ( slot->policy.usage & usage ) != usage )
857 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858
859 /* Enforce that the usage policy permits the requested algortihm. */
860 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100861 return( PSA_ERROR_NOT_PERMITTED );
862
863 *p_slot = slot;
864 return( PSA_SUCCESS );
865}
Darryl Green940d72c2018-07-13 13:18:51 +0100866
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100867/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100868static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000869{
870 if( slot->type == PSA_KEY_TYPE_NONE )
871 {
872 /* No key material to clean. */
873 }
874 else if( key_type_is_raw_bytes( slot->type ) )
875 {
876 mbedtls_free( slot->data.raw.data );
877 }
878 else
879#if defined(MBEDTLS_RSA_C)
880 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
881 {
882 mbedtls_rsa_free( slot->data.rsa );
883 mbedtls_free( slot->data.rsa );
884 }
885 else
886#endif /* defined(MBEDTLS_RSA_C) */
887#if defined(MBEDTLS_ECP_C)
888 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
889 {
890 mbedtls_ecp_keypair_free( slot->data.ecp );
891 mbedtls_free( slot->data.ecp );
892 }
893 else
894#endif /* defined(MBEDTLS_ECP_C) */
895 {
896 /* Shouldn't happen: the key type is not any type that we
897 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200898 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000899 }
900
901 return( PSA_SUCCESS );
902}
903
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100904static void psa_abort_operations_using_key( psa_key_slot_t *slot )
905{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200906 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100907 (void) slot;
908}
909
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100910/** Completely wipe a slot in memory, including its policy.
911 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100912psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100913{
914 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100915 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100916 /* At this point, key material and other type-specific content has
917 * been wiped. Clear remaining metadata. We can call memset and not
918 * zeroize because the metadata is not particularly sensitive. */
919 memset( slot, 0, sizeof( *slot ) );
920 return( status );
921}
922
Gilles Peskinec5487a82018-12-03 18:08:14 +0100923psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100925 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100926 psa_status_t status = PSA_SUCCESS;
927 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928
Gilles Peskinec5487a82018-12-03 18:08:14 +0100929 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200930 if( status != PSA_SUCCESS )
931 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100932#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
933 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
934 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100935 storage_status =
936 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100937 }
938#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100939 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100940 if( status != PSA_SUCCESS )
941 return( status );
942 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943}
944
Gilles Peskineb870b182018-07-06 16:02:09 +0200945/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200946static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200947{
948 if( key_type_is_raw_bytes( slot->type ) )
949 return( slot->data.raw.bytes * 8 );
950#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200951 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100952 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200953#endif /* defined(MBEDTLS_RSA_C) */
954#if defined(MBEDTLS_ECP_C)
955 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
956 return( slot->data.ecp->grp.pbits );
957#endif /* defined(MBEDTLS_ECP_C) */
958 /* Shouldn't happen except on an empty slot. */
959 return( 0 );
960}
961
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200962void psa_reset_key_attributes( psa_key_attributes_t *attributes )
963{
Gilles Peskineb699f072019-04-26 16:06:02 +0200964 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200965 memset( attributes, 0, sizeof( *attributes ) );
966}
967
Gilles Peskineb699f072019-04-26 16:06:02 +0200968psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
969 psa_key_type_t type,
970 const uint8_t *data,
971 size_t data_length )
972{
973 uint8_t *copy = NULL;
974
975 if( data_length != 0 )
976 {
977 copy = mbedtls_calloc( 1, data_length );
978 if( copy == NULL )
979 return( PSA_ERROR_INSUFFICIENT_MEMORY );
980 memcpy( copy, data, data_length );
981 }
982 /* After this point, this function is guaranteed to succeed, so it
983 * can start modifying `*attributes`. */
984
985 if( attributes->domain_parameters != NULL )
986 {
987 mbedtls_free( attributes->domain_parameters );
988 attributes->domain_parameters = NULL;
989 attributes->domain_parameters_size = 0;
990 }
991
992 attributes->domain_parameters = copy;
993 attributes->domain_parameters_size = data_length;
994 attributes->type = type;
995 return( PSA_SUCCESS );
996}
997
998psa_status_t psa_get_key_domain_parameters(
999 const psa_key_attributes_t *attributes,
1000 uint8_t *data, size_t data_size, size_t *data_length )
1001{
1002 if( attributes->domain_parameters_size > data_size )
1003 return( PSA_ERROR_BUFFER_TOO_SMALL );
1004 *data_length = attributes->domain_parameters_size;
1005 if( attributes->domain_parameters_size != 0 )
1006 memcpy( data, attributes->domain_parameters,
1007 attributes->domain_parameters_size );
1008 return( PSA_SUCCESS );
1009}
1010
1011#if defined(MBEDTLS_RSA_C)
1012static psa_status_t psa_get_rsa_public_exponent(
1013 const mbedtls_rsa_context *rsa,
1014 psa_key_attributes_t *attributes )
1015{
1016 mbedtls_mpi mpi;
1017 int ret;
1018 uint8_t *buffer = NULL;
1019 size_t buflen;
1020 mbedtls_mpi_init( &mpi );
1021
1022 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1023 if( ret != 0 )
1024 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001025 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1026 {
1027 /* It's the default value, which is reported as an empty string,
1028 * so there's nothing to do. */
1029 goto exit;
1030 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001031
1032 buflen = mbedtls_mpi_size( &mpi );
1033 buffer = mbedtls_calloc( 1, buflen );
1034 if( buffer == NULL )
1035 {
1036 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1037 goto exit;
1038 }
1039 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1040 if( ret != 0 )
1041 goto exit;
1042 attributes->domain_parameters = buffer;
1043 attributes->domain_parameters_size = buflen;
1044
1045exit:
1046 mbedtls_mpi_free( &mpi );
1047 if( ret != 0 )
1048 mbedtls_free( buffer );
1049 return( mbedtls_to_psa_error( ret ) );
1050}
1051#endif /* MBEDTLS_RSA_C */
1052
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1054 psa_key_attributes_t *attributes )
1055{
1056 psa_key_slot_t *slot;
1057 psa_status_t status;
1058
1059 psa_reset_key_attributes( attributes );
1060
1061 status = psa_get_key_slot( handle, &slot );
1062 if( status != PSA_SUCCESS )
1063 return( status );
1064
1065 attributes->id = slot->persistent_storage_id;
1066 attributes->lifetime = slot->lifetime;
1067 attributes->policy = slot->policy;
1068 attributes->type = slot->type;
1069 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001070
1071 switch( slot->type )
1072 {
1073#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001074 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001075 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1076 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1077 break;
1078#endif
1079 default:
1080 /* Nothing else to do. */
1081 break;
1082 }
1083
1084 if( status != PSA_SUCCESS )
1085 psa_reset_key_attributes( attributes );
1086 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087}
1088
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001089#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001090static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1091 unsigned char *buf, size_t size )
1092{
1093 int ret;
1094 unsigned char *c;
1095 size_t len = 0;
1096
1097 c = buf + size;
1098
1099 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1100
1101 return( (int) len );
1102}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001103#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001104
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001105static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1106 uint8_t *data,
1107 size_t data_size,
1108 size_t *data_length,
1109 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001110{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001111 *data_length = 0;
1112
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001113 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001114 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001115
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001116 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117 {
1118 if( slot->data.raw.bytes > data_size )
1119 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001120 if( data_size != 0 )
1121 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001122 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001123 memset( data + slot->data.raw.bytes, 0,
1124 data_size - slot->data.raw.bytes );
1125 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 *data_length = slot->data.raw.bytes;
1127 return( PSA_SUCCESS );
1128 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001129#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001130 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001131 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001132 psa_status_t status;
1133
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001134 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001135 if( bytes > data_size )
1136 return( PSA_ERROR_BUFFER_TOO_SMALL );
1137 status = mbedtls_to_psa_error(
1138 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1139 if( status != PSA_SUCCESS )
1140 return( status );
1141 memset( data + bytes, 0, data_size - bytes );
1142 *data_length = bytes;
1143 return( PSA_SUCCESS );
1144 }
1145#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001146 else
Moran Peker17e36e12018-05-02 12:55:20 +03001147 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001148#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001149 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001150 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001151 {
Moran Pekera998bc62018-04-16 18:16:20 +03001152 mbedtls_pk_context pk;
1153 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001154 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001155 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001156#if defined(MBEDTLS_RSA_C)
1157 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001158 pk.pk_info = &mbedtls_rsa_info;
1159 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001160#else
1161 return( PSA_ERROR_NOT_SUPPORTED );
1162#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001163 }
1164 else
1165 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001166#if defined(MBEDTLS_ECP_C)
1167 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001168 pk.pk_info = &mbedtls_eckey_info;
1169 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001170#else
1171 return( PSA_ERROR_NOT_SUPPORTED );
1172#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001173 }
Moran Pekerd7326592018-05-29 16:56:39 +03001174 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001175 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001176 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001177 }
Moran Peker17e36e12018-05-02 12:55:20 +03001178 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001179 {
Moran Peker17e36e12018-05-02 12:55:20 +03001180 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001181 }
Moran Peker60364322018-04-29 11:34:58 +03001182 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001183 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001184 /* If data_size is 0 then data may be NULL and then the
1185 * call to memset would have undefined behavior. */
1186 if( data_size != 0 )
1187 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001188 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001189 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001190 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1191 * Move the data to the beginning and erase remaining data
1192 * at the original location. */
1193 if( 2 * (size_t) ret <= data_size )
1194 {
1195 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001196 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001197 }
1198 else if( (size_t) ret < data_size )
1199 {
1200 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001201 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001202 }
Moran Pekera998bc62018-04-16 18:16:20 +03001203 *data_length = ret;
1204 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001205 }
1206 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001207#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001208 {
1209 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001210 it is valid for a special-purpose implementation to omit
1211 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001212 return( PSA_ERROR_NOT_SUPPORTED );
1213 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001214 }
1215}
1216
Gilles Peskinec5487a82018-12-03 18:08:14 +01001217psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001218 uint8_t *data,
1219 size_t data_size,
1220 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001221{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001222 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001223 psa_status_t status;
1224
1225 /* Set the key to empty now, so that even when there are errors, we always
1226 * set data_length to a value between 0 and data_size. On error, setting
1227 * the key to empty is a good choice because an empty key representation is
1228 * unlikely to be accepted anywhere. */
1229 *data_length = 0;
1230
1231 /* Export requires the EXPORT flag. There is an exception for public keys,
1232 * which don't require any flag, but psa_get_key_from_slot takes
1233 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001234 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001235 if( status != PSA_SUCCESS )
1236 return( status );
1237 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001238 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240
Gilles Peskinec5487a82018-12-03 18:08:14 +01001241psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001242 uint8_t *data,
1243 size_t data_size,
1244 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001245{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001246 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001247 psa_status_t status;
1248
1249 /* Set the key to empty now, so that even when there are errors, we always
1250 * set data_length to a value between 0 and data_size. On error, setting
1251 * the key to empty is a good choice because an empty key representation is
1252 * unlikely to be accepted anywhere. */
1253 *data_length = 0;
1254
1255 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001256 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001257 if( status != PSA_SUCCESS )
1258 return( status );
1259 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001260 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001261}
1262
Gilles Peskine4747d192019-04-17 15:05:45 +02001263static psa_status_t psa_set_key_policy_internal(
1264 psa_key_slot_t *slot,
1265 const psa_key_policy_t *policy )
1266{
1267 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001268 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001269 PSA_KEY_USAGE_ENCRYPT |
1270 PSA_KEY_USAGE_DECRYPT |
1271 PSA_KEY_USAGE_SIGN |
1272 PSA_KEY_USAGE_VERIFY |
1273 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1274 return( PSA_ERROR_INVALID_ARGUMENT );
1275
1276 slot->policy = *policy;
1277 return( PSA_SUCCESS );
1278}
1279
1280/** Prepare a key slot to receive key material.
1281 *
1282 * This function allocates a key slot and sets its metadata.
1283 *
1284 * If this function fails, call psa_fail_key_creation().
1285 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001286 * This function is intended to be used as follows:
1287 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1288 * it with the specified attributes, and assign it a handle.
1289 * -# Populate the slot with the key material.
1290 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1291 * In case of failure at any step, stop the sequence and call
1292 * psa_fail_key_creation().
1293 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001294 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001295 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001296 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001297 *
1298 * \retval #PSA_SUCCESS
1299 * The key slot is ready to receive key material.
1300 * \return If this function fails, the key slot is an invalid state.
1301 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001302 */
1303static psa_status_t psa_start_key_creation(
1304 const psa_key_attributes_t *attributes,
1305 psa_key_handle_t *handle,
1306 psa_key_slot_t **p_slot )
1307{
1308 psa_status_t status;
1309 psa_key_slot_t *slot;
1310
Gilles Peskine267c6562019-05-27 19:01:54 +02001311 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001312 if( status != PSA_SUCCESS )
1313 return( status );
1314 slot = *p_slot;
1315
1316 status = psa_set_key_policy_internal( slot, &attributes->policy );
1317 if( status != PSA_SUCCESS )
1318 return( status );
1319 slot->lifetime = attributes->lifetime;
1320 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001321 {
1322 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +02001323 attributes->id, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001324 if( status != PSA_SUCCESS )
1325 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001326 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001327 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001328 slot->type = attributes->type;
1329
1330 return( status );
1331}
1332
1333/** Finalize the creation of a key once its key material has been set.
1334 *
1335 * This entails writing the key to persistent storage.
1336 *
1337 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001338 * See the documentation of psa_start_key_creation() for the intended use
1339 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001340 *
1341 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001342 *
1343 * \retval #PSA_SUCCESS
1344 * The key was successfully created. The handle is now valid.
1345 * \return If this function fails, the key slot is an invalid state.
1346 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001347 */
1348static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1349{
1350 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001351 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001352
1353#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1354 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1355 {
1356 uint8_t *buffer = NULL;
1357 size_t buffer_size = 0;
1358 size_t length;
1359
1360 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001361 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001362 buffer = mbedtls_calloc( 1, buffer_size );
1363 if( buffer == NULL && buffer_size != 0 )
1364 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1365 status = psa_internal_export_key( slot,
1366 buffer, buffer_size, &length,
1367 0 );
1368
1369 if( status == PSA_SUCCESS )
1370 {
1371 status = psa_save_persistent_key( slot->persistent_storage_id,
1372 slot->type, &slot->policy,
1373 buffer, length );
1374 }
1375
1376 if( buffer_size != 0 )
1377 mbedtls_platform_zeroize( buffer, buffer_size );
1378 mbedtls_free( buffer );
1379 }
1380#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1381
1382 return( status );
1383}
1384
1385/** Abort the creation of a key.
1386 *
1387 * You may call this function after calling psa_start_key_creation(),
1388 * or after psa_finish_key_creation() fails. In other circumstances, this
1389 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001390 * See the documentation of psa_start_key_creation() for the intended use
1391 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001392 *
1393 * \param slot Pointer to the slot with key material.
1394 */
1395static void psa_fail_key_creation( psa_key_slot_t *slot )
1396{
1397 if( slot == NULL )
1398 return;
1399 psa_wipe_key_slot( slot );
1400}
1401
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001402static psa_status_t psa_check_key_slot_attributes(
1403 const psa_key_slot_t *slot,
1404 const psa_key_attributes_t *attributes )
1405{
1406 if( attributes->type != 0 )
1407 {
1408 if( attributes->type != slot->type )
1409 return( PSA_ERROR_INVALID_ARGUMENT );
1410 }
1411
1412 if( attributes->domain_parameters_size != 0 )
1413 {
1414#if defined(MBEDTLS_RSA_C)
1415 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1416 {
1417 mbedtls_mpi actual, required;
1418 int ret;
1419 mbedtls_mpi_init( &actual );
1420 mbedtls_mpi_init( &required );
1421 ret = mbedtls_rsa_export( slot->data.rsa,
1422 NULL, NULL, NULL, NULL, &actual );
1423 if( ret != 0 )
1424 goto rsa_exit;
1425 ret = mbedtls_mpi_read_binary( &required,
1426 attributes->domain_parameters,
1427 attributes->domain_parameters_size );
1428 if( ret != 0 )
1429 goto rsa_exit;
1430 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1431 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1432 rsa_exit:
1433 mbedtls_mpi_free( &actual );
1434 mbedtls_mpi_free( &required );
1435 if( ret != 0)
1436 return( mbedtls_to_psa_error( ret ) );
1437 }
1438 else
1439#endif
1440 {
1441 return( PSA_ERROR_INVALID_ARGUMENT );
1442 }
1443 }
1444
1445 if( attributes->bits != 0 )
1446 {
1447 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1448 return( PSA_ERROR_INVALID_ARGUMENT );
1449 }
1450
1451 return( PSA_SUCCESS );
1452}
1453
Gilles Peskine4747d192019-04-17 15:05:45 +02001454psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001455 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001456 size_t data_length,
1457 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001458{
1459 psa_status_t status;
1460 psa_key_slot_t *slot = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001461
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 status = psa_start_key_creation( attributes, handle, &slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001463 if( status != PSA_SUCCESS )
1464 goto exit;
1465
1466 status = psa_import_key_into_slot( slot, data, data_length );
1467 if( status != PSA_SUCCESS )
1468 goto exit;
1469 status = psa_check_key_slot_attributes( slot, attributes );
1470 if( status != PSA_SUCCESS )
1471 goto exit;
1472
1473 status = psa_finish_key_creation( slot );
1474exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 if( status != PSA_SUCCESS )
1476 {
1477 psa_fail_key_creation( slot );
1478 *handle = 0;
1479 }
1480 return( status );
1481}
1482
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001483static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001484 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001485{
1486 psa_status_t status;
1487 uint8_t *buffer = NULL;
1488 size_t buffer_size = 0;
1489 size_t length;
1490
1491 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001492 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001493 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001494 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001495 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001496 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1497 if( status != PSA_SUCCESS )
1498 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001499 target->type = source->type;
1500 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001501
1502exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001503 if( buffer_size != 0 )
1504 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001505 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001506 return( status );
1507}
1508
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001509psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1510 const psa_key_attributes_t *specified_attributes,
1511 psa_key_handle_t *target_handle )
1512{
1513 psa_status_t status;
1514 psa_key_slot_t *source_slot = NULL;
1515 psa_key_slot_t *target_slot = NULL;
1516 psa_key_attributes_t actual_attributes = *specified_attributes;
1517
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001518 status = psa_get_key_from_slot( source_handle, &source_slot,
1519 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001520 if( status != PSA_SUCCESS )
1521 goto exit;
1522
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001523 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1524 if( status != PSA_SUCCESS )
1525 goto exit;
1526
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001527 status = psa_restrict_key_policy( &actual_attributes.policy,
1528 &source_slot->policy );
1529 if( status != PSA_SUCCESS )
1530 goto exit;
1531
1532 status = psa_start_key_creation( &actual_attributes,
1533 target_handle, &target_slot );
1534 if( status != PSA_SUCCESS )
1535 goto exit;
1536
1537 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001538 if( status != PSA_SUCCESS )
1539 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001540
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001541 status = psa_finish_key_creation( target_slot );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001542exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001543 if( status != PSA_SUCCESS )
1544 {
1545 psa_fail_key_creation( target_slot );
1546 *target_handle = 0;
1547 }
1548 return( status );
1549}
1550
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001551
1552
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001554/* Message digests */
1555/****************************************************************/
1556
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001557static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001558{
1559 switch( alg )
1560 {
1561#if defined(MBEDTLS_MD2_C)
1562 case PSA_ALG_MD2:
1563 return( &mbedtls_md2_info );
1564#endif
1565#if defined(MBEDTLS_MD4_C)
1566 case PSA_ALG_MD4:
1567 return( &mbedtls_md4_info );
1568#endif
1569#if defined(MBEDTLS_MD5_C)
1570 case PSA_ALG_MD5:
1571 return( &mbedtls_md5_info );
1572#endif
1573#if defined(MBEDTLS_RIPEMD160_C)
1574 case PSA_ALG_RIPEMD160:
1575 return( &mbedtls_ripemd160_info );
1576#endif
1577#if defined(MBEDTLS_SHA1_C)
1578 case PSA_ALG_SHA_1:
1579 return( &mbedtls_sha1_info );
1580#endif
1581#if defined(MBEDTLS_SHA256_C)
1582 case PSA_ALG_SHA_224:
1583 return( &mbedtls_sha224_info );
1584 case PSA_ALG_SHA_256:
1585 return( &mbedtls_sha256_info );
1586#endif
1587#if defined(MBEDTLS_SHA512_C)
1588 case PSA_ALG_SHA_384:
1589 return( &mbedtls_sha384_info );
1590 case PSA_ALG_SHA_512:
1591 return( &mbedtls_sha512_info );
1592#endif
1593 default:
1594 return( NULL );
1595 }
1596}
1597
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1599{
1600 switch( operation->alg )
1601 {
Gilles Peskine81736312018-06-26 15:04:31 +02001602 case 0:
1603 /* The object has (apparently) been initialized but it is not
1604 * in use. It's ok to call abort on such an object, and there's
1605 * nothing to do. */
1606 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001607#if defined(MBEDTLS_MD2_C)
1608 case PSA_ALG_MD2:
1609 mbedtls_md2_free( &operation->ctx.md2 );
1610 break;
1611#endif
1612#if defined(MBEDTLS_MD4_C)
1613 case PSA_ALG_MD4:
1614 mbedtls_md4_free( &operation->ctx.md4 );
1615 break;
1616#endif
1617#if defined(MBEDTLS_MD5_C)
1618 case PSA_ALG_MD5:
1619 mbedtls_md5_free( &operation->ctx.md5 );
1620 break;
1621#endif
1622#if defined(MBEDTLS_RIPEMD160_C)
1623 case PSA_ALG_RIPEMD160:
1624 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1625 break;
1626#endif
1627#if defined(MBEDTLS_SHA1_C)
1628 case PSA_ALG_SHA_1:
1629 mbedtls_sha1_free( &operation->ctx.sha1 );
1630 break;
1631#endif
1632#if defined(MBEDTLS_SHA256_C)
1633 case PSA_ALG_SHA_224:
1634 case PSA_ALG_SHA_256:
1635 mbedtls_sha256_free( &operation->ctx.sha256 );
1636 break;
1637#endif
1638#if defined(MBEDTLS_SHA512_C)
1639 case PSA_ALG_SHA_384:
1640 case PSA_ALG_SHA_512:
1641 mbedtls_sha512_free( &operation->ctx.sha512 );
1642 break;
1643#endif
1644 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001645 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001646 }
1647 operation->alg = 0;
1648 return( PSA_SUCCESS );
1649}
1650
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001651psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001652 psa_algorithm_t alg )
1653{
1654 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001655
1656 /* A context must be freshly initialized before it can be set up. */
1657 if( operation->alg != 0 )
1658 {
1659 return( PSA_ERROR_BAD_STATE );
1660 }
1661
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001662 switch( alg )
1663 {
1664#if defined(MBEDTLS_MD2_C)
1665 case PSA_ALG_MD2:
1666 mbedtls_md2_init( &operation->ctx.md2 );
1667 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1668 break;
1669#endif
1670#if defined(MBEDTLS_MD4_C)
1671 case PSA_ALG_MD4:
1672 mbedtls_md4_init( &operation->ctx.md4 );
1673 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1674 break;
1675#endif
1676#if defined(MBEDTLS_MD5_C)
1677 case PSA_ALG_MD5:
1678 mbedtls_md5_init( &operation->ctx.md5 );
1679 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1680 break;
1681#endif
1682#if defined(MBEDTLS_RIPEMD160_C)
1683 case PSA_ALG_RIPEMD160:
1684 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1685 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1686 break;
1687#endif
1688#if defined(MBEDTLS_SHA1_C)
1689 case PSA_ALG_SHA_1:
1690 mbedtls_sha1_init( &operation->ctx.sha1 );
1691 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1692 break;
1693#endif
1694#if defined(MBEDTLS_SHA256_C)
1695 case PSA_ALG_SHA_224:
1696 mbedtls_sha256_init( &operation->ctx.sha256 );
1697 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1698 break;
1699 case PSA_ALG_SHA_256:
1700 mbedtls_sha256_init( &operation->ctx.sha256 );
1701 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1702 break;
1703#endif
1704#if defined(MBEDTLS_SHA512_C)
1705 case PSA_ALG_SHA_384:
1706 mbedtls_sha512_init( &operation->ctx.sha512 );
1707 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1708 break;
1709 case PSA_ALG_SHA_512:
1710 mbedtls_sha512_init( &operation->ctx.sha512 );
1711 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1712 break;
1713#endif
1714 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001715 return( PSA_ALG_IS_HASH( alg ) ?
1716 PSA_ERROR_NOT_SUPPORTED :
1717 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001718 }
1719 if( ret == 0 )
1720 operation->alg = alg;
1721 else
1722 psa_hash_abort( operation );
1723 return( mbedtls_to_psa_error( ret ) );
1724}
1725
1726psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1727 const uint8_t *input,
1728 size_t input_length )
1729{
1730 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001731
1732 /* Don't require hash implementations to behave correctly on a
1733 * zero-length input, which may have an invalid pointer. */
1734 if( input_length == 0 )
1735 return( PSA_SUCCESS );
1736
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001737 switch( operation->alg )
1738 {
1739#if defined(MBEDTLS_MD2_C)
1740 case PSA_ALG_MD2:
1741 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1742 input, input_length );
1743 break;
1744#endif
1745#if defined(MBEDTLS_MD4_C)
1746 case PSA_ALG_MD4:
1747 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1748 input, input_length );
1749 break;
1750#endif
1751#if defined(MBEDTLS_MD5_C)
1752 case PSA_ALG_MD5:
1753 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1754 input, input_length );
1755 break;
1756#endif
1757#if defined(MBEDTLS_RIPEMD160_C)
1758 case PSA_ALG_RIPEMD160:
1759 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1760 input, input_length );
1761 break;
1762#endif
1763#if defined(MBEDTLS_SHA1_C)
1764 case PSA_ALG_SHA_1:
1765 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1766 input, input_length );
1767 break;
1768#endif
1769#if defined(MBEDTLS_SHA256_C)
1770 case PSA_ALG_SHA_224:
1771 case PSA_ALG_SHA_256:
1772 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1773 input, input_length );
1774 break;
1775#endif
1776#if defined(MBEDTLS_SHA512_C)
1777 case PSA_ALG_SHA_384:
1778 case PSA_ALG_SHA_512:
1779 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1780 input, input_length );
1781 break;
1782#endif
1783 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001784 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001785 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001786
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001787 if( ret != 0 )
1788 psa_hash_abort( operation );
1789 return( mbedtls_to_psa_error( ret ) );
1790}
1791
1792psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1793 uint8_t *hash,
1794 size_t hash_size,
1795 size_t *hash_length )
1796{
itayzafrir40835d42018-08-02 13:14:17 +03001797 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001798 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001799 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001800
1801 /* Fill the output buffer with something that isn't a valid hash
1802 * (barring an attack on the hash and deliberately-crafted input),
1803 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001804 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001805 /* If hash_size is 0 then hash may be NULL and then the
1806 * call to memset would have undefined behavior. */
1807 if( hash_size != 0 )
1808 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001809
1810 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001811 {
1812 status = PSA_ERROR_BUFFER_TOO_SMALL;
1813 goto exit;
1814 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001815
1816 switch( operation->alg )
1817 {
1818#if defined(MBEDTLS_MD2_C)
1819 case PSA_ALG_MD2:
1820 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1821 break;
1822#endif
1823#if defined(MBEDTLS_MD4_C)
1824 case PSA_ALG_MD4:
1825 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1826 break;
1827#endif
1828#if defined(MBEDTLS_MD5_C)
1829 case PSA_ALG_MD5:
1830 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1831 break;
1832#endif
1833#if defined(MBEDTLS_RIPEMD160_C)
1834 case PSA_ALG_RIPEMD160:
1835 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1836 break;
1837#endif
1838#if defined(MBEDTLS_SHA1_C)
1839 case PSA_ALG_SHA_1:
1840 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1841 break;
1842#endif
1843#if defined(MBEDTLS_SHA256_C)
1844 case PSA_ALG_SHA_224:
1845 case PSA_ALG_SHA_256:
1846 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1847 break;
1848#endif
1849#if defined(MBEDTLS_SHA512_C)
1850 case PSA_ALG_SHA_384:
1851 case PSA_ALG_SHA_512:
1852 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1853 break;
1854#endif
1855 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001856 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001857 }
itayzafrir40835d42018-08-02 13:14:17 +03001858 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001859
itayzafrir40835d42018-08-02 13:14:17 +03001860exit:
1861 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001862 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001863 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001864 return( psa_hash_abort( operation ) );
1865 }
1866 else
1867 {
1868 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001869 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001870 }
1871}
1872
Gilles Peskine2d277862018-06-18 15:41:12 +02001873psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1874 const uint8_t *hash,
1875 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001876{
1877 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1878 size_t actual_hash_length;
1879 psa_status_t status = psa_hash_finish( operation,
1880 actual_hash, sizeof( actual_hash ),
1881 &actual_hash_length );
1882 if( status != PSA_SUCCESS )
1883 return( status );
1884 if( actual_hash_length != hash_length )
1885 return( PSA_ERROR_INVALID_SIGNATURE );
1886 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1887 return( PSA_ERROR_INVALID_SIGNATURE );
1888 return( PSA_SUCCESS );
1889}
1890
Gilles Peskineeb35d782019-01-22 17:56:16 +01001891psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1892 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001893{
1894 if( target_operation->alg != 0 )
1895 return( PSA_ERROR_BAD_STATE );
1896
1897 switch( source_operation->alg )
1898 {
1899 case 0:
1900 return( PSA_ERROR_BAD_STATE );
1901#if defined(MBEDTLS_MD2_C)
1902 case PSA_ALG_MD2:
1903 mbedtls_md2_clone( &target_operation->ctx.md2,
1904 &source_operation->ctx.md2 );
1905 break;
1906#endif
1907#if defined(MBEDTLS_MD4_C)
1908 case PSA_ALG_MD4:
1909 mbedtls_md4_clone( &target_operation->ctx.md4,
1910 &source_operation->ctx.md4 );
1911 break;
1912#endif
1913#if defined(MBEDTLS_MD5_C)
1914 case PSA_ALG_MD5:
1915 mbedtls_md5_clone( &target_operation->ctx.md5,
1916 &source_operation->ctx.md5 );
1917 break;
1918#endif
1919#if defined(MBEDTLS_RIPEMD160_C)
1920 case PSA_ALG_RIPEMD160:
1921 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1922 &source_operation->ctx.ripemd160 );
1923 break;
1924#endif
1925#if defined(MBEDTLS_SHA1_C)
1926 case PSA_ALG_SHA_1:
1927 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1928 &source_operation->ctx.sha1 );
1929 break;
1930#endif
1931#if defined(MBEDTLS_SHA256_C)
1932 case PSA_ALG_SHA_224:
1933 case PSA_ALG_SHA_256:
1934 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1935 &source_operation->ctx.sha256 );
1936 break;
1937#endif
1938#if defined(MBEDTLS_SHA512_C)
1939 case PSA_ALG_SHA_384:
1940 case PSA_ALG_SHA_512:
1941 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1942 &source_operation->ctx.sha512 );
1943 break;
1944#endif
1945 default:
1946 return( PSA_ERROR_NOT_SUPPORTED );
1947 }
1948
1949 target_operation->alg = source_operation->alg;
1950 return( PSA_SUCCESS );
1951}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001952
1953
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001954/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001955/* MAC */
1956/****************************************************************/
1957
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001958static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001959 psa_algorithm_t alg,
1960 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001961 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001962 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001963{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001965 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001966
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001967 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001968 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001969
Gilles Peskine8c9def32018-02-08 10:02:12 +01001970 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1971 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001972 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001973 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001974 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02001975 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001976 mode = MBEDTLS_MODE_STREAM;
1977 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001978 case PSA_ALG_CTR:
1979 mode = MBEDTLS_MODE_CTR;
1980 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001981 case PSA_ALG_CFB:
1982 mode = MBEDTLS_MODE_CFB;
1983 break;
1984 case PSA_ALG_OFB:
1985 mode = MBEDTLS_MODE_OFB;
1986 break;
1987 case PSA_ALG_CBC_NO_PADDING:
1988 mode = MBEDTLS_MODE_CBC;
1989 break;
1990 case PSA_ALG_CBC_PKCS7:
1991 mode = MBEDTLS_MODE_CBC;
1992 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001993 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994 mode = MBEDTLS_MODE_CCM;
1995 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001996 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001997 mode = MBEDTLS_MODE_GCM;
1998 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02001999 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2000 mode = MBEDTLS_MODE_CHACHAPOLY;
2001 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002 default:
2003 return( NULL );
2004 }
2005 }
2006 else if( alg == PSA_ALG_CMAC )
2007 mode = MBEDTLS_MODE_ECB;
2008 else if( alg == PSA_ALG_GMAC )
2009 mode = MBEDTLS_MODE_GCM;
2010 else
2011 return( NULL );
2012
2013 switch( key_type )
2014 {
2015 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002016 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017 break;
2018 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002019 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2020 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002022 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002024 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002025 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2026 * but two-key Triple-DES is functionally three-key Triple-DES
2027 * with K1=K3, so that's how we present it to mbedtls. */
2028 if( key_bits == 128 )
2029 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030 break;
2031 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002032 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002033 break;
2034 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002035 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002036 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002037 case PSA_KEY_TYPE_CHACHA20:
2038 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2039 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002040 default:
2041 return( NULL );
2042 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002043 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002044 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045
Jaeden Amero23bbb752018-06-26 14:16:54 +01002046 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2047 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048}
2049
Gilles Peskinea05219c2018-11-16 16:02:56 +01002050#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002051static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002052{
Gilles Peskine2d277862018-06-18 15:41:12 +02002053 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002054 {
2055 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002056 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002057 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002058 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002059 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002060 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002061 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002062 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002063 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002064 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002065 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002066 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002067 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002068 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002069 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002070 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002071 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002072 return( 128 );
2073 default:
2074 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002075 }
2076}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002077#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002078
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002079/* Initialize the MAC operation structure. Once this function has been
2080 * called, psa_mac_abort can run and will do the right thing. */
2081static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2082 psa_algorithm_t alg )
2083{
2084 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2085
2086 operation->alg = alg;
2087 operation->key_set = 0;
2088 operation->iv_set = 0;
2089 operation->iv_required = 0;
2090 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002091 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092
2093#if defined(MBEDTLS_CMAC_C)
2094 if( alg == PSA_ALG_CMAC )
2095 {
2096 operation->iv_required = 0;
2097 mbedtls_cipher_init( &operation->ctx.cmac );
2098 status = PSA_SUCCESS;
2099 }
2100 else
2101#endif /* MBEDTLS_CMAC_C */
2102#if defined(MBEDTLS_MD_C)
2103 if( PSA_ALG_IS_HMAC( operation->alg ) )
2104 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002105 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2106 operation->ctx.hmac.hash_ctx.alg = 0;
2107 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002108 }
2109 else
2110#endif /* MBEDTLS_MD_C */
2111 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002112 if( ! PSA_ALG_IS_MAC( alg ) )
2113 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002114 }
2115
2116 if( status != PSA_SUCCESS )
2117 memset( operation, 0, sizeof( *operation ) );
2118 return( status );
2119}
2120
Gilles Peskine01126fa2018-07-12 17:04:55 +02002121#if defined(MBEDTLS_MD_C)
2122static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2123{
Gilles Peskine3f108122018-12-07 18:14:53 +01002124 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002125 return( psa_hash_abort( &hmac->hash_ctx ) );
2126}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002127
Janos Follath999f6482019-06-11 12:04:10 +01002128#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002129static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2130{
2131 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2132 memset( hmac, 0, sizeof( *hmac ) );
2133}
Janos Follath999f6482019-06-11 12:04:10 +01002134#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002135#endif /* MBEDTLS_MD_C */
2136
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2138{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002139 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002141 /* The object has (apparently) been initialized but it is not
2142 * in use. It's ok to call abort on such an object, and there's
2143 * nothing to do. */
2144 return( PSA_SUCCESS );
2145 }
2146 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002147#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002148 if( operation->alg == PSA_ALG_CMAC )
2149 {
2150 mbedtls_cipher_free( &operation->ctx.cmac );
2151 }
2152 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002153#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002154#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002155 if( PSA_ALG_IS_HMAC( operation->alg ) )
2156 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002157 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002158 }
2159 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002161 {
2162 /* Sanity check (shouldn't happen: operation->alg should
2163 * always have been initialized to a valid value). */
2164 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002165 }
Moran Peker41deec42018-04-04 15:43:05 +03002166
Gilles Peskine8c9def32018-02-08 10:02:12 +01002167 operation->alg = 0;
2168 operation->key_set = 0;
2169 operation->iv_set = 0;
2170 operation->iv_required = 0;
2171 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002172 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002173
Gilles Peskine8c9def32018-02-08 10:02:12 +01002174 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002175
2176bad_state:
2177 /* If abort is called on an uninitialized object, we can't trust
2178 * anything. Wipe the object in case it contains confidential data.
2179 * This may result in a memory leak if a pointer gets overwritten,
2180 * but it's too late to do anything about this. */
2181 memset( operation, 0, sizeof( *operation ) );
2182 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183}
2184
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002185#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002186static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002187 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002188 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002189 const mbedtls_cipher_info_t *cipher_info )
2190{
2191 int ret;
2192
2193 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002194
2195 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2196 if( ret != 0 )
2197 return( ret );
2198
2199 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2200 slot->data.raw.data,
2201 key_bits );
2202 return( ret );
2203}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002204#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002205
Gilles Peskine248051a2018-06-20 16:09:38 +02002206#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002207static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2208 const uint8_t *key,
2209 size_t key_length,
2210 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002211{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002212 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002213 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002214 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002215 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002216 psa_status_t status;
2217
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002218 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2219 * overflow below. This should never trigger if the hash algorithm
2220 * is implemented correctly. */
2221 /* The size checks against the ipad and opad buffers cannot be written
2222 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2223 * because that triggers -Wlogical-op on GCC 7.3. */
2224 if( block_size > sizeof( ipad ) )
2225 return( PSA_ERROR_NOT_SUPPORTED );
2226 if( block_size > sizeof( hmac->opad ) )
2227 return( PSA_ERROR_NOT_SUPPORTED );
2228 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002229 return( PSA_ERROR_NOT_SUPPORTED );
2230
Gilles Peskined223b522018-06-11 18:12:58 +02002231 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002232 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002233 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002234 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002235 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002236 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002237 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002238 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002239 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002240 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002241 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002242 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002243 }
Gilles Peskine96889972018-07-12 17:07:03 +02002244 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2245 * but it is permitted. It is common when HMAC is used in HKDF, for
2246 * example. Don't call `memcpy` in the 0-length because `key` could be
2247 * an invalid pointer which would make the behavior undefined. */
2248 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002249 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002250
Gilles Peskined223b522018-06-11 18:12:58 +02002251 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2252 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002253 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002254 ipad[i] ^= 0x36;
2255 memset( ipad + key_length, 0x36, block_size - key_length );
2256
2257 /* Copy the key material from ipad to opad, flipping the requisite bits,
2258 * and filling the rest of opad with the requisite constant. */
2259 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002260 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2261 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002262
Gilles Peskine01126fa2018-07-12 17:04:55 +02002263 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002264 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002265 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002266
Gilles Peskine01126fa2018-07-12 17:04:55 +02002267 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002268
2269cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002270 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002271
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002272 return( status );
2273}
Gilles Peskine248051a2018-06-20 16:09:38 +02002274#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002275
Gilles Peskine89167cb2018-07-08 20:12:23 +02002276static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002277 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002278 psa_algorithm_t alg,
2279 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002281 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002282 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002283 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002284 psa_key_usage_t usage =
2285 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002286 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002287 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002289 /* A context must be freshly initialized before it can be set up. */
2290 if( operation->alg != 0 )
2291 {
2292 return( PSA_ERROR_BAD_STATE );
2293 }
2294
Gilles Peskined911eb72018-08-14 15:18:45 +02002295 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296 if( status != PSA_SUCCESS )
2297 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002298 if( is_sign )
2299 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002300
Gilles Peskinec5487a82018-12-03 18:08:14 +01002301 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002302 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002303 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002304 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002305
Gilles Peskine8c9def32018-02-08 10:02:12 +01002306#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002307 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002308 {
2309 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002310 mbedtls_cipher_info_from_psa( full_length_alg,
2311 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002312 int ret;
2313 if( cipher_info == NULL )
2314 {
2315 status = PSA_ERROR_NOT_SUPPORTED;
2316 goto exit;
2317 }
2318 operation->mac_size = cipher_info->block_size;
2319 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2320 status = mbedtls_to_psa_error( ret );
2321 }
2322 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002325 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002326 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002327 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002328 if( hash_alg == 0 )
2329 {
2330 status = PSA_ERROR_NOT_SUPPORTED;
2331 goto exit;
2332 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002333
2334 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2335 /* Sanity check. This shouldn't fail on a valid configuration. */
2336 if( operation->mac_size == 0 ||
2337 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2338 {
2339 status = PSA_ERROR_NOT_SUPPORTED;
2340 goto exit;
2341 }
2342
Gilles Peskine01126fa2018-07-12 17:04:55 +02002343 if( slot->type != PSA_KEY_TYPE_HMAC )
2344 {
2345 status = PSA_ERROR_INVALID_ARGUMENT;
2346 goto exit;
2347 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002348
Gilles Peskine01126fa2018-07-12 17:04:55 +02002349 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2350 slot->data.raw.data,
2351 slot->data.raw.bytes,
2352 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002353 }
2354 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002356 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002357 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002358 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002360
Gilles Peskined911eb72018-08-14 15:18:45 +02002361 if( truncated == 0 )
2362 {
2363 /* The "normal" case: untruncated algorithm. Nothing to do. */
2364 }
2365 else if( truncated < 4 )
2366 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002367 /* A very short MAC is too short for security since it can be
2368 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2369 * so we make this our minimum, even though 32 bits is still
2370 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002371 status = PSA_ERROR_NOT_SUPPORTED;
2372 }
2373 else if( truncated > operation->mac_size )
2374 {
2375 /* It's impossible to "truncate" to a larger length. */
2376 status = PSA_ERROR_INVALID_ARGUMENT;
2377 }
2378 else
2379 operation->mac_size = truncated;
2380
Gilles Peskinefbfac682018-07-08 20:51:54 +02002381exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002382 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002383 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002384 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002386 else
2387 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002388 operation->key_set = 1;
2389 }
2390 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391}
2392
Gilles Peskine89167cb2018-07-08 20:12:23 +02002393psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002394 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002395 psa_algorithm_t alg )
2396{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002397 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002398}
2399
2400psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002401 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002402 psa_algorithm_t alg )
2403{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002404 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002405}
2406
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2408 const uint8_t *input,
2409 size_t input_length )
2410{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002411 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002413 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002415 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416 operation->has_input = 1;
2417
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002419 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002420 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002421 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2422 input, input_length );
2423 status = mbedtls_to_psa_error( ret );
2424 }
2425 else
2426#endif /* MBEDTLS_CMAC_C */
2427#if defined(MBEDTLS_MD_C)
2428 if( PSA_ALG_IS_HMAC( operation->alg ) )
2429 {
2430 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2431 input_length );
2432 }
2433 else
2434#endif /* MBEDTLS_MD_C */
2435 {
2436 /* This shouldn't happen if `operation` was initialized by
2437 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002438 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002439 }
2440
Gilles Peskinefbfac682018-07-08 20:51:54 +02002441 if( status != PSA_SUCCESS )
2442 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002443 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002444}
2445
Gilles Peskine01126fa2018-07-12 17:04:55 +02002446#if defined(MBEDTLS_MD_C)
2447static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2448 uint8_t *mac,
2449 size_t mac_size )
2450{
2451 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2452 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2453 size_t hash_size = 0;
2454 size_t block_size = psa_get_hash_block_size( hash_alg );
2455 psa_status_t status;
2456
Gilles Peskine01126fa2018-07-12 17:04:55 +02002457 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2458 if( status != PSA_SUCCESS )
2459 return( status );
2460 /* From here on, tmp needs to be wiped. */
2461
2462 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2463 if( status != PSA_SUCCESS )
2464 goto exit;
2465
2466 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2467 if( status != PSA_SUCCESS )
2468 goto exit;
2469
2470 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2471 if( status != PSA_SUCCESS )
2472 goto exit;
2473
Gilles Peskined911eb72018-08-14 15:18:45 +02002474 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2475 if( status != PSA_SUCCESS )
2476 goto exit;
2477
2478 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002479
2480exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002481 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002482 return( status );
2483}
2484#endif /* MBEDTLS_MD_C */
2485
mohammad16036df908f2018-04-02 08:34:15 -07002486static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002487 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002488 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002490 if( ! operation->key_set )
2491 return( PSA_ERROR_BAD_STATE );
2492 if( operation->iv_required && ! operation->iv_set )
2493 return( PSA_ERROR_BAD_STATE );
2494
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 if( mac_size < operation->mac_size )
2496 return( PSA_ERROR_BUFFER_TOO_SMALL );
2497
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002499 if( operation->alg == PSA_ALG_CMAC )
2500 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002501 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2502 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2503 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002504 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002505 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002506 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002508 else
2509#endif /* MBEDTLS_CMAC_C */
2510#if defined(MBEDTLS_MD_C)
2511 if( PSA_ALG_IS_HMAC( operation->alg ) )
2512 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002513 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002514 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002515 }
2516 else
2517#endif /* MBEDTLS_MD_C */
2518 {
2519 /* This shouldn't happen if `operation` was initialized by
2520 * a setup function. */
2521 return( PSA_ERROR_BAD_STATE );
2522 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523}
2524
Gilles Peskineacd4be32018-07-08 19:56:25 +02002525psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2526 uint8_t *mac,
2527 size_t mac_size,
2528 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002529{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002530 psa_status_t status;
2531
Jaeden Amero252ef282019-02-15 14:05:35 +00002532 if( operation->alg == 0 )
2533 {
2534 return( PSA_ERROR_BAD_STATE );
2535 }
2536
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002537 /* Fill the output buffer with something that isn't a valid mac
2538 * (barring an attack on the mac and deliberately-crafted input),
2539 * in case the caller doesn't check the return status properly. */
2540 *mac_length = mac_size;
2541 /* If mac_size is 0 then mac may be NULL and then the
2542 * call to memset would have undefined behavior. */
2543 if( mac_size != 0 )
2544 memset( mac, '!', mac_size );
2545
Gilles Peskine89167cb2018-07-08 20:12:23 +02002546 if( ! operation->is_sign )
2547 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002548 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002549 }
mohammad16036df908f2018-04-02 08:34:15 -07002550
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002551 status = psa_mac_finish_internal( operation, mac, mac_size );
2552
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002553 if( status == PSA_SUCCESS )
2554 {
2555 status = psa_mac_abort( operation );
2556 if( status == PSA_SUCCESS )
2557 *mac_length = operation->mac_size;
2558 else
2559 memset( mac, '!', mac_size );
2560 }
2561 else
2562 psa_mac_abort( operation );
2563 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002564}
2565
Gilles Peskineacd4be32018-07-08 19:56:25 +02002566psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2567 const uint8_t *mac,
2568 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569{
Gilles Peskine828ed142018-06-18 23:25:51 +02002570 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002571 psa_status_t status;
2572
Jaeden Amero252ef282019-02-15 14:05:35 +00002573 if( operation->alg == 0 )
2574 {
2575 return( PSA_ERROR_BAD_STATE );
2576 }
2577
Gilles Peskine89167cb2018-07-08 20:12:23 +02002578 if( operation->is_sign )
2579 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002580 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002581 }
2582 if( operation->mac_size != mac_length )
2583 {
2584 status = PSA_ERROR_INVALID_SIGNATURE;
2585 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002586 }
mohammad16036df908f2018-04-02 08:34:15 -07002587
2588 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002589 actual_mac, sizeof( actual_mac ) );
2590
2591 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2592 status = PSA_ERROR_INVALID_SIGNATURE;
2593
2594cleanup:
2595 if( status == PSA_SUCCESS )
2596 status = psa_mac_abort( operation );
2597 else
2598 psa_mac_abort( operation );
2599
Gilles Peskine3f108122018-12-07 18:14:53 +01002600 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002601
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002602 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603}
2604
2605
Gilles Peskine20035e32018-02-03 22:44:14 +01002606
Gilles Peskine20035e32018-02-03 22:44:14 +01002607/****************************************************************/
2608/* Asymmetric cryptography */
2609/****************************************************************/
2610
Gilles Peskine2b450e32018-06-27 15:42:46 +02002611#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002612/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002613 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002614static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2615 size_t hash_length,
2616 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002617{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002618 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002619 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002620 *md_alg = mbedtls_md_get_type( md_info );
2621
2622 /* The Mbed TLS RSA module uses an unsigned int for hash length
2623 * parameters. Validate that it fits so that we don't risk an
2624 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002625#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002626 if( hash_length > UINT_MAX )
2627 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002628#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002629
2630#if defined(MBEDTLS_PKCS1_V15)
2631 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2632 * must be correct. */
2633 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2634 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002635 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002636 if( md_info == NULL )
2637 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002638 if( mbedtls_md_get_size( md_info ) != hash_length )
2639 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002640 }
2641#endif /* MBEDTLS_PKCS1_V15 */
2642
2643#if defined(MBEDTLS_PKCS1_V21)
2644 /* PSS requires a hash internally. */
2645 if( PSA_ALG_IS_RSA_PSS( alg ) )
2646 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002647 if( md_info == NULL )
2648 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002649 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002650#endif /* MBEDTLS_PKCS1_V21 */
2651
Gilles Peskine61b91d42018-06-08 16:09:36 +02002652 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002653}
2654
Gilles Peskine2b450e32018-06-27 15:42:46 +02002655static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2656 psa_algorithm_t alg,
2657 const uint8_t *hash,
2658 size_t hash_length,
2659 uint8_t *signature,
2660 size_t signature_size,
2661 size_t *signature_length )
2662{
2663 psa_status_t status;
2664 int ret;
2665 mbedtls_md_type_t md_alg;
2666
2667 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2668 if( status != PSA_SUCCESS )
2669 return( status );
2670
Gilles Peskine630a18a2018-06-29 17:49:35 +02002671 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002672 return( PSA_ERROR_BUFFER_TOO_SMALL );
2673
2674#if defined(MBEDTLS_PKCS1_V15)
2675 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2676 {
2677 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2678 MBEDTLS_MD_NONE );
2679 ret = mbedtls_rsa_pkcs1_sign( rsa,
2680 mbedtls_ctr_drbg_random,
2681 &global_data.ctr_drbg,
2682 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002683 md_alg,
2684 (unsigned int) hash_length,
2685 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002686 signature );
2687 }
2688 else
2689#endif /* MBEDTLS_PKCS1_V15 */
2690#if defined(MBEDTLS_PKCS1_V21)
2691 if( PSA_ALG_IS_RSA_PSS( alg ) )
2692 {
2693 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2694 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2695 mbedtls_ctr_drbg_random,
2696 &global_data.ctr_drbg,
2697 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002698 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002699 (unsigned int) hash_length,
2700 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002701 signature );
2702 }
2703 else
2704#endif /* MBEDTLS_PKCS1_V21 */
2705 {
2706 return( PSA_ERROR_INVALID_ARGUMENT );
2707 }
2708
2709 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002710 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002711 return( mbedtls_to_psa_error( ret ) );
2712}
2713
2714static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2715 psa_algorithm_t alg,
2716 const uint8_t *hash,
2717 size_t hash_length,
2718 const uint8_t *signature,
2719 size_t signature_length )
2720{
2721 psa_status_t status;
2722 int ret;
2723 mbedtls_md_type_t md_alg;
2724
2725 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2726 if( status != PSA_SUCCESS )
2727 return( status );
2728
Gilles Peskine630a18a2018-06-29 17:49:35 +02002729 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002730 return( PSA_ERROR_BUFFER_TOO_SMALL );
2731
2732#if defined(MBEDTLS_PKCS1_V15)
2733 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2734 {
2735 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2736 MBEDTLS_MD_NONE );
2737 ret = mbedtls_rsa_pkcs1_verify( rsa,
2738 mbedtls_ctr_drbg_random,
2739 &global_data.ctr_drbg,
2740 MBEDTLS_RSA_PUBLIC,
2741 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002742 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002743 hash,
2744 signature );
2745 }
2746 else
2747#endif /* MBEDTLS_PKCS1_V15 */
2748#if defined(MBEDTLS_PKCS1_V21)
2749 if( PSA_ALG_IS_RSA_PSS( alg ) )
2750 {
2751 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2752 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2753 mbedtls_ctr_drbg_random,
2754 &global_data.ctr_drbg,
2755 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002756 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002757 (unsigned int) hash_length,
2758 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002759 signature );
2760 }
2761 else
2762#endif /* MBEDTLS_PKCS1_V21 */
2763 {
2764 return( PSA_ERROR_INVALID_ARGUMENT );
2765 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002766
2767 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2768 * the rest of the signature is invalid". This has little use in
2769 * practice and PSA doesn't report this distinction. */
2770 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2771 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002772 return( mbedtls_to_psa_error( ret ) );
2773}
2774#endif /* MBEDTLS_RSA_C */
2775
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002776#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002777/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2778 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2779 * (even though these functions don't modify it). */
2780static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2781 psa_algorithm_t alg,
2782 const uint8_t *hash,
2783 size_t hash_length,
2784 uint8_t *signature,
2785 size_t signature_size,
2786 size_t *signature_length )
2787{
2788 int ret;
2789 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002790 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002791 mbedtls_mpi_init( &r );
2792 mbedtls_mpi_init( &s );
2793
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002794 if( signature_size < 2 * curve_bytes )
2795 {
2796 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2797 goto cleanup;
2798 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002799
Gilles Peskinea05219c2018-11-16 16:02:56 +01002800#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002801 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2802 {
2803 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2804 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2805 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2806 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2807 hash, hash_length,
2808 md_alg ) );
2809 }
2810 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002811#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002812 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002813 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002814 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2815 hash, hash_length,
2816 mbedtls_ctr_drbg_random,
2817 &global_data.ctr_drbg ) );
2818 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002819
2820 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2821 signature,
2822 curve_bytes ) );
2823 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2824 signature + curve_bytes,
2825 curve_bytes ) );
2826
2827cleanup:
2828 mbedtls_mpi_free( &r );
2829 mbedtls_mpi_free( &s );
2830 if( ret == 0 )
2831 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002832 return( mbedtls_to_psa_error( ret ) );
2833}
2834
2835static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2836 const uint8_t *hash,
2837 size_t hash_length,
2838 const uint8_t *signature,
2839 size_t signature_length )
2840{
2841 int ret;
2842 mbedtls_mpi r, s;
2843 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2844 mbedtls_mpi_init( &r );
2845 mbedtls_mpi_init( &s );
2846
2847 if( signature_length != 2 * curve_bytes )
2848 return( PSA_ERROR_INVALID_SIGNATURE );
2849
2850 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2851 signature,
2852 curve_bytes ) );
2853 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2854 signature + curve_bytes,
2855 curve_bytes ) );
2856
2857 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2858 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002859
2860cleanup:
2861 mbedtls_mpi_free( &r );
2862 mbedtls_mpi_free( &s );
2863 return( mbedtls_to_psa_error( ret ) );
2864}
2865#endif /* MBEDTLS_ECDSA_C */
2866
Gilles Peskinec5487a82018-12-03 18:08:14 +01002867psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002868 psa_algorithm_t alg,
2869 const uint8_t *hash,
2870 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002871 uint8_t *signature,
2872 size_t signature_size,
2873 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002874{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002875 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002876 psa_status_t status;
2877
2878 *signature_length = signature_size;
2879
Gilles Peskinec5487a82018-12-03 18:08:14 +01002880 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002881 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002882 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002883 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002884 {
2885 status = PSA_ERROR_INVALID_ARGUMENT;
2886 goto exit;
2887 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002888
Gilles Peskine20035e32018-02-03 22:44:14 +01002889#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002890 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002891 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002892 status = psa_rsa_sign( slot->data.rsa,
2893 alg,
2894 hash, hash_length,
2895 signature, signature_size,
2896 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002897 }
2898 else
2899#endif /* defined(MBEDTLS_RSA_C) */
2900#if defined(MBEDTLS_ECP_C)
2901 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2902 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002903#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002904 if(
2905#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2906 PSA_ALG_IS_ECDSA( alg )
2907#else
2908 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2909#endif
2910 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002911 status = psa_ecdsa_sign( slot->data.ecp,
2912 alg,
2913 hash, hash_length,
2914 signature, signature_size,
2915 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002916 else
2917#endif /* defined(MBEDTLS_ECDSA_C) */
2918 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002919 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002920 }
itayzafrir5c753392018-05-08 11:18:38 +03002921 }
2922 else
2923#endif /* defined(MBEDTLS_ECP_C) */
2924 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002925 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002926 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002927
2928exit:
2929 /* Fill the unused part of the output buffer (the whole buffer on error,
2930 * the trailing part on success) with something that isn't a valid mac
2931 * (barring an attack on the mac and deliberately-crafted input),
2932 * in case the caller doesn't check the return status properly. */
2933 if( status == PSA_SUCCESS )
2934 memset( signature + *signature_length, '!',
2935 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002936 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002937 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002938 /* If signature_size is 0 then we have nothing to do. We must not call
2939 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002940 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002941}
2942
Gilles Peskinec5487a82018-12-03 18:08:14 +01002943psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002944 psa_algorithm_t alg,
2945 const uint8_t *hash,
2946 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002947 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002948 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002949{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002950 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002951 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002952
Gilles Peskinec5487a82018-12-03 18:08:14 +01002953 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002954 if( status != PSA_SUCCESS )
2955 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002956
Gilles Peskine61b91d42018-06-08 16:09:36 +02002957#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002958 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002960 return( psa_rsa_verify( slot->data.rsa,
2961 alg,
2962 hash, hash_length,
2963 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964 }
2965 else
2966#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002967#if defined(MBEDTLS_ECP_C)
2968 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2969 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002970#if defined(MBEDTLS_ECDSA_C)
2971 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002972 return( psa_ecdsa_verify( slot->data.ecp,
2973 hash, hash_length,
2974 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002975 else
2976#endif /* defined(MBEDTLS_ECDSA_C) */
2977 {
2978 return( PSA_ERROR_INVALID_ARGUMENT );
2979 }
itayzafrir5c753392018-05-08 11:18:38 +03002980 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002981 else
2982#endif /* defined(MBEDTLS_ECP_C) */
2983 {
2984 return( PSA_ERROR_NOT_SUPPORTED );
2985 }
2986}
2987
Gilles Peskine072ac562018-06-30 00:21:29 +02002988#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2989static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2990 mbedtls_rsa_context *rsa )
2991{
2992 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2993 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2994 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2995 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2996}
2997#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2998
Gilles Peskinec5487a82018-12-03 18:08:14 +01002999psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003000 psa_algorithm_t alg,
3001 const uint8_t *input,
3002 size_t input_length,
3003 const uint8_t *salt,
3004 size_t salt_length,
3005 uint8_t *output,
3006 size_t output_size,
3007 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003009 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003010 psa_status_t status;
3011
Darryl Green5cc689a2018-07-24 15:34:10 +01003012 (void) input;
3013 (void) input_length;
3014 (void) salt;
3015 (void) output;
3016 (void) output_size;
3017
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003018 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003019
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003020 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3021 return( PSA_ERROR_INVALID_ARGUMENT );
3022
Gilles Peskinec5487a82018-12-03 18:08:14 +01003023 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003024 if( status != PSA_SUCCESS )
3025 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003026 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003027 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029
3030#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003031 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032 {
3033 mbedtls_rsa_context *rsa = slot->data.rsa;
3034 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003035 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003036 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003037#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003038 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003039 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003040 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3041 mbedtls_ctr_drbg_random,
3042 &global_data.ctr_drbg,
3043 MBEDTLS_RSA_PUBLIC,
3044 input_length,
3045 input,
3046 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047 }
3048 else
3049#endif /* MBEDTLS_PKCS1_V15 */
3050#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003051 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003053 psa_rsa_oaep_set_padding_mode( alg, rsa );
3054 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3055 mbedtls_ctr_drbg_random,
3056 &global_data.ctr_drbg,
3057 MBEDTLS_RSA_PUBLIC,
3058 salt, salt_length,
3059 input_length,
3060 input,
3061 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062 }
3063 else
3064#endif /* MBEDTLS_PKCS1_V21 */
3065 {
3066 return( PSA_ERROR_INVALID_ARGUMENT );
3067 }
3068 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003069 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070 return( mbedtls_to_psa_error( ret ) );
3071 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003073#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074 {
3075 return( PSA_ERROR_NOT_SUPPORTED );
3076 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003077}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078
Gilles Peskinec5487a82018-12-03 18:08:14 +01003079psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003080 psa_algorithm_t alg,
3081 const uint8_t *input,
3082 size_t input_length,
3083 const uint8_t *salt,
3084 size_t salt_length,
3085 uint8_t *output,
3086 size_t output_size,
3087 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003089 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003090 psa_status_t status;
3091
Darryl Green5cc689a2018-07-24 15:34:10 +01003092 (void) input;
3093 (void) input_length;
3094 (void) salt;
3095 (void) output;
3096 (void) output_size;
3097
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003098 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003099
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003100 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3101 return( PSA_ERROR_INVALID_ARGUMENT );
3102
Gilles Peskinec5487a82018-12-03 18:08:14 +01003103 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003104 if( status != PSA_SUCCESS )
3105 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003106 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003107 return( PSA_ERROR_INVALID_ARGUMENT );
3108
3109#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003110 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111 {
3112 mbedtls_rsa_context *rsa = slot->data.rsa;
3113 int ret;
3114
Gilles Peskine630a18a2018-06-29 17:49:35 +02003115 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116 return( PSA_ERROR_INVALID_ARGUMENT );
3117
3118#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003119 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003121 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3122 mbedtls_ctr_drbg_random,
3123 &global_data.ctr_drbg,
3124 MBEDTLS_RSA_PRIVATE,
3125 output_length,
3126 input,
3127 output,
3128 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129 }
3130 else
3131#endif /* MBEDTLS_PKCS1_V15 */
3132#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003133 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003135 psa_rsa_oaep_set_padding_mode( alg, rsa );
3136 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3137 mbedtls_ctr_drbg_random,
3138 &global_data.ctr_drbg,
3139 MBEDTLS_RSA_PRIVATE,
3140 salt, salt_length,
3141 output_length,
3142 input,
3143 output,
3144 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145 }
3146 else
3147#endif /* MBEDTLS_PKCS1_V21 */
3148 {
3149 return( PSA_ERROR_INVALID_ARGUMENT );
3150 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003151
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 return( mbedtls_to_psa_error( ret ) );
3153 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003155#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003156 {
3157 return( PSA_ERROR_NOT_SUPPORTED );
3158 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003159}
Gilles Peskine20035e32018-02-03 22:44:14 +01003160
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003161
3162
mohammad1603503973b2018-03-12 15:59:30 +02003163/****************************************************************/
3164/* Symmetric cryptography */
3165/****************************************************************/
3166
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167/* Initialize the cipher operation structure. Once this function has been
3168 * called, psa_cipher_abort can run and will do the right thing. */
3169static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3170 psa_algorithm_t alg )
3171{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003172 if( ! PSA_ALG_IS_CIPHER( alg ) )
3173 {
3174 memset( operation, 0, sizeof( *operation ) );
3175 return( PSA_ERROR_INVALID_ARGUMENT );
3176 }
3177
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178 operation->alg = alg;
3179 operation->key_set = 0;
3180 operation->iv_set = 0;
3181 operation->iv_required = 1;
3182 operation->iv_size = 0;
3183 operation->block_size = 0;
3184 mbedtls_cipher_init( &operation->ctx.cipher );
3185 return( PSA_SUCCESS );
3186}
3187
Gilles Peskinee553c652018-06-04 16:22:46 +02003188static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003189 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003190 psa_algorithm_t alg,
3191 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003192{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003193 int ret = 0;
3194 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003195 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003196 size_t key_bits;
3197 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003198 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3199 PSA_KEY_USAGE_ENCRYPT :
3200 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003201
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003202 /* A context must be freshly initialized before it can be set up. */
3203 if( operation->alg != 0 )
3204 {
3205 return( PSA_ERROR_BAD_STATE );
3206 }
3207
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003208 status = psa_cipher_init( operation, alg );
3209 if( status != PSA_SUCCESS )
3210 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003211
Gilles Peskinec5487a82018-12-03 18:08:14 +01003212 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003213 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003214 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003215 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003216
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003217 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003218 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003219 {
3220 status = PSA_ERROR_NOT_SUPPORTED;
3221 goto exit;
3222 }
mohammad1603503973b2018-03-12 15:59:30 +02003223
mohammad1603503973b2018-03-12 15:59:30 +02003224 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003225 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003226 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003227
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003228#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003229 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003230 {
3231 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3232 unsigned char keys[24];
3233 memcpy( keys, slot->data.raw.data, 16 );
3234 memcpy( keys + 16, slot->data.raw.data, 8 );
3235 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3236 keys,
3237 192, cipher_operation );
3238 }
3239 else
3240#endif
3241 {
3242 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3243 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003244 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003245 }
Moran Peker41deec42018-04-04 15:43:05 +03003246 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003247 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003248
mohammad16038481e742018-03-18 13:57:31 +02003249#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003250 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003251 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003252 case PSA_ALG_CBC_NO_PADDING:
3253 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3254 MBEDTLS_PADDING_NONE );
3255 break;
3256 case PSA_ALG_CBC_PKCS7:
3257 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3258 MBEDTLS_PADDING_PKCS7 );
3259 break;
3260 default:
3261 /* The algorithm doesn't involve padding. */
3262 ret = 0;
3263 break;
3264 }
3265 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003266 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003267#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3268
mohammad1603503973b2018-03-12 15:59:30 +02003269 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003270 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3271 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3272 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003273 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003274 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003275 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003276#if defined(MBEDTLS_CHACHA20_C)
3277 else
3278 if( alg == PSA_ALG_CHACHA20 )
3279 operation->iv_size = 12;
3280#endif
mohammad1603503973b2018-03-12 15:59:30 +02003281
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003282exit:
3283 if( status == 0 )
3284 status = mbedtls_to_psa_error( ret );
3285 if( status != 0 )
3286 psa_cipher_abort( operation );
3287 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003288}
3289
Gilles Peskinefe119512018-07-08 21:39:34 +02003290psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003291 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003292 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003293{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003294 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003295}
3296
Gilles Peskinefe119512018-07-08 21:39:34 +02003297psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003298 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003299 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003300{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003301 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003302}
3303
Gilles Peskinefe119512018-07-08 21:39:34 +02003304psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3305 unsigned char *iv,
3306 size_t iv_size,
3307 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003308{
itayzafrir534bd7c2018-08-02 13:56:32 +03003309 psa_status_t status;
3310 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003311 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003312 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003313 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003314 }
Moran Peker41deec42018-04-04 15:43:05 +03003315 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003316 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003317 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003318 goto exit;
3319 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003320 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3321 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003322 if( ret != 0 )
3323 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003324 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003325 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003326 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003327
mohammad16038481e742018-03-18 13:57:31 +02003328 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003329 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003330
Moran Peker395db872018-05-31 14:07:14 +03003331exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003332 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003333 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003334 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003335}
3336
Gilles Peskinefe119512018-07-08 21:39:34 +02003337psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3338 const unsigned char *iv,
3339 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003340{
itayzafrir534bd7c2018-08-02 13:56:32 +03003341 psa_status_t status;
3342 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003343 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003344 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003345 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003346 }
Moran Pekera28258c2018-05-29 16:25:04 +03003347 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003348 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003349 status = PSA_ERROR_INVALID_ARGUMENT;
3350 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003351 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003352 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3353 status = mbedtls_to_psa_error( ret );
3354exit:
3355 if( status == PSA_SUCCESS )
3356 operation->iv_set = 1;
3357 else
mohammad1603503973b2018-03-12 15:59:30 +02003358 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003359 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003360}
3361
Gilles Peskinee553c652018-06-04 16:22:46 +02003362psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3363 const uint8_t *input,
3364 size_t input_length,
3365 unsigned char *output,
3366 size_t output_size,
3367 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003368{
itayzafrir534bd7c2018-08-02 13:56:32 +03003369 psa_status_t status;
3370 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003371 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003372
3373 if( operation->alg == 0 )
3374 {
3375 return( PSA_ERROR_BAD_STATE );
3376 }
3377
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003378 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003379 {
3380 /* Take the unprocessed partial block left over from previous
3381 * update calls, if any, plus the input to this call. Remove
3382 * the last partial block, if any. You get the data that will be
3383 * output in this call. */
3384 expected_output_size =
3385 ( operation->ctx.cipher.unprocessed_len + input_length )
3386 / operation->block_size * operation->block_size;
3387 }
3388 else
3389 {
3390 expected_output_size = input_length;
3391 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003392
Gilles Peskine89d789c2018-06-04 17:17:16 +02003393 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003394 {
3395 status = PSA_ERROR_BUFFER_TOO_SMALL;
3396 goto exit;
3397 }
mohammad160382759612018-03-12 18:16:40 +02003398
mohammad1603503973b2018-03-12 15:59:30 +02003399 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003400 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003401 status = mbedtls_to_psa_error( ret );
3402exit:
3403 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003404 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003405 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003406}
3407
Gilles Peskinee553c652018-06-04 16:22:46 +02003408psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3409 uint8_t *output,
3410 size_t output_size,
3411 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003412{
David Saadab4ecc272019-02-14 13:48:10 +02003413 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003414 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003415 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003416
mohammad1603503973b2018-03-12 15:59:30 +02003417 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003418 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003419 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003420 }
3421 if( operation->iv_required && ! operation->iv_set )
3422 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003423 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003424 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003425
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003426 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003427 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3428 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003429 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003430 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003431 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003432 }
3433
Janos Follath315b51c2018-07-09 16:04:51 +01003434 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3435 temp_output_buffer,
3436 output_length );
3437 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003438 {
Janos Follath315b51c2018-07-09 16:04:51 +01003439 status = mbedtls_to_psa_error( cipher_ret );
3440 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003441 }
Janos Follath315b51c2018-07-09 16:04:51 +01003442
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003443 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003444 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003445 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003446 memcpy( output, temp_output_buffer, *output_length );
3447 else
3448 {
Janos Follath315b51c2018-07-09 16:04:51 +01003449 status = PSA_ERROR_BUFFER_TOO_SMALL;
3450 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003451 }
mohammad1603503973b2018-03-12 15:59:30 +02003452
Gilles Peskine3f108122018-12-07 18:14:53 +01003453 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003454 status = psa_cipher_abort( operation );
3455
3456 return( status );
3457
3458error:
3459
3460 *output_length = 0;
3461
Gilles Peskine3f108122018-12-07 18:14:53 +01003462 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003463 (void) psa_cipher_abort( operation );
3464
3465 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003466}
3467
Gilles Peskinee553c652018-06-04 16:22:46 +02003468psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3469{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003470 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003471 {
3472 /* The object has (apparently) been initialized but it is not
3473 * in use. It's ok to call abort on such an object, and there's
3474 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003475 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003476 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003477
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003478 /* Sanity check (shouldn't happen: operation->alg should
3479 * always have been initialized to a valid value). */
3480 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3481 return( PSA_ERROR_BAD_STATE );
3482
mohammad1603503973b2018-03-12 15:59:30 +02003483 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003484
Moran Peker41deec42018-04-04 15:43:05 +03003485 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003486 operation->key_set = 0;
3487 operation->iv_set = 0;
3488 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003489 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003490 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003491
Moran Peker395db872018-05-31 14:07:14 +03003492 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003493}
3494
Gilles Peskinea0655c32018-04-30 17:06:50 +02003495
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003496
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003497
mohammad16035955c982018-04-26 00:53:03 +03003498/****************************************************************/
3499/* AEAD */
3500/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003501
Gilles Peskineedf9a652018-08-17 18:11:56 +02003502typedef struct
3503{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003504 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003505 const mbedtls_cipher_info_t *cipher_info;
3506 union
3507 {
3508#if defined(MBEDTLS_CCM_C)
3509 mbedtls_ccm_context ccm;
3510#endif /* MBEDTLS_CCM_C */
3511#if defined(MBEDTLS_GCM_C)
3512 mbedtls_gcm_context gcm;
3513#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003514#if defined(MBEDTLS_CHACHAPOLY_C)
3515 mbedtls_chachapoly_context chachapoly;
3516#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003517 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003518 psa_algorithm_t core_alg;
3519 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003520 uint8_t tag_length;
3521} aead_operation_t;
3522
Gilles Peskine30a9e412019-01-14 18:36:12 +01003523static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003524{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003525 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003526 {
3527#if defined(MBEDTLS_CCM_C)
3528 case PSA_ALG_CCM:
3529 mbedtls_ccm_free( &operation->ctx.ccm );
3530 break;
3531#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003532#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003533 case PSA_ALG_GCM:
3534 mbedtls_gcm_free( &operation->ctx.gcm );
3535 break;
3536#endif /* MBEDTLS_GCM_C */
3537 }
3538}
3539
3540static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003541 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003542 psa_key_usage_t usage,
3543 psa_algorithm_t alg )
3544{
3545 psa_status_t status;
3546 size_t key_bits;
3547 mbedtls_cipher_id_t cipher_id;
3548
Gilles Peskinec5487a82018-12-03 18:08:14 +01003549 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003550 if( status != PSA_SUCCESS )
3551 return( status );
3552
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003553 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003554
3555 operation->cipher_info =
3556 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3557 &cipher_id );
3558 if( operation->cipher_info == NULL )
3559 return( PSA_ERROR_NOT_SUPPORTED );
3560
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003561 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003562 {
3563#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003564 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3565 operation->core_alg = PSA_ALG_CCM;
3566 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003567 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3568 * The call to mbedtls_ccm_encrypt_and_tag or
3569 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003570 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3571 return( PSA_ERROR_INVALID_ARGUMENT );
3572 mbedtls_ccm_init( &operation->ctx.ccm );
3573 status = mbedtls_to_psa_error(
3574 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3575 operation->slot->data.raw.data,
3576 (unsigned int) key_bits ) );
3577 if( status != 0 )
3578 goto cleanup;
3579 break;
3580#endif /* MBEDTLS_CCM_C */
3581
3582#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003583 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3584 operation->core_alg = PSA_ALG_GCM;
3585 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003586 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3587 * The call to mbedtls_gcm_crypt_and_tag or
3588 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003589 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3590 return( PSA_ERROR_INVALID_ARGUMENT );
3591 mbedtls_gcm_init( &operation->ctx.gcm );
3592 status = mbedtls_to_psa_error(
3593 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3594 operation->slot->data.raw.data,
3595 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003596 if( status != 0 )
3597 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003598 break;
3599#endif /* MBEDTLS_GCM_C */
3600
Gilles Peskine26869f22019-05-06 15:25:00 +02003601#if defined(MBEDTLS_CHACHAPOLY_C)
3602 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3603 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3604 operation->full_tag_length = 16;
3605 /* We only support the default tag length. */
3606 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3607 return( PSA_ERROR_NOT_SUPPORTED );
3608 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3609 status = mbedtls_to_psa_error(
3610 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3611 operation->slot->data.raw.data ) );
3612 if( status != 0 )
3613 goto cleanup;
3614 break;
3615#endif /* MBEDTLS_CHACHAPOLY_C */
3616
Gilles Peskineedf9a652018-08-17 18:11:56 +02003617 default:
3618 return( PSA_ERROR_NOT_SUPPORTED );
3619 }
3620
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003621 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3622 {
3623 status = PSA_ERROR_INVALID_ARGUMENT;
3624 goto cleanup;
3625 }
3626 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003627
Gilles Peskineedf9a652018-08-17 18:11:56 +02003628 return( PSA_SUCCESS );
3629
3630cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003631 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003632 return( status );
3633}
3634
Gilles Peskinec5487a82018-12-03 18:08:14 +01003635psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003636 psa_algorithm_t alg,
3637 const uint8_t *nonce,
3638 size_t nonce_length,
3639 const uint8_t *additional_data,
3640 size_t additional_data_length,
3641 const uint8_t *plaintext,
3642 size_t plaintext_length,
3643 uint8_t *ciphertext,
3644 size_t ciphertext_size,
3645 size_t *ciphertext_length )
3646{
mohammad16035955c982018-04-26 00:53:03 +03003647 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003648 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003649 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003650
mohammad1603f08a5502018-06-03 15:05:47 +03003651 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003652
Gilles Peskinec5487a82018-12-03 18:08:14 +01003653 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003654 if( status != PSA_SUCCESS )
3655 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003656
Gilles Peskineedf9a652018-08-17 18:11:56 +02003657 /* For all currently supported modes, the tag is at the end of the
3658 * ciphertext. */
3659 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3660 {
3661 status = PSA_ERROR_BUFFER_TOO_SMALL;
3662 goto exit;
3663 }
3664 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003665
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003666#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003667 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003668 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003669 status = mbedtls_to_psa_error(
3670 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3671 MBEDTLS_GCM_ENCRYPT,
3672 plaintext_length,
3673 nonce, nonce_length,
3674 additional_data, additional_data_length,
3675 plaintext, ciphertext,
3676 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003677 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003678 else
3679#endif /* MBEDTLS_GCM_C */
3680#if defined(MBEDTLS_CCM_C)
3681 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003682 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003683 status = mbedtls_to_psa_error(
3684 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3685 plaintext_length,
3686 nonce, nonce_length,
3687 additional_data,
3688 additional_data_length,
3689 plaintext, ciphertext,
3690 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003691 }
mohammad16035c8845f2018-05-09 05:40:09 -07003692 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003693#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003694#if defined(MBEDTLS_CHACHAPOLY_C)
3695 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3696 {
3697 if( nonce_length != 12 || operation.tag_length != 16 )
3698 {
3699 status = PSA_ERROR_NOT_SUPPORTED;
3700 goto exit;
3701 }
3702 status = mbedtls_to_psa_error(
3703 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3704 plaintext_length,
3705 nonce,
3706 additional_data,
3707 additional_data_length,
3708 plaintext,
3709 ciphertext,
3710 tag ) );
3711 }
3712 else
3713#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003714 {
mohammad1603554faad2018-06-03 15:07:38 +03003715 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003716 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003717
Gilles Peskineedf9a652018-08-17 18:11:56 +02003718 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3719 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003720
Gilles Peskineedf9a652018-08-17 18:11:56 +02003721exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003722 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003723 if( status == PSA_SUCCESS )
3724 *ciphertext_length = plaintext_length + operation.tag_length;
3725 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003726}
3727
Gilles Peskineee652a32018-06-01 19:23:52 +02003728/* Locate the tag in a ciphertext buffer containing the encrypted data
3729 * followed by the tag. Return the length of the part preceding the tag in
3730 * *plaintext_length. This is the size of the plaintext in modes where
3731 * the encrypted data has the same size as the plaintext, such as
3732 * CCM and GCM. */
3733static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3734 const uint8_t *ciphertext,
3735 size_t ciphertext_length,
3736 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003737 const uint8_t **p_tag )
3738{
3739 size_t payload_length;
3740 if( tag_length > ciphertext_length )
3741 return( PSA_ERROR_INVALID_ARGUMENT );
3742 payload_length = ciphertext_length - tag_length;
3743 if( payload_length > plaintext_size )
3744 return( PSA_ERROR_BUFFER_TOO_SMALL );
3745 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003746 return( PSA_SUCCESS );
3747}
3748
Gilles Peskinec5487a82018-12-03 18:08:14 +01003749psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003750 psa_algorithm_t alg,
3751 const uint8_t *nonce,
3752 size_t nonce_length,
3753 const uint8_t *additional_data,
3754 size_t additional_data_length,
3755 const uint8_t *ciphertext,
3756 size_t ciphertext_length,
3757 uint8_t *plaintext,
3758 size_t plaintext_size,
3759 size_t *plaintext_length )
3760{
mohammad16035955c982018-04-26 00:53:03 +03003761 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003762 aead_operation_t operation;
3763 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003764
Gilles Peskineee652a32018-06-01 19:23:52 +02003765 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003766
Gilles Peskinec5487a82018-12-03 18:08:14 +01003767 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003768 if( status != PSA_SUCCESS )
3769 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003770
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003771 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3772 ciphertext, ciphertext_length,
3773 plaintext_size, &tag );
3774 if( status != PSA_SUCCESS )
3775 goto exit;
3776
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003777#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003778 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003779 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780 status = mbedtls_to_psa_error(
3781 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3782 ciphertext_length - operation.tag_length,
3783 nonce, nonce_length,
3784 additional_data,
3785 additional_data_length,
3786 tag, operation.tag_length,
3787 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003788 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003789 else
3790#endif /* MBEDTLS_GCM_C */
3791#if defined(MBEDTLS_CCM_C)
3792 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003793 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003794 status = mbedtls_to_psa_error(
3795 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3796 ciphertext_length - operation.tag_length,
3797 nonce, nonce_length,
3798 additional_data,
3799 additional_data_length,
3800 ciphertext, plaintext,
3801 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003802 }
mohammad160339574652018-06-01 04:39:53 -07003803 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003804#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003805#if defined(MBEDTLS_CHACHAPOLY_C)
3806 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3807 {
3808 if( nonce_length != 12 || operation.tag_length != 16 )
3809 {
3810 status = PSA_ERROR_NOT_SUPPORTED;
3811 goto exit;
3812 }
3813 status = mbedtls_to_psa_error(
3814 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3815 ciphertext_length - operation.tag_length,
3816 nonce,
3817 additional_data,
3818 additional_data_length,
3819 tag,
3820 ciphertext,
3821 plaintext ) );
3822 }
3823 else
3824#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003825 {
mohammad1603554faad2018-06-03 15:07:38 +03003826 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003827 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003828
Gilles Peskineedf9a652018-08-17 18:11:56 +02003829 if( status != PSA_SUCCESS && plaintext_size != 0 )
3830 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003831
Gilles Peskineedf9a652018-08-17 18:11:56 +02003832exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003833 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003834 if( status == PSA_SUCCESS )
3835 *plaintext_length = ciphertext_length - operation.tag_length;
3836 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003837}
3838
Gilles Peskinea0655c32018-04-30 17:06:50 +02003839
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003840
Gilles Peskine20035e32018-02-03 22:44:14 +01003841/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003842/* Generators */
3843/****************************************************************/
3844
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003845#define HKDF_STATE_INIT 0 /* no input yet */
3846#define HKDF_STATE_STARTED 1 /* got salt */
3847#define HKDF_STATE_KEYED 2 /* got key */
3848#define HKDF_STATE_OUTPUT 3 /* output started */
3849
Gilles Peskinecbe66502019-05-16 16:59:18 +02003850static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003851 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003852{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003853 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3854 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003855 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003856 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003857}
3858
3859
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003860psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003861{
3862 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003863 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003864 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003865 {
3866 /* The object has (apparently) been initialized but it is not
3867 * in use. It's ok to call abort on such an object, and there's
3868 * nothing to do. */
3869 }
3870 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003871#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003872 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003873 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003874 mbedtls_free( operation->ctx.hkdf.info );
3875 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003876 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003877 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003878 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003879 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003880 {
Janos Follath6a1d2622019-06-11 10:37:28 +01003881#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003882 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003883 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003884 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3885 operation->ctx.tls12_prf.key_len );
3886 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003887 }
Hanno Becker580fba12018-11-13 20:50:45 +00003888
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003889 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003890 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003891 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3892 operation->ctx.tls12_prf.Ai_with_seed_len );
3893 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003894 }
Janos Follath6a1d2622019-06-11 10:37:28 +01003895#else
3896 if( operation->ctx.tls12_prf.seed != NULL )
3897 {
3898 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3899 operation->ctx.tls12_prf.seed_length );
3900 mbedtls_free( operation->ctx.tls12_prf.seed );
3901 }
3902
3903 if( operation->ctx.tls12_prf.label != NULL )
3904 {
3905 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3906 operation->ctx.tls12_prf.label_length );
3907 mbedtls_free( operation->ctx.tls12_prf.label );
3908 }
3909
3910 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
3911
3912 /* We leave the fields Ai and output_block to be erased safely by the
3913 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01003914#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003915 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003916 else
3917#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003918 {
3919 status = PSA_ERROR_BAD_STATE;
3920 }
Janos Follath083036a2019-06-11 10:22:26 +01003921 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003922 return( status );
3923}
3924
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003925psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003926 size_t *capacity)
3927{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003929 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003930 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003931 return PSA_ERROR_BAD_STATE;
3932 }
3933
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003934 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003935 return( PSA_SUCCESS );
3936}
3937
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003938psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003939 size_t capacity )
3940{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003941 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003942 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003943 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003944 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003945 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003946 return( PSA_SUCCESS );
3947}
3948
Gilles Peskinebef7f142018-07-12 17:22:21 +02003949#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003950/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003951 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003952static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003953 psa_algorithm_t hash_alg,
3954 uint8_t *output,
3955 size_t output_length )
3956{
3957 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3958 psa_status_t status;
3959
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003960 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3961 return( PSA_ERROR_BAD_STATE );
3962 hkdf->state = HKDF_STATE_OUTPUT;
3963
Gilles Peskinebef7f142018-07-12 17:22:21 +02003964 while( output_length != 0 )
3965 {
3966 /* Copy what remains of the current block */
3967 uint8_t n = hash_length - hkdf->offset_in_block;
3968 if( n > output_length )
3969 n = (uint8_t) output_length;
3970 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3971 output += n;
3972 output_length -= n;
3973 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003974 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003975 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003976 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003977 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003978 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003979 * object was corrupted or if this function is called directly
3980 * inside the library. */
3981 if( hkdf->block_number == 0xff )
3982 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003983
3984 /* We need a new block */
3985 ++hkdf->block_number;
3986 hkdf->offset_in_block = 0;
3987 status = psa_hmac_setup_internal( &hkdf->hmac,
3988 hkdf->prk, hash_length,
3989 hash_alg );
3990 if( status != PSA_SUCCESS )
3991 return( status );
3992 if( hkdf->block_number != 1 )
3993 {
3994 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3995 hkdf->output_block,
3996 hash_length );
3997 if( status != PSA_SUCCESS )
3998 return( status );
3999 }
4000 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4001 hkdf->info,
4002 hkdf->info_length );
4003 if( status != PSA_SUCCESS )
4004 return( status );
4005 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4006 &hkdf->block_number, 1 );
4007 if( status != PSA_SUCCESS )
4008 return( status );
4009 status = psa_hmac_finish_internal( &hkdf->hmac,
4010 hkdf->output_block,
4011 sizeof( hkdf->output_block ) );
4012 if( status != PSA_SUCCESS )
4013 return( status );
4014 }
4015
4016 return( PSA_SUCCESS );
4017}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004018
Janos Follath999f6482019-06-11 12:04:10 +01004019#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004020static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4021 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004022 psa_algorithm_t alg )
4023{
4024 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4025 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4026 psa_hmac_internal_data hmac;
4027 psa_status_t status, cleanup_status;
4028
Hanno Becker3b339e22018-11-13 20:56:14 +00004029 unsigned char *Ai;
4030 size_t Ai_len;
4031
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004032 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004033 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004034 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004035 * object was corrupted or if this function is called directly
4036 * inside the library. */
4037 if( tls12_prf->block_number == 0xff )
4038 return( PSA_ERROR_BAD_STATE );
4039
4040 /* We need a new block */
4041 ++tls12_prf->block_number;
4042 tls12_prf->offset_in_block = 0;
4043
4044 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4045 *
4046 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4047 *
4048 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4049 * HMAC_hash(secret, A(2) + seed) +
4050 * HMAC_hash(secret, A(3) + seed) + ...
4051 *
4052 * A(0) = seed
4053 * A(i) = HMAC_hash( secret, A(i-1) )
4054 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004055 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004056 * `HMAC_hash(secret, A(i) + seed)` from which the output
4057 * is currently extracted as `output_block`, while
4058 * `A(i) + seed` is stored in `Ai_with_seed`.
4059 *
4060 * Generating a new block means recalculating `Ai_with_seed`
4061 * from the A(i)-part of it, and afterwards recalculating
4062 * `output_block`.
4063 *
4064 * A(0) is computed at setup time.
4065 *
4066 */
4067
4068 psa_hmac_init_internal( &hmac );
4069
4070 /* We must distinguish the calculation of A(1) from those
4071 * of A(2) and higher, because A(0)=seed has a different
4072 * length than the other A(i). */
4073 if( tls12_prf->block_number == 1 )
4074 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004075 Ai = tls12_prf->Ai_with_seed + hash_length;
4076 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004077 }
4078 else
4079 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004080 Ai = tls12_prf->Ai_with_seed;
4081 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004082 }
4083
Hanno Becker3b339e22018-11-13 20:56:14 +00004084 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4085 status = psa_hmac_setup_internal( &hmac,
4086 tls12_prf->key,
4087 tls12_prf->key_len,
4088 hash_alg );
4089 if( status != PSA_SUCCESS )
4090 goto cleanup;
4091
4092 status = psa_hash_update( &hmac.hash_ctx,
4093 Ai, Ai_len );
4094 if( status != PSA_SUCCESS )
4095 goto cleanup;
4096
4097 status = psa_hmac_finish_internal( &hmac,
4098 tls12_prf->Ai_with_seed,
4099 hash_length );
4100 if( status != PSA_SUCCESS )
4101 goto cleanup;
4102
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004103 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4104 status = psa_hmac_setup_internal( &hmac,
4105 tls12_prf->key,
4106 tls12_prf->key_len,
4107 hash_alg );
4108 if( status != PSA_SUCCESS )
4109 goto cleanup;
4110
4111 status = psa_hash_update( &hmac.hash_ctx,
4112 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004113 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004114 if( status != PSA_SUCCESS )
4115 goto cleanup;
4116
4117 status = psa_hmac_finish_internal( &hmac,
4118 tls12_prf->output_block,
4119 hash_length );
4120 if( status != PSA_SUCCESS )
4121 goto cleanup;
4122
4123cleanup:
4124
4125 cleanup_status = psa_hmac_abort_internal( &hmac );
4126 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4127 status = cleanup_status;
4128
4129 return( status );
4130}
Janos Follath7742fee2019-06-17 12:58:10 +01004131#else
4132static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4133 psa_tls12_prf_key_derivation_t *tls12_prf,
4134 psa_algorithm_t alg )
4135{
4136 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4137 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004138 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4139 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004140
Janos Follath7742fee2019-06-17 12:58:10 +01004141 /* We can't be wanting more output after block 0xff, otherwise
4142 * the capacity check in psa_key_derivation_output_bytes() would have
4143 * prevented this call. It could happen only if the operation
4144 * object was corrupted or if this function is called directly
4145 * inside the library. */
4146 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004147 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004148
4149 /* We need a new block */
4150 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004151 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004152
4153 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4154 *
4155 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4156 *
4157 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4158 * HMAC_hash(secret, A(2) + seed) +
4159 * HMAC_hash(secret, A(3) + seed) + ...
4160 *
4161 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004162 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004163 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004164 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004165 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004166 * is currently extracted as `output_block` and where i is
4167 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004168 */
4169
Janos Follathea29bfb2019-06-19 12:21:20 +01004170 /* Save the hash context before using it, to preserve the hash state with
4171 * only the inner padding in it. We need this, because inner padding depends
4172 * on the key (secret in the RFC's terminology). */
4173 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4174 if( status != PSA_SUCCESS )
4175 goto cleanup;
4176
4177 /* Calculate A(i) where i = tls12_prf->block_number. */
4178 if( tls12_prf->block_number == 1 )
4179 {
4180 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4181 * the variable seed and in this instance means it in the context of the
4182 * P_hash function, where seed = label + seed.) */
4183 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4184 tls12_prf->label, tls12_prf->label_length );
4185 if( status != PSA_SUCCESS )
4186 goto cleanup;
4187 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4188 tls12_prf->seed, tls12_prf->seed_length );
4189 if( status != PSA_SUCCESS )
4190 goto cleanup;
4191 }
4192 else
4193 {
4194 /* A(i) = HMAC_hash(secret, A(i-1)) */
4195 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4196 tls12_prf->Ai, hash_length );
4197 if( status != PSA_SUCCESS )
4198 goto cleanup;
4199 }
4200
4201 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4202 tls12_prf->Ai, hash_length );
4203 if( status != PSA_SUCCESS )
4204 goto cleanup;
4205 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4206 if( status != PSA_SUCCESS )
4207 goto cleanup;
4208
4209 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4210 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4211 tls12_prf->Ai, hash_length );
4212 if( status != PSA_SUCCESS )
4213 goto cleanup;
4214 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4215 tls12_prf->label, tls12_prf->label_length );
4216 if( status != PSA_SUCCESS )
4217 goto cleanup;
4218 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4219 tls12_prf->seed, tls12_prf->seed_length );
4220 if( status != PSA_SUCCESS )
4221 goto cleanup;
4222 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4223 tls12_prf->output_block, hash_length );
4224 if( status != PSA_SUCCESS )
4225 goto cleanup;
4226 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4227 if( status != PSA_SUCCESS )
4228 goto cleanup;
4229
Janos Follath7742fee2019-06-17 12:58:10 +01004230
4231cleanup:
4232
Janos Follathea29bfb2019-06-19 12:21:20 +01004233 cleanup_status = psa_hash_abort( &backup );
4234 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4235 status = cleanup_status;
4236
4237 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004238}
Janos Follath999f6482019-06-11 12:04:10 +01004239#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004240
Janos Follath999f6482019-06-11 12:04:10 +01004241#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004242/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004243 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004244static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004245 psa_tls12_prf_key_derivation_t *tls12_prf,
4246 psa_algorithm_t alg,
4247 uint8_t *output,
4248 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004249{
4250 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4251 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4252 psa_status_t status;
4253
4254 while( output_length != 0 )
4255 {
4256 /* Copy what remains of the current block */
4257 uint8_t n = hash_length - tls12_prf->offset_in_block;
4258
4259 /* Check if we have fully processed the current block. */
4260 if( n == 0 )
4261 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004262 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004263 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004264 if( status != PSA_SUCCESS )
4265 return( status );
4266
4267 continue;
4268 }
4269
4270 if( n > output_length )
4271 n = (uint8_t) output_length;
4272 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4273 n );
4274 output += n;
4275 output_length -= n;
4276 tls12_prf->offset_in_block += n;
4277 }
4278
4279 return( PSA_SUCCESS );
4280}
Janos Follath844eb0e2019-06-19 12:10:49 +01004281#else
4282static psa_status_t psa_key_derivation_tls12_prf_read(
4283 psa_tls12_prf_key_derivation_t *tls12_prf,
4284 psa_algorithm_t alg,
4285 uint8_t *output,
4286 size_t output_length )
4287{
4288 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4289 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4290 psa_status_t status;
4291 uint8_t offset, length;
4292
4293 while( output_length != 0 )
4294 {
4295 /* Check if we have fully processed the current block. */
4296 if( tls12_prf->left_in_block == 0 )
4297 {
4298 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4299 alg );
4300 if( status != PSA_SUCCESS )
4301 return( status );
4302
4303 continue;
4304 }
4305
4306 if( tls12_prf->left_in_block > output_length )
4307 length = (uint8_t) output_length;
4308 else
4309 length = tls12_prf->left_in_block;
4310
4311 offset = hash_length - tls12_prf->left_in_block;
4312 memcpy( output, tls12_prf->output_block + offset, length );
4313 output += length;
4314 output_length -= length;
4315 tls12_prf->left_in_block -= length;
4316 }
4317
4318 return( PSA_SUCCESS );
4319}
Janos Follath999f6482019-06-11 12:04:10 +01004320#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004321#endif /* MBEDTLS_MD_C */
4322
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004323psa_status_t psa_key_derivation_output_bytes(
4324 psa_key_derivation_operation_t *operation,
4325 uint8_t *output,
4326 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004327{
4328 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004329 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004331 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004332 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004333 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004334 return PSA_ERROR_BAD_STATE;
4335 }
4336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004337 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004338 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004339 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004340 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004342 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004343 goto exit;
4344 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004346 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004348 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004349 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4350 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004352 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004353 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004354 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004356
Gilles Peskinebef7f142018-07-12 17:22:21 +02004357#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004358 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004359 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004360 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004362 output, output_length );
4363 }
Janos Follathadbec812019-06-14 11:05:39 +01004364 else
Janos Follathadbec812019-06-14 11:05:39 +01004365 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004366 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004367 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004369 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004370 output_length );
4371 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004372 else
4373#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004374 {
4375 return( PSA_ERROR_BAD_STATE );
4376 }
4377
4378exit:
4379 if( status != PSA_SUCCESS )
4380 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004381 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 * This allows us to differentiate between exhausted operations and
4383 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4384 * operations. */
4385 psa_algorithm_t alg = operation->alg;
4386 psa_key_derivation_abort( operation );
4387 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004388 memset( output, '!', output_length );
4389 }
4390 return( status );
4391}
4392
Gilles Peskine08542d82018-07-19 17:05:42 +02004393#if defined(MBEDTLS_DES_C)
4394static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4395{
4396 if( data_size >= 8 )
4397 mbedtls_des_key_set_parity( data );
4398 if( data_size >= 16 )
4399 mbedtls_des_key_set_parity( data + 8 );
4400 if( data_size >= 24 )
4401 mbedtls_des_key_set_parity( data + 16 );
4402}
4403#endif /* MBEDTLS_DES_C */
4404
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004405static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004406 psa_key_slot_t *slot,
4407 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004409{
4410 uint8_t *data = NULL;
4411 size_t bytes = PSA_BITS_TO_BYTES( bits );
4412 psa_status_t status;
4413
4414 if( ! key_type_is_raw_bytes( slot->type ) )
4415 return( PSA_ERROR_INVALID_ARGUMENT );
4416 if( bits % 8 != 0 )
4417 return( PSA_ERROR_INVALID_ARGUMENT );
4418 data = mbedtls_calloc( 1, bytes );
4419 if( data == NULL )
4420 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4421
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004423 if( status != PSA_SUCCESS )
4424 goto exit;
4425#if defined(MBEDTLS_DES_C)
4426 if( slot->type == PSA_KEY_TYPE_DES )
4427 psa_des_set_key_parity( data, bytes );
4428#endif /* MBEDTLS_DES_C */
4429 status = psa_import_key_into_slot( slot, data, bytes );
4430
4431exit:
4432 mbedtls_free( data );
4433 return( status );
4434}
4435
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004436psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004438 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004439{
4440 psa_status_t status;
4441 psa_key_slot_t *slot = NULL;
4442 status = psa_start_key_creation( attributes, handle, &slot );
4443 if( status == PSA_SUCCESS )
4444 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004445 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004446 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004448 }
4449 if( status == PSA_SUCCESS )
4450 status = psa_finish_key_creation( slot );
4451 if( status != PSA_SUCCESS )
4452 {
4453 psa_fail_key_creation( slot );
4454 *handle = 0;
4455 }
4456 return( status );
4457}
4458
Gilles Peskineeab56e42018-07-12 17:12:33 +02004459
4460
4461/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004462/* Key derivation */
4463/****************************************************************/
4464
Gilles Peskinea05219c2018-11-16 16:02:56 +01004465#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004466#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004468 * of the HKDF algorithm.
4469 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004470 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004471 * to potentially free embedded data structures and wipe confidential data.
4472 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004473static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004474 const uint8_t *secret,
4475 size_t secret_length,
4476 psa_algorithm_t hash_alg,
4477 const uint8_t *salt,
4478 size_t salt_length,
4479 const uint8_t *label,
4480 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004481{
4482 psa_status_t status;
4483 status = psa_hmac_setup_internal( &hkdf->hmac,
4484 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004485 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004486 if( status != PSA_SUCCESS )
4487 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004488 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004489 if( status != PSA_SUCCESS )
4490 return( status );
4491 status = psa_hmac_finish_internal( &hkdf->hmac,
4492 hkdf->prk,
4493 sizeof( hkdf->prk ) );
4494 if( status != PSA_SUCCESS )
4495 return( status );
4496 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4497 hkdf->block_number = 0;
4498 hkdf->info_length = label_length;
4499 if( label_length != 0 )
4500 {
4501 hkdf->info = mbedtls_calloc( 1, label_length );
4502 if( hkdf->info == NULL )
4503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4504 memcpy( hkdf->info, label, label_length );
4505 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004506 hkdf->state = HKDF_STATE_KEYED;
4507 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004508 return( PSA_SUCCESS );
4509}
Janos Follath71a4c912019-06-11 09:14:47 +01004510#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004511#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004512
Gilles Peskinea05219c2018-11-16 16:02:56 +01004513#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004514#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004515/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004516 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004517 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004518 * to potentially free embedded data structures and wipe confidential data.
4519 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004520static psa_status_t psa_key_derivation_tls12_prf_setup(
4521 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004522 const unsigned char *key,
4523 size_t key_len,
4524 psa_algorithm_t hash_alg,
4525 const uint8_t *salt,
4526 size_t salt_length,
4527 const uint8_t *label,
4528 size_t label_length )
4529{
4530 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004531 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4532 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004533
4534 tls12_prf->key = mbedtls_calloc( 1, key_len );
4535 if( tls12_prf->key == NULL )
4536 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4537 tls12_prf->key_len = key_len;
4538 memcpy( tls12_prf->key, key, key_len );
4539
Hanno Becker580fba12018-11-13 20:50:45 +00004540 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004541 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004542 if( overflow )
4543 return( PSA_ERROR_INVALID_ARGUMENT );
4544
4545 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4546 if( tls12_prf->Ai_with_seed == NULL )
4547 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4548 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4549
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004550 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4551 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004552 if( label_length != 0 )
4553 {
4554 memcpy( tls12_prf->Ai_with_seed + hash_length,
4555 label, label_length );
4556 }
4557
4558 if( salt_length != 0 )
4559 {
4560 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4561 salt, salt_length );
4562 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004563
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004564 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004565 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004566 tls12_prf->block_number = 0;
4567 tls12_prf->offset_in_block = hash_length;
4568
4569 return( PSA_SUCCESS );
4570}
Janos Follath71a4c912019-06-11 09:14:47 +01004571#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004572
Janos Follath71a4c912019-06-11 09:14:47 +01004573#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004574/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004575static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4576 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004577 const unsigned char *psk,
4578 size_t psk_len,
4579 psa_algorithm_t hash_alg,
4580 const uint8_t *salt,
4581 size_t salt_length,
4582 const uint8_t *label,
4583 size_t label_length )
4584{
4585 psa_status_t status;
4586 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4587
4588 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4589 return( PSA_ERROR_INVALID_ARGUMENT );
4590
4591 /* Quoting RFC 4279, Section 2:
4592 *
4593 * The premaster secret is formed as follows: if the PSK is N octets
4594 * long, concatenate a uint16 with the value N, N zero octets, a second
4595 * uint16 with the value N, and the PSK itself.
4596 */
4597
4598 pms[0] = ( psk_len >> 8 ) & 0xff;
4599 pms[1] = ( psk_len >> 0 ) & 0xff;
4600 memset( pms + 2, 0, psk_len );
4601 pms[2 + psk_len + 0] = pms[0];
4602 pms[2 + psk_len + 1] = pms[1];
4603 memcpy( pms + 4 + psk_len, psk, psk_len );
4604
Gilles Peskinecbe66502019-05-16 16:59:18 +02004605 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004606 pms, 4 + 2 * psk_len,
4607 hash_alg,
4608 salt, salt_length,
4609 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004610
Gilles Peskine3f108122018-12-07 18:14:53 +01004611 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004612 return( status );
4613}
Janos Follath71a4c912019-06-11 09:14:47 +01004614#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004615#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004616
Janos Follath71a4c912019-06-11 09:14:47 +01004617#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004618/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004619 * to potentially free embedded data structures and wipe confidential data.
4620 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004621static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004623 const uint8_t *secret, size_t secret_length,
4624 psa_algorithm_t alg,
4625 const uint8_t *salt, size_t salt_length,
4626 const uint8_t *label, size_t label_length,
4627 size_t capacity )
4628{
4629 psa_status_t status;
4630 size_t max_capacity;
4631
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 /* Set operation->alg even on failure so that abort knows what to do. */
4633 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004634
4635#if defined(MBEDTLS_MD_C)
4636 if( PSA_ALG_IS_HKDF( alg ) )
4637 {
4638 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4639 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4640 if( hash_size == 0 )
4641 return( PSA_ERROR_NOT_SUPPORTED );
4642 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004643 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004644 secret, secret_length,
4645 hash_alg,
4646 salt, salt_length,
4647 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004648 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004649 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4650 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4651 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004652 {
4653 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4654 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4655
4656 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4657 if( hash_alg != PSA_ALG_SHA_256 &&
4658 hash_alg != PSA_ALG_SHA_384 )
4659 {
4660 return( PSA_ERROR_NOT_SUPPORTED );
4661 }
4662
4663 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004664
4665 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4666 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004668 secret, secret_length,
4669 hash_alg, salt, salt_length,
4670 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004671 }
4672 else
4673 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004674 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004676 secret, secret_length,
4677 hash_alg, salt, salt_length,
4678 label, label_length );
4679 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004680 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004681 else
4682#endif
4683 {
4684 return( PSA_ERROR_NOT_SUPPORTED );
4685 }
4686
4687 if( status != PSA_SUCCESS )
4688 return( status );
4689
4690 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004692 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004694 else
4695 return( PSA_ERROR_INVALID_ARGUMENT );
4696
4697 return( PSA_SUCCESS );
4698}
Janos Follath71a4c912019-06-11 09:14:47 +01004699#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004700
Janos Follath71a4c912019-06-11 09:14:47 +01004701#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004703 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004704 psa_algorithm_t alg,
4705 const uint8_t *salt,
4706 size_t salt_length,
4707 const uint8_t *label,
4708 size_t label_length,
4709 size_t capacity )
4710{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004711 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004712 psa_status_t status;
4713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004715 return( PSA_ERROR_BAD_STATE );
4716
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004717 /* Make sure that alg is a key derivation algorithm. This prevents
4718 * key selection algorithms, which psa_key_derivation_internal
4719 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004720 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4721 return( PSA_ERROR_INVALID_ARGUMENT );
4722
Gilles Peskinec5487a82018-12-03 18:08:14 +01004723 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004724 if( status != PSA_SUCCESS )
4725 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004726
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004727 if( slot->type != PSA_KEY_TYPE_DERIVE )
4728 return( PSA_ERROR_INVALID_ARGUMENT );
4729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004731 slot->data.raw.data,
4732 slot->data.raw.bytes,
4733 alg,
4734 salt, salt_length,
4735 label, label_length,
4736 capacity );
4737 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004739 return( status );
4740}
Janos Follath71a4c912019-06-11 09:14:47 +01004741#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004742
Gilles Peskine969c5d62019-01-16 15:53:06 +01004743static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004745 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004746{
Janos Follath5fe19732019-06-20 15:09:30 +01004747 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4748 * initialisers for this union leave some bytes unspecified.) */
4749 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4750
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004752#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004753 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4754 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4755 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004756 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004758 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4759 if( hash_size == 0 )
4760 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004761 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4762 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004763 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004764 {
4765 return( PSA_ERROR_NOT_SUPPORTED );
4766 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004768 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004769 }
4770#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004771 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004772 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004773}
4774
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004776 psa_algorithm_t alg )
4777{
4778 psa_status_t status;
4779
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004781 return( PSA_ERROR_BAD_STATE );
4782
Gilles Peskine6843c292019-01-18 16:44:49 +01004783 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4784 return( PSA_ERROR_INVALID_ARGUMENT );
4785 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004786 {
4787 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004789 }
4790 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4791 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004793 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004794 else
4795 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004796
4797 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004799 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004800}
4801
4802#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004803static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004804 psa_algorithm_t hash_alg,
4805 psa_key_derivation_step_t step,
4806 const uint8_t *data,
4807 size_t data_length )
4808{
4809 psa_status_t status;
4810 switch( step )
4811 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004812 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004813 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004814 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004815 status = psa_hmac_setup_internal( &hkdf->hmac,
4816 data, data_length,
4817 hash_alg );
4818 if( status != PSA_SUCCESS )
4819 return( status );
4820 hkdf->state = HKDF_STATE_STARTED;
4821 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004822 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823 /* If no salt was provided, use an empty salt. */
4824 if( hkdf->state == HKDF_STATE_INIT )
4825 {
4826 status = psa_hmac_setup_internal( &hkdf->hmac,
4827 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004828 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829 if( status != PSA_SUCCESS )
4830 return( status );
4831 hkdf->state = HKDF_STATE_STARTED;
4832 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004833 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004834 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004835 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4836 data, data_length );
4837 if( status != PSA_SUCCESS )
4838 return( status );
4839 status = psa_hmac_finish_internal( &hkdf->hmac,
4840 hkdf->prk,
4841 sizeof( hkdf->prk ) );
4842 if( status != PSA_SUCCESS )
4843 return( status );
4844 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4845 hkdf->block_number = 0;
4846 hkdf->state = HKDF_STATE_KEYED;
4847 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004848 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004849 if( hkdf->state == HKDF_STATE_OUTPUT )
4850 return( PSA_ERROR_BAD_STATE );
4851 if( hkdf->info_set )
4852 return( PSA_ERROR_BAD_STATE );
4853 hkdf->info_length = data_length;
4854 if( data_length != 0 )
4855 {
4856 hkdf->info = mbedtls_calloc( 1, data_length );
4857 if( hkdf->info == NULL )
4858 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4859 memcpy( hkdf->info, data, data_length );
4860 }
4861 hkdf->info_set = 1;
4862 return( PSA_SUCCESS );
4863 default:
4864 return( PSA_ERROR_INVALID_ARGUMENT );
4865 }
4866}
Janos Follathb03233e2019-06-11 15:30:30 +01004867
4868#if defined(PSA_PRE_1_0_KEY_DERIVATION)
4869static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4870 psa_algorithm_t hash_alg,
4871 psa_key_derivation_step_t step,
4872 const uint8_t *data,
4873 size_t data_length )
4874{
4875 (void) prf;
4876 (void) hash_alg;
4877 (void) step;
4878 (void) data;
4879 (void) data_length;
4880
4881 return( PSA_ERROR_INVALID_ARGUMENT );
4882}
Janos Follath6660f0e2019-06-17 08:44:03 +01004883
4884static psa_status_t psa_tls12_prf_psk_to_ms_input(
4885 psa_tls12_prf_key_derivation_t *prf,
4886 psa_algorithm_t hash_alg,
4887 psa_key_derivation_step_t step,
4888 const uint8_t *data,
4889 size_t data_length )
4890{
4891 (void) prf;
4892 (void) hash_alg;
4893 (void) step;
4894 (void) data;
4895 (void) data_length;
4896
4897 return( PSA_ERROR_INVALID_ARGUMENT );
4898}
Janos Follathb03233e2019-06-11 15:30:30 +01004899#else
Janos Follathf08e2652019-06-13 09:05:41 +01004900static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4901 const uint8_t *data,
4902 size_t data_length )
4903{
4904 if( prf->state != TLS12_PRF_STATE_INIT )
4905 return( PSA_ERROR_BAD_STATE );
4906
Janos Follathd6dce9f2019-07-04 09:11:38 +01004907 if( data_length != 0 )
4908 {
4909 prf->seed = mbedtls_calloc( 1, data_length );
4910 if( prf->seed == NULL )
4911 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004912
Janos Follathd6dce9f2019-07-04 09:11:38 +01004913 memcpy( prf->seed, data, data_length );
4914 prf->seed_length = data_length;
4915 }
Janos Follathf08e2652019-06-13 09:05:41 +01004916
4917 prf->state = TLS12_PRF_STATE_SEED_SET;
4918
4919 return( PSA_SUCCESS );
4920}
4921
Janos Follath81550542019-06-13 14:26:34 +01004922static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4923 psa_algorithm_t hash_alg,
4924 const uint8_t *data,
4925 size_t data_length )
4926{
4927 psa_status_t status;
4928 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4929 return( PSA_ERROR_BAD_STATE );
4930
4931 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4932 if( status != PSA_SUCCESS )
4933 return( status );
4934
4935 prf->state = TLS12_PRF_STATE_KEY_SET;
4936
4937 return( PSA_SUCCESS );
4938}
4939
Janos Follath6660f0e2019-06-17 08:44:03 +01004940static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4941 psa_tls12_prf_key_derivation_t *prf,
4942 psa_algorithm_t hash_alg,
4943 const uint8_t *data,
4944 size_t data_length )
4945{
4946 psa_status_t status;
4947 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01004948 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004949
4950 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4951 return( PSA_ERROR_INVALID_ARGUMENT );
4952
4953 /* Quoting RFC 4279, Section 2:
4954 *
4955 * The premaster secret is formed as follows: if the PSK is N octets
4956 * long, concatenate a uint16 with the value N, N zero octets, a second
4957 * uint16 with the value N, and the PSK itself.
4958 */
4959
Janos Follath40e13932019-06-26 13:22:29 +01004960 *cur++ = ( data_length >> 8 ) & 0xff;
4961 *cur++ = ( data_length >> 0 ) & 0xff;
4962 memset( cur, 0, data_length );
4963 cur += data_length;
4964 *cur++ = pms[0];
4965 *cur++ = pms[1];
4966 memcpy( cur, data, data_length );
4967 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01004968
Janos Follath40e13932019-06-26 13:22:29 +01004969 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01004970
4971 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4972 return( status );
4973}
4974
Janos Follath63028dd2019-06-13 09:15:47 +01004975static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4976 const uint8_t *data,
4977 size_t data_length )
4978{
4979 if( prf->state != TLS12_PRF_STATE_KEY_SET )
4980 return( PSA_ERROR_BAD_STATE );
4981
Janos Follathd6dce9f2019-07-04 09:11:38 +01004982 if( data_length != 0 )
4983 {
4984 prf->label = mbedtls_calloc( 1, data_length );
4985 if( prf->label == NULL )
4986 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004987
Janos Follathd6dce9f2019-07-04 09:11:38 +01004988 memcpy( prf->label, data, data_length );
4989 prf->label_length = data_length;
4990 }
Janos Follath63028dd2019-06-13 09:15:47 +01004991
4992 prf->state = TLS12_PRF_STATE_LABEL_SET;
4993
4994 return( PSA_SUCCESS );
4995}
4996
Janos Follathb03233e2019-06-11 15:30:30 +01004997static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4998 psa_algorithm_t hash_alg,
4999 psa_key_derivation_step_t step,
5000 const uint8_t *data,
5001 size_t data_length )
5002{
Janos Follathb03233e2019-06-11 15:30:30 +01005003 switch( step )
5004 {
Janos Follathf08e2652019-06-13 09:05:41 +01005005 case PSA_KEY_DERIVATION_INPUT_SEED:
5006 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005007 case PSA_KEY_DERIVATION_INPUT_SECRET:
5008 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005009 case PSA_KEY_DERIVATION_INPUT_LABEL:
5010 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005011 default:
5012 return( PSA_ERROR_INVALID_ARGUMENT );
5013 }
5014}
Janos Follath6660f0e2019-06-17 08:44:03 +01005015
5016static psa_status_t psa_tls12_prf_psk_to_ms_input(
5017 psa_tls12_prf_key_derivation_t *prf,
5018 psa_algorithm_t hash_alg,
5019 psa_key_derivation_step_t step,
5020 const uint8_t *data,
5021 size_t data_length )
5022{
5023 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005024 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005025 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5026 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005027 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005028
5029 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5030}
Janos Follathb03233e2019-06-11 15:30:30 +01005031#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032#endif /* MBEDTLS_MD_C */
5033
Janos Follathb80a94e2019-06-12 15:54:46 +01005034static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005036 psa_key_derivation_step_t step,
5037 const uint8_t *data,
5038 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005039{
5040 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005042
5043#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005045 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005047 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005048 step, data, data_length );
5049 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005050 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005051#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005052#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005053 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005054 {
Janos Follathb03233e2019-06-11 15:30:30 +01005055 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5056 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5057 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005058 }
5059 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5060 {
5061 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5062 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5063 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005064 }
5065 else
5066#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005069 return( PSA_ERROR_BAD_STATE );
5070 }
5071
5072 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005074 return( status );
5075}
5076
Janos Follath51f4a0f2019-06-14 11:35:55 +01005077psa_status_t psa_key_derivation_input_bytes(
5078 psa_key_derivation_operation_t *operation,
5079 psa_key_derivation_step_t step,
5080 const uint8_t *data,
5081 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005082{
Janos Follathc5621512019-06-14 11:27:57 +01005083 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5084 return( PSA_ERROR_INVALID_ARGUMENT );
5085
5086 return( psa_key_derivation_input_internal( operation, step,
5087 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005088}
5089
Janos Follath51f4a0f2019-06-14 11:35:55 +01005090psa_status_t psa_key_derivation_input_key(
5091 psa_key_derivation_operation_t *operation,
5092 psa_key_derivation_step_t step,
5093 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005094{
5095 psa_key_slot_t *slot;
5096 psa_status_t status;
5097 status = psa_get_key_from_slot( handle, &slot,
5098 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005100 if( status != PSA_SUCCESS )
5101 return( status );
5102 if( slot->type != PSA_KEY_TYPE_DERIVE )
5103 return( PSA_ERROR_INVALID_ARGUMENT );
5104 /* Don't allow a key to be used as an input that is usually public.
5105 * This is debatable. It's ok from a cryptographic perspective to
5106 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005107 * the material should be dedicated to a particular input step,
5108 * otherwise this may allow the key to be used in an unintended way
5109 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005110 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005111 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005112 return( psa_key_derivation_input_internal( operation,
5113 step,
5114 slot->data.raw.data,
5115 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116}
5117
Gilles Peskineea0fb492018-07-12 17:17:20 +02005118
5119
5120/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005121/* Key agreement */
5122/****************************************************************/
5123
Gilles Peskinea05219c2018-11-16 16:02:56 +01005124#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005125static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5126 size_t peer_key_length,
5127 const mbedtls_ecp_keypair *our_key,
5128 uint8_t *shared_secret,
5129 size_t shared_secret_size,
5130 size_t *shared_secret_length )
5131{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005132 mbedtls_ecp_keypair *their_key = NULL;
5133 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005134 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005135 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005136
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005137 status = psa_import_ec_public_key(
5138 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5139 peer_key, peer_key_length,
5140 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005141 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005142 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005143
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005144 status = mbedtls_to_psa_error(
5145 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5146 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005147 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005148 status = mbedtls_to_psa_error(
5149 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5150 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005151 goto exit;
5152
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005153 status = mbedtls_to_psa_error(
5154 mbedtls_ecdh_calc_secret( &ecdh,
5155 shared_secret_length,
5156 shared_secret, shared_secret_size,
5157 mbedtls_ctr_drbg_random,
5158 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005159
5160exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005161 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005162 mbedtls_ecp_keypair_free( their_key );
5163 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005164 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005165}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005166#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005167
Gilles Peskine01d718c2018-09-18 12:01:02 +02005168#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5169
Gilles Peskine0216fe12019-04-11 21:23:21 +02005170static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5171 psa_key_slot_t *private_key,
5172 const uint8_t *peer_key,
5173 size_t peer_key_length,
5174 uint8_t *shared_secret,
5175 size_t shared_secret_size,
5176 size_t *shared_secret_length )
5177{
5178 switch( alg )
5179 {
5180#if defined(MBEDTLS_ECDH_C)
5181 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005182 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005183 return( PSA_ERROR_INVALID_ARGUMENT );
5184 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5185 private_key->data.ecp,
5186 shared_secret, shared_secret_size,
5187 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005188#endif /* MBEDTLS_ECDH_C */
5189 default:
5190 (void) private_key;
5191 (void) peer_key;
5192 (void) peer_key_length;
5193 (void) shared_secret;
5194 (void) shared_secret_size;
5195 (void) shared_secret_length;
5196 return( PSA_ERROR_NOT_SUPPORTED );
5197 }
5198}
5199
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005200/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005201 * to potentially free embedded data structures and wipe confidential data.
5202 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005205 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005206 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005208{
5209 psa_status_t status;
5210 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5211 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005213
5214 /* Step 1: run the secret agreement algorithm to generate the shared
5215 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005216 status = psa_key_agreement_raw_internal( ka_alg,
5217 private_key,
5218 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005219 shared_secret,
5220 sizeof( shared_secret ),
5221 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005222 if( status != PSA_SUCCESS )
5223 goto exit;
5224
5225 /* Step 2: set up the key derivation to generate key material from
5226 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005227 status = psa_key_derivation_input_internal( operation, step,
5228 shared_secret,
5229 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005230
Gilles Peskine01d718c2018-09-18 12:01:02 +02005231exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005232 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233 return( status );
5234}
5235
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005237 psa_key_derivation_step_t step,
5238 psa_key_handle_t private_key,
5239 const uint8_t *peer_key,
5240 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005241{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005242 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005243 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005245 return( PSA_ERROR_INVALID_ARGUMENT );
5246 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005248 if( status != PSA_SUCCESS )
5249 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005251 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005253 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005255 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005256}
5257
Gilles Peskinebe697d82019-05-16 18:00:41 +02005258psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5259 psa_key_handle_t private_key,
5260 const uint8_t *peer_key,
5261 size_t peer_key_length,
5262 uint8_t *output,
5263 size_t output_size,
5264 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005265{
5266 psa_key_slot_t *slot;
5267 psa_status_t status;
5268
5269 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5270 {
5271 status = PSA_ERROR_INVALID_ARGUMENT;
5272 goto exit;
5273 }
5274 status = psa_get_key_from_slot( private_key, &slot,
5275 PSA_KEY_USAGE_DERIVE, alg );
5276 if( status != PSA_SUCCESS )
5277 goto exit;
5278
5279 status = psa_key_agreement_raw_internal( alg, slot,
5280 peer_key, peer_key_length,
5281 output, output_size,
5282 output_length );
5283
5284exit:
5285 if( status != PSA_SUCCESS )
5286 {
5287 /* If an error happens and is not handled properly, the output
5288 * may be used as a key to protect sensitive data. Arrange for such
5289 * a key to be random, which is likely to result in decryption or
5290 * verification errors. This is better than filling the buffer with
5291 * some constant data such as zeros, which would result in the data
5292 * being protected with a reproducible, easily knowable key.
5293 */
5294 psa_generate_random( output, output_size );
5295 *output_length = output_size;
5296 }
5297 return( status );
5298}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005299
5300
5301/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005302/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005303/****************************************************************/
5304
5305psa_status_t psa_generate_random( uint8_t *output,
5306 size_t output_size )
5307{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005308 int ret;
5309 GUARD_MODULE_INITIALIZED;
5310
5311 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005312 return( mbedtls_to_psa_error( ret ) );
5313}
5314
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005315#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5316#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005317
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005318psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5319 size_t seed_size )
5320{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005321 if( global_data.initialized )
5322 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005323
5324 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5325 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5326 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5327 return( PSA_ERROR_INVALID_ARGUMENT );
5328
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005329 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005330}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005331#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005332
Gilles Peskinee56e8782019-04-26 17:34:02 +02005333#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5334static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5335 size_t domain_parameters_size,
5336 int *exponent )
5337{
5338 size_t i;
5339 uint32_t acc = 0;
5340
5341 if( domain_parameters_size == 0 )
5342 {
5343 *exponent = 65537;
5344 return( PSA_SUCCESS );
5345 }
5346
5347 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5348 * support values that fit in a 32-bit integer, which is larger than
5349 * int on just about every platform anyway. */
5350 if( domain_parameters_size > sizeof( acc ) )
5351 return( PSA_ERROR_NOT_SUPPORTED );
5352 for( i = 0; i < domain_parameters_size; i++ )
5353 acc = ( acc << 8 ) | domain_parameters[i];
5354 if( acc > INT_MAX )
5355 return( PSA_ERROR_NOT_SUPPORTED );
5356 *exponent = acc;
5357 return( PSA_SUCCESS );
5358}
5359#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5360
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005361static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005362 psa_key_slot_t *slot, size_t bits,
5363 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005364{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005365 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005366
Gilles Peskinee56e8782019-04-26 17:34:02 +02005367 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368 return( PSA_ERROR_INVALID_ARGUMENT );
5369
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005370 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005372 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005373 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005374 if( status != PSA_SUCCESS )
5375 return( status );
5376 status = psa_generate_random( slot->data.raw.data,
5377 slot->data.raw.bytes );
5378 if( status != PSA_SUCCESS )
5379 {
5380 mbedtls_free( slot->data.raw.data );
5381 return( status );
5382 }
5383#if defined(MBEDTLS_DES_C)
5384 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005385 psa_des_set_key_parity( slot->data.raw.data,
5386 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005387#endif /* MBEDTLS_DES_C */
5388 }
5389 else
5390
5391#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005392 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393 {
5394 mbedtls_rsa_context *rsa;
5395 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005396 int exponent;
5397 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005398 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5399 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005400 /* Accept only byte-aligned keys, for the same reasons as
5401 * in psa_import_rsa_key(). */
5402 if( bits % 8 != 0 )
5403 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005404 status = psa_read_rsa_exponent( domain_parameters,
5405 domain_parameters_size,
5406 &exponent );
5407 if( status != PSA_SUCCESS )
5408 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005409 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5410 if( rsa == NULL )
5411 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5412 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5413 ret = mbedtls_rsa_gen_key( rsa,
5414 mbedtls_ctr_drbg_random,
5415 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005416 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005417 exponent );
5418 if( ret != 0 )
5419 {
5420 mbedtls_rsa_free( rsa );
5421 mbedtls_free( rsa );
5422 return( mbedtls_to_psa_error( ret ) );
5423 }
5424 slot->data.rsa = rsa;
5425 }
5426 else
5427#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5428
5429#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005430 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005431 {
5432 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5433 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5434 const mbedtls_ecp_curve_info *curve_info =
5435 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5436 mbedtls_ecp_keypair *ecp;
5437 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005438 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005439 return( PSA_ERROR_NOT_SUPPORTED );
5440 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5441 return( PSA_ERROR_NOT_SUPPORTED );
5442 if( curve_info->bit_size != bits )
5443 return( PSA_ERROR_INVALID_ARGUMENT );
5444 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5445 if( ecp == NULL )
5446 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5447 mbedtls_ecp_keypair_init( ecp );
5448 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5449 mbedtls_ctr_drbg_random,
5450 &global_data.ctr_drbg );
5451 if( ret != 0 )
5452 {
5453 mbedtls_ecp_keypair_free( ecp );
5454 mbedtls_free( ecp );
5455 return( mbedtls_to_psa_error( ret ) );
5456 }
5457 slot->data.ecp = ecp;
5458 }
5459 else
5460#endif /* MBEDTLS_ECP_C */
5461
5462 return( PSA_ERROR_NOT_SUPPORTED );
5463
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005464 return( PSA_SUCCESS );
5465}
5466
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005467psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005468 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005469{
5470 psa_status_t status;
5471 psa_key_slot_t *slot = NULL;
5472 status = psa_start_key_creation( attributes, handle, &slot );
5473 if( status == PSA_SUCCESS )
5474 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005475 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005476 slot, attributes->bits,
5477 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005478 }
5479 if( status == PSA_SUCCESS )
5480 status = psa_finish_key_creation( slot );
5481 if( status != PSA_SUCCESS )
5482 {
5483 psa_fail_key_creation( slot );
5484 *handle = 0;
5485 }
5486 return( status );
5487}
5488
5489
Gilles Peskine05d69892018-06-19 22:00:52 +02005490
5491/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005492/* Module setup */
5493/****************************************************************/
5494
Gilles Peskine5e769522018-11-20 21:59:56 +01005495psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5496 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5497 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5498{
5499 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5500 return( PSA_ERROR_BAD_STATE );
5501 global_data.entropy_init = entropy_init;
5502 global_data.entropy_free = entropy_free;
5503 return( PSA_SUCCESS );
5504}
5505
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506void mbedtls_psa_crypto_free( void )
5507{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005508 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005509 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5510 {
5511 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005512 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005513 }
5514 /* Wipe all remaining data, including configuration.
5515 * In particular, this sets all state indicator to the value
5516 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005517 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005518#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005519 /* Unregister all secure element drivers, so that we restart from
5520 * a pristine state. */
5521 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005522#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005523}
5524
5525psa_status_t psa_crypto_init( void )
5526{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005527 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005528 const unsigned char drbg_seed[] = "PSA";
5529
Gilles Peskinec6b69072018-11-20 21:42:52 +01005530 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531 if( global_data.initialized != 0 )
5532 return( PSA_SUCCESS );
5533
Gilles Peskine5e769522018-11-20 21:59:56 +01005534 /* Set default configuration if
5535 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5536 if( global_data.entropy_init == NULL )
5537 global_data.entropy_init = mbedtls_entropy_init;
5538 if( global_data.entropy_free == NULL )
5539 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540
Gilles Peskinec6b69072018-11-20 21:42:52 +01005541 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005542 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005543 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005544 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005545 status = mbedtls_to_psa_error(
5546 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5547 mbedtls_entropy_func,
5548 &global_data.entropy,
5549 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5550 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005551 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005552 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005553
Gilles Peskine66fb1262018-12-10 16:29:04 +01005554 status = psa_initialize_key_slots( );
5555 if( status != PSA_SUCCESS )
5556 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005557
5558 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005559 global_data.initialized = 1;
5560
Gilles Peskinee59236f2018-01-27 23:32:46 +01005561exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005562 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005563 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005564 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005565}
5566
5567#endif /* MBEDTLS_PSA_CRYPTO_C */