blob: cfa07a6eee9c478d6a7c856ea6d71fb27fd5b4b7 [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 Amero25384a22019-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"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020063#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Netanel Gonen2bcd3122018-11-19 11:53:02 +020081#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020082#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020083#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010084
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
189 return( PSA_ERROR_NOT_SUPPORTED );
190 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
193 return( PSA_ERROR_INSUFFICIENT_MEMORY );
194 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
195 return( PSA_ERROR_INVALID_PADDING );
196 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
197 return( PSA_ERROR_BAD_STATE );
198 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
199 return( PSA_ERROR_INVALID_SIGNATURE );
200 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
201 return( PSA_ERROR_TAMPERING_DETECTED );
202 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
203 return( PSA_ERROR_HARDWARE_FAILURE );
204
205 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
206 return( PSA_ERROR_HARDWARE_FAILURE );
207
208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215
216 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
222 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
223 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225
226 case MBEDTLS_ERR_GCM_AUTH_FAILED:
227 return( PSA_ERROR_INVALID_SIGNATURE );
228 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200229 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
234 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
235 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
241 return( PSA_ERROR_INVALID_ARGUMENT );
242 case MBEDTLS_ERR_MD_ALLOC_FAILED:
243 return( PSA_ERROR_INSUFFICIENT_MEMORY );
244 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
252 return( PSA_ERROR_INVALID_ARGUMENT );
253 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
254 return( PSA_ERROR_INVALID_ARGUMENT );
255 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
256 return( PSA_ERROR_BUFFER_TOO_SMALL );
257 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
264 return( PSA_ERROR_INSUFFICIENT_MEMORY );
265
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100266 case MBEDTLS_ERR_PK_ALLOC_FAILED:
267 return( PSA_ERROR_INSUFFICIENT_MEMORY );
268 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
269 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100272 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
274 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
277 return( PSA_ERROR_NOT_SUPPORTED );
278 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
279 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
280 return( PSA_ERROR_NOT_PERMITTED );
281 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_INVALID_ALG:
284 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
285 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
288 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
290 return( PSA_ERROR_HARDWARE_FAILURE );
291
292 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
293 return( PSA_ERROR_HARDWARE_FAILURE );
294
295 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_RSA_INVALID_PADDING:
298 return( PSA_ERROR_INVALID_PADDING );
299 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
304 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
305 return( PSA_ERROR_TAMPERING_DETECTED );
306 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
307 return( PSA_ERROR_INVALID_SIGNATURE );
308 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
309 return( PSA_ERROR_BUFFER_TOO_SMALL );
310 case MBEDTLS_ERR_RSA_RNG_FAILED:
311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
312 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
318 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
319 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
322 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
323 return( PSA_ERROR_INVALID_ARGUMENT );
324 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
itayzafrir5c753392018-05-08 11:18:38 +0300327 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300328 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300329 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
331 return( PSA_ERROR_BUFFER_TOO_SMALL );
332 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
333 return( PSA_ERROR_NOT_SUPPORTED );
334 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
335 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
336 return( PSA_ERROR_INVALID_SIGNATURE );
337 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
338 return( PSA_ERROR_INSUFFICIENT_MEMORY );
339 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300341
Gilles Peskinee59236f2018-01-27 23:32:46 +0100342 default:
David Saadab4ecc272019-02-14 13:48:10 +0200343 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100344 }
345}
346
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200348
349
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350/****************************************************************/
351/* Key management */
352/****************************************************************/
353
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100354#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200355static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
356{
357 switch( grpid )
358 {
359 case MBEDTLS_ECP_DP_SECP192R1:
360 return( PSA_ECC_CURVE_SECP192R1 );
361 case MBEDTLS_ECP_DP_SECP224R1:
362 return( PSA_ECC_CURVE_SECP224R1 );
363 case MBEDTLS_ECP_DP_SECP256R1:
364 return( PSA_ECC_CURVE_SECP256R1 );
365 case MBEDTLS_ECP_DP_SECP384R1:
366 return( PSA_ECC_CURVE_SECP384R1 );
367 case MBEDTLS_ECP_DP_SECP521R1:
368 return( PSA_ECC_CURVE_SECP521R1 );
369 case MBEDTLS_ECP_DP_BP256R1:
370 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
371 case MBEDTLS_ECP_DP_BP384R1:
372 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
373 case MBEDTLS_ECP_DP_BP512R1:
374 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
375 case MBEDTLS_ECP_DP_CURVE25519:
376 return( PSA_ECC_CURVE_CURVE25519 );
377 case MBEDTLS_ECP_DP_SECP192K1:
378 return( PSA_ECC_CURVE_SECP192K1 );
379 case MBEDTLS_ECP_DP_SECP224K1:
380 return( PSA_ECC_CURVE_SECP224K1 );
381 case MBEDTLS_ECP_DP_SECP256K1:
382 return( PSA_ECC_CURVE_SECP256K1 );
383 case MBEDTLS_ECP_DP_CURVE448:
384 return( PSA_ECC_CURVE_CURVE448 );
385 default:
386 return( 0 );
387 }
388}
389
Gilles Peskine12313cd2018-06-20 00:20:32 +0200390static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
391{
392 switch( curve )
393 {
394 case PSA_ECC_CURVE_SECP192R1:
395 return( MBEDTLS_ECP_DP_SECP192R1 );
396 case PSA_ECC_CURVE_SECP224R1:
397 return( MBEDTLS_ECP_DP_SECP224R1 );
398 case PSA_ECC_CURVE_SECP256R1:
399 return( MBEDTLS_ECP_DP_SECP256R1 );
400 case PSA_ECC_CURVE_SECP384R1:
401 return( MBEDTLS_ECP_DP_SECP384R1 );
402 case PSA_ECC_CURVE_SECP521R1:
403 return( MBEDTLS_ECP_DP_SECP521R1 );
404 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 case PSA_ECC_CURVE_CURVE25519:
411 return( MBEDTLS_ECP_DP_CURVE25519 );
412 case PSA_ECC_CURVE_SECP192K1:
413 return( MBEDTLS_ECP_DP_SECP192K1 );
414 case PSA_ECC_CURVE_SECP224K1:
415 return( MBEDTLS_ECP_DP_SECP224K1 );
416 case PSA_ECC_CURVE_SECP256K1:
417 return( MBEDTLS_ECP_DP_SECP256K1 );
418 case PSA_ECC_CURVE_CURVE448:
419 return( MBEDTLS_ECP_DP_CURVE448 );
420 default:
421 return( MBEDTLS_ECP_DP_NONE );
422 }
423}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100424#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200425
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200426static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
427 size_t bits,
428 struct raw_data *raw )
429{
430 /* Check that the bit size is acceptable for the key type */
431 switch( type )
432 {
433 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200434 if( bits == 0 )
435 {
436 raw->bytes = 0;
437 raw->data = NULL;
438 return( PSA_SUCCESS );
439 }
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_MD_C)
442 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200443#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200444 case PSA_KEY_TYPE_DERIVE:
445 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446#if defined(MBEDTLS_AES_C)
447 case PSA_KEY_TYPE_AES:
448 if( bits != 128 && bits != 192 && bits != 256 )
449 return( PSA_ERROR_INVALID_ARGUMENT );
450 break;
451#endif
452#if defined(MBEDTLS_CAMELLIA_C)
453 case PSA_KEY_TYPE_CAMELLIA:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_DES_C)
459 case PSA_KEY_TYPE_DES:
460 if( bits != 64 && bits != 128 && bits != 192 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_ARC4_C)
465 case PSA_KEY_TYPE_ARC4:
466 if( bits < 8 || bits > 2048 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470 default:
471 return( PSA_ERROR_NOT_SUPPORTED );
472 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200473 if( bits % 8 != 0 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200475
476 /* Allocate memory for the key */
477 raw->bytes = PSA_BITS_TO_BYTES( bits );
478 raw->data = mbedtls_calloc( 1, raw->bytes );
479 if( raw->data == NULL )
480 {
481 raw->bytes = 0;
482 return( PSA_ERROR_INSUFFICIENT_MEMORY );
483 }
484 return( PSA_SUCCESS );
485}
486
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200487#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100488/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
489 * that are not a multiple of 8) well. For example, there is only
490 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
491 * way to return the exact bit size of a key.
492 * To keep things simple, reject non-byte-aligned key sizes. */
493static psa_status_t psa_check_rsa_key_byte_aligned(
494 const mbedtls_rsa_context *rsa )
495{
496 mbedtls_mpi n;
497 psa_status_t status;
498 mbedtls_mpi_init( &n );
499 status = mbedtls_to_psa_error(
500 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
501 if( status == PSA_SUCCESS )
502 {
503 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
504 status = PSA_ERROR_NOT_SUPPORTED;
505 }
506 mbedtls_mpi_free( &n );
507 return( status );
508}
509
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000510static psa_status_t psa_import_rsa_key( psa_key_type_t type,
511 const uint8_t *data,
512 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513 mbedtls_rsa_context **p_rsa )
514{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000515 psa_status_t status;
516 mbedtls_pk_context pk;
517 mbedtls_rsa_context *rsa;
518 size_t bits;
519
520 mbedtls_pk_init( &pk );
521
522 /* Parse the data. */
523 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
524 status = mbedtls_to_psa_error(
525 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200526 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000527 status = mbedtls_to_psa_error(
528 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
529 if( status != PSA_SUCCESS )
530 goto exit;
531
532 /* We have something that the pkparse module recognizes. If it is a
533 * valid RSA key, store it. */
534 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 status = PSA_ERROR_INVALID_ARGUMENT;
537 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000539
540 rsa = mbedtls_pk_rsa( pk );
541 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
542 * supports non-byte-aligned key sizes, but not well. For example,
543 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
544 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
545 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
546 {
547 status = PSA_ERROR_NOT_SUPPORTED;
548 goto exit;
549 }
550 status = psa_check_rsa_key_byte_aligned( rsa );
551
552exit:
553 /* Free the content of the pk object only on error. */
554 if( status != PSA_SUCCESS )
555 {
556 mbedtls_pk_free( &pk );
557 return( status );
558 }
559
560 /* On success, store the content of the object in the RSA context. */
561 *p_rsa = rsa;
562
563 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564}
565#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
566
Jaeden Ameroccdce902019-01-10 11:42:27 +0000567#if defined(MBEDTLS_ECP_C)
568
569/* Import a public key given as the uncompressed representation defined by SEC1
570 * 2.3.3 as the content of an ECPoint. */
571static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
572 const uint8_t *data,
573 size_t data_length,
574 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200575{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000576 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
577 mbedtls_ecp_keypair *ecp = NULL;
578 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
579
580 *p_ecp = NULL;
581 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
582 if( ecp == NULL )
583 return( PSA_ERROR_INSUFFICIENT_MEMORY );
584 mbedtls_ecp_keypair_init( ecp );
585
586 /* Load the group. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
589 if( status != PSA_SUCCESS )
590 goto exit;
591 /* Load the public value. */
592 status = mbedtls_to_psa_error(
593 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
594 data, data_length ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597
598 /* Check that the point is on the curve. */
599 status = mbedtls_to_psa_error(
600 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603
604 *p_ecp = ecp;
605 return( PSA_SUCCESS );
606
607exit:
608 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000610 mbedtls_ecp_keypair_free( ecp );
611 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200612 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000613 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200614}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000615#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200616
Gilles Peskinef76aa772018-10-29 19:24:33 +0100617#if defined(MBEDTLS_ECP_C)
618/* Import a private key given as a byte string which is the private value
619 * in big-endian order. */
620static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
621 const uint8_t *data,
622 size_t data_length,
623 mbedtls_ecp_keypair **p_ecp )
624{
625 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
626 mbedtls_ecp_keypair *ecp = NULL;
627 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
628
629 *p_ecp = NULL;
630 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
631 if( ecp == NULL )
632 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000633 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100634
635 /* Load the group. */
636 status = mbedtls_to_psa_error(
637 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Load the secret value. */
641 status = mbedtls_to_psa_error(
642 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Validate the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
648 if( status != PSA_SUCCESS )
649 goto exit;
650 /* Calculate the public key from the private key. */
651 status = mbedtls_to_psa_error(
652 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
653 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
654 if( status != PSA_SUCCESS )
655 goto exit;
656
657 *p_ecp = ecp;
658 return( PSA_SUCCESS );
659
660exit:
661 if( ecp != NULL )
662 {
663 mbedtls_ecp_keypair_free( ecp );
664 mbedtls_free( ecp );
665 }
666 return( status );
667}
668#endif /* defined(MBEDTLS_ECP_C) */
669
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100670/** Import key data into a slot. `slot->type` must have been set
671 * previously. This function assumes that the slot does not contain
672 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100673psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
674 const uint8_t *data,
675 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200677 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
Darryl Green940d72c2018-07-13 13:18:51 +0100679 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100681 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 if( data_length > SIZE_MAX / 8 )
683 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100684 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200685 PSA_BYTES_TO_BITS( data_length ),
686 &slot->data.raw );
687 if( status != PSA_SUCCESS )
688 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200689 if( data_length != 0 )
690 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 }
692 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100693#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100694 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100695 {
Darryl Green940d72c2018-07-13 13:18:51 +0100696 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100697 data, data_length,
698 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100699 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000700 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000702 status = psa_import_ec_public_key(
703 PSA_KEY_TYPE_GET_CURVE( slot->type ),
704 data, data_length,
705 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 }
707 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100708#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000709#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
710 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000712 status = psa_import_rsa_key( slot->type,
713 data, data_length,
714 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 }
716 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000717#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 {
719 return( PSA_ERROR_NOT_SUPPORTED );
720 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000721 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100722}
723
Darryl Green06fd18d2018-07-16 11:21:11 +0100724/* Retrieve an empty key slot (slot with no key data, but possibly
725 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100726static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100727 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100728{
729 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100730 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100731
732 *p_slot = NULL;
733
Gilles Peskinec5487a82018-12-03 18:08:14 +0100734 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735 if( status != PSA_SUCCESS )
736 return( status );
737
738 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200739 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100740
741 *p_slot = slot;
742 return( status );
743}
744
Gilles Peskinef603c712019-01-19 13:40:11 +0100745/** Calculate the intersection of two algorithm usage policies.
746 *
747 * Return 0 (which allows no operation) on incompatibility.
748 */
749static psa_algorithm_t psa_key_policy_algorithm_intersection(
750 psa_algorithm_t alg1,
751 psa_algorithm_t alg2 )
752{
753 /* Common case: the policy only allows alg. */
754 if( alg1 == alg2 )
755 return( alg1 );
756 /* If the policies are from the same hash-and-sign family, check
757 * if one is a wildcard. If so the other has the specific algorithm. */
758 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
759 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
760 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
761 {
762 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
763 return( alg2 );
764 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
765 return( alg1 );
766 }
767 /* If the policies are incompatible, allow nothing. */
768 return( 0 );
769}
770
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100771/** Test whether a policy permits an algorithm.
772 *
773 * The caller must test usage flags separately.
774 */
775static int psa_key_policy_permits( const psa_key_policy_t *policy,
776 psa_algorithm_t alg )
777{
778 /* Common case: the policy only allows alg. */
779 if( alg == policy->alg )
780 return( 1 );
781 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
782 * and alg is the same hash-and-sign family with any hash,
783 * then alg is compliant with policy->alg. */
784 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
785 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
786 {
787 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
788 ( alg & ~PSA_ALG_HASH_MASK ) );
789 }
790 /* If it isn't permitted, it's forbidden. */
791 return( 0 );
792}
793
Gilles Peskinef603c712019-01-19 13:40:11 +0100794/** Restrict a key policy based on a constraint.
795 *
796 * \param[in,out] policy The policy to restrict.
797 * \param[in] constraint The policy constraint to apply.
798 *
799 * \retval #PSA_SUCCESS
800 * \c *policy contains the intersection of the original value of
801 * \c *policy and \c *constraint.
802 * \retval #PSA_ERROR_INVALID_ARGUMENT
803 * \c *policy and \c *constraint are incompatible.
804 * \c *policy is unchanged.
805 */
806static psa_status_t psa_restrict_key_policy(
807 psa_key_policy_t *policy,
808 const psa_key_policy_t *constraint )
809{
810 psa_algorithm_t intersection_alg =
811 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
812 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
813 return( PSA_ERROR_INVALID_ARGUMENT );
814 policy->usage &= constraint->usage;
815 policy->alg = intersection_alg;
816 return( PSA_SUCCESS );
817}
818
Darryl Green06fd18d2018-07-16 11:21:11 +0100819/** Retrieve a slot which must contain a key. The key must have allow all the
820 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
821 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100822static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100824 psa_key_usage_t usage,
825 psa_algorithm_t alg )
826{
827 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100828 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100829
830 *p_slot = NULL;
831
Gilles Peskinec5487a82018-12-03 18:08:14 +0100832 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100833 if( status != PSA_SUCCESS )
834 return( status );
835 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200836 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100837
838 /* Enforce that usage policy for the key slot contains all the flags
839 * required by the usage parameter. There is one exception: public
840 * keys can always be exported, so we treat public key objects as
841 * if they had the export flag. */
842 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
843 usage &= ~PSA_KEY_USAGE_EXPORT;
844 if( ( slot->policy.usage & usage ) != usage )
845 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100846
847 /* Enforce that the usage policy permits the requested algortihm. */
848 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100849 return( PSA_ERROR_NOT_PERMITTED );
850
851 *p_slot = slot;
852 return( PSA_SUCCESS );
853}
Darryl Green940d72c2018-07-13 13:18:51 +0100854
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100855/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100856static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000857{
858 if( slot->type == PSA_KEY_TYPE_NONE )
859 {
860 /* No key material to clean. */
861 }
862 else if( key_type_is_raw_bytes( slot->type ) )
863 {
864 mbedtls_free( slot->data.raw.data );
865 }
866 else
867#if defined(MBEDTLS_RSA_C)
868 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
869 {
870 mbedtls_rsa_free( slot->data.rsa );
871 mbedtls_free( slot->data.rsa );
872 }
873 else
874#endif /* defined(MBEDTLS_RSA_C) */
875#if defined(MBEDTLS_ECP_C)
876 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
877 {
878 mbedtls_ecp_keypair_free( slot->data.ecp );
879 mbedtls_free( slot->data.ecp );
880 }
881 else
882#endif /* defined(MBEDTLS_ECP_C) */
883 {
884 /* Shouldn't happen: the key type is not any type that we
885 * put in. */
886 return( PSA_ERROR_TAMPERING_DETECTED );
887 }
888
889 return( PSA_SUCCESS );
890}
891
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100892/** Completely wipe a slot in memory, including its policy.
893 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100894psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100895{
896 psa_status_t status = psa_remove_key_data_from_memory( slot );
897 /* At this point, key material and other type-specific content has
898 * been wiped. Clear remaining metadata. We can call memset and not
899 * zeroize because the metadata is not particularly sensitive. */
900 memset( slot, 0, sizeof( *slot ) );
901 return( status );
902}
903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100905 psa_key_type_t type,
906 const uint8_t *data,
907 size_t data_length )
908{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100909 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100910 psa_status_t status;
911
Gilles Peskinec5487a82018-12-03 18:08:14 +0100912 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100913 if( status != PSA_SUCCESS )
914 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100915
916 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100917
918 status = psa_import_key_into_slot( slot, data, data_length );
919 if( status != PSA_SUCCESS )
920 {
921 slot->type = PSA_KEY_TYPE_NONE;
922 return( status );
923 }
924
Darryl Greend49a4992018-06-18 17:27:26 +0100925#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
926 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
927 {
928 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100929 status = psa_save_persistent_key( slot->persistent_storage_id,
930 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100931 data_length );
932 if( status != PSA_SUCCESS )
933 {
934 (void) psa_remove_key_data_from_memory( slot );
935 slot->type = PSA_KEY_TYPE_NONE;
936 }
937 }
938#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
939
940 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941}
942
Gilles Peskinec5487a82018-12-03 18:08:14 +0100943psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100945 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100946 psa_status_t status = PSA_SUCCESS;
947 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948
Gilles Peskinec5487a82018-12-03 18:08:14 +0100949 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( status != PSA_SUCCESS )
951 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100952#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
953 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
954 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100955 storage_status =
956 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100957 }
958#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100959 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100960 if( status != PSA_SUCCESS )
961 return( status );
962 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100963}
964
Gilles Peskineb870b182018-07-06 16:02:09 +0200965/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100966static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200967{
968 if( key_type_is_raw_bytes( slot->type ) )
969 return( slot->data.raw.bytes * 8 );
970#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200971 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100972 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200973#endif /* defined(MBEDTLS_RSA_C) */
974#if defined(MBEDTLS_ECP_C)
975 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
976 return( slot->data.ecp->grp.pbits );
977#endif /* defined(MBEDTLS_ECP_C) */
978 /* Shouldn't happen except on an empty slot. */
979 return( 0 );
980}
981
Gilles Peskinec5487a82018-12-03 18:08:14 +0100982psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200983 psa_key_type_t *type,
984 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100986 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200987 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200990 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( bits != NULL )
992 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100993 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200994 if( status != PSA_SUCCESS )
995 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200996
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200998 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200999 if( type != NULL )
1000 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001001 if( bits != NULL )
1002 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 return( PSA_SUCCESS );
1004}
1005
Jaeden Ameroccdce902019-01-10 11:42:27 +00001006#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001007static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1008 unsigned char *buf, size_t size )
1009{
1010 int ret;
1011 unsigned char *c;
1012 size_t len = 0;
1013
1014 c = buf + size;
1015
1016 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1017
1018 return( (int) len );
1019}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001020#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001021
Gilles Peskinef603c712019-01-19 13:40:11 +01001022static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1023 uint8_t *data,
1024 size_t data_size,
1025 size_t *data_length,
1026 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001028 *data_length = 0;
1029
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001030 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001031 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001032
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001033 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 {
1035 if( slot->data.raw.bytes > data_size )
1036 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001037 if( data_size != 0 )
1038 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001039 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001040 memset( data + slot->data.raw.bytes, 0,
1041 data_size - slot->data.raw.bytes );
1042 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043 *data_length = slot->data.raw.bytes;
1044 return( PSA_SUCCESS );
1045 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001046#if defined(MBEDTLS_ECP_C)
1047 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1048 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001049 psa_status_t status;
1050
Gilles Peskine188c71e2018-10-29 19:26:02 +01001051 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1052 if( bytes > data_size )
1053 return( PSA_ERROR_BUFFER_TOO_SMALL );
1054 status = mbedtls_to_psa_error(
1055 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1056 if( status != PSA_SUCCESS )
1057 return( status );
1058 memset( data + bytes, 0, data_size - bytes );
1059 *data_length = bytes;
1060 return( PSA_SUCCESS );
1061 }
1062#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063 else
Moran Peker17e36e12018-05-02 12:55:20 +03001064 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001065#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001066 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001067 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001068 {
Moran Pekera998bc62018-04-16 18:16:20 +03001069 mbedtls_pk_context pk;
1070 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001071 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001072 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001073#if defined(MBEDTLS_RSA_C)
1074 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001075 pk.pk_info = &mbedtls_rsa_info;
1076 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001077#else
1078 return( PSA_ERROR_NOT_SUPPORTED );
1079#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001080 }
1081 else
1082 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001083#if defined(MBEDTLS_ECP_C)
1084 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001085 pk.pk_info = &mbedtls_eckey_info;
1086 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001087#else
1088 return( PSA_ERROR_NOT_SUPPORTED );
1089#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001090 }
Moran Pekerd7326592018-05-29 16:56:39 +03001091 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001092 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001093 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001094 }
Moran Peker17e36e12018-05-02 12:55:20 +03001095 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001096 {
Moran Peker17e36e12018-05-02 12:55:20 +03001097 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001098 }
Moran Peker60364322018-04-29 11:34:58 +03001099 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001100 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001101 /* If data_size is 0 then data may be NULL and then the
1102 * call to memset would have undefined behavior. */
1103 if( data_size != 0 )
1104 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001105 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001106 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001107 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1108 * Move the data to the beginning and erase remaining data
1109 * at the original location. */
1110 if( 2 * (size_t) ret <= data_size )
1111 {
1112 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001113 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001114 }
1115 else if( (size_t) ret < data_size )
1116 {
1117 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001118 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001119 }
Moran Pekera998bc62018-04-16 18:16:20 +03001120 *data_length = ret;
1121 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001122 }
1123 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001124#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001125 {
1126 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001127 it is valid for a special-purpose implementation to omit
1128 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001129 return( PSA_ERROR_NOT_SUPPORTED );
1130 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001131 }
1132}
1133
Gilles Peskinec5487a82018-12-03 18:08:14 +01001134psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001135 uint8_t *data,
1136 size_t data_size,
1137 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001138{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001139 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001140 psa_status_t status;
1141
1142 /* Set the key to empty now, so that even when there are errors, we always
1143 * set data_length to a value between 0 and data_size. On error, setting
1144 * the key to empty is a good choice because an empty key representation is
1145 * unlikely to be accepted anywhere. */
1146 *data_length = 0;
1147
1148 /* Export requires the EXPORT flag. There is an exception for public keys,
1149 * which don't require any flag, but psa_get_key_from_slot takes
1150 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001151 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001152 if( status != PSA_SUCCESS )
1153 return( status );
1154 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001155 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001157
Gilles Peskinec5487a82018-12-03 18:08:14 +01001158psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001159 uint8_t *data,
1160 size_t data_size,
1161 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001162{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001163 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001164 psa_status_t status;
1165
1166 /* Set the key to empty now, so that even when there are errors, we always
1167 * set data_length to a value between 0 and data_size. On error, setting
1168 * the key to empty is a good choice because an empty key representation is
1169 * unlikely to be accepted anywhere. */
1170 *data_length = 0;
1171
1172 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001173 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001174 if( status != PSA_SUCCESS )
1175 return( status );
1176 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001177 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001178}
1179
Darryl Green0c6575a2018-11-07 16:05:30 +00001180#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001181static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001182 size_t bits )
1183{
1184 psa_status_t status;
1185 uint8_t *data;
1186 size_t key_length;
1187 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1188 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001189 if( data == NULL )
1190 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001191 /* Get key data in export format */
1192 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1193 if( status != PSA_SUCCESS )
1194 {
1195 slot->type = PSA_KEY_TYPE_NONE;
1196 goto exit;
1197 }
1198 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001199 status = psa_save_persistent_key( slot->persistent_storage_id,
1200 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001201 data, key_length );
1202 if( status != PSA_SUCCESS )
1203 {
1204 slot->type = PSA_KEY_TYPE_NONE;
1205 }
1206exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001207 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001208 mbedtls_free( data );
1209 return( status );
1210}
1211#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1212
Gilles Peskinef603c712019-01-19 13:40:11 +01001213static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1214 psa_key_handle_t target )
1215{
1216 psa_status_t status;
1217 uint8_t *buffer = NULL;
1218 size_t buffer_size = 0;
1219 size_t length;
1220
1221 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1222 psa_get_key_bits( source ) );
1223 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001224 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001225 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001226 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1227 if( status != PSA_SUCCESS )
1228 goto exit;
1229 status = psa_import_key( target, source->type, buffer, length );
1230
1231exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001232 if( buffer_size != 0 )
1233 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001234 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001235 return( status );
1236}
1237
1238psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1239 psa_key_handle_t target_handle,
1240 const psa_key_policy_t *constraint)
1241{
1242 psa_key_slot_t *source_slot = NULL;
1243 psa_key_slot_t *target_slot = NULL;
1244 psa_key_policy_t new_policy;
1245 psa_status_t status;
1246 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1247 if( status != PSA_SUCCESS )
1248 return( status );
1249 status = psa_get_empty_key_slot( target_handle, &target_slot );
1250 if( status != PSA_SUCCESS )
1251 return( status );
1252
1253 new_policy = target_slot->policy;
1254 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1255 if( status != PSA_SUCCESS )
1256 return( status );
1257 if( constraint != NULL )
1258 {
1259 status = psa_restrict_key_policy( &new_policy, constraint );
1260 if( status != PSA_SUCCESS )
1261 return( status );
1262 }
1263
1264 status = psa_copy_key_material( source_slot, target_handle );
1265 if( status != PSA_SUCCESS )
1266 return( status );
1267
1268 target_slot->policy = new_policy;
1269 return( PSA_SUCCESS );
1270}
1271
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001272
1273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001275/* Message digests */
1276/****************************************************************/
1277
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001278static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001279{
1280 switch( alg )
1281 {
1282#if defined(MBEDTLS_MD2_C)
1283 case PSA_ALG_MD2:
1284 return( &mbedtls_md2_info );
1285#endif
1286#if defined(MBEDTLS_MD4_C)
1287 case PSA_ALG_MD4:
1288 return( &mbedtls_md4_info );
1289#endif
1290#if defined(MBEDTLS_MD5_C)
1291 case PSA_ALG_MD5:
1292 return( &mbedtls_md5_info );
1293#endif
1294#if defined(MBEDTLS_RIPEMD160_C)
1295 case PSA_ALG_RIPEMD160:
1296 return( &mbedtls_ripemd160_info );
1297#endif
1298#if defined(MBEDTLS_SHA1_C)
1299 case PSA_ALG_SHA_1:
1300 return( &mbedtls_sha1_info );
1301#endif
1302#if defined(MBEDTLS_SHA256_C)
1303 case PSA_ALG_SHA_224:
1304 return( &mbedtls_sha224_info );
1305 case PSA_ALG_SHA_256:
1306 return( &mbedtls_sha256_info );
1307#endif
1308#if defined(MBEDTLS_SHA512_C)
1309 case PSA_ALG_SHA_384:
1310 return( &mbedtls_sha384_info );
1311 case PSA_ALG_SHA_512:
1312 return( &mbedtls_sha512_info );
1313#endif
1314 default:
1315 return( NULL );
1316 }
1317}
1318
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001319psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1320{
1321 switch( operation->alg )
1322 {
Gilles Peskine81736312018-06-26 15:04:31 +02001323 case 0:
1324 /* The object has (apparently) been initialized but it is not
1325 * in use. It's ok to call abort on such an object, and there's
1326 * nothing to do. */
1327 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001328#if defined(MBEDTLS_MD2_C)
1329 case PSA_ALG_MD2:
1330 mbedtls_md2_free( &operation->ctx.md2 );
1331 break;
1332#endif
1333#if defined(MBEDTLS_MD4_C)
1334 case PSA_ALG_MD4:
1335 mbedtls_md4_free( &operation->ctx.md4 );
1336 break;
1337#endif
1338#if defined(MBEDTLS_MD5_C)
1339 case PSA_ALG_MD5:
1340 mbedtls_md5_free( &operation->ctx.md5 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_RIPEMD160_C)
1344 case PSA_ALG_RIPEMD160:
1345 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1346 break;
1347#endif
1348#if defined(MBEDTLS_SHA1_C)
1349 case PSA_ALG_SHA_1:
1350 mbedtls_sha1_free( &operation->ctx.sha1 );
1351 break;
1352#endif
1353#if defined(MBEDTLS_SHA256_C)
1354 case PSA_ALG_SHA_224:
1355 case PSA_ALG_SHA_256:
1356 mbedtls_sha256_free( &operation->ctx.sha256 );
1357 break;
1358#endif
1359#if defined(MBEDTLS_SHA512_C)
1360 case PSA_ALG_SHA_384:
1361 case PSA_ALG_SHA_512:
1362 mbedtls_sha512_free( &operation->ctx.sha512 );
1363 break;
1364#endif
1365 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001366 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001367 }
1368 operation->alg = 0;
1369 return( PSA_SUCCESS );
1370}
1371
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001372psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001373 psa_algorithm_t alg )
1374{
1375 int ret;
1376 operation->alg = 0;
1377 switch( alg )
1378 {
1379#if defined(MBEDTLS_MD2_C)
1380 case PSA_ALG_MD2:
1381 mbedtls_md2_init( &operation->ctx.md2 );
1382 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1383 break;
1384#endif
1385#if defined(MBEDTLS_MD4_C)
1386 case PSA_ALG_MD4:
1387 mbedtls_md4_init( &operation->ctx.md4 );
1388 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1389 break;
1390#endif
1391#if defined(MBEDTLS_MD5_C)
1392 case PSA_ALG_MD5:
1393 mbedtls_md5_init( &operation->ctx.md5 );
1394 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1395 break;
1396#endif
1397#if defined(MBEDTLS_RIPEMD160_C)
1398 case PSA_ALG_RIPEMD160:
1399 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1400 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1401 break;
1402#endif
1403#if defined(MBEDTLS_SHA1_C)
1404 case PSA_ALG_SHA_1:
1405 mbedtls_sha1_init( &operation->ctx.sha1 );
1406 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1407 break;
1408#endif
1409#if defined(MBEDTLS_SHA256_C)
1410 case PSA_ALG_SHA_224:
1411 mbedtls_sha256_init( &operation->ctx.sha256 );
1412 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1413 break;
1414 case PSA_ALG_SHA_256:
1415 mbedtls_sha256_init( &operation->ctx.sha256 );
1416 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1417 break;
1418#endif
1419#if defined(MBEDTLS_SHA512_C)
1420 case PSA_ALG_SHA_384:
1421 mbedtls_sha512_init( &operation->ctx.sha512 );
1422 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1423 break;
1424 case PSA_ALG_SHA_512:
1425 mbedtls_sha512_init( &operation->ctx.sha512 );
1426 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1427 break;
1428#endif
1429 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001430 return( PSA_ALG_IS_HASH( alg ) ?
1431 PSA_ERROR_NOT_SUPPORTED :
1432 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001433 }
1434 if( ret == 0 )
1435 operation->alg = alg;
1436 else
1437 psa_hash_abort( operation );
1438 return( mbedtls_to_psa_error( ret ) );
1439}
1440
1441psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1442 const uint8_t *input,
1443 size_t input_length )
1444{
1445 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001446
1447 /* Don't require hash implementations to behave correctly on a
1448 * zero-length input, which may have an invalid pointer. */
1449 if( input_length == 0 )
1450 return( PSA_SUCCESS );
1451
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001452 switch( operation->alg )
1453 {
1454#if defined(MBEDTLS_MD2_C)
1455 case PSA_ALG_MD2:
1456 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1457 input, input_length );
1458 break;
1459#endif
1460#if defined(MBEDTLS_MD4_C)
1461 case PSA_ALG_MD4:
1462 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1463 input, input_length );
1464 break;
1465#endif
1466#if defined(MBEDTLS_MD5_C)
1467 case PSA_ALG_MD5:
1468 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1469 input, input_length );
1470 break;
1471#endif
1472#if defined(MBEDTLS_RIPEMD160_C)
1473 case PSA_ALG_RIPEMD160:
1474 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1475 input, input_length );
1476 break;
1477#endif
1478#if defined(MBEDTLS_SHA1_C)
1479 case PSA_ALG_SHA_1:
1480 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1481 input, input_length );
1482 break;
1483#endif
1484#if defined(MBEDTLS_SHA256_C)
1485 case PSA_ALG_SHA_224:
1486 case PSA_ALG_SHA_256:
1487 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1488 input, input_length );
1489 break;
1490#endif
1491#if defined(MBEDTLS_SHA512_C)
1492 case PSA_ALG_SHA_384:
1493 case PSA_ALG_SHA_512:
1494 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1495 input, input_length );
1496 break;
1497#endif
1498 default:
1499 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1500 break;
1501 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001503 if( ret != 0 )
1504 psa_hash_abort( operation );
1505 return( mbedtls_to_psa_error( ret ) );
1506}
1507
1508psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1509 uint8_t *hash,
1510 size_t hash_size,
1511 size_t *hash_length )
1512{
itayzafrir40835d42018-08-02 13:14:17 +03001513 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001514 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001515 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001516
1517 /* Fill the output buffer with something that isn't a valid hash
1518 * (barring an attack on the hash and deliberately-crafted input),
1519 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001520 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001521 /* If hash_size is 0 then hash may be NULL and then the
1522 * call to memset would have undefined behavior. */
1523 if( hash_size != 0 )
1524 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001525
1526 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001527 {
1528 status = PSA_ERROR_BUFFER_TOO_SMALL;
1529 goto exit;
1530 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531
1532 switch( operation->alg )
1533 {
1534#if defined(MBEDTLS_MD2_C)
1535 case PSA_ALG_MD2:
1536 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1537 break;
1538#endif
1539#if defined(MBEDTLS_MD4_C)
1540 case PSA_ALG_MD4:
1541 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1542 break;
1543#endif
1544#if defined(MBEDTLS_MD5_C)
1545 case PSA_ALG_MD5:
1546 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1547 break;
1548#endif
1549#if defined(MBEDTLS_RIPEMD160_C)
1550 case PSA_ALG_RIPEMD160:
1551 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1552 break;
1553#endif
1554#if defined(MBEDTLS_SHA1_C)
1555 case PSA_ALG_SHA_1:
1556 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1557 break;
1558#endif
1559#if defined(MBEDTLS_SHA256_C)
1560 case PSA_ALG_SHA_224:
1561 case PSA_ALG_SHA_256:
1562 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1563 break;
1564#endif
1565#if defined(MBEDTLS_SHA512_C)
1566 case PSA_ALG_SHA_384:
1567 case PSA_ALG_SHA_512:
1568 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1569 break;
1570#endif
1571 default:
1572 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1573 break;
1574 }
itayzafrir40835d42018-08-02 13:14:17 +03001575 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576
itayzafrir40835d42018-08-02 13:14:17 +03001577exit:
1578 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001580 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001581 return( psa_hash_abort( operation ) );
1582 }
1583 else
1584 {
1585 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001586 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587 }
1588}
1589
Gilles Peskine2d277862018-06-18 15:41:12 +02001590psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1591 const uint8_t *hash,
1592 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593{
1594 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1595 size_t actual_hash_length;
1596 psa_status_t status = psa_hash_finish( operation,
1597 actual_hash, sizeof( actual_hash ),
1598 &actual_hash_length );
1599 if( status != PSA_SUCCESS )
1600 return( status );
1601 if( actual_hash_length != hash_length )
1602 return( PSA_ERROR_INVALID_SIGNATURE );
1603 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1604 return( PSA_ERROR_INVALID_SIGNATURE );
1605 return( PSA_SUCCESS );
1606}
1607
Gilles Peskineeb35d782019-01-22 17:56:16 +01001608psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1609 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001610{
1611 if( target_operation->alg != 0 )
1612 return( PSA_ERROR_BAD_STATE );
1613
1614 switch( source_operation->alg )
1615 {
1616 case 0:
1617 return( PSA_ERROR_BAD_STATE );
1618#if defined(MBEDTLS_MD2_C)
1619 case PSA_ALG_MD2:
1620 mbedtls_md2_clone( &target_operation->ctx.md2,
1621 &source_operation->ctx.md2 );
1622 break;
1623#endif
1624#if defined(MBEDTLS_MD4_C)
1625 case PSA_ALG_MD4:
1626 mbedtls_md4_clone( &target_operation->ctx.md4,
1627 &source_operation->ctx.md4 );
1628 break;
1629#endif
1630#if defined(MBEDTLS_MD5_C)
1631 case PSA_ALG_MD5:
1632 mbedtls_md5_clone( &target_operation->ctx.md5,
1633 &source_operation->ctx.md5 );
1634 break;
1635#endif
1636#if defined(MBEDTLS_RIPEMD160_C)
1637 case PSA_ALG_RIPEMD160:
1638 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1639 &source_operation->ctx.ripemd160 );
1640 break;
1641#endif
1642#if defined(MBEDTLS_SHA1_C)
1643 case PSA_ALG_SHA_1:
1644 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1645 &source_operation->ctx.sha1 );
1646 break;
1647#endif
1648#if defined(MBEDTLS_SHA256_C)
1649 case PSA_ALG_SHA_224:
1650 case PSA_ALG_SHA_256:
1651 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1652 &source_operation->ctx.sha256 );
1653 break;
1654#endif
1655#if defined(MBEDTLS_SHA512_C)
1656 case PSA_ALG_SHA_384:
1657 case PSA_ALG_SHA_512:
1658 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1659 &source_operation->ctx.sha512 );
1660 break;
1661#endif
1662 default:
1663 return( PSA_ERROR_NOT_SUPPORTED );
1664 }
1665
1666 target_operation->alg = source_operation->alg;
1667 return( PSA_SUCCESS );
1668}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001669
1670
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001671/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672/* MAC */
1673/****************************************************************/
1674
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001675static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676 psa_algorithm_t alg,
1677 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001678 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001679 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001682 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001684 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001685 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001686
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1688 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001689 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001691 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 mode = MBEDTLS_MODE_STREAM;
1693 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 case PSA_ALG_CTR:
1695 mode = MBEDTLS_MODE_CTR;
1696 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001697 case PSA_ALG_CFB:
1698 mode = MBEDTLS_MODE_CFB;
1699 break;
1700 case PSA_ALG_OFB:
1701 mode = MBEDTLS_MODE_OFB;
1702 break;
1703 case PSA_ALG_CBC_NO_PADDING:
1704 mode = MBEDTLS_MODE_CBC;
1705 break;
1706 case PSA_ALG_CBC_PKCS7:
1707 mode = MBEDTLS_MODE_CBC;
1708 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001709 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001710 mode = MBEDTLS_MODE_CCM;
1711 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001712 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001713 mode = MBEDTLS_MODE_GCM;
1714 break;
1715 default:
1716 return( NULL );
1717 }
1718 }
1719 else if( alg == PSA_ALG_CMAC )
1720 mode = MBEDTLS_MODE_ECB;
1721 else if( alg == PSA_ALG_GMAC )
1722 mode = MBEDTLS_MODE_GCM;
1723 else
1724 return( NULL );
1725
1726 switch( key_type )
1727 {
1728 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001729 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730 break;
1731 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001732 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1733 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001734 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001735 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001736 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001737 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001738 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1739 * but two-key Triple-DES is functionally three-key Triple-DES
1740 * with K1=K3, so that's how we present it to mbedtls. */
1741 if( key_bits == 128 )
1742 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001743 break;
1744 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001745 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746 break;
1747 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001748 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001749 break;
1750 default:
1751 return( NULL );
1752 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001753 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001754 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755
Jaeden Amero23bbb752018-06-26 14:16:54 +01001756 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1757 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758}
1759
Gilles Peskinea05219c2018-11-16 16:02:56 +01001760#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001761static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001762{
Gilles Peskine2d277862018-06-18 15:41:12 +02001763 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001764 {
1765 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001766 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001767 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001768 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001769 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001770 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001771 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001774 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001775 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001776 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001779 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001782 return( 128 );
1783 default:
1784 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 }
1786}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001787#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001788
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001789/* Initialize the MAC operation structure. Once this function has been
1790 * called, psa_mac_abort can run and will do the right thing. */
1791static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1792 psa_algorithm_t alg )
1793{
1794 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1795
1796 operation->alg = alg;
1797 operation->key_set = 0;
1798 operation->iv_set = 0;
1799 operation->iv_required = 0;
1800 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001801 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802
1803#if defined(MBEDTLS_CMAC_C)
1804 if( alg == PSA_ALG_CMAC )
1805 {
1806 operation->iv_required = 0;
1807 mbedtls_cipher_init( &operation->ctx.cmac );
1808 status = PSA_SUCCESS;
1809 }
1810 else
1811#endif /* MBEDTLS_CMAC_C */
1812#if defined(MBEDTLS_MD_C)
1813 if( PSA_ALG_IS_HMAC( operation->alg ) )
1814 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001815 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1816 operation->ctx.hmac.hash_ctx.alg = 0;
1817 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818 }
1819 else
1820#endif /* MBEDTLS_MD_C */
1821 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001822 if( ! PSA_ALG_IS_MAC( alg ) )
1823 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001824 }
1825
1826 if( status != PSA_SUCCESS )
1827 memset( operation, 0, sizeof( *operation ) );
1828 return( status );
1829}
1830
Gilles Peskine01126fa2018-07-12 17:04:55 +02001831#if defined(MBEDTLS_MD_C)
1832static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1833{
Gilles Peskine3f108122018-12-07 18:14:53 +01001834 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001835 return( psa_hash_abort( &hmac->hash_ctx ) );
1836}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001837
1838static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1839{
1840 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1841 memset( hmac, 0, sizeof( *hmac ) );
1842}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001843#endif /* MBEDTLS_MD_C */
1844
Gilles Peskine8c9def32018-02-08 10:02:12 +01001845psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1846{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001847 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001848 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001849 /* The object has (apparently) been initialized but it is not
1850 * in use. It's ok to call abort on such an object, and there's
1851 * nothing to do. */
1852 return( PSA_SUCCESS );
1853 }
1854 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001855#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001856 if( operation->alg == PSA_ALG_CMAC )
1857 {
1858 mbedtls_cipher_free( &operation->ctx.cmac );
1859 }
1860 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001862#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001863 if( PSA_ALG_IS_HMAC( operation->alg ) )
1864 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001865 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001866 }
1867 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001868#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 {
1870 /* Sanity check (shouldn't happen: operation->alg should
1871 * always have been initialized to a valid value). */
1872 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873 }
Moran Peker41deec42018-04-04 15:43:05 +03001874
Gilles Peskine8c9def32018-02-08 10:02:12 +01001875 operation->alg = 0;
1876 operation->key_set = 0;
1877 operation->iv_set = 0;
1878 operation->iv_required = 0;
1879 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001880 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001881
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001883
1884bad_state:
1885 /* If abort is called on an uninitialized object, we can't trust
1886 * anything. Wipe the object in case it contains confidential data.
1887 * This may result in a memory leak if a pointer gets overwritten,
1888 * but it's too late to do anything about this. */
1889 memset( operation, 0, sizeof( *operation ) );
1890 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001891}
1892
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001893#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001894static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001895 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001896 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001897 const mbedtls_cipher_info_t *cipher_info )
1898{
1899 int ret;
1900
1901 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001902
1903 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1904 if( ret != 0 )
1905 return( ret );
1906
1907 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1908 slot->data.raw.data,
1909 key_bits );
1910 return( ret );
1911}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001912#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913
Gilles Peskine248051a2018-06-20 16:09:38 +02001914#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001915static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1916 const uint8_t *key,
1917 size_t key_length,
1918 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001919{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001920 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001921 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001922 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001923 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001924 psa_status_t status;
1925
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001926 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1927 * overflow below. This should never trigger if the hash algorithm
1928 * is implemented correctly. */
1929 /* The size checks against the ipad and opad buffers cannot be written
1930 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1931 * because that triggers -Wlogical-op on GCC 7.3. */
1932 if( block_size > sizeof( ipad ) )
1933 return( PSA_ERROR_NOT_SUPPORTED );
1934 if( block_size > sizeof( hmac->opad ) )
1935 return( PSA_ERROR_NOT_SUPPORTED );
1936 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001937 return( PSA_ERROR_NOT_SUPPORTED );
1938
Gilles Peskined223b522018-06-11 18:12:58 +02001939 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001940 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001941 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001942 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001943 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001944 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001945 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001946 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001947 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001948 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001949 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001950 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001951 }
Gilles Peskine96889972018-07-12 17:07:03 +02001952 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1953 * but it is permitted. It is common when HMAC is used in HKDF, for
1954 * example. Don't call `memcpy` in the 0-length because `key` could be
1955 * an invalid pointer which would make the behavior undefined. */
1956 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001957 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001958
Gilles Peskined223b522018-06-11 18:12:58 +02001959 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1960 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001961 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001962 ipad[i] ^= 0x36;
1963 memset( ipad + key_length, 0x36, block_size - key_length );
1964
1965 /* Copy the key material from ipad to opad, flipping the requisite bits,
1966 * and filling the rest of opad with the requisite constant. */
1967 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001968 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1969 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001970
Gilles Peskine01126fa2018-07-12 17:04:55 +02001971 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001972 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001973 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001974
Gilles Peskine01126fa2018-07-12 17:04:55 +02001975 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001976
1977cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001978 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001979
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001980 return( status );
1981}
Gilles Peskine248051a2018-06-20 16:09:38 +02001982#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001983
Gilles Peskine89167cb2018-07-08 20:12:23 +02001984static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001985 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001986 psa_algorithm_t alg,
1987 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001989 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001990 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001992 psa_key_usage_t usage =
1993 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001994 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001995 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001996
Gilles Peskined911eb72018-08-14 15:18:45 +02001997 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001998 if( status != PSA_SUCCESS )
1999 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002000 if( is_sign )
2001 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002
Gilles Peskinec5487a82018-12-03 18:08:14 +01002003 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002004 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002005 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002006 key_bits = psa_get_key_bits( slot );
2007
Gilles Peskine8c9def32018-02-08 10:02:12 +01002008#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002009 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002010 {
2011 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002012 mbedtls_cipher_info_from_psa( full_length_alg,
2013 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002014 int ret;
2015 if( cipher_info == NULL )
2016 {
2017 status = PSA_ERROR_NOT_SUPPORTED;
2018 goto exit;
2019 }
2020 operation->mac_size = cipher_info->block_size;
2021 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2022 status = mbedtls_to_psa_error( ret );
2023 }
2024 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002027 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002028 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002029 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002030 if( hash_alg == 0 )
2031 {
2032 status = PSA_ERROR_NOT_SUPPORTED;
2033 goto exit;
2034 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002035
2036 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2037 /* Sanity check. This shouldn't fail on a valid configuration. */
2038 if( operation->mac_size == 0 ||
2039 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2040 {
2041 status = PSA_ERROR_NOT_SUPPORTED;
2042 goto exit;
2043 }
2044
Gilles Peskine01126fa2018-07-12 17:04:55 +02002045 if( slot->type != PSA_KEY_TYPE_HMAC )
2046 {
2047 status = PSA_ERROR_INVALID_ARGUMENT;
2048 goto exit;
2049 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002050
Gilles Peskine01126fa2018-07-12 17:04:55 +02002051 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2052 slot->data.raw.data,
2053 slot->data.raw.bytes,
2054 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002055 }
2056 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002058 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002059 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002060 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002061 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002062
Gilles Peskined911eb72018-08-14 15:18:45 +02002063 if( truncated == 0 )
2064 {
2065 /* The "normal" case: untruncated algorithm. Nothing to do. */
2066 }
2067 else if( truncated < 4 )
2068 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002069 /* A very short MAC is too short for security since it can be
2070 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2071 * so we make this our minimum, even though 32 bits is still
2072 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002073 status = PSA_ERROR_NOT_SUPPORTED;
2074 }
2075 else if( truncated > operation->mac_size )
2076 {
2077 /* It's impossible to "truncate" to a larger length. */
2078 status = PSA_ERROR_INVALID_ARGUMENT;
2079 }
2080 else
2081 operation->mac_size = truncated;
2082
Gilles Peskinefbfac682018-07-08 20:51:54 +02002083exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002084 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002086 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002088 else
2089 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002090 operation->key_set = 1;
2091 }
2092 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002093}
2094
Gilles Peskine89167cb2018-07-08 20:12:23 +02002095psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002096 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002097 psa_algorithm_t alg )
2098{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002099 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002100}
2101
2102psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002103 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002104 psa_algorithm_t alg )
2105{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002106 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002107}
2108
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2110 const uint8_t *input,
2111 size_t input_length )
2112{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002113 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002114 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002115 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002117 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002118 operation->has_input = 1;
2119
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002121 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002122 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002123 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2124 input, input_length );
2125 status = mbedtls_to_psa_error( ret );
2126 }
2127 else
2128#endif /* MBEDTLS_CMAC_C */
2129#if defined(MBEDTLS_MD_C)
2130 if( PSA_ALG_IS_HMAC( operation->alg ) )
2131 {
2132 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2133 input_length );
2134 }
2135 else
2136#endif /* MBEDTLS_MD_C */
2137 {
2138 /* This shouldn't happen if `operation` was initialized by
2139 * a setup function. */
2140 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002141 }
2142
Gilles Peskinefbfac682018-07-08 20:51:54 +02002143cleanup:
2144 if( status != PSA_SUCCESS )
2145 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002146 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002147}
2148
Gilles Peskine01126fa2018-07-12 17:04:55 +02002149#if defined(MBEDTLS_MD_C)
2150static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2151 uint8_t *mac,
2152 size_t mac_size )
2153{
2154 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2155 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2156 size_t hash_size = 0;
2157 size_t block_size = psa_get_hash_block_size( hash_alg );
2158 psa_status_t status;
2159
Gilles Peskine01126fa2018-07-12 17:04:55 +02002160 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2161 if( status != PSA_SUCCESS )
2162 return( status );
2163 /* From here on, tmp needs to be wiped. */
2164
2165 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2166 if( status != PSA_SUCCESS )
2167 goto exit;
2168
2169 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2170 if( status != PSA_SUCCESS )
2171 goto exit;
2172
2173 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2174 if( status != PSA_SUCCESS )
2175 goto exit;
2176
Gilles Peskined911eb72018-08-14 15:18:45 +02002177 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2178 if( status != PSA_SUCCESS )
2179 goto exit;
2180
2181 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002182
2183exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002184 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002185 return( status );
2186}
2187#endif /* MBEDTLS_MD_C */
2188
mohammad16036df908f2018-04-02 08:34:15 -07002189static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002190 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002191 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002193 if( ! operation->key_set )
2194 return( PSA_ERROR_BAD_STATE );
2195 if( operation->iv_required && ! operation->iv_set )
2196 return( PSA_ERROR_BAD_STATE );
2197
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198 if( mac_size < operation->mac_size )
2199 return( PSA_ERROR_BUFFER_TOO_SMALL );
2200
Gilles Peskine8c9def32018-02-08 10:02:12 +01002201#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002202 if( operation->alg == PSA_ALG_CMAC )
2203 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002204 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2205 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2206 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002207 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002208 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002209 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002211 else
2212#endif /* MBEDTLS_CMAC_C */
2213#if defined(MBEDTLS_MD_C)
2214 if( PSA_ALG_IS_HMAC( operation->alg ) )
2215 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002216 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002217 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002218 }
2219 else
2220#endif /* MBEDTLS_MD_C */
2221 {
2222 /* This shouldn't happen if `operation` was initialized by
2223 * a setup function. */
2224 return( PSA_ERROR_BAD_STATE );
2225 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226}
2227
Gilles Peskineacd4be32018-07-08 19:56:25 +02002228psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2229 uint8_t *mac,
2230 size_t mac_size,
2231 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002232{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002233 psa_status_t status;
2234
2235 /* Fill the output buffer with something that isn't a valid mac
2236 * (barring an attack on the mac and deliberately-crafted input),
2237 * in case the caller doesn't check the return status properly. */
2238 *mac_length = mac_size;
2239 /* If mac_size is 0 then mac may be NULL and then the
2240 * call to memset would have undefined behavior. */
2241 if( mac_size != 0 )
2242 memset( mac, '!', mac_size );
2243
Gilles Peskine89167cb2018-07-08 20:12:23 +02002244 if( ! operation->is_sign )
2245 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002246 status = PSA_ERROR_BAD_STATE;
2247 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002248 }
mohammad16036df908f2018-04-02 08:34:15 -07002249
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002250 status = psa_mac_finish_internal( operation, mac, mac_size );
2251
2252cleanup:
2253 if( status == PSA_SUCCESS )
2254 {
2255 status = psa_mac_abort( operation );
2256 if( status == PSA_SUCCESS )
2257 *mac_length = operation->mac_size;
2258 else
2259 memset( mac, '!', mac_size );
2260 }
2261 else
2262 psa_mac_abort( operation );
2263 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002264}
2265
Gilles Peskineacd4be32018-07-08 19:56:25 +02002266psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2267 const uint8_t *mac,
2268 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002269{
Gilles Peskine828ed142018-06-18 23:25:51 +02002270 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002271 psa_status_t status;
2272
Gilles Peskine89167cb2018-07-08 20:12:23 +02002273 if( operation->is_sign )
2274 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002275 status = PSA_ERROR_BAD_STATE;
2276 goto cleanup;
2277 }
2278 if( operation->mac_size != mac_length )
2279 {
2280 status = PSA_ERROR_INVALID_SIGNATURE;
2281 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002282 }
mohammad16036df908f2018-04-02 08:34:15 -07002283
2284 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002285 actual_mac, sizeof( actual_mac ) );
2286
2287 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2288 status = PSA_ERROR_INVALID_SIGNATURE;
2289
2290cleanup:
2291 if( status == PSA_SUCCESS )
2292 status = psa_mac_abort( operation );
2293 else
2294 psa_mac_abort( operation );
2295
Gilles Peskine3f108122018-12-07 18:14:53 +01002296 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002297
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002298 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299}
2300
2301
Gilles Peskine20035e32018-02-03 22:44:14 +01002302
Gilles Peskine20035e32018-02-03 22:44:14 +01002303/****************************************************************/
2304/* Asymmetric cryptography */
2305/****************************************************************/
2306
Gilles Peskine2b450e32018-06-27 15:42:46 +02002307#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002308/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002309 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002310static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2311 size_t hash_length,
2312 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002313{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002314 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002315 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002316 *md_alg = mbedtls_md_get_type( md_info );
2317
2318 /* The Mbed TLS RSA module uses an unsigned int for hash length
2319 * parameters. Validate that it fits so that we don't risk an
2320 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002321#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002322 if( hash_length > UINT_MAX )
2323 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002324#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002325
2326#if defined(MBEDTLS_PKCS1_V15)
2327 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2328 * must be correct. */
2329 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2330 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002331 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002332 if( md_info == NULL )
2333 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002334 if( mbedtls_md_get_size( md_info ) != hash_length )
2335 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002336 }
2337#endif /* MBEDTLS_PKCS1_V15 */
2338
2339#if defined(MBEDTLS_PKCS1_V21)
2340 /* PSS requires a hash internally. */
2341 if( PSA_ALG_IS_RSA_PSS( alg ) )
2342 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002343 if( md_info == NULL )
2344 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002345 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002346#endif /* MBEDTLS_PKCS1_V21 */
2347
Gilles Peskine61b91d42018-06-08 16:09:36 +02002348 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002349}
2350
Gilles Peskine2b450e32018-06-27 15:42:46 +02002351static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2352 psa_algorithm_t alg,
2353 const uint8_t *hash,
2354 size_t hash_length,
2355 uint8_t *signature,
2356 size_t signature_size,
2357 size_t *signature_length )
2358{
2359 psa_status_t status;
2360 int ret;
2361 mbedtls_md_type_t md_alg;
2362
2363 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2364 if( status != PSA_SUCCESS )
2365 return( status );
2366
Gilles Peskine630a18a2018-06-29 17:49:35 +02002367 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002368 return( PSA_ERROR_BUFFER_TOO_SMALL );
2369
2370#if defined(MBEDTLS_PKCS1_V15)
2371 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2372 {
2373 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2374 MBEDTLS_MD_NONE );
2375 ret = mbedtls_rsa_pkcs1_sign( rsa,
2376 mbedtls_ctr_drbg_random,
2377 &global_data.ctr_drbg,
2378 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002379 md_alg,
2380 (unsigned int) hash_length,
2381 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002382 signature );
2383 }
2384 else
2385#endif /* MBEDTLS_PKCS1_V15 */
2386#if defined(MBEDTLS_PKCS1_V21)
2387 if( PSA_ALG_IS_RSA_PSS( alg ) )
2388 {
2389 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2390 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2391 mbedtls_ctr_drbg_random,
2392 &global_data.ctr_drbg,
2393 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002394 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002395 (unsigned int) hash_length,
2396 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002397 signature );
2398 }
2399 else
2400#endif /* MBEDTLS_PKCS1_V21 */
2401 {
2402 return( PSA_ERROR_INVALID_ARGUMENT );
2403 }
2404
2405 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002406 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002407 return( mbedtls_to_psa_error( ret ) );
2408}
2409
2410static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2411 psa_algorithm_t alg,
2412 const uint8_t *hash,
2413 size_t hash_length,
2414 const uint8_t *signature,
2415 size_t signature_length )
2416{
2417 psa_status_t status;
2418 int ret;
2419 mbedtls_md_type_t md_alg;
2420
2421 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2422 if( status != PSA_SUCCESS )
2423 return( status );
2424
Gilles Peskine630a18a2018-06-29 17:49:35 +02002425 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002426 return( PSA_ERROR_BUFFER_TOO_SMALL );
2427
2428#if defined(MBEDTLS_PKCS1_V15)
2429 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2430 {
2431 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2432 MBEDTLS_MD_NONE );
2433 ret = mbedtls_rsa_pkcs1_verify( rsa,
2434 mbedtls_ctr_drbg_random,
2435 &global_data.ctr_drbg,
2436 MBEDTLS_RSA_PUBLIC,
2437 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002438 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002439 hash,
2440 signature );
2441 }
2442 else
2443#endif /* MBEDTLS_PKCS1_V15 */
2444#if defined(MBEDTLS_PKCS1_V21)
2445 if( PSA_ALG_IS_RSA_PSS( alg ) )
2446 {
2447 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2448 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2449 mbedtls_ctr_drbg_random,
2450 &global_data.ctr_drbg,
2451 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002452 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002453 (unsigned int) hash_length,
2454 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002455 signature );
2456 }
2457 else
2458#endif /* MBEDTLS_PKCS1_V21 */
2459 {
2460 return( PSA_ERROR_INVALID_ARGUMENT );
2461 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002462
2463 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2464 * the rest of the signature is invalid". This has little use in
2465 * practice and PSA doesn't report this distinction. */
2466 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2467 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002468 return( mbedtls_to_psa_error( ret ) );
2469}
2470#endif /* MBEDTLS_RSA_C */
2471
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002472#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002473/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2474 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2475 * (even though these functions don't modify it). */
2476static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2477 psa_algorithm_t alg,
2478 const uint8_t *hash,
2479 size_t hash_length,
2480 uint8_t *signature,
2481 size_t signature_size,
2482 size_t *signature_length )
2483{
2484 int ret;
2485 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002486 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002487 mbedtls_mpi_init( &r );
2488 mbedtls_mpi_init( &s );
2489
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002490 if( signature_size < 2 * curve_bytes )
2491 {
2492 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2493 goto cleanup;
2494 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002495
Gilles Peskinea05219c2018-11-16 16:02:56 +01002496#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002497 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2498 {
2499 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2500 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2501 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2502 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2503 hash, hash_length,
2504 md_alg ) );
2505 }
2506 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002507#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002508 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002509 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002510 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2511 hash, hash_length,
2512 mbedtls_ctr_drbg_random,
2513 &global_data.ctr_drbg ) );
2514 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002515
2516 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2517 signature,
2518 curve_bytes ) );
2519 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2520 signature + curve_bytes,
2521 curve_bytes ) );
2522
2523cleanup:
2524 mbedtls_mpi_free( &r );
2525 mbedtls_mpi_free( &s );
2526 if( ret == 0 )
2527 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002528 return( mbedtls_to_psa_error( ret ) );
2529}
2530
2531static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2532 const uint8_t *hash,
2533 size_t hash_length,
2534 const uint8_t *signature,
2535 size_t signature_length )
2536{
2537 int ret;
2538 mbedtls_mpi r, s;
2539 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2540 mbedtls_mpi_init( &r );
2541 mbedtls_mpi_init( &s );
2542
2543 if( signature_length != 2 * curve_bytes )
2544 return( PSA_ERROR_INVALID_SIGNATURE );
2545
2546 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2547 signature,
2548 curve_bytes ) );
2549 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2550 signature + curve_bytes,
2551 curve_bytes ) );
2552
2553 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2554 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002555
2556cleanup:
2557 mbedtls_mpi_free( &r );
2558 mbedtls_mpi_free( &s );
2559 return( mbedtls_to_psa_error( ret ) );
2560}
2561#endif /* MBEDTLS_ECDSA_C */
2562
Gilles Peskinec5487a82018-12-03 18:08:14 +01002563psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002564 psa_algorithm_t alg,
2565 const uint8_t *hash,
2566 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002567 uint8_t *signature,
2568 size_t signature_size,
2569 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002570{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002571 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002572 psa_status_t status;
2573
2574 *signature_length = signature_size;
2575
Gilles Peskinec5487a82018-12-03 18:08:14 +01002576 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002577 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002578 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002579 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002580 {
2581 status = PSA_ERROR_INVALID_ARGUMENT;
2582 goto exit;
2583 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002584
Gilles Peskine20035e32018-02-03 22:44:14 +01002585#if defined(MBEDTLS_RSA_C)
2586 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2587 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002588 status = psa_rsa_sign( slot->data.rsa,
2589 alg,
2590 hash, hash_length,
2591 signature, signature_size,
2592 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002593 }
2594 else
2595#endif /* defined(MBEDTLS_RSA_C) */
2596#if defined(MBEDTLS_ECP_C)
2597 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2598 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002599#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002600 if(
2601#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2602 PSA_ALG_IS_ECDSA( alg )
2603#else
2604 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2605#endif
2606 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002607 status = psa_ecdsa_sign( slot->data.ecp,
2608 alg,
2609 hash, hash_length,
2610 signature, signature_size,
2611 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002612 else
2613#endif /* defined(MBEDTLS_ECDSA_C) */
2614 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002615 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002616 }
itayzafrir5c753392018-05-08 11:18:38 +03002617 }
2618 else
2619#endif /* defined(MBEDTLS_ECP_C) */
2620 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002621 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002622 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002623
2624exit:
2625 /* Fill the unused part of the output buffer (the whole buffer on error,
2626 * the trailing part on success) with something that isn't a valid mac
2627 * (barring an attack on the mac and deliberately-crafted input),
2628 * in case the caller doesn't check the return status properly. */
2629 if( status == PSA_SUCCESS )
2630 memset( signature + *signature_length, '!',
2631 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002632 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002633 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002634 /* If signature_size is 0 then we have nothing to do. We must not call
2635 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002636 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002637}
2638
Gilles Peskinec5487a82018-12-03 18:08:14 +01002639psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002640 psa_algorithm_t alg,
2641 const uint8_t *hash,
2642 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002643 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002644 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002645{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002646 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002647 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002648
Gilles Peskinec5487a82018-12-03 18:08:14 +01002649 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002650 if( status != PSA_SUCCESS )
2651 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002652
Gilles Peskine61b91d42018-06-08 16:09:36 +02002653#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002654 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002655 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002656 return( psa_rsa_verify( slot->data.rsa,
2657 alg,
2658 hash, hash_length,
2659 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002660 }
2661 else
2662#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002663#if defined(MBEDTLS_ECP_C)
2664 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2665 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002666#if defined(MBEDTLS_ECDSA_C)
2667 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002668 return( psa_ecdsa_verify( slot->data.ecp,
2669 hash, hash_length,
2670 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002671 else
2672#endif /* defined(MBEDTLS_ECDSA_C) */
2673 {
2674 return( PSA_ERROR_INVALID_ARGUMENT );
2675 }
itayzafrir5c753392018-05-08 11:18:38 +03002676 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002677 else
2678#endif /* defined(MBEDTLS_ECP_C) */
2679 {
2680 return( PSA_ERROR_NOT_SUPPORTED );
2681 }
2682}
2683
Gilles Peskine072ac562018-06-30 00:21:29 +02002684#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2685static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2686 mbedtls_rsa_context *rsa )
2687{
2688 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2689 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2690 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2691 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2692}
2693#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2694
Gilles Peskinec5487a82018-12-03 18:08:14 +01002695psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002696 psa_algorithm_t alg,
2697 const uint8_t *input,
2698 size_t input_length,
2699 const uint8_t *salt,
2700 size_t salt_length,
2701 uint8_t *output,
2702 size_t output_size,
2703 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002705 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002706 psa_status_t status;
2707
Darryl Green5cc689a2018-07-24 15:34:10 +01002708 (void) input;
2709 (void) input_length;
2710 (void) salt;
2711 (void) output;
2712 (void) output_size;
2713
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002714 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002715
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002716 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2717 return( PSA_ERROR_INVALID_ARGUMENT );
2718
Gilles Peskinec5487a82018-12-03 18:08:14 +01002719 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002720 if( status != PSA_SUCCESS )
2721 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002722 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2723 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002724 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725
2726#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002727 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002728 {
2729 mbedtls_rsa_context *rsa = slot->data.rsa;
2730 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002731 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002732 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002733#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002734 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002736 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2737 mbedtls_ctr_drbg_random,
2738 &global_data.ctr_drbg,
2739 MBEDTLS_RSA_PUBLIC,
2740 input_length,
2741 input,
2742 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002743 }
2744 else
2745#endif /* MBEDTLS_PKCS1_V15 */
2746#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002747 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002748 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002749 psa_rsa_oaep_set_padding_mode( alg, rsa );
2750 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2751 mbedtls_ctr_drbg_random,
2752 &global_data.ctr_drbg,
2753 MBEDTLS_RSA_PUBLIC,
2754 salt, salt_length,
2755 input_length,
2756 input,
2757 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002758 }
2759 else
2760#endif /* MBEDTLS_PKCS1_V21 */
2761 {
2762 return( PSA_ERROR_INVALID_ARGUMENT );
2763 }
2764 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002765 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002766 return( mbedtls_to_psa_error( ret ) );
2767 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002768 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002769#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002770 {
2771 return( PSA_ERROR_NOT_SUPPORTED );
2772 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002773}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774
Gilles Peskinec5487a82018-12-03 18:08:14 +01002775psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002776 psa_algorithm_t alg,
2777 const uint8_t *input,
2778 size_t input_length,
2779 const uint8_t *salt,
2780 size_t salt_length,
2781 uint8_t *output,
2782 size_t output_size,
2783 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002785 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002786 psa_status_t status;
2787
Darryl Green5cc689a2018-07-24 15:34:10 +01002788 (void) input;
2789 (void) input_length;
2790 (void) salt;
2791 (void) output;
2792 (void) output_size;
2793
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002794 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002795
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002796 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2797 return( PSA_ERROR_INVALID_ARGUMENT );
2798
Gilles Peskinec5487a82018-12-03 18:08:14 +01002799 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002800 if( status != PSA_SUCCESS )
2801 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002802 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2803 return( PSA_ERROR_INVALID_ARGUMENT );
2804
2805#if defined(MBEDTLS_RSA_C)
2806 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2807 {
2808 mbedtls_rsa_context *rsa = slot->data.rsa;
2809 int ret;
2810
Gilles Peskine630a18a2018-06-29 17:49:35 +02002811 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002812 return( PSA_ERROR_INVALID_ARGUMENT );
2813
2814#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002815 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002816 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002817 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2818 mbedtls_ctr_drbg_random,
2819 &global_data.ctr_drbg,
2820 MBEDTLS_RSA_PRIVATE,
2821 output_length,
2822 input,
2823 output,
2824 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002825 }
2826 else
2827#endif /* MBEDTLS_PKCS1_V15 */
2828#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002829 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002830 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002831 psa_rsa_oaep_set_padding_mode( alg, rsa );
2832 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2833 mbedtls_ctr_drbg_random,
2834 &global_data.ctr_drbg,
2835 MBEDTLS_RSA_PRIVATE,
2836 salt, salt_length,
2837 output_length,
2838 input,
2839 output,
2840 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002841 }
2842 else
2843#endif /* MBEDTLS_PKCS1_V21 */
2844 {
2845 return( PSA_ERROR_INVALID_ARGUMENT );
2846 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002847
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002848 return( mbedtls_to_psa_error( ret ) );
2849 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002850 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002851#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002852 {
2853 return( PSA_ERROR_NOT_SUPPORTED );
2854 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002855}
Gilles Peskine20035e32018-02-03 22:44:14 +01002856
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002857
2858
mohammad1603503973b2018-03-12 15:59:30 +02002859/****************************************************************/
2860/* Symmetric cryptography */
2861/****************************************************************/
2862
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002863/* Initialize the cipher operation structure. Once this function has been
2864 * called, psa_cipher_abort can run and will do the right thing. */
2865static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2866 psa_algorithm_t alg )
2867{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002868 if( ! PSA_ALG_IS_CIPHER( alg ) )
2869 {
2870 memset( operation, 0, sizeof( *operation ) );
2871 return( PSA_ERROR_INVALID_ARGUMENT );
2872 }
2873
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002874 operation->alg = alg;
2875 operation->key_set = 0;
2876 operation->iv_set = 0;
2877 operation->iv_required = 1;
2878 operation->iv_size = 0;
2879 operation->block_size = 0;
2880 mbedtls_cipher_init( &operation->ctx.cipher );
2881 return( PSA_SUCCESS );
2882}
2883
Gilles Peskinee553c652018-06-04 16:22:46 +02002884static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002885 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002886 psa_algorithm_t alg,
2887 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002888{
2889 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2890 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002891 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002892 size_t key_bits;
2893 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002894 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2895 PSA_KEY_USAGE_ENCRYPT :
2896 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002897
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002898 status = psa_cipher_init( operation, alg );
2899 if( status != PSA_SUCCESS )
2900 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002901
Gilles Peskinec5487a82018-12-03 18:08:14 +01002902 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002903 if( status != PSA_SUCCESS )
2904 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002905 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002906
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002907 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002908 if( cipher_info == NULL )
2909 return( PSA_ERROR_NOT_SUPPORTED );
2910
mohammad1603503973b2018-03-12 15:59:30 +02002911 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002912 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002913 {
2914 psa_cipher_abort( operation );
2915 return( mbedtls_to_psa_error( ret ) );
2916 }
2917
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002918#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002919 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002920 {
2921 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2922 unsigned char keys[24];
2923 memcpy( keys, slot->data.raw.data, 16 );
2924 memcpy( keys + 16, slot->data.raw.data, 8 );
2925 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2926 keys,
2927 192, cipher_operation );
2928 }
2929 else
2930#endif
2931 {
2932 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2933 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002934 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002935 }
Moran Peker41deec42018-04-04 15:43:05 +03002936 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002937 {
2938 psa_cipher_abort( operation );
2939 return( mbedtls_to_psa_error( ret ) );
2940 }
2941
mohammad16038481e742018-03-18 13:57:31 +02002942#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002943 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002944 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002945 case PSA_ALG_CBC_NO_PADDING:
2946 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2947 MBEDTLS_PADDING_NONE );
2948 break;
2949 case PSA_ALG_CBC_PKCS7:
2950 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2951 MBEDTLS_PADDING_PKCS7 );
2952 break;
2953 default:
2954 /* The algorithm doesn't involve padding. */
2955 ret = 0;
2956 break;
2957 }
2958 if( ret != 0 )
2959 {
2960 psa_cipher_abort( operation );
2961 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002962 }
2963#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2964
mohammad1603503973b2018-03-12 15:59:30 +02002965 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002966 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2967 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2968 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002969 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002970 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002971 }
mohammad1603503973b2018-03-12 15:59:30 +02002972
Moran Peker395db872018-05-31 14:07:14 +03002973 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002974}
2975
Gilles Peskinefe119512018-07-08 21:39:34 +02002976psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002977 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002978 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002979{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002980 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002981}
2982
Gilles Peskinefe119512018-07-08 21:39:34 +02002983psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002984 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002985 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002986{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002987 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002988}
2989
Gilles Peskinefe119512018-07-08 21:39:34 +02002990psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2991 unsigned char *iv,
2992 size_t iv_size,
2993 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002994{
itayzafrir534bd7c2018-08-02 13:56:32 +03002995 psa_status_t status;
2996 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002997 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002998 {
2999 status = PSA_ERROR_BAD_STATE;
3000 goto exit;
3001 }
Moran Peker41deec42018-04-04 15:43:05 +03003002 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003003 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003004 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003005 goto exit;
3006 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003007 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3008 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003009 if( ret != 0 )
3010 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003011 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003012 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003013 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003014
mohammad16038481e742018-03-18 13:57:31 +02003015 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003016 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003017
Moran Peker395db872018-05-31 14:07:14 +03003018exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003019 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003020 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003021 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003022}
3023
Gilles Peskinefe119512018-07-08 21:39:34 +02003024psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3025 const unsigned char *iv,
3026 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003027{
itayzafrir534bd7c2018-08-02 13:56:32 +03003028 psa_status_t status;
3029 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003030 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003031 {
3032 status = PSA_ERROR_BAD_STATE;
3033 goto exit;
3034 }
Moran Pekera28258c2018-05-29 16:25:04 +03003035 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003036 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003037 status = PSA_ERROR_INVALID_ARGUMENT;
3038 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003039 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003040 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3041 status = mbedtls_to_psa_error( ret );
3042exit:
3043 if( status == PSA_SUCCESS )
3044 operation->iv_set = 1;
3045 else
mohammad1603503973b2018-03-12 15:59:30 +02003046 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003047 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003048}
3049
Gilles Peskinee553c652018-06-04 16:22:46 +02003050psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3051 const uint8_t *input,
3052 size_t input_length,
3053 unsigned char *output,
3054 size_t output_size,
3055 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003056{
itayzafrir534bd7c2018-08-02 13:56:32 +03003057 psa_status_t status;
3058 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003059 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003060 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003061 {
3062 /* Take the unprocessed partial block left over from previous
3063 * update calls, if any, plus the input to this call. Remove
3064 * the last partial block, if any. You get the data that will be
3065 * output in this call. */
3066 expected_output_size =
3067 ( operation->ctx.cipher.unprocessed_len + input_length )
3068 / operation->block_size * operation->block_size;
3069 }
3070 else
3071 {
3072 expected_output_size = input_length;
3073 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003074
Gilles Peskine89d789c2018-06-04 17:17:16 +02003075 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003076 {
3077 status = PSA_ERROR_BUFFER_TOO_SMALL;
3078 goto exit;
3079 }
mohammad160382759612018-03-12 18:16:40 +02003080
mohammad1603503973b2018-03-12 15:59:30 +02003081 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003082 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003083 status = mbedtls_to_psa_error( ret );
3084exit:
3085 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003086 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003087 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003088}
3089
Gilles Peskinee553c652018-06-04 16:22:46 +02003090psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3091 uint8_t *output,
3092 size_t output_size,
3093 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003094{
David Saadab4ecc272019-02-14 13:48:10 +02003095 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003096 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003097 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003098
mohammad1603503973b2018-03-12 15:59:30 +02003099 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003100 {
Janos Follath315b51c2018-07-09 16:04:51 +01003101 status = PSA_ERROR_BAD_STATE;
3102 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003103 }
3104 if( operation->iv_required && ! operation->iv_set )
3105 {
Janos Follath315b51c2018-07-09 16:04:51 +01003106 status = PSA_ERROR_BAD_STATE;
3107 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003108 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003109
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003110 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003111 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3112 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003113 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003114 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003115 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003116 }
3117
Janos Follath315b51c2018-07-09 16:04:51 +01003118 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3119 temp_output_buffer,
3120 output_length );
3121 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003122 {
Janos Follath315b51c2018-07-09 16:04:51 +01003123 status = mbedtls_to_psa_error( cipher_ret );
3124 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003125 }
Janos Follath315b51c2018-07-09 16:04:51 +01003126
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003127 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003128 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003129 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003130 memcpy( output, temp_output_buffer, *output_length );
3131 else
3132 {
Janos Follath315b51c2018-07-09 16:04:51 +01003133 status = PSA_ERROR_BUFFER_TOO_SMALL;
3134 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003135 }
mohammad1603503973b2018-03-12 15:59:30 +02003136
Gilles Peskine3f108122018-12-07 18:14:53 +01003137 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003138 status = psa_cipher_abort( operation );
3139
3140 return( status );
3141
3142error:
3143
3144 *output_length = 0;
3145
Gilles Peskine3f108122018-12-07 18:14:53 +01003146 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003147 (void) psa_cipher_abort( operation );
3148
3149 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003150}
3151
Gilles Peskinee553c652018-06-04 16:22:46 +02003152psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3153{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003154 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003155 {
3156 /* The object has (apparently) been initialized but it is not
3157 * in use. It's ok to call abort on such an object, and there's
3158 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003160 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003162 /* Sanity check (shouldn't happen: operation->alg should
3163 * always have been initialized to a valid value). */
3164 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3165 return( PSA_ERROR_BAD_STATE );
3166
mohammad1603503973b2018-03-12 15:59:30 +02003167 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003168
Moran Peker41deec42018-04-04 15:43:05 +03003169 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003170 operation->key_set = 0;
3171 operation->iv_set = 0;
3172 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003173 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003174 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003175
Moran Peker395db872018-05-31 14:07:14 +03003176 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003177}
3178
Gilles Peskinea0655c32018-04-30 17:06:50 +02003179
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003180
mohammad16038cc1cee2018-03-28 01:21:33 +03003181/****************************************************************/
3182/* Key Policy */
3183/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003184
mohammad160327010052018-07-03 13:16:15 +03003185#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003186void psa_key_policy_set_usage( psa_key_policy_t *policy,
3187 psa_key_usage_t usage,
3188 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003189{
mohammad16034eed7572018-03-28 05:14:59 -07003190 policy->usage = usage;
3191 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003192}
3193
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003194psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003195{
mohammad16036df908f2018-04-02 08:34:15 -07003196 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003197}
3198
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003199psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003200{
mohammad16036df908f2018-04-02 08:34:15 -07003201 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003202}
mohammad160327010052018-07-03 13:16:15 +03003203#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003204
Gilles Peskinec5487a82018-12-03 18:08:14 +01003205psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003206 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003207{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003208 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003209 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003210
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003211 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003213
Gilles Peskinec5487a82018-12-03 18:08:14 +01003214 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003215 if( status != PSA_SUCCESS )
3216 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003217
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003218 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3219 PSA_KEY_USAGE_ENCRYPT |
3220 PSA_KEY_USAGE_DECRYPT |
3221 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003222 PSA_KEY_USAGE_VERIFY |
3223 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003224 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003225
mohammad16036df908f2018-04-02 08:34:15 -07003226 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003227
3228 return( PSA_SUCCESS );
3229}
3230
Gilles Peskinec5487a82018-12-03 18:08:14 +01003231psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003232 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003233{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003234 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003235 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003236
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003237 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003238 return( PSA_ERROR_INVALID_ARGUMENT );
3239
Gilles Peskinec5487a82018-12-03 18:08:14 +01003240 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003241 if( status != PSA_SUCCESS )
3242 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003243
mohammad16036df908f2018-04-02 08:34:15 -07003244 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003245
3246 return( PSA_SUCCESS );
3247}
Gilles Peskine20035e32018-02-03 22:44:14 +01003248
Gilles Peskinea0655c32018-04-30 17:06:50 +02003249
3250
mohammad1603804cd712018-03-20 22:44:08 +02003251/****************************************************************/
3252/* Key Lifetime */
3253/****************************************************************/
3254
Gilles Peskinec5487a82018-12-03 18:08:14 +01003255psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003256 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003257{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003258 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003259 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003260
Gilles Peskinec5487a82018-12-03 18:08:14 +01003261 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003262 if( status != PSA_SUCCESS )
3263 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003264
mohammad1603804cd712018-03-20 22:44:08 +02003265 *lifetime = slot->lifetime;
3266
3267 return( PSA_SUCCESS );
3268}
3269
Gilles Peskine20035e32018-02-03 22:44:14 +01003270
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003271
mohammad16035955c982018-04-26 00:53:03 +03003272/****************************************************************/
3273/* AEAD */
3274/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003275
Gilles Peskineedf9a652018-08-17 18:11:56 +02003276typedef struct
3277{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003278 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003279 const mbedtls_cipher_info_t *cipher_info;
3280 union
3281 {
3282#if defined(MBEDTLS_CCM_C)
3283 mbedtls_ccm_context ccm;
3284#endif /* MBEDTLS_CCM_C */
3285#if defined(MBEDTLS_GCM_C)
3286 mbedtls_gcm_context gcm;
3287#endif /* MBEDTLS_GCM_C */
3288 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003289 psa_algorithm_t core_alg;
3290 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003291 uint8_t tag_length;
3292} aead_operation_t;
3293
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003294static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003295{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003296 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003297 {
3298#if defined(MBEDTLS_CCM_C)
3299 case PSA_ALG_CCM:
3300 mbedtls_ccm_free( &operation->ctx.ccm );
3301 break;
3302#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003303#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003304 case PSA_ALG_GCM:
3305 mbedtls_gcm_free( &operation->ctx.gcm );
3306 break;
3307#endif /* MBEDTLS_GCM_C */
3308 }
3309}
3310
3311static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003312 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003313 psa_key_usage_t usage,
3314 psa_algorithm_t alg )
3315{
3316 psa_status_t status;
3317 size_t key_bits;
3318 mbedtls_cipher_id_t cipher_id;
3319
Gilles Peskinec5487a82018-12-03 18:08:14 +01003320 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321 if( status != PSA_SUCCESS )
3322 return( status );
3323
3324 key_bits = psa_get_key_bits( operation->slot );
3325
3326 operation->cipher_info =
3327 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3328 &cipher_id );
3329 if( operation->cipher_info == NULL )
3330 return( PSA_ERROR_NOT_SUPPORTED );
3331
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003332 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003333 {
3334#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003335 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3336 operation->core_alg = PSA_ALG_CCM;
3337 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003338 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3339 return( PSA_ERROR_INVALID_ARGUMENT );
3340 mbedtls_ccm_init( &operation->ctx.ccm );
3341 status = mbedtls_to_psa_error(
3342 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3343 operation->slot->data.raw.data,
3344 (unsigned int) key_bits ) );
3345 if( status != 0 )
3346 goto cleanup;
3347 break;
3348#endif /* MBEDTLS_CCM_C */
3349
3350#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003351 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3352 operation->core_alg = PSA_ALG_GCM;
3353 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003354 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3355 return( PSA_ERROR_INVALID_ARGUMENT );
3356 mbedtls_gcm_init( &operation->ctx.gcm );
3357 status = mbedtls_to_psa_error(
3358 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3359 operation->slot->data.raw.data,
3360 (unsigned int) key_bits ) );
3361 break;
3362#endif /* MBEDTLS_GCM_C */
3363
3364 default:
3365 return( PSA_ERROR_NOT_SUPPORTED );
3366 }
3367
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003368 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3369 {
3370 status = PSA_ERROR_INVALID_ARGUMENT;
3371 goto cleanup;
3372 }
3373 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3374 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3375 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3376 * In both cases, mbedtls_xxx will validate the tag length below. */
3377
Gilles Peskineedf9a652018-08-17 18:11:56 +02003378 return( PSA_SUCCESS );
3379
3380cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003381 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003382 return( status );
3383}
3384
Gilles Peskinec5487a82018-12-03 18:08:14 +01003385psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003386 psa_algorithm_t alg,
3387 const uint8_t *nonce,
3388 size_t nonce_length,
3389 const uint8_t *additional_data,
3390 size_t additional_data_length,
3391 const uint8_t *plaintext,
3392 size_t plaintext_length,
3393 uint8_t *ciphertext,
3394 size_t ciphertext_size,
3395 size_t *ciphertext_length )
3396{
mohammad16035955c982018-04-26 00:53:03 +03003397 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003398 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003399 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003400
mohammad1603f08a5502018-06-03 15:05:47 +03003401 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003402
Gilles Peskinec5487a82018-12-03 18:08:14 +01003403 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003404 if( status != PSA_SUCCESS )
3405 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003406
Gilles Peskineedf9a652018-08-17 18:11:56 +02003407 /* For all currently supported modes, the tag is at the end of the
3408 * ciphertext. */
3409 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3410 {
3411 status = PSA_ERROR_BUFFER_TOO_SMALL;
3412 goto exit;
3413 }
3414 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003415
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003416#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003417 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003418 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003419 status = mbedtls_to_psa_error(
3420 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3421 MBEDTLS_GCM_ENCRYPT,
3422 plaintext_length,
3423 nonce, nonce_length,
3424 additional_data, additional_data_length,
3425 plaintext, ciphertext,
3426 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003427 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003428 else
3429#endif /* MBEDTLS_GCM_C */
3430#if defined(MBEDTLS_CCM_C)
3431 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003432 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003433 status = mbedtls_to_psa_error(
3434 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3435 plaintext_length,
3436 nonce, nonce_length,
3437 additional_data,
3438 additional_data_length,
3439 plaintext, ciphertext,
3440 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003441 }
mohammad16035c8845f2018-05-09 05:40:09 -07003442 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003443#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003444 {
mohammad1603554faad2018-06-03 15:07:38 +03003445 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003446 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003447
Gilles Peskineedf9a652018-08-17 18:11:56 +02003448 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3449 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003450
Gilles Peskineedf9a652018-08-17 18:11:56 +02003451exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003452 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003453 if( status == PSA_SUCCESS )
3454 *ciphertext_length = plaintext_length + operation.tag_length;
3455 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003456}
3457
Gilles Peskineee652a32018-06-01 19:23:52 +02003458/* Locate the tag in a ciphertext buffer containing the encrypted data
3459 * followed by the tag. Return the length of the part preceding the tag in
3460 * *plaintext_length. This is the size of the plaintext in modes where
3461 * the encrypted data has the same size as the plaintext, such as
3462 * CCM and GCM. */
3463static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3464 const uint8_t *ciphertext,
3465 size_t ciphertext_length,
3466 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003467 const uint8_t **p_tag )
3468{
3469 size_t payload_length;
3470 if( tag_length > ciphertext_length )
3471 return( PSA_ERROR_INVALID_ARGUMENT );
3472 payload_length = ciphertext_length - tag_length;
3473 if( payload_length > plaintext_size )
3474 return( PSA_ERROR_BUFFER_TOO_SMALL );
3475 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003476 return( PSA_SUCCESS );
3477}
3478
Gilles Peskinec5487a82018-12-03 18:08:14 +01003479psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003480 psa_algorithm_t alg,
3481 const uint8_t *nonce,
3482 size_t nonce_length,
3483 const uint8_t *additional_data,
3484 size_t additional_data_length,
3485 const uint8_t *ciphertext,
3486 size_t ciphertext_length,
3487 uint8_t *plaintext,
3488 size_t plaintext_size,
3489 size_t *plaintext_length )
3490{
mohammad16035955c982018-04-26 00:53:03 +03003491 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003492 aead_operation_t operation;
3493 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003494
Gilles Peskineee652a32018-06-01 19:23:52 +02003495 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003496
Gilles Peskinec5487a82018-12-03 18:08:14 +01003497 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003498 if( status != PSA_SUCCESS )
3499 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003500
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003501#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003502 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003503 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003504 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003505 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003506 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003507 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003508 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003509
Gilles Peskineedf9a652018-08-17 18:11:56 +02003510 status = mbedtls_to_psa_error(
3511 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3512 ciphertext_length - operation.tag_length,
3513 nonce, nonce_length,
3514 additional_data,
3515 additional_data_length,
3516 tag, operation.tag_length,
3517 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003518 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003519 else
3520#endif /* MBEDTLS_GCM_C */
3521#if defined(MBEDTLS_CCM_C)
3522 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003523 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003524 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003525 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003526 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003527 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003528 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003529
Gilles Peskineedf9a652018-08-17 18:11:56 +02003530 status = mbedtls_to_psa_error(
3531 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3532 ciphertext_length - operation.tag_length,
3533 nonce, nonce_length,
3534 additional_data,
3535 additional_data_length,
3536 ciphertext, plaintext,
3537 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003538 }
mohammad160339574652018-06-01 04:39:53 -07003539 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003540#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003541 {
mohammad1603554faad2018-06-03 15:07:38 +03003542 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003543 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003544
Gilles Peskineedf9a652018-08-17 18:11:56 +02003545 if( status != PSA_SUCCESS && plaintext_size != 0 )
3546 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003547
Gilles Peskineedf9a652018-08-17 18:11:56 +02003548exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003549 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003550 if( status == PSA_SUCCESS )
3551 *plaintext_length = ciphertext_length - operation.tag_length;
3552 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003553}
3554
Gilles Peskinea0655c32018-04-30 17:06:50 +02003555
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003556
Gilles Peskine20035e32018-02-03 22:44:14 +01003557/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003558/* Generators */
3559/****************************************************************/
3560
3561psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3562{
3563 psa_status_t status = PSA_SUCCESS;
3564 if( generator->alg == 0 )
3565 {
3566 /* The object has (apparently) been initialized but it is not
3567 * in use. It's ok to call abort on such an object, and there's
3568 * nothing to do. */
3569 }
3570 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003571 if( generator->alg == PSA_ALG_SELECT_RAW )
3572 {
3573 if( generator->ctx.buffer.data != NULL )
3574 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003575 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003576 generator->ctx.buffer.size );
3577 mbedtls_free( generator->ctx.buffer.data );
3578 }
3579 }
3580 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003581#if defined(MBEDTLS_MD_C)
3582 if( PSA_ALG_IS_HKDF( generator->alg ) )
3583 {
3584 mbedtls_free( generator->ctx.hkdf.info );
3585 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3586 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003587 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3588 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3589 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003590 {
3591 if( generator->ctx.tls12_prf.key != NULL )
3592 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003593 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003594 generator->ctx.tls12_prf.key_len );
3595 mbedtls_free( generator->ctx.tls12_prf.key );
3596 }
Hanno Becker580fba12018-11-13 20:50:45 +00003597
3598 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3599 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003600 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003601 generator->ctx.tls12_prf.Ai_with_seed_len );
3602 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3603 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003604 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003605 else
3606#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003607 {
3608 status = PSA_ERROR_BAD_STATE;
3609 }
3610 memset( generator, 0, sizeof( *generator ) );
3611 return( status );
3612}
3613
3614
3615psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3616 size_t *capacity)
3617{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003618 if( generator->alg == 0 )
3619 {
3620 /* This is a blank generator. */
3621 return PSA_ERROR_BAD_STATE;
3622 }
3623
Gilles Peskineeab56e42018-07-12 17:12:33 +02003624 *capacity = generator->capacity;
3625 return( PSA_SUCCESS );
3626}
3627
Gilles Peskinebef7f142018-07-12 17:22:21 +02003628#if defined(MBEDTLS_MD_C)
3629/* Read some bytes from an HKDF-based generator. This performs a chunk
3630 * of the expand phase of the HKDF algorithm. */
3631static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3632 psa_algorithm_t hash_alg,
3633 uint8_t *output,
3634 size_t output_length )
3635{
3636 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3637 psa_status_t status;
3638
3639 while( output_length != 0 )
3640 {
3641 /* Copy what remains of the current block */
3642 uint8_t n = hash_length - hkdf->offset_in_block;
3643 if( n > output_length )
3644 n = (uint8_t) output_length;
3645 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3646 output += n;
3647 output_length -= n;
3648 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003649 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003650 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003651 /* We can't be wanting more output after block 0xff, otherwise
3652 * the capacity check in psa_generator_read() would have
3653 * prevented this call. It could happen only if the generator
3654 * object was corrupted or if this function is called directly
3655 * inside the library. */
3656 if( hkdf->block_number == 0xff )
3657 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003658
3659 /* We need a new block */
3660 ++hkdf->block_number;
3661 hkdf->offset_in_block = 0;
3662 status = psa_hmac_setup_internal( &hkdf->hmac,
3663 hkdf->prk, hash_length,
3664 hash_alg );
3665 if( status != PSA_SUCCESS )
3666 return( status );
3667 if( hkdf->block_number != 1 )
3668 {
3669 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3670 hkdf->output_block,
3671 hash_length );
3672 if( status != PSA_SUCCESS )
3673 return( status );
3674 }
3675 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3676 hkdf->info,
3677 hkdf->info_length );
3678 if( status != PSA_SUCCESS )
3679 return( status );
3680 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3681 &hkdf->block_number, 1 );
3682 if( status != PSA_SUCCESS )
3683 return( status );
3684 status = psa_hmac_finish_internal( &hkdf->hmac,
3685 hkdf->output_block,
3686 sizeof( hkdf->output_block ) );
3687 if( status != PSA_SUCCESS )
3688 return( status );
3689 }
3690
3691 return( PSA_SUCCESS );
3692}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003693
3694static psa_status_t psa_generator_tls12_prf_generate_next_block(
3695 psa_tls12_prf_generator_t *tls12_prf,
3696 psa_algorithm_t alg )
3697{
3698 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3699 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3700 psa_hmac_internal_data hmac;
3701 psa_status_t status, cleanup_status;
3702
Hanno Becker3b339e22018-11-13 20:56:14 +00003703 unsigned char *Ai;
3704 size_t Ai_len;
3705
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003706 /* We can't be wanting more output after block 0xff, otherwise
3707 * the capacity check in psa_generator_read() would have
3708 * prevented this call. It could happen only if the generator
3709 * object was corrupted or if this function is called directly
3710 * inside the library. */
3711 if( tls12_prf->block_number == 0xff )
3712 return( PSA_ERROR_BAD_STATE );
3713
3714 /* We need a new block */
3715 ++tls12_prf->block_number;
3716 tls12_prf->offset_in_block = 0;
3717
3718 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3719 *
3720 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3721 *
3722 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3723 * HMAC_hash(secret, A(2) + seed) +
3724 * HMAC_hash(secret, A(3) + seed) + ...
3725 *
3726 * A(0) = seed
3727 * A(i) = HMAC_hash( secret, A(i-1) )
3728 *
3729 * The `psa_tls12_prf_generator` structures saves the block
3730 * `HMAC_hash(secret, A(i) + seed)` from which the output
3731 * is currently extracted as `output_block`, while
3732 * `A(i) + seed` is stored in `Ai_with_seed`.
3733 *
3734 * Generating a new block means recalculating `Ai_with_seed`
3735 * from the A(i)-part of it, and afterwards recalculating
3736 * `output_block`.
3737 *
3738 * A(0) is computed at setup time.
3739 *
3740 */
3741
3742 psa_hmac_init_internal( &hmac );
3743
3744 /* We must distinguish the calculation of A(1) from those
3745 * of A(2) and higher, because A(0)=seed has a different
3746 * length than the other A(i). */
3747 if( tls12_prf->block_number == 1 )
3748 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003749 Ai = tls12_prf->Ai_with_seed + hash_length;
3750 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003751 }
3752 else
3753 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003754 Ai = tls12_prf->Ai_with_seed;
3755 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003756 }
3757
Hanno Becker3b339e22018-11-13 20:56:14 +00003758 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3759 status = psa_hmac_setup_internal( &hmac,
3760 tls12_prf->key,
3761 tls12_prf->key_len,
3762 hash_alg );
3763 if( status != PSA_SUCCESS )
3764 goto cleanup;
3765
3766 status = psa_hash_update( &hmac.hash_ctx,
3767 Ai, Ai_len );
3768 if( status != PSA_SUCCESS )
3769 goto cleanup;
3770
3771 status = psa_hmac_finish_internal( &hmac,
3772 tls12_prf->Ai_with_seed,
3773 hash_length );
3774 if( status != PSA_SUCCESS )
3775 goto cleanup;
3776
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003777 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3778 status = psa_hmac_setup_internal( &hmac,
3779 tls12_prf->key,
3780 tls12_prf->key_len,
3781 hash_alg );
3782 if( status != PSA_SUCCESS )
3783 goto cleanup;
3784
3785 status = psa_hash_update( &hmac.hash_ctx,
3786 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003787 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003788 if( status != PSA_SUCCESS )
3789 goto cleanup;
3790
3791 status = psa_hmac_finish_internal( &hmac,
3792 tls12_prf->output_block,
3793 hash_length );
3794 if( status != PSA_SUCCESS )
3795 goto cleanup;
3796
3797cleanup:
3798
3799 cleanup_status = psa_hmac_abort_internal( &hmac );
3800 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3801 status = cleanup_status;
3802
3803 return( status );
3804}
3805
3806/* Read some bytes from an TLS-1.2-PRF-based generator.
3807 * See Section 5 of RFC 5246. */
3808static psa_status_t psa_generator_tls12_prf_read(
3809 psa_tls12_prf_generator_t *tls12_prf,
3810 psa_algorithm_t alg,
3811 uint8_t *output,
3812 size_t output_length )
3813{
3814 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3815 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3816 psa_status_t status;
3817
3818 while( output_length != 0 )
3819 {
3820 /* Copy what remains of the current block */
3821 uint8_t n = hash_length - tls12_prf->offset_in_block;
3822
3823 /* Check if we have fully processed the current block. */
3824 if( n == 0 )
3825 {
3826 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3827 alg );
3828 if( status != PSA_SUCCESS )
3829 return( status );
3830
3831 continue;
3832 }
3833
3834 if( n > output_length )
3835 n = (uint8_t) output_length;
3836 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3837 n );
3838 output += n;
3839 output_length -= n;
3840 tls12_prf->offset_in_block += n;
3841 }
3842
3843 return( PSA_SUCCESS );
3844}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003845#endif /* MBEDTLS_MD_C */
3846
Gilles Peskineeab56e42018-07-12 17:12:33 +02003847psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3848 uint8_t *output,
3849 size_t output_length )
3850{
3851 psa_status_t status;
3852
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003853 if( generator->alg == 0 )
3854 {
3855 /* This is a blank generator. */
3856 return PSA_ERROR_BAD_STATE;
3857 }
3858
Gilles Peskineeab56e42018-07-12 17:12:33 +02003859 if( output_length > generator->capacity )
3860 {
3861 generator->capacity = 0;
3862 /* Go through the error path to wipe all confidential data now
3863 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003864 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003865 goto exit;
3866 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003867 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003868 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003869 /* Edge case: this is a finished generator, and 0 bytes
3870 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003871 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3872 * INSUFFICIENT_CAPACITY, which is right for a finished
3873 * generator, for consistency with the case when
3874 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003875 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003876 }
3877 generator->capacity -= output_length;
3878
Gilles Peskine751d9652018-09-18 12:05:44 +02003879 if( generator->alg == PSA_ALG_SELECT_RAW )
3880 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003881 /* Initially, the capacity of a selection generator is always
3882 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3883 * abbreviated in this comment as `size`. When the remaining
3884 * capacity is `c`, the next bytes to serve start `c` bytes
3885 * from the end of the buffer, i.e. `size - c` from the
3886 * beginning of the buffer. Since `generator->capacity` was just
3887 * decremented above, we need to serve the bytes from
3888 * `size - generator->capacity - output_length` to
3889 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003890 size_t offset =
3891 generator->ctx.buffer.size - generator->capacity - output_length;
3892 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3893 status = PSA_SUCCESS;
3894 }
3895 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003896#if defined(MBEDTLS_MD_C)
3897 if( PSA_ALG_IS_HKDF( generator->alg ) )
3898 {
3899 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3900 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3901 output, output_length );
3902 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003903 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3904 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003905 {
3906 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3907 generator->alg, output,
3908 output_length );
3909 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003910 else
3911#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003912 {
3913 return( PSA_ERROR_BAD_STATE );
3914 }
3915
3916exit:
3917 if( status != PSA_SUCCESS )
3918 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003919 /* Preserve the algorithm upon errors, but clear all sensitive state.
3920 * This allows us to differentiate between exhausted generators and
3921 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3922 * generators. */
3923 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003924 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003925 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003926 memset( output, '!', output_length );
3927 }
3928 return( status );
3929}
3930
Gilles Peskine08542d82018-07-19 17:05:42 +02003931#if defined(MBEDTLS_DES_C)
3932static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3933{
3934 if( data_size >= 8 )
3935 mbedtls_des_key_set_parity( data );
3936 if( data_size >= 16 )
3937 mbedtls_des_key_set_parity( data + 8 );
3938 if( data_size >= 24 )
3939 mbedtls_des_key_set_parity( data + 16 );
3940}
3941#endif /* MBEDTLS_DES_C */
3942
Gilles Peskinec5487a82018-12-03 18:08:14 +01003943psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003944 psa_key_type_t type,
3945 size_t bits,
3946 psa_crypto_generator_t *generator )
3947{
3948 uint8_t *data = NULL;
3949 size_t bytes = PSA_BITS_TO_BYTES( bits );
3950 psa_status_t status;
3951
3952 if( ! key_type_is_raw_bytes( type ) )
3953 return( PSA_ERROR_INVALID_ARGUMENT );
3954 if( bits % 8 != 0 )
3955 return( PSA_ERROR_INVALID_ARGUMENT );
3956 data = mbedtls_calloc( 1, bytes );
3957 if( data == NULL )
3958 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3959
3960 status = psa_generator_read( generator, data, bytes );
3961 if( status != PSA_SUCCESS )
3962 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003963#if defined(MBEDTLS_DES_C)
3964 if( type == PSA_KEY_TYPE_DES )
3965 psa_des_set_key_parity( data, bytes );
3966#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003967 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003968
3969exit:
3970 mbedtls_free( data );
3971 return( status );
3972}
3973
3974
3975
3976/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003977/* Key derivation */
3978/****************************************************************/
3979
Gilles Peskinea05219c2018-11-16 16:02:56 +01003980#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003981/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003982 * of the HKDF algorithm.
3983 *
3984 * Note that if this function fails, you must call psa_generator_abort()
3985 * to potentially free embedded data structures and wipe confidential data.
3986 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003987static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003988 const uint8_t *secret,
3989 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003990 psa_algorithm_t hash_alg,
3991 const uint8_t *salt,
3992 size_t salt_length,
3993 const uint8_t *label,
3994 size_t label_length )
3995{
3996 psa_status_t status;
3997 status = psa_hmac_setup_internal( &hkdf->hmac,
3998 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003999 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004000 if( status != PSA_SUCCESS )
4001 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004002 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004003 if( status != PSA_SUCCESS )
4004 return( status );
4005 status = psa_hmac_finish_internal( &hkdf->hmac,
4006 hkdf->prk,
4007 sizeof( hkdf->prk ) );
4008 if( status != PSA_SUCCESS )
4009 return( status );
4010 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4011 hkdf->block_number = 0;
4012 hkdf->info_length = label_length;
4013 if( label_length != 0 )
4014 {
4015 hkdf->info = mbedtls_calloc( 1, label_length );
4016 if( hkdf->info == NULL )
4017 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4018 memcpy( hkdf->info, label, label_length );
4019 }
4020 return( PSA_SUCCESS );
4021}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004022#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004023
Gilles Peskinea05219c2018-11-16 16:02:56 +01004024#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004025/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4026 *
4027 * Note that if this function fails, you must call psa_generator_abort()
4028 * to potentially free embedded data structures and wipe confidential data.
4029 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004030static psa_status_t psa_generator_tls12_prf_setup(
4031 psa_tls12_prf_generator_t *tls12_prf,
4032 const unsigned char *key,
4033 size_t key_len,
4034 psa_algorithm_t hash_alg,
4035 const uint8_t *salt,
4036 size_t salt_length,
4037 const uint8_t *label,
4038 size_t label_length )
4039{
4040 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004041 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4042 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004043
4044 tls12_prf->key = mbedtls_calloc( 1, key_len );
4045 if( tls12_prf->key == NULL )
4046 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4047 tls12_prf->key_len = key_len;
4048 memcpy( tls12_prf->key, key, key_len );
4049
Hanno Becker580fba12018-11-13 20:50:45 +00004050 overflow = ( salt_length + label_length < salt_length ) ||
4051 ( salt_length + label_length + hash_length < hash_length );
4052 if( overflow )
4053 return( PSA_ERROR_INVALID_ARGUMENT );
4054
4055 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4056 if( tls12_prf->Ai_with_seed == NULL )
4057 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4058 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4059
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004060 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4061 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004062 if( label_length != 0 )
4063 {
4064 memcpy( tls12_prf->Ai_with_seed + hash_length,
4065 label, label_length );
4066 }
4067
4068 if( salt_length != 0 )
4069 {
4070 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4071 salt, salt_length );
4072 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004073
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004074 /* The first block gets generated when
4075 * psa_generator_read() is called. */
4076 tls12_prf->block_number = 0;
4077 tls12_prf->offset_in_block = hash_length;
4078
4079 return( PSA_SUCCESS );
4080}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004081
4082/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4083static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4084 psa_tls12_prf_generator_t *tls12_prf,
4085 const unsigned char *psk,
4086 size_t psk_len,
4087 psa_algorithm_t hash_alg,
4088 const uint8_t *salt,
4089 size_t salt_length,
4090 const uint8_t *label,
4091 size_t label_length )
4092{
4093 psa_status_t status;
4094 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4095
4096 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4097 return( PSA_ERROR_INVALID_ARGUMENT );
4098
4099 /* Quoting RFC 4279, Section 2:
4100 *
4101 * The premaster secret is formed as follows: if the PSK is N octets
4102 * long, concatenate a uint16 with the value N, N zero octets, a second
4103 * uint16 with the value N, and the PSK itself.
4104 */
4105
4106 pms[0] = ( psk_len >> 8 ) & 0xff;
4107 pms[1] = ( psk_len >> 0 ) & 0xff;
4108 memset( pms + 2, 0, psk_len );
4109 pms[2 + psk_len + 0] = pms[0];
4110 pms[2 + psk_len + 1] = pms[1];
4111 memcpy( pms + 4 + psk_len, psk, psk_len );
4112
4113 status = psa_generator_tls12_prf_setup( tls12_prf,
4114 pms, 4 + 2 * psk_len,
4115 hash_alg,
4116 salt, salt_length,
4117 label, label_length );
4118
Gilles Peskine3f108122018-12-07 18:14:53 +01004119 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004120 return( status );
4121}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004122#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004123
Gilles Peskine346797d2018-11-16 16:05:06 +01004124/* Note that if this function fails, you must call psa_generator_abort()
4125 * to potentially free embedded data structures and wipe confidential data.
4126 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004127static psa_status_t psa_key_derivation_internal(
4128 psa_crypto_generator_t *generator,
4129 const uint8_t *secret, size_t secret_length,
4130 psa_algorithm_t alg,
4131 const uint8_t *salt, size_t salt_length,
4132 const uint8_t *label, size_t label_length,
4133 size_t capacity )
4134{
4135 psa_status_t status;
4136 size_t max_capacity;
4137
4138 /* Set generator->alg even on failure so that abort knows what to do. */
4139 generator->alg = alg;
4140
Gilles Peskine751d9652018-09-18 12:05:44 +02004141 if( alg == PSA_ALG_SELECT_RAW )
4142 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004143 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004144 if( salt_length != 0 )
4145 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004146 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004147 if( label_length != 0 )
4148 return( PSA_ERROR_INVALID_ARGUMENT );
4149 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4150 if( generator->ctx.buffer.data == NULL )
4151 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4152 memcpy( generator->ctx.buffer.data, secret, secret_length );
4153 generator->ctx.buffer.size = secret_length;
4154 max_capacity = secret_length;
4155 status = PSA_SUCCESS;
4156 }
4157 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004158#if defined(MBEDTLS_MD_C)
4159 if( PSA_ALG_IS_HKDF( alg ) )
4160 {
4161 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4162 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4163 if( hash_size == 0 )
4164 return( PSA_ERROR_NOT_SUPPORTED );
4165 max_capacity = 255 * hash_size;
4166 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4167 secret, secret_length,
4168 hash_alg,
4169 salt, salt_length,
4170 label, label_length );
4171 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004172 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4173 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4174 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004175 {
4176 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4177 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4178
4179 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4180 if( hash_alg != PSA_ALG_SHA_256 &&
4181 hash_alg != PSA_ALG_SHA_384 )
4182 {
4183 return( PSA_ERROR_NOT_SUPPORTED );
4184 }
4185
4186 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004187
4188 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4189 {
4190 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4191 secret, secret_length,
4192 hash_alg, salt, salt_length,
4193 label, label_length );
4194 }
4195 else
4196 {
4197 status = psa_generator_tls12_psk_to_ms_setup(
4198 &generator->ctx.tls12_prf,
4199 secret, secret_length,
4200 hash_alg, salt, salt_length,
4201 label, label_length );
4202 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004203 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004204 else
4205#endif
4206 {
4207 return( PSA_ERROR_NOT_SUPPORTED );
4208 }
4209
4210 if( status != PSA_SUCCESS )
4211 return( status );
4212
4213 if( capacity <= max_capacity )
4214 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004215 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4216 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004217 else
4218 return( PSA_ERROR_INVALID_ARGUMENT );
4219
4220 return( PSA_SUCCESS );
4221}
4222
Gilles Peskineea0fb492018-07-12 17:17:20 +02004223psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004224 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004225 psa_algorithm_t alg,
4226 const uint8_t *salt,
4227 size_t salt_length,
4228 const uint8_t *label,
4229 size_t label_length,
4230 size_t capacity )
4231{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004232 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004233 psa_status_t status;
4234
4235 if( generator->alg != 0 )
4236 return( PSA_ERROR_BAD_STATE );
4237
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004238 /* Make sure that alg is a key derivation algorithm. This prevents
4239 * key selection algorithms, which psa_key_derivation_internal
4240 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004241 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4242 return( PSA_ERROR_INVALID_ARGUMENT );
4243
Gilles Peskinec5487a82018-12-03 18:08:14 +01004244 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004245 if( status != PSA_SUCCESS )
4246 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004247
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004248 if( slot->type != PSA_KEY_TYPE_DERIVE )
4249 return( PSA_ERROR_INVALID_ARGUMENT );
4250
4251 status = psa_key_derivation_internal( generator,
4252 slot->data.raw.data,
4253 slot->data.raw.bytes,
4254 alg,
4255 salt, salt_length,
4256 label, label_length,
4257 capacity );
4258 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004259 psa_generator_abort( generator );
4260 return( status );
4261}
4262
4263
4264
4265/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004266/* Key agreement */
4267/****************************************************************/
4268
Gilles Peskinea05219c2018-11-16 16:02:56 +01004269#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004270static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4271 size_t peer_key_length,
4272 const mbedtls_ecp_keypair *our_key,
4273 uint8_t *shared_secret,
4274 size_t shared_secret_size,
4275 size_t *shared_secret_length )
4276{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004277 mbedtls_ecp_keypair *their_key = NULL;
4278 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004279 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004280 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004281
Jaeden Ameroccdce902019-01-10 11:42:27 +00004282 status = psa_import_ec_public_key(
4283 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4284 peer_key, peer_key_length,
4285 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004286 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004287 goto exit;
4288
Jaeden Amero97271b32019-01-10 19:38:51 +00004289 status = mbedtls_to_psa_error(
4290 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4291 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004292 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004293 status = mbedtls_to_psa_error(
4294 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4295 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004296 goto exit;
4297
Jaeden Amero97271b32019-01-10 19:38:51 +00004298 status = mbedtls_to_psa_error(
4299 mbedtls_ecdh_calc_secret( &ecdh,
4300 shared_secret_length,
4301 shared_secret, shared_secret_size,
4302 mbedtls_ctr_drbg_random,
4303 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004304
4305exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004306 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004307 mbedtls_ecp_keypair_free( their_key );
4308 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004309 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004310}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004311#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004312
Gilles Peskine01d718c2018-09-18 12:01:02 +02004313#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4314
Gilles Peskine346797d2018-11-16 16:05:06 +01004315/* Note that if this function fails, you must call psa_generator_abort()
4316 * to potentially free embedded data structures and wipe confidential data.
4317 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004318static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004319 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004320 const uint8_t *peer_key,
4321 size_t peer_key_length,
4322 psa_algorithm_t alg )
4323{
4324 psa_status_t status;
4325 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4326 size_t shared_secret_length = 0;
4327
4328 /* Step 1: run the secret agreement algorithm to generate the shared
4329 * secret. */
4330 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4331 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004332#if defined(MBEDTLS_ECDH_C)
4333 case PSA_ALG_ECDH_BASE:
4334 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4335 return( PSA_ERROR_INVALID_ARGUMENT );
4336 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4337 private_key->data.ecp,
4338 shared_secret,
4339 sizeof( shared_secret ),
4340 &shared_secret_length );
4341 break;
4342#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004343 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004344 (void) private_key;
4345 (void) peer_key;
4346 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004347 return( PSA_ERROR_NOT_SUPPORTED );
4348 }
4349 if( status != PSA_SUCCESS )
4350 goto exit;
4351
4352 /* Step 2: set up the key derivation to generate key material from
4353 * the shared secret. */
4354 status = psa_key_derivation_internal( generator,
4355 shared_secret, shared_secret_length,
4356 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4357 NULL, 0, NULL, 0,
4358 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4359exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004360 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004361 return( status );
4362}
4363
4364psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004365 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004366 const uint8_t *peer_key,
4367 size_t peer_key_length,
4368 psa_algorithm_t alg )
4369{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004370 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004371 psa_status_t status;
4372 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4373 return( PSA_ERROR_INVALID_ARGUMENT );
4374 status = psa_get_key_from_slot( private_key, &slot,
4375 PSA_KEY_USAGE_DERIVE, alg );
4376 if( status != PSA_SUCCESS )
4377 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004378 status = psa_key_agreement_internal( generator,
4379 slot,
4380 peer_key, peer_key_length,
4381 alg );
4382 if( status != PSA_SUCCESS )
4383 psa_generator_abort( generator );
4384 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004385}
4386
4387
4388
4389/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004390/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004391/****************************************************************/
4392
4393psa_status_t psa_generate_random( uint8_t *output,
4394 size_t output_size )
4395{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004396 int ret;
4397 GUARD_MODULE_INITIALIZED;
4398
4399 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004400 return( mbedtls_to_psa_error( ret ) );
4401}
4402
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004403#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004404
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004405psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4406 size_t seed_size )
4407{
4408 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +02004409 struct psa_storage_info_t p_info;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004410 if( global_data.initialized )
4411 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004412
4413 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4414 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4415 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4416 return( PSA_ERROR_INVALID_ARGUMENT );
4417
David Saadaa2523b22019-02-18 13:56:26 +02004418 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004419
David Saadaa2523b22019-02-18 13:56:26 +02004420 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004421 {
David Saadaa2523b22019-02-18 13:56:26 +02004422 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004423 }
David Saadaa2523b22019-02-18 13:56:26 +02004424 else if( PSA_SUCCESS == status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004425 {
4426 /* You should not be here. Seed needs to be injected only once */
4427 status = PSA_ERROR_NOT_PERMITTED;
4428 }
4429 return( status );
4430}
4431#endif
4432
Gilles Peskinec5487a82018-12-03 18:08:14 +01004433psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004434 psa_key_type_t type,
4435 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004436 const void *extra,
4437 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004438{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004439 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004440 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004441
Gilles Peskine53d991e2018-07-12 01:14:59 +02004442 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004443 return( PSA_ERROR_INVALID_ARGUMENT );
4444
Gilles Peskinec5487a82018-12-03 18:08:14 +01004445 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004446 if( status != PSA_SUCCESS )
4447 return( status );
4448
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004449 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004450 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004451 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004452 if( status != PSA_SUCCESS )
4453 return( status );
4454 status = psa_generate_random( slot->data.raw.data,
4455 slot->data.raw.bytes );
4456 if( status != PSA_SUCCESS )
4457 {
4458 mbedtls_free( slot->data.raw.data );
4459 return( status );
4460 }
4461#if defined(MBEDTLS_DES_C)
4462 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004463 psa_des_set_key_parity( slot->data.raw.data,
4464 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004465#endif /* MBEDTLS_DES_C */
4466 }
4467 else
4468
4469#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4470 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4471 {
4472 mbedtls_rsa_context *rsa;
4473 int ret;
4474 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004475 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4476 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004477 /* Accept only byte-aligned keys, for the same reasons as
4478 * in psa_import_rsa_key(). */
4479 if( bits % 8 != 0 )
4480 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004481 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004482 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004483 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004484 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004485 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004486#if INT_MAX < 0xffffffff
4487 /* Check that the uint32_t value passed by the caller fits
4488 * in the range supported by this implementation. */
4489 if( p->e > INT_MAX )
4490 return( PSA_ERROR_NOT_SUPPORTED );
4491#endif
4492 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004493 }
4494 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4495 if( rsa == NULL )
4496 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4497 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4498 ret = mbedtls_rsa_gen_key( rsa,
4499 mbedtls_ctr_drbg_random,
4500 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004501 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004502 exponent );
4503 if( ret != 0 )
4504 {
4505 mbedtls_rsa_free( rsa );
4506 mbedtls_free( rsa );
4507 return( mbedtls_to_psa_error( ret ) );
4508 }
4509 slot->data.rsa = rsa;
4510 }
4511 else
4512#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4513
4514#if defined(MBEDTLS_ECP_C)
4515 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4516 {
4517 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4518 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4519 const mbedtls_ecp_curve_info *curve_info =
4520 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4521 mbedtls_ecp_keypair *ecp;
4522 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004523 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004524 return( PSA_ERROR_NOT_SUPPORTED );
4525 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4526 return( PSA_ERROR_NOT_SUPPORTED );
4527 if( curve_info->bit_size != bits )
4528 return( PSA_ERROR_INVALID_ARGUMENT );
4529 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4530 if( ecp == NULL )
4531 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4532 mbedtls_ecp_keypair_init( ecp );
4533 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4534 mbedtls_ctr_drbg_random,
4535 &global_data.ctr_drbg );
4536 if( ret != 0 )
4537 {
4538 mbedtls_ecp_keypair_free( ecp );
4539 mbedtls_free( ecp );
4540 return( mbedtls_to_psa_error( ret ) );
4541 }
4542 slot->data.ecp = ecp;
4543 }
4544 else
4545#endif /* MBEDTLS_ECP_C */
4546
4547 return( PSA_ERROR_NOT_SUPPORTED );
4548
4549 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004550
4551#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4552 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4553 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004554 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004555 }
4556#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4557
4558 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004559}
4560
4561
4562/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004563/* Module setup */
4564/****************************************************************/
4565
Gilles Peskine5e769522018-11-20 21:59:56 +01004566psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4567 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4568 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4569{
4570 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4571 return( PSA_ERROR_BAD_STATE );
4572 global_data.entropy_init = entropy_init;
4573 global_data.entropy_free = entropy_free;
4574 return( PSA_SUCCESS );
4575}
4576
Gilles Peskinee59236f2018-01-27 23:32:46 +01004577void mbedtls_psa_crypto_free( void )
4578{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004579 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004580 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4581 {
4582 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004583 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004584 }
4585 /* Wipe all remaining data, including configuration.
4586 * In particular, this sets all state indicator to the value
4587 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004588 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004589}
4590
4591psa_status_t psa_crypto_init( void )
4592{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004593 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004594 const unsigned char drbg_seed[] = "PSA";
4595
Gilles Peskinec6b69072018-11-20 21:42:52 +01004596 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004597 if( global_data.initialized != 0 )
4598 return( PSA_SUCCESS );
4599
Gilles Peskine5e769522018-11-20 21:59:56 +01004600 /* Set default configuration if
4601 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4602 if( global_data.entropy_init == NULL )
4603 global_data.entropy_init = mbedtls_entropy_init;
4604 if( global_data.entropy_free == NULL )
4605 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004606
Gilles Peskinec6b69072018-11-20 21:42:52 +01004607 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004608 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004609 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004610 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004611 status = mbedtls_to_psa_error(
4612 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4613 mbedtls_entropy_func,
4614 &global_data.entropy,
4615 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4616 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004617 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004618 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004619
Gilles Peskine66fb1262018-12-10 16:29:04 +01004620 status = psa_initialize_key_slots( );
4621 if( status != PSA_SUCCESS )
4622 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004623
4624 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004625 global_data.initialized = 1;
4626
Gilles Peskinee59236f2018-01-27 23:32:46 +01004627exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004628 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004629 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004630 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004631}
4632
4633#endif /* MBEDTLS_PSA_CRYPTO_C */