blob: 3b9c78ffc7f2b38ed05408161c9027262fc4f093 [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"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/sha1.h"
76#include "mbedtls/sha256.h"
77#include "mbedtls/sha512.h"
78#include "mbedtls/xtea.h"
79
Gilles Peskine996deb12018-08-01 15:45:45 +020080#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
81
Gilles Peskine9ef733f2018-02-07 21:05:37 +010082/* constant-time buffer comparison */
83static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
84{
85 size_t i;
86 unsigned char diff = 0;
87
88 for( i = 0; i < n; i++ )
89 diff |= a[i] ^ b[i];
90
91 return( diff );
92}
93
94
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/****************************************************************/
97/* Global data, support functions and library management */
98/****************************************************************/
99
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100static int key_type_is_raw_bytes( psa_key_type_t type )
101{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200102 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103}
104
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100105/* Values for psa_global_data_t::rng_state */
106#define RNG_NOT_INITIALIZED 0
107#define RNG_INITIALIZED 1
108#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109
Gilles Peskine2d277862018-06-18 15:41:12 +0200110typedef struct
111{
Gilles Peskine5e769522018-11-20 21:59:56 +0100112 void (* entropy_init )( mbedtls_entropy_context *ctx );
113 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
123 if( global_data.initialized == 0 ) \
124 return( PSA_ERROR_BAD_STATE );
125
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126static psa_status_t mbedtls_to_psa_error( int ret )
127{
Gilles Peskinea5905292018-02-07 20:59:33 +0100128 /* If there's both a high-level code and low-level code, dispatch on
129 * the high-level code. */
130 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 {
132 case 0:
133 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
137 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
138 return( PSA_ERROR_NOT_SUPPORTED );
139 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
140 return( PSA_ERROR_HARDWARE_FAILURE );
141
142 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
150 return( PSA_ERROR_INVALID_ARGUMENT );
151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
152 return( PSA_ERROR_INSUFFICIENT_MEMORY );
153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
154 return( PSA_ERROR_BUFFER_TOO_SMALL );
155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
160#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
162 return( PSA_ERROR_NOT_SUPPORTED );
163 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
169 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
179 return( PSA_ERROR_INVALID_SIGNATURE );
180 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
189 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
190 return( PSA_ERROR_INVALID_PADDING );
191 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
196 return( PSA_ERROR_TAMPERING_DETECTED );
197 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210
211 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
222 return( PSA_ERROR_INVALID_SIGNATURE );
223 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_MD_ALLOC_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
239 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
240 return( PSA_ERROR_STORAGE_FAILURE );
241 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
251 return( PSA_ERROR_BUFFER_TOO_SMALL );
252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
David Saadab4ecc272019-02-14 13:48:10 +0200338 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200343
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345/****************************************************************/
346/* Key management */
347/****************************************************************/
348
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100349#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200350static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
351{
352 switch( grpid )
353 {
354 case MBEDTLS_ECP_DP_SECP192R1:
355 return( PSA_ECC_CURVE_SECP192R1 );
356 case MBEDTLS_ECP_DP_SECP224R1:
357 return( PSA_ECC_CURVE_SECP224R1 );
358 case MBEDTLS_ECP_DP_SECP256R1:
359 return( PSA_ECC_CURVE_SECP256R1 );
360 case MBEDTLS_ECP_DP_SECP384R1:
361 return( PSA_ECC_CURVE_SECP384R1 );
362 case MBEDTLS_ECP_DP_SECP521R1:
363 return( PSA_ECC_CURVE_SECP521R1 );
364 case MBEDTLS_ECP_DP_BP256R1:
365 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
366 case MBEDTLS_ECP_DP_BP384R1:
367 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
368 case MBEDTLS_ECP_DP_BP512R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
370 case MBEDTLS_ECP_DP_CURVE25519:
371 return( PSA_ECC_CURVE_CURVE25519 );
372 case MBEDTLS_ECP_DP_SECP192K1:
373 return( PSA_ECC_CURVE_SECP192K1 );
374 case MBEDTLS_ECP_DP_SECP224K1:
375 return( PSA_ECC_CURVE_SECP224K1 );
376 case MBEDTLS_ECP_DP_SECP256K1:
377 return( PSA_ECC_CURVE_SECP256K1 );
378 case MBEDTLS_ECP_DP_CURVE448:
379 return( PSA_ECC_CURVE_CURVE448 );
380 default:
381 return( 0 );
382 }
383}
384
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
386{
387 switch( curve )
388 {
389 case PSA_ECC_CURVE_SECP192R1:
390 return( MBEDTLS_ECP_DP_SECP192R1 );
391 case PSA_ECC_CURVE_SECP224R1:
392 return( MBEDTLS_ECP_DP_SECP224R1 );
393 case PSA_ECC_CURVE_SECP256R1:
394 return( MBEDTLS_ECP_DP_SECP256R1 );
395 case PSA_ECC_CURVE_SECP384R1:
396 return( MBEDTLS_ECP_DP_SECP384R1 );
397 case PSA_ECC_CURVE_SECP521R1:
398 return( MBEDTLS_ECP_DP_SECP521R1 );
399 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 case PSA_ECC_CURVE_CURVE25519:
406 return( MBEDTLS_ECP_DP_CURVE25519 );
407 case PSA_ECC_CURVE_SECP192K1:
408 return( MBEDTLS_ECP_DP_SECP192K1 );
409 case PSA_ECC_CURVE_SECP224K1:
410 return( MBEDTLS_ECP_DP_SECP224K1 );
411 case PSA_ECC_CURVE_SECP256K1:
412 return( MBEDTLS_ECP_DP_SECP256K1 );
413 case PSA_ECC_CURVE_CURVE448:
414 return( MBEDTLS_ECP_DP_CURVE448 );
415 default:
416 return( MBEDTLS_ECP_DP_NONE );
417 }
418}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200421static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
422 size_t bits,
423 struct raw_data *raw )
424{
425 /* Check that the bit size is acceptable for the key type */
426 switch( type )
427 {
428 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200429 if( bits == 0 )
430 {
431 raw->bytes = 0;
432 raw->data = NULL;
433 return( PSA_SUCCESS );
434 }
435 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436#if defined(MBEDTLS_MD_C)
437 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200439 case PSA_KEY_TYPE_DERIVE:
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_AES_C)
442 case PSA_KEY_TYPE_AES:
443 if( bits != 128 && bits != 192 && bits != 256 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
445 break;
446#endif
447#if defined(MBEDTLS_CAMELLIA_C)
448 case PSA_KEY_TYPE_CAMELLIA:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
453#if defined(MBEDTLS_DES_C)
454 case PSA_KEY_TYPE_DES:
455 if( bits != 64 && bits != 128 && bits != 192 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
459#if defined(MBEDTLS_ARC4_C)
460 case PSA_KEY_TYPE_ARC4:
461 if( bits < 8 || bits > 2048 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465 default:
466 return( PSA_ERROR_NOT_SUPPORTED );
467 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200468 if( bits % 8 != 0 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470
471 /* Allocate memory for the key */
472 raw->bytes = PSA_BITS_TO_BYTES( bits );
473 raw->data = mbedtls_calloc( 1, raw->bytes );
474 if( raw->data == NULL )
475 {
476 raw->bytes = 0;
477 return( PSA_ERROR_INSUFFICIENT_MEMORY );
478 }
479 return( PSA_SUCCESS );
480}
481
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100483/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
484 * that are not a multiple of 8) well. For example, there is only
485 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
486 * way to return the exact bit size of a key.
487 * To keep things simple, reject non-byte-aligned key sizes. */
488static psa_status_t psa_check_rsa_key_byte_aligned(
489 const mbedtls_rsa_context *rsa )
490{
491 mbedtls_mpi n;
492 psa_status_t status;
493 mbedtls_mpi_init( &n );
494 status = mbedtls_to_psa_error(
495 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
496 if( status == PSA_SUCCESS )
497 {
498 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
499 status = PSA_ERROR_NOT_SUPPORTED;
500 }
501 mbedtls_mpi_free( &n );
502 return( status );
503}
504
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000505static psa_status_t psa_import_rsa_key( psa_key_type_t type,
506 const uint8_t *data,
507 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508 mbedtls_rsa_context **p_rsa )
509{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000510 psa_status_t status;
511 mbedtls_pk_context pk;
512 mbedtls_rsa_context *rsa;
513 size_t bits;
514
515 mbedtls_pk_init( &pk );
516
517 /* Parse the data. */
518 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000522 status = mbedtls_to_psa_error(
523 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
524 if( status != PSA_SUCCESS )
525 goto exit;
526
527 /* We have something that the pkparse module recognizes. If it is a
528 * valid RSA key, store it. */
529 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000531 status = PSA_ERROR_INVALID_ARGUMENT;
532 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534
535 rsa = mbedtls_pk_rsa( pk );
536 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
537 * supports non-byte-aligned key sizes, but not well. For example,
538 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
539 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
540 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
541 {
542 status = PSA_ERROR_NOT_SUPPORTED;
543 goto exit;
544 }
545 status = psa_check_rsa_key_byte_aligned( rsa );
546
547exit:
548 /* Free the content of the pk object only on error. */
549 if( status != PSA_SUCCESS )
550 {
551 mbedtls_pk_free( &pk );
552 return( status );
553 }
554
555 /* On success, store the content of the object in the RSA context. */
556 *p_rsa = rsa;
557
558 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
Jaeden Ameroccdce902019-01-10 11:42:27 +0000562#if defined(MBEDTLS_ECP_C)
563
564/* Import a public key given as the uncompressed representation defined by SEC1
565 * 2.3.3 as the content of an ECPoint. */
566static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
567 const uint8_t *data,
568 size_t data_length,
569 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000571 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
572 mbedtls_ecp_keypair *ecp = NULL;
573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
574
575 *p_ecp = NULL;
576 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
577 if( ecp == NULL )
578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
579 mbedtls_ecp_keypair_init( ecp );
580
581 /* Load the group. */
582 status = mbedtls_to_psa_error(
583 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Load the public value. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
589 data, data_length ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* Check that the point is on the curve. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000608 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000610#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612#if defined(MBEDTLS_ECP_C)
613/* Import a private key given as a byte string which is the private value
614 * in big-endian order. */
615static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
616 const uint8_t *data,
617 size_t data_length,
618 mbedtls_ecp_keypair **p_ecp )
619{
620 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
621 mbedtls_ecp_keypair *ecp = NULL;
622 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
623
624 *p_ecp = NULL;
625 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
626 if( ecp == NULL )
627 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000628 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629
630 /* Load the group. */
631 status = mbedtls_to_psa_error(
632 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
633 if( status != PSA_SUCCESS )
634 goto exit;
635 /* Load the secret value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Validate the private key. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Calculate the public key from the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
648 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651
652 *p_ecp = ecp;
653 return( PSA_SUCCESS );
654
655exit:
656 if( ecp != NULL )
657 {
658 mbedtls_ecp_keypair_free( ecp );
659 mbedtls_free( ecp );
660 }
661 return( status );
662}
663#endif /* defined(MBEDTLS_ECP_C) */
664
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100665/** Import key data into a slot. `slot->type` must have been set
666 * previously. This function assumes that the slot does not contain
667 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100668psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Darryl Green940d72c2018-07-13 13:18:51 +0100674 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100679 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100690 {
Darryl Green940d72c2018-07-13 13:18:51 +0100691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 data, data_length,
693 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000695 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000697 status = psa_import_ec_public_key(
698 PSA_KEY_TYPE_GET_CURVE( slot->type ),
699 data, data_length,
700 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 }
702 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100703#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
705 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000707 status = psa_import_rsa_key( slot->type,
708 data, data_length,
709 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000712#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 {
714 return( PSA_ERROR_NOT_SUPPORTED );
715 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000716 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100717}
718
Darryl Green06fd18d2018-07-16 11:21:11 +0100719/* Retrieve an empty key slot (slot with no key data, but possibly
720 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100721static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
724 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100726
727 *p_slot = NULL;
728
Gilles Peskinec5487a82018-12-03 18:08:14 +0100729 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100730 if( status != PSA_SUCCESS )
731 return( status );
732
733 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200734 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735
736 *p_slot = slot;
737 return( status );
738}
739
Gilles Peskinef603c712019-01-19 13:40:11 +0100740/** Calculate the intersection of two algorithm usage policies.
741 *
742 * Return 0 (which allows no operation) on incompatibility.
743 */
744static psa_algorithm_t psa_key_policy_algorithm_intersection(
745 psa_algorithm_t alg1,
746 psa_algorithm_t alg2 )
747{
748 /* Common case: the policy only allows alg. */
749 if( alg1 == alg2 )
750 return( alg1 );
751 /* If the policies are from the same hash-and-sign family, check
752 * if one is a wildcard. If so the other has the specific algorithm. */
753 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
754 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
755 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
756 {
757 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
758 return( alg2 );
759 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
760 return( alg1 );
761 }
762 /* If the policies are incompatible, allow nothing. */
763 return( 0 );
764}
765
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100766/** Test whether a policy permits an algorithm.
767 *
768 * The caller must test usage flags separately.
769 */
770static int psa_key_policy_permits( const psa_key_policy_t *policy,
771 psa_algorithm_t alg )
772{
773 /* Common case: the policy only allows alg. */
774 if( alg == policy->alg )
775 return( 1 );
776 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
777 * and alg is the same hash-and-sign family with any hash,
778 * then alg is compliant with policy->alg. */
779 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
780 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
781 {
782 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
783 ( alg & ~PSA_ALG_HASH_MASK ) );
784 }
785 /* If it isn't permitted, it's forbidden. */
786 return( 0 );
787}
788
Gilles Peskinef603c712019-01-19 13:40:11 +0100789/** Restrict a key policy based on a constraint.
790 *
791 * \param[in,out] policy The policy to restrict.
792 * \param[in] constraint The policy constraint to apply.
793 *
794 * \retval #PSA_SUCCESS
795 * \c *policy contains the intersection of the original value of
796 * \c *policy and \c *constraint.
797 * \retval #PSA_ERROR_INVALID_ARGUMENT
798 * \c *policy and \c *constraint are incompatible.
799 * \c *policy is unchanged.
800 */
801static psa_status_t psa_restrict_key_policy(
802 psa_key_policy_t *policy,
803 const psa_key_policy_t *constraint )
804{
805 psa_algorithm_t intersection_alg =
806 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
807 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
808 return( PSA_ERROR_INVALID_ARGUMENT );
809 policy->usage &= constraint->usage;
810 policy->alg = intersection_alg;
811 return( PSA_SUCCESS );
812}
813
Darryl Green06fd18d2018-07-16 11:21:11 +0100814/** Retrieve a slot which must contain a key. The key must have allow all the
815 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
816 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100817static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100818 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100819 psa_key_usage_t usage,
820 psa_algorithm_t alg )
821{
822 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100824
825 *p_slot = NULL;
826
Gilles Peskinec5487a82018-12-03 18:08:14 +0100827 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100828 if( status != PSA_SUCCESS )
829 return( status );
830 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200831 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100832
833 /* Enforce that usage policy for the key slot contains all the flags
834 * required by the usage parameter. There is one exception: public
835 * keys can always be exported, so we treat public key objects as
836 * if they had the export flag. */
837 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
838 usage &= ~PSA_KEY_USAGE_EXPORT;
839 if( ( slot->policy.usage & usage ) != usage )
840 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100841
842 /* Enforce that the usage policy permits the requested algortihm. */
843 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100844 return( PSA_ERROR_NOT_PERMITTED );
845
846 *p_slot = slot;
847 return( PSA_SUCCESS );
848}
Darryl Green940d72c2018-07-13 13:18:51 +0100849
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100851static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000852{
853 if( slot->type == PSA_KEY_TYPE_NONE )
854 {
855 /* No key material to clean. */
856 }
857 else if( key_type_is_raw_bytes( slot->type ) )
858 {
859 mbedtls_free( slot->data.raw.data );
860 }
861 else
862#if defined(MBEDTLS_RSA_C)
863 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
864 {
865 mbedtls_rsa_free( slot->data.rsa );
866 mbedtls_free( slot->data.rsa );
867 }
868 else
869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 {
873 mbedtls_ecp_keypair_free( slot->data.ecp );
874 mbedtls_free( slot->data.ecp );
875 }
876 else
877#endif /* defined(MBEDTLS_ECP_C) */
878 {
879 /* Shouldn't happen: the key type is not any type that we
880 * put in. */
881 return( PSA_ERROR_TAMPERING_DETECTED );
882 }
883
884 return( PSA_SUCCESS );
885}
886
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100887/** Completely wipe a slot in memory, including its policy.
888 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100889psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100890{
891 psa_status_t status = psa_remove_key_data_from_memory( slot );
892 /* At this point, key material and other type-specific content has
893 * been wiped. Clear remaining metadata. We can call memset and not
894 * zeroize because the metadata is not particularly sensitive. */
895 memset( slot, 0, sizeof( *slot ) );
896 return( status );
897}
898
Gilles Peskinec5487a82018-12-03 18:08:14 +0100899psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100900 psa_key_type_t type,
901 const uint8_t *data,
902 size_t data_length )
903{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100904 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100905 psa_status_t status;
906
Gilles Peskinec5487a82018-12-03 18:08:14 +0100907 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100908 if( status != PSA_SUCCESS )
909 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100910
911 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100912
913 status = psa_import_key_into_slot( slot, data, data_length );
914 if( status != PSA_SUCCESS )
915 {
916 slot->type = PSA_KEY_TYPE_NONE;
917 return( status );
918 }
919
Darryl Greend49a4992018-06-18 17:27:26 +0100920#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
921 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
922 {
923 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100924 status = psa_save_persistent_key( slot->persistent_storage_id,
925 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100926 data_length );
927 if( status != PSA_SUCCESS )
928 {
929 (void) psa_remove_key_data_from_memory( slot );
930 slot->type = PSA_KEY_TYPE_NONE;
931 }
932 }
933#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
934
935 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936}
937
Gilles Peskinec5487a82018-12-03 18:08:14 +0100938psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100940 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100941 psa_status_t status = PSA_SUCCESS;
942 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943
Gilles Peskinec5487a82018-12-03 18:08:14 +0100944 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 if( status != PSA_SUCCESS )
946 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100947#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
948 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
949 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100950 storage_status =
951 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100952 }
953#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100955 if( status != PSA_SUCCESS )
956 return( status );
957 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958}
959
Gilles Peskineb870b182018-07-06 16:02:09 +0200960/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100961static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200962{
963 if( key_type_is_raw_bytes( slot->type ) )
964 return( slot->data.raw.bytes * 8 );
965#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200966 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100967 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200968#endif /* defined(MBEDTLS_RSA_C) */
969#if defined(MBEDTLS_ECP_C)
970 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
971 return( slot->data.ecp->grp.pbits );
972#endif /* defined(MBEDTLS_ECP_C) */
973 /* Shouldn't happen except on an empty slot. */
974 return( 0 );
975}
976
Gilles Peskinec5487a82018-12-03 18:08:14 +0100977psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200978 psa_key_type_t *type,
979 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100981 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200982 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200985 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 if( bits != NULL )
987 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100988 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200989 if( status != PSA_SUCCESS )
990 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200991
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200993 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200994 if( type != NULL )
995 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200996 if( bits != NULL )
997 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 return( PSA_SUCCESS );
999}
1000
Jaeden Ameroccdce902019-01-10 11:42:27 +00001001#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001002static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1003 unsigned char *buf, size_t size )
1004{
1005 int ret;
1006 unsigned char *c;
1007 size_t len = 0;
1008
1009 c = buf + size;
1010
1011 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1012
1013 return( (int) len );
1014}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001015#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001016
Gilles Peskinef603c712019-01-19 13:40:11 +01001017static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1018 uint8_t *data,
1019 size_t data_size,
1020 size_t *data_length,
1021 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001023 *data_length = 0;
1024
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001025 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001026 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001027
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001028 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029 {
1030 if( slot->data.raw.bytes > data_size )
1031 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001032 if( data_size != 0 )
1033 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001034 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001035 memset( data + slot->data.raw.bytes, 0,
1036 data_size - slot->data.raw.bytes );
1037 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001038 *data_length = slot->data.raw.bytes;
1039 return( PSA_SUCCESS );
1040 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001041#if defined(MBEDTLS_ECP_C)
1042 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1043 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001044 psa_status_t status;
1045
Gilles Peskine188c71e2018-10-29 19:26:02 +01001046 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1047 if( bytes > data_size )
1048 return( PSA_ERROR_BUFFER_TOO_SMALL );
1049 status = mbedtls_to_psa_error(
1050 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1051 if( status != PSA_SUCCESS )
1052 return( status );
1053 memset( data + bytes, 0, data_size - bytes );
1054 *data_length = bytes;
1055 return( PSA_SUCCESS );
1056 }
1057#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 else
Moran Peker17e36e12018-05-02 12:55:20 +03001059 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001060#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001061 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001062 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001063 {
Moran Pekera998bc62018-04-16 18:16:20 +03001064 mbedtls_pk_context pk;
1065 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001066 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001067 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001068#if defined(MBEDTLS_RSA_C)
1069 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001070 pk.pk_info = &mbedtls_rsa_info;
1071 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001072#else
1073 return( PSA_ERROR_NOT_SUPPORTED );
1074#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001075 }
1076 else
1077 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001078#if defined(MBEDTLS_ECP_C)
1079 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001080 pk.pk_info = &mbedtls_eckey_info;
1081 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001082#else
1083 return( PSA_ERROR_NOT_SUPPORTED );
1084#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001085 }
Moran Pekerd7326592018-05-29 16:56:39 +03001086 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001087 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001088 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001089 }
Moran Peker17e36e12018-05-02 12:55:20 +03001090 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001091 {
Moran Peker17e36e12018-05-02 12:55:20 +03001092 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001093 }
Moran Peker60364322018-04-29 11:34:58 +03001094 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001095 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001096 /* If data_size is 0 then data may be NULL and then the
1097 * call to memset would have undefined behavior. */
1098 if( data_size != 0 )
1099 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001100 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001101 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001102 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1103 * Move the data to the beginning and erase remaining data
1104 * at the original location. */
1105 if( 2 * (size_t) ret <= data_size )
1106 {
1107 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001108 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001109 }
1110 else if( (size_t) ret < data_size )
1111 {
1112 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001113 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001114 }
Moran Pekera998bc62018-04-16 18:16:20 +03001115 *data_length = ret;
1116 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001117 }
1118 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001119#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001120 {
1121 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001122 it is valid for a special-purpose implementation to omit
1123 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001124 return( PSA_ERROR_NOT_SUPPORTED );
1125 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 }
1127}
1128
Gilles Peskinec5487a82018-12-03 18:08:14 +01001129psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001130 uint8_t *data,
1131 size_t data_size,
1132 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001133{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001134 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001135 psa_status_t status;
1136
1137 /* Set the key to empty now, so that even when there are errors, we always
1138 * set data_length to a value between 0 and data_size. On error, setting
1139 * the key to empty is a good choice because an empty key representation is
1140 * unlikely to be accepted anywhere. */
1141 *data_length = 0;
1142
1143 /* Export requires the EXPORT flag. There is an exception for public keys,
1144 * which don't require any flag, but psa_get_key_from_slot takes
1145 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001146 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001147 if( status != PSA_SUCCESS )
1148 return( status );
1149 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001150 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001151}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152
Gilles Peskinec5487a82018-12-03 18:08:14 +01001153psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001154 uint8_t *data,
1155 size_t data_size,
1156 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001157{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001158 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001159 psa_status_t status;
1160
1161 /* Set the key to empty now, so that even when there are errors, we always
1162 * set data_length to a value between 0 and data_size. On error, setting
1163 * the key to empty is a good choice because an empty key representation is
1164 * unlikely to be accepted anywhere. */
1165 *data_length = 0;
1166
1167 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001168 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001169 if( status != PSA_SUCCESS )
1170 return( status );
1171 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001172 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001173}
1174
Darryl Green0c6575a2018-11-07 16:05:30 +00001175#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001176static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001177 size_t bits )
1178{
1179 psa_status_t status;
1180 uint8_t *data;
1181 size_t key_length;
1182 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1183 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001184 if( data == NULL )
1185 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001186 /* Get key data in export format */
1187 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1188 if( status != PSA_SUCCESS )
1189 {
1190 slot->type = PSA_KEY_TYPE_NONE;
1191 goto exit;
1192 }
1193 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001194 status = psa_save_persistent_key( slot->persistent_storage_id,
1195 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001196 data, key_length );
1197 if( status != PSA_SUCCESS )
1198 {
1199 slot->type = PSA_KEY_TYPE_NONE;
1200 }
1201exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001202 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001203 mbedtls_free( data );
1204 return( status );
1205}
1206#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1207
Gilles Peskinef603c712019-01-19 13:40:11 +01001208static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1209 psa_key_handle_t target )
1210{
1211 psa_status_t status;
1212 uint8_t *buffer = NULL;
1213 size_t buffer_size = 0;
1214 size_t length;
1215
1216 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1217 psa_get_key_bits( source ) );
1218 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001219 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001220 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001221 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1222 if( status != PSA_SUCCESS )
1223 goto exit;
1224 status = psa_import_key( target, source->type, buffer, length );
1225
1226exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001227 if( buffer_size != 0 )
1228 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001229 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001230 return( status );
1231}
1232
1233psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1234 psa_key_handle_t target_handle,
1235 const psa_key_policy_t *constraint)
1236{
1237 psa_key_slot_t *source_slot = NULL;
1238 psa_key_slot_t *target_slot = NULL;
1239 psa_key_policy_t new_policy;
1240 psa_status_t status;
1241 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1242 if( status != PSA_SUCCESS )
1243 return( status );
1244 status = psa_get_empty_key_slot( target_handle, &target_slot );
1245 if( status != PSA_SUCCESS )
1246 return( status );
1247
1248 new_policy = target_slot->policy;
1249 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1250 if( status != PSA_SUCCESS )
1251 return( status );
1252 if( constraint != NULL )
1253 {
1254 status = psa_restrict_key_policy( &new_policy, constraint );
1255 if( status != PSA_SUCCESS )
1256 return( status );
1257 }
1258
1259 status = psa_copy_key_material( source_slot, target_handle );
1260 if( status != PSA_SUCCESS )
1261 return( status );
1262
1263 target_slot->policy = new_policy;
1264 return( PSA_SUCCESS );
1265}
1266
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001267
1268
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001270/* Message digests */
1271/****************************************************************/
1272
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001273static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001274{
1275 switch( alg )
1276 {
1277#if defined(MBEDTLS_MD2_C)
1278 case PSA_ALG_MD2:
1279 return( &mbedtls_md2_info );
1280#endif
1281#if defined(MBEDTLS_MD4_C)
1282 case PSA_ALG_MD4:
1283 return( &mbedtls_md4_info );
1284#endif
1285#if defined(MBEDTLS_MD5_C)
1286 case PSA_ALG_MD5:
1287 return( &mbedtls_md5_info );
1288#endif
1289#if defined(MBEDTLS_RIPEMD160_C)
1290 case PSA_ALG_RIPEMD160:
1291 return( &mbedtls_ripemd160_info );
1292#endif
1293#if defined(MBEDTLS_SHA1_C)
1294 case PSA_ALG_SHA_1:
1295 return( &mbedtls_sha1_info );
1296#endif
1297#if defined(MBEDTLS_SHA256_C)
1298 case PSA_ALG_SHA_224:
1299 return( &mbedtls_sha224_info );
1300 case PSA_ALG_SHA_256:
1301 return( &mbedtls_sha256_info );
1302#endif
1303#if defined(MBEDTLS_SHA512_C)
1304 case PSA_ALG_SHA_384:
1305 return( &mbedtls_sha384_info );
1306 case PSA_ALG_SHA_512:
1307 return( &mbedtls_sha512_info );
1308#endif
1309 default:
1310 return( NULL );
1311 }
1312}
1313
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001314psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1315{
1316 switch( operation->alg )
1317 {
Gilles Peskine81736312018-06-26 15:04:31 +02001318 case 0:
1319 /* The object has (apparently) been initialized but it is not
1320 * in use. It's ok to call abort on such an object, and there's
1321 * nothing to do. */
1322 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001323#if defined(MBEDTLS_MD2_C)
1324 case PSA_ALG_MD2:
1325 mbedtls_md2_free( &operation->ctx.md2 );
1326 break;
1327#endif
1328#if defined(MBEDTLS_MD4_C)
1329 case PSA_ALG_MD4:
1330 mbedtls_md4_free( &operation->ctx.md4 );
1331 break;
1332#endif
1333#if defined(MBEDTLS_MD5_C)
1334 case PSA_ALG_MD5:
1335 mbedtls_md5_free( &operation->ctx.md5 );
1336 break;
1337#endif
1338#if defined(MBEDTLS_RIPEMD160_C)
1339 case PSA_ALG_RIPEMD160:
1340 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_SHA1_C)
1344 case PSA_ALG_SHA_1:
1345 mbedtls_sha1_free( &operation->ctx.sha1 );
1346 break;
1347#endif
1348#if defined(MBEDTLS_SHA256_C)
1349 case PSA_ALG_SHA_224:
1350 case PSA_ALG_SHA_256:
1351 mbedtls_sha256_free( &operation->ctx.sha256 );
1352 break;
1353#endif
1354#if defined(MBEDTLS_SHA512_C)
1355 case PSA_ALG_SHA_384:
1356 case PSA_ALG_SHA_512:
1357 mbedtls_sha512_free( &operation->ctx.sha512 );
1358 break;
1359#endif
1360 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001361 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001362 }
1363 operation->alg = 0;
1364 return( PSA_SUCCESS );
1365}
1366
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001367psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001368 psa_algorithm_t alg )
1369{
1370 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001371
1372 /* A context must be freshly initialized before it can be set up. */
1373 if( operation->alg != 0 )
1374 {
1375 return( PSA_ERROR_BAD_STATE );
1376 }
1377
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001378 switch( alg )
1379 {
1380#if defined(MBEDTLS_MD2_C)
1381 case PSA_ALG_MD2:
1382 mbedtls_md2_init( &operation->ctx.md2 );
1383 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1384 break;
1385#endif
1386#if defined(MBEDTLS_MD4_C)
1387 case PSA_ALG_MD4:
1388 mbedtls_md4_init( &operation->ctx.md4 );
1389 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1390 break;
1391#endif
1392#if defined(MBEDTLS_MD5_C)
1393 case PSA_ALG_MD5:
1394 mbedtls_md5_init( &operation->ctx.md5 );
1395 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1396 break;
1397#endif
1398#if defined(MBEDTLS_RIPEMD160_C)
1399 case PSA_ALG_RIPEMD160:
1400 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1401 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1402 break;
1403#endif
1404#if defined(MBEDTLS_SHA1_C)
1405 case PSA_ALG_SHA_1:
1406 mbedtls_sha1_init( &operation->ctx.sha1 );
1407 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1408 break;
1409#endif
1410#if defined(MBEDTLS_SHA256_C)
1411 case PSA_ALG_SHA_224:
1412 mbedtls_sha256_init( &operation->ctx.sha256 );
1413 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1414 break;
1415 case PSA_ALG_SHA_256:
1416 mbedtls_sha256_init( &operation->ctx.sha256 );
1417 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1418 break;
1419#endif
1420#if defined(MBEDTLS_SHA512_C)
1421 case PSA_ALG_SHA_384:
1422 mbedtls_sha512_init( &operation->ctx.sha512 );
1423 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1424 break;
1425 case PSA_ALG_SHA_512:
1426 mbedtls_sha512_init( &operation->ctx.sha512 );
1427 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1428 break;
1429#endif
1430 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001431 return( PSA_ALG_IS_HASH( alg ) ?
1432 PSA_ERROR_NOT_SUPPORTED :
1433 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001434 }
1435 if( ret == 0 )
1436 operation->alg = alg;
1437 else
1438 psa_hash_abort( operation );
1439 return( mbedtls_to_psa_error( ret ) );
1440}
1441
1442psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1443 const uint8_t *input,
1444 size_t input_length )
1445{
1446 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001447
1448 /* Don't require hash implementations to behave correctly on a
1449 * zero-length input, which may have an invalid pointer. */
1450 if( input_length == 0 )
1451 return( PSA_SUCCESS );
1452
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001453 switch( operation->alg )
1454 {
1455#if defined(MBEDTLS_MD2_C)
1456 case PSA_ALG_MD2:
1457 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1458 input, input_length );
1459 break;
1460#endif
1461#if defined(MBEDTLS_MD4_C)
1462 case PSA_ALG_MD4:
1463 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1464 input, input_length );
1465 break;
1466#endif
1467#if defined(MBEDTLS_MD5_C)
1468 case PSA_ALG_MD5:
1469 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1470 input, input_length );
1471 break;
1472#endif
1473#if defined(MBEDTLS_RIPEMD160_C)
1474 case PSA_ALG_RIPEMD160:
1475 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1476 input, input_length );
1477 break;
1478#endif
1479#if defined(MBEDTLS_SHA1_C)
1480 case PSA_ALG_SHA_1:
1481 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1482 input, input_length );
1483 break;
1484#endif
1485#if defined(MBEDTLS_SHA256_C)
1486 case PSA_ALG_SHA_224:
1487 case PSA_ALG_SHA_256:
1488 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1489 input, input_length );
1490 break;
1491#endif
1492#if defined(MBEDTLS_SHA512_C)
1493 case PSA_ALG_SHA_384:
1494 case PSA_ALG_SHA_512:
1495 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1496 input, input_length );
1497 break;
1498#endif
1499 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001500 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001501 }
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:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001572 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001573 }
itayzafrir40835d42018-08-02 13:14:17 +03001574 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575
itayzafrir40835d42018-08-02 13:14:17 +03001576exit:
1577 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001579 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580 return( psa_hash_abort( operation ) );
1581 }
1582 else
1583 {
1584 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001585 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586 }
1587}
1588
Gilles Peskine2d277862018-06-18 15:41:12 +02001589psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1590 const uint8_t *hash,
1591 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001592{
1593 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1594 size_t actual_hash_length;
1595 psa_status_t status = psa_hash_finish( operation,
1596 actual_hash, sizeof( actual_hash ),
1597 &actual_hash_length );
1598 if( status != PSA_SUCCESS )
1599 return( status );
1600 if( actual_hash_length != hash_length )
1601 return( PSA_ERROR_INVALID_SIGNATURE );
1602 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1603 return( PSA_ERROR_INVALID_SIGNATURE );
1604 return( PSA_SUCCESS );
1605}
1606
Gilles Peskineeb35d782019-01-22 17:56:16 +01001607psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1608 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001609{
1610 if( target_operation->alg != 0 )
1611 return( PSA_ERROR_BAD_STATE );
1612
1613 switch( source_operation->alg )
1614 {
1615 case 0:
1616 return( PSA_ERROR_BAD_STATE );
1617#if defined(MBEDTLS_MD2_C)
1618 case PSA_ALG_MD2:
1619 mbedtls_md2_clone( &target_operation->ctx.md2,
1620 &source_operation->ctx.md2 );
1621 break;
1622#endif
1623#if defined(MBEDTLS_MD4_C)
1624 case PSA_ALG_MD4:
1625 mbedtls_md4_clone( &target_operation->ctx.md4,
1626 &source_operation->ctx.md4 );
1627 break;
1628#endif
1629#if defined(MBEDTLS_MD5_C)
1630 case PSA_ALG_MD5:
1631 mbedtls_md5_clone( &target_operation->ctx.md5,
1632 &source_operation->ctx.md5 );
1633 break;
1634#endif
1635#if defined(MBEDTLS_RIPEMD160_C)
1636 case PSA_ALG_RIPEMD160:
1637 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1638 &source_operation->ctx.ripemd160 );
1639 break;
1640#endif
1641#if defined(MBEDTLS_SHA1_C)
1642 case PSA_ALG_SHA_1:
1643 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1644 &source_operation->ctx.sha1 );
1645 break;
1646#endif
1647#if defined(MBEDTLS_SHA256_C)
1648 case PSA_ALG_SHA_224:
1649 case PSA_ALG_SHA_256:
1650 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1651 &source_operation->ctx.sha256 );
1652 break;
1653#endif
1654#if defined(MBEDTLS_SHA512_C)
1655 case PSA_ALG_SHA_384:
1656 case PSA_ALG_SHA_512:
1657 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1658 &source_operation->ctx.sha512 );
1659 break;
1660#endif
1661 default:
1662 return( PSA_ERROR_NOT_SUPPORTED );
1663 }
1664
1665 target_operation->alg = source_operation->alg;
1666 return( PSA_SUCCESS );
1667}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001668
1669
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001670/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671/* MAC */
1672/****************************************************************/
1673
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001674static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001675 psa_algorithm_t alg,
1676 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001677 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001678 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001681 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001683 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001684 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001685
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1687 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001688 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001690 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 mode = MBEDTLS_MODE_STREAM;
1692 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693 case PSA_ALG_CTR:
1694 mode = MBEDTLS_MODE_CTR;
1695 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001696 case PSA_ALG_CFB:
1697 mode = MBEDTLS_MODE_CFB;
1698 break;
1699 case PSA_ALG_OFB:
1700 mode = MBEDTLS_MODE_OFB;
1701 break;
1702 case PSA_ALG_CBC_NO_PADDING:
1703 mode = MBEDTLS_MODE_CBC;
1704 break;
1705 case PSA_ALG_CBC_PKCS7:
1706 mode = MBEDTLS_MODE_CBC;
1707 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001708 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001709 mode = MBEDTLS_MODE_CCM;
1710 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001711 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 mode = MBEDTLS_MODE_GCM;
1713 break;
1714 default:
1715 return( NULL );
1716 }
1717 }
1718 else if( alg == PSA_ALG_CMAC )
1719 mode = MBEDTLS_MODE_ECB;
1720 else if( alg == PSA_ALG_GMAC )
1721 mode = MBEDTLS_MODE_GCM;
1722 else
1723 return( NULL );
1724
1725 switch( key_type )
1726 {
1727 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001728 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001729 break;
1730 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001731 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1732 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001733 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001734 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001736 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001737 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1738 * but two-key Triple-DES is functionally three-key Triple-DES
1739 * with K1=K3, so that's how we present it to mbedtls. */
1740 if( key_bits == 128 )
1741 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001742 break;
1743 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001744 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745 break;
1746 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001747 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748 break;
1749 default:
1750 return( NULL );
1751 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001752 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001753 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754
Jaeden Amero23bbb752018-06-26 14:16:54 +01001755 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1756 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757}
1758
Gilles Peskinea05219c2018-11-16 16:02:56 +01001759#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001760static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001761{
Gilles Peskine2d277862018-06-18 15:41:12 +02001762 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001763 {
1764 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001765 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001766 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001767 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001768 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001769 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001770 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001771 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001772 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001773 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001774 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001775 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001776 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001777 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001778 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001779 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001780 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001781 return( 128 );
1782 default:
1783 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001784 }
1785}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001786#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001787
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001788/* Initialize the MAC operation structure. Once this function has been
1789 * called, psa_mac_abort can run and will do the right thing. */
1790static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1791 psa_algorithm_t alg )
1792{
1793 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1794
1795 operation->alg = alg;
1796 operation->key_set = 0;
1797 operation->iv_set = 0;
1798 operation->iv_required = 0;
1799 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001800 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001801
1802#if defined(MBEDTLS_CMAC_C)
1803 if( alg == PSA_ALG_CMAC )
1804 {
1805 operation->iv_required = 0;
1806 mbedtls_cipher_init( &operation->ctx.cmac );
1807 status = PSA_SUCCESS;
1808 }
1809 else
1810#endif /* MBEDTLS_CMAC_C */
1811#if defined(MBEDTLS_MD_C)
1812 if( PSA_ALG_IS_HMAC( operation->alg ) )
1813 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001814 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1815 operation->ctx.hmac.hash_ctx.alg = 0;
1816 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001817 }
1818 else
1819#endif /* MBEDTLS_MD_C */
1820 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001821 if( ! PSA_ALG_IS_MAC( alg ) )
1822 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 }
1824
1825 if( status != PSA_SUCCESS )
1826 memset( operation, 0, sizeof( *operation ) );
1827 return( status );
1828}
1829
Gilles Peskine01126fa2018-07-12 17:04:55 +02001830#if defined(MBEDTLS_MD_C)
1831static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1832{
Gilles Peskine3f108122018-12-07 18:14:53 +01001833 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001834 return( psa_hash_abort( &hmac->hash_ctx ) );
1835}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001836
1837static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1838{
1839 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1840 memset( hmac, 0, sizeof( *hmac ) );
1841}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001842#endif /* MBEDTLS_MD_C */
1843
Gilles Peskine8c9def32018-02-08 10:02:12 +01001844psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1845{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001846 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001847 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001848 /* The object has (apparently) been initialized but it is not
1849 * in use. It's ok to call abort on such an object, and there's
1850 * nothing to do. */
1851 return( PSA_SUCCESS );
1852 }
1853 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001854#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001855 if( operation->alg == PSA_ALG_CMAC )
1856 {
1857 mbedtls_cipher_free( &operation->ctx.cmac );
1858 }
1859 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001862 if( PSA_ALG_IS_HMAC( operation->alg ) )
1863 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001864 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001865 }
1866 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001868 {
1869 /* Sanity check (shouldn't happen: operation->alg should
1870 * always have been initialized to a valid value). */
1871 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872 }
Moran Peker41deec42018-04-04 15:43:05 +03001873
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874 operation->alg = 0;
1875 operation->key_set = 0;
1876 operation->iv_set = 0;
1877 operation->iv_required = 0;
1878 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001879 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001880
Gilles Peskine8c9def32018-02-08 10:02:12 +01001881 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001882
1883bad_state:
1884 /* If abort is called on an uninitialized object, we can't trust
1885 * anything. Wipe the object in case it contains confidential data.
1886 * This may result in a memory leak if a pointer gets overwritten,
1887 * but it's too late to do anything about this. */
1888 memset( operation, 0, sizeof( *operation ) );
1889 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890}
1891
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001892#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001893static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001894 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001895 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001896 const mbedtls_cipher_info_t *cipher_info )
1897{
1898 int ret;
1899
1900 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901
1902 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1903 if( ret != 0 )
1904 return( ret );
1905
1906 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1907 slot->data.raw.data,
1908 key_bits );
1909 return( ret );
1910}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001911#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001912
Gilles Peskine248051a2018-06-20 16:09:38 +02001913#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001914static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1915 const uint8_t *key,
1916 size_t key_length,
1917 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001918{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001919 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001920 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001921 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001922 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001923 psa_status_t status;
1924
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001925 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1926 * overflow below. This should never trigger if the hash algorithm
1927 * is implemented correctly. */
1928 /* The size checks against the ipad and opad buffers cannot be written
1929 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1930 * because that triggers -Wlogical-op on GCC 7.3. */
1931 if( block_size > sizeof( ipad ) )
1932 return( PSA_ERROR_NOT_SUPPORTED );
1933 if( block_size > sizeof( hmac->opad ) )
1934 return( PSA_ERROR_NOT_SUPPORTED );
1935 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001936 return( PSA_ERROR_NOT_SUPPORTED );
1937
Gilles Peskined223b522018-06-11 18:12:58 +02001938 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001939 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001940 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001941 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001942 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001943 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001944 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001945 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001946 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001947 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001948 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001949 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001950 }
Gilles Peskine96889972018-07-12 17:07:03 +02001951 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1952 * but it is permitted. It is common when HMAC is used in HKDF, for
1953 * example. Don't call `memcpy` in the 0-length because `key` could be
1954 * an invalid pointer which would make the behavior undefined. */
1955 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001956 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001957
Gilles Peskined223b522018-06-11 18:12:58 +02001958 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1959 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001960 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001961 ipad[i] ^= 0x36;
1962 memset( ipad + key_length, 0x36, block_size - key_length );
1963
1964 /* Copy the key material from ipad to opad, flipping the requisite bits,
1965 * and filling the rest of opad with the requisite constant. */
1966 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001967 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1968 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001969
Gilles Peskine01126fa2018-07-12 17:04:55 +02001970 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001971 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001972 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001973
Gilles Peskine01126fa2018-07-12 17:04:55 +02001974 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001975
1976cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001977 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001978
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001979 return( status );
1980}
Gilles Peskine248051a2018-06-20 16:09:38 +02001981#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001982
Gilles Peskine89167cb2018-07-08 20:12:23 +02001983static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001984 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001985 psa_algorithm_t alg,
1986 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001987{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001989 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001990 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001991 psa_key_usage_t usage =
1992 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001993 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001994 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001996 /* A context must be freshly initialized before it can be set up. */
1997 if( operation->alg != 0 )
1998 {
1999 return( PSA_ERROR_BAD_STATE );
2000 }
2001
Gilles Peskined911eb72018-08-14 15:18:45 +02002002 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002003 if( status != PSA_SUCCESS )
2004 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002005 if( is_sign )
2006 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007
Gilles Peskinec5487a82018-12-03 18:08:14 +01002008 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002009 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002010 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002011 key_bits = psa_get_key_bits( slot );
2012
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002014 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002015 {
2016 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002017 mbedtls_cipher_info_from_psa( full_length_alg,
2018 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 int ret;
2020 if( cipher_info == NULL )
2021 {
2022 status = PSA_ERROR_NOT_SUPPORTED;
2023 goto exit;
2024 }
2025 operation->mac_size = cipher_info->block_size;
2026 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2027 status = mbedtls_to_psa_error( ret );
2028 }
2029 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002032 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002033 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002034 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002035 if( hash_alg == 0 )
2036 {
2037 status = PSA_ERROR_NOT_SUPPORTED;
2038 goto exit;
2039 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002040
2041 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2042 /* Sanity check. This shouldn't fail on a valid configuration. */
2043 if( operation->mac_size == 0 ||
2044 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2045 {
2046 status = PSA_ERROR_NOT_SUPPORTED;
2047 goto exit;
2048 }
2049
Gilles Peskine01126fa2018-07-12 17:04:55 +02002050 if( slot->type != PSA_KEY_TYPE_HMAC )
2051 {
2052 status = PSA_ERROR_INVALID_ARGUMENT;
2053 goto exit;
2054 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002055
Gilles Peskine01126fa2018-07-12 17:04:55 +02002056 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2057 slot->data.raw.data,
2058 slot->data.raw.bytes,
2059 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002060 }
2061 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002063 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002064 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002065 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002067
Gilles Peskined911eb72018-08-14 15:18:45 +02002068 if( truncated == 0 )
2069 {
2070 /* The "normal" case: untruncated algorithm. Nothing to do. */
2071 }
2072 else if( truncated < 4 )
2073 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002074 /* A very short MAC is too short for security since it can be
2075 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2076 * so we make this our minimum, even though 32 bits is still
2077 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002078 status = PSA_ERROR_NOT_SUPPORTED;
2079 }
2080 else if( truncated > operation->mac_size )
2081 {
2082 /* It's impossible to "truncate" to a larger length. */
2083 status = PSA_ERROR_INVALID_ARGUMENT;
2084 }
2085 else
2086 operation->mac_size = truncated;
2087
Gilles Peskinefbfac682018-07-08 20:51:54 +02002088exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002089 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002091 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002093 else
2094 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002095 operation->key_set = 1;
2096 }
2097 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002098}
2099
Gilles Peskine89167cb2018-07-08 20:12:23 +02002100psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002101 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002102 psa_algorithm_t alg )
2103{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002104 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002105}
2106
2107psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002108 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002109 psa_algorithm_t alg )
2110{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002111 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002112}
2113
Gilles Peskine8c9def32018-02-08 10:02:12 +01002114psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2115 const uint8_t *input,
2116 size_t input_length )
2117{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002118 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002119 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002120 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002121 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002122 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002123 operation->has_input = 1;
2124
Gilles Peskine8c9def32018-02-08 10:02:12 +01002125#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002126 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002127 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002128 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2129 input, input_length );
2130 status = mbedtls_to_psa_error( ret );
2131 }
2132 else
2133#endif /* MBEDTLS_CMAC_C */
2134#if defined(MBEDTLS_MD_C)
2135 if( PSA_ALG_IS_HMAC( operation->alg ) )
2136 {
2137 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2138 input_length );
2139 }
2140 else
2141#endif /* MBEDTLS_MD_C */
2142 {
2143 /* This shouldn't happen if `operation` was initialized by
2144 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002145 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002146 }
2147
Gilles Peskinefbfac682018-07-08 20:51:54 +02002148 if( status != PSA_SUCCESS )
2149 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002150 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002151}
2152
Gilles Peskine01126fa2018-07-12 17:04:55 +02002153#if defined(MBEDTLS_MD_C)
2154static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2155 uint8_t *mac,
2156 size_t mac_size )
2157{
2158 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2159 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2160 size_t hash_size = 0;
2161 size_t block_size = psa_get_hash_block_size( hash_alg );
2162 psa_status_t status;
2163
Gilles Peskine01126fa2018-07-12 17:04:55 +02002164 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2165 if( status != PSA_SUCCESS )
2166 return( status );
2167 /* From here on, tmp needs to be wiped. */
2168
2169 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2170 if( status != PSA_SUCCESS )
2171 goto exit;
2172
2173 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2174 if( status != PSA_SUCCESS )
2175 goto exit;
2176
2177 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2178 if( status != PSA_SUCCESS )
2179 goto exit;
2180
Gilles Peskined911eb72018-08-14 15:18:45 +02002181 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2182 if( status != PSA_SUCCESS )
2183 goto exit;
2184
2185 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002186
2187exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002188 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002189 return( status );
2190}
2191#endif /* MBEDTLS_MD_C */
2192
mohammad16036df908f2018-04-02 08:34:15 -07002193static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002194 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002195 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002196{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002197 if( ! operation->key_set )
2198 return( PSA_ERROR_BAD_STATE );
2199 if( operation->iv_required && ! operation->iv_set )
2200 return( PSA_ERROR_BAD_STATE );
2201
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202 if( mac_size < operation->mac_size )
2203 return( PSA_ERROR_BUFFER_TOO_SMALL );
2204
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002206 if( operation->alg == PSA_ALG_CMAC )
2207 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002208 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2209 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2210 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002211 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002212 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002213 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002214 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002215 else
2216#endif /* MBEDTLS_CMAC_C */
2217#if defined(MBEDTLS_MD_C)
2218 if( PSA_ALG_IS_HMAC( operation->alg ) )
2219 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002220 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002221 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002222 }
2223 else
2224#endif /* MBEDTLS_MD_C */
2225 {
2226 /* This shouldn't happen if `operation` was initialized by
2227 * a setup function. */
2228 return( PSA_ERROR_BAD_STATE );
2229 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002230}
2231
Gilles Peskineacd4be32018-07-08 19:56:25 +02002232psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2233 uint8_t *mac,
2234 size_t mac_size,
2235 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002236{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002237 psa_status_t status;
2238
Jaeden Amero252ef282019-02-15 14:05:35 +00002239 if( operation->alg == 0 )
2240 {
2241 return( PSA_ERROR_BAD_STATE );
2242 }
2243
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002244 /* Fill the output buffer with something that isn't a valid mac
2245 * (barring an attack on the mac and deliberately-crafted input),
2246 * in case the caller doesn't check the return status properly. */
2247 *mac_length = mac_size;
2248 /* If mac_size is 0 then mac may be NULL and then the
2249 * call to memset would have undefined behavior. */
2250 if( mac_size != 0 )
2251 memset( mac, '!', mac_size );
2252
Gilles Peskine89167cb2018-07-08 20:12:23 +02002253 if( ! operation->is_sign )
2254 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002255 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002256 }
mohammad16036df908f2018-04-02 08:34:15 -07002257
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002258 status = psa_mac_finish_internal( operation, mac, mac_size );
2259
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002260 if( status == PSA_SUCCESS )
2261 {
2262 status = psa_mac_abort( operation );
2263 if( status == PSA_SUCCESS )
2264 *mac_length = operation->mac_size;
2265 else
2266 memset( mac, '!', mac_size );
2267 }
2268 else
2269 psa_mac_abort( operation );
2270 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002271}
2272
Gilles Peskineacd4be32018-07-08 19:56:25 +02002273psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2274 const uint8_t *mac,
2275 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276{
Gilles Peskine828ed142018-06-18 23:25:51 +02002277 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002278 psa_status_t status;
2279
Jaeden Amero252ef282019-02-15 14:05:35 +00002280 if( operation->alg == 0 )
2281 {
2282 return( PSA_ERROR_BAD_STATE );
2283 }
2284
Gilles Peskine89167cb2018-07-08 20:12:23 +02002285 if( operation->is_sign )
2286 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002287 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002288 }
2289 if( operation->mac_size != mac_length )
2290 {
2291 status = PSA_ERROR_INVALID_SIGNATURE;
2292 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002293 }
mohammad16036df908f2018-04-02 08:34:15 -07002294
2295 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002296 actual_mac, sizeof( actual_mac ) );
2297
2298 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2299 status = PSA_ERROR_INVALID_SIGNATURE;
2300
2301cleanup:
2302 if( status == PSA_SUCCESS )
2303 status = psa_mac_abort( operation );
2304 else
2305 psa_mac_abort( operation );
2306
Gilles Peskine3f108122018-12-07 18:14:53 +01002307 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002308
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002309 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002310}
2311
2312
Gilles Peskine20035e32018-02-03 22:44:14 +01002313
Gilles Peskine20035e32018-02-03 22:44:14 +01002314/****************************************************************/
2315/* Asymmetric cryptography */
2316/****************************************************************/
2317
Gilles Peskine2b450e32018-06-27 15:42:46 +02002318#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002319/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002320 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002321static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2322 size_t hash_length,
2323 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002324{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002325 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002326 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002327 *md_alg = mbedtls_md_get_type( md_info );
2328
2329 /* The Mbed TLS RSA module uses an unsigned int for hash length
2330 * parameters. Validate that it fits so that we don't risk an
2331 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002332#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002333 if( hash_length > UINT_MAX )
2334 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002335#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002336
2337#if defined(MBEDTLS_PKCS1_V15)
2338 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2339 * must be correct. */
2340 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2341 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002342 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002343 if( md_info == NULL )
2344 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002345 if( mbedtls_md_get_size( md_info ) != hash_length )
2346 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002347 }
2348#endif /* MBEDTLS_PKCS1_V15 */
2349
2350#if defined(MBEDTLS_PKCS1_V21)
2351 /* PSS requires a hash internally. */
2352 if( PSA_ALG_IS_RSA_PSS( alg ) )
2353 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002354 if( md_info == NULL )
2355 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002356 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002357#endif /* MBEDTLS_PKCS1_V21 */
2358
Gilles Peskine61b91d42018-06-08 16:09:36 +02002359 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002360}
2361
Gilles Peskine2b450e32018-06-27 15:42:46 +02002362static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2363 psa_algorithm_t alg,
2364 const uint8_t *hash,
2365 size_t hash_length,
2366 uint8_t *signature,
2367 size_t signature_size,
2368 size_t *signature_length )
2369{
2370 psa_status_t status;
2371 int ret;
2372 mbedtls_md_type_t md_alg;
2373
2374 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2375 if( status != PSA_SUCCESS )
2376 return( status );
2377
Gilles Peskine630a18a2018-06-29 17:49:35 +02002378 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002379 return( PSA_ERROR_BUFFER_TOO_SMALL );
2380
2381#if defined(MBEDTLS_PKCS1_V15)
2382 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2383 {
2384 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2385 MBEDTLS_MD_NONE );
2386 ret = mbedtls_rsa_pkcs1_sign( rsa,
2387 mbedtls_ctr_drbg_random,
2388 &global_data.ctr_drbg,
2389 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002390 md_alg,
2391 (unsigned int) hash_length,
2392 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002393 signature );
2394 }
2395 else
2396#endif /* MBEDTLS_PKCS1_V15 */
2397#if defined(MBEDTLS_PKCS1_V21)
2398 if( PSA_ALG_IS_RSA_PSS( alg ) )
2399 {
2400 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2401 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2402 mbedtls_ctr_drbg_random,
2403 &global_data.ctr_drbg,
2404 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002405 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002406 (unsigned int) hash_length,
2407 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002408 signature );
2409 }
2410 else
2411#endif /* MBEDTLS_PKCS1_V21 */
2412 {
2413 return( PSA_ERROR_INVALID_ARGUMENT );
2414 }
2415
2416 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002417 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002418 return( mbedtls_to_psa_error( ret ) );
2419}
2420
2421static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2422 psa_algorithm_t alg,
2423 const uint8_t *hash,
2424 size_t hash_length,
2425 const uint8_t *signature,
2426 size_t signature_length )
2427{
2428 psa_status_t status;
2429 int ret;
2430 mbedtls_md_type_t md_alg;
2431
2432 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2433 if( status != PSA_SUCCESS )
2434 return( status );
2435
Gilles Peskine630a18a2018-06-29 17:49:35 +02002436 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002437 return( PSA_ERROR_BUFFER_TOO_SMALL );
2438
2439#if defined(MBEDTLS_PKCS1_V15)
2440 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2441 {
2442 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2443 MBEDTLS_MD_NONE );
2444 ret = mbedtls_rsa_pkcs1_verify( rsa,
2445 mbedtls_ctr_drbg_random,
2446 &global_data.ctr_drbg,
2447 MBEDTLS_RSA_PUBLIC,
2448 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002449 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002450 hash,
2451 signature );
2452 }
2453 else
2454#endif /* MBEDTLS_PKCS1_V15 */
2455#if defined(MBEDTLS_PKCS1_V21)
2456 if( PSA_ALG_IS_RSA_PSS( alg ) )
2457 {
2458 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2459 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2460 mbedtls_ctr_drbg_random,
2461 &global_data.ctr_drbg,
2462 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002463 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002464 (unsigned int) hash_length,
2465 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002466 signature );
2467 }
2468 else
2469#endif /* MBEDTLS_PKCS1_V21 */
2470 {
2471 return( PSA_ERROR_INVALID_ARGUMENT );
2472 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002473
2474 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2475 * the rest of the signature is invalid". This has little use in
2476 * practice and PSA doesn't report this distinction. */
2477 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2478 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002479 return( mbedtls_to_psa_error( ret ) );
2480}
2481#endif /* MBEDTLS_RSA_C */
2482
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002483#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002484/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2485 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2486 * (even though these functions don't modify it). */
2487static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2488 psa_algorithm_t alg,
2489 const uint8_t *hash,
2490 size_t hash_length,
2491 uint8_t *signature,
2492 size_t signature_size,
2493 size_t *signature_length )
2494{
2495 int ret;
2496 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002497 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002498 mbedtls_mpi_init( &r );
2499 mbedtls_mpi_init( &s );
2500
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002501 if( signature_size < 2 * curve_bytes )
2502 {
2503 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2504 goto cleanup;
2505 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002506
Gilles Peskinea05219c2018-11-16 16:02:56 +01002507#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002508 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2509 {
2510 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2511 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2512 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2513 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2514 hash, hash_length,
2515 md_alg ) );
2516 }
2517 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002518#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002519 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002520 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002521 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2522 hash, hash_length,
2523 mbedtls_ctr_drbg_random,
2524 &global_data.ctr_drbg ) );
2525 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002526
2527 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2528 signature,
2529 curve_bytes ) );
2530 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2531 signature + curve_bytes,
2532 curve_bytes ) );
2533
2534cleanup:
2535 mbedtls_mpi_free( &r );
2536 mbedtls_mpi_free( &s );
2537 if( ret == 0 )
2538 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002539 return( mbedtls_to_psa_error( ret ) );
2540}
2541
2542static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2543 const uint8_t *hash,
2544 size_t hash_length,
2545 const uint8_t *signature,
2546 size_t signature_length )
2547{
2548 int ret;
2549 mbedtls_mpi r, s;
2550 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2551 mbedtls_mpi_init( &r );
2552 mbedtls_mpi_init( &s );
2553
2554 if( signature_length != 2 * curve_bytes )
2555 return( PSA_ERROR_INVALID_SIGNATURE );
2556
2557 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2558 signature,
2559 curve_bytes ) );
2560 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2561 signature + curve_bytes,
2562 curve_bytes ) );
2563
2564 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2565 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002566
2567cleanup:
2568 mbedtls_mpi_free( &r );
2569 mbedtls_mpi_free( &s );
2570 return( mbedtls_to_psa_error( ret ) );
2571}
2572#endif /* MBEDTLS_ECDSA_C */
2573
Gilles Peskinec5487a82018-12-03 18:08:14 +01002574psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002575 psa_algorithm_t alg,
2576 const uint8_t *hash,
2577 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002578 uint8_t *signature,
2579 size_t signature_size,
2580 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002581{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002582 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002583 psa_status_t status;
2584
2585 *signature_length = signature_size;
2586
Gilles Peskinec5487a82018-12-03 18:08:14 +01002587 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002588 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002589 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002590 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002591 {
2592 status = PSA_ERROR_INVALID_ARGUMENT;
2593 goto exit;
2594 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002595
Gilles Peskine20035e32018-02-03 22:44:14 +01002596#if defined(MBEDTLS_RSA_C)
2597 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2598 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002599 status = psa_rsa_sign( slot->data.rsa,
2600 alg,
2601 hash, hash_length,
2602 signature, signature_size,
2603 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002604 }
2605 else
2606#endif /* defined(MBEDTLS_RSA_C) */
2607#if defined(MBEDTLS_ECP_C)
2608 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2609 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002610#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002611 if(
2612#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2613 PSA_ALG_IS_ECDSA( alg )
2614#else
2615 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2616#endif
2617 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002618 status = psa_ecdsa_sign( slot->data.ecp,
2619 alg,
2620 hash, hash_length,
2621 signature, signature_size,
2622 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002623 else
2624#endif /* defined(MBEDTLS_ECDSA_C) */
2625 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002626 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002627 }
itayzafrir5c753392018-05-08 11:18:38 +03002628 }
2629 else
2630#endif /* defined(MBEDTLS_ECP_C) */
2631 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002632 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002633 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002634
2635exit:
2636 /* Fill the unused part of the output buffer (the whole buffer on error,
2637 * the trailing part on success) with something that isn't a valid mac
2638 * (barring an attack on the mac and deliberately-crafted input),
2639 * in case the caller doesn't check the return status properly. */
2640 if( status == PSA_SUCCESS )
2641 memset( signature + *signature_length, '!',
2642 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002643 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002644 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002645 /* If signature_size is 0 then we have nothing to do. We must not call
2646 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002647 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002648}
2649
Gilles Peskinec5487a82018-12-03 18:08:14 +01002650psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002651 psa_algorithm_t alg,
2652 const uint8_t *hash,
2653 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002654 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002655 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002656{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002657 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002658 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002659
Gilles Peskinec5487a82018-12-03 18:08:14 +01002660 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002661 if( status != PSA_SUCCESS )
2662 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002663
Gilles Peskine61b91d42018-06-08 16:09:36 +02002664#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002665 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002666 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002667 return( psa_rsa_verify( slot->data.rsa,
2668 alg,
2669 hash, hash_length,
2670 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 }
2672 else
2673#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002674#if defined(MBEDTLS_ECP_C)
2675 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2676 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002677#if defined(MBEDTLS_ECDSA_C)
2678 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002679 return( psa_ecdsa_verify( slot->data.ecp,
2680 hash, hash_length,
2681 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002682 else
2683#endif /* defined(MBEDTLS_ECDSA_C) */
2684 {
2685 return( PSA_ERROR_INVALID_ARGUMENT );
2686 }
itayzafrir5c753392018-05-08 11:18:38 +03002687 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002688 else
2689#endif /* defined(MBEDTLS_ECP_C) */
2690 {
2691 return( PSA_ERROR_NOT_SUPPORTED );
2692 }
2693}
2694
Gilles Peskine072ac562018-06-30 00:21:29 +02002695#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2696static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2697 mbedtls_rsa_context *rsa )
2698{
2699 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2700 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2701 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2702 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2703}
2704#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2705
Gilles Peskinec5487a82018-12-03 18:08:14 +01002706psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002707 psa_algorithm_t alg,
2708 const uint8_t *input,
2709 size_t input_length,
2710 const uint8_t *salt,
2711 size_t salt_length,
2712 uint8_t *output,
2713 size_t output_size,
2714 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002715{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002716 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002717 psa_status_t status;
2718
Darryl Green5cc689a2018-07-24 15:34:10 +01002719 (void) input;
2720 (void) input_length;
2721 (void) salt;
2722 (void) output;
2723 (void) output_size;
2724
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002725 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002726
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002727 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2728 return( PSA_ERROR_INVALID_ARGUMENT );
2729
Gilles Peskinec5487a82018-12-03 18:08:14 +01002730 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002731 if( status != PSA_SUCCESS )
2732 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002733 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2734 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002736
2737#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002739 {
2740 mbedtls_rsa_context *rsa = slot->data.rsa;
2741 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002742 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002743 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002745 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002746 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002747 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2748 mbedtls_ctr_drbg_random,
2749 &global_data.ctr_drbg,
2750 MBEDTLS_RSA_PUBLIC,
2751 input_length,
2752 input,
2753 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002754 }
2755 else
2756#endif /* MBEDTLS_PKCS1_V15 */
2757#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002758 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002759 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002760 psa_rsa_oaep_set_padding_mode( alg, rsa );
2761 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2762 mbedtls_ctr_drbg_random,
2763 &global_data.ctr_drbg,
2764 MBEDTLS_RSA_PUBLIC,
2765 salt, salt_length,
2766 input_length,
2767 input,
2768 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769 }
2770 else
2771#endif /* MBEDTLS_PKCS1_V21 */
2772 {
2773 return( PSA_ERROR_INVALID_ARGUMENT );
2774 }
2775 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002776 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002777 return( mbedtls_to_psa_error( ret ) );
2778 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002779 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002780#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002781 {
2782 return( PSA_ERROR_NOT_SUPPORTED );
2783 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002784}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002785
Gilles Peskinec5487a82018-12-03 18:08:14 +01002786psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002787 psa_algorithm_t alg,
2788 const uint8_t *input,
2789 size_t input_length,
2790 const uint8_t *salt,
2791 size_t salt_length,
2792 uint8_t *output,
2793 size_t output_size,
2794 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002795{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002796 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002797 psa_status_t status;
2798
Darryl Green5cc689a2018-07-24 15:34:10 +01002799 (void) input;
2800 (void) input_length;
2801 (void) salt;
2802 (void) output;
2803 (void) output_size;
2804
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002805 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002806
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002807 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2808 return( PSA_ERROR_INVALID_ARGUMENT );
2809
Gilles Peskinec5487a82018-12-03 18:08:14 +01002810 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002811 if( status != PSA_SUCCESS )
2812 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002813 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2814 return( PSA_ERROR_INVALID_ARGUMENT );
2815
2816#if defined(MBEDTLS_RSA_C)
2817 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2818 {
2819 mbedtls_rsa_context *rsa = slot->data.rsa;
2820 int ret;
2821
Gilles Peskine630a18a2018-06-29 17:49:35 +02002822 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002823 return( PSA_ERROR_INVALID_ARGUMENT );
2824
2825#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002826 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002827 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002828 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2829 mbedtls_ctr_drbg_random,
2830 &global_data.ctr_drbg,
2831 MBEDTLS_RSA_PRIVATE,
2832 output_length,
2833 input,
2834 output,
2835 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002836 }
2837 else
2838#endif /* MBEDTLS_PKCS1_V15 */
2839#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002840 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002841 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002842 psa_rsa_oaep_set_padding_mode( alg, rsa );
2843 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2844 mbedtls_ctr_drbg_random,
2845 &global_data.ctr_drbg,
2846 MBEDTLS_RSA_PRIVATE,
2847 salt, salt_length,
2848 output_length,
2849 input,
2850 output,
2851 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002852 }
2853 else
2854#endif /* MBEDTLS_PKCS1_V21 */
2855 {
2856 return( PSA_ERROR_INVALID_ARGUMENT );
2857 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002858
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002859 return( mbedtls_to_psa_error( ret ) );
2860 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002861 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002862#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002863 {
2864 return( PSA_ERROR_NOT_SUPPORTED );
2865 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002866}
Gilles Peskine20035e32018-02-03 22:44:14 +01002867
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002868
2869
mohammad1603503973b2018-03-12 15:59:30 +02002870/****************************************************************/
2871/* Symmetric cryptography */
2872/****************************************************************/
2873
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002874/* Initialize the cipher operation structure. Once this function has been
2875 * called, psa_cipher_abort can run and will do the right thing. */
2876static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2877 psa_algorithm_t alg )
2878{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002879 if( ! PSA_ALG_IS_CIPHER( alg ) )
2880 {
2881 memset( operation, 0, sizeof( *operation ) );
2882 return( PSA_ERROR_INVALID_ARGUMENT );
2883 }
2884
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002885 operation->alg = alg;
2886 operation->key_set = 0;
2887 operation->iv_set = 0;
2888 operation->iv_required = 1;
2889 operation->iv_size = 0;
2890 operation->block_size = 0;
2891 mbedtls_cipher_init( &operation->ctx.cipher );
2892 return( PSA_SUCCESS );
2893}
2894
Gilles Peskinee553c652018-06-04 16:22:46 +02002895static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002896 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002897 psa_algorithm_t alg,
2898 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002899{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002900 int ret = 0;
2901 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002902 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002903 size_t key_bits;
2904 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002905 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2906 PSA_KEY_USAGE_ENCRYPT :
2907 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002908
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002909 /* A context must be freshly initialized before it can be set up. */
2910 if( operation->alg != 0 )
2911 {
2912 return( PSA_ERROR_BAD_STATE );
2913 }
2914
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002915 status = psa_cipher_init( operation, alg );
2916 if( status != PSA_SUCCESS )
2917 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002918
Gilles Peskinec5487a82018-12-03 18:08:14 +01002919 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002920 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002921 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002922 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002923
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002924 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002925 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002926 {
2927 status = PSA_ERROR_NOT_SUPPORTED;
2928 goto exit;
2929 }
mohammad1603503973b2018-03-12 15:59:30 +02002930
mohammad1603503973b2018-03-12 15:59:30 +02002931 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002932 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002933 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002934
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002935#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002936 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002937 {
2938 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2939 unsigned char keys[24];
2940 memcpy( keys, slot->data.raw.data, 16 );
2941 memcpy( keys + 16, slot->data.raw.data, 8 );
2942 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2943 keys,
2944 192, cipher_operation );
2945 }
2946 else
2947#endif
2948 {
2949 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2950 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002951 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002952 }
Moran Peker41deec42018-04-04 15:43:05 +03002953 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002954 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002955
mohammad16038481e742018-03-18 13:57:31 +02002956#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002957 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002958 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002959 case PSA_ALG_CBC_NO_PADDING:
2960 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2961 MBEDTLS_PADDING_NONE );
2962 break;
2963 case PSA_ALG_CBC_PKCS7:
2964 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2965 MBEDTLS_PADDING_PKCS7 );
2966 break;
2967 default:
2968 /* The algorithm doesn't involve padding. */
2969 ret = 0;
2970 break;
2971 }
2972 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002973 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02002974#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2975
mohammad1603503973b2018-03-12 15:59:30 +02002976 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002977 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2978 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2979 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002980 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002981 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002982 }
mohammad1603503973b2018-03-12 15:59:30 +02002983
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002984exit:
2985 if( status == 0 )
2986 status = mbedtls_to_psa_error( ret );
2987 if( status != 0 )
2988 psa_cipher_abort( operation );
2989 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002990}
2991
Gilles Peskinefe119512018-07-08 21:39:34 +02002992psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002993 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002994 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002995{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002996 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002997}
2998
Gilles Peskinefe119512018-07-08 21:39:34 +02002999psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003000 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003001 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003002{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003003 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003004}
3005
Gilles Peskinefe119512018-07-08 21:39:34 +02003006psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3007 unsigned char *iv,
3008 size_t iv_size,
3009 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003010{
itayzafrir534bd7c2018-08-02 13:56:32 +03003011 psa_status_t status;
3012 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003013 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003014 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003015 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003016 }
Moran Peker41deec42018-04-04 15:43:05 +03003017 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003018 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003019 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003020 goto exit;
3021 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003022 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3023 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003024 if( ret != 0 )
3025 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003026 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003027 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003028 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003029
mohammad16038481e742018-03-18 13:57:31 +02003030 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003031 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003032
Moran Peker395db872018-05-31 14:07:14 +03003033exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003034 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003035 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003036 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003037}
3038
Gilles Peskinefe119512018-07-08 21:39:34 +02003039psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3040 const unsigned char *iv,
3041 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003042{
itayzafrir534bd7c2018-08-02 13:56:32 +03003043 psa_status_t status;
3044 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003045 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003046 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003047 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003048 }
Moran Pekera28258c2018-05-29 16:25:04 +03003049 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003050 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003051 status = PSA_ERROR_INVALID_ARGUMENT;
3052 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003053 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003054 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3055 status = mbedtls_to_psa_error( ret );
3056exit:
3057 if( status == PSA_SUCCESS )
3058 operation->iv_set = 1;
3059 else
mohammad1603503973b2018-03-12 15:59:30 +02003060 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003061 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003062}
3063
Gilles Peskinee553c652018-06-04 16:22:46 +02003064psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3065 const uint8_t *input,
3066 size_t input_length,
3067 unsigned char *output,
3068 size_t output_size,
3069 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003070{
itayzafrir534bd7c2018-08-02 13:56:32 +03003071 psa_status_t status;
3072 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003073 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003074
3075 if( operation->alg == 0 )
3076 {
3077 return( PSA_ERROR_BAD_STATE );
3078 }
3079
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003080 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003081 {
3082 /* Take the unprocessed partial block left over from previous
3083 * update calls, if any, plus the input to this call. Remove
3084 * the last partial block, if any. You get the data that will be
3085 * output in this call. */
3086 expected_output_size =
3087 ( operation->ctx.cipher.unprocessed_len + input_length )
3088 / operation->block_size * operation->block_size;
3089 }
3090 else
3091 {
3092 expected_output_size = input_length;
3093 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003094
Gilles Peskine89d789c2018-06-04 17:17:16 +02003095 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003096 {
3097 status = PSA_ERROR_BUFFER_TOO_SMALL;
3098 goto exit;
3099 }
mohammad160382759612018-03-12 18:16:40 +02003100
mohammad1603503973b2018-03-12 15:59:30 +02003101 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003102 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003103 status = mbedtls_to_psa_error( ret );
3104exit:
3105 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003106 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003107 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003108}
3109
Gilles Peskinee553c652018-06-04 16:22:46 +02003110psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3111 uint8_t *output,
3112 size_t output_size,
3113 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003114{
David Saadab4ecc272019-02-14 13:48:10 +02003115 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003116 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003117 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003118
mohammad1603503973b2018-03-12 15:59:30 +02003119 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003120 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003121 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003122 }
3123 if( operation->iv_required && ! operation->iv_set )
3124 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003125 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003126 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003127
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003128 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003129 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3130 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003131 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003132 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003133 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003134 }
3135
Janos Follath315b51c2018-07-09 16:04:51 +01003136 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3137 temp_output_buffer,
3138 output_length );
3139 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003140 {
Janos Follath315b51c2018-07-09 16:04:51 +01003141 status = mbedtls_to_psa_error( cipher_ret );
3142 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003143 }
Janos Follath315b51c2018-07-09 16:04:51 +01003144
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003145 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003146 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003147 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003148 memcpy( output, temp_output_buffer, *output_length );
3149 else
3150 {
Janos Follath315b51c2018-07-09 16:04:51 +01003151 status = PSA_ERROR_BUFFER_TOO_SMALL;
3152 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003153 }
mohammad1603503973b2018-03-12 15:59:30 +02003154
Gilles Peskine3f108122018-12-07 18:14:53 +01003155 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003156 status = psa_cipher_abort( operation );
3157
3158 return( status );
3159
3160error:
3161
3162 *output_length = 0;
3163
Gilles Peskine3f108122018-12-07 18:14:53 +01003164 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003165 (void) psa_cipher_abort( operation );
3166
3167 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003168}
3169
Gilles Peskinee553c652018-06-04 16:22:46 +02003170psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3171{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003172 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003173 {
3174 /* The object has (apparently) been initialized but it is not
3175 * in use. It's ok to call abort on such an object, and there's
3176 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003178 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003179
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003180 /* Sanity check (shouldn't happen: operation->alg should
3181 * always have been initialized to a valid value). */
3182 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3183 return( PSA_ERROR_BAD_STATE );
3184
mohammad1603503973b2018-03-12 15:59:30 +02003185 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003186
Moran Peker41deec42018-04-04 15:43:05 +03003187 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003188 operation->key_set = 0;
3189 operation->iv_set = 0;
3190 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003191 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003192 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003193
Moran Peker395db872018-05-31 14:07:14 +03003194 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003195}
3196
Gilles Peskinea0655c32018-04-30 17:06:50 +02003197
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003198
mohammad16038cc1cee2018-03-28 01:21:33 +03003199/****************************************************************/
3200/* Key Policy */
3201/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003202
mohammad160327010052018-07-03 13:16:15 +03003203#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003204void psa_key_policy_set_usage( psa_key_policy_t *policy,
3205 psa_key_usage_t usage,
3206 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003207{
mohammad16034eed7572018-03-28 05:14:59 -07003208 policy->usage = usage;
3209 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003210}
3211
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003212psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003213{
mohammad16036df908f2018-04-02 08:34:15 -07003214 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003215}
3216
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003217psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003218{
mohammad16036df908f2018-04-02 08:34:15 -07003219 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003220}
mohammad160327010052018-07-03 13:16:15 +03003221#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003222
Gilles Peskinec5487a82018-12-03 18:08:14 +01003223psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003224 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003225{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003226 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003227 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003228
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003229 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003230 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003231
Gilles Peskinec5487a82018-12-03 18:08:14 +01003232 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003233 if( status != PSA_SUCCESS )
3234 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003235
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003236 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3237 PSA_KEY_USAGE_ENCRYPT |
3238 PSA_KEY_USAGE_DECRYPT |
3239 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003240 PSA_KEY_USAGE_VERIFY |
3241 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003242 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003243
mohammad16036df908f2018-04-02 08:34:15 -07003244 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003245
3246 return( PSA_SUCCESS );
3247}
3248
Gilles Peskinec5487a82018-12-03 18:08:14 +01003249psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003250 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003251{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003252 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003253 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003254
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003255 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003256 return( PSA_ERROR_INVALID_ARGUMENT );
3257
Gilles Peskinec5487a82018-12-03 18:08:14 +01003258 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003259 if( status != PSA_SUCCESS )
3260 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003261
mohammad16036df908f2018-04-02 08:34:15 -07003262 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003263
3264 return( PSA_SUCCESS );
3265}
Gilles Peskine20035e32018-02-03 22:44:14 +01003266
Gilles Peskinea0655c32018-04-30 17:06:50 +02003267
3268
mohammad1603804cd712018-03-20 22:44:08 +02003269/****************************************************************/
3270/* Key Lifetime */
3271/****************************************************************/
3272
Gilles Peskinec5487a82018-12-03 18:08:14 +01003273psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003274 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003275{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003276 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003277 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003278
Gilles Peskinec5487a82018-12-03 18:08:14 +01003279 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003280 if( status != PSA_SUCCESS )
3281 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003282
mohammad1603804cd712018-03-20 22:44:08 +02003283 *lifetime = slot->lifetime;
3284
3285 return( PSA_SUCCESS );
3286}
3287
Gilles Peskine20035e32018-02-03 22:44:14 +01003288
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003289
mohammad16035955c982018-04-26 00:53:03 +03003290/****************************************************************/
3291/* AEAD */
3292/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003293
Gilles Peskineedf9a652018-08-17 18:11:56 +02003294typedef struct
3295{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003296 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003297 const mbedtls_cipher_info_t *cipher_info;
3298 union
3299 {
3300#if defined(MBEDTLS_CCM_C)
3301 mbedtls_ccm_context ccm;
3302#endif /* MBEDTLS_CCM_C */
3303#if defined(MBEDTLS_GCM_C)
3304 mbedtls_gcm_context gcm;
3305#endif /* MBEDTLS_GCM_C */
3306 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003307 psa_algorithm_t core_alg;
3308 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309 uint8_t tag_length;
3310} aead_operation_t;
3311
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003312static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003313{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003314 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003315 {
3316#if defined(MBEDTLS_CCM_C)
3317 case PSA_ALG_CCM:
3318 mbedtls_ccm_free( &operation->ctx.ccm );
3319 break;
3320#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003321#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003322 case PSA_ALG_GCM:
3323 mbedtls_gcm_free( &operation->ctx.gcm );
3324 break;
3325#endif /* MBEDTLS_GCM_C */
3326 }
3327}
3328
3329static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003330 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003331 psa_key_usage_t usage,
3332 psa_algorithm_t alg )
3333{
3334 psa_status_t status;
3335 size_t key_bits;
3336 mbedtls_cipher_id_t cipher_id;
3337
Gilles Peskinec5487a82018-12-03 18:08:14 +01003338 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003339 if( status != PSA_SUCCESS )
3340 return( status );
3341
3342 key_bits = psa_get_key_bits( operation->slot );
3343
3344 operation->cipher_info =
3345 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3346 &cipher_id );
3347 if( operation->cipher_info == NULL )
3348 return( PSA_ERROR_NOT_SUPPORTED );
3349
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003350 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003351 {
3352#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003353 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3354 operation->core_alg = PSA_ALG_CCM;
3355 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003356 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3357 return( PSA_ERROR_INVALID_ARGUMENT );
3358 mbedtls_ccm_init( &operation->ctx.ccm );
3359 status = mbedtls_to_psa_error(
3360 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3361 operation->slot->data.raw.data,
3362 (unsigned int) key_bits ) );
3363 if( status != 0 )
3364 goto cleanup;
3365 break;
3366#endif /* MBEDTLS_CCM_C */
3367
3368#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003369 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3370 operation->core_alg = PSA_ALG_GCM;
3371 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003372 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3373 return( PSA_ERROR_INVALID_ARGUMENT );
3374 mbedtls_gcm_init( &operation->ctx.gcm );
3375 status = mbedtls_to_psa_error(
3376 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3377 operation->slot->data.raw.data,
3378 (unsigned int) key_bits ) );
3379 break;
3380#endif /* MBEDTLS_GCM_C */
3381
3382 default:
3383 return( PSA_ERROR_NOT_SUPPORTED );
3384 }
3385
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003386 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3387 {
3388 status = PSA_ERROR_INVALID_ARGUMENT;
3389 goto cleanup;
3390 }
3391 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3392 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3393 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3394 * In both cases, mbedtls_xxx will validate the tag length below. */
3395
Gilles Peskineedf9a652018-08-17 18:11:56 +02003396 return( PSA_SUCCESS );
3397
3398cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003399 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003400 return( status );
3401}
3402
Gilles Peskinec5487a82018-12-03 18:08:14 +01003403psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003404 psa_algorithm_t alg,
3405 const uint8_t *nonce,
3406 size_t nonce_length,
3407 const uint8_t *additional_data,
3408 size_t additional_data_length,
3409 const uint8_t *plaintext,
3410 size_t plaintext_length,
3411 uint8_t *ciphertext,
3412 size_t ciphertext_size,
3413 size_t *ciphertext_length )
3414{
mohammad16035955c982018-04-26 00:53:03 +03003415 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003416 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003417 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003418
mohammad1603f08a5502018-06-03 15:05:47 +03003419 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003420
Gilles Peskinec5487a82018-12-03 18:08:14 +01003421 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003422 if( status != PSA_SUCCESS )
3423 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003424
Gilles Peskineedf9a652018-08-17 18:11:56 +02003425 /* For all currently supported modes, the tag is at the end of the
3426 * ciphertext. */
3427 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3428 {
3429 status = PSA_ERROR_BUFFER_TOO_SMALL;
3430 goto exit;
3431 }
3432 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003433
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003434#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003435 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003436 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003437 status = mbedtls_to_psa_error(
3438 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3439 MBEDTLS_GCM_ENCRYPT,
3440 plaintext_length,
3441 nonce, nonce_length,
3442 additional_data, additional_data_length,
3443 plaintext, ciphertext,
3444 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003445 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003446 else
3447#endif /* MBEDTLS_GCM_C */
3448#if defined(MBEDTLS_CCM_C)
3449 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003450 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003451 status = mbedtls_to_psa_error(
3452 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3453 plaintext_length,
3454 nonce, nonce_length,
3455 additional_data,
3456 additional_data_length,
3457 plaintext, ciphertext,
3458 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003459 }
mohammad16035c8845f2018-05-09 05:40:09 -07003460 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003461#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003462 {
mohammad1603554faad2018-06-03 15:07:38 +03003463 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003464 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003465
Gilles Peskineedf9a652018-08-17 18:11:56 +02003466 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3467 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003468
Gilles Peskineedf9a652018-08-17 18:11:56 +02003469exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003470 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003471 if( status == PSA_SUCCESS )
3472 *ciphertext_length = plaintext_length + operation.tag_length;
3473 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003474}
3475
Gilles Peskineee652a32018-06-01 19:23:52 +02003476/* Locate the tag in a ciphertext buffer containing the encrypted data
3477 * followed by the tag. Return the length of the part preceding the tag in
3478 * *plaintext_length. This is the size of the plaintext in modes where
3479 * the encrypted data has the same size as the plaintext, such as
3480 * CCM and GCM. */
3481static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3482 const uint8_t *ciphertext,
3483 size_t ciphertext_length,
3484 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003485 const uint8_t **p_tag )
3486{
3487 size_t payload_length;
3488 if( tag_length > ciphertext_length )
3489 return( PSA_ERROR_INVALID_ARGUMENT );
3490 payload_length = ciphertext_length - tag_length;
3491 if( payload_length > plaintext_size )
3492 return( PSA_ERROR_BUFFER_TOO_SMALL );
3493 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003494 return( PSA_SUCCESS );
3495}
3496
Gilles Peskinec5487a82018-12-03 18:08:14 +01003497psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003498 psa_algorithm_t alg,
3499 const uint8_t *nonce,
3500 size_t nonce_length,
3501 const uint8_t *additional_data,
3502 size_t additional_data_length,
3503 const uint8_t *ciphertext,
3504 size_t ciphertext_length,
3505 uint8_t *plaintext,
3506 size_t plaintext_size,
3507 size_t *plaintext_length )
3508{
mohammad16035955c982018-04-26 00:53:03 +03003509 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003510 aead_operation_t operation;
3511 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003512
Gilles Peskineee652a32018-06-01 19:23:52 +02003513 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003514
Gilles Peskinec5487a82018-12-03 18:08:14 +01003515 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003516 if( status != PSA_SUCCESS )
3517 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003518
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003519#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003520 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003521 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003522 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003523 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003524 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003525 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003526 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003527
Gilles Peskineedf9a652018-08-17 18:11:56 +02003528 status = mbedtls_to_psa_error(
3529 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3530 ciphertext_length - operation.tag_length,
3531 nonce, nonce_length,
3532 additional_data,
3533 additional_data_length,
3534 tag, operation.tag_length,
3535 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003536 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003537 else
3538#endif /* MBEDTLS_GCM_C */
3539#if defined(MBEDTLS_CCM_C)
3540 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003541 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003542 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003543 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003544 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003545 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003546 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003547
Gilles Peskineedf9a652018-08-17 18:11:56 +02003548 status = mbedtls_to_psa_error(
3549 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3550 ciphertext_length - operation.tag_length,
3551 nonce, nonce_length,
3552 additional_data,
3553 additional_data_length,
3554 ciphertext, plaintext,
3555 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003556 }
mohammad160339574652018-06-01 04:39:53 -07003557 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003558#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003559 {
mohammad1603554faad2018-06-03 15:07:38 +03003560 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003561 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003562
Gilles Peskineedf9a652018-08-17 18:11:56 +02003563 if( status != PSA_SUCCESS && plaintext_size != 0 )
3564 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003565
Gilles Peskineedf9a652018-08-17 18:11:56 +02003566exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003567 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003568 if( status == PSA_SUCCESS )
3569 *plaintext_length = ciphertext_length - operation.tag_length;
3570 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003571}
3572
Gilles Peskinea0655c32018-04-30 17:06:50 +02003573
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003574
Gilles Peskine20035e32018-02-03 22:44:14 +01003575/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003576/* Generators */
3577/****************************************************************/
3578
3579psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3580{
3581 psa_status_t status = PSA_SUCCESS;
3582 if( generator->alg == 0 )
3583 {
3584 /* The object has (apparently) been initialized but it is not
3585 * in use. It's ok to call abort on such an object, and there's
3586 * nothing to do. */
3587 }
3588 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003589 if( generator->alg == PSA_ALG_SELECT_RAW )
3590 {
3591 if( generator->ctx.buffer.data != NULL )
3592 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003593 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003594 generator->ctx.buffer.size );
3595 mbedtls_free( generator->ctx.buffer.data );
3596 }
3597 }
3598 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003599#if defined(MBEDTLS_MD_C)
3600 if( PSA_ALG_IS_HKDF( generator->alg ) )
3601 {
3602 mbedtls_free( generator->ctx.hkdf.info );
3603 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3604 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003605 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3606 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3607 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003608 {
3609 if( generator->ctx.tls12_prf.key != NULL )
3610 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003611 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003612 generator->ctx.tls12_prf.key_len );
3613 mbedtls_free( generator->ctx.tls12_prf.key );
3614 }
Hanno Becker580fba12018-11-13 20:50:45 +00003615
3616 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3617 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003618 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003619 generator->ctx.tls12_prf.Ai_with_seed_len );
3620 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3621 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003622 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003623 else
3624#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003625 {
3626 status = PSA_ERROR_BAD_STATE;
3627 }
3628 memset( generator, 0, sizeof( *generator ) );
3629 return( status );
3630}
3631
3632
3633psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3634 size_t *capacity)
3635{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003636 if( generator->alg == 0 )
3637 {
3638 /* This is a blank generator. */
3639 return PSA_ERROR_BAD_STATE;
3640 }
3641
Gilles Peskineeab56e42018-07-12 17:12:33 +02003642 *capacity = generator->capacity;
3643 return( PSA_SUCCESS );
3644}
3645
Gilles Peskinebef7f142018-07-12 17:22:21 +02003646#if defined(MBEDTLS_MD_C)
3647/* Read some bytes from an HKDF-based generator. This performs a chunk
3648 * of the expand phase of the HKDF algorithm. */
3649static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3650 psa_algorithm_t hash_alg,
3651 uint8_t *output,
3652 size_t output_length )
3653{
3654 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3655 psa_status_t status;
3656
3657 while( output_length != 0 )
3658 {
3659 /* Copy what remains of the current block */
3660 uint8_t n = hash_length - hkdf->offset_in_block;
3661 if( n > output_length )
3662 n = (uint8_t) output_length;
3663 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3664 output += n;
3665 output_length -= n;
3666 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003667 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003668 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003669 /* We can't be wanting more output after block 0xff, otherwise
3670 * the capacity check in psa_generator_read() would have
3671 * prevented this call. It could happen only if the generator
3672 * object was corrupted or if this function is called directly
3673 * inside the library. */
3674 if( hkdf->block_number == 0xff )
3675 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003676
3677 /* We need a new block */
3678 ++hkdf->block_number;
3679 hkdf->offset_in_block = 0;
3680 status = psa_hmac_setup_internal( &hkdf->hmac,
3681 hkdf->prk, hash_length,
3682 hash_alg );
3683 if( status != PSA_SUCCESS )
3684 return( status );
3685 if( hkdf->block_number != 1 )
3686 {
3687 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3688 hkdf->output_block,
3689 hash_length );
3690 if( status != PSA_SUCCESS )
3691 return( status );
3692 }
3693 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3694 hkdf->info,
3695 hkdf->info_length );
3696 if( status != PSA_SUCCESS )
3697 return( status );
3698 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3699 &hkdf->block_number, 1 );
3700 if( status != PSA_SUCCESS )
3701 return( status );
3702 status = psa_hmac_finish_internal( &hkdf->hmac,
3703 hkdf->output_block,
3704 sizeof( hkdf->output_block ) );
3705 if( status != PSA_SUCCESS )
3706 return( status );
3707 }
3708
3709 return( PSA_SUCCESS );
3710}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003711
3712static psa_status_t psa_generator_tls12_prf_generate_next_block(
3713 psa_tls12_prf_generator_t *tls12_prf,
3714 psa_algorithm_t alg )
3715{
3716 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3717 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3718 psa_hmac_internal_data hmac;
3719 psa_status_t status, cleanup_status;
3720
Hanno Becker3b339e22018-11-13 20:56:14 +00003721 unsigned char *Ai;
3722 size_t Ai_len;
3723
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003724 /* We can't be wanting more output after block 0xff, otherwise
3725 * the capacity check in psa_generator_read() would have
3726 * prevented this call. It could happen only if the generator
3727 * object was corrupted or if this function is called directly
3728 * inside the library. */
3729 if( tls12_prf->block_number == 0xff )
3730 return( PSA_ERROR_BAD_STATE );
3731
3732 /* We need a new block */
3733 ++tls12_prf->block_number;
3734 tls12_prf->offset_in_block = 0;
3735
3736 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3737 *
3738 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3739 *
3740 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3741 * HMAC_hash(secret, A(2) + seed) +
3742 * HMAC_hash(secret, A(3) + seed) + ...
3743 *
3744 * A(0) = seed
3745 * A(i) = HMAC_hash( secret, A(i-1) )
3746 *
3747 * The `psa_tls12_prf_generator` structures saves the block
3748 * `HMAC_hash(secret, A(i) + seed)` from which the output
3749 * is currently extracted as `output_block`, while
3750 * `A(i) + seed` is stored in `Ai_with_seed`.
3751 *
3752 * Generating a new block means recalculating `Ai_with_seed`
3753 * from the A(i)-part of it, and afterwards recalculating
3754 * `output_block`.
3755 *
3756 * A(0) is computed at setup time.
3757 *
3758 */
3759
3760 psa_hmac_init_internal( &hmac );
3761
3762 /* We must distinguish the calculation of A(1) from those
3763 * of A(2) and higher, because A(0)=seed has a different
3764 * length than the other A(i). */
3765 if( tls12_prf->block_number == 1 )
3766 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003767 Ai = tls12_prf->Ai_with_seed + hash_length;
3768 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003769 }
3770 else
3771 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003772 Ai = tls12_prf->Ai_with_seed;
3773 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003774 }
3775
Hanno Becker3b339e22018-11-13 20:56:14 +00003776 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3777 status = psa_hmac_setup_internal( &hmac,
3778 tls12_prf->key,
3779 tls12_prf->key_len,
3780 hash_alg );
3781 if( status != PSA_SUCCESS )
3782 goto cleanup;
3783
3784 status = psa_hash_update( &hmac.hash_ctx,
3785 Ai, Ai_len );
3786 if( status != PSA_SUCCESS )
3787 goto cleanup;
3788
3789 status = psa_hmac_finish_internal( &hmac,
3790 tls12_prf->Ai_with_seed,
3791 hash_length );
3792 if( status != PSA_SUCCESS )
3793 goto cleanup;
3794
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003795 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3796 status = psa_hmac_setup_internal( &hmac,
3797 tls12_prf->key,
3798 tls12_prf->key_len,
3799 hash_alg );
3800 if( status != PSA_SUCCESS )
3801 goto cleanup;
3802
3803 status = psa_hash_update( &hmac.hash_ctx,
3804 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003805 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003806 if( status != PSA_SUCCESS )
3807 goto cleanup;
3808
3809 status = psa_hmac_finish_internal( &hmac,
3810 tls12_prf->output_block,
3811 hash_length );
3812 if( status != PSA_SUCCESS )
3813 goto cleanup;
3814
3815cleanup:
3816
3817 cleanup_status = psa_hmac_abort_internal( &hmac );
3818 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3819 status = cleanup_status;
3820
3821 return( status );
3822}
3823
3824/* Read some bytes from an TLS-1.2-PRF-based generator.
3825 * See Section 5 of RFC 5246. */
3826static psa_status_t psa_generator_tls12_prf_read(
3827 psa_tls12_prf_generator_t *tls12_prf,
3828 psa_algorithm_t alg,
3829 uint8_t *output,
3830 size_t output_length )
3831{
3832 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3833 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3834 psa_status_t status;
3835
3836 while( output_length != 0 )
3837 {
3838 /* Copy what remains of the current block */
3839 uint8_t n = hash_length - tls12_prf->offset_in_block;
3840
3841 /* Check if we have fully processed the current block. */
3842 if( n == 0 )
3843 {
3844 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3845 alg );
3846 if( status != PSA_SUCCESS )
3847 return( status );
3848
3849 continue;
3850 }
3851
3852 if( n > output_length )
3853 n = (uint8_t) output_length;
3854 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3855 n );
3856 output += n;
3857 output_length -= n;
3858 tls12_prf->offset_in_block += n;
3859 }
3860
3861 return( PSA_SUCCESS );
3862}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003863#endif /* MBEDTLS_MD_C */
3864
Gilles Peskineeab56e42018-07-12 17:12:33 +02003865psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3866 uint8_t *output,
3867 size_t output_length )
3868{
3869 psa_status_t status;
3870
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003871 if( generator->alg == 0 )
3872 {
3873 /* This is a blank generator. */
3874 return PSA_ERROR_BAD_STATE;
3875 }
3876
Gilles Peskineeab56e42018-07-12 17:12:33 +02003877 if( output_length > generator->capacity )
3878 {
3879 generator->capacity = 0;
3880 /* Go through the error path to wipe all confidential data now
3881 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003882 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003883 goto exit;
3884 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003885 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003886 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003887 /* Edge case: this is a finished generator, and 0 bytes
3888 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3890 * INSUFFICIENT_CAPACITY, which is right for a finished
3891 * generator, for consistency with the case when
3892 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003893 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003894 }
3895 generator->capacity -= output_length;
3896
Gilles Peskine751d9652018-09-18 12:05:44 +02003897 if( generator->alg == PSA_ALG_SELECT_RAW )
3898 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003899 /* Initially, the capacity of a selection generator is always
3900 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3901 * abbreviated in this comment as `size`. When the remaining
3902 * capacity is `c`, the next bytes to serve start `c` bytes
3903 * from the end of the buffer, i.e. `size - c` from the
3904 * beginning of the buffer. Since `generator->capacity` was just
3905 * decremented above, we need to serve the bytes from
3906 * `size - generator->capacity - output_length` to
3907 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003908 size_t offset =
3909 generator->ctx.buffer.size - generator->capacity - output_length;
3910 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3911 status = PSA_SUCCESS;
3912 }
3913 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003914#if defined(MBEDTLS_MD_C)
3915 if( PSA_ALG_IS_HKDF( generator->alg ) )
3916 {
3917 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3918 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3919 output, output_length );
3920 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003921 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3922 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003923 {
3924 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3925 generator->alg, output,
3926 output_length );
3927 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003928 else
3929#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003930 {
3931 return( PSA_ERROR_BAD_STATE );
3932 }
3933
3934exit:
3935 if( status != PSA_SUCCESS )
3936 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003937 /* Preserve the algorithm upon errors, but clear all sensitive state.
3938 * This allows us to differentiate between exhausted generators and
3939 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3940 * generators. */
3941 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003942 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003943 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003944 memset( output, '!', output_length );
3945 }
3946 return( status );
3947}
3948
Gilles Peskine08542d82018-07-19 17:05:42 +02003949#if defined(MBEDTLS_DES_C)
3950static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3951{
3952 if( data_size >= 8 )
3953 mbedtls_des_key_set_parity( data );
3954 if( data_size >= 16 )
3955 mbedtls_des_key_set_parity( data + 8 );
3956 if( data_size >= 24 )
3957 mbedtls_des_key_set_parity( data + 16 );
3958}
3959#endif /* MBEDTLS_DES_C */
3960
Gilles Peskinec5487a82018-12-03 18:08:14 +01003961psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003962 psa_key_type_t type,
3963 size_t bits,
3964 psa_crypto_generator_t *generator )
3965{
3966 uint8_t *data = NULL;
3967 size_t bytes = PSA_BITS_TO_BYTES( bits );
3968 psa_status_t status;
3969
3970 if( ! key_type_is_raw_bytes( type ) )
3971 return( PSA_ERROR_INVALID_ARGUMENT );
3972 if( bits % 8 != 0 )
3973 return( PSA_ERROR_INVALID_ARGUMENT );
3974 data = mbedtls_calloc( 1, bytes );
3975 if( data == NULL )
3976 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3977
3978 status = psa_generator_read( generator, data, bytes );
3979 if( status != PSA_SUCCESS )
3980 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003981#if defined(MBEDTLS_DES_C)
3982 if( type == PSA_KEY_TYPE_DES )
3983 psa_des_set_key_parity( data, bytes );
3984#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003985 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003986
3987exit:
3988 mbedtls_free( data );
3989 return( status );
3990}
3991
3992
3993
3994/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003995/* Key derivation */
3996/****************************************************************/
3997
Gilles Peskinea05219c2018-11-16 16:02:56 +01003998#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003999/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004000 * of the HKDF algorithm.
4001 *
4002 * Note that if this function fails, you must call psa_generator_abort()
4003 * to potentially free embedded data structures and wipe confidential data.
4004 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004005static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004006 const uint8_t *secret,
4007 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004008 psa_algorithm_t hash_alg,
4009 const uint8_t *salt,
4010 size_t salt_length,
4011 const uint8_t *label,
4012 size_t label_length )
4013{
4014 psa_status_t status;
4015 status = psa_hmac_setup_internal( &hkdf->hmac,
4016 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004017 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004018 if( status != PSA_SUCCESS )
4019 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004020 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004021 if( status != PSA_SUCCESS )
4022 return( status );
4023 status = psa_hmac_finish_internal( &hkdf->hmac,
4024 hkdf->prk,
4025 sizeof( hkdf->prk ) );
4026 if( status != PSA_SUCCESS )
4027 return( status );
4028 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4029 hkdf->block_number = 0;
4030 hkdf->info_length = label_length;
4031 if( label_length != 0 )
4032 {
4033 hkdf->info = mbedtls_calloc( 1, label_length );
4034 if( hkdf->info == NULL )
4035 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4036 memcpy( hkdf->info, label, label_length );
4037 }
4038 return( PSA_SUCCESS );
4039}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004040#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004041
Gilles Peskinea05219c2018-11-16 16:02:56 +01004042#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004043/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4044 *
4045 * Note that if this function fails, you must call psa_generator_abort()
4046 * to potentially free embedded data structures and wipe confidential data.
4047 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004048static psa_status_t psa_generator_tls12_prf_setup(
4049 psa_tls12_prf_generator_t *tls12_prf,
4050 const unsigned char *key,
4051 size_t key_len,
4052 psa_algorithm_t hash_alg,
4053 const uint8_t *salt,
4054 size_t salt_length,
4055 const uint8_t *label,
4056 size_t label_length )
4057{
4058 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004059 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4060 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004061
4062 tls12_prf->key = mbedtls_calloc( 1, key_len );
4063 if( tls12_prf->key == NULL )
4064 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4065 tls12_prf->key_len = key_len;
4066 memcpy( tls12_prf->key, key, key_len );
4067
Hanno Becker580fba12018-11-13 20:50:45 +00004068 overflow = ( salt_length + label_length < salt_length ) ||
4069 ( salt_length + label_length + hash_length < hash_length );
4070 if( overflow )
4071 return( PSA_ERROR_INVALID_ARGUMENT );
4072
4073 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4074 if( tls12_prf->Ai_with_seed == NULL )
4075 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4076 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4077
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004078 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4079 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004080 if( label_length != 0 )
4081 {
4082 memcpy( tls12_prf->Ai_with_seed + hash_length,
4083 label, label_length );
4084 }
4085
4086 if( salt_length != 0 )
4087 {
4088 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4089 salt, salt_length );
4090 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004091
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004092 /* The first block gets generated when
4093 * psa_generator_read() is called. */
4094 tls12_prf->block_number = 0;
4095 tls12_prf->offset_in_block = hash_length;
4096
4097 return( PSA_SUCCESS );
4098}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004099
4100/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4101static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4102 psa_tls12_prf_generator_t *tls12_prf,
4103 const unsigned char *psk,
4104 size_t psk_len,
4105 psa_algorithm_t hash_alg,
4106 const uint8_t *salt,
4107 size_t salt_length,
4108 const uint8_t *label,
4109 size_t label_length )
4110{
4111 psa_status_t status;
4112 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4113
4114 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4115 return( PSA_ERROR_INVALID_ARGUMENT );
4116
4117 /* Quoting RFC 4279, Section 2:
4118 *
4119 * The premaster secret is formed as follows: if the PSK is N octets
4120 * long, concatenate a uint16 with the value N, N zero octets, a second
4121 * uint16 with the value N, and the PSK itself.
4122 */
4123
4124 pms[0] = ( psk_len >> 8 ) & 0xff;
4125 pms[1] = ( psk_len >> 0 ) & 0xff;
4126 memset( pms + 2, 0, psk_len );
4127 pms[2 + psk_len + 0] = pms[0];
4128 pms[2 + psk_len + 1] = pms[1];
4129 memcpy( pms + 4 + psk_len, psk, psk_len );
4130
4131 status = psa_generator_tls12_prf_setup( tls12_prf,
4132 pms, 4 + 2 * psk_len,
4133 hash_alg,
4134 salt, salt_length,
4135 label, label_length );
4136
Gilles Peskine3f108122018-12-07 18:14:53 +01004137 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004138 return( status );
4139}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004140#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004141
Gilles Peskine346797d2018-11-16 16:05:06 +01004142/* Note that if this function fails, you must call psa_generator_abort()
4143 * to potentially free embedded data structures and wipe confidential data.
4144 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004145static psa_status_t psa_key_derivation_internal(
4146 psa_crypto_generator_t *generator,
4147 const uint8_t *secret, size_t secret_length,
4148 psa_algorithm_t alg,
4149 const uint8_t *salt, size_t salt_length,
4150 const uint8_t *label, size_t label_length,
4151 size_t capacity )
4152{
4153 psa_status_t status;
4154 size_t max_capacity;
4155
4156 /* Set generator->alg even on failure so that abort knows what to do. */
4157 generator->alg = alg;
4158
Gilles Peskine751d9652018-09-18 12:05:44 +02004159 if( alg == PSA_ALG_SELECT_RAW )
4160 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004161 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004162 if( salt_length != 0 )
4163 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004164 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004165 if( label_length != 0 )
4166 return( PSA_ERROR_INVALID_ARGUMENT );
4167 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4168 if( generator->ctx.buffer.data == NULL )
4169 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4170 memcpy( generator->ctx.buffer.data, secret, secret_length );
4171 generator->ctx.buffer.size = secret_length;
4172 max_capacity = secret_length;
4173 status = PSA_SUCCESS;
4174 }
4175 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004176#if defined(MBEDTLS_MD_C)
4177 if( PSA_ALG_IS_HKDF( alg ) )
4178 {
4179 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4180 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4181 if( hash_size == 0 )
4182 return( PSA_ERROR_NOT_SUPPORTED );
4183 max_capacity = 255 * hash_size;
4184 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4185 secret, secret_length,
4186 hash_alg,
4187 salt, salt_length,
4188 label, label_length );
4189 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004190 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4191 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4192 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004193 {
4194 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4195 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4196
4197 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4198 if( hash_alg != PSA_ALG_SHA_256 &&
4199 hash_alg != PSA_ALG_SHA_384 )
4200 {
4201 return( PSA_ERROR_NOT_SUPPORTED );
4202 }
4203
4204 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004205
4206 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4207 {
4208 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4209 secret, secret_length,
4210 hash_alg, salt, salt_length,
4211 label, label_length );
4212 }
4213 else
4214 {
4215 status = psa_generator_tls12_psk_to_ms_setup(
4216 &generator->ctx.tls12_prf,
4217 secret, secret_length,
4218 hash_alg, salt, salt_length,
4219 label, label_length );
4220 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004221 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004222 else
4223#endif
4224 {
4225 return( PSA_ERROR_NOT_SUPPORTED );
4226 }
4227
4228 if( status != PSA_SUCCESS )
4229 return( status );
4230
4231 if( capacity <= max_capacity )
4232 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004233 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4234 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004235 else
4236 return( PSA_ERROR_INVALID_ARGUMENT );
4237
4238 return( PSA_SUCCESS );
4239}
4240
Gilles Peskineea0fb492018-07-12 17:17:20 +02004241psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004242 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004243 psa_algorithm_t alg,
4244 const uint8_t *salt,
4245 size_t salt_length,
4246 const uint8_t *label,
4247 size_t label_length,
4248 size_t capacity )
4249{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004250 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004251 psa_status_t status;
4252
4253 if( generator->alg != 0 )
4254 return( PSA_ERROR_BAD_STATE );
4255
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004256 /* Make sure that alg is a key derivation algorithm. This prevents
4257 * key selection algorithms, which psa_key_derivation_internal
4258 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004259 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4260 return( PSA_ERROR_INVALID_ARGUMENT );
4261
Gilles Peskinec5487a82018-12-03 18:08:14 +01004262 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004263 if( status != PSA_SUCCESS )
4264 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004265
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004266 if( slot->type != PSA_KEY_TYPE_DERIVE )
4267 return( PSA_ERROR_INVALID_ARGUMENT );
4268
4269 status = psa_key_derivation_internal( generator,
4270 slot->data.raw.data,
4271 slot->data.raw.bytes,
4272 alg,
4273 salt, salt_length,
4274 label, label_length,
4275 capacity );
4276 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004277 psa_generator_abort( generator );
4278 return( status );
4279}
4280
4281
4282
4283/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004284/* Key agreement */
4285/****************************************************************/
4286
Gilles Peskinea05219c2018-11-16 16:02:56 +01004287#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004288static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4289 size_t peer_key_length,
4290 const mbedtls_ecp_keypair *our_key,
4291 uint8_t *shared_secret,
4292 size_t shared_secret_size,
4293 size_t *shared_secret_length )
4294{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004295 mbedtls_ecp_keypair *their_key = NULL;
4296 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004297 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004298 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004299
Jaeden Ameroccdce902019-01-10 11:42:27 +00004300 status = psa_import_ec_public_key(
4301 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4302 peer_key, peer_key_length,
4303 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004304 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004305 goto exit;
4306
Jaeden Amero97271b32019-01-10 19:38:51 +00004307 status = mbedtls_to_psa_error(
4308 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4309 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004310 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004311 status = mbedtls_to_psa_error(
4312 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4313 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004314 goto exit;
4315
Jaeden Amero97271b32019-01-10 19:38:51 +00004316 status = mbedtls_to_psa_error(
4317 mbedtls_ecdh_calc_secret( &ecdh,
4318 shared_secret_length,
4319 shared_secret, shared_secret_size,
4320 mbedtls_ctr_drbg_random,
4321 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004322
4323exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004324 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004325 mbedtls_ecp_keypair_free( their_key );
4326 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004327 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004328}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004329#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004330
Gilles Peskine01d718c2018-09-18 12:01:02 +02004331#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4332
Gilles Peskine346797d2018-11-16 16:05:06 +01004333/* Note that if this function fails, you must call psa_generator_abort()
4334 * to potentially free embedded data structures and wipe confidential data.
4335 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004336static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004337 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004338 const uint8_t *peer_key,
4339 size_t peer_key_length,
4340 psa_algorithm_t alg )
4341{
4342 psa_status_t status;
4343 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4344 size_t shared_secret_length = 0;
4345
4346 /* Step 1: run the secret agreement algorithm to generate the shared
4347 * secret. */
4348 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4349 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004350#if defined(MBEDTLS_ECDH_C)
4351 case PSA_ALG_ECDH_BASE:
4352 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4353 return( PSA_ERROR_INVALID_ARGUMENT );
4354 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4355 private_key->data.ecp,
4356 shared_secret,
4357 sizeof( shared_secret ),
4358 &shared_secret_length );
4359 break;
4360#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004361 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004362 (void) private_key;
4363 (void) peer_key;
4364 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004365 return( PSA_ERROR_NOT_SUPPORTED );
4366 }
4367 if( status != PSA_SUCCESS )
4368 goto exit;
4369
4370 /* Step 2: set up the key derivation to generate key material from
4371 * the shared secret. */
4372 status = psa_key_derivation_internal( generator,
4373 shared_secret, shared_secret_length,
4374 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4375 NULL, 0, NULL, 0,
4376 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4377exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004378 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004379 return( status );
4380}
4381
4382psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004383 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004384 const uint8_t *peer_key,
4385 size_t peer_key_length,
4386 psa_algorithm_t alg )
4387{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004388 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004389 psa_status_t status;
4390 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4391 return( PSA_ERROR_INVALID_ARGUMENT );
4392 status = psa_get_key_from_slot( private_key, &slot,
4393 PSA_KEY_USAGE_DERIVE, alg );
4394 if( status != PSA_SUCCESS )
4395 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004396 status = psa_key_agreement_internal( generator,
4397 slot,
4398 peer_key, peer_key_length,
4399 alg );
4400 if( status != PSA_SUCCESS )
4401 psa_generator_abort( generator );
4402 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004403}
4404
4405
4406
4407/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004408/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004409/****************************************************************/
4410
4411psa_status_t psa_generate_random( uint8_t *output,
4412 size_t output_size )
4413{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004414 int ret;
4415 GUARD_MODULE_INITIALIZED;
4416
4417 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004418 return( mbedtls_to_psa_error( ret ) );
4419}
4420
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004421#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4422#include "mbedtls/entropy_poll.h"
4423
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004424psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4425 size_t seed_size )
4426{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004427 if( global_data.initialized )
4428 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004429
4430 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4431 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4432 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4433 return( PSA_ERROR_INVALID_ARGUMENT );
4434
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004435 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004436}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004437#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004438
Gilles Peskinec5487a82018-12-03 18:08:14 +01004439psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004440 psa_key_type_t type,
4441 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004442 const void *extra,
4443 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004444{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004445 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004446 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004447
Gilles Peskine53d991e2018-07-12 01:14:59 +02004448 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004449 return( PSA_ERROR_INVALID_ARGUMENT );
4450
Gilles Peskinec5487a82018-12-03 18:08:14 +01004451 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004452 if( status != PSA_SUCCESS )
4453 return( status );
4454
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004455 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004456 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004457 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004458 if( status != PSA_SUCCESS )
4459 return( status );
4460 status = psa_generate_random( slot->data.raw.data,
4461 slot->data.raw.bytes );
4462 if( status != PSA_SUCCESS )
4463 {
4464 mbedtls_free( slot->data.raw.data );
4465 return( status );
4466 }
4467#if defined(MBEDTLS_DES_C)
4468 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004469 psa_des_set_key_parity( slot->data.raw.data,
4470 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004471#endif /* MBEDTLS_DES_C */
4472 }
4473 else
4474
4475#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4476 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4477 {
4478 mbedtls_rsa_context *rsa;
4479 int ret;
4480 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004481 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4482 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004483 /* Accept only byte-aligned keys, for the same reasons as
4484 * in psa_import_rsa_key(). */
4485 if( bits % 8 != 0 )
4486 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004487 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004488 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004489 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004490 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004491 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004492#if INT_MAX < 0xffffffff
4493 /* Check that the uint32_t value passed by the caller fits
4494 * in the range supported by this implementation. */
4495 if( p->e > INT_MAX )
4496 return( PSA_ERROR_NOT_SUPPORTED );
4497#endif
4498 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004499 }
4500 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4501 if( rsa == NULL )
4502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4503 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4504 ret = mbedtls_rsa_gen_key( rsa,
4505 mbedtls_ctr_drbg_random,
4506 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004507 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004508 exponent );
4509 if( ret != 0 )
4510 {
4511 mbedtls_rsa_free( rsa );
4512 mbedtls_free( rsa );
4513 return( mbedtls_to_psa_error( ret ) );
4514 }
4515 slot->data.rsa = rsa;
4516 }
4517 else
4518#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4519
4520#if defined(MBEDTLS_ECP_C)
4521 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4522 {
4523 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4524 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4525 const mbedtls_ecp_curve_info *curve_info =
4526 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4527 mbedtls_ecp_keypair *ecp;
4528 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004529 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004530 return( PSA_ERROR_NOT_SUPPORTED );
4531 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4532 return( PSA_ERROR_NOT_SUPPORTED );
4533 if( curve_info->bit_size != bits )
4534 return( PSA_ERROR_INVALID_ARGUMENT );
4535 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4536 if( ecp == NULL )
4537 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4538 mbedtls_ecp_keypair_init( ecp );
4539 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4540 mbedtls_ctr_drbg_random,
4541 &global_data.ctr_drbg );
4542 if( ret != 0 )
4543 {
4544 mbedtls_ecp_keypair_free( ecp );
4545 mbedtls_free( ecp );
4546 return( mbedtls_to_psa_error( ret ) );
4547 }
4548 slot->data.ecp = ecp;
4549 }
4550 else
4551#endif /* MBEDTLS_ECP_C */
4552
4553 return( PSA_ERROR_NOT_SUPPORTED );
4554
4555 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004556
4557#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4558 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4559 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004560 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004561 }
4562#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4563
4564 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004565}
4566
4567
4568/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004569/* Module setup */
4570/****************************************************************/
4571
Gilles Peskine5e769522018-11-20 21:59:56 +01004572psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4573 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4574 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4575{
4576 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4577 return( PSA_ERROR_BAD_STATE );
4578 global_data.entropy_init = entropy_init;
4579 global_data.entropy_free = entropy_free;
4580 return( PSA_SUCCESS );
4581}
4582
Gilles Peskinee59236f2018-01-27 23:32:46 +01004583void mbedtls_psa_crypto_free( void )
4584{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004585 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004586 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4587 {
4588 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004589 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004590 }
4591 /* Wipe all remaining data, including configuration.
4592 * In particular, this sets all state indicator to the value
4593 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004594 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004595}
4596
4597psa_status_t psa_crypto_init( void )
4598{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004599 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004600 const unsigned char drbg_seed[] = "PSA";
4601
Gilles Peskinec6b69072018-11-20 21:42:52 +01004602 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004603 if( global_data.initialized != 0 )
4604 return( PSA_SUCCESS );
4605
Gilles Peskine5e769522018-11-20 21:59:56 +01004606 /* Set default configuration if
4607 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4608 if( global_data.entropy_init == NULL )
4609 global_data.entropy_init = mbedtls_entropy_init;
4610 if( global_data.entropy_free == NULL )
4611 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004612
Gilles Peskinec6b69072018-11-20 21:42:52 +01004613 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004614 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004615 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004616 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004617 status = mbedtls_to_psa_error(
4618 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4619 mbedtls_entropy_func,
4620 &global_data.entropy,
4621 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4622 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004623 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004624 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004625
Gilles Peskine66fb1262018-12-10 16:29:04 +01004626 status = psa_initialize_key_slots( );
4627 if( status != PSA_SUCCESS )
4628 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004629
4630 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004631 global_data.initialized = 1;
4632
Gilles Peskinee59236f2018-01-27 23:32:46 +01004633exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004634 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004635 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004636 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004637}
4638
4639#endif /* MBEDTLS_PSA_CRYPTO_C */