blob: 9cf90ddafd0e975a3ae86712afbd42c7e0ac918c [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 Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/sha1.h"
76#include "mbedtls/sha256.h"
77#include "mbedtls/sha512.h"
78#include "mbedtls/xtea.h"
79
Gilles Peskine996deb12018-08-01 15:45:45 +020080#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
81
Gilles Peskine9ef733f2018-02-07 21:05:37 +010082/* constant-time buffer comparison */
83static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
84{
85 size_t i;
86 unsigned char diff = 0;
87
88 for( i = 0; i < n; i++ )
89 diff |= a[i] ^ b[i];
90
91 return( diff );
92}
93
94
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/****************************************************************/
97/* Global data, support functions and library management */
98/****************************************************************/
99
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100static int key_type_is_raw_bytes( psa_key_type_t type )
101{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200102 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103}
104
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100105/* Values for psa_global_data_t::rng_state */
106#define RNG_NOT_INITIALIZED 0
107#define RNG_INITIALIZED 1
108#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109
Gilles Peskine2d277862018-06-18 15:41:12 +0200110typedef struct
111{
Gilles Peskine5e769522018-11-20 21:59:56 +0100112 void (* entropy_init )( mbedtls_entropy_context *ctx );
113 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
123 if( global_data.initialized == 0 ) \
124 return( PSA_ERROR_BAD_STATE );
125
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126static psa_status_t mbedtls_to_psa_error( int ret )
127{
Gilles Peskinea5905292018-02-07 20:59:33 +0100128 /* If there's both a high-level code and low-level code, dispatch on
129 * the high-level code. */
130 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 {
132 case 0:
133 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
137 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
138 return( PSA_ERROR_NOT_SUPPORTED );
139 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
140 return( PSA_ERROR_HARDWARE_FAILURE );
141
142 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
150 return( PSA_ERROR_INVALID_ARGUMENT );
151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
152 return( PSA_ERROR_INSUFFICIENT_MEMORY );
153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
154 return( PSA_ERROR_BUFFER_TOO_SMALL );
155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
162 return( PSA_ERROR_NOT_SUPPORTED );
163 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
179 return( PSA_ERROR_INVALID_SIGNATURE );
180 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
189 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
190 return( PSA_ERROR_INVALID_PADDING );
191 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
196 return( PSA_ERROR_TAMPERING_DETECTED );
197 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210
211 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
222 return( PSA_ERROR_INVALID_SIGNATURE );
223 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_MD_ALLOC_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
239 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
240 return( PSA_ERROR_STORAGE_FAILURE );
241 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
251 return( PSA_ERROR_BUFFER_TOO_SMALL );
252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
David Saadab4ecc272019-02-14 13:48:10 +0200338 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200343
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345/****************************************************************/
346/* Key management */
347/****************************************************************/
348
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100349#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200350static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
351{
352 switch( grpid )
353 {
354 case MBEDTLS_ECP_DP_SECP192R1:
355 return( PSA_ECC_CURVE_SECP192R1 );
356 case MBEDTLS_ECP_DP_SECP224R1:
357 return( PSA_ECC_CURVE_SECP224R1 );
358 case MBEDTLS_ECP_DP_SECP256R1:
359 return( PSA_ECC_CURVE_SECP256R1 );
360 case MBEDTLS_ECP_DP_SECP384R1:
361 return( PSA_ECC_CURVE_SECP384R1 );
362 case MBEDTLS_ECP_DP_SECP521R1:
363 return( PSA_ECC_CURVE_SECP521R1 );
364 case MBEDTLS_ECP_DP_BP256R1:
365 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
366 case MBEDTLS_ECP_DP_BP384R1:
367 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
368 case MBEDTLS_ECP_DP_BP512R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
370 case MBEDTLS_ECP_DP_CURVE25519:
371 return( PSA_ECC_CURVE_CURVE25519 );
372 case MBEDTLS_ECP_DP_SECP192K1:
373 return( PSA_ECC_CURVE_SECP192K1 );
374 case MBEDTLS_ECP_DP_SECP224K1:
375 return( PSA_ECC_CURVE_SECP224K1 );
376 case MBEDTLS_ECP_DP_SECP256K1:
377 return( PSA_ECC_CURVE_SECP256K1 );
378 case MBEDTLS_ECP_DP_CURVE448:
379 return( PSA_ECC_CURVE_CURVE448 );
380 default:
381 return( 0 );
382 }
383}
384
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
386{
387 switch( curve )
388 {
389 case PSA_ECC_CURVE_SECP192R1:
390 return( MBEDTLS_ECP_DP_SECP192R1 );
391 case PSA_ECC_CURVE_SECP224R1:
392 return( MBEDTLS_ECP_DP_SECP224R1 );
393 case PSA_ECC_CURVE_SECP256R1:
394 return( MBEDTLS_ECP_DP_SECP256R1 );
395 case PSA_ECC_CURVE_SECP384R1:
396 return( MBEDTLS_ECP_DP_SECP384R1 );
397 case PSA_ECC_CURVE_SECP521R1:
398 return( MBEDTLS_ECP_DP_SECP521R1 );
399 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 case PSA_ECC_CURVE_CURVE25519:
406 return( MBEDTLS_ECP_DP_CURVE25519 );
407 case PSA_ECC_CURVE_SECP192K1:
408 return( MBEDTLS_ECP_DP_SECP192K1 );
409 case PSA_ECC_CURVE_SECP224K1:
410 return( MBEDTLS_ECP_DP_SECP224K1 );
411 case PSA_ECC_CURVE_SECP256K1:
412 return( MBEDTLS_ECP_DP_SECP256K1 );
413 case PSA_ECC_CURVE_CURVE448:
414 return( MBEDTLS_ECP_DP_CURVE448 );
415 default:
416 return( MBEDTLS_ECP_DP_NONE );
417 }
418}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200421static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
422 size_t bits,
423 struct raw_data *raw )
424{
425 /* Check that the bit size is acceptable for the key type */
426 switch( type )
427 {
428 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200429 if( bits == 0 )
430 {
431 raw->bytes = 0;
432 raw->data = NULL;
433 return( PSA_SUCCESS );
434 }
435 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436#if defined(MBEDTLS_MD_C)
437 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200439 case PSA_KEY_TYPE_DERIVE:
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_AES_C)
442 case PSA_KEY_TYPE_AES:
443 if( bits != 128 && bits != 192 && bits != 256 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
445 break;
446#endif
447#if defined(MBEDTLS_CAMELLIA_C)
448 case PSA_KEY_TYPE_CAMELLIA:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
453#if defined(MBEDTLS_DES_C)
454 case PSA_KEY_TYPE_DES:
455 if( bits != 64 && bits != 128 && bits != 192 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
459#if defined(MBEDTLS_ARC4_C)
460 case PSA_KEY_TYPE_ARC4:
461 if( bits < 8 || bits > 2048 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465 default:
466 return( PSA_ERROR_NOT_SUPPORTED );
467 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200468 if( bits % 8 != 0 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470
471 /* Allocate memory for the key */
472 raw->bytes = PSA_BITS_TO_BYTES( bits );
473 raw->data = mbedtls_calloc( 1, raw->bytes );
474 if( raw->data == NULL )
475 {
476 raw->bytes = 0;
477 return( PSA_ERROR_INSUFFICIENT_MEMORY );
478 }
479 return( PSA_SUCCESS );
480}
481
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100483/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
484 * that are not a multiple of 8) well. For example, there is only
485 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
486 * way to return the exact bit size of a key.
487 * To keep things simple, reject non-byte-aligned key sizes. */
488static psa_status_t psa_check_rsa_key_byte_aligned(
489 const mbedtls_rsa_context *rsa )
490{
491 mbedtls_mpi n;
492 psa_status_t status;
493 mbedtls_mpi_init( &n );
494 status = mbedtls_to_psa_error(
495 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
496 if( status == PSA_SUCCESS )
497 {
498 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
499 status = PSA_ERROR_NOT_SUPPORTED;
500 }
501 mbedtls_mpi_free( &n );
502 return( status );
503}
504
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000505static psa_status_t psa_import_rsa_key( psa_key_type_t type,
506 const uint8_t *data,
507 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508 mbedtls_rsa_context **p_rsa )
509{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000510 psa_status_t status;
511 mbedtls_pk_context pk;
512 mbedtls_rsa_context *rsa;
513 size_t bits;
514
515 mbedtls_pk_init( &pk );
516
517 /* Parse the data. */
518 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000522 status = mbedtls_to_psa_error(
523 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
524 if( status != PSA_SUCCESS )
525 goto exit;
526
527 /* We have something that the pkparse module recognizes. If it is a
528 * valid RSA key, store it. */
529 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000531 status = PSA_ERROR_INVALID_ARGUMENT;
532 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000534
535 rsa = mbedtls_pk_rsa( pk );
536 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
537 * supports non-byte-aligned key sizes, but not well. For example,
538 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
539 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
540 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
541 {
542 status = PSA_ERROR_NOT_SUPPORTED;
543 goto exit;
544 }
545 status = psa_check_rsa_key_byte_aligned( rsa );
546
547exit:
548 /* Free the content of the pk object only on error. */
549 if( status != PSA_SUCCESS )
550 {
551 mbedtls_pk_free( &pk );
552 return( status );
553 }
554
555 /* On success, store the content of the object in the RSA context. */
556 *p_rsa = rsa;
557
558 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000562#if defined(MBEDTLS_ECP_C)
563
564/* Import a public key given as the uncompressed representation defined by SEC1
565 * 2.3.3 as the content of an ECPoint. */
566static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
567 const uint8_t *data,
568 size_t data_length,
569 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000571 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
572 mbedtls_ecp_keypair *ecp = NULL;
573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
574
575 *p_ecp = NULL;
576 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
577 if( ecp == NULL )
578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
579 mbedtls_ecp_keypair_init( ecp );
580
581 /* Load the group. */
582 status = mbedtls_to_psa_error(
583 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Load the public value. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
589 data, data_length ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* Check that the point is on the curve. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000608 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000610#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612#if defined(MBEDTLS_ECP_C)
613/* Import a private key given as a byte string which is the private value
614 * in big-endian order. */
615static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
616 const uint8_t *data,
617 size_t data_length,
618 mbedtls_ecp_keypair **p_ecp )
619{
620 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
621 mbedtls_ecp_keypair *ecp = NULL;
622 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
623
624 *p_ecp = NULL;
625 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
626 if( ecp == NULL )
627 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000628 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629
630 /* Load the group. */
631 status = mbedtls_to_psa_error(
632 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
633 if( status != PSA_SUCCESS )
634 goto exit;
635 /* Load the secret value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Validate the private key. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Calculate the public key from the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
648 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651
652 *p_ecp = ecp;
653 return( PSA_SUCCESS );
654
655exit:
656 if( ecp != NULL )
657 {
658 mbedtls_ecp_keypair_free( ecp );
659 mbedtls_free( ecp );
660 }
661 return( status );
662}
663#endif /* defined(MBEDTLS_ECP_C) */
664
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100665/** Import key data into a slot. `slot->type` must have been set
666 * previously. This function assumes that the slot does not contain
667 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100668psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Darryl Green940d72c2018-07-13 13:18:51 +0100674 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100679 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100690 {
Darryl Green940d72c2018-07-13 13:18:51 +0100691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 data, data_length,
693 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000695 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
696 {
697 status = psa_import_ec_public_key(
698 PSA_KEY_TYPE_GET_CURVE( slot->type ),
699 data, data_length,
700 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000701 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100702 else
703#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
705 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000707 status = psa_import_rsa_key( slot->type,
708 data, data_length,
709 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000712#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 {
714 return( PSA_ERROR_NOT_SUPPORTED );
715 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000716 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100717}
718
Darryl Green06fd18d2018-07-16 11:21:11 +0100719/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000720 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100721static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
724 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100726
727 *p_slot = NULL;
728
Gilles Peskinec5487a82018-12-03 18:08:14 +0100729 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100730 if( status != PSA_SUCCESS )
731 return( status );
732
733 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200734 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735
736 *p_slot = slot;
737 return( status );
738}
739
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100740/** Calculate the intersection of two algorithm usage policies.
741 *
742 * Return 0 (which allows no operation) on incompatibility.
743 */
744static psa_algorithm_t psa_key_policy_algorithm_intersection(
745 psa_algorithm_t alg1,
746 psa_algorithm_t alg2 )
747{
748 /* Common case: the policy only allows alg. */
749 if( alg1 == alg2 )
750 return( alg1 );
751 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
754 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
755 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
756 {
757 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
758 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100759 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760 return( alg1 );
761 }
762 /* If the policies are incompatible, allow nothing. */
763 return( 0 );
764}
765
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100766/** Test whether a policy permits an algorithm.
767 *
768 * The caller must test usage flags separately.
769 */
770static int psa_key_policy_permits( const psa_key_policy_t *policy,
771 psa_algorithm_t alg )
772{
773 /* Common case: the policy only allows alg. */
774 if( alg == policy->alg )
775 return( 1 );
776 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
777 * and alg is the same hash-and-sign family with any hash,
778 * then alg is compliant with policy->alg. */
779 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
780 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
781 {
782 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
783 ( alg & ~PSA_ALG_HASH_MASK ) );
784 }
785 /* If it isn't permitted, it's forbidden. */
786 return( 0 );
787}
788
Gilles Peskinef603c712019-01-19 13:40:11 +0100789/** Restrict a key policy based on a constraint.
790 *
791 * \param[in,out] policy The policy to restrict.
792 * \param[in] constraint The policy constraint to apply.
793 *
794 * \retval #PSA_SUCCESS
795 * \c *policy contains the intersection of the original value of
796 * \c *policy and \c *constraint.
797 * \retval #PSA_ERROR_INVALID_ARGUMENT
798 * \c *policy and \c *constraint are incompatible.
799 * \c *policy is unchanged.
800 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801static psa_status_t psa_restrict_key_policy(
802 psa_key_policy_t *policy,
803 const psa_key_policy_t *constraint )
804{
805 psa_algorithm_t intersection_alg =
806 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
807 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
808 return( PSA_ERROR_INVALID_ARGUMENT );
809 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100810 policy->alg = intersection_alg;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 return( PSA_SUCCESS );
812}
813
Darryl Green06fd18d2018-07-16 11:21:11 +0100814/** Retrieve a slot which must contain a key. The key must have allow all the
815 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
816 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100817static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100818 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100819 psa_key_usage_t usage,
820 psa_algorithm_t alg )
821{
822 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100824
825 *p_slot = NULL;
826
Gilles Peskinec5487a82018-12-03 18:08:14 +0100827 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100828 if( status != PSA_SUCCESS )
829 return( status );
830 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200831 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100832
833 /* Enforce that usage policy for the key slot contains all the flags
834 * required by the usage parameter. There is one exception: public
835 * keys can always be exported, so we treat public key objects as
836 * if they had the export flag. */
837 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
838 usage &= ~PSA_KEY_USAGE_EXPORT;
839 if( ( slot->policy.usage & usage ) != usage )
840 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841
842 /* Enforce that the usage policy permits the requested algortihm. */
843 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100844 return( PSA_ERROR_NOT_PERMITTED );
845
846 *p_slot = slot;
847 return( PSA_SUCCESS );
848}
Darryl Green940d72c2018-07-13 13:18:51 +0100849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100851static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000852{
853 if( slot->type == PSA_KEY_TYPE_NONE )
854 {
855 /* No key material to clean. */
856 }
857 else if( key_type_is_raw_bytes( slot->type ) )
858 {
859 mbedtls_free( slot->data.raw.data );
860 }
861 else
862#if defined(MBEDTLS_RSA_C)
863 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
864 {
865 mbedtls_rsa_free( slot->data.rsa );
866 mbedtls_free( slot->data.rsa );
867 }
868 else
869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 {
873 mbedtls_ecp_keypair_free( slot->data.ecp );
874 mbedtls_free( slot->data.ecp );
875 }
876 else
877#endif /* defined(MBEDTLS_ECP_C) */
878 {
879 /* Shouldn't happen: the key type is not any type that we
880 * put in. */
881 return( PSA_ERROR_TAMPERING_DETECTED );
882 }
883
884 return( PSA_SUCCESS );
885}
886
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100887static void psa_abort_operations_using_key( psa_key_slot_t *slot )
888{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200889 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100890 (void) slot;
891}
892
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100893/** Completely wipe a slot in memory, including its policy.
894 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100895psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896{
897 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100898 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100899 /* At this point, key material and other type-specific content has
900 * been wiped. Clear remaining metadata. We can call memset and not
901 * zeroize because the metadata is not particularly sensitive. */
902 memset( slot, 0, sizeof( *slot ) );
903 return( status );
904}
905
Gilles Peskine87a5e562019-04-17 12:28:25 +0200906psa_status_t psa_import_key_to_handle( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100907 psa_key_type_t type,
908 const uint8_t *data,
909 size_t data_length )
910{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100911 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100912 psa_status_t status;
913
Gilles Peskinec5487a82018-12-03 18:08:14 +0100914 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100915 if( status != PSA_SUCCESS )
916 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917
918 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100919
920 status = psa_import_key_into_slot( slot, data, data_length );
921 if( status != PSA_SUCCESS )
922 {
923 slot->type = PSA_KEY_TYPE_NONE;
924 return( status );
925 }
926
Darryl Greend49a4992018-06-18 17:27:26 +0100927#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
928 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
929 {
930 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100931 status = psa_save_persistent_key( slot->persistent_storage_id,
932 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100933 data_length );
934 if( status != PSA_SUCCESS )
935 {
936 (void) psa_remove_key_data_from_memory( slot );
937 slot->type = PSA_KEY_TYPE_NONE;
938 }
939 }
940#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
941
942 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943}
944
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100948 psa_status_t status = PSA_SUCCESS;
949 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950
Gilles Peskinec5487a82018-12-03 18:08:14 +0100951 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( status != PSA_SUCCESS )
953 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100954#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
955 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
956 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100957 storage_status =
958 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100959 }
960#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100961 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100962 if( status != PSA_SUCCESS )
963 return( status );
964 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965}
966
Gilles Peskineb870b182018-07-06 16:02:09 +0200967/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200968static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200969{
970 if( key_type_is_raw_bytes( slot->type ) )
971 return( slot->data.raw.bytes * 8 );
972#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100974 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200975#endif /* defined(MBEDTLS_RSA_C) */
976#if defined(MBEDTLS_ECP_C)
977 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
978 return( slot->data.ecp->grp.pbits );
979#endif /* defined(MBEDTLS_ECP_C) */
980 /* Shouldn't happen except on an empty slot. */
981 return( 0 );
982}
983
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200984void psa_reset_key_attributes( psa_key_attributes_t *attributes )
985{
Gilles Peskineb699f072019-04-26 16:06:02 +0200986 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200987 memset( attributes, 0, sizeof( *attributes ) );
988}
989
Gilles Peskineb699f072019-04-26 16:06:02 +0200990psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
991 psa_key_type_t type,
992 const uint8_t *data,
993 size_t data_length )
994{
995 uint8_t *copy = NULL;
996
997 if( data_length != 0 )
998 {
999 copy = mbedtls_calloc( 1, data_length );
1000 if( copy == NULL )
1001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1002 memcpy( copy, data, data_length );
1003 }
1004 /* After this point, this function is guaranteed to succeed, so it
1005 * can start modifying `*attributes`. */
1006
1007 if( attributes->domain_parameters != NULL )
1008 {
1009 mbedtls_free( attributes->domain_parameters );
1010 attributes->domain_parameters = NULL;
1011 attributes->domain_parameters_size = 0;
1012 }
1013
1014 attributes->domain_parameters = copy;
1015 attributes->domain_parameters_size = data_length;
1016 attributes->type = type;
1017 return( PSA_SUCCESS );
1018}
1019
1020psa_status_t psa_get_key_domain_parameters(
1021 const psa_key_attributes_t *attributes,
1022 uint8_t *data, size_t data_size, size_t *data_length )
1023{
1024 if( attributes->domain_parameters_size > data_size )
1025 return( PSA_ERROR_BUFFER_TOO_SMALL );
1026 *data_length = attributes->domain_parameters_size;
1027 if( attributes->domain_parameters_size != 0 )
1028 memcpy( data, attributes->domain_parameters,
1029 attributes->domain_parameters_size );
1030 return( PSA_SUCCESS );
1031}
1032
1033#if defined(MBEDTLS_RSA_C)
1034static psa_status_t psa_get_rsa_public_exponent(
1035 const mbedtls_rsa_context *rsa,
1036 psa_key_attributes_t *attributes )
1037{
1038 mbedtls_mpi mpi;
1039 int ret;
1040 uint8_t *buffer = NULL;
1041 size_t buflen;
1042 mbedtls_mpi_init( &mpi );
1043
1044 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1045 if( ret != 0 )
1046 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001047 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1048 {
1049 /* It's the default value, which is reported as an empty string,
1050 * so there's nothing to do. */
1051 goto exit;
1052 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001053
1054 buflen = mbedtls_mpi_size( &mpi );
1055 buffer = mbedtls_calloc( 1, buflen );
1056 if( buffer == NULL )
1057 {
1058 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1059 goto exit;
1060 }
1061 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1062 if( ret != 0 )
1063 goto exit;
1064 attributes->domain_parameters = buffer;
1065 attributes->domain_parameters_size = buflen;
1066
1067exit:
1068 mbedtls_mpi_free( &mpi );
1069 if( ret != 0 )
1070 mbedtls_free( buffer );
1071 return( mbedtls_to_psa_error( ret ) );
1072}
1073#endif /* MBEDTLS_RSA_C */
1074
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1076 psa_key_attributes_t *attributes )
1077{
1078 psa_key_slot_t *slot;
1079 psa_status_t status;
1080
1081 psa_reset_key_attributes( attributes );
1082
1083 status = psa_get_key_slot( handle, &slot );
1084 if( status != PSA_SUCCESS )
1085 return( status );
1086
1087 attributes->id = slot->persistent_storage_id;
1088 attributes->lifetime = slot->lifetime;
1089 attributes->policy = slot->policy;
1090 attributes->type = slot->type;
1091 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001092
1093 switch( slot->type )
1094 {
1095#if defined(MBEDTLS_RSA_C)
1096 case PSA_KEY_TYPE_RSA_KEYPAIR:
1097 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1098 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1099 break;
1100#endif
1101 default:
1102 /* Nothing else to do. */
1103 break;
1104 }
1105
1106 if( status != PSA_SUCCESS )
1107 psa_reset_key_attributes( attributes );
1108 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001109}
1110
Gilles Peskinec5487a82018-12-03 18:08:14 +01001111psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001112 psa_key_type_t *type,
1113 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001115 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001116 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001119 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120 if( bits != NULL )
1121 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001122 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001123 if( status != PSA_SUCCESS )
1124 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001125
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001127 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001128 if( type != NULL )
1129 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001130 if( bits != NULL )
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001131 *bits = psa_get_key_slot_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001132 return( PSA_SUCCESS );
1133}
1134
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001135#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001136static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1137 unsigned char *buf, size_t size )
1138{
1139 int ret;
1140 unsigned char *c;
1141 size_t len = 0;
1142
1143 c = buf + size;
1144
1145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1146
1147 return( (int) len );
1148}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001149#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001150
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001151static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1152 uint8_t *data,
1153 size_t data_size,
1154 size_t *data_length,
1155 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001157 *data_length = 0;
1158
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001159 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001160 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001161
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001162 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163 {
1164 if( slot->data.raw.bytes > data_size )
1165 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001166 if( data_size != 0 )
1167 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001168 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001169 memset( data + slot->data.raw.bytes, 0,
1170 data_size - slot->data.raw.bytes );
1171 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001172 *data_length = slot->data.raw.bytes;
1173 return( PSA_SUCCESS );
1174 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001175#if defined(MBEDTLS_ECP_C)
1176 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1177 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001178 psa_status_t status;
1179
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001180 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001181 if( bytes > data_size )
1182 return( PSA_ERROR_BUFFER_TOO_SMALL );
1183 status = mbedtls_to_psa_error(
1184 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1185 if( status != PSA_SUCCESS )
1186 return( status );
1187 memset( data + bytes, 0, data_size - bytes );
1188 *data_length = bytes;
1189 return( PSA_SUCCESS );
1190 }
1191#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192 else
Moran Peker17e36e12018-05-02 12:55:20 +03001193 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001194#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001195 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001196 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001197 {
Moran Pekera998bc62018-04-16 18:16:20 +03001198 mbedtls_pk_context pk;
1199 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001200 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001201 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001202#if defined(MBEDTLS_RSA_C)
1203 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001204 pk.pk_info = &mbedtls_rsa_info;
1205 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001206#else
1207 return( PSA_ERROR_NOT_SUPPORTED );
1208#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001209 }
1210 else
1211 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001212#if defined(MBEDTLS_ECP_C)
1213 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001214 pk.pk_info = &mbedtls_eckey_info;
1215 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001216#else
1217 return( PSA_ERROR_NOT_SUPPORTED );
1218#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001219 }
Moran Pekerd7326592018-05-29 16:56:39 +03001220 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001221 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001222 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001223 }
Moran Peker17e36e12018-05-02 12:55:20 +03001224 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001225 {
Moran Peker17e36e12018-05-02 12:55:20 +03001226 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001227 }
Moran Peker60364322018-04-29 11:34:58 +03001228 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001229 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001230 /* If data_size is 0 then data may be NULL and then the
1231 * call to memset would have undefined behavior. */
1232 if( data_size != 0 )
1233 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001234 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001235 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001236 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1237 * Move the data to the beginning and erase remaining data
1238 * at the original location. */
1239 if( 2 * (size_t) ret <= data_size )
1240 {
1241 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001242 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001243 }
1244 else if( (size_t) ret < data_size )
1245 {
1246 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001247 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001248 }
Moran Pekera998bc62018-04-16 18:16:20 +03001249 *data_length = ret;
1250 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001251 }
1252 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001253#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001254 {
1255 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001256 it is valid for a special-purpose implementation to omit
1257 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001258 return( PSA_ERROR_NOT_SUPPORTED );
1259 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 }
1261}
1262
Gilles Peskinec5487a82018-12-03 18:08:14 +01001263psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001264 uint8_t *data,
1265 size_t data_size,
1266 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001267{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001268 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001269 psa_status_t status;
1270
1271 /* Set the key to empty now, so that even when there are errors, we always
1272 * set data_length to a value between 0 and data_size. On error, setting
1273 * the key to empty is a good choice because an empty key representation is
1274 * unlikely to be accepted anywhere. */
1275 *data_length = 0;
1276
1277 /* Export requires the EXPORT flag. There is an exception for public keys,
1278 * which don't require any flag, but psa_get_key_from_slot takes
1279 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001280 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001281 if( status != PSA_SUCCESS )
1282 return( status );
1283 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001284 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286
Gilles Peskinec5487a82018-12-03 18:08:14 +01001287psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001288 uint8_t *data,
1289 size_t data_size,
1290 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001291{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001292 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001293 psa_status_t status;
1294
1295 /* Set the key to empty now, so that even when there are errors, we always
1296 * set data_length to a value between 0 and data_size. On error, setting
1297 * the key to empty is a good choice because an empty key representation is
1298 * unlikely to be accepted anywhere. */
1299 *data_length = 0;
1300
1301 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001302 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001303 if( status != PSA_SUCCESS )
1304 return( status );
1305 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001306 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001307}
1308
Darryl Green0c6575a2018-11-07 16:05:30 +00001309#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001310static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001311 size_t bits )
1312{
1313 psa_status_t status;
1314 uint8_t *data;
1315 size_t key_length;
1316 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1317 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001318 if( data == NULL )
1319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001320 /* Get key data in export format */
1321 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1322 if( status != PSA_SUCCESS )
1323 {
1324 slot->type = PSA_KEY_TYPE_NONE;
1325 goto exit;
1326 }
1327 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001328 status = psa_save_persistent_key( slot->persistent_storage_id,
1329 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001330 data, key_length );
1331 if( status != PSA_SUCCESS )
1332 {
1333 slot->type = PSA_KEY_TYPE_NONE;
1334 }
1335exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001336 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001337 mbedtls_free( data );
1338 return( status );
1339}
1340#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1341
Gilles Peskine4747d192019-04-17 15:05:45 +02001342static psa_status_t psa_set_key_policy_internal(
1343 psa_key_slot_t *slot,
1344 const psa_key_policy_t *policy )
1345{
1346 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
1347 PSA_KEY_USAGE_ENCRYPT |
1348 PSA_KEY_USAGE_DECRYPT |
1349 PSA_KEY_USAGE_SIGN |
1350 PSA_KEY_USAGE_VERIFY |
1351 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1352 return( PSA_ERROR_INVALID_ARGUMENT );
1353
1354 slot->policy = *policy;
1355 return( PSA_SUCCESS );
1356}
1357
1358/** Prepare a key slot to receive key material.
1359 *
1360 * This function allocates a key slot and sets its metadata.
1361 *
1362 * If this function fails, call psa_fail_key_creation().
1363 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001364 * This function is intended to be used as follows:
1365 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1366 * it with the specified attributes, and assign it a handle.
1367 * -# Populate the slot with the key material.
1368 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1369 * In case of failure at any step, stop the sequence and call
1370 * psa_fail_key_creation().
1371 *
Gilles Peskine4747d192019-04-17 15:05:45 +02001372 * \param attributes Key attributes for the new key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001373 * \param handle On success, a handle for the allocated slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001374 * \param p_slot On success, a pointer to the prepared slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001375 *
1376 * \retval #PSA_SUCCESS
1377 * The key slot is ready to receive key material.
1378 * \return If this function fails, the key slot is an invalid state.
1379 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001380 */
1381static psa_status_t psa_start_key_creation(
1382 const psa_key_attributes_t *attributes,
1383 psa_key_handle_t *handle,
1384 psa_key_slot_t **p_slot )
1385{
1386 psa_status_t status;
1387 psa_key_slot_t *slot;
1388
1389 status = psa_allocate_key( handle );
1390 if( status != PSA_SUCCESS )
1391 return( status );
1392 status = psa_get_key_slot( *handle, p_slot );
1393 if( status != PSA_SUCCESS )
1394 return( status );
1395 slot = *p_slot;
1396
1397 status = psa_set_key_policy_internal( slot, &attributes->policy );
1398 if( status != PSA_SUCCESS )
1399 return( status );
1400 slot->lifetime = attributes->lifetime;
1401 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001402 {
1403 status = psa_validate_persistent_key_parameters( attributes->lifetime,
1404 attributes->id );
1405 if( status != PSA_SUCCESS )
1406 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001407 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001408 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001409 slot->type = attributes->type;
1410
1411 return( status );
1412}
1413
1414/** Finalize the creation of a key once its key material has been set.
1415 *
1416 * This entails writing the key to persistent storage.
1417 *
1418 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001419 * See the documentation of psa_start_key_creation() for the intended use
1420 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001421 *
1422 * \param slot Pointer to the slot with key material.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001423 *
1424 * \retval #PSA_SUCCESS
1425 * The key was successfully created. The handle is now valid.
1426 * \return If this function fails, the key slot is an invalid state.
1427 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 */
1429static psa_status_t psa_finish_key_creation( psa_key_slot_t *slot )
1430{
1431 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001432 (void) slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001433
1434#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1435 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1436 {
1437 uint8_t *buffer = NULL;
1438 size_t buffer_size = 0;
1439 size_t length;
1440
1441 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001442 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 buffer = mbedtls_calloc( 1, buffer_size );
1444 if( buffer == NULL && buffer_size != 0 )
1445 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1446 status = psa_internal_export_key( slot,
1447 buffer, buffer_size, &length,
1448 0 );
1449
1450 if( status == PSA_SUCCESS )
1451 {
1452 status = psa_save_persistent_key( slot->persistent_storage_id,
1453 slot->type, &slot->policy,
1454 buffer, length );
1455 }
1456
1457 if( buffer_size != 0 )
1458 mbedtls_platform_zeroize( buffer, buffer_size );
1459 mbedtls_free( buffer );
1460 }
1461#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1462
1463 return( status );
1464}
1465
1466/** Abort the creation of a key.
1467 *
1468 * You may call this function after calling psa_start_key_creation(),
1469 * or after psa_finish_key_creation() fails. In other circumstances, this
1470 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001471 * See the documentation of psa_start_key_creation() for the intended use
1472 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 *
1474 * \param slot Pointer to the slot with key material.
1475 */
1476static void psa_fail_key_creation( psa_key_slot_t *slot )
1477{
1478 if( slot == NULL )
1479 return;
1480 psa_wipe_key_slot( slot );
1481}
1482
1483psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1484 psa_key_handle_t *handle,
1485 const uint8_t *data,
1486 size_t data_length )
1487{
1488 psa_status_t status;
1489 psa_key_slot_t *slot = NULL;
1490 status = psa_start_key_creation( attributes, handle, &slot );
1491 if( status == PSA_SUCCESS )
1492 {
1493 status = psa_import_key_into_slot( slot, data, data_length );
1494 }
1495 if( status == PSA_SUCCESS )
1496 status = psa_finish_key_creation( slot );
1497 if( status != PSA_SUCCESS )
1498 {
1499 psa_fail_key_creation( slot );
1500 *handle = 0;
1501 }
1502 return( status );
1503}
1504
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001505static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001506 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001507{
1508 psa_status_t status;
1509 uint8_t *buffer = NULL;
1510 size_t buffer_size = 0;
1511 size_t length;
1512
1513 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001514 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001515 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001516 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001517 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001518 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1519 if( status != PSA_SUCCESS )
1520 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001521 target->type = source->type;
1522 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001523
1524exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001525 if( buffer_size != 0 )
1526 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001527 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001528 return( status );
1529}
1530
Gilles Peskine87a5e562019-04-17 12:28:25 +02001531psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001532 psa_key_handle_t target_handle,
1533 const psa_key_policy_t *constraint)
1534{
1535 psa_key_slot_t *source_slot = NULL;
1536 psa_key_slot_t *target_slot = NULL;
1537 psa_key_policy_t new_policy;
1538 psa_status_t status;
1539 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1540 if( status != PSA_SUCCESS )
1541 return( status );
1542 status = psa_get_empty_key_slot( target_handle, &target_slot );
1543 if( status != PSA_SUCCESS )
1544 return( status );
1545
1546 new_policy = target_slot->policy;
1547 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1548 if( status != PSA_SUCCESS )
1549 return( status );
1550 if( constraint != NULL )
1551 {
1552 status = psa_restrict_key_policy( &new_policy, constraint );
1553 if( status != PSA_SUCCESS )
1554 return( status );
1555 }
1556
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001557 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001558 if( status != PSA_SUCCESS )
1559 return( status );
1560
1561 target_slot->policy = new_policy;
1562 return( PSA_SUCCESS );
1563}
1564
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001565psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1566 const psa_key_attributes_t *specified_attributes,
1567 psa_key_handle_t *target_handle )
1568{
1569 psa_status_t status;
1570 psa_key_slot_t *source_slot = NULL;
1571 psa_key_slot_t *target_slot = NULL;
1572 psa_key_attributes_t actual_attributes = *specified_attributes;
1573
1574 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1575 if( status != PSA_SUCCESS )
1576 goto exit;
1577
1578 status = psa_restrict_key_policy( &actual_attributes.policy,
1579 &source_slot->policy );
1580 if( status != PSA_SUCCESS )
1581 goto exit;
1582
1583 status = psa_start_key_creation( &actual_attributes,
1584 target_handle, &target_slot );
1585 if( status != PSA_SUCCESS )
1586 goto exit;
1587
1588 status = psa_copy_key_material( source_slot, target_slot );
1589
1590exit:
1591 if( status == PSA_SUCCESS )
1592 status = psa_finish_key_creation( target_slot );
1593 if( status != PSA_SUCCESS )
1594 {
1595 psa_fail_key_creation( target_slot );
1596 *target_handle = 0;
1597 }
1598 return( status );
1599}
1600
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001601
1602
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001604/* Message digests */
1605/****************************************************************/
1606
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001607static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001608{
1609 switch( alg )
1610 {
1611#if defined(MBEDTLS_MD2_C)
1612 case PSA_ALG_MD2:
1613 return( &mbedtls_md2_info );
1614#endif
1615#if defined(MBEDTLS_MD4_C)
1616 case PSA_ALG_MD4:
1617 return( &mbedtls_md4_info );
1618#endif
1619#if defined(MBEDTLS_MD5_C)
1620 case PSA_ALG_MD5:
1621 return( &mbedtls_md5_info );
1622#endif
1623#if defined(MBEDTLS_RIPEMD160_C)
1624 case PSA_ALG_RIPEMD160:
1625 return( &mbedtls_ripemd160_info );
1626#endif
1627#if defined(MBEDTLS_SHA1_C)
1628 case PSA_ALG_SHA_1:
1629 return( &mbedtls_sha1_info );
1630#endif
1631#if defined(MBEDTLS_SHA256_C)
1632 case PSA_ALG_SHA_224:
1633 return( &mbedtls_sha224_info );
1634 case PSA_ALG_SHA_256:
1635 return( &mbedtls_sha256_info );
1636#endif
1637#if defined(MBEDTLS_SHA512_C)
1638 case PSA_ALG_SHA_384:
1639 return( &mbedtls_sha384_info );
1640 case PSA_ALG_SHA_512:
1641 return( &mbedtls_sha512_info );
1642#endif
1643 default:
1644 return( NULL );
1645 }
1646}
1647
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001648psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1649{
1650 switch( operation->alg )
1651 {
Gilles Peskine81736312018-06-26 15:04:31 +02001652 case 0:
1653 /* The object has (apparently) been initialized but it is not
1654 * in use. It's ok to call abort on such an object, and there's
1655 * nothing to do. */
1656 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001657#if defined(MBEDTLS_MD2_C)
1658 case PSA_ALG_MD2:
1659 mbedtls_md2_free( &operation->ctx.md2 );
1660 break;
1661#endif
1662#if defined(MBEDTLS_MD4_C)
1663 case PSA_ALG_MD4:
1664 mbedtls_md4_free( &operation->ctx.md4 );
1665 break;
1666#endif
1667#if defined(MBEDTLS_MD5_C)
1668 case PSA_ALG_MD5:
1669 mbedtls_md5_free( &operation->ctx.md5 );
1670 break;
1671#endif
1672#if defined(MBEDTLS_RIPEMD160_C)
1673 case PSA_ALG_RIPEMD160:
1674 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1675 break;
1676#endif
1677#if defined(MBEDTLS_SHA1_C)
1678 case PSA_ALG_SHA_1:
1679 mbedtls_sha1_free( &operation->ctx.sha1 );
1680 break;
1681#endif
1682#if defined(MBEDTLS_SHA256_C)
1683 case PSA_ALG_SHA_224:
1684 case PSA_ALG_SHA_256:
1685 mbedtls_sha256_free( &operation->ctx.sha256 );
1686 break;
1687#endif
1688#if defined(MBEDTLS_SHA512_C)
1689 case PSA_ALG_SHA_384:
1690 case PSA_ALG_SHA_512:
1691 mbedtls_sha512_free( &operation->ctx.sha512 );
1692 break;
1693#endif
1694 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001695 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001696 }
1697 operation->alg = 0;
1698 return( PSA_SUCCESS );
1699}
1700
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001701psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001702 psa_algorithm_t alg )
1703{
1704 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001705
1706 /* A context must be freshly initialized before it can be set up. */
1707 if( operation->alg != 0 )
1708 {
1709 return( PSA_ERROR_BAD_STATE );
1710 }
1711
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001712 switch( alg )
1713 {
1714#if defined(MBEDTLS_MD2_C)
1715 case PSA_ALG_MD2:
1716 mbedtls_md2_init( &operation->ctx.md2 );
1717 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1718 break;
1719#endif
1720#if defined(MBEDTLS_MD4_C)
1721 case PSA_ALG_MD4:
1722 mbedtls_md4_init( &operation->ctx.md4 );
1723 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1724 break;
1725#endif
1726#if defined(MBEDTLS_MD5_C)
1727 case PSA_ALG_MD5:
1728 mbedtls_md5_init( &operation->ctx.md5 );
1729 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1730 break;
1731#endif
1732#if defined(MBEDTLS_RIPEMD160_C)
1733 case PSA_ALG_RIPEMD160:
1734 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1735 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1736 break;
1737#endif
1738#if defined(MBEDTLS_SHA1_C)
1739 case PSA_ALG_SHA_1:
1740 mbedtls_sha1_init( &operation->ctx.sha1 );
1741 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1742 break;
1743#endif
1744#if defined(MBEDTLS_SHA256_C)
1745 case PSA_ALG_SHA_224:
1746 mbedtls_sha256_init( &operation->ctx.sha256 );
1747 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1748 break;
1749 case PSA_ALG_SHA_256:
1750 mbedtls_sha256_init( &operation->ctx.sha256 );
1751 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1752 break;
1753#endif
1754#if defined(MBEDTLS_SHA512_C)
1755 case PSA_ALG_SHA_384:
1756 mbedtls_sha512_init( &operation->ctx.sha512 );
1757 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1758 break;
1759 case PSA_ALG_SHA_512:
1760 mbedtls_sha512_init( &operation->ctx.sha512 );
1761 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1762 break;
1763#endif
1764 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001765 return( PSA_ALG_IS_HASH( alg ) ?
1766 PSA_ERROR_NOT_SUPPORTED :
1767 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001768 }
1769 if( ret == 0 )
1770 operation->alg = alg;
1771 else
1772 psa_hash_abort( operation );
1773 return( mbedtls_to_psa_error( ret ) );
1774}
1775
1776psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1777 const uint8_t *input,
1778 size_t input_length )
1779{
1780 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001781
1782 /* Don't require hash implementations to behave correctly on a
1783 * zero-length input, which may have an invalid pointer. */
1784 if( input_length == 0 )
1785 return( PSA_SUCCESS );
1786
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001787 switch( operation->alg )
1788 {
1789#if defined(MBEDTLS_MD2_C)
1790 case PSA_ALG_MD2:
1791 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1792 input, input_length );
1793 break;
1794#endif
1795#if defined(MBEDTLS_MD4_C)
1796 case PSA_ALG_MD4:
1797 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1798 input, input_length );
1799 break;
1800#endif
1801#if defined(MBEDTLS_MD5_C)
1802 case PSA_ALG_MD5:
1803 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1804 input, input_length );
1805 break;
1806#endif
1807#if defined(MBEDTLS_RIPEMD160_C)
1808 case PSA_ALG_RIPEMD160:
1809 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1810 input, input_length );
1811 break;
1812#endif
1813#if defined(MBEDTLS_SHA1_C)
1814 case PSA_ALG_SHA_1:
1815 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1816 input, input_length );
1817 break;
1818#endif
1819#if defined(MBEDTLS_SHA256_C)
1820 case PSA_ALG_SHA_224:
1821 case PSA_ALG_SHA_256:
1822 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1823 input, input_length );
1824 break;
1825#endif
1826#if defined(MBEDTLS_SHA512_C)
1827 case PSA_ALG_SHA_384:
1828 case PSA_ALG_SHA_512:
1829 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1830 input, input_length );
1831 break;
1832#endif
1833 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001834 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001835 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001836
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001837 if( ret != 0 )
1838 psa_hash_abort( operation );
1839 return( mbedtls_to_psa_error( ret ) );
1840}
1841
1842psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1843 uint8_t *hash,
1844 size_t hash_size,
1845 size_t *hash_length )
1846{
itayzafrir40835d42018-08-02 13:14:17 +03001847 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001848 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001849 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001850
1851 /* Fill the output buffer with something that isn't a valid hash
1852 * (barring an attack on the hash and deliberately-crafted input),
1853 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001854 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001855 /* If hash_size is 0 then hash may be NULL and then the
1856 * call to memset would have undefined behavior. */
1857 if( hash_size != 0 )
1858 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001859
1860 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001861 {
1862 status = PSA_ERROR_BUFFER_TOO_SMALL;
1863 goto exit;
1864 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001865
1866 switch( operation->alg )
1867 {
1868#if defined(MBEDTLS_MD2_C)
1869 case PSA_ALG_MD2:
1870 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1871 break;
1872#endif
1873#if defined(MBEDTLS_MD4_C)
1874 case PSA_ALG_MD4:
1875 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1876 break;
1877#endif
1878#if defined(MBEDTLS_MD5_C)
1879 case PSA_ALG_MD5:
1880 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1881 break;
1882#endif
1883#if defined(MBEDTLS_RIPEMD160_C)
1884 case PSA_ALG_RIPEMD160:
1885 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1886 break;
1887#endif
1888#if defined(MBEDTLS_SHA1_C)
1889 case PSA_ALG_SHA_1:
1890 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1891 break;
1892#endif
1893#if defined(MBEDTLS_SHA256_C)
1894 case PSA_ALG_SHA_224:
1895 case PSA_ALG_SHA_256:
1896 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1897 break;
1898#endif
1899#if defined(MBEDTLS_SHA512_C)
1900 case PSA_ALG_SHA_384:
1901 case PSA_ALG_SHA_512:
1902 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1903 break;
1904#endif
1905 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001906 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001907 }
itayzafrir40835d42018-08-02 13:14:17 +03001908 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001909
itayzafrir40835d42018-08-02 13:14:17 +03001910exit:
1911 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001912 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001913 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001914 return( psa_hash_abort( operation ) );
1915 }
1916 else
1917 {
1918 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001919 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001920 }
1921}
1922
Gilles Peskine2d277862018-06-18 15:41:12 +02001923psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1924 const uint8_t *hash,
1925 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001926{
1927 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1928 size_t actual_hash_length;
1929 psa_status_t status = psa_hash_finish( operation,
1930 actual_hash, sizeof( actual_hash ),
1931 &actual_hash_length );
1932 if( status != PSA_SUCCESS )
1933 return( status );
1934 if( actual_hash_length != hash_length )
1935 return( PSA_ERROR_INVALID_SIGNATURE );
1936 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1937 return( PSA_ERROR_INVALID_SIGNATURE );
1938 return( PSA_SUCCESS );
1939}
1940
Gilles Peskineeb35d782019-01-22 17:56:16 +01001941psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1942 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001943{
1944 if( target_operation->alg != 0 )
1945 return( PSA_ERROR_BAD_STATE );
1946
1947 switch( source_operation->alg )
1948 {
1949 case 0:
1950 return( PSA_ERROR_BAD_STATE );
1951#if defined(MBEDTLS_MD2_C)
1952 case PSA_ALG_MD2:
1953 mbedtls_md2_clone( &target_operation->ctx.md2,
1954 &source_operation->ctx.md2 );
1955 break;
1956#endif
1957#if defined(MBEDTLS_MD4_C)
1958 case PSA_ALG_MD4:
1959 mbedtls_md4_clone( &target_operation->ctx.md4,
1960 &source_operation->ctx.md4 );
1961 break;
1962#endif
1963#if defined(MBEDTLS_MD5_C)
1964 case PSA_ALG_MD5:
1965 mbedtls_md5_clone( &target_operation->ctx.md5,
1966 &source_operation->ctx.md5 );
1967 break;
1968#endif
1969#if defined(MBEDTLS_RIPEMD160_C)
1970 case PSA_ALG_RIPEMD160:
1971 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1972 &source_operation->ctx.ripemd160 );
1973 break;
1974#endif
1975#if defined(MBEDTLS_SHA1_C)
1976 case PSA_ALG_SHA_1:
1977 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1978 &source_operation->ctx.sha1 );
1979 break;
1980#endif
1981#if defined(MBEDTLS_SHA256_C)
1982 case PSA_ALG_SHA_224:
1983 case PSA_ALG_SHA_256:
1984 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1985 &source_operation->ctx.sha256 );
1986 break;
1987#endif
1988#if defined(MBEDTLS_SHA512_C)
1989 case PSA_ALG_SHA_384:
1990 case PSA_ALG_SHA_512:
1991 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1992 &source_operation->ctx.sha512 );
1993 break;
1994#endif
1995 default:
1996 return( PSA_ERROR_NOT_SUPPORTED );
1997 }
1998
1999 target_operation->alg = source_operation->alg;
2000 return( PSA_SUCCESS );
2001}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002002
2003
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002004/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005/* MAC */
2006/****************************************************************/
2007
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002008static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009 psa_algorithm_t alg,
2010 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002011 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002012 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002015 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002016
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002017 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002018 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002019
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2021 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002022 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002024 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025 mode = MBEDTLS_MODE_STREAM;
2026 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 case PSA_ALG_CTR:
2028 mode = MBEDTLS_MODE_CTR;
2029 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002030 case PSA_ALG_CFB:
2031 mode = MBEDTLS_MODE_CFB;
2032 break;
2033 case PSA_ALG_OFB:
2034 mode = MBEDTLS_MODE_OFB;
2035 break;
2036 case PSA_ALG_CBC_NO_PADDING:
2037 mode = MBEDTLS_MODE_CBC;
2038 break;
2039 case PSA_ALG_CBC_PKCS7:
2040 mode = MBEDTLS_MODE_CBC;
2041 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002042 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043 mode = MBEDTLS_MODE_CCM;
2044 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002045 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046 mode = MBEDTLS_MODE_GCM;
2047 break;
2048 default:
2049 return( NULL );
2050 }
2051 }
2052 else if( alg == PSA_ALG_CMAC )
2053 mode = MBEDTLS_MODE_ECB;
2054 else if( alg == PSA_ALG_GMAC )
2055 mode = MBEDTLS_MODE_GCM;
2056 else
2057 return( NULL );
2058
2059 switch( key_type )
2060 {
2061 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002062 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063 break;
2064 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002065 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2066 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002068 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002070 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002071 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2072 * but two-key Triple-DES is functionally three-key Triple-DES
2073 * with K1=K3, so that's how we present it to mbedtls. */
2074 if( key_bits == 128 )
2075 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002076 break;
2077 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002078 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079 break;
2080 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002081 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002082 break;
2083 default:
2084 return( NULL );
2085 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002086 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002087 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088
Jaeden Amero23bbb752018-06-26 14:16:54 +01002089 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2090 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091}
2092
Gilles Peskinea05219c2018-11-16 16:02:56 +01002093#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002094static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002095{
Gilles Peskine2d277862018-06-18 15:41:12 +02002096 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002097 {
2098 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002099 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002100 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002101 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002102 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002103 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002104 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002105 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002106 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002107 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002108 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002109 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002110 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002111 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002112 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002113 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002114 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002115 return( 128 );
2116 default:
2117 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002118 }
2119}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002120#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002121
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002122/* Initialize the MAC operation structure. Once this function has been
2123 * called, psa_mac_abort can run and will do the right thing. */
2124static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2125 psa_algorithm_t alg )
2126{
2127 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2128
2129 operation->alg = alg;
2130 operation->key_set = 0;
2131 operation->iv_set = 0;
2132 operation->iv_required = 0;
2133 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002134 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002135
2136#if defined(MBEDTLS_CMAC_C)
2137 if( alg == PSA_ALG_CMAC )
2138 {
2139 operation->iv_required = 0;
2140 mbedtls_cipher_init( &operation->ctx.cmac );
2141 status = PSA_SUCCESS;
2142 }
2143 else
2144#endif /* MBEDTLS_CMAC_C */
2145#if defined(MBEDTLS_MD_C)
2146 if( PSA_ALG_IS_HMAC( operation->alg ) )
2147 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002148 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2149 operation->ctx.hmac.hash_ctx.alg = 0;
2150 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002151 }
2152 else
2153#endif /* MBEDTLS_MD_C */
2154 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002155 if( ! PSA_ALG_IS_MAC( alg ) )
2156 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002157 }
2158
2159 if( status != PSA_SUCCESS )
2160 memset( operation, 0, sizeof( *operation ) );
2161 return( status );
2162}
2163
Gilles Peskine01126fa2018-07-12 17:04:55 +02002164#if defined(MBEDTLS_MD_C)
2165static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2166{
Gilles Peskine3f108122018-12-07 18:14:53 +01002167 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002168 return( psa_hash_abort( &hmac->hash_ctx ) );
2169}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002170
2171static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2172{
2173 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2174 memset( hmac, 0, sizeof( *hmac ) );
2175}
Gilles Peskine01126fa2018-07-12 17:04:55 +02002176#endif /* MBEDTLS_MD_C */
2177
Gilles Peskine8c9def32018-02-08 10:02:12 +01002178psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2179{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002180 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002181 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002182 /* The object has (apparently) been initialized but it is not
2183 * in use. It's ok to call abort on such an object, and there's
2184 * nothing to do. */
2185 return( PSA_SUCCESS );
2186 }
2187 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002189 if( operation->alg == PSA_ALG_CMAC )
2190 {
2191 mbedtls_cipher_free( &operation->ctx.cmac );
2192 }
2193 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002194#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002195#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002196 if( PSA_ALG_IS_HMAC( operation->alg ) )
2197 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002198 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002199 }
2200 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002201#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002202 {
2203 /* Sanity check (shouldn't happen: operation->alg should
2204 * always have been initialized to a valid value). */
2205 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206 }
Moran Peker41deec42018-04-04 15:43:05 +03002207
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208 operation->alg = 0;
2209 operation->key_set = 0;
2210 operation->iv_set = 0;
2211 operation->iv_required = 0;
2212 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002213 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002214
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002216
2217bad_state:
2218 /* If abort is called on an uninitialized object, we can't trust
2219 * anything. Wipe the object in case it contains confidential data.
2220 * This may result in a memory leak if a pointer gets overwritten,
2221 * but it's too late to do anything about this. */
2222 memset( operation, 0, sizeof( *operation ) );
2223 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224}
2225
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002226#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002227static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002228 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002229 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002230 const mbedtls_cipher_info_t *cipher_info )
2231{
2232 int ret;
2233
2234 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002235
2236 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2237 if( ret != 0 )
2238 return( ret );
2239
2240 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2241 slot->data.raw.data,
2242 key_bits );
2243 return( ret );
2244}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002245#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002246
Gilles Peskine248051a2018-06-20 16:09:38 +02002247#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002248static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2249 const uint8_t *key,
2250 size_t key_length,
2251 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002252{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002253 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002254 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002255 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002256 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002257 psa_status_t status;
2258
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002259 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2260 * overflow below. This should never trigger if the hash algorithm
2261 * is implemented correctly. */
2262 /* The size checks against the ipad and opad buffers cannot be written
2263 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2264 * because that triggers -Wlogical-op on GCC 7.3. */
2265 if( block_size > sizeof( ipad ) )
2266 return( PSA_ERROR_NOT_SUPPORTED );
2267 if( block_size > sizeof( hmac->opad ) )
2268 return( PSA_ERROR_NOT_SUPPORTED );
2269 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002270 return( PSA_ERROR_NOT_SUPPORTED );
2271
Gilles Peskined223b522018-06-11 18:12:58 +02002272 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002273 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002274 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002275 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002276 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002277 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002278 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002279 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002280 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002281 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002282 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002283 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002284 }
Gilles Peskine96889972018-07-12 17:07:03 +02002285 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2286 * but it is permitted. It is common when HMAC is used in HKDF, for
2287 * example. Don't call `memcpy` in the 0-length because `key` could be
2288 * an invalid pointer which would make the behavior undefined. */
2289 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002290 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002291
Gilles Peskined223b522018-06-11 18:12:58 +02002292 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2293 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002294 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002295 ipad[i] ^= 0x36;
2296 memset( ipad + key_length, 0x36, block_size - key_length );
2297
2298 /* Copy the key material from ipad to opad, flipping the requisite bits,
2299 * and filling the rest of opad with the requisite constant. */
2300 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002301 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2302 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002303
Gilles Peskine01126fa2018-07-12 17:04:55 +02002304 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002305 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002306 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002307
Gilles Peskine01126fa2018-07-12 17:04:55 +02002308 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002309
2310cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002311 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002312
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002313 return( status );
2314}
Gilles Peskine248051a2018-06-20 16:09:38 +02002315#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002316
Gilles Peskine89167cb2018-07-08 20:12:23 +02002317static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002318 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002319 psa_algorithm_t alg,
2320 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002321{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002322 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002323 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002325 psa_key_usage_t usage =
2326 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002327 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002328 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002330 /* A context must be freshly initialized before it can be set up. */
2331 if( operation->alg != 0 )
2332 {
2333 return( PSA_ERROR_BAD_STATE );
2334 }
2335
Gilles Peskined911eb72018-08-14 15:18:45 +02002336 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002337 if( status != PSA_SUCCESS )
2338 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002339 if( is_sign )
2340 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341
Gilles Peskinec5487a82018-12-03 18:08:14 +01002342 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002343 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002344 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002345 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002346
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002348 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002349 {
2350 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002351 mbedtls_cipher_info_from_psa( full_length_alg,
2352 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002353 int ret;
2354 if( cipher_info == NULL )
2355 {
2356 status = PSA_ERROR_NOT_SUPPORTED;
2357 goto exit;
2358 }
2359 operation->mac_size = cipher_info->block_size;
2360 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2361 status = mbedtls_to_psa_error( ret );
2362 }
2363 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002365#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002366 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002367 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002368 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002369 if( hash_alg == 0 )
2370 {
2371 status = PSA_ERROR_NOT_SUPPORTED;
2372 goto exit;
2373 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002374
2375 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2376 /* Sanity check. This shouldn't fail on a valid configuration. */
2377 if( operation->mac_size == 0 ||
2378 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2379 {
2380 status = PSA_ERROR_NOT_SUPPORTED;
2381 goto exit;
2382 }
2383
Gilles Peskine01126fa2018-07-12 17:04:55 +02002384 if( slot->type != PSA_KEY_TYPE_HMAC )
2385 {
2386 status = PSA_ERROR_INVALID_ARGUMENT;
2387 goto exit;
2388 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002389
Gilles Peskine01126fa2018-07-12 17:04:55 +02002390 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2391 slot->data.raw.data,
2392 slot->data.raw.bytes,
2393 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002394 }
2395 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002396#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002397 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002398 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002399 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002401
Gilles Peskined911eb72018-08-14 15:18:45 +02002402 if( truncated == 0 )
2403 {
2404 /* The "normal" case: untruncated algorithm. Nothing to do. */
2405 }
2406 else if( truncated < 4 )
2407 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002408 /* A very short MAC is too short for security since it can be
2409 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2410 * so we make this our minimum, even though 32 bits is still
2411 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002412 status = PSA_ERROR_NOT_SUPPORTED;
2413 }
2414 else if( truncated > operation->mac_size )
2415 {
2416 /* It's impossible to "truncate" to a larger length. */
2417 status = PSA_ERROR_INVALID_ARGUMENT;
2418 }
2419 else
2420 operation->mac_size = truncated;
2421
Gilles Peskinefbfac682018-07-08 20:51:54 +02002422exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002423 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002425 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002426 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002427 else
2428 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002429 operation->key_set = 1;
2430 }
2431 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432}
2433
Gilles Peskine89167cb2018-07-08 20:12:23 +02002434psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002435 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002436 psa_algorithm_t alg )
2437{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002438 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002439}
2440
2441psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002442 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443 psa_algorithm_t alg )
2444{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002445 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002446}
2447
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2449 const uint8_t *input,
2450 size_t input_length )
2451{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002452 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002454 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002456 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457 operation->has_input = 1;
2458
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002460 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002461 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002462 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2463 input, input_length );
2464 status = mbedtls_to_psa_error( ret );
2465 }
2466 else
2467#endif /* MBEDTLS_CMAC_C */
2468#if defined(MBEDTLS_MD_C)
2469 if( PSA_ALG_IS_HMAC( operation->alg ) )
2470 {
2471 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2472 input_length );
2473 }
2474 else
2475#endif /* MBEDTLS_MD_C */
2476 {
2477 /* This shouldn't happen if `operation` was initialized by
2478 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002479 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002480 }
2481
Gilles Peskinefbfac682018-07-08 20:51:54 +02002482 if( status != PSA_SUCCESS )
2483 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002484 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485}
2486
Gilles Peskine01126fa2018-07-12 17:04:55 +02002487#if defined(MBEDTLS_MD_C)
2488static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2489 uint8_t *mac,
2490 size_t mac_size )
2491{
2492 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2493 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2494 size_t hash_size = 0;
2495 size_t block_size = psa_get_hash_block_size( hash_alg );
2496 psa_status_t status;
2497
Gilles Peskine01126fa2018-07-12 17:04:55 +02002498 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2499 if( status != PSA_SUCCESS )
2500 return( status );
2501 /* From here on, tmp needs to be wiped. */
2502
2503 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2504 if( status != PSA_SUCCESS )
2505 goto exit;
2506
2507 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2508 if( status != PSA_SUCCESS )
2509 goto exit;
2510
2511 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2512 if( status != PSA_SUCCESS )
2513 goto exit;
2514
Gilles Peskined911eb72018-08-14 15:18:45 +02002515 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2516 if( status != PSA_SUCCESS )
2517 goto exit;
2518
2519 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002520
2521exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002522 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002523 return( status );
2524}
2525#endif /* MBEDTLS_MD_C */
2526
mohammad16036df908f2018-04-02 08:34:15 -07002527static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002528 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002529 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002531 if( ! operation->key_set )
2532 return( PSA_ERROR_BAD_STATE );
2533 if( operation->iv_required && ! operation->iv_set )
2534 return( PSA_ERROR_BAD_STATE );
2535
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536 if( mac_size < operation->mac_size )
2537 return( PSA_ERROR_BUFFER_TOO_SMALL );
2538
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002540 if( operation->alg == PSA_ALG_CMAC )
2541 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002542 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2543 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2544 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002545 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002546 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002547 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002549 else
2550#endif /* MBEDTLS_CMAC_C */
2551#if defined(MBEDTLS_MD_C)
2552 if( PSA_ALG_IS_HMAC( operation->alg ) )
2553 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002554 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002555 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002556 }
2557 else
2558#endif /* MBEDTLS_MD_C */
2559 {
2560 /* This shouldn't happen if `operation` was initialized by
2561 * a setup function. */
2562 return( PSA_ERROR_BAD_STATE );
2563 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564}
2565
Gilles Peskineacd4be32018-07-08 19:56:25 +02002566psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2567 uint8_t *mac,
2568 size_t mac_size,
2569 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002570{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002571 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 Peskine5d0b8642018-07-08 20:35:02 +02002578 /* Fill the output buffer with something that isn't a valid mac
2579 * (barring an attack on the mac and deliberately-crafted input),
2580 * in case the caller doesn't check the return status properly. */
2581 *mac_length = mac_size;
2582 /* If mac_size is 0 then mac may be NULL and then the
2583 * call to memset would have undefined behavior. */
2584 if( mac_size != 0 )
2585 memset( mac, '!', mac_size );
2586
Gilles Peskine89167cb2018-07-08 20:12:23 +02002587 if( ! operation->is_sign )
2588 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002589 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002590 }
mohammad16036df908f2018-04-02 08:34:15 -07002591
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002592 status = psa_mac_finish_internal( operation, mac, mac_size );
2593
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002594 if( status == PSA_SUCCESS )
2595 {
2596 status = psa_mac_abort( operation );
2597 if( status == PSA_SUCCESS )
2598 *mac_length = operation->mac_size;
2599 else
2600 memset( mac, '!', mac_size );
2601 }
2602 else
2603 psa_mac_abort( operation );
2604 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002605}
2606
Gilles Peskineacd4be32018-07-08 19:56:25 +02002607psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2608 const uint8_t *mac,
2609 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610{
Gilles Peskine828ed142018-06-18 23:25:51 +02002611 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002612 psa_status_t status;
2613
Jaeden Amero252ef282019-02-15 14:05:35 +00002614 if( operation->alg == 0 )
2615 {
2616 return( PSA_ERROR_BAD_STATE );
2617 }
2618
Gilles Peskine89167cb2018-07-08 20:12:23 +02002619 if( operation->is_sign )
2620 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002621 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002622 }
2623 if( operation->mac_size != mac_length )
2624 {
2625 status = PSA_ERROR_INVALID_SIGNATURE;
2626 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627 }
mohammad16036df908f2018-04-02 08:34:15 -07002628
2629 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002630 actual_mac, sizeof( actual_mac ) );
2631
2632 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2633 status = PSA_ERROR_INVALID_SIGNATURE;
2634
2635cleanup:
2636 if( status == PSA_SUCCESS )
2637 status = psa_mac_abort( operation );
2638 else
2639 psa_mac_abort( operation );
2640
Gilles Peskine3f108122018-12-07 18:14:53 +01002641 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002642
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002643 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644}
2645
2646
Gilles Peskine20035e32018-02-03 22:44:14 +01002647
Gilles Peskine20035e32018-02-03 22:44:14 +01002648/****************************************************************/
2649/* Asymmetric cryptography */
2650/****************************************************************/
2651
Gilles Peskine2b450e32018-06-27 15:42:46 +02002652#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002653/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002654 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002655static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2656 size_t hash_length,
2657 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002658{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002659 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002660 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002661 *md_alg = mbedtls_md_get_type( md_info );
2662
2663 /* The Mbed TLS RSA module uses an unsigned int for hash length
2664 * parameters. Validate that it fits so that we don't risk an
2665 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002666#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002667 if( hash_length > UINT_MAX )
2668 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002669#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002670
2671#if defined(MBEDTLS_PKCS1_V15)
2672 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2673 * must be correct. */
2674 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2675 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002676 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002677 if( md_info == NULL )
2678 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002679 if( mbedtls_md_get_size( md_info ) != hash_length )
2680 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002681 }
2682#endif /* MBEDTLS_PKCS1_V15 */
2683
2684#if defined(MBEDTLS_PKCS1_V21)
2685 /* PSS requires a hash internally. */
2686 if( PSA_ALG_IS_RSA_PSS( alg ) )
2687 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002688 if( md_info == NULL )
2689 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002690 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002691#endif /* MBEDTLS_PKCS1_V21 */
2692
Gilles Peskine61b91d42018-06-08 16:09:36 +02002693 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002694}
2695
Gilles Peskine2b450e32018-06-27 15:42:46 +02002696static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2697 psa_algorithm_t alg,
2698 const uint8_t *hash,
2699 size_t hash_length,
2700 uint8_t *signature,
2701 size_t signature_size,
2702 size_t *signature_length )
2703{
2704 psa_status_t status;
2705 int ret;
2706 mbedtls_md_type_t md_alg;
2707
2708 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2709 if( status != PSA_SUCCESS )
2710 return( status );
2711
Gilles Peskine630a18a2018-06-29 17:49:35 +02002712 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002713 return( PSA_ERROR_BUFFER_TOO_SMALL );
2714
2715#if defined(MBEDTLS_PKCS1_V15)
2716 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2717 {
2718 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2719 MBEDTLS_MD_NONE );
2720 ret = mbedtls_rsa_pkcs1_sign( rsa,
2721 mbedtls_ctr_drbg_random,
2722 &global_data.ctr_drbg,
2723 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002724 md_alg,
2725 (unsigned int) hash_length,
2726 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002727 signature );
2728 }
2729 else
2730#endif /* MBEDTLS_PKCS1_V15 */
2731#if defined(MBEDTLS_PKCS1_V21)
2732 if( PSA_ALG_IS_RSA_PSS( alg ) )
2733 {
2734 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2735 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2736 mbedtls_ctr_drbg_random,
2737 &global_data.ctr_drbg,
2738 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002739 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002740 (unsigned int) hash_length,
2741 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002742 signature );
2743 }
2744 else
2745#endif /* MBEDTLS_PKCS1_V21 */
2746 {
2747 return( PSA_ERROR_INVALID_ARGUMENT );
2748 }
2749
2750 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002751 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002752 return( mbedtls_to_psa_error( ret ) );
2753}
2754
2755static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2756 psa_algorithm_t alg,
2757 const uint8_t *hash,
2758 size_t hash_length,
2759 const uint8_t *signature,
2760 size_t signature_length )
2761{
2762 psa_status_t status;
2763 int ret;
2764 mbedtls_md_type_t md_alg;
2765
2766 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2767 if( status != PSA_SUCCESS )
2768 return( status );
2769
Gilles Peskine630a18a2018-06-29 17:49:35 +02002770 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002771 return( PSA_ERROR_BUFFER_TOO_SMALL );
2772
2773#if defined(MBEDTLS_PKCS1_V15)
2774 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2775 {
2776 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2777 MBEDTLS_MD_NONE );
2778 ret = mbedtls_rsa_pkcs1_verify( rsa,
2779 mbedtls_ctr_drbg_random,
2780 &global_data.ctr_drbg,
2781 MBEDTLS_RSA_PUBLIC,
2782 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002783 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002784 hash,
2785 signature );
2786 }
2787 else
2788#endif /* MBEDTLS_PKCS1_V15 */
2789#if defined(MBEDTLS_PKCS1_V21)
2790 if( PSA_ALG_IS_RSA_PSS( alg ) )
2791 {
2792 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2793 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2794 mbedtls_ctr_drbg_random,
2795 &global_data.ctr_drbg,
2796 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002797 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002798 (unsigned int) hash_length,
2799 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002800 signature );
2801 }
2802 else
2803#endif /* MBEDTLS_PKCS1_V21 */
2804 {
2805 return( PSA_ERROR_INVALID_ARGUMENT );
2806 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002807
2808 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2809 * the rest of the signature is invalid". This has little use in
2810 * practice and PSA doesn't report this distinction. */
2811 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2812 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002813 return( mbedtls_to_psa_error( ret ) );
2814}
2815#endif /* MBEDTLS_RSA_C */
2816
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002817#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002818/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2819 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2820 * (even though these functions don't modify it). */
2821static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2822 psa_algorithm_t alg,
2823 const uint8_t *hash,
2824 size_t hash_length,
2825 uint8_t *signature,
2826 size_t signature_size,
2827 size_t *signature_length )
2828{
2829 int ret;
2830 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002831 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002832 mbedtls_mpi_init( &r );
2833 mbedtls_mpi_init( &s );
2834
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002835 if( signature_size < 2 * curve_bytes )
2836 {
2837 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2838 goto cleanup;
2839 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002840
Gilles Peskinea05219c2018-11-16 16:02:56 +01002841#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002842 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2843 {
2844 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2845 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2846 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2847 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2848 hash, hash_length,
2849 md_alg ) );
2850 }
2851 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002852#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002853 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002854 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002855 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2856 hash, hash_length,
2857 mbedtls_ctr_drbg_random,
2858 &global_data.ctr_drbg ) );
2859 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002860
2861 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2862 signature,
2863 curve_bytes ) );
2864 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2865 signature + curve_bytes,
2866 curve_bytes ) );
2867
2868cleanup:
2869 mbedtls_mpi_free( &r );
2870 mbedtls_mpi_free( &s );
2871 if( ret == 0 )
2872 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002873 return( mbedtls_to_psa_error( ret ) );
2874}
2875
2876static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2877 const uint8_t *hash,
2878 size_t hash_length,
2879 const uint8_t *signature,
2880 size_t signature_length )
2881{
2882 int ret;
2883 mbedtls_mpi r, s;
2884 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2885 mbedtls_mpi_init( &r );
2886 mbedtls_mpi_init( &s );
2887
2888 if( signature_length != 2 * curve_bytes )
2889 return( PSA_ERROR_INVALID_SIGNATURE );
2890
2891 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2892 signature,
2893 curve_bytes ) );
2894 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2895 signature + curve_bytes,
2896 curve_bytes ) );
2897
2898 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2899 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002900
2901cleanup:
2902 mbedtls_mpi_free( &r );
2903 mbedtls_mpi_free( &s );
2904 return( mbedtls_to_psa_error( ret ) );
2905}
2906#endif /* MBEDTLS_ECDSA_C */
2907
Gilles Peskinec5487a82018-12-03 18:08:14 +01002908psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002909 psa_algorithm_t alg,
2910 const uint8_t *hash,
2911 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002912 uint8_t *signature,
2913 size_t signature_size,
2914 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002915{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002916 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002917 psa_status_t status;
2918
2919 *signature_length = signature_size;
2920
Gilles Peskinec5487a82018-12-03 18:08:14 +01002921 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002922 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002923 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002924 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002925 {
2926 status = PSA_ERROR_INVALID_ARGUMENT;
2927 goto exit;
2928 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002929
Gilles Peskine20035e32018-02-03 22:44:14 +01002930#if defined(MBEDTLS_RSA_C)
2931 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2932 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002933 status = psa_rsa_sign( slot->data.rsa,
2934 alg,
2935 hash, hash_length,
2936 signature, signature_size,
2937 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002938 }
2939 else
2940#endif /* defined(MBEDTLS_RSA_C) */
2941#if defined(MBEDTLS_ECP_C)
2942 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2943 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002944#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002945 if(
2946#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2947 PSA_ALG_IS_ECDSA( alg )
2948#else
2949 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2950#endif
2951 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002952 status = psa_ecdsa_sign( slot->data.ecp,
2953 alg,
2954 hash, hash_length,
2955 signature, signature_size,
2956 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002957 else
2958#endif /* defined(MBEDTLS_ECDSA_C) */
2959 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002960 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002961 }
itayzafrir5c753392018-05-08 11:18:38 +03002962 }
2963 else
2964#endif /* defined(MBEDTLS_ECP_C) */
2965 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002966 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002967 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002968
2969exit:
2970 /* Fill the unused part of the output buffer (the whole buffer on error,
2971 * the trailing part on success) with something that isn't a valid mac
2972 * (barring an attack on the mac and deliberately-crafted input),
2973 * in case the caller doesn't check the return status properly. */
2974 if( status == PSA_SUCCESS )
2975 memset( signature + *signature_length, '!',
2976 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002977 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002978 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002979 /* If signature_size is 0 then we have nothing to do. We must not call
2980 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002981 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002982}
2983
Gilles Peskinec5487a82018-12-03 18:08:14 +01002984psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002985 psa_algorithm_t alg,
2986 const uint8_t *hash,
2987 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002988 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002989 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002990{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002991 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002992 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002993
Gilles Peskinec5487a82018-12-03 18:08:14 +01002994 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002995 if( status != PSA_SUCCESS )
2996 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002997
Gilles Peskine61b91d42018-06-08 16:09:36 +02002998#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002999 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003000 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003001 return( psa_rsa_verify( slot->data.rsa,
3002 alg,
3003 hash, hash_length,
3004 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003005 }
3006 else
3007#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003008#if defined(MBEDTLS_ECP_C)
3009 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3010 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003011#if defined(MBEDTLS_ECDSA_C)
3012 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003013 return( psa_ecdsa_verify( slot->data.ecp,
3014 hash, hash_length,
3015 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003016 else
3017#endif /* defined(MBEDTLS_ECDSA_C) */
3018 {
3019 return( PSA_ERROR_INVALID_ARGUMENT );
3020 }
itayzafrir5c753392018-05-08 11:18:38 +03003021 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003022 else
3023#endif /* defined(MBEDTLS_ECP_C) */
3024 {
3025 return( PSA_ERROR_NOT_SUPPORTED );
3026 }
3027}
3028
Gilles Peskine072ac562018-06-30 00:21:29 +02003029#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3030static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3031 mbedtls_rsa_context *rsa )
3032{
3033 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3034 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3035 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3036 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3037}
3038#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3039
Gilles Peskinec5487a82018-12-03 18:08:14 +01003040psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003041 psa_algorithm_t alg,
3042 const uint8_t *input,
3043 size_t input_length,
3044 const uint8_t *salt,
3045 size_t salt_length,
3046 uint8_t *output,
3047 size_t output_size,
3048 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003049{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003050 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003051 psa_status_t status;
3052
Darryl Green5cc689a2018-07-24 15:34:10 +01003053 (void) input;
3054 (void) input_length;
3055 (void) salt;
3056 (void) output;
3057 (void) output_size;
3058
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003059 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003061 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3062 return( PSA_ERROR_INVALID_ARGUMENT );
3063
Gilles Peskinec5487a82018-12-03 18:08:14 +01003064 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003065 if( status != PSA_SUCCESS )
3066 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003067 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
3068 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070
3071#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003072 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073 {
3074 mbedtls_rsa_context *rsa = slot->data.rsa;
3075 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003076 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003077 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003079 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003080 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003081 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3082 mbedtls_ctr_drbg_random,
3083 &global_data.ctr_drbg,
3084 MBEDTLS_RSA_PUBLIC,
3085 input_length,
3086 input,
3087 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088 }
3089 else
3090#endif /* MBEDTLS_PKCS1_V15 */
3091#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003092 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003094 psa_rsa_oaep_set_padding_mode( alg, rsa );
3095 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3096 mbedtls_ctr_drbg_random,
3097 &global_data.ctr_drbg,
3098 MBEDTLS_RSA_PUBLIC,
3099 salt, salt_length,
3100 input_length,
3101 input,
3102 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103 }
3104 else
3105#endif /* MBEDTLS_PKCS1_V21 */
3106 {
3107 return( PSA_ERROR_INVALID_ARGUMENT );
3108 }
3109 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003110 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111 return( mbedtls_to_psa_error( ret ) );
3112 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003114#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 {
3116 return( PSA_ERROR_NOT_SUPPORTED );
3117 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003118}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119
Gilles Peskinec5487a82018-12-03 18:08:14 +01003120psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003121 psa_algorithm_t alg,
3122 const uint8_t *input,
3123 size_t input_length,
3124 const uint8_t *salt,
3125 size_t salt_length,
3126 uint8_t *output,
3127 size_t output_size,
3128 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003130 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003131 psa_status_t status;
3132
Darryl Green5cc689a2018-07-24 15:34:10 +01003133 (void) input;
3134 (void) input_length;
3135 (void) salt;
3136 (void) output;
3137 (void) output_size;
3138
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003139 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003141 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3142 return( PSA_ERROR_INVALID_ARGUMENT );
3143
Gilles Peskinec5487a82018-12-03 18:08:14 +01003144 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003145 if( status != PSA_SUCCESS )
3146 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
3148 return( PSA_ERROR_INVALID_ARGUMENT );
3149
3150#if defined(MBEDTLS_RSA_C)
3151 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
3152 {
3153 mbedtls_rsa_context *rsa = slot->data.rsa;
3154 int ret;
3155
Gilles Peskine630a18a2018-06-29 17:49:35 +02003156 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157 return( PSA_ERROR_INVALID_ARGUMENT );
3158
3159#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003160 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003162 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3163 mbedtls_ctr_drbg_random,
3164 &global_data.ctr_drbg,
3165 MBEDTLS_RSA_PRIVATE,
3166 output_length,
3167 input,
3168 output,
3169 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170 }
3171 else
3172#endif /* MBEDTLS_PKCS1_V15 */
3173#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003174 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003176 psa_rsa_oaep_set_padding_mode( alg, rsa );
3177 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3178 mbedtls_ctr_drbg_random,
3179 &global_data.ctr_drbg,
3180 MBEDTLS_RSA_PRIVATE,
3181 salt, salt_length,
3182 output_length,
3183 input,
3184 output,
3185 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186 }
3187 else
3188#endif /* MBEDTLS_PKCS1_V21 */
3189 {
3190 return( PSA_ERROR_INVALID_ARGUMENT );
3191 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003192
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193 return( mbedtls_to_psa_error( ret ) );
3194 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003196#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 {
3198 return( PSA_ERROR_NOT_SUPPORTED );
3199 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003200}
Gilles Peskine20035e32018-02-03 22:44:14 +01003201
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003202
3203
mohammad1603503973b2018-03-12 15:59:30 +02003204/****************************************************************/
3205/* Symmetric cryptography */
3206/****************************************************************/
3207
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003208/* Initialize the cipher operation structure. Once this function has been
3209 * called, psa_cipher_abort can run and will do the right thing. */
3210static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3211 psa_algorithm_t alg )
3212{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003213 if( ! PSA_ALG_IS_CIPHER( alg ) )
3214 {
3215 memset( operation, 0, sizeof( *operation ) );
3216 return( PSA_ERROR_INVALID_ARGUMENT );
3217 }
3218
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003219 operation->alg = alg;
3220 operation->key_set = 0;
3221 operation->iv_set = 0;
3222 operation->iv_required = 1;
3223 operation->iv_size = 0;
3224 operation->block_size = 0;
3225 mbedtls_cipher_init( &operation->ctx.cipher );
3226 return( PSA_SUCCESS );
3227}
3228
Gilles Peskinee553c652018-06-04 16:22:46 +02003229static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003230 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003231 psa_algorithm_t alg,
3232 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003233{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003234 int ret = 0;
3235 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003236 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003237 size_t key_bits;
3238 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003239 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3240 PSA_KEY_USAGE_ENCRYPT :
3241 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003242
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003243 /* A context must be freshly initialized before it can be set up. */
3244 if( operation->alg != 0 )
3245 {
3246 return( PSA_ERROR_BAD_STATE );
3247 }
3248
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003249 status = psa_cipher_init( operation, alg );
3250 if( status != PSA_SUCCESS )
3251 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003252
Gilles Peskinec5487a82018-12-03 18:08:14 +01003253 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003255 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003256 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003257
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003258 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003259 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003260 {
3261 status = PSA_ERROR_NOT_SUPPORTED;
3262 goto exit;
3263 }
mohammad1603503973b2018-03-12 15:59:30 +02003264
mohammad1603503973b2018-03-12 15:59:30 +02003265 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003266 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003267 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003268
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003269#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003270 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003271 {
3272 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3273 unsigned char keys[24];
3274 memcpy( keys, slot->data.raw.data, 16 );
3275 memcpy( keys + 16, slot->data.raw.data, 8 );
3276 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3277 keys,
3278 192, cipher_operation );
3279 }
3280 else
3281#endif
3282 {
3283 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3284 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003285 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003286 }
Moran Peker41deec42018-04-04 15:43:05 +03003287 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003288 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003289
mohammad16038481e742018-03-18 13:57:31 +02003290#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003291 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003292 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003293 case PSA_ALG_CBC_NO_PADDING:
3294 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3295 MBEDTLS_PADDING_NONE );
3296 break;
3297 case PSA_ALG_CBC_PKCS7:
3298 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3299 MBEDTLS_PADDING_PKCS7 );
3300 break;
3301 default:
3302 /* The algorithm doesn't involve padding. */
3303 ret = 0;
3304 break;
3305 }
3306 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003307 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003308#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3309
mohammad1603503973b2018-03-12 15:59:30 +02003310 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003311 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3312 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3313 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003314 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003315 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003316 }
mohammad1603503973b2018-03-12 15:59:30 +02003317
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003318exit:
3319 if( status == 0 )
3320 status = mbedtls_to_psa_error( ret );
3321 if( status != 0 )
3322 psa_cipher_abort( operation );
3323 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003324}
3325
Gilles Peskinefe119512018-07-08 21:39:34 +02003326psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003327 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003328 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003329{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003330 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003331}
3332
Gilles Peskinefe119512018-07-08 21:39:34 +02003333psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003334 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003335 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003336{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003337 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003338}
3339
Gilles Peskinefe119512018-07-08 21:39:34 +02003340psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3341 unsigned char *iv,
3342 size_t iv_size,
3343 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003344{
itayzafrir534bd7c2018-08-02 13:56:32 +03003345 psa_status_t status;
3346 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003347 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003348 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003349 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003350 }
Moran Peker41deec42018-04-04 15:43:05 +03003351 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003352 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003353 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003354 goto exit;
3355 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003356 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3357 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003358 if( ret != 0 )
3359 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003360 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003361 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003362 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003363
mohammad16038481e742018-03-18 13:57:31 +02003364 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003365 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003366
Moran Peker395db872018-05-31 14:07:14 +03003367exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003368 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003369 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003370 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003371}
3372
Gilles Peskinefe119512018-07-08 21:39:34 +02003373psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3374 const unsigned char *iv,
3375 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003376{
itayzafrir534bd7c2018-08-02 13:56:32 +03003377 psa_status_t status;
3378 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003379 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003380 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003381 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003382 }
Moran Pekera28258c2018-05-29 16:25:04 +03003383 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003384 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003385 status = PSA_ERROR_INVALID_ARGUMENT;
3386 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003387 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003388 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3389 status = mbedtls_to_psa_error( ret );
3390exit:
3391 if( status == PSA_SUCCESS )
3392 operation->iv_set = 1;
3393 else
mohammad1603503973b2018-03-12 15:59:30 +02003394 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003395 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003396}
3397
Gilles Peskinee553c652018-06-04 16:22:46 +02003398psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3399 const uint8_t *input,
3400 size_t input_length,
3401 unsigned char *output,
3402 size_t output_size,
3403 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003404{
itayzafrir534bd7c2018-08-02 13:56:32 +03003405 psa_status_t status;
3406 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003407 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003408
3409 if( operation->alg == 0 )
3410 {
3411 return( PSA_ERROR_BAD_STATE );
3412 }
3413
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003414 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003415 {
3416 /* Take the unprocessed partial block left over from previous
3417 * update calls, if any, plus the input to this call. Remove
3418 * the last partial block, if any. You get the data that will be
3419 * output in this call. */
3420 expected_output_size =
3421 ( operation->ctx.cipher.unprocessed_len + input_length )
3422 / operation->block_size * operation->block_size;
3423 }
3424 else
3425 {
3426 expected_output_size = input_length;
3427 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003428
Gilles Peskine89d789c2018-06-04 17:17:16 +02003429 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003430 {
3431 status = PSA_ERROR_BUFFER_TOO_SMALL;
3432 goto exit;
3433 }
mohammad160382759612018-03-12 18:16:40 +02003434
mohammad1603503973b2018-03-12 15:59:30 +02003435 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003436 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003437 status = mbedtls_to_psa_error( ret );
3438exit:
3439 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003440 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003441 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003442}
3443
Gilles Peskinee553c652018-06-04 16:22:46 +02003444psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3445 uint8_t *output,
3446 size_t output_size,
3447 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003448{
David Saadab4ecc272019-02-14 13:48:10 +02003449 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003450 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003451 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003452
mohammad1603503973b2018-03-12 15:59:30 +02003453 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003454 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003455 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003456 }
3457 if( operation->iv_required && ! operation->iv_set )
3458 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003459 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003460 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003461
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003462 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003463 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3464 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003465 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003466 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003467 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003468 }
3469
Janos Follath315b51c2018-07-09 16:04:51 +01003470 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3471 temp_output_buffer,
3472 output_length );
3473 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003474 {
Janos Follath315b51c2018-07-09 16:04:51 +01003475 status = mbedtls_to_psa_error( cipher_ret );
3476 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003477 }
Janos Follath315b51c2018-07-09 16:04:51 +01003478
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003479 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003480 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003481 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003482 memcpy( output, temp_output_buffer, *output_length );
3483 else
3484 {
Janos Follath315b51c2018-07-09 16:04:51 +01003485 status = PSA_ERROR_BUFFER_TOO_SMALL;
3486 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003487 }
mohammad1603503973b2018-03-12 15:59:30 +02003488
Gilles Peskine3f108122018-12-07 18:14:53 +01003489 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003490 status = psa_cipher_abort( operation );
3491
3492 return( status );
3493
3494error:
3495
3496 *output_length = 0;
3497
Gilles Peskine3f108122018-12-07 18:14:53 +01003498 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003499 (void) psa_cipher_abort( operation );
3500
3501 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003502}
3503
Gilles Peskinee553c652018-06-04 16:22:46 +02003504psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3505{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003506 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003507 {
3508 /* The object has (apparently) been initialized but it is not
3509 * in use. It's ok to call abort on such an object, and there's
3510 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003511 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003512 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003513
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003514 /* Sanity check (shouldn't happen: operation->alg should
3515 * always have been initialized to a valid value). */
3516 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3517 return( PSA_ERROR_BAD_STATE );
3518
mohammad1603503973b2018-03-12 15:59:30 +02003519 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003520
Moran Peker41deec42018-04-04 15:43:05 +03003521 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003522 operation->key_set = 0;
3523 operation->iv_set = 0;
3524 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003525 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003526 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003527
Moran Peker395db872018-05-31 14:07:14 +03003528 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003529}
3530
Gilles Peskinea0655c32018-04-30 17:06:50 +02003531
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003532
mohammad16038cc1cee2018-03-28 01:21:33 +03003533/****************************************************************/
3534/* Key Policy */
3535/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003536
mohammad160327010052018-07-03 13:16:15 +03003537#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003538void psa_key_policy_set_usage( psa_key_policy_t *policy,
3539 psa_key_usage_t usage,
3540 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003541{
mohammad16034eed7572018-03-28 05:14:59 -07003542 policy->usage = usage;
3543 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003544}
3545
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003546psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003547{
mohammad16036df908f2018-04-02 08:34:15 -07003548 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003549}
3550
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003551psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003552{
mohammad16036df908f2018-04-02 08:34:15 -07003553 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003554}
mohammad160327010052018-07-03 13:16:15 +03003555#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003556
Gilles Peskinec5487a82018-12-03 18:08:14 +01003557psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003558 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003559{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003560 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003561 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003562
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003563 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003564 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003565
Gilles Peskinec5487a82018-12-03 18:08:14 +01003566 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003567 if( status != PSA_SUCCESS )
3568 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003569
Gilles Peskine4747d192019-04-17 15:05:45 +02003570 return( psa_set_key_policy_internal( slot, policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003571}
3572
Gilles Peskinec5487a82018-12-03 18:08:14 +01003573psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003574 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003575{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003576 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003577 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003578
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003580 return( PSA_ERROR_INVALID_ARGUMENT );
3581
Gilles Peskinec5487a82018-12-03 18:08:14 +01003582 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003583 if( status != PSA_SUCCESS )
3584 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003585
mohammad16036df908f2018-04-02 08:34:15 -07003586 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003587
3588 return( PSA_SUCCESS );
3589}
Gilles Peskine20035e32018-02-03 22:44:14 +01003590
Gilles Peskinea0655c32018-04-30 17:06:50 +02003591
3592
mohammad1603804cd712018-03-20 22:44:08 +02003593/****************************************************************/
3594/* Key Lifetime */
3595/****************************************************************/
3596
Gilles Peskine87a5e562019-04-17 12:28:25 +02003597psa_status_t psa_get_key_lifetime_from_handle( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003598 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003599{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003600 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003601 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003602
Gilles Peskinec5487a82018-12-03 18:08:14 +01003603 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003604 if( status != PSA_SUCCESS )
3605 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003606
mohammad1603804cd712018-03-20 22:44:08 +02003607 *lifetime = slot->lifetime;
3608
3609 return( PSA_SUCCESS );
3610}
3611
Gilles Peskine20035e32018-02-03 22:44:14 +01003612
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003613
mohammad16035955c982018-04-26 00:53:03 +03003614/****************************************************************/
3615/* AEAD */
3616/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003617
Gilles Peskineedf9a652018-08-17 18:11:56 +02003618typedef struct
3619{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003620 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003621 const mbedtls_cipher_info_t *cipher_info;
3622 union
3623 {
3624#if defined(MBEDTLS_CCM_C)
3625 mbedtls_ccm_context ccm;
3626#endif /* MBEDTLS_CCM_C */
3627#if defined(MBEDTLS_GCM_C)
3628 mbedtls_gcm_context gcm;
3629#endif /* MBEDTLS_GCM_C */
3630 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003631 psa_algorithm_t core_alg;
3632 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003633 uint8_t tag_length;
3634} aead_operation_t;
3635
Gilles Peskine30a9e412019-01-14 18:36:12 +01003636static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003637{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003638 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003639 {
3640#if defined(MBEDTLS_CCM_C)
3641 case PSA_ALG_CCM:
3642 mbedtls_ccm_free( &operation->ctx.ccm );
3643 break;
3644#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003645#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003646 case PSA_ALG_GCM:
3647 mbedtls_gcm_free( &operation->ctx.gcm );
3648 break;
3649#endif /* MBEDTLS_GCM_C */
3650 }
3651}
3652
3653static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003654 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003655 psa_key_usage_t usage,
3656 psa_algorithm_t alg )
3657{
3658 psa_status_t status;
3659 size_t key_bits;
3660 mbedtls_cipher_id_t cipher_id;
3661
Gilles Peskinec5487a82018-12-03 18:08:14 +01003662 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003663 if( status != PSA_SUCCESS )
3664 return( status );
3665
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003666 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003667
3668 operation->cipher_info =
3669 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3670 &cipher_id );
3671 if( operation->cipher_info == NULL )
3672 return( PSA_ERROR_NOT_SUPPORTED );
3673
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003674 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003675 {
3676#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003677 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3678 operation->core_alg = PSA_ALG_CCM;
3679 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003680 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3681 return( PSA_ERROR_INVALID_ARGUMENT );
3682 mbedtls_ccm_init( &operation->ctx.ccm );
3683 status = mbedtls_to_psa_error(
3684 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3685 operation->slot->data.raw.data,
3686 (unsigned int) key_bits ) );
3687 if( status != 0 )
3688 goto cleanup;
3689 break;
3690#endif /* MBEDTLS_CCM_C */
3691
3692#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003693 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3694 operation->core_alg = PSA_ALG_GCM;
3695 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003696 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3697 return( PSA_ERROR_INVALID_ARGUMENT );
3698 mbedtls_gcm_init( &operation->ctx.gcm );
3699 status = mbedtls_to_psa_error(
3700 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3701 operation->slot->data.raw.data,
3702 (unsigned int) key_bits ) );
3703 break;
3704#endif /* MBEDTLS_GCM_C */
3705
3706 default:
3707 return( PSA_ERROR_NOT_SUPPORTED );
3708 }
3709
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003710 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3711 {
3712 status = PSA_ERROR_INVALID_ARGUMENT;
3713 goto cleanup;
3714 }
3715 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3716 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3717 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3718 * In both cases, mbedtls_xxx will validate the tag length below. */
3719
Gilles Peskineedf9a652018-08-17 18:11:56 +02003720 return( PSA_SUCCESS );
3721
3722cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003723 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003724 return( status );
3725}
3726
Gilles Peskinec5487a82018-12-03 18:08:14 +01003727psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003728 psa_algorithm_t alg,
3729 const uint8_t *nonce,
3730 size_t nonce_length,
3731 const uint8_t *additional_data,
3732 size_t additional_data_length,
3733 const uint8_t *plaintext,
3734 size_t plaintext_length,
3735 uint8_t *ciphertext,
3736 size_t ciphertext_size,
3737 size_t *ciphertext_length )
3738{
mohammad16035955c982018-04-26 00:53:03 +03003739 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003740 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003741 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003742
mohammad1603f08a5502018-06-03 15:05:47 +03003743 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003744
Gilles Peskinec5487a82018-12-03 18:08:14 +01003745 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003746 if( status != PSA_SUCCESS )
3747 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003748
Gilles Peskineedf9a652018-08-17 18:11:56 +02003749 /* For all currently supported modes, the tag is at the end of the
3750 * ciphertext. */
3751 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3752 {
3753 status = PSA_ERROR_BUFFER_TOO_SMALL;
3754 goto exit;
3755 }
3756 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003757
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003758#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003759 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003760 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003761 status = mbedtls_to_psa_error(
3762 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3763 MBEDTLS_GCM_ENCRYPT,
3764 plaintext_length,
3765 nonce, nonce_length,
3766 additional_data, additional_data_length,
3767 plaintext, ciphertext,
3768 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003769 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003770 else
3771#endif /* MBEDTLS_GCM_C */
3772#if defined(MBEDTLS_CCM_C)
3773 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003774 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003775 status = mbedtls_to_psa_error(
3776 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3777 plaintext_length,
3778 nonce, nonce_length,
3779 additional_data,
3780 additional_data_length,
3781 plaintext, ciphertext,
3782 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003783 }
mohammad16035c8845f2018-05-09 05:40:09 -07003784 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003785#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003786 {
mohammad1603554faad2018-06-03 15:07:38 +03003787 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003788 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003789
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3791 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003792
Gilles Peskineedf9a652018-08-17 18:11:56 +02003793exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003794 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003795 if( status == PSA_SUCCESS )
3796 *ciphertext_length = plaintext_length + operation.tag_length;
3797 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003798}
3799
Gilles Peskineee652a32018-06-01 19:23:52 +02003800/* Locate the tag in a ciphertext buffer containing the encrypted data
3801 * followed by the tag. Return the length of the part preceding the tag in
3802 * *plaintext_length. This is the size of the plaintext in modes where
3803 * the encrypted data has the same size as the plaintext, such as
3804 * CCM and GCM. */
3805static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3806 const uint8_t *ciphertext,
3807 size_t ciphertext_length,
3808 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003809 const uint8_t **p_tag )
3810{
3811 size_t payload_length;
3812 if( tag_length > ciphertext_length )
3813 return( PSA_ERROR_INVALID_ARGUMENT );
3814 payload_length = ciphertext_length - tag_length;
3815 if( payload_length > plaintext_size )
3816 return( PSA_ERROR_BUFFER_TOO_SMALL );
3817 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003818 return( PSA_SUCCESS );
3819}
3820
Gilles Peskinec5487a82018-12-03 18:08:14 +01003821psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003822 psa_algorithm_t alg,
3823 const uint8_t *nonce,
3824 size_t nonce_length,
3825 const uint8_t *additional_data,
3826 size_t additional_data_length,
3827 const uint8_t *ciphertext,
3828 size_t ciphertext_length,
3829 uint8_t *plaintext,
3830 size_t plaintext_size,
3831 size_t *plaintext_length )
3832{
mohammad16035955c982018-04-26 00:53:03 +03003833 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003834 aead_operation_t operation;
3835 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003836
Gilles Peskineee652a32018-06-01 19:23:52 +02003837 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003838
Gilles Peskinec5487a82018-12-03 18:08:14 +01003839 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003840 if( status != PSA_SUCCESS )
3841 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003842
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003843#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003844 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003845 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003846 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003847 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003848 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003849 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003850 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003851
Gilles Peskineedf9a652018-08-17 18:11:56 +02003852 status = mbedtls_to_psa_error(
3853 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3854 ciphertext_length - operation.tag_length,
3855 nonce, nonce_length,
3856 additional_data,
3857 additional_data_length,
3858 tag, operation.tag_length,
3859 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003860 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003861 else
3862#endif /* MBEDTLS_GCM_C */
3863#if defined(MBEDTLS_CCM_C)
3864 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003865 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003866 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003867 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003868 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003869 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003870 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003871
Gilles Peskineedf9a652018-08-17 18:11:56 +02003872 status = mbedtls_to_psa_error(
3873 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3874 ciphertext_length - operation.tag_length,
3875 nonce, nonce_length,
3876 additional_data,
3877 additional_data_length,
3878 ciphertext, plaintext,
3879 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003880 }
mohammad160339574652018-06-01 04:39:53 -07003881 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003882#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003883 {
mohammad1603554faad2018-06-03 15:07:38 +03003884 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003885 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003886
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887 if( status != PSA_SUCCESS && plaintext_size != 0 )
3888 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003889
Gilles Peskineedf9a652018-08-17 18:11:56 +02003890exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003891 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003892 if( status == PSA_SUCCESS )
3893 *plaintext_length = ciphertext_length - operation.tag_length;
3894 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003895}
3896
Gilles Peskinea0655c32018-04-30 17:06:50 +02003897
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003898
Gilles Peskine20035e32018-02-03 22:44:14 +01003899/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900/* Generators */
3901/****************************************************************/
3902
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003903#define HKDF_STATE_INIT 0 /* no input yet */
3904#define HKDF_STATE_STARTED 1 /* got salt */
3905#define HKDF_STATE_KEYED 2 /* got key */
3906#define HKDF_STATE_OUTPUT 3 /* output started */
3907
Gilles Peskine969c5d62019-01-16 15:53:06 +01003908static psa_algorithm_t psa_generator_get_kdf_alg(
3909 const psa_crypto_generator_t *generator )
3910{
3911 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3912 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3913 else
3914 return( generator->alg );
3915}
3916
3917
Gilles Peskineeab56e42018-07-12 17:12:33 +02003918psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3919{
3920 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003921 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3922 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003923 {
3924 /* The object has (apparently) been initialized but it is not
3925 * in use. It's ok to call abort on such an object, and there's
3926 * nothing to do. */
3927 }
3928 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003929 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003930 {
3931 if( generator->ctx.buffer.data != NULL )
3932 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003933 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003934 generator->ctx.buffer.size );
3935 mbedtls_free( generator->ctx.buffer.data );
3936 }
3937 }
3938 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003939#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003940 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941 {
3942 mbedtls_free( generator->ctx.hkdf.info );
3943 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3944 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003945 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003946 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003947 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003948 {
3949 if( generator->ctx.tls12_prf.key != NULL )
3950 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003951 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003952 generator->ctx.tls12_prf.key_len );
3953 mbedtls_free( generator->ctx.tls12_prf.key );
3954 }
Hanno Becker580fba12018-11-13 20:50:45 +00003955
3956 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3957 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003958 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003959 generator->ctx.tls12_prf.Ai_with_seed_len );
3960 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3961 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003962 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003963 else
3964#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003965 {
3966 status = PSA_ERROR_BAD_STATE;
3967 }
3968 memset( generator, 0, sizeof( *generator ) );
3969 return( status );
3970}
3971
Gilles Peskineeab56e42018-07-12 17:12:33 +02003972psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3973 size_t *capacity)
3974{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003975 if( generator->alg == 0 )
3976 {
3977 /* This is a blank generator. */
3978 return PSA_ERROR_BAD_STATE;
3979 }
3980
Gilles Peskineeab56e42018-07-12 17:12:33 +02003981 *capacity = generator->capacity;
3982 return( PSA_SUCCESS );
3983}
3984
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003985psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3986 size_t capacity )
3987{
3988 if( generator->alg == 0 )
3989 return( PSA_ERROR_BAD_STATE );
3990 if( capacity > generator->capacity )
3991 return( PSA_ERROR_INVALID_ARGUMENT );
3992 generator->capacity = capacity;
3993 return( PSA_SUCCESS );
3994}
3995
Gilles Peskinebef7f142018-07-12 17:22:21 +02003996#if defined(MBEDTLS_MD_C)
3997/* Read some bytes from an HKDF-based generator. This performs a chunk
3998 * of the expand phase of the HKDF algorithm. */
3999static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
4000 psa_algorithm_t hash_alg,
4001 uint8_t *output,
4002 size_t output_length )
4003{
4004 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4005 psa_status_t status;
4006
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004007 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4008 return( PSA_ERROR_BAD_STATE );
4009 hkdf->state = HKDF_STATE_OUTPUT;
4010
Gilles Peskinebef7f142018-07-12 17:22:21 +02004011 while( output_length != 0 )
4012 {
4013 /* Copy what remains of the current block */
4014 uint8_t n = hash_length - hkdf->offset_in_block;
4015 if( n > output_length )
4016 n = (uint8_t) output_length;
4017 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4018 output += n;
4019 output_length -= n;
4020 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004021 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004022 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004023 /* We can't be wanting more output after block 0xff, otherwise
4024 * the capacity check in psa_generator_read() would have
4025 * prevented this call. It could happen only if the generator
4026 * object was corrupted or if this function is called directly
4027 * inside the library. */
4028 if( hkdf->block_number == 0xff )
4029 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004030
4031 /* We need a new block */
4032 ++hkdf->block_number;
4033 hkdf->offset_in_block = 0;
4034 status = psa_hmac_setup_internal( &hkdf->hmac,
4035 hkdf->prk, hash_length,
4036 hash_alg );
4037 if( status != PSA_SUCCESS )
4038 return( status );
4039 if( hkdf->block_number != 1 )
4040 {
4041 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4042 hkdf->output_block,
4043 hash_length );
4044 if( status != PSA_SUCCESS )
4045 return( status );
4046 }
4047 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4048 hkdf->info,
4049 hkdf->info_length );
4050 if( status != PSA_SUCCESS )
4051 return( status );
4052 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4053 &hkdf->block_number, 1 );
4054 if( status != PSA_SUCCESS )
4055 return( status );
4056 status = psa_hmac_finish_internal( &hkdf->hmac,
4057 hkdf->output_block,
4058 sizeof( hkdf->output_block ) );
4059 if( status != PSA_SUCCESS )
4060 return( status );
4061 }
4062
4063 return( PSA_SUCCESS );
4064}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004065
4066static psa_status_t psa_generator_tls12_prf_generate_next_block(
4067 psa_tls12_prf_generator_t *tls12_prf,
4068 psa_algorithm_t alg )
4069{
4070 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4071 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4072 psa_hmac_internal_data hmac;
4073 psa_status_t status, cleanup_status;
4074
Hanno Becker3b339e22018-11-13 20:56:14 +00004075 unsigned char *Ai;
4076 size_t Ai_len;
4077
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004078 /* We can't be wanting more output after block 0xff, otherwise
4079 * the capacity check in psa_generator_read() would have
4080 * prevented this call. It could happen only if the generator
4081 * object was corrupted or if this function is called directly
4082 * inside the library. */
4083 if( tls12_prf->block_number == 0xff )
4084 return( PSA_ERROR_BAD_STATE );
4085
4086 /* We need a new block */
4087 ++tls12_prf->block_number;
4088 tls12_prf->offset_in_block = 0;
4089
4090 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4091 *
4092 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4093 *
4094 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4095 * HMAC_hash(secret, A(2) + seed) +
4096 * HMAC_hash(secret, A(3) + seed) + ...
4097 *
4098 * A(0) = seed
4099 * A(i) = HMAC_hash( secret, A(i-1) )
4100 *
4101 * The `psa_tls12_prf_generator` structures saves the block
4102 * `HMAC_hash(secret, A(i) + seed)` from which the output
4103 * is currently extracted as `output_block`, while
4104 * `A(i) + seed` is stored in `Ai_with_seed`.
4105 *
4106 * Generating a new block means recalculating `Ai_with_seed`
4107 * from the A(i)-part of it, and afterwards recalculating
4108 * `output_block`.
4109 *
4110 * A(0) is computed at setup time.
4111 *
4112 */
4113
4114 psa_hmac_init_internal( &hmac );
4115
4116 /* We must distinguish the calculation of A(1) from those
4117 * of A(2) and higher, because A(0)=seed has a different
4118 * length than the other A(i). */
4119 if( tls12_prf->block_number == 1 )
4120 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004121 Ai = tls12_prf->Ai_with_seed + hash_length;
4122 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004123 }
4124 else
4125 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004126 Ai = tls12_prf->Ai_with_seed;
4127 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004128 }
4129
Hanno Becker3b339e22018-11-13 20:56:14 +00004130 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4131 status = psa_hmac_setup_internal( &hmac,
4132 tls12_prf->key,
4133 tls12_prf->key_len,
4134 hash_alg );
4135 if( status != PSA_SUCCESS )
4136 goto cleanup;
4137
4138 status = psa_hash_update( &hmac.hash_ctx,
4139 Ai, Ai_len );
4140 if( status != PSA_SUCCESS )
4141 goto cleanup;
4142
4143 status = psa_hmac_finish_internal( &hmac,
4144 tls12_prf->Ai_with_seed,
4145 hash_length );
4146 if( status != PSA_SUCCESS )
4147 goto cleanup;
4148
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004149 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4150 status = psa_hmac_setup_internal( &hmac,
4151 tls12_prf->key,
4152 tls12_prf->key_len,
4153 hash_alg );
4154 if( status != PSA_SUCCESS )
4155 goto cleanup;
4156
4157 status = psa_hash_update( &hmac.hash_ctx,
4158 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004159 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004160 if( status != PSA_SUCCESS )
4161 goto cleanup;
4162
4163 status = psa_hmac_finish_internal( &hmac,
4164 tls12_prf->output_block,
4165 hash_length );
4166 if( status != PSA_SUCCESS )
4167 goto cleanup;
4168
4169cleanup:
4170
4171 cleanup_status = psa_hmac_abort_internal( &hmac );
4172 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4173 status = cleanup_status;
4174
4175 return( status );
4176}
4177
4178/* Read some bytes from an TLS-1.2-PRF-based generator.
4179 * See Section 5 of RFC 5246. */
4180static psa_status_t psa_generator_tls12_prf_read(
4181 psa_tls12_prf_generator_t *tls12_prf,
4182 psa_algorithm_t alg,
4183 uint8_t *output,
4184 size_t output_length )
4185{
4186 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4187 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4188 psa_status_t status;
4189
4190 while( output_length != 0 )
4191 {
4192 /* Copy what remains of the current block */
4193 uint8_t n = hash_length - tls12_prf->offset_in_block;
4194
4195 /* Check if we have fully processed the current block. */
4196 if( n == 0 )
4197 {
4198 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
4199 alg );
4200 if( status != PSA_SUCCESS )
4201 return( status );
4202
4203 continue;
4204 }
4205
4206 if( n > output_length )
4207 n = (uint8_t) output_length;
4208 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4209 n );
4210 output += n;
4211 output_length -= n;
4212 tls12_prf->offset_in_block += n;
4213 }
4214
4215 return( PSA_SUCCESS );
4216}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004217#endif /* MBEDTLS_MD_C */
4218
Gilles Peskineeab56e42018-07-12 17:12:33 +02004219psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
4220 uint8_t *output,
4221 size_t output_length )
4222{
4223 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004224 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004225
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004226 if( generator->alg == 0 )
4227 {
4228 /* This is a blank generator. */
4229 return PSA_ERROR_BAD_STATE;
4230 }
4231
Gilles Peskineeab56e42018-07-12 17:12:33 +02004232 if( output_length > generator->capacity )
4233 {
4234 generator->capacity = 0;
4235 /* Go through the error path to wipe all confidential data now
4236 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004237 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004238 goto exit;
4239 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004240 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004241 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004242 /* Edge case: this is a finished generator, and 0 bytes
4243 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004244 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4245 * INSUFFICIENT_CAPACITY, which is right for a finished
4246 * generator, for consistency with the case when
4247 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004248 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004249 }
4250 generator->capacity -= output_length;
4251
Gilles Peskine969c5d62019-01-16 15:53:06 +01004252 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02004253 {
Gilles Peskine211a4362018-10-25 22:22:31 +02004254 /* Initially, the capacity of a selection generator is always
4255 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
4256 * abbreviated in this comment as `size`. When the remaining
4257 * capacity is `c`, the next bytes to serve start `c` bytes
4258 * from the end of the buffer, i.e. `size - c` from the
4259 * beginning of the buffer. Since `generator->capacity` was just
4260 * decremented above, we need to serve the bytes from
4261 * `size - generator->capacity - output_length` to
4262 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02004263 size_t offset =
4264 generator->ctx.buffer.size - generator->capacity - output_length;
4265 memcpy( output, generator->ctx.buffer.data + offset, output_length );
4266 status = PSA_SUCCESS;
4267 }
4268 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004269#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004270 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004271 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004272 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004273 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
4274 output, output_length );
4275 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004276 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4277 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004278 {
4279 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004280 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004281 output_length );
4282 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004283 else
4284#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004285 {
4286 return( PSA_ERROR_BAD_STATE );
4287 }
4288
4289exit:
4290 if( status != PSA_SUCCESS )
4291 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004292 /* Preserve the algorithm upon errors, but clear all sensitive state.
4293 * This allows us to differentiate between exhausted generators and
4294 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
4295 * generators. */
4296 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004297 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004298 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299 memset( output, '!', output_length );
4300 }
4301 return( status );
4302}
4303
Gilles Peskine08542d82018-07-19 17:05:42 +02004304#if defined(MBEDTLS_DES_C)
4305static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4306{
4307 if( data_size >= 8 )
4308 mbedtls_des_key_set_parity( data );
4309 if( data_size >= 16 )
4310 mbedtls_des_key_set_parity( data + 8 );
4311 if( data_size >= 24 )
4312 mbedtls_des_key_set_parity( data + 16 );
4313}
4314#endif /* MBEDTLS_DES_C */
4315
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004316static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004317 psa_key_slot_t *slot,
4318 size_t bits,
4319 psa_crypto_generator_t *generator )
4320{
4321 uint8_t *data = NULL;
4322 size_t bytes = PSA_BITS_TO_BYTES( bits );
4323 psa_status_t status;
4324
4325 if( ! key_type_is_raw_bytes( slot->type ) )
4326 return( PSA_ERROR_INVALID_ARGUMENT );
4327 if( bits % 8 != 0 )
4328 return( PSA_ERROR_INVALID_ARGUMENT );
4329 data = mbedtls_calloc( 1, bytes );
4330 if( data == NULL )
4331 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4332
4333 status = psa_generator_read( generator, data, bytes );
4334 if( status != PSA_SUCCESS )
4335 goto exit;
4336#if defined(MBEDTLS_DES_C)
4337 if( slot->type == PSA_KEY_TYPE_DES )
4338 psa_des_set_key_parity( data, bytes );
4339#endif /* MBEDTLS_DES_C */
4340 status = psa_import_key_into_slot( slot, data, bytes );
4341
4342exit:
4343 mbedtls_free( data );
4344 return( status );
4345}
4346
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004347psa_status_t psa_generate_derived_key( const psa_key_attributes_t *attributes,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004348 psa_key_handle_t *handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004349 psa_crypto_generator_t *generator )
4350{
4351 psa_status_t status;
4352 psa_key_slot_t *slot = NULL;
4353 status = psa_start_key_creation( attributes, handle, &slot );
4354 if( status == PSA_SUCCESS )
4355 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004356 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004357 attributes->bits,
4358 generator );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004359 }
4360 if( status == PSA_SUCCESS )
4361 status = psa_finish_key_creation( slot );
4362 if( status != PSA_SUCCESS )
4363 {
4364 psa_fail_key_creation( slot );
4365 *handle = 0;
4366 }
4367 return( status );
4368}
4369
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004370psa_status_t psa_generate_derived_key_to_handle( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004371 psa_key_type_t type,
4372 size_t bits,
4373 psa_crypto_generator_t *generator )
4374{
4375 uint8_t *data = NULL;
4376 size_t bytes = PSA_BITS_TO_BYTES( bits );
4377 psa_status_t status;
4378
4379 if( ! key_type_is_raw_bytes( type ) )
4380 return( PSA_ERROR_INVALID_ARGUMENT );
4381 if( bits % 8 != 0 )
4382 return( PSA_ERROR_INVALID_ARGUMENT );
4383 data = mbedtls_calloc( 1, bytes );
4384 if( data == NULL )
4385 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4386
4387 status = psa_generator_read( generator, data, bytes );
4388 if( status != PSA_SUCCESS )
4389 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004390#if defined(MBEDTLS_DES_C)
4391 if( type == PSA_KEY_TYPE_DES )
4392 psa_des_set_key_parity( data, bytes );
4393#endif /* MBEDTLS_DES_C */
Gilles Peskine87a5e562019-04-17 12:28:25 +02004394 status = psa_import_key_to_handle( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004395
4396exit:
4397 mbedtls_free( data );
4398 return( status );
4399}
4400
4401
4402
4403/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004404/* Key derivation */
4405/****************************************************************/
4406
Gilles Peskinea05219c2018-11-16 16:02:56 +01004407#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004408/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004409 * of the HKDF algorithm.
4410 *
4411 * Note that if this function fails, you must call psa_generator_abort()
4412 * to potentially free embedded data structures and wipe confidential data.
4413 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004414static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004415 const uint8_t *secret,
4416 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004417 psa_algorithm_t hash_alg,
4418 const uint8_t *salt,
4419 size_t salt_length,
4420 const uint8_t *label,
4421 size_t label_length )
4422{
4423 psa_status_t status;
4424 status = psa_hmac_setup_internal( &hkdf->hmac,
4425 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004426 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004427 if( status != PSA_SUCCESS )
4428 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004429 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430 if( status != PSA_SUCCESS )
4431 return( status );
4432 status = psa_hmac_finish_internal( &hkdf->hmac,
4433 hkdf->prk,
4434 sizeof( hkdf->prk ) );
4435 if( status != PSA_SUCCESS )
4436 return( status );
4437 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4438 hkdf->block_number = 0;
4439 hkdf->info_length = label_length;
4440 if( label_length != 0 )
4441 {
4442 hkdf->info = mbedtls_calloc( 1, label_length );
4443 if( hkdf->info == NULL )
4444 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4445 memcpy( hkdf->info, label, label_length );
4446 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004447 hkdf->state = HKDF_STATE_KEYED;
4448 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449 return( PSA_SUCCESS );
4450}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004451#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004452
Gilles Peskinea05219c2018-11-16 16:02:56 +01004453#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004454/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4455 *
4456 * Note that if this function fails, you must call psa_generator_abort()
4457 * to potentially free embedded data structures and wipe confidential data.
4458 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004459static psa_status_t psa_generator_tls12_prf_setup(
4460 psa_tls12_prf_generator_t *tls12_prf,
4461 const unsigned char *key,
4462 size_t key_len,
4463 psa_algorithm_t hash_alg,
4464 const uint8_t *salt,
4465 size_t salt_length,
4466 const uint8_t *label,
4467 size_t label_length )
4468{
4469 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004470 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4471 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004472
4473 tls12_prf->key = mbedtls_calloc( 1, key_len );
4474 if( tls12_prf->key == NULL )
4475 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4476 tls12_prf->key_len = key_len;
4477 memcpy( tls12_prf->key, key, key_len );
4478
Hanno Becker580fba12018-11-13 20:50:45 +00004479 overflow = ( salt_length + label_length < salt_length ) ||
4480 ( salt_length + label_length + hash_length < hash_length );
4481 if( overflow )
4482 return( PSA_ERROR_INVALID_ARGUMENT );
4483
4484 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4485 if( tls12_prf->Ai_with_seed == NULL )
4486 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4487 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4488
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004489 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4490 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004491 if( label_length != 0 )
4492 {
4493 memcpy( tls12_prf->Ai_with_seed + hash_length,
4494 label, label_length );
4495 }
4496
4497 if( salt_length != 0 )
4498 {
4499 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4500 salt, salt_length );
4501 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004502
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004503 /* The first block gets generated when
4504 * psa_generator_read() is called. */
4505 tls12_prf->block_number = 0;
4506 tls12_prf->offset_in_block = hash_length;
4507
4508 return( PSA_SUCCESS );
4509}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004510
4511/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4512static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4513 psa_tls12_prf_generator_t *tls12_prf,
4514 const unsigned char *psk,
4515 size_t psk_len,
4516 psa_algorithm_t hash_alg,
4517 const uint8_t *salt,
4518 size_t salt_length,
4519 const uint8_t *label,
4520 size_t label_length )
4521{
4522 psa_status_t status;
4523 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4524
4525 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4526 return( PSA_ERROR_INVALID_ARGUMENT );
4527
4528 /* Quoting RFC 4279, Section 2:
4529 *
4530 * The premaster secret is formed as follows: if the PSK is N octets
4531 * long, concatenate a uint16 with the value N, N zero octets, a second
4532 * uint16 with the value N, and the PSK itself.
4533 */
4534
4535 pms[0] = ( psk_len >> 8 ) & 0xff;
4536 pms[1] = ( psk_len >> 0 ) & 0xff;
4537 memset( pms + 2, 0, psk_len );
4538 pms[2 + psk_len + 0] = pms[0];
4539 pms[2 + psk_len + 1] = pms[1];
4540 memcpy( pms + 4 + psk_len, psk, psk_len );
4541
4542 status = psa_generator_tls12_prf_setup( tls12_prf,
4543 pms, 4 + 2 * psk_len,
4544 hash_alg,
4545 salt, salt_length,
4546 label, label_length );
4547
Gilles Peskine3f108122018-12-07 18:14:53 +01004548 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004549 return( status );
4550}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004551#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004552
Gilles Peskine346797d2018-11-16 16:05:06 +01004553/* Note that if this function fails, you must call psa_generator_abort()
4554 * to potentially free embedded data structures and wipe confidential data.
4555 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004556static psa_status_t psa_key_derivation_internal(
4557 psa_crypto_generator_t *generator,
4558 const uint8_t *secret, size_t secret_length,
4559 psa_algorithm_t alg,
4560 const uint8_t *salt, size_t salt_length,
4561 const uint8_t *label, size_t label_length,
4562 size_t capacity )
4563{
4564 psa_status_t status;
4565 size_t max_capacity;
4566
4567 /* Set generator->alg even on failure so that abort knows what to do. */
4568 generator->alg = alg;
4569
Gilles Peskine751d9652018-09-18 12:05:44 +02004570 if( alg == PSA_ALG_SELECT_RAW )
4571 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004572 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004573 if( salt_length != 0 )
4574 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004575 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004576 if( label_length != 0 )
4577 return( PSA_ERROR_INVALID_ARGUMENT );
4578 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4579 if( generator->ctx.buffer.data == NULL )
4580 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4581 memcpy( generator->ctx.buffer.data, secret, secret_length );
4582 generator->ctx.buffer.size = secret_length;
4583 max_capacity = secret_length;
4584 status = PSA_SUCCESS;
4585 }
4586 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004587#if defined(MBEDTLS_MD_C)
4588 if( PSA_ALG_IS_HKDF( alg ) )
4589 {
4590 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4591 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4592 if( hash_size == 0 )
4593 return( PSA_ERROR_NOT_SUPPORTED );
4594 max_capacity = 255 * hash_size;
4595 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4596 secret, secret_length,
4597 hash_alg,
4598 salt, salt_length,
4599 label, label_length );
4600 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004601 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4602 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4603 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004604 {
4605 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4606 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4607
4608 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4609 if( hash_alg != PSA_ALG_SHA_256 &&
4610 hash_alg != PSA_ALG_SHA_384 )
4611 {
4612 return( PSA_ERROR_NOT_SUPPORTED );
4613 }
4614
4615 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004616
4617 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4618 {
4619 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4620 secret, secret_length,
4621 hash_alg, salt, salt_length,
4622 label, label_length );
4623 }
4624 else
4625 {
4626 status = psa_generator_tls12_psk_to_ms_setup(
4627 &generator->ctx.tls12_prf,
4628 secret, secret_length,
4629 hash_alg, salt, salt_length,
4630 label, label_length );
4631 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004632 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004633 else
4634#endif
4635 {
4636 return( PSA_ERROR_NOT_SUPPORTED );
4637 }
4638
4639 if( status != PSA_SUCCESS )
4640 return( status );
4641
4642 if( capacity <= max_capacity )
4643 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004644 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4645 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004646 else
4647 return( PSA_ERROR_INVALID_ARGUMENT );
4648
4649 return( PSA_SUCCESS );
4650}
4651
Gilles Peskineea0fb492018-07-12 17:17:20 +02004652psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004653 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004654 psa_algorithm_t alg,
4655 const uint8_t *salt,
4656 size_t salt_length,
4657 const uint8_t *label,
4658 size_t label_length,
4659 size_t capacity )
4660{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004661 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004662 psa_status_t status;
4663
4664 if( generator->alg != 0 )
4665 return( PSA_ERROR_BAD_STATE );
4666
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004667 /* Make sure that alg is a key derivation algorithm. This prevents
4668 * key selection algorithms, which psa_key_derivation_internal
4669 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004670 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4671 return( PSA_ERROR_INVALID_ARGUMENT );
4672
Gilles Peskinec5487a82018-12-03 18:08:14 +01004673 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004674 if( status != PSA_SUCCESS )
4675 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004676
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004677 if( slot->type != PSA_KEY_TYPE_DERIVE )
4678 return( PSA_ERROR_INVALID_ARGUMENT );
4679
4680 status = psa_key_derivation_internal( generator,
4681 slot->data.raw.data,
4682 slot->data.raw.bytes,
4683 alg,
4684 salt, salt_length,
4685 label, label_length,
4686 capacity );
4687 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004688 psa_generator_abort( generator );
4689 return( status );
4690}
4691
Gilles Peskine969c5d62019-01-16 15:53:06 +01004692static psa_status_t psa_key_derivation_setup_kdf(
4693 psa_crypto_generator_t *generator,
4694 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004695{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004696 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004697#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004698 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4699 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4700 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004701 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004702 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004703 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4704 if( hash_size == 0 )
4705 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004706 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4707 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004708 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004709 {
4710 return( PSA_ERROR_NOT_SUPPORTED );
4711 }
4712 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004713 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004714 }
4715#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004716 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004717 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004718}
4719
4720psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4721 psa_algorithm_t alg )
4722{
4723 psa_status_t status;
4724
4725 if( generator->alg != 0 )
4726 return( PSA_ERROR_BAD_STATE );
4727
Gilles Peskine6843c292019-01-18 16:44:49 +01004728 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4729 return( PSA_ERROR_INVALID_ARGUMENT );
4730 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004731 {
4732 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004733 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004734 }
4735 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4736 {
4737 status = psa_key_derivation_setup_kdf( generator, alg );
4738 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004739 else
4740 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004741
4742 if( status == PSA_SUCCESS )
4743 generator->alg = alg;
4744 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004745}
4746
4747#if defined(MBEDTLS_MD_C)
4748static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4749 psa_algorithm_t hash_alg,
4750 psa_key_derivation_step_t step,
4751 const uint8_t *data,
4752 size_t data_length )
4753{
4754 psa_status_t status;
4755 switch( step )
4756 {
4757 case PSA_KDF_STEP_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004758 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004759 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004760 status = psa_hmac_setup_internal( &hkdf->hmac,
4761 data, data_length,
4762 hash_alg );
4763 if( status != PSA_SUCCESS )
4764 return( status );
4765 hkdf->state = HKDF_STATE_STARTED;
4766 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767 case PSA_KDF_STEP_SECRET:
4768 /* If no salt was provided, use an empty salt. */
4769 if( hkdf->state == HKDF_STATE_INIT )
4770 {
4771 status = psa_hmac_setup_internal( &hkdf->hmac,
4772 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004773 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004774 if( status != PSA_SUCCESS )
4775 return( status );
4776 hkdf->state = HKDF_STATE_STARTED;
4777 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004778 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004779 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004780 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4781 data, data_length );
4782 if( status != PSA_SUCCESS )
4783 return( status );
4784 status = psa_hmac_finish_internal( &hkdf->hmac,
4785 hkdf->prk,
4786 sizeof( hkdf->prk ) );
4787 if( status != PSA_SUCCESS )
4788 return( status );
4789 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4790 hkdf->block_number = 0;
4791 hkdf->state = HKDF_STATE_KEYED;
4792 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004793 case PSA_KDF_STEP_INFO:
4794 if( hkdf->state == HKDF_STATE_OUTPUT )
4795 return( PSA_ERROR_BAD_STATE );
4796 if( hkdf->info_set )
4797 return( PSA_ERROR_BAD_STATE );
4798 hkdf->info_length = data_length;
4799 if( data_length != 0 )
4800 {
4801 hkdf->info = mbedtls_calloc( 1, data_length );
4802 if( hkdf->info == NULL )
4803 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4804 memcpy( hkdf->info, data, data_length );
4805 }
4806 hkdf->info_set = 1;
4807 return( PSA_SUCCESS );
4808 default:
4809 return( PSA_ERROR_INVALID_ARGUMENT );
4810 }
4811}
4812#endif /* MBEDTLS_MD_C */
4813
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004814static psa_status_t psa_key_derivation_input_raw(
4815 psa_crypto_generator_t *generator,
4816 psa_key_derivation_step_t step,
4817 const uint8_t *data,
4818 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819{
4820 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 if( kdf_alg == PSA_ALG_SELECT_RAW )
4824 {
4825 if( generator->capacity != 0 )
4826 return( PSA_ERROR_INVALID_ARGUMENT );
4827 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4828 if( generator->ctx.buffer.data == NULL )
4829 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4830 memcpy( generator->ctx.buffer.data, data, data_length );
4831 generator->ctx.buffer.size = data_length;
4832 generator->capacity = data_length;
4833 status = PSA_SUCCESS;
4834 }
4835 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004837 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004838 {
4839 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004840 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004841 step, data, data_length );
4842 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004843 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004844#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004845#if defined(MBEDTLS_MD_C)
4846 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004847 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4848 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004849 {
Gilles Peskinec88644d2019-04-12 15:03:38 +02004850 // To do: implement this
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004851 status = PSA_ERROR_NOT_SUPPORTED;
4852 }
4853 else
4854#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004855 {
4856 /* This can't happen unless the generator object was not initialized */
4857 return( PSA_ERROR_BAD_STATE );
4858 }
4859
4860 if( status != PSA_SUCCESS )
4861 psa_generator_abort( generator );
4862 return( status );
4863}
4864
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004865psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4866 psa_key_derivation_step_t step,
4867 const uint8_t *data,
4868 size_t data_length )
4869{
4870 switch( step )
4871 {
4872 case PSA_KDF_STEP_LABEL:
4873 case PSA_KDF_STEP_SALT:
4874 case PSA_KDF_STEP_INFO:
4875 return( psa_key_derivation_input_raw( generator, step,
4876 data, data_length ) );
4877 default:
4878 return( PSA_ERROR_INVALID_ARGUMENT );
4879 }
4880}
4881
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004882psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4883 psa_key_derivation_step_t step,
4884 psa_key_handle_t handle )
4885{
4886 psa_key_slot_t *slot;
4887 psa_status_t status;
4888 status = psa_get_key_from_slot( handle, &slot,
4889 PSA_KEY_USAGE_DERIVE,
4890 generator->alg );
4891 if( status != PSA_SUCCESS )
4892 return( status );
4893 if( slot->type != PSA_KEY_TYPE_DERIVE )
4894 return( PSA_ERROR_INVALID_ARGUMENT );
4895 /* Don't allow a key to be used as an input that is usually public.
4896 * This is debatable. It's ok from a cryptographic perspective to
4897 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004898 * the material should be dedicated to a particular input step,
4899 * otherwise this may allow the key to be used in an unintended way
4900 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 if( step != PSA_KDF_STEP_SECRET )
4902 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004903 return( psa_key_derivation_input_raw( generator,
4904 step,
4905 slot->data.raw.data,
4906 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907}
4908
Gilles Peskineea0fb492018-07-12 17:17:20 +02004909
4910
4911/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004912/* Key agreement */
4913/****************************************************************/
4914
Gilles Peskinea05219c2018-11-16 16:02:56 +01004915#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004916static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4917 size_t peer_key_length,
4918 const mbedtls_ecp_keypair *our_key,
4919 uint8_t *shared_secret,
4920 size_t shared_secret_size,
4921 size_t *shared_secret_length )
4922{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004923 mbedtls_ecp_keypair *their_key = NULL;
4924 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004925 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004926 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004927
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004928 status = psa_import_ec_public_key(
4929 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4930 peer_key, peer_key_length,
4931 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004932 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004933 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004934
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004935 status = mbedtls_to_psa_error(
4936 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4937 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004938 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004939 status = mbedtls_to_psa_error(
4940 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4941 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004942 goto exit;
4943
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004944 status = mbedtls_to_psa_error(
4945 mbedtls_ecdh_calc_secret( &ecdh,
4946 shared_secret_length,
4947 shared_secret, shared_secret_size,
4948 mbedtls_ctr_drbg_random,
4949 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004950
4951exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004952 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004953 mbedtls_ecp_keypair_free( their_key );
4954 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004955 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004956}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004957#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004958
Gilles Peskine01d718c2018-09-18 12:01:02 +02004959#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4960
Gilles Peskine0216fe12019-04-11 21:23:21 +02004961static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4962 psa_key_slot_t *private_key,
4963 const uint8_t *peer_key,
4964 size_t peer_key_length,
4965 uint8_t *shared_secret,
4966 size_t shared_secret_size,
4967 size_t *shared_secret_length )
4968{
4969 switch( alg )
4970 {
4971#if defined(MBEDTLS_ECDH_C)
4972 case PSA_ALG_ECDH:
4973 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4974 return( PSA_ERROR_INVALID_ARGUMENT );
4975 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
4976 private_key->data.ecp,
4977 shared_secret, shared_secret_size,
4978 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004979#endif /* MBEDTLS_ECDH_C */
4980 default:
4981 (void) private_key;
4982 (void) peer_key;
4983 (void) peer_key_length;
4984 (void) shared_secret;
4985 (void) shared_secret_size;
4986 (void) shared_secret_length;
4987 return( PSA_ERROR_NOT_SUPPORTED );
4988 }
4989}
4990
Gilles Peskine346797d2018-11-16 16:05:06 +01004991/* Note that if this function fails, you must call psa_generator_abort()
4992 * to potentially free embedded data structures and wipe confidential data.
4993 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004994static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004996 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004997 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004998 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004999{
5000 psa_status_t status;
5001 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5002 size_t shared_secret_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005003 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005004
5005 /* Step 1: run the secret agreement algorithm to generate the shared
5006 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005007 status = psa_key_agreement_raw_internal( ka_alg,
5008 private_key,
5009 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005010 shared_secret,
5011 sizeof( shared_secret ),
5012 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005013 if( status != PSA_SUCCESS )
5014 goto exit;
5015
5016 /* Step 2: set up the key derivation to generate key material from
5017 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018 status = psa_key_derivation_input_raw( generator, step,
5019 shared_secret, shared_secret_length );
5020
Gilles Peskine01d718c2018-09-18 12:01:02 +02005021exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005022 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005023 return( status );
5024}
5025
5026psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005027 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01005028 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005029 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005030 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005031{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005032 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005033 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005035 return( PSA_ERROR_INVALID_ARGUMENT );
5036 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005037 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005038 if( status != PSA_SUCCESS )
5039 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005040 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005041 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005043 if( status != PSA_SUCCESS )
5044 psa_generator_abort( generator );
5045 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005046}
5047
Gilles Peskine0216fe12019-04-11 21:23:21 +02005048psa_status_t psa_key_agreement_raw_shared_secret( psa_algorithm_t alg,
5049 psa_key_handle_t private_key,
5050 const uint8_t *peer_key,
5051 size_t peer_key_length,
5052 uint8_t *output,
5053 size_t output_size,
5054 size_t *output_length )
5055{
5056 psa_key_slot_t *slot;
5057 psa_status_t status;
5058
5059 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5060 {
5061 status = PSA_ERROR_INVALID_ARGUMENT;
5062 goto exit;
5063 }
5064 status = psa_get_key_from_slot( private_key, &slot,
5065 PSA_KEY_USAGE_DERIVE, alg );
5066 if( status != PSA_SUCCESS )
5067 goto exit;
5068
5069 status = psa_key_agreement_raw_internal( alg, slot,
5070 peer_key, peer_key_length,
5071 output, output_size,
5072 output_length );
5073
5074exit:
5075 if( status != PSA_SUCCESS )
5076 {
5077 /* If an error happens and is not handled properly, the output
5078 * may be used as a key to protect sensitive data. Arrange for such
5079 * a key to be random, which is likely to result in decryption or
5080 * verification errors. This is better than filling the buffer with
5081 * some constant data such as zeros, which would result in the data
5082 * being protected with a reproducible, easily knowable key.
5083 */
5084 psa_generate_random( output, output_size );
5085 *output_length = output_size;
5086 }
5087 return( status );
5088}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005089
5090
5091/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005093/****************************************************************/
5094
5095psa_status_t psa_generate_random( uint8_t *output,
5096 size_t output_size )
5097{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005098 int ret;
5099 GUARD_MODULE_INITIALIZED;
5100
5101 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005102 return( mbedtls_to_psa_error( ret ) );
5103}
5104
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005105#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5106#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005107
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005108psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5109 size_t seed_size )
5110{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005111 if( global_data.initialized )
5112 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005113
5114 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5115 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5116 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5117 return( PSA_ERROR_INVALID_ARGUMENT );
5118
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005119 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005120}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005121#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005122
Gilles Peskinee56e8782019-04-26 17:34:02 +02005123#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5124static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5125 size_t domain_parameters_size,
5126 int *exponent )
5127{
5128 size_t i;
5129 uint32_t acc = 0;
5130
5131 if( domain_parameters_size == 0 )
5132 {
5133 *exponent = 65537;
5134 return( PSA_SUCCESS );
5135 }
5136
5137 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5138 * support values that fit in a 32-bit integer, which is larger than
5139 * int on just about every platform anyway. */
5140 if( domain_parameters_size > sizeof( acc ) )
5141 return( PSA_ERROR_NOT_SUPPORTED );
5142 for( i = 0; i < domain_parameters_size; i++ )
5143 acc = ( acc << 8 ) | domain_parameters[i];
5144 if( acc > INT_MAX )
5145 return( PSA_ERROR_NOT_SUPPORTED );
5146 *exponent = acc;
5147 return( PSA_SUCCESS );
5148}
5149#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5150
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005151static psa_status_t psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005152 psa_key_slot_t *slot, size_t bits,
5153 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005154{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005155 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005156
Gilles Peskinee56e8782019-04-26 17:34:02 +02005157 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005158 return( PSA_ERROR_INVALID_ARGUMENT );
5159
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005160 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005161 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005162 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005163 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005164 if( status != PSA_SUCCESS )
5165 return( status );
5166 status = psa_generate_random( slot->data.raw.data,
5167 slot->data.raw.bytes );
5168 if( status != PSA_SUCCESS )
5169 {
5170 mbedtls_free( slot->data.raw.data );
5171 return( status );
5172 }
5173#if defined(MBEDTLS_DES_C)
5174 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005175 psa_des_set_key_parity( slot->data.raw.data,
5176 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005177#endif /* MBEDTLS_DES_C */
5178 }
5179 else
5180
5181#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5182 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
5183 {
5184 mbedtls_rsa_context *rsa;
5185 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005186 int exponent;
5187 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005188 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5189 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005190 /* Accept only byte-aligned keys, for the same reasons as
5191 * in psa_import_rsa_key(). */
5192 if( bits % 8 != 0 )
5193 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005194 status = psa_read_rsa_exponent( domain_parameters,
5195 domain_parameters_size,
5196 &exponent );
5197 if( status != PSA_SUCCESS )
5198 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005199 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5200 if( rsa == NULL )
5201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5202 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5203 ret = mbedtls_rsa_gen_key( rsa,
5204 mbedtls_ctr_drbg_random,
5205 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005206 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005207 exponent );
5208 if( ret != 0 )
5209 {
5210 mbedtls_rsa_free( rsa );
5211 mbedtls_free( rsa );
5212 return( mbedtls_to_psa_error( ret ) );
5213 }
5214 slot->data.rsa = rsa;
5215 }
5216 else
5217#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5218
5219#if defined(MBEDTLS_ECP_C)
5220 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
5221 {
5222 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5223 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5224 const mbedtls_ecp_curve_info *curve_info =
5225 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5226 mbedtls_ecp_keypair *ecp;
5227 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005228 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005229 return( PSA_ERROR_NOT_SUPPORTED );
5230 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5231 return( PSA_ERROR_NOT_SUPPORTED );
5232 if( curve_info->bit_size != bits )
5233 return( PSA_ERROR_INVALID_ARGUMENT );
5234 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5235 if( ecp == NULL )
5236 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5237 mbedtls_ecp_keypair_init( ecp );
5238 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5239 mbedtls_ctr_drbg_random,
5240 &global_data.ctr_drbg );
5241 if( ret != 0 )
5242 {
5243 mbedtls_ecp_keypair_free( ecp );
5244 mbedtls_free( ecp );
5245 return( mbedtls_to_psa_error( ret ) );
5246 }
5247 slot->data.ecp = ecp;
5248 }
5249 else
5250#endif /* MBEDTLS_ECP_C */
5251
5252 return( PSA_ERROR_NOT_SUPPORTED );
5253
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005254 return( PSA_SUCCESS );
5255}
5256
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005257psa_status_t psa_generate_random_key_to_handle( psa_key_handle_t handle,
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005258 psa_key_type_t type,
5259 size_t bits,
5260 const void *extra,
5261 size_t extra_size )
5262{
5263 psa_key_slot_t *slot;
5264 psa_status_t status;
5265
Gilles Peskinee56e8782019-04-26 17:34:02 +02005266#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5267 /* The old public exponent encoding is no longer supported. */
5268 if( extra_size != 0 )
5269 return( PSA_ERROR_NOT_SUPPORTED );
5270#endif
5271
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005272 status = psa_get_empty_key_slot( handle, &slot );
5273 if( status != PSA_SUCCESS )
5274 return( status );
5275
Gilles Peskine12313cd2018-06-20 00:20:32 +02005276 slot->type = type;
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005277 status = psa_generate_random_key_internal( slot, bits, extra, extra_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005278 if( status != PSA_SUCCESS )
5279 slot->type = 0;
Darryl Green0c6575a2018-11-07 16:05:30 +00005280
5281#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
5282 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
5283 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01005284 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005285 }
5286#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
5287
5288 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02005289}
5290
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005291psa_status_t psa_generate_random_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005292 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005293{
5294 psa_status_t status;
5295 psa_key_slot_t *slot = NULL;
5296 status = psa_start_key_creation( attributes, handle, &slot );
5297 if( status == PSA_SUCCESS )
5298 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005299 status = psa_generate_random_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005300 slot, attributes->bits,
5301 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005302 }
5303 if( status == PSA_SUCCESS )
5304 status = psa_finish_key_creation( slot );
5305 if( status != PSA_SUCCESS )
5306 {
5307 psa_fail_key_creation( slot );
5308 *handle = 0;
5309 }
5310 return( status );
5311}
5312
5313
Gilles Peskine05d69892018-06-19 22:00:52 +02005314
5315/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005316/* Module setup */
5317/****************************************************************/
5318
Gilles Peskine5e769522018-11-20 21:59:56 +01005319psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5320 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5321 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5322{
5323 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5324 return( PSA_ERROR_BAD_STATE );
5325 global_data.entropy_init = entropy_init;
5326 global_data.entropy_free = entropy_free;
5327 return( PSA_SUCCESS );
5328}
5329
Gilles Peskinee59236f2018-01-27 23:32:46 +01005330void mbedtls_psa_crypto_free( void )
5331{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005332 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005333 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5334 {
5335 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005336 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005337 }
5338 /* Wipe all remaining data, including configuration.
5339 * In particular, this sets all state indicator to the value
5340 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005341 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005342}
5343
5344psa_status_t psa_crypto_init( void )
5345{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005346 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005347 const unsigned char drbg_seed[] = "PSA";
5348
Gilles Peskinec6b69072018-11-20 21:42:52 +01005349 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005350 if( global_data.initialized != 0 )
5351 return( PSA_SUCCESS );
5352
Gilles Peskine5e769522018-11-20 21:59:56 +01005353 /* Set default configuration if
5354 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5355 if( global_data.entropy_init == NULL )
5356 global_data.entropy_init = mbedtls_entropy_init;
5357 if( global_data.entropy_free == NULL )
5358 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005359
Gilles Peskinec6b69072018-11-20 21:42:52 +01005360 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005361 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005362 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005363 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005364 status = mbedtls_to_psa_error(
5365 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5366 mbedtls_entropy_func,
5367 &global_data.entropy,
5368 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5369 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005370 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005371 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005372
Gilles Peskine66fb1262018-12-10 16:29:04 +01005373 status = psa_initialize_key_slots( );
5374 if( status != PSA_SUCCESS )
5375 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005376
5377 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005378 global_data.initialized = 1;
5379
Gilles Peskinee59236f2018-01-27 23:32:46 +01005380exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005381 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005382 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005383 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005384}
5385
5386#endif /* MBEDTLS_PSA_CRYPTO_C */