blob: 1b554b5b04d15c09c256b2568e3593dd72e68d18 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020063#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Gilles Peskine996deb12018-08-01 15:45:45 +020081#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
82
Gilles Peskine9ef733f2018-02-07 21:05:37 +010083/* constant-time buffer comparison */
84static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
85{
86 size_t i;
87 unsigned char diff = 0;
88
89 for( i = 0; i < n; i++ )
90 diff |= a[i] ^ b[i];
91
92 return( diff );
93}
94
95
96
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/****************************************************************/
98/* Global data, support functions and library management */
99/****************************************************************/
100
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101static int key_type_is_raw_bytes( psa_key_type_t type )
102{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200103 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104}
105
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106/* Values for psa_global_data_t::rng_state */
107#define RNG_NOT_INITIALIZED 0
108#define RNG_INITIALIZED 1
109#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100110
Gilles Peskine2d277862018-06-18 15:41:12 +0200111typedef struct
112{
Gilles Peskine5e769522018-11-20 21:59:56 +0100113 void (* entropy_init )( mbedtls_entropy_context *ctx );
114 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100115 mbedtls_entropy_context entropy;
116 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100118 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119} psa_global_data_t;
120
121static psa_global_data_t global_data;
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
124 if( global_data.initialized == 0 ) \
125 return( PSA_ERROR_BAD_STATE );
126
Gilles Peskinee59236f2018-01-27 23:32:46 +0100127static psa_status_t mbedtls_to_psa_error( int ret )
128{
Gilles Peskinea5905292018-02-07 20:59:33 +0100129 /* If there's both a high-level code and low-level code, dispatch on
130 * the high-level code. */
131 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 {
133 case 0:
134 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100135
136 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
137 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
138 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
139 return( PSA_ERROR_NOT_SUPPORTED );
140 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
141 return( PSA_ERROR_HARDWARE_FAILURE );
142
143 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
Gilles Peskine9a944802018-06-21 09:35:35 +0200146 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
147 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
148 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
149 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
150 case MBEDTLS_ERR_ASN1_INVALID_DATA:
151 return( PSA_ERROR_INVALID_ARGUMENT );
152 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
153 return( PSA_ERROR_INSUFFICIENT_MEMORY );
154 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
155 return( PSA_ERROR_BUFFER_TOO_SMALL );
156
Jaeden Amero93e21112019-02-20 13:57:28 +0000157#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000158 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
160 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
161#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000168 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
170 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
171#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
173 return( PSA_ERROR_NOT_SUPPORTED );
174 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
175 return( PSA_ERROR_HARDWARE_FAILURE );
176
177 case MBEDTLS_ERR_CCM_BAD_INPUT:
178 return( PSA_ERROR_INVALID_ARGUMENT );
179 case MBEDTLS_ERR_CCM_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
181 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
182 return( PSA_ERROR_HARDWARE_FAILURE );
183
184 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
185 return( PSA_ERROR_NOT_SUPPORTED );
186 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
189 return( PSA_ERROR_INSUFFICIENT_MEMORY );
190 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
191 return( PSA_ERROR_INVALID_PADDING );
192 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
197 return( PSA_ERROR_TAMPERING_DETECTED );
198 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
199 return( PSA_ERROR_HARDWARE_FAILURE );
200
201 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
205 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
206 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
207 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
208 return( PSA_ERROR_NOT_SUPPORTED );
209 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
210 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
211
212 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
213 return( PSA_ERROR_NOT_SUPPORTED );
214 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
Gilles Peskinee59236f2018-01-27 23:32:46 +0100217 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
218 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
219 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
222 case MBEDTLS_ERR_GCM_AUTH_FAILED:
223 return( PSA_ERROR_INVALID_SIGNATURE );
224 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200225 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
229 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
231 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
232 return( PSA_ERROR_HARDWARE_FAILURE );
233
234 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
235 return( PSA_ERROR_NOT_SUPPORTED );
236 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
237 return( PSA_ERROR_INVALID_ARGUMENT );
238 case MBEDTLS_ERR_MD_ALLOC_FAILED:
239 return( PSA_ERROR_INSUFFICIENT_MEMORY );
240 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
241 return( PSA_ERROR_STORAGE_FAILURE );
242 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
Gilles Peskinef76aa772018-10-29 19:24:33 +0100245 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
246 return( PSA_ERROR_STORAGE_FAILURE );
247 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
252 return( PSA_ERROR_BUFFER_TOO_SMALL );
253 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
254 return( PSA_ERROR_INVALID_ARGUMENT );
255 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_MEMORY );
261
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 case MBEDTLS_ERR_PK_ALLOC_FAILED:
263 return( PSA_ERROR_INSUFFICIENT_MEMORY );
264 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
265 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100269 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
270 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
273 return( PSA_ERROR_NOT_SUPPORTED );
274 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
275 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
276 return( PSA_ERROR_NOT_PERMITTED );
277 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_PK_INVALID_ALG:
280 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
281 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
284 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
286 return( PSA_ERROR_HARDWARE_FAILURE );
287
288 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
289 return( PSA_ERROR_HARDWARE_FAILURE );
290
291 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
292 return( PSA_ERROR_INVALID_ARGUMENT );
293 case MBEDTLS_ERR_RSA_INVALID_PADDING:
294 return( PSA_ERROR_INVALID_PADDING );
295 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
300 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
301 return( PSA_ERROR_TAMPERING_DETECTED );
302 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
303 return( PSA_ERROR_INVALID_SIGNATURE );
304 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
305 return( PSA_ERROR_BUFFER_TOO_SMALL );
306 case MBEDTLS_ERR_RSA_RNG_FAILED:
307 return( PSA_ERROR_INSUFFICIENT_MEMORY );
308 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
309 return( PSA_ERROR_NOT_SUPPORTED );
310 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312
313 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
315 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
316 return( PSA_ERROR_HARDWARE_FAILURE );
317
318 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
319 return( PSA_ERROR_INVALID_ARGUMENT );
320 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
itayzafrir5c753392018-05-08 11:18:38 +0300323 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300324 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300325 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300326 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
327 return( PSA_ERROR_BUFFER_TOO_SMALL );
328 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
329 return( PSA_ERROR_NOT_SUPPORTED );
330 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
331 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
332 return( PSA_ERROR_INVALID_SIGNATURE );
333 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
334 return( PSA_ERROR_INSUFFICIENT_MEMORY );
335 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300337
Gilles Peskinee59236f2018-01-27 23:32:46 +0100338 default:
David Saadab4ecc272019-02-14 13:48:10 +0200339 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100340 }
341}
342
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200343
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200344
345
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346/****************************************************************/
347/* Key management */
348/****************************************************************/
349
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100350#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200351static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
352{
353 switch( grpid )
354 {
355 case MBEDTLS_ECP_DP_SECP192R1:
356 return( PSA_ECC_CURVE_SECP192R1 );
357 case MBEDTLS_ECP_DP_SECP224R1:
358 return( PSA_ECC_CURVE_SECP224R1 );
359 case MBEDTLS_ECP_DP_SECP256R1:
360 return( PSA_ECC_CURVE_SECP256R1 );
361 case MBEDTLS_ECP_DP_SECP384R1:
362 return( PSA_ECC_CURVE_SECP384R1 );
363 case MBEDTLS_ECP_DP_SECP521R1:
364 return( PSA_ECC_CURVE_SECP521R1 );
365 case MBEDTLS_ECP_DP_BP256R1:
366 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
367 case MBEDTLS_ECP_DP_BP384R1:
368 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
369 case MBEDTLS_ECP_DP_BP512R1:
370 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
371 case MBEDTLS_ECP_DP_CURVE25519:
372 return( PSA_ECC_CURVE_CURVE25519 );
373 case MBEDTLS_ECP_DP_SECP192K1:
374 return( PSA_ECC_CURVE_SECP192K1 );
375 case MBEDTLS_ECP_DP_SECP224K1:
376 return( PSA_ECC_CURVE_SECP224K1 );
377 case MBEDTLS_ECP_DP_SECP256K1:
378 return( PSA_ECC_CURVE_SECP256K1 );
379 case MBEDTLS_ECP_DP_CURVE448:
380 return( PSA_ECC_CURVE_CURVE448 );
381 default:
382 return( 0 );
383 }
384}
385
Gilles Peskine12313cd2018-06-20 00:20:32 +0200386static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
387{
388 switch( curve )
389 {
390 case PSA_ECC_CURVE_SECP192R1:
391 return( MBEDTLS_ECP_DP_SECP192R1 );
392 case PSA_ECC_CURVE_SECP224R1:
393 return( MBEDTLS_ECP_DP_SECP224R1 );
394 case PSA_ECC_CURVE_SECP256R1:
395 return( MBEDTLS_ECP_DP_SECP256R1 );
396 case PSA_ECC_CURVE_SECP384R1:
397 return( MBEDTLS_ECP_DP_SECP384R1 );
398 case PSA_ECC_CURVE_SECP521R1:
399 return( MBEDTLS_ECP_DP_SECP521R1 );
400 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 case PSA_ECC_CURVE_CURVE25519:
407 return( MBEDTLS_ECP_DP_CURVE25519 );
408 case PSA_ECC_CURVE_SECP192K1:
409 return( MBEDTLS_ECP_DP_SECP192K1 );
410 case PSA_ECC_CURVE_SECP224K1:
411 return( MBEDTLS_ECP_DP_SECP224K1 );
412 case PSA_ECC_CURVE_SECP256K1:
413 return( MBEDTLS_ECP_DP_SECP256K1 );
414 case PSA_ECC_CURVE_CURVE448:
415 return( MBEDTLS_ECP_DP_CURVE448 );
416 default:
417 return( MBEDTLS_ECP_DP_NONE );
418 }
419}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100420#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200421
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200422static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
423 size_t bits,
424 struct raw_data *raw )
425{
426 /* Check that the bit size is acceptable for the key type */
427 switch( type )
428 {
429 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200430 if( bits == 0 )
431 {
432 raw->bytes = 0;
433 raw->data = NULL;
434 return( PSA_SUCCESS );
435 }
436 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200437#if defined(MBEDTLS_MD_C)
438 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200439#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200440 case PSA_KEY_TYPE_DERIVE:
441 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200442#if defined(MBEDTLS_AES_C)
443 case PSA_KEY_TYPE_AES:
444 if( bits != 128 && bits != 192 && bits != 256 )
445 return( PSA_ERROR_INVALID_ARGUMENT );
446 break;
447#endif
448#if defined(MBEDTLS_CAMELLIA_C)
449 case PSA_KEY_TYPE_CAMELLIA:
450 if( bits != 128 && bits != 192 && bits != 256 )
451 return( PSA_ERROR_INVALID_ARGUMENT );
452 break;
453#endif
454#if defined(MBEDTLS_DES_C)
455 case PSA_KEY_TYPE_DES:
456 if( bits != 64 && bits != 128 && bits != 192 )
457 return( PSA_ERROR_INVALID_ARGUMENT );
458 break;
459#endif
460#if defined(MBEDTLS_ARC4_C)
461 case PSA_KEY_TYPE_ARC4:
462 if( bits < 8 || bits > 2048 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466 default:
467 return( PSA_ERROR_NOT_SUPPORTED );
468 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200469 if( bits % 8 != 0 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200471
472 /* Allocate memory for the key */
473 raw->bytes = PSA_BITS_TO_BYTES( bits );
474 raw->data = mbedtls_calloc( 1, raw->bytes );
475 if( raw->data == NULL )
476 {
477 raw->bytes = 0;
478 return( PSA_ERROR_INSUFFICIENT_MEMORY );
479 }
480 return( PSA_SUCCESS );
481}
482
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200483#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100484/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
485 * that are not a multiple of 8) well. For example, there is only
486 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
487 * way to return the exact bit size of a key.
488 * To keep things simple, reject non-byte-aligned key sizes. */
489static psa_status_t psa_check_rsa_key_byte_aligned(
490 const mbedtls_rsa_context *rsa )
491{
492 mbedtls_mpi n;
493 psa_status_t status;
494 mbedtls_mpi_init( &n );
495 status = mbedtls_to_psa_error(
496 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
497 if( status == PSA_SUCCESS )
498 {
499 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
500 status = PSA_ERROR_NOT_SUPPORTED;
501 }
502 mbedtls_mpi_free( &n );
503 return( status );
504}
505
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000506static psa_status_t psa_import_rsa_key( psa_key_type_t type,
507 const uint8_t *data,
508 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200509 mbedtls_rsa_context **p_rsa )
510{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000511 psa_status_t status;
512 mbedtls_pk_context pk;
513 mbedtls_rsa_context *rsa;
514 size_t bits;
515
516 mbedtls_pk_init( &pk );
517
518 /* Parse the data. */
519 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
520 status = mbedtls_to_psa_error(
521 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200522 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000523 status = mbedtls_to_psa_error(
524 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
525 if( status != PSA_SUCCESS )
526 goto exit;
527
528 /* We have something that the pkparse module recognizes. If it is a
529 * valid RSA key, store it. */
530 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532 status = PSA_ERROR_INVALID_ARGUMENT;
533 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535
536 rsa = mbedtls_pk_rsa( pk );
537 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
538 * supports non-byte-aligned key sizes, but not well. For example,
539 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
540 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
541 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
542 {
543 status = PSA_ERROR_NOT_SUPPORTED;
544 goto exit;
545 }
546 status = psa_check_rsa_key_byte_aligned( rsa );
547
548exit:
549 /* Free the content of the pk object only on error. */
550 if( status != PSA_SUCCESS )
551 {
552 mbedtls_pk_free( &pk );
553 return( status );
554 }
555
556 /* On success, store the content of the object in the RSA context. */
557 *p_rsa = rsa;
558
559 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560}
561#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
562
Jaeden Ameroccdce902019-01-10 11:42:27 +0000563#if defined(MBEDTLS_ECP_C)
564
565/* Import a public key given as the uncompressed representation defined by SEC1
566 * 2.3.3 as the content of an ECPoint. */
567static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
568 const uint8_t *data,
569 size_t data_length,
570 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200571{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000572 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
573 mbedtls_ecp_keypair *ecp = NULL;
574 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
575
576 *p_ecp = NULL;
577 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
578 if( ecp == NULL )
579 return( PSA_ERROR_INSUFFICIENT_MEMORY );
580 mbedtls_ecp_keypair_init( ecp );
581
582 /* Load the group. */
583 status = mbedtls_to_psa_error(
584 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
585 if( status != PSA_SUCCESS )
586 goto exit;
587 /* Load the public value. */
588 status = mbedtls_to_psa_error(
589 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
590 data, data_length ) );
591 if( status != PSA_SUCCESS )
592 goto exit;
593
594 /* Check that the point is on the curve. */
595 status = mbedtls_to_psa_error(
596 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
597 if( status != PSA_SUCCESS )
598 goto exit;
599
600 *p_ecp = ecp;
601 return( PSA_SUCCESS );
602
603exit:
604 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200605 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000606 mbedtls_ecp_keypair_free( ecp );
607 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200608 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000609 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200610}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000611#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200612
Gilles Peskinef76aa772018-10-29 19:24:33 +0100613#if defined(MBEDTLS_ECP_C)
614/* Import a private key given as a byte string which is the private value
615 * in big-endian order. */
616static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
617 const uint8_t *data,
618 size_t data_length,
619 mbedtls_ecp_keypair **p_ecp )
620{
621 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
622 mbedtls_ecp_keypair *ecp = NULL;
623 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
624
625 *p_ecp = NULL;
626 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
627 if( ecp == NULL )
628 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000629 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100630
631 /* Load the group. */
632 status = mbedtls_to_psa_error(
633 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
634 if( status != PSA_SUCCESS )
635 goto exit;
636 /* Load the secret value. */
637 status = mbedtls_to_psa_error(
638 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
639 if( status != PSA_SUCCESS )
640 goto exit;
641 /* Validate the private key. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646 /* Calculate the public key from the private key. */
647 status = mbedtls_to_psa_error(
648 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
649 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
650 if( status != PSA_SUCCESS )
651 goto exit;
652
653 *p_ecp = ecp;
654 return( PSA_SUCCESS );
655
656exit:
657 if( ecp != NULL )
658 {
659 mbedtls_ecp_keypair_free( ecp );
660 mbedtls_free( ecp );
661 }
662 return( status );
663}
664#endif /* defined(MBEDTLS_ECP_C) */
665
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100666/** Import key data into a slot. `slot->type` must have been set
667 * previously. This function assumes that the slot does not contain
668 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100669psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
670 const uint8_t *data,
671 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200673 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Darryl Green940d72c2018-07-13 13:18:51 +0100675 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100677 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 if( data_length > SIZE_MAX / 8 )
679 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100680 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200681 PSA_BYTES_TO_BITS( data_length ),
682 &slot->data.raw );
683 if( status != PSA_SUCCESS )
684 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200685 if( data_length != 0 )
686 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 }
688 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100689#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100690 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100691 {
Darryl Green940d72c2018-07-13 13:18:51 +0100692 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100693 data, data_length,
694 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100695 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000696 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000698 status = psa_import_ec_public_key(
699 PSA_KEY_TYPE_GET_CURVE( slot->type ),
700 data, data_length,
701 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702 }
703 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100704#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000705#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
706 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000708 status = psa_import_rsa_key( slot->type,
709 data, data_length,
710 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 }
712 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000713#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100714 {
715 return( PSA_ERROR_NOT_SUPPORTED );
716 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000717 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100718}
719
Darryl Green06fd18d2018-07-16 11:21:11 +0100720/* Retrieve an empty key slot (slot with no key data, but possibly
721 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100722static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100723 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100724{
725 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100726 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100727
728 *p_slot = NULL;
729
Gilles Peskinec5487a82018-12-03 18:08:14 +0100730 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100731 if( status != PSA_SUCCESS )
732 return( status );
733
734 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200735 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100736
737 *p_slot = slot;
738 return( status );
739}
740
Gilles Peskinef603c712019-01-19 13:40:11 +0100741/** Calculate the intersection of two algorithm usage policies.
742 *
743 * Return 0 (which allows no operation) on incompatibility.
744 */
745static psa_algorithm_t psa_key_policy_algorithm_intersection(
746 psa_algorithm_t alg1,
747 psa_algorithm_t alg2 )
748{
749 /* Common case: the policy only allows alg. */
750 if( alg1 == alg2 )
751 return( alg1 );
752 /* If the policies are from the same hash-and-sign family, check
753 * if one is a wildcard. If so the other has the specific algorithm. */
754 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
755 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
756 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
757 {
758 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
759 return( alg2 );
760 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
761 return( alg1 );
762 }
763 /* If the policies are incompatible, allow nothing. */
764 return( 0 );
765}
766
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100767/** Test whether a policy permits an algorithm.
768 *
769 * The caller must test usage flags separately.
770 */
771static int psa_key_policy_permits( const psa_key_policy_t *policy,
772 psa_algorithm_t alg )
773{
774 /* Common case: the policy only allows alg. */
775 if( alg == policy->alg )
776 return( 1 );
777 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
778 * and alg is the same hash-and-sign family with any hash,
779 * then alg is compliant with policy->alg. */
780 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
781 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
782 {
783 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
784 ( alg & ~PSA_ALG_HASH_MASK ) );
785 }
786 /* If it isn't permitted, it's forbidden. */
787 return( 0 );
788}
789
Gilles Peskinef603c712019-01-19 13:40:11 +0100790/** Restrict a key policy based on a constraint.
791 *
792 * \param[in,out] policy The policy to restrict.
793 * \param[in] constraint The policy constraint to apply.
794 *
795 * \retval #PSA_SUCCESS
796 * \c *policy contains the intersection of the original value of
797 * \c *policy and \c *constraint.
798 * \retval #PSA_ERROR_INVALID_ARGUMENT
799 * \c *policy and \c *constraint are incompatible.
800 * \c *policy is unchanged.
801 */
802static psa_status_t psa_restrict_key_policy(
803 psa_key_policy_t *policy,
804 const psa_key_policy_t *constraint )
805{
806 psa_algorithm_t intersection_alg =
807 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
808 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
809 return( PSA_ERROR_INVALID_ARGUMENT );
810 policy->usage &= constraint->usage;
811 policy->alg = intersection_alg;
812 return( PSA_SUCCESS );
813}
814
Darryl Green06fd18d2018-07-16 11:21:11 +0100815/** Retrieve a slot which must contain a key. The key must have allow all the
816 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
817 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100818static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100819 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100820 psa_key_usage_t usage,
821 psa_algorithm_t alg )
822{
823 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100824 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100825
826 *p_slot = NULL;
827
Gilles Peskinec5487a82018-12-03 18:08:14 +0100828 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100829 if( status != PSA_SUCCESS )
830 return( status );
831 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200832 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100833
834 /* Enforce that usage policy for the key slot contains all the flags
835 * required by the usage parameter. There is one exception: public
836 * keys can always be exported, so we treat public key objects as
837 * if they had the export flag. */
838 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
839 usage &= ~PSA_KEY_USAGE_EXPORT;
840 if( ( slot->policy.usage & usage ) != usage )
841 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100842
843 /* Enforce that the usage policy permits the requested algortihm. */
844 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100845 return( PSA_ERROR_NOT_PERMITTED );
846
847 *p_slot = slot;
848 return( PSA_SUCCESS );
849}
Darryl Green940d72c2018-07-13 13:18:51 +0100850
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100851/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100852static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000853{
854 if( slot->type == PSA_KEY_TYPE_NONE )
855 {
856 /* No key material to clean. */
857 }
858 else if( key_type_is_raw_bytes( slot->type ) )
859 {
860 mbedtls_free( slot->data.raw.data );
861 }
862 else
863#if defined(MBEDTLS_RSA_C)
864 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
865 {
866 mbedtls_rsa_free( slot->data.rsa );
867 mbedtls_free( slot->data.rsa );
868 }
869 else
870#endif /* defined(MBEDTLS_RSA_C) */
871#if defined(MBEDTLS_ECP_C)
872 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
873 {
874 mbedtls_ecp_keypair_free( slot->data.ecp );
875 mbedtls_free( slot->data.ecp );
876 }
877 else
878#endif /* defined(MBEDTLS_ECP_C) */
879 {
880 /* Shouldn't happen: the key type is not any type that we
881 * put in. */
882 return( PSA_ERROR_TAMPERING_DETECTED );
883 }
884
885 return( PSA_SUCCESS );
886}
887
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100888/** Completely wipe a slot in memory, including its policy.
889 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100890psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100891{
892 psa_status_t status = psa_remove_key_data_from_memory( slot );
893 /* At this point, key material and other type-specific content has
894 * been wiped. Clear remaining metadata. We can call memset and not
895 * zeroize because the metadata is not particularly sensitive. */
896 memset( slot, 0, sizeof( *slot ) );
897 return( status );
898}
899
Gilles Peskinec5487a82018-12-03 18:08:14 +0100900psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100901 psa_key_type_t type,
902 const uint8_t *data,
903 size_t data_length )
904{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100905 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100906 psa_status_t status;
907
Gilles Peskinec5487a82018-12-03 18:08:14 +0100908 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100909 if( status != PSA_SUCCESS )
910 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100911
912 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100913
914 status = psa_import_key_into_slot( slot, data, data_length );
915 if( status != PSA_SUCCESS )
916 {
917 slot->type = PSA_KEY_TYPE_NONE;
918 return( status );
919 }
920
Darryl Greend49a4992018-06-18 17:27:26 +0100921#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
922 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
923 {
924 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100925 status = psa_save_persistent_key( slot->persistent_storage_id,
926 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100927 data_length );
928 if( status != PSA_SUCCESS )
929 {
930 (void) psa_remove_key_data_from_memory( slot );
931 slot->type = PSA_KEY_TYPE_NONE;
932 }
933 }
934#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
935
936 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937}
938
Gilles Peskinec5487a82018-12-03 18:08:14 +0100939psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100941 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100942 psa_status_t status = PSA_SUCCESS;
943 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946 if( status != PSA_SUCCESS )
947 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100948#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
949 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
950 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100951 storage_status =
952 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100953 }
954#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100955 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100956 if( status != PSA_SUCCESS )
957 return( status );
958 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959}
960
Gilles Peskineb870b182018-07-06 16:02:09 +0200961/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100962static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200963{
964 if( key_type_is_raw_bytes( slot->type ) )
965 return( slot->data.raw.bytes * 8 );
966#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200967 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100968 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200969#endif /* defined(MBEDTLS_RSA_C) */
970#if defined(MBEDTLS_ECP_C)
971 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
972 return( slot->data.ecp->grp.pbits );
973#endif /* defined(MBEDTLS_ECP_C) */
974 /* Shouldn't happen except on an empty slot. */
975 return( 0 );
976}
977
Gilles Peskinec5487a82018-12-03 18:08:14 +0100978psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200979 psa_key_type_t *type,
980 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100982 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200983 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200986 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 if( bits != NULL )
988 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100989 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200990 if( status != PSA_SUCCESS )
991 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200992
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200994 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200995 if( type != NULL )
996 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200997 if( bits != NULL )
998 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 return( PSA_SUCCESS );
1000}
1001
Jaeden Ameroccdce902019-01-10 11:42:27 +00001002#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001003static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1004 unsigned char *buf, size_t size )
1005{
1006 int ret;
1007 unsigned char *c;
1008 size_t len = 0;
1009
1010 c = buf + size;
1011
1012 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1013
1014 return( (int) len );
1015}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001016#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001017
Gilles Peskinef603c712019-01-19 13:40:11 +01001018static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1019 uint8_t *data,
1020 size_t data_size,
1021 size_t *data_length,
1022 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001024 *data_length = 0;
1025
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001026 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001027 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001028
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001029 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 {
1031 if( slot->data.raw.bytes > data_size )
1032 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001033 if( data_size != 0 )
1034 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001035 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001036 memset( data + slot->data.raw.bytes, 0,
1037 data_size - slot->data.raw.bytes );
1038 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001039 *data_length = slot->data.raw.bytes;
1040 return( PSA_SUCCESS );
1041 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001042#if defined(MBEDTLS_ECP_C)
1043 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1044 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001045 psa_status_t status;
1046
Gilles Peskine188c71e2018-10-29 19:26:02 +01001047 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1048 if( bytes > data_size )
1049 return( PSA_ERROR_BUFFER_TOO_SMALL );
1050 status = mbedtls_to_psa_error(
1051 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1052 if( status != PSA_SUCCESS )
1053 return( status );
1054 memset( data + bytes, 0, data_size - bytes );
1055 *data_length = bytes;
1056 return( PSA_SUCCESS );
1057 }
1058#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059 else
Moran Peker17e36e12018-05-02 12:55:20 +03001060 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001061#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001062 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001063 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001064 {
Moran Pekera998bc62018-04-16 18:16:20 +03001065 mbedtls_pk_context pk;
1066 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001067 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001068 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001069#if defined(MBEDTLS_RSA_C)
1070 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001071 pk.pk_info = &mbedtls_rsa_info;
1072 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001073#else
1074 return( PSA_ERROR_NOT_SUPPORTED );
1075#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001076 }
1077 else
1078 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001079#if defined(MBEDTLS_ECP_C)
1080 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001081 pk.pk_info = &mbedtls_eckey_info;
1082 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001083#else
1084 return( PSA_ERROR_NOT_SUPPORTED );
1085#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001086 }
Moran Pekerd7326592018-05-29 16:56:39 +03001087 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001088 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001089 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001090 }
Moran Peker17e36e12018-05-02 12:55:20 +03001091 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001092 {
Moran Peker17e36e12018-05-02 12:55:20 +03001093 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001094 }
Moran Peker60364322018-04-29 11:34:58 +03001095 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001096 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001097 /* If data_size is 0 then data may be NULL and then the
1098 * call to memset would have undefined behavior. */
1099 if( data_size != 0 )
1100 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001101 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001102 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001103 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1104 * Move the data to the beginning and erase remaining data
1105 * at the original location. */
1106 if( 2 * (size_t) ret <= data_size )
1107 {
1108 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001109 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001110 }
1111 else if( (size_t) ret < data_size )
1112 {
1113 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001114 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001115 }
Moran Pekera998bc62018-04-16 18:16:20 +03001116 *data_length = ret;
1117 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001118 }
1119 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001120#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001121 {
1122 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001123 it is valid for a special-purpose implementation to omit
1124 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001125 return( PSA_ERROR_NOT_SUPPORTED );
1126 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001127 }
1128}
1129
Gilles Peskinec5487a82018-12-03 18:08:14 +01001130psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001131 uint8_t *data,
1132 size_t data_size,
1133 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001134{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001135 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001136 psa_status_t status;
1137
1138 /* Set the key to empty now, so that even when there are errors, we always
1139 * set data_length to a value between 0 and data_size. On error, setting
1140 * the key to empty is a good choice because an empty key representation is
1141 * unlikely to be accepted anywhere. */
1142 *data_length = 0;
1143
1144 /* Export requires the EXPORT flag. There is an exception for public keys,
1145 * which don't require any flag, but psa_get_key_from_slot takes
1146 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001147 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001148 if( status != PSA_SUCCESS )
1149 return( status );
1150 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001151 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001153
Gilles Peskinec5487a82018-12-03 18:08:14 +01001154psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001155 uint8_t *data,
1156 size_t data_size,
1157 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001158{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001159 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001160 psa_status_t status;
1161
1162 /* Set the key to empty now, so that even when there are errors, we always
1163 * set data_length to a value between 0 and data_size. On error, setting
1164 * the key to empty is a good choice because an empty key representation is
1165 * unlikely to be accepted anywhere. */
1166 *data_length = 0;
1167
1168 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001169 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001170 if( status != PSA_SUCCESS )
1171 return( status );
1172 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001173 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001174}
1175
Darryl Green0c6575a2018-11-07 16:05:30 +00001176#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001177static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001178 size_t bits )
1179{
1180 psa_status_t status;
1181 uint8_t *data;
1182 size_t key_length;
1183 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1184 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001185 if( data == NULL )
1186 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001187 /* Get key data in export format */
1188 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1189 if( status != PSA_SUCCESS )
1190 {
1191 slot->type = PSA_KEY_TYPE_NONE;
1192 goto exit;
1193 }
1194 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001195 status = psa_save_persistent_key( slot->persistent_storage_id,
1196 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001197 data, key_length );
1198 if( status != PSA_SUCCESS )
1199 {
1200 slot->type = PSA_KEY_TYPE_NONE;
1201 }
1202exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001203 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001204 mbedtls_free( data );
1205 return( status );
1206}
1207#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1208
Gilles Peskinef603c712019-01-19 13:40:11 +01001209static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1210 psa_key_handle_t target )
1211{
1212 psa_status_t status;
1213 uint8_t *buffer = NULL;
1214 size_t buffer_size = 0;
1215 size_t length;
1216
1217 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1218 psa_get_key_bits( source ) );
1219 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001220 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001221 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001222 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1223 if( status != PSA_SUCCESS )
1224 goto exit;
1225 status = psa_import_key( target, source->type, buffer, length );
1226
1227exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001228 if( buffer_size != 0 )
1229 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001230 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001231 return( status );
1232}
1233
1234psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1235 psa_key_handle_t target_handle,
1236 const psa_key_policy_t *constraint)
1237{
1238 psa_key_slot_t *source_slot = NULL;
1239 psa_key_slot_t *target_slot = NULL;
1240 psa_key_policy_t new_policy;
1241 psa_status_t status;
1242 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1243 if( status != PSA_SUCCESS )
1244 return( status );
1245 status = psa_get_empty_key_slot( target_handle, &target_slot );
1246 if( status != PSA_SUCCESS )
1247 return( status );
1248
1249 new_policy = target_slot->policy;
1250 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1251 if( status != PSA_SUCCESS )
1252 return( status );
1253 if( constraint != NULL )
1254 {
1255 status = psa_restrict_key_policy( &new_policy, constraint );
1256 if( status != PSA_SUCCESS )
1257 return( status );
1258 }
1259
1260 status = psa_copy_key_material( source_slot, target_handle );
1261 if( status != PSA_SUCCESS )
1262 return( status );
1263
1264 target_slot->policy = new_policy;
1265 return( PSA_SUCCESS );
1266}
1267
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001268
1269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001271/* Message digests */
1272/****************************************************************/
1273
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001274static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001275{
1276 switch( alg )
1277 {
1278#if defined(MBEDTLS_MD2_C)
1279 case PSA_ALG_MD2:
1280 return( &mbedtls_md2_info );
1281#endif
1282#if defined(MBEDTLS_MD4_C)
1283 case PSA_ALG_MD4:
1284 return( &mbedtls_md4_info );
1285#endif
1286#if defined(MBEDTLS_MD5_C)
1287 case PSA_ALG_MD5:
1288 return( &mbedtls_md5_info );
1289#endif
1290#if defined(MBEDTLS_RIPEMD160_C)
1291 case PSA_ALG_RIPEMD160:
1292 return( &mbedtls_ripemd160_info );
1293#endif
1294#if defined(MBEDTLS_SHA1_C)
1295 case PSA_ALG_SHA_1:
1296 return( &mbedtls_sha1_info );
1297#endif
1298#if defined(MBEDTLS_SHA256_C)
1299 case PSA_ALG_SHA_224:
1300 return( &mbedtls_sha224_info );
1301 case PSA_ALG_SHA_256:
1302 return( &mbedtls_sha256_info );
1303#endif
1304#if defined(MBEDTLS_SHA512_C)
1305 case PSA_ALG_SHA_384:
1306 return( &mbedtls_sha384_info );
1307 case PSA_ALG_SHA_512:
1308 return( &mbedtls_sha512_info );
1309#endif
1310 default:
1311 return( NULL );
1312 }
1313}
1314
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001315psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1316{
1317 switch( operation->alg )
1318 {
Gilles Peskine81736312018-06-26 15:04:31 +02001319 case 0:
1320 /* The object has (apparently) been initialized but it is not
1321 * in use. It's ok to call abort on such an object, and there's
1322 * nothing to do. */
1323 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001324#if defined(MBEDTLS_MD2_C)
1325 case PSA_ALG_MD2:
1326 mbedtls_md2_free( &operation->ctx.md2 );
1327 break;
1328#endif
1329#if defined(MBEDTLS_MD4_C)
1330 case PSA_ALG_MD4:
1331 mbedtls_md4_free( &operation->ctx.md4 );
1332 break;
1333#endif
1334#if defined(MBEDTLS_MD5_C)
1335 case PSA_ALG_MD5:
1336 mbedtls_md5_free( &operation->ctx.md5 );
1337 break;
1338#endif
1339#if defined(MBEDTLS_RIPEMD160_C)
1340 case PSA_ALG_RIPEMD160:
1341 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1342 break;
1343#endif
1344#if defined(MBEDTLS_SHA1_C)
1345 case PSA_ALG_SHA_1:
1346 mbedtls_sha1_free( &operation->ctx.sha1 );
1347 break;
1348#endif
1349#if defined(MBEDTLS_SHA256_C)
1350 case PSA_ALG_SHA_224:
1351 case PSA_ALG_SHA_256:
1352 mbedtls_sha256_free( &operation->ctx.sha256 );
1353 break;
1354#endif
1355#if defined(MBEDTLS_SHA512_C)
1356 case PSA_ALG_SHA_384:
1357 case PSA_ALG_SHA_512:
1358 mbedtls_sha512_free( &operation->ctx.sha512 );
1359 break;
1360#endif
1361 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001362 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001363 }
1364 operation->alg = 0;
1365 return( PSA_SUCCESS );
1366}
1367
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001368psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001369 psa_algorithm_t alg )
1370{
1371 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001372
1373 /* A context must be freshly initialized before it can be set up. */
1374 if( operation->alg != 0 )
1375 {
1376 return( PSA_ERROR_BAD_STATE );
1377 }
1378
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001379 switch( alg )
1380 {
1381#if defined(MBEDTLS_MD2_C)
1382 case PSA_ALG_MD2:
1383 mbedtls_md2_init( &operation->ctx.md2 );
1384 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1385 break;
1386#endif
1387#if defined(MBEDTLS_MD4_C)
1388 case PSA_ALG_MD4:
1389 mbedtls_md4_init( &operation->ctx.md4 );
1390 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1391 break;
1392#endif
1393#if defined(MBEDTLS_MD5_C)
1394 case PSA_ALG_MD5:
1395 mbedtls_md5_init( &operation->ctx.md5 );
1396 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1397 break;
1398#endif
1399#if defined(MBEDTLS_RIPEMD160_C)
1400 case PSA_ALG_RIPEMD160:
1401 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1402 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1403 break;
1404#endif
1405#if defined(MBEDTLS_SHA1_C)
1406 case PSA_ALG_SHA_1:
1407 mbedtls_sha1_init( &operation->ctx.sha1 );
1408 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1409 break;
1410#endif
1411#if defined(MBEDTLS_SHA256_C)
1412 case PSA_ALG_SHA_224:
1413 mbedtls_sha256_init( &operation->ctx.sha256 );
1414 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1415 break;
1416 case PSA_ALG_SHA_256:
1417 mbedtls_sha256_init( &operation->ctx.sha256 );
1418 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1419 break;
1420#endif
1421#if defined(MBEDTLS_SHA512_C)
1422 case PSA_ALG_SHA_384:
1423 mbedtls_sha512_init( &operation->ctx.sha512 );
1424 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1425 break;
1426 case PSA_ALG_SHA_512:
1427 mbedtls_sha512_init( &operation->ctx.sha512 );
1428 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1429 break;
1430#endif
1431 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001432 return( PSA_ALG_IS_HASH( alg ) ?
1433 PSA_ERROR_NOT_SUPPORTED :
1434 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001435 }
1436 if( ret == 0 )
1437 operation->alg = alg;
1438 else
1439 psa_hash_abort( operation );
1440 return( mbedtls_to_psa_error( ret ) );
1441}
1442
1443psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1444 const uint8_t *input,
1445 size_t input_length )
1446{
1447 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001448
1449 /* Don't require hash implementations to behave correctly on a
1450 * zero-length input, which may have an invalid pointer. */
1451 if( input_length == 0 )
1452 return( PSA_SUCCESS );
1453
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001454 switch( operation->alg )
1455 {
1456#if defined(MBEDTLS_MD2_C)
1457 case PSA_ALG_MD2:
1458 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1459 input, input_length );
1460 break;
1461#endif
1462#if defined(MBEDTLS_MD4_C)
1463 case PSA_ALG_MD4:
1464 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1465 input, input_length );
1466 break;
1467#endif
1468#if defined(MBEDTLS_MD5_C)
1469 case PSA_ALG_MD5:
1470 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1471 input, input_length );
1472 break;
1473#endif
1474#if defined(MBEDTLS_RIPEMD160_C)
1475 case PSA_ALG_RIPEMD160:
1476 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1477 input, input_length );
1478 break;
1479#endif
1480#if defined(MBEDTLS_SHA1_C)
1481 case PSA_ALG_SHA_1:
1482 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1483 input, input_length );
1484 break;
1485#endif
1486#if defined(MBEDTLS_SHA256_C)
1487 case PSA_ALG_SHA_224:
1488 case PSA_ALG_SHA_256:
1489 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1490 input, input_length );
1491 break;
1492#endif
1493#if defined(MBEDTLS_SHA512_C)
1494 case PSA_ALG_SHA_384:
1495 case PSA_ALG_SHA_512:
1496 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1497 input, input_length );
1498 break;
1499#endif
1500 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001501 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001502 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001503
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001504 if( ret != 0 )
1505 psa_hash_abort( operation );
1506 return( mbedtls_to_psa_error( ret ) );
1507}
1508
1509psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1510 uint8_t *hash,
1511 size_t hash_size,
1512 size_t *hash_length )
1513{
itayzafrir40835d42018-08-02 13:14:17 +03001514 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001515 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001516 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001517
1518 /* Fill the output buffer with something that isn't a valid hash
1519 * (barring an attack on the hash and deliberately-crafted input),
1520 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001521 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001522 /* If hash_size is 0 then hash may be NULL and then the
1523 * call to memset would have undefined behavior. */
1524 if( hash_size != 0 )
1525 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001526
1527 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001528 {
1529 status = PSA_ERROR_BUFFER_TOO_SMALL;
1530 goto exit;
1531 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001532
1533 switch( operation->alg )
1534 {
1535#if defined(MBEDTLS_MD2_C)
1536 case PSA_ALG_MD2:
1537 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1538 break;
1539#endif
1540#if defined(MBEDTLS_MD4_C)
1541 case PSA_ALG_MD4:
1542 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1543 break;
1544#endif
1545#if defined(MBEDTLS_MD5_C)
1546 case PSA_ALG_MD5:
1547 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1548 break;
1549#endif
1550#if defined(MBEDTLS_RIPEMD160_C)
1551 case PSA_ALG_RIPEMD160:
1552 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1553 break;
1554#endif
1555#if defined(MBEDTLS_SHA1_C)
1556 case PSA_ALG_SHA_1:
1557 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1558 break;
1559#endif
1560#if defined(MBEDTLS_SHA256_C)
1561 case PSA_ALG_SHA_224:
1562 case PSA_ALG_SHA_256:
1563 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1564 break;
1565#endif
1566#if defined(MBEDTLS_SHA512_C)
1567 case PSA_ALG_SHA_384:
1568 case PSA_ALG_SHA_512:
1569 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1570 break;
1571#endif
1572 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001573 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001574 }
itayzafrir40835d42018-08-02 13:14:17 +03001575 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576
itayzafrir40835d42018-08-02 13:14:17 +03001577exit:
1578 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001580 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001581 return( psa_hash_abort( operation ) );
1582 }
1583 else
1584 {
1585 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001586 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587 }
1588}
1589
Gilles Peskine2d277862018-06-18 15:41:12 +02001590psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1591 const uint8_t *hash,
1592 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593{
1594 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1595 size_t actual_hash_length;
1596 psa_status_t status = psa_hash_finish( operation,
1597 actual_hash, sizeof( actual_hash ),
1598 &actual_hash_length );
1599 if( status != PSA_SUCCESS )
1600 return( status );
1601 if( actual_hash_length != hash_length )
1602 return( PSA_ERROR_INVALID_SIGNATURE );
1603 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1604 return( PSA_ERROR_INVALID_SIGNATURE );
1605 return( PSA_SUCCESS );
1606}
1607
Gilles Peskineeb35d782019-01-22 17:56:16 +01001608psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1609 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001610{
1611 if( target_operation->alg != 0 )
1612 return( PSA_ERROR_BAD_STATE );
1613
1614 switch( source_operation->alg )
1615 {
1616 case 0:
1617 return( PSA_ERROR_BAD_STATE );
1618#if defined(MBEDTLS_MD2_C)
1619 case PSA_ALG_MD2:
1620 mbedtls_md2_clone( &target_operation->ctx.md2,
1621 &source_operation->ctx.md2 );
1622 break;
1623#endif
1624#if defined(MBEDTLS_MD4_C)
1625 case PSA_ALG_MD4:
1626 mbedtls_md4_clone( &target_operation->ctx.md4,
1627 &source_operation->ctx.md4 );
1628 break;
1629#endif
1630#if defined(MBEDTLS_MD5_C)
1631 case PSA_ALG_MD5:
1632 mbedtls_md5_clone( &target_operation->ctx.md5,
1633 &source_operation->ctx.md5 );
1634 break;
1635#endif
1636#if defined(MBEDTLS_RIPEMD160_C)
1637 case PSA_ALG_RIPEMD160:
1638 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1639 &source_operation->ctx.ripemd160 );
1640 break;
1641#endif
1642#if defined(MBEDTLS_SHA1_C)
1643 case PSA_ALG_SHA_1:
1644 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1645 &source_operation->ctx.sha1 );
1646 break;
1647#endif
1648#if defined(MBEDTLS_SHA256_C)
1649 case PSA_ALG_SHA_224:
1650 case PSA_ALG_SHA_256:
1651 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1652 &source_operation->ctx.sha256 );
1653 break;
1654#endif
1655#if defined(MBEDTLS_SHA512_C)
1656 case PSA_ALG_SHA_384:
1657 case PSA_ALG_SHA_512:
1658 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1659 &source_operation->ctx.sha512 );
1660 break;
1661#endif
1662 default:
1663 return( PSA_ERROR_NOT_SUPPORTED );
1664 }
1665
1666 target_operation->alg = source_operation->alg;
1667 return( PSA_SUCCESS );
1668}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001669
1670
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001671/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672/* MAC */
1673/****************************************************************/
1674
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001675static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676 psa_algorithm_t alg,
1677 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001678 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001679 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001682 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001684 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001685 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001686
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1688 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001689 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001691 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 mode = MBEDTLS_MODE_STREAM;
1693 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 case PSA_ALG_CTR:
1695 mode = MBEDTLS_MODE_CTR;
1696 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001697 case PSA_ALG_CFB:
1698 mode = MBEDTLS_MODE_CFB;
1699 break;
1700 case PSA_ALG_OFB:
1701 mode = MBEDTLS_MODE_OFB;
1702 break;
1703 case PSA_ALG_CBC_NO_PADDING:
1704 mode = MBEDTLS_MODE_CBC;
1705 break;
1706 case PSA_ALG_CBC_PKCS7:
1707 mode = MBEDTLS_MODE_CBC;
1708 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001709 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001710 mode = MBEDTLS_MODE_CCM;
1711 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001712 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001713 mode = MBEDTLS_MODE_GCM;
1714 break;
1715 default:
1716 return( NULL );
1717 }
1718 }
1719 else if( alg == PSA_ALG_CMAC )
1720 mode = MBEDTLS_MODE_ECB;
1721 else if( alg == PSA_ALG_GMAC )
1722 mode = MBEDTLS_MODE_GCM;
1723 else
1724 return( NULL );
1725
1726 switch( key_type )
1727 {
1728 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001729 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730 break;
1731 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001732 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1733 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001734 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001735 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001736 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001737 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001738 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1739 * but two-key Triple-DES is functionally three-key Triple-DES
1740 * with K1=K3, so that's how we present it to mbedtls. */
1741 if( key_bits == 128 )
1742 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001743 break;
1744 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001745 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746 break;
1747 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001748 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001749 break;
1750 default:
1751 return( NULL );
1752 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001753 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001754 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755
Jaeden Amero23bbb752018-06-26 14:16:54 +01001756 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1757 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758}
1759
Gilles Peskinea05219c2018-11-16 16:02:56 +01001760#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001761static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001762{
Gilles Peskine2d277862018-06-18 15:41:12 +02001763 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001764 {
1765 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001766 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001767 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001768 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001769 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001770 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001771 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001774 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001775 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001776 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001779 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001782 return( 128 );
1783 default:
1784 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 }
1786}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001787#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001788
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001789/* Initialize the MAC operation structure. Once this function has been
1790 * called, psa_mac_abort can run and will do the right thing. */
1791static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1792 psa_algorithm_t alg )
1793{
1794 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1795
1796 operation->alg = alg;
1797 operation->key_set = 0;
1798 operation->iv_set = 0;
1799 operation->iv_required = 0;
1800 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001801 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802
1803#if defined(MBEDTLS_CMAC_C)
1804 if( alg == PSA_ALG_CMAC )
1805 {
1806 operation->iv_required = 0;
1807 mbedtls_cipher_init( &operation->ctx.cmac );
1808 status = PSA_SUCCESS;
1809 }
1810 else
1811#endif /* MBEDTLS_CMAC_C */
1812#if defined(MBEDTLS_MD_C)
1813 if( PSA_ALG_IS_HMAC( operation->alg ) )
1814 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001815 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1816 operation->ctx.hmac.hash_ctx.alg = 0;
1817 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818 }
1819 else
1820#endif /* MBEDTLS_MD_C */
1821 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001822 if( ! PSA_ALG_IS_MAC( alg ) )
1823 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001824 }
1825
1826 if( status != PSA_SUCCESS )
1827 memset( operation, 0, sizeof( *operation ) );
1828 return( status );
1829}
1830
Gilles Peskine01126fa2018-07-12 17:04:55 +02001831#if defined(MBEDTLS_MD_C)
1832static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1833{
Gilles Peskine3f108122018-12-07 18:14:53 +01001834 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001835 return( psa_hash_abort( &hmac->hash_ctx ) );
1836}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001837
1838static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1839{
1840 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1841 memset( hmac, 0, sizeof( *hmac ) );
1842}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001843#endif /* MBEDTLS_MD_C */
1844
Gilles Peskine8c9def32018-02-08 10:02:12 +01001845psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1846{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001847 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001848 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001849 /* The object has (apparently) been initialized but it is not
1850 * in use. It's ok to call abort on such an object, and there's
1851 * nothing to do. */
1852 return( PSA_SUCCESS );
1853 }
1854 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001855#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001856 if( operation->alg == PSA_ALG_CMAC )
1857 {
1858 mbedtls_cipher_free( &operation->ctx.cmac );
1859 }
1860 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001862#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001863 if( PSA_ALG_IS_HMAC( operation->alg ) )
1864 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001865 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001866 }
1867 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001868#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 {
1870 /* Sanity check (shouldn't happen: operation->alg should
1871 * always have been initialized to a valid value). */
1872 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873 }
Moran Peker41deec42018-04-04 15:43:05 +03001874
Gilles Peskine8c9def32018-02-08 10:02:12 +01001875 operation->alg = 0;
1876 operation->key_set = 0;
1877 operation->iv_set = 0;
1878 operation->iv_required = 0;
1879 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001880 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001881
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001883
1884bad_state:
1885 /* If abort is called on an uninitialized object, we can't trust
1886 * anything. Wipe the object in case it contains confidential data.
1887 * This may result in a memory leak if a pointer gets overwritten,
1888 * but it's too late to do anything about this. */
1889 memset( operation, 0, sizeof( *operation ) );
1890 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001891}
1892
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001893#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001894static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001895 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001896 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001897 const mbedtls_cipher_info_t *cipher_info )
1898{
1899 int ret;
1900
1901 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001902
1903 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1904 if( ret != 0 )
1905 return( ret );
1906
1907 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1908 slot->data.raw.data,
1909 key_bits );
1910 return( ret );
1911}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001912#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913
Gilles Peskine248051a2018-06-20 16:09:38 +02001914#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001915static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1916 const uint8_t *key,
1917 size_t key_length,
1918 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001919{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001920 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001921 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001922 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001923 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001924 psa_status_t status;
1925
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001926 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1927 * overflow below. This should never trigger if the hash algorithm
1928 * is implemented correctly. */
1929 /* The size checks against the ipad and opad buffers cannot be written
1930 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1931 * because that triggers -Wlogical-op on GCC 7.3. */
1932 if( block_size > sizeof( ipad ) )
1933 return( PSA_ERROR_NOT_SUPPORTED );
1934 if( block_size > sizeof( hmac->opad ) )
1935 return( PSA_ERROR_NOT_SUPPORTED );
1936 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001937 return( PSA_ERROR_NOT_SUPPORTED );
1938
Gilles Peskined223b522018-06-11 18:12:58 +02001939 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001940 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001941 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001942 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001943 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001944 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001945 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001946 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001947 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001948 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001949 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001950 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001951 }
Gilles Peskine96889972018-07-12 17:07:03 +02001952 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1953 * but it is permitted. It is common when HMAC is used in HKDF, for
1954 * example. Don't call `memcpy` in the 0-length because `key` could be
1955 * an invalid pointer which would make the behavior undefined. */
1956 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001957 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001958
Gilles Peskined223b522018-06-11 18:12:58 +02001959 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1960 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001961 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001962 ipad[i] ^= 0x36;
1963 memset( ipad + key_length, 0x36, block_size - key_length );
1964
1965 /* Copy the key material from ipad to opad, flipping the requisite bits,
1966 * and filling the rest of opad with the requisite constant. */
1967 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001968 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1969 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001970
Gilles Peskine01126fa2018-07-12 17:04:55 +02001971 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001972 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001973 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001974
Gilles Peskine01126fa2018-07-12 17:04:55 +02001975 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001976
1977cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001978 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001979
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001980 return( status );
1981}
Gilles Peskine248051a2018-06-20 16:09:38 +02001982#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001983
Gilles Peskine89167cb2018-07-08 20:12:23 +02001984static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001985 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001986 psa_algorithm_t alg,
1987 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001989 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001990 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001991 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001992 psa_key_usage_t usage =
1993 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001994 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001995 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001996
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001997 /* A context must be freshly initialized before it can be set up. */
1998 if( operation->alg != 0 )
1999 {
2000 return( PSA_ERROR_BAD_STATE );
2001 }
2002
Gilles Peskined911eb72018-08-14 15:18:45 +02002003 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002004 if( status != PSA_SUCCESS )
2005 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002006 if( is_sign )
2007 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002008
Gilles Peskinec5487a82018-12-03 18:08:14 +01002009 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002010 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002011 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002012 key_bits = psa_get_key_bits( slot );
2013
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002015 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002016 {
2017 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002018 mbedtls_cipher_info_from_psa( full_length_alg,
2019 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020 int ret;
2021 if( cipher_info == NULL )
2022 {
2023 status = PSA_ERROR_NOT_SUPPORTED;
2024 goto exit;
2025 }
2026 operation->mac_size = cipher_info->block_size;
2027 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2028 status = mbedtls_to_psa_error( ret );
2029 }
2030 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002033 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002035 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002036 if( hash_alg == 0 )
2037 {
2038 status = PSA_ERROR_NOT_SUPPORTED;
2039 goto exit;
2040 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002041
2042 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2043 /* Sanity check. This shouldn't fail on a valid configuration. */
2044 if( operation->mac_size == 0 ||
2045 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2046 {
2047 status = PSA_ERROR_NOT_SUPPORTED;
2048 goto exit;
2049 }
2050
Gilles Peskine01126fa2018-07-12 17:04:55 +02002051 if( slot->type != PSA_KEY_TYPE_HMAC )
2052 {
2053 status = PSA_ERROR_INVALID_ARGUMENT;
2054 goto exit;
2055 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002056
Gilles Peskine01126fa2018-07-12 17:04:55 +02002057 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2058 slot->data.raw.data,
2059 slot->data.raw.bytes,
2060 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002061 }
2062 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002064 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002065 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002066 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002068
Gilles Peskined911eb72018-08-14 15:18:45 +02002069 if( truncated == 0 )
2070 {
2071 /* The "normal" case: untruncated algorithm. Nothing to do. */
2072 }
2073 else if( truncated < 4 )
2074 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002075 /* A very short MAC is too short for security since it can be
2076 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2077 * so we make this our minimum, even though 32 bits is still
2078 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002079 status = PSA_ERROR_NOT_SUPPORTED;
2080 }
2081 else if( truncated > operation->mac_size )
2082 {
2083 /* It's impossible to "truncate" to a larger length. */
2084 status = PSA_ERROR_INVALID_ARGUMENT;
2085 }
2086 else
2087 operation->mac_size = truncated;
2088
Gilles Peskinefbfac682018-07-08 20:51:54 +02002089exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002090 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002092 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002093 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002094 else
2095 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002096 operation->key_set = 1;
2097 }
2098 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099}
2100
Gilles Peskine89167cb2018-07-08 20:12:23 +02002101psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002102 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002103 psa_algorithm_t alg )
2104{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002105 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002106}
2107
2108psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002109 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002110 psa_algorithm_t alg )
2111{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002112 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002113}
2114
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2116 const uint8_t *input,
2117 size_t input_length )
2118{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002119 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002121 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002123 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002124 operation->has_input = 1;
2125
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002127 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002128 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002129 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2130 input, input_length );
2131 status = mbedtls_to_psa_error( ret );
2132 }
2133 else
2134#endif /* MBEDTLS_CMAC_C */
2135#if defined(MBEDTLS_MD_C)
2136 if( PSA_ALG_IS_HMAC( operation->alg ) )
2137 {
2138 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2139 input_length );
2140 }
2141 else
2142#endif /* MBEDTLS_MD_C */
2143 {
2144 /* This shouldn't happen if `operation` was initialized by
2145 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002146 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002147 }
2148
Gilles Peskinefbfac682018-07-08 20:51:54 +02002149 if( status != PSA_SUCCESS )
2150 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002151 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002152}
2153
Gilles Peskine01126fa2018-07-12 17:04:55 +02002154#if defined(MBEDTLS_MD_C)
2155static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2156 uint8_t *mac,
2157 size_t mac_size )
2158{
2159 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2160 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2161 size_t hash_size = 0;
2162 size_t block_size = psa_get_hash_block_size( hash_alg );
2163 psa_status_t status;
2164
Gilles Peskine01126fa2018-07-12 17:04:55 +02002165 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2166 if( status != PSA_SUCCESS )
2167 return( status );
2168 /* From here on, tmp needs to be wiped. */
2169
2170 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2171 if( status != PSA_SUCCESS )
2172 goto exit;
2173
2174 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2175 if( status != PSA_SUCCESS )
2176 goto exit;
2177
2178 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2179 if( status != PSA_SUCCESS )
2180 goto exit;
2181
Gilles Peskined911eb72018-08-14 15:18:45 +02002182 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2183 if( status != PSA_SUCCESS )
2184 goto exit;
2185
2186 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002187
2188exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002189 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002190 return( status );
2191}
2192#endif /* MBEDTLS_MD_C */
2193
mohammad16036df908f2018-04-02 08:34:15 -07002194static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002195 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002196 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002198 if( ! operation->key_set )
2199 return( PSA_ERROR_BAD_STATE );
2200 if( operation->iv_required && ! operation->iv_set )
2201 return( PSA_ERROR_BAD_STATE );
2202
Gilles Peskine8c9def32018-02-08 10:02:12 +01002203 if( mac_size < operation->mac_size )
2204 return( PSA_ERROR_BUFFER_TOO_SMALL );
2205
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002207 if( operation->alg == PSA_ALG_CMAC )
2208 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002209 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2210 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2211 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002212 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002213 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002214 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002216 else
2217#endif /* MBEDTLS_CMAC_C */
2218#if defined(MBEDTLS_MD_C)
2219 if( PSA_ALG_IS_HMAC( operation->alg ) )
2220 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002221 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002222 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002223 }
2224 else
2225#endif /* MBEDTLS_MD_C */
2226 {
2227 /* This shouldn't happen if `operation` was initialized by
2228 * a setup function. */
2229 return( PSA_ERROR_BAD_STATE );
2230 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002231}
2232
Gilles Peskineacd4be32018-07-08 19:56:25 +02002233psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2234 uint8_t *mac,
2235 size_t mac_size,
2236 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002237{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002238 psa_status_t status;
2239
Jaeden Amero252ef282019-02-15 14:05:35 +00002240 if( operation->alg == 0 )
2241 {
2242 return( PSA_ERROR_BAD_STATE );
2243 }
2244
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002245 /* Fill the output buffer with something that isn't a valid mac
2246 * (barring an attack on the mac and deliberately-crafted input),
2247 * in case the caller doesn't check the return status properly. */
2248 *mac_length = mac_size;
2249 /* If mac_size is 0 then mac may be NULL and then the
2250 * call to memset would have undefined behavior. */
2251 if( mac_size != 0 )
2252 memset( mac, '!', mac_size );
2253
Gilles Peskine89167cb2018-07-08 20:12:23 +02002254 if( ! operation->is_sign )
2255 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002256 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002257 }
mohammad16036df908f2018-04-02 08:34:15 -07002258
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002259 status = psa_mac_finish_internal( operation, mac, mac_size );
2260
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002261 if( status == PSA_SUCCESS )
2262 {
2263 status = psa_mac_abort( operation );
2264 if( status == PSA_SUCCESS )
2265 *mac_length = operation->mac_size;
2266 else
2267 memset( mac, '!', mac_size );
2268 }
2269 else
2270 psa_mac_abort( operation );
2271 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002272}
2273
Gilles Peskineacd4be32018-07-08 19:56:25 +02002274psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2275 const uint8_t *mac,
2276 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277{
Gilles Peskine828ed142018-06-18 23:25:51 +02002278 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002279 psa_status_t status;
2280
Jaeden Amero252ef282019-02-15 14:05:35 +00002281 if( operation->alg == 0 )
2282 {
2283 return( PSA_ERROR_BAD_STATE );
2284 }
2285
Gilles Peskine89167cb2018-07-08 20:12:23 +02002286 if( operation->is_sign )
2287 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002288 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002289 }
2290 if( operation->mac_size != mac_length )
2291 {
2292 status = PSA_ERROR_INVALID_SIGNATURE;
2293 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002294 }
mohammad16036df908f2018-04-02 08:34:15 -07002295
2296 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002297 actual_mac, sizeof( actual_mac ) );
2298
2299 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2300 status = PSA_ERROR_INVALID_SIGNATURE;
2301
2302cleanup:
2303 if( status == PSA_SUCCESS )
2304 status = psa_mac_abort( operation );
2305 else
2306 psa_mac_abort( operation );
2307
Gilles Peskine3f108122018-12-07 18:14:53 +01002308 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002309
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002310 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311}
2312
2313
Gilles Peskine20035e32018-02-03 22:44:14 +01002314
Gilles Peskine20035e32018-02-03 22:44:14 +01002315/****************************************************************/
2316/* Asymmetric cryptography */
2317/****************************************************************/
2318
Gilles Peskine2b450e32018-06-27 15:42:46 +02002319#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002320/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002321 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002322static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2323 size_t hash_length,
2324 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002325{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002326 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002327 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002328 *md_alg = mbedtls_md_get_type( md_info );
2329
2330 /* The Mbed TLS RSA module uses an unsigned int for hash length
2331 * parameters. Validate that it fits so that we don't risk an
2332 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002333#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002334 if( hash_length > UINT_MAX )
2335 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002336#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002337
2338#if defined(MBEDTLS_PKCS1_V15)
2339 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2340 * must be correct. */
2341 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2342 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002343 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002344 if( md_info == NULL )
2345 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002346 if( mbedtls_md_get_size( md_info ) != hash_length )
2347 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348 }
2349#endif /* MBEDTLS_PKCS1_V15 */
2350
2351#if defined(MBEDTLS_PKCS1_V21)
2352 /* PSS requires a hash internally. */
2353 if( PSA_ALG_IS_RSA_PSS( alg ) )
2354 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002355 if( md_info == NULL )
2356 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002357 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002358#endif /* MBEDTLS_PKCS1_V21 */
2359
Gilles Peskine61b91d42018-06-08 16:09:36 +02002360 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002361}
2362
Gilles Peskine2b450e32018-06-27 15:42:46 +02002363static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2364 psa_algorithm_t alg,
2365 const uint8_t *hash,
2366 size_t hash_length,
2367 uint8_t *signature,
2368 size_t signature_size,
2369 size_t *signature_length )
2370{
2371 psa_status_t status;
2372 int ret;
2373 mbedtls_md_type_t md_alg;
2374
2375 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2376 if( status != PSA_SUCCESS )
2377 return( status );
2378
Gilles Peskine630a18a2018-06-29 17:49:35 +02002379 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002380 return( PSA_ERROR_BUFFER_TOO_SMALL );
2381
2382#if defined(MBEDTLS_PKCS1_V15)
2383 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2384 {
2385 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2386 MBEDTLS_MD_NONE );
2387 ret = mbedtls_rsa_pkcs1_sign( rsa,
2388 mbedtls_ctr_drbg_random,
2389 &global_data.ctr_drbg,
2390 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002391 md_alg,
2392 (unsigned int) hash_length,
2393 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002394 signature );
2395 }
2396 else
2397#endif /* MBEDTLS_PKCS1_V15 */
2398#if defined(MBEDTLS_PKCS1_V21)
2399 if( PSA_ALG_IS_RSA_PSS( alg ) )
2400 {
2401 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2402 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2403 mbedtls_ctr_drbg_random,
2404 &global_data.ctr_drbg,
2405 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002406 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002407 (unsigned int) hash_length,
2408 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002409 signature );
2410 }
2411 else
2412#endif /* MBEDTLS_PKCS1_V21 */
2413 {
2414 return( PSA_ERROR_INVALID_ARGUMENT );
2415 }
2416
2417 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002418 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002419 return( mbedtls_to_psa_error( ret ) );
2420}
2421
2422static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2423 psa_algorithm_t alg,
2424 const uint8_t *hash,
2425 size_t hash_length,
2426 const uint8_t *signature,
2427 size_t signature_length )
2428{
2429 psa_status_t status;
2430 int ret;
2431 mbedtls_md_type_t md_alg;
2432
2433 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2434 if( status != PSA_SUCCESS )
2435 return( status );
2436
Gilles Peskine630a18a2018-06-29 17:49:35 +02002437 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002438 return( PSA_ERROR_BUFFER_TOO_SMALL );
2439
2440#if defined(MBEDTLS_PKCS1_V15)
2441 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2442 {
2443 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2444 MBEDTLS_MD_NONE );
2445 ret = mbedtls_rsa_pkcs1_verify( rsa,
2446 mbedtls_ctr_drbg_random,
2447 &global_data.ctr_drbg,
2448 MBEDTLS_RSA_PUBLIC,
2449 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002450 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002451 hash,
2452 signature );
2453 }
2454 else
2455#endif /* MBEDTLS_PKCS1_V15 */
2456#if defined(MBEDTLS_PKCS1_V21)
2457 if( PSA_ALG_IS_RSA_PSS( alg ) )
2458 {
2459 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2460 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2461 mbedtls_ctr_drbg_random,
2462 &global_data.ctr_drbg,
2463 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002464 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002465 (unsigned int) hash_length,
2466 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002467 signature );
2468 }
2469 else
2470#endif /* MBEDTLS_PKCS1_V21 */
2471 {
2472 return( PSA_ERROR_INVALID_ARGUMENT );
2473 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002474
2475 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2476 * the rest of the signature is invalid". This has little use in
2477 * practice and PSA doesn't report this distinction. */
2478 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2479 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002480 return( mbedtls_to_psa_error( ret ) );
2481}
2482#endif /* MBEDTLS_RSA_C */
2483
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002484#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002485/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2486 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2487 * (even though these functions don't modify it). */
2488static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2489 psa_algorithm_t alg,
2490 const uint8_t *hash,
2491 size_t hash_length,
2492 uint8_t *signature,
2493 size_t signature_size,
2494 size_t *signature_length )
2495{
2496 int ret;
2497 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002498 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002499 mbedtls_mpi_init( &r );
2500 mbedtls_mpi_init( &s );
2501
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002502 if( signature_size < 2 * curve_bytes )
2503 {
2504 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2505 goto cleanup;
2506 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002507
Gilles Peskinea05219c2018-11-16 16:02:56 +01002508#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002509 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2510 {
2511 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2512 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2513 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2514 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2515 hash, hash_length,
2516 md_alg ) );
2517 }
2518 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002519#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002520 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002521 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002522 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2523 hash, hash_length,
2524 mbedtls_ctr_drbg_random,
2525 &global_data.ctr_drbg ) );
2526 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002527
2528 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2529 signature,
2530 curve_bytes ) );
2531 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2532 signature + curve_bytes,
2533 curve_bytes ) );
2534
2535cleanup:
2536 mbedtls_mpi_free( &r );
2537 mbedtls_mpi_free( &s );
2538 if( ret == 0 )
2539 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002540 return( mbedtls_to_psa_error( ret ) );
2541}
2542
2543static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2544 const uint8_t *hash,
2545 size_t hash_length,
2546 const uint8_t *signature,
2547 size_t signature_length )
2548{
2549 int ret;
2550 mbedtls_mpi r, s;
2551 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2552 mbedtls_mpi_init( &r );
2553 mbedtls_mpi_init( &s );
2554
2555 if( signature_length != 2 * curve_bytes )
2556 return( PSA_ERROR_INVALID_SIGNATURE );
2557
2558 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2559 signature,
2560 curve_bytes ) );
2561 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2562 signature + curve_bytes,
2563 curve_bytes ) );
2564
2565 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2566 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002567
2568cleanup:
2569 mbedtls_mpi_free( &r );
2570 mbedtls_mpi_free( &s );
2571 return( mbedtls_to_psa_error( ret ) );
2572}
2573#endif /* MBEDTLS_ECDSA_C */
2574
Gilles Peskinec5487a82018-12-03 18:08:14 +01002575psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002576 psa_algorithm_t alg,
2577 const uint8_t *hash,
2578 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002579 uint8_t *signature,
2580 size_t signature_size,
2581 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002582{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002583 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002584 psa_status_t status;
2585
2586 *signature_length = signature_size;
2587
Gilles Peskinec5487a82018-12-03 18:08:14 +01002588 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002589 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002590 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002591 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002592 {
2593 status = PSA_ERROR_INVALID_ARGUMENT;
2594 goto exit;
2595 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002596
Gilles Peskine20035e32018-02-03 22:44:14 +01002597#if defined(MBEDTLS_RSA_C)
2598 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2599 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002600 status = psa_rsa_sign( slot->data.rsa,
2601 alg,
2602 hash, hash_length,
2603 signature, signature_size,
2604 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002605 }
2606 else
2607#endif /* defined(MBEDTLS_RSA_C) */
2608#if defined(MBEDTLS_ECP_C)
2609 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2610 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002611#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002612 if(
2613#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2614 PSA_ALG_IS_ECDSA( alg )
2615#else
2616 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2617#endif
2618 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002619 status = psa_ecdsa_sign( slot->data.ecp,
2620 alg,
2621 hash, hash_length,
2622 signature, signature_size,
2623 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002624 else
2625#endif /* defined(MBEDTLS_ECDSA_C) */
2626 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002627 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002628 }
itayzafrir5c753392018-05-08 11:18:38 +03002629 }
2630 else
2631#endif /* defined(MBEDTLS_ECP_C) */
2632 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002633 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002634 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002635
2636exit:
2637 /* Fill the unused part of the output buffer (the whole buffer on error,
2638 * the trailing part on success) with something that isn't a valid mac
2639 * (barring an attack on the mac and deliberately-crafted input),
2640 * in case the caller doesn't check the return status properly. */
2641 if( status == PSA_SUCCESS )
2642 memset( signature + *signature_length, '!',
2643 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002644 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002645 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002646 /* If signature_size is 0 then we have nothing to do. We must not call
2647 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002648 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002649}
2650
Gilles Peskinec5487a82018-12-03 18:08:14 +01002651psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002652 psa_algorithm_t alg,
2653 const uint8_t *hash,
2654 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002655 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002656 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002657{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002658 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002659 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002660
Gilles Peskinec5487a82018-12-03 18:08:14 +01002661 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002662 if( status != PSA_SUCCESS )
2663 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002664
Gilles Peskine61b91d42018-06-08 16:09:36 +02002665#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002666 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002667 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002668 return( psa_rsa_verify( slot->data.rsa,
2669 alg,
2670 hash, hash_length,
2671 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002672 }
2673 else
2674#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002675#if defined(MBEDTLS_ECP_C)
2676 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2677 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002678#if defined(MBEDTLS_ECDSA_C)
2679 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002680 return( psa_ecdsa_verify( slot->data.ecp,
2681 hash, hash_length,
2682 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002683 else
2684#endif /* defined(MBEDTLS_ECDSA_C) */
2685 {
2686 return( PSA_ERROR_INVALID_ARGUMENT );
2687 }
itayzafrir5c753392018-05-08 11:18:38 +03002688 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002689 else
2690#endif /* defined(MBEDTLS_ECP_C) */
2691 {
2692 return( PSA_ERROR_NOT_SUPPORTED );
2693 }
2694}
2695
Gilles Peskine072ac562018-06-30 00:21:29 +02002696#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2697static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2698 mbedtls_rsa_context *rsa )
2699{
2700 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2701 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2702 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2703 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2704}
2705#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2706
Gilles Peskinec5487a82018-12-03 18:08:14 +01002707psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002708 psa_algorithm_t alg,
2709 const uint8_t *input,
2710 size_t input_length,
2711 const uint8_t *salt,
2712 size_t salt_length,
2713 uint8_t *output,
2714 size_t output_size,
2715 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002716{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002717 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002718 psa_status_t status;
2719
Darryl Green5cc689a2018-07-24 15:34:10 +01002720 (void) input;
2721 (void) input_length;
2722 (void) salt;
2723 (void) output;
2724 (void) output_size;
2725
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002726 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002728 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2729 return( PSA_ERROR_INVALID_ARGUMENT );
2730
Gilles Peskinec5487a82018-12-03 18:08:14 +01002731 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002732 if( status != PSA_SUCCESS )
2733 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002734 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2735 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002736 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002737
2738#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002739 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740 {
2741 mbedtls_rsa_context *rsa = slot->data.rsa;
2742 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002743 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002744 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002745#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002746 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002747 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002748 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2749 mbedtls_ctr_drbg_random,
2750 &global_data.ctr_drbg,
2751 MBEDTLS_RSA_PUBLIC,
2752 input_length,
2753 input,
2754 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002755 }
2756 else
2757#endif /* MBEDTLS_PKCS1_V15 */
2758#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002759 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002760 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002761 psa_rsa_oaep_set_padding_mode( alg, rsa );
2762 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2763 mbedtls_ctr_drbg_random,
2764 &global_data.ctr_drbg,
2765 MBEDTLS_RSA_PUBLIC,
2766 salt, salt_length,
2767 input_length,
2768 input,
2769 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002770 }
2771 else
2772#endif /* MBEDTLS_PKCS1_V21 */
2773 {
2774 return( PSA_ERROR_INVALID_ARGUMENT );
2775 }
2776 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002777 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002778 return( mbedtls_to_psa_error( ret ) );
2779 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002780 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002781#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 {
2783 return( PSA_ERROR_NOT_SUPPORTED );
2784 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002785}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002786
Gilles Peskinec5487a82018-12-03 18:08:14 +01002787psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002788 psa_algorithm_t alg,
2789 const uint8_t *input,
2790 size_t input_length,
2791 const uint8_t *salt,
2792 size_t salt_length,
2793 uint8_t *output,
2794 size_t output_size,
2795 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002797 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002798 psa_status_t status;
2799
Darryl Green5cc689a2018-07-24 15:34:10 +01002800 (void) input;
2801 (void) input_length;
2802 (void) salt;
2803 (void) output;
2804 (void) output_size;
2805
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002806 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002807
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002808 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2809 return( PSA_ERROR_INVALID_ARGUMENT );
2810
Gilles Peskinec5487a82018-12-03 18:08:14 +01002811 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002812 if( status != PSA_SUCCESS )
2813 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002814 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2815 return( PSA_ERROR_INVALID_ARGUMENT );
2816
2817#if defined(MBEDTLS_RSA_C)
2818 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2819 {
2820 mbedtls_rsa_context *rsa = slot->data.rsa;
2821 int ret;
2822
Gilles Peskine630a18a2018-06-29 17:49:35 +02002823 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002824 return( PSA_ERROR_INVALID_ARGUMENT );
2825
2826#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002827 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002829 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2830 mbedtls_ctr_drbg_random,
2831 &global_data.ctr_drbg,
2832 MBEDTLS_RSA_PRIVATE,
2833 output_length,
2834 input,
2835 output,
2836 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002837 }
2838 else
2839#endif /* MBEDTLS_PKCS1_V15 */
2840#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002841 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002842 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002843 psa_rsa_oaep_set_padding_mode( alg, rsa );
2844 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2845 mbedtls_ctr_drbg_random,
2846 &global_data.ctr_drbg,
2847 MBEDTLS_RSA_PRIVATE,
2848 salt, salt_length,
2849 output_length,
2850 input,
2851 output,
2852 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002853 }
2854 else
2855#endif /* MBEDTLS_PKCS1_V21 */
2856 {
2857 return( PSA_ERROR_INVALID_ARGUMENT );
2858 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002859
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002860 return( mbedtls_to_psa_error( ret ) );
2861 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002862 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002863#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002864 {
2865 return( PSA_ERROR_NOT_SUPPORTED );
2866 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002867}
Gilles Peskine20035e32018-02-03 22:44:14 +01002868
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002869
2870
mohammad1603503973b2018-03-12 15:59:30 +02002871/****************************************************************/
2872/* Symmetric cryptography */
2873/****************************************************************/
2874
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002875/* Initialize the cipher operation structure. Once this function has been
2876 * called, psa_cipher_abort can run and will do the right thing. */
2877static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2878 psa_algorithm_t alg )
2879{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002880 if( ! PSA_ALG_IS_CIPHER( alg ) )
2881 {
2882 memset( operation, 0, sizeof( *operation ) );
2883 return( PSA_ERROR_INVALID_ARGUMENT );
2884 }
2885
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002886 operation->alg = alg;
2887 operation->key_set = 0;
2888 operation->iv_set = 0;
2889 operation->iv_required = 1;
2890 operation->iv_size = 0;
2891 operation->block_size = 0;
2892 mbedtls_cipher_init( &operation->ctx.cipher );
2893 return( PSA_SUCCESS );
2894}
2895
Gilles Peskinee553c652018-06-04 16:22:46 +02002896static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002897 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002898 psa_algorithm_t alg,
2899 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002900{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002901 int ret = 0;
2902 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002903 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002904 size_t key_bits;
2905 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002906 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2907 PSA_KEY_USAGE_ENCRYPT :
2908 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002909
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002910 /* A context must be freshly initialized before it can be set up. */
2911 if( operation->alg != 0 )
2912 {
2913 return( PSA_ERROR_BAD_STATE );
2914 }
2915
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002916 status = psa_cipher_init( operation, alg );
2917 if( status != PSA_SUCCESS )
2918 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002919
Gilles Peskinec5487a82018-12-03 18:08:14 +01002920 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002921 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002922 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002923 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002924
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002925 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002926 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002927 {
2928 status = PSA_ERROR_NOT_SUPPORTED;
2929 goto exit;
2930 }
mohammad1603503973b2018-03-12 15:59:30 +02002931
mohammad1603503973b2018-03-12 15:59:30 +02002932 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002933 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002934 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002935
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002936#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002937 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002938 {
2939 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2940 unsigned char keys[24];
2941 memcpy( keys, slot->data.raw.data, 16 );
2942 memcpy( keys + 16, slot->data.raw.data, 8 );
2943 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2944 keys,
2945 192, cipher_operation );
2946 }
2947 else
2948#endif
2949 {
2950 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2951 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002952 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002953 }
Moran Peker41deec42018-04-04 15:43:05 +03002954 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002955 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002956
mohammad16038481e742018-03-18 13:57:31 +02002957#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002958 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002959 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002960 case PSA_ALG_CBC_NO_PADDING:
2961 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2962 MBEDTLS_PADDING_NONE );
2963 break;
2964 case PSA_ALG_CBC_PKCS7:
2965 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2966 MBEDTLS_PADDING_PKCS7 );
2967 break;
2968 default:
2969 /* The algorithm doesn't involve padding. */
2970 ret = 0;
2971 break;
2972 }
2973 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002974 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02002975#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2976
mohammad1603503973b2018-03-12 15:59:30 +02002977 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002978 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2979 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2980 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002981 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002982 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002983 }
mohammad1603503973b2018-03-12 15:59:30 +02002984
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002985exit:
2986 if( status == 0 )
2987 status = mbedtls_to_psa_error( ret );
2988 if( status != 0 )
2989 psa_cipher_abort( operation );
2990 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002991}
2992
Gilles Peskinefe119512018-07-08 21:39:34 +02002993psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002994 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002995 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002996{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002997 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002998}
2999
Gilles Peskinefe119512018-07-08 21:39:34 +02003000psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003001 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003002 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003003{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003004 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003005}
3006
Gilles Peskinefe119512018-07-08 21:39:34 +02003007psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3008 unsigned char *iv,
3009 size_t iv_size,
3010 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003011{
itayzafrir534bd7c2018-08-02 13:56:32 +03003012 psa_status_t status;
3013 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003014 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003015 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003016 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003017 }
Moran Peker41deec42018-04-04 15:43:05 +03003018 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003019 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003020 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003021 goto exit;
3022 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003023 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3024 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003025 if( ret != 0 )
3026 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003027 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003028 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003029 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003030
mohammad16038481e742018-03-18 13:57:31 +02003031 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003032 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003033
Moran Peker395db872018-05-31 14:07:14 +03003034exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003035 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003036 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003037 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003038}
3039
Gilles Peskinefe119512018-07-08 21:39:34 +02003040psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3041 const unsigned char *iv,
3042 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003043{
itayzafrir534bd7c2018-08-02 13:56:32 +03003044 psa_status_t status;
3045 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003046 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003047 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003048 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003049 }
Moran Pekera28258c2018-05-29 16:25:04 +03003050 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003051 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003052 status = PSA_ERROR_INVALID_ARGUMENT;
3053 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003054 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003055 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3056 status = mbedtls_to_psa_error( ret );
3057exit:
3058 if( status == PSA_SUCCESS )
3059 operation->iv_set = 1;
3060 else
mohammad1603503973b2018-03-12 15:59:30 +02003061 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003062 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003063}
3064
Gilles Peskinee553c652018-06-04 16:22:46 +02003065psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3066 const uint8_t *input,
3067 size_t input_length,
3068 unsigned char *output,
3069 size_t output_size,
3070 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003071{
itayzafrir534bd7c2018-08-02 13:56:32 +03003072 psa_status_t status;
3073 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003074 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003075
3076 if( operation->alg == 0 )
3077 {
3078 return( PSA_ERROR_BAD_STATE );
3079 }
3080
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003081 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003082 {
3083 /* Take the unprocessed partial block left over from previous
3084 * update calls, if any, plus the input to this call. Remove
3085 * the last partial block, if any. You get the data that will be
3086 * output in this call. */
3087 expected_output_size =
3088 ( operation->ctx.cipher.unprocessed_len + input_length )
3089 / operation->block_size * operation->block_size;
3090 }
3091 else
3092 {
3093 expected_output_size = input_length;
3094 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003095
Gilles Peskine89d789c2018-06-04 17:17:16 +02003096 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003097 {
3098 status = PSA_ERROR_BUFFER_TOO_SMALL;
3099 goto exit;
3100 }
mohammad160382759612018-03-12 18:16:40 +02003101
mohammad1603503973b2018-03-12 15:59:30 +02003102 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003103 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003104 status = mbedtls_to_psa_error( ret );
3105exit:
3106 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003107 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003108 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003109}
3110
Gilles Peskinee553c652018-06-04 16:22:46 +02003111psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3112 uint8_t *output,
3113 size_t output_size,
3114 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003115{
David Saadab4ecc272019-02-14 13:48:10 +02003116 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003117 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003118 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003119
mohammad1603503973b2018-03-12 15:59:30 +02003120 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003121 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003122 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003123 }
3124 if( operation->iv_required && ! operation->iv_set )
3125 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003126 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003127 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003128
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003129 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003130 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3131 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003132 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003133 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003134 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003135 }
3136
Janos Follath315b51c2018-07-09 16:04:51 +01003137 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3138 temp_output_buffer,
3139 output_length );
3140 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003141 {
Janos Follath315b51c2018-07-09 16:04:51 +01003142 status = mbedtls_to_psa_error( cipher_ret );
3143 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003144 }
Janos Follath315b51c2018-07-09 16:04:51 +01003145
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003146 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003147 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003148 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003149 memcpy( output, temp_output_buffer, *output_length );
3150 else
3151 {
Janos Follath315b51c2018-07-09 16:04:51 +01003152 status = PSA_ERROR_BUFFER_TOO_SMALL;
3153 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003154 }
mohammad1603503973b2018-03-12 15:59:30 +02003155
Gilles Peskine3f108122018-12-07 18:14:53 +01003156 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003157 status = psa_cipher_abort( operation );
3158
3159 return( status );
3160
3161error:
3162
3163 *output_length = 0;
3164
Gilles Peskine3f108122018-12-07 18:14:53 +01003165 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003166 (void) psa_cipher_abort( operation );
3167
3168 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003169}
3170
Gilles Peskinee553c652018-06-04 16:22:46 +02003171psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3172{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003174 {
3175 /* The object has (apparently) been initialized but it is not
3176 * in use. It's ok to call abort on such an object, and there's
3177 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003179 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003180
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003181 /* Sanity check (shouldn't happen: operation->alg should
3182 * always have been initialized to a valid value). */
3183 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3184 return( PSA_ERROR_BAD_STATE );
3185
mohammad1603503973b2018-03-12 15:59:30 +02003186 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003187
Moran Peker41deec42018-04-04 15:43:05 +03003188 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003189 operation->key_set = 0;
3190 operation->iv_set = 0;
3191 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003192 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003193 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003194
Moran Peker395db872018-05-31 14:07:14 +03003195 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003196}
3197
Gilles Peskinea0655c32018-04-30 17:06:50 +02003198
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003199
mohammad16038cc1cee2018-03-28 01:21:33 +03003200/****************************************************************/
3201/* Key Policy */
3202/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003203
mohammad160327010052018-07-03 13:16:15 +03003204#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003205void psa_key_policy_set_usage( psa_key_policy_t *policy,
3206 psa_key_usage_t usage,
3207 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003208{
mohammad16034eed7572018-03-28 05:14:59 -07003209 policy->usage = usage;
3210 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003211}
3212
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003213psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003214{
mohammad16036df908f2018-04-02 08:34:15 -07003215 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003216}
3217
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003218psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003219{
mohammad16036df908f2018-04-02 08:34:15 -07003220 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003221}
mohammad160327010052018-07-03 13:16:15 +03003222#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003223
Gilles Peskinec5487a82018-12-03 18:08:14 +01003224psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003225 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003226{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003227 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003228 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003229
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003230 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003231 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003232
Gilles Peskinec5487a82018-12-03 18:08:14 +01003233 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003234 if( status != PSA_SUCCESS )
3235 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003236
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003237 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3238 PSA_KEY_USAGE_ENCRYPT |
3239 PSA_KEY_USAGE_DECRYPT |
3240 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003241 PSA_KEY_USAGE_VERIFY |
3242 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003243 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003244
mohammad16036df908f2018-04-02 08:34:15 -07003245 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003246
3247 return( PSA_SUCCESS );
3248}
3249
Gilles Peskinec5487a82018-12-03 18:08:14 +01003250psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003251 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003252{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003253 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003255
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003256 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003257 return( PSA_ERROR_INVALID_ARGUMENT );
3258
Gilles Peskinec5487a82018-12-03 18:08:14 +01003259 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003260 if( status != PSA_SUCCESS )
3261 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003262
mohammad16036df908f2018-04-02 08:34:15 -07003263 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003264
3265 return( PSA_SUCCESS );
3266}
Gilles Peskine20035e32018-02-03 22:44:14 +01003267
Gilles Peskinea0655c32018-04-30 17:06:50 +02003268
3269
mohammad1603804cd712018-03-20 22:44:08 +02003270/****************************************************************/
3271/* Key Lifetime */
3272/****************************************************************/
3273
Gilles Peskinec5487a82018-12-03 18:08:14 +01003274psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003275 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003276{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003277 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003278 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003279
Gilles Peskinec5487a82018-12-03 18:08:14 +01003280 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281 if( status != PSA_SUCCESS )
3282 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003283
mohammad1603804cd712018-03-20 22:44:08 +02003284 *lifetime = slot->lifetime;
3285
3286 return( PSA_SUCCESS );
3287}
3288
Gilles Peskine20035e32018-02-03 22:44:14 +01003289
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003290
mohammad16035955c982018-04-26 00:53:03 +03003291/****************************************************************/
3292/* AEAD */
3293/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003294
Gilles Peskineedf9a652018-08-17 18:11:56 +02003295typedef struct
3296{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003297 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003298 const mbedtls_cipher_info_t *cipher_info;
3299 union
3300 {
3301#if defined(MBEDTLS_CCM_C)
3302 mbedtls_ccm_context ccm;
3303#endif /* MBEDTLS_CCM_C */
3304#if defined(MBEDTLS_GCM_C)
3305 mbedtls_gcm_context gcm;
3306#endif /* MBEDTLS_GCM_C */
3307 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003308 psa_algorithm_t core_alg;
3309 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003310 uint8_t tag_length;
3311} aead_operation_t;
3312
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003313static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003314{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003315 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003316 {
3317#if defined(MBEDTLS_CCM_C)
3318 case PSA_ALG_CCM:
3319 mbedtls_ccm_free( &operation->ctx.ccm );
3320 break;
3321#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003322#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003323 case PSA_ALG_GCM:
3324 mbedtls_gcm_free( &operation->ctx.gcm );
3325 break;
3326#endif /* MBEDTLS_GCM_C */
3327 }
3328}
3329
3330static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003331 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003332 psa_key_usage_t usage,
3333 psa_algorithm_t alg )
3334{
3335 psa_status_t status;
3336 size_t key_bits;
3337 mbedtls_cipher_id_t cipher_id;
3338
Gilles Peskinec5487a82018-12-03 18:08:14 +01003339 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003340 if( status != PSA_SUCCESS )
3341 return( status );
3342
3343 key_bits = psa_get_key_bits( operation->slot );
3344
3345 operation->cipher_info =
3346 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3347 &cipher_id );
3348 if( operation->cipher_info == NULL )
3349 return( PSA_ERROR_NOT_SUPPORTED );
3350
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003351 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003352 {
3353#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003354 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3355 operation->core_alg = PSA_ALG_CCM;
3356 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003357 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3358 return( PSA_ERROR_INVALID_ARGUMENT );
3359 mbedtls_ccm_init( &operation->ctx.ccm );
3360 status = mbedtls_to_psa_error(
3361 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3362 operation->slot->data.raw.data,
3363 (unsigned int) key_bits ) );
3364 if( status != 0 )
3365 goto cleanup;
3366 break;
3367#endif /* MBEDTLS_CCM_C */
3368
3369#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003370 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3371 operation->core_alg = PSA_ALG_GCM;
3372 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003373 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3374 return( PSA_ERROR_INVALID_ARGUMENT );
3375 mbedtls_gcm_init( &operation->ctx.gcm );
3376 status = mbedtls_to_psa_error(
3377 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3378 operation->slot->data.raw.data,
3379 (unsigned int) key_bits ) );
3380 break;
3381#endif /* MBEDTLS_GCM_C */
3382
3383 default:
3384 return( PSA_ERROR_NOT_SUPPORTED );
3385 }
3386
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003387 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3388 {
3389 status = PSA_ERROR_INVALID_ARGUMENT;
3390 goto cleanup;
3391 }
3392 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3393 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3394 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3395 * In both cases, mbedtls_xxx will validate the tag length below. */
3396
Gilles Peskineedf9a652018-08-17 18:11:56 +02003397 return( PSA_SUCCESS );
3398
3399cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003400 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 return( status );
3402}
3403
Gilles Peskinec5487a82018-12-03 18:08:14 +01003404psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003405 psa_algorithm_t alg,
3406 const uint8_t *nonce,
3407 size_t nonce_length,
3408 const uint8_t *additional_data,
3409 size_t additional_data_length,
3410 const uint8_t *plaintext,
3411 size_t plaintext_length,
3412 uint8_t *ciphertext,
3413 size_t ciphertext_size,
3414 size_t *ciphertext_length )
3415{
mohammad16035955c982018-04-26 00:53:03 +03003416 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003417 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003418 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003419
mohammad1603f08a5502018-06-03 15:05:47 +03003420 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003421
Gilles Peskinec5487a82018-12-03 18:08:14 +01003422 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003423 if( status != PSA_SUCCESS )
3424 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003425
Gilles Peskineedf9a652018-08-17 18:11:56 +02003426 /* For all currently supported modes, the tag is at the end of the
3427 * ciphertext. */
3428 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3429 {
3430 status = PSA_ERROR_BUFFER_TOO_SMALL;
3431 goto exit;
3432 }
3433 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003434
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003435#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003436 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003437 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003438 status = mbedtls_to_psa_error(
3439 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3440 MBEDTLS_GCM_ENCRYPT,
3441 plaintext_length,
3442 nonce, nonce_length,
3443 additional_data, additional_data_length,
3444 plaintext, ciphertext,
3445 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003446 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003447 else
3448#endif /* MBEDTLS_GCM_C */
3449#if defined(MBEDTLS_CCM_C)
3450 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003451 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003452 status = mbedtls_to_psa_error(
3453 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3454 plaintext_length,
3455 nonce, nonce_length,
3456 additional_data,
3457 additional_data_length,
3458 plaintext, ciphertext,
3459 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003460 }
mohammad16035c8845f2018-05-09 05:40:09 -07003461 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003462#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003463 {
mohammad1603554faad2018-06-03 15:07:38 +03003464 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003465 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003466
Gilles Peskineedf9a652018-08-17 18:11:56 +02003467 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3468 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003469
Gilles Peskineedf9a652018-08-17 18:11:56 +02003470exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003471 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003472 if( status == PSA_SUCCESS )
3473 *ciphertext_length = plaintext_length + operation.tag_length;
3474 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003475}
3476
Gilles Peskineee652a32018-06-01 19:23:52 +02003477/* Locate the tag in a ciphertext buffer containing the encrypted data
3478 * followed by the tag. Return the length of the part preceding the tag in
3479 * *plaintext_length. This is the size of the plaintext in modes where
3480 * the encrypted data has the same size as the plaintext, such as
3481 * CCM and GCM. */
3482static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3483 const uint8_t *ciphertext,
3484 size_t ciphertext_length,
3485 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003486 const uint8_t **p_tag )
3487{
3488 size_t payload_length;
3489 if( tag_length > ciphertext_length )
3490 return( PSA_ERROR_INVALID_ARGUMENT );
3491 payload_length = ciphertext_length - tag_length;
3492 if( payload_length > plaintext_size )
3493 return( PSA_ERROR_BUFFER_TOO_SMALL );
3494 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003495 return( PSA_SUCCESS );
3496}
3497
Gilles Peskinec5487a82018-12-03 18:08:14 +01003498psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003499 psa_algorithm_t alg,
3500 const uint8_t *nonce,
3501 size_t nonce_length,
3502 const uint8_t *additional_data,
3503 size_t additional_data_length,
3504 const uint8_t *ciphertext,
3505 size_t ciphertext_length,
3506 uint8_t *plaintext,
3507 size_t plaintext_size,
3508 size_t *plaintext_length )
3509{
mohammad16035955c982018-04-26 00:53:03 +03003510 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003511 aead_operation_t operation;
3512 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003513
Gilles Peskineee652a32018-06-01 19:23:52 +02003514 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003515
Gilles Peskinec5487a82018-12-03 18:08:14 +01003516 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003517 if( status != PSA_SUCCESS )
3518 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003519
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003520#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003521 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003522 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003523 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003524 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003525 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003526 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003527 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003528
Gilles Peskineedf9a652018-08-17 18:11:56 +02003529 status = mbedtls_to_psa_error(
3530 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3531 ciphertext_length - operation.tag_length,
3532 nonce, nonce_length,
3533 additional_data,
3534 additional_data_length,
3535 tag, operation.tag_length,
3536 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003537 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003538 else
3539#endif /* MBEDTLS_GCM_C */
3540#if defined(MBEDTLS_CCM_C)
3541 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003542 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003543 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003544 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003545 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003546 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003547 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003548
Gilles Peskineedf9a652018-08-17 18:11:56 +02003549 status = mbedtls_to_psa_error(
3550 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3551 ciphertext_length - operation.tag_length,
3552 nonce, nonce_length,
3553 additional_data,
3554 additional_data_length,
3555 ciphertext, plaintext,
3556 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003557 }
mohammad160339574652018-06-01 04:39:53 -07003558 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003559#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003560 {
mohammad1603554faad2018-06-03 15:07:38 +03003561 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003562 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003563
Gilles Peskineedf9a652018-08-17 18:11:56 +02003564 if( status != PSA_SUCCESS && plaintext_size != 0 )
3565 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003566
Gilles Peskineedf9a652018-08-17 18:11:56 +02003567exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003568 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003569 if( status == PSA_SUCCESS )
3570 *plaintext_length = ciphertext_length - operation.tag_length;
3571 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003572}
3573
Gilles Peskinea0655c32018-04-30 17:06:50 +02003574
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003575
Gilles Peskine20035e32018-02-03 22:44:14 +01003576/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003577/* Generators */
3578/****************************************************************/
3579
3580psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3581{
3582 psa_status_t status = PSA_SUCCESS;
3583 if( generator->alg == 0 )
3584 {
3585 /* The object has (apparently) been initialized but it is not
3586 * in use. It's ok to call abort on such an object, and there's
3587 * nothing to do. */
3588 }
3589 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003590 if( generator->alg == PSA_ALG_SELECT_RAW )
3591 {
3592 if( generator->ctx.buffer.data != NULL )
3593 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003594 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003595 generator->ctx.buffer.size );
3596 mbedtls_free( generator->ctx.buffer.data );
3597 }
3598 }
3599 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003600#if defined(MBEDTLS_MD_C)
3601 if( PSA_ALG_IS_HKDF( generator->alg ) )
3602 {
3603 mbedtls_free( generator->ctx.hkdf.info );
3604 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3605 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003606 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3607 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3608 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003609 {
3610 if( generator->ctx.tls12_prf.key != NULL )
3611 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003612 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003613 generator->ctx.tls12_prf.key_len );
3614 mbedtls_free( generator->ctx.tls12_prf.key );
3615 }
Hanno Becker580fba12018-11-13 20:50:45 +00003616
3617 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3618 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003619 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003620 generator->ctx.tls12_prf.Ai_with_seed_len );
3621 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3622 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003623 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003624 else
3625#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003626 {
3627 status = PSA_ERROR_BAD_STATE;
3628 }
3629 memset( generator, 0, sizeof( *generator ) );
3630 return( status );
3631}
3632
3633
3634psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3635 size_t *capacity)
3636{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003637 if( generator->alg == 0 )
3638 {
3639 /* This is a blank generator. */
3640 return PSA_ERROR_BAD_STATE;
3641 }
3642
Gilles Peskineeab56e42018-07-12 17:12:33 +02003643 *capacity = generator->capacity;
3644 return( PSA_SUCCESS );
3645}
3646
Gilles Peskinebef7f142018-07-12 17:22:21 +02003647#if defined(MBEDTLS_MD_C)
3648/* Read some bytes from an HKDF-based generator. This performs a chunk
3649 * of the expand phase of the HKDF algorithm. */
3650static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3651 psa_algorithm_t hash_alg,
3652 uint8_t *output,
3653 size_t output_length )
3654{
3655 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3656 psa_status_t status;
3657
3658 while( output_length != 0 )
3659 {
3660 /* Copy what remains of the current block */
3661 uint8_t n = hash_length - hkdf->offset_in_block;
3662 if( n > output_length )
3663 n = (uint8_t) output_length;
3664 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3665 output += n;
3666 output_length -= n;
3667 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003668 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003669 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003670 /* We can't be wanting more output after block 0xff, otherwise
3671 * the capacity check in psa_generator_read() would have
3672 * prevented this call. It could happen only if the generator
3673 * object was corrupted or if this function is called directly
3674 * inside the library. */
3675 if( hkdf->block_number == 0xff )
3676 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003677
3678 /* We need a new block */
3679 ++hkdf->block_number;
3680 hkdf->offset_in_block = 0;
3681 status = psa_hmac_setup_internal( &hkdf->hmac,
3682 hkdf->prk, hash_length,
3683 hash_alg );
3684 if( status != PSA_SUCCESS )
3685 return( status );
3686 if( hkdf->block_number != 1 )
3687 {
3688 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3689 hkdf->output_block,
3690 hash_length );
3691 if( status != PSA_SUCCESS )
3692 return( status );
3693 }
3694 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3695 hkdf->info,
3696 hkdf->info_length );
3697 if( status != PSA_SUCCESS )
3698 return( status );
3699 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3700 &hkdf->block_number, 1 );
3701 if( status != PSA_SUCCESS )
3702 return( status );
3703 status = psa_hmac_finish_internal( &hkdf->hmac,
3704 hkdf->output_block,
3705 sizeof( hkdf->output_block ) );
3706 if( status != PSA_SUCCESS )
3707 return( status );
3708 }
3709
3710 return( PSA_SUCCESS );
3711}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003712
3713static psa_status_t psa_generator_tls12_prf_generate_next_block(
3714 psa_tls12_prf_generator_t *tls12_prf,
3715 psa_algorithm_t alg )
3716{
3717 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3718 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3719 psa_hmac_internal_data hmac;
3720 psa_status_t status, cleanup_status;
3721
Hanno Becker3b339e22018-11-13 20:56:14 +00003722 unsigned char *Ai;
3723 size_t Ai_len;
3724
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003725 /* We can't be wanting more output after block 0xff, otherwise
3726 * the capacity check in psa_generator_read() would have
3727 * prevented this call. It could happen only if the generator
3728 * object was corrupted or if this function is called directly
3729 * inside the library. */
3730 if( tls12_prf->block_number == 0xff )
3731 return( PSA_ERROR_BAD_STATE );
3732
3733 /* We need a new block */
3734 ++tls12_prf->block_number;
3735 tls12_prf->offset_in_block = 0;
3736
3737 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3738 *
3739 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3740 *
3741 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3742 * HMAC_hash(secret, A(2) + seed) +
3743 * HMAC_hash(secret, A(3) + seed) + ...
3744 *
3745 * A(0) = seed
3746 * A(i) = HMAC_hash( secret, A(i-1) )
3747 *
3748 * The `psa_tls12_prf_generator` structures saves the block
3749 * `HMAC_hash(secret, A(i) + seed)` from which the output
3750 * is currently extracted as `output_block`, while
3751 * `A(i) + seed` is stored in `Ai_with_seed`.
3752 *
3753 * Generating a new block means recalculating `Ai_with_seed`
3754 * from the A(i)-part of it, and afterwards recalculating
3755 * `output_block`.
3756 *
3757 * A(0) is computed at setup time.
3758 *
3759 */
3760
3761 psa_hmac_init_internal( &hmac );
3762
3763 /* We must distinguish the calculation of A(1) from those
3764 * of A(2) and higher, because A(0)=seed has a different
3765 * length than the other A(i). */
3766 if( tls12_prf->block_number == 1 )
3767 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003768 Ai = tls12_prf->Ai_with_seed + hash_length;
3769 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003770 }
3771 else
3772 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003773 Ai = tls12_prf->Ai_with_seed;
3774 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003775 }
3776
Hanno Becker3b339e22018-11-13 20:56:14 +00003777 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3778 status = psa_hmac_setup_internal( &hmac,
3779 tls12_prf->key,
3780 tls12_prf->key_len,
3781 hash_alg );
3782 if( status != PSA_SUCCESS )
3783 goto cleanup;
3784
3785 status = psa_hash_update( &hmac.hash_ctx,
3786 Ai, Ai_len );
3787 if( status != PSA_SUCCESS )
3788 goto cleanup;
3789
3790 status = psa_hmac_finish_internal( &hmac,
3791 tls12_prf->Ai_with_seed,
3792 hash_length );
3793 if( status != PSA_SUCCESS )
3794 goto cleanup;
3795
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003796 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3797 status = psa_hmac_setup_internal( &hmac,
3798 tls12_prf->key,
3799 tls12_prf->key_len,
3800 hash_alg );
3801 if( status != PSA_SUCCESS )
3802 goto cleanup;
3803
3804 status = psa_hash_update( &hmac.hash_ctx,
3805 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003806 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003807 if( status != PSA_SUCCESS )
3808 goto cleanup;
3809
3810 status = psa_hmac_finish_internal( &hmac,
3811 tls12_prf->output_block,
3812 hash_length );
3813 if( status != PSA_SUCCESS )
3814 goto cleanup;
3815
3816cleanup:
3817
3818 cleanup_status = psa_hmac_abort_internal( &hmac );
3819 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3820 status = cleanup_status;
3821
3822 return( status );
3823}
3824
3825/* Read some bytes from an TLS-1.2-PRF-based generator.
3826 * See Section 5 of RFC 5246. */
3827static psa_status_t psa_generator_tls12_prf_read(
3828 psa_tls12_prf_generator_t *tls12_prf,
3829 psa_algorithm_t alg,
3830 uint8_t *output,
3831 size_t output_length )
3832{
3833 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3834 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3835 psa_status_t status;
3836
3837 while( output_length != 0 )
3838 {
3839 /* Copy what remains of the current block */
3840 uint8_t n = hash_length - tls12_prf->offset_in_block;
3841
3842 /* Check if we have fully processed the current block. */
3843 if( n == 0 )
3844 {
3845 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3846 alg );
3847 if( status != PSA_SUCCESS )
3848 return( status );
3849
3850 continue;
3851 }
3852
3853 if( n > output_length )
3854 n = (uint8_t) output_length;
3855 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3856 n );
3857 output += n;
3858 output_length -= n;
3859 tls12_prf->offset_in_block += n;
3860 }
3861
3862 return( PSA_SUCCESS );
3863}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003864#endif /* MBEDTLS_MD_C */
3865
Gilles Peskineeab56e42018-07-12 17:12:33 +02003866psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3867 uint8_t *output,
3868 size_t output_length )
3869{
3870 psa_status_t status;
3871
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003872 if( generator->alg == 0 )
3873 {
3874 /* This is a blank generator. */
3875 return PSA_ERROR_BAD_STATE;
3876 }
3877
Gilles Peskineeab56e42018-07-12 17:12:33 +02003878 if( output_length > generator->capacity )
3879 {
3880 generator->capacity = 0;
3881 /* Go through the error path to wipe all confidential data now
3882 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003883 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003884 goto exit;
3885 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003886 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003887 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003888 /* Edge case: this is a finished generator, and 0 bytes
3889 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003890 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3891 * INSUFFICIENT_CAPACITY, which is right for a finished
3892 * generator, for consistency with the case when
3893 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003894 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003895 }
3896 generator->capacity -= output_length;
3897
Gilles Peskine751d9652018-09-18 12:05:44 +02003898 if( generator->alg == PSA_ALG_SELECT_RAW )
3899 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003900 /* Initially, the capacity of a selection generator is always
3901 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3902 * abbreviated in this comment as `size`. When the remaining
3903 * capacity is `c`, the next bytes to serve start `c` bytes
3904 * from the end of the buffer, i.e. `size - c` from the
3905 * beginning of the buffer. Since `generator->capacity` was just
3906 * decremented above, we need to serve the bytes from
3907 * `size - generator->capacity - output_length` to
3908 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003909 size_t offset =
3910 generator->ctx.buffer.size - generator->capacity - output_length;
3911 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3912 status = PSA_SUCCESS;
3913 }
3914 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003915#if defined(MBEDTLS_MD_C)
3916 if( PSA_ALG_IS_HKDF( generator->alg ) )
3917 {
3918 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3919 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3920 output, output_length );
3921 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003922 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3923 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003924 {
3925 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3926 generator->alg, output,
3927 output_length );
3928 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003929 else
3930#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003931 {
3932 return( PSA_ERROR_BAD_STATE );
3933 }
3934
3935exit:
3936 if( status != PSA_SUCCESS )
3937 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003938 /* Preserve the algorithm upon errors, but clear all sensitive state.
3939 * This allows us to differentiate between exhausted generators and
3940 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3941 * generators. */
3942 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003943 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003944 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003945 memset( output, '!', output_length );
3946 }
3947 return( status );
3948}
3949
Gilles Peskine08542d82018-07-19 17:05:42 +02003950#if defined(MBEDTLS_DES_C)
3951static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3952{
3953 if( data_size >= 8 )
3954 mbedtls_des_key_set_parity( data );
3955 if( data_size >= 16 )
3956 mbedtls_des_key_set_parity( data + 8 );
3957 if( data_size >= 24 )
3958 mbedtls_des_key_set_parity( data + 16 );
3959}
3960#endif /* MBEDTLS_DES_C */
3961
Gilles Peskinec5487a82018-12-03 18:08:14 +01003962psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003963 psa_key_type_t type,
3964 size_t bits,
3965 psa_crypto_generator_t *generator )
3966{
3967 uint8_t *data = NULL;
3968 size_t bytes = PSA_BITS_TO_BYTES( bits );
3969 psa_status_t status;
3970
3971 if( ! key_type_is_raw_bytes( type ) )
3972 return( PSA_ERROR_INVALID_ARGUMENT );
3973 if( bits % 8 != 0 )
3974 return( PSA_ERROR_INVALID_ARGUMENT );
3975 data = mbedtls_calloc( 1, bytes );
3976 if( data == NULL )
3977 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3978
3979 status = psa_generator_read( generator, data, bytes );
3980 if( status != PSA_SUCCESS )
3981 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003982#if defined(MBEDTLS_DES_C)
3983 if( type == PSA_KEY_TYPE_DES )
3984 psa_des_set_key_parity( data, bytes );
3985#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003986 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003987
3988exit:
3989 mbedtls_free( data );
3990 return( status );
3991}
3992
3993
3994
3995/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003996/* Key derivation */
3997/****************************************************************/
3998
Gilles Peskinea05219c2018-11-16 16:02:56 +01003999#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004000/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004001 * of the HKDF algorithm.
4002 *
4003 * Note that if this function fails, you must call psa_generator_abort()
4004 * to potentially free embedded data structures and wipe confidential data.
4005 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004006static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004007 const uint8_t *secret,
4008 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004009 psa_algorithm_t hash_alg,
4010 const uint8_t *salt,
4011 size_t salt_length,
4012 const uint8_t *label,
4013 size_t label_length )
4014{
4015 psa_status_t status;
4016 status = psa_hmac_setup_internal( &hkdf->hmac,
4017 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004018 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004019 if( status != PSA_SUCCESS )
4020 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004021 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004022 if( status != PSA_SUCCESS )
4023 return( status );
4024 status = psa_hmac_finish_internal( &hkdf->hmac,
4025 hkdf->prk,
4026 sizeof( hkdf->prk ) );
4027 if( status != PSA_SUCCESS )
4028 return( status );
4029 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4030 hkdf->block_number = 0;
4031 hkdf->info_length = label_length;
4032 if( label_length != 0 )
4033 {
4034 hkdf->info = mbedtls_calloc( 1, label_length );
4035 if( hkdf->info == NULL )
4036 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4037 memcpy( hkdf->info, label, label_length );
4038 }
4039 return( PSA_SUCCESS );
4040}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004041#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004042
Gilles Peskinea05219c2018-11-16 16:02:56 +01004043#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004044/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4045 *
4046 * Note that if this function fails, you must call psa_generator_abort()
4047 * to potentially free embedded data structures and wipe confidential data.
4048 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004049static psa_status_t psa_generator_tls12_prf_setup(
4050 psa_tls12_prf_generator_t *tls12_prf,
4051 const unsigned char *key,
4052 size_t key_len,
4053 psa_algorithm_t hash_alg,
4054 const uint8_t *salt,
4055 size_t salt_length,
4056 const uint8_t *label,
4057 size_t label_length )
4058{
4059 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004060 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4061 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004062
4063 tls12_prf->key = mbedtls_calloc( 1, key_len );
4064 if( tls12_prf->key == NULL )
4065 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4066 tls12_prf->key_len = key_len;
4067 memcpy( tls12_prf->key, key, key_len );
4068
Hanno Becker580fba12018-11-13 20:50:45 +00004069 overflow = ( salt_length + label_length < salt_length ) ||
4070 ( salt_length + label_length + hash_length < hash_length );
4071 if( overflow )
4072 return( PSA_ERROR_INVALID_ARGUMENT );
4073
4074 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4075 if( tls12_prf->Ai_with_seed == NULL )
4076 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4077 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4078
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004079 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4080 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004081 if( label_length != 0 )
4082 {
4083 memcpy( tls12_prf->Ai_with_seed + hash_length,
4084 label, label_length );
4085 }
4086
4087 if( salt_length != 0 )
4088 {
4089 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4090 salt, salt_length );
4091 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004092
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004093 /* The first block gets generated when
4094 * psa_generator_read() is called. */
4095 tls12_prf->block_number = 0;
4096 tls12_prf->offset_in_block = hash_length;
4097
4098 return( PSA_SUCCESS );
4099}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004100
4101/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4102static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4103 psa_tls12_prf_generator_t *tls12_prf,
4104 const unsigned char *psk,
4105 size_t psk_len,
4106 psa_algorithm_t hash_alg,
4107 const uint8_t *salt,
4108 size_t salt_length,
4109 const uint8_t *label,
4110 size_t label_length )
4111{
4112 psa_status_t status;
4113 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4114
4115 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4116 return( PSA_ERROR_INVALID_ARGUMENT );
4117
4118 /* Quoting RFC 4279, Section 2:
4119 *
4120 * The premaster secret is formed as follows: if the PSK is N octets
4121 * long, concatenate a uint16 with the value N, N zero octets, a second
4122 * uint16 with the value N, and the PSK itself.
4123 */
4124
4125 pms[0] = ( psk_len >> 8 ) & 0xff;
4126 pms[1] = ( psk_len >> 0 ) & 0xff;
4127 memset( pms + 2, 0, psk_len );
4128 pms[2 + psk_len + 0] = pms[0];
4129 pms[2 + psk_len + 1] = pms[1];
4130 memcpy( pms + 4 + psk_len, psk, psk_len );
4131
4132 status = psa_generator_tls12_prf_setup( tls12_prf,
4133 pms, 4 + 2 * psk_len,
4134 hash_alg,
4135 salt, salt_length,
4136 label, label_length );
4137
Gilles Peskine3f108122018-12-07 18:14:53 +01004138 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004139 return( status );
4140}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004141#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004142
Gilles Peskine346797d2018-11-16 16:05:06 +01004143/* Note that if this function fails, you must call psa_generator_abort()
4144 * to potentially free embedded data structures and wipe confidential data.
4145 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004146static psa_status_t psa_key_derivation_internal(
4147 psa_crypto_generator_t *generator,
4148 const uint8_t *secret, size_t secret_length,
4149 psa_algorithm_t alg,
4150 const uint8_t *salt, size_t salt_length,
4151 const uint8_t *label, size_t label_length,
4152 size_t capacity )
4153{
4154 psa_status_t status;
4155 size_t max_capacity;
4156
4157 /* Set generator->alg even on failure so that abort knows what to do. */
4158 generator->alg = alg;
4159
Gilles Peskine751d9652018-09-18 12:05:44 +02004160 if( alg == PSA_ALG_SELECT_RAW )
4161 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004162 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004163 if( salt_length != 0 )
4164 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004165 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004166 if( label_length != 0 )
4167 return( PSA_ERROR_INVALID_ARGUMENT );
4168 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4169 if( generator->ctx.buffer.data == NULL )
4170 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4171 memcpy( generator->ctx.buffer.data, secret, secret_length );
4172 generator->ctx.buffer.size = secret_length;
4173 max_capacity = secret_length;
4174 status = PSA_SUCCESS;
4175 }
4176 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004177#if defined(MBEDTLS_MD_C)
4178 if( PSA_ALG_IS_HKDF( alg ) )
4179 {
4180 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4181 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4182 if( hash_size == 0 )
4183 return( PSA_ERROR_NOT_SUPPORTED );
4184 max_capacity = 255 * hash_size;
4185 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4186 secret, secret_length,
4187 hash_alg,
4188 salt, salt_length,
4189 label, label_length );
4190 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004191 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4192 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4193 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004194 {
4195 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4196 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4197
4198 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4199 if( hash_alg != PSA_ALG_SHA_256 &&
4200 hash_alg != PSA_ALG_SHA_384 )
4201 {
4202 return( PSA_ERROR_NOT_SUPPORTED );
4203 }
4204
4205 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004206
4207 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4208 {
4209 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4210 secret, secret_length,
4211 hash_alg, salt, salt_length,
4212 label, label_length );
4213 }
4214 else
4215 {
4216 status = psa_generator_tls12_psk_to_ms_setup(
4217 &generator->ctx.tls12_prf,
4218 secret, secret_length,
4219 hash_alg, salt, salt_length,
4220 label, label_length );
4221 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004222 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004223 else
4224#endif
4225 {
4226 return( PSA_ERROR_NOT_SUPPORTED );
4227 }
4228
4229 if( status != PSA_SUCCESS )
4230 return( status );
4231
4232 if( capacity <= max_capacity )
4233 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004234 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4235 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004236 else
4237 return( PSA_ERROR_INVALID_ARGUMENT );
4238
4239 return( PSA_SUCCESS );
4240}
4241
Gilles Peskineea0fb492018-07-12 17:17:20 +02004242psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004243 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004244 psa_algorithm_t alg,
4245 const uint8_t *salt,
4246 size_t salt_length,
4247 const uint8_t *label,
4248 size_t label_length,
4249 size_t capacity )
4250{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004251 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004252 psa_status_t status;
4253
4254 if( generator->alg != 0 )
4255 return( PSA_ERROR_BAD_STATE );
4256
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004257 /* Make sure that alg is a key derivation algorithm. This prevents
4258 * key selection algorithms, which psa_key_derivation_internal
4259 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004260 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4261 return( PSA_ERROR_INVALID_ARGUMENT );
4262
Gilles Peskinec5487a82018-12-03 18:08:14 +01004263 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004264 if( status != PSA_SUCCESS )
4265 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004266
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004267 if( slot->type != PSA_KEY_TYPE_DERIVE )
4268 return( PSA_ERROR_INVALID_ARGUMENT );
4269
4270 status = psa_key_derivation_internal( generator,
4271 slot->data.raw.data,
4272 slot->data.raw.bytes,
4273 alg,
4274 salt, salt_length,
4275 label, label_length,
4276 capacity );
4277 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004278 psa_generator_abort( generator );
4279 return( status );
4280}
4281
4282
4283
4284/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004285/* Key agreement */
4286/****************************************************************/
4287
Gilles Peskinea05219c2018-11-16 16:02:56 +01004288#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004289static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4290 size_t peer_key_length,
4291 const mbedtls_ecp_keypair *our_key,
4292 uint8_t *shared_secret,
4293 size_t shared_secret_size,
4294 size_t *shared_secret_length )
4295{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004296 mbedtls_ecp_keypair *their_key = NULL;
4297 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004298 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004299 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004300
Jaeden Ameroccdce902019-01-10 11:42:27 +00004301 status = psa_import_ec_public_key(
4302 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4303 peer_key, peer_key_length,
4304 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004305 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004306 goto exit;
4307
Jaeden Amero97271b32019-01-10 19:38:51 +00004308 status = mbedtls_to_psa_error(
4309 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4310 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004311 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004312 status = mbedtls_to_psa_error(
4313 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4314 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004315 goto exit;
4316
Jaeden Amero97271b32019-01-10 19:38:51 +00004317 status = mbedtls_to_psa_error(
4318 mbedtls_ecdh_calc_secret( &ecdh,
4319 shared_secret_length,
4320 shared_secret, shared_secret_size,
4321 mbedtls_ctr_drbg_random,
4322 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004323
4324exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004325 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004326 mbedtls_ecp_keypair_free( their_key );
4327 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004328 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004329}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004330#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004331
Gilles Peskine01d718c2018-09-18 12:01:02 +02004332#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4333
Gilles Peskine346797d2018-11-16 16:05:06 +01004334/* Note that if this function fails, you must call psa_generator_abort()
4335 * to potentially free embedded data structures and wipe confidential data.
4336 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004337static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004338 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004339 const uint8_t *peer_key,
4340 size_t peer_key_length,
4341 psa_algorithm_t alg )
4342{
4343 psa_status_t status;
4344 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4345 size_t shared_secret_length = 0;
4346
4347 /* Step 1: run the secret agreement algorithm to generate the shared
4348 * secret. */
4349 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4350 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004351#if defined(MBEDTLS_ECDH_C)
4352 case PSA_ALG_ECDH_BASE:
4353 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4354 return( PSA_ERROR_INVALID_ARGUMENT );
4355 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4356 private_key->data.ecp,
4357 shared_secret,
4358 sizeof( shared_secret ),
4359 &shared_secret_length );
4360 break;
4361#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004362 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004363 (void) private_key;
4364 (void) peer_key;
4365 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004366 return( PSA_ERROR_NOT_SUPPORTED );
4367 }
4368 if( status != PSA_SUCCESS )
4369 goto exit;
4370
4371 /* Step 2: set up the key derivation to generate key material from
4372 * the shared secret. */
4373 status = psa_key_derivation_internal( generator,
4374 shared_secret, shared_secret_length,
4375 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4376 NULL, 0, NULL, 0,
4377 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4378exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004379 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004380 return( status );
4381}
4382
4383psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004384 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004385 const uint8_t *peer_key,
4386 size_t peer_key_length,
4387 psa_algorithm_t alg )
4388{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004389 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004390 psa_status_t status;
4391 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4392 return( PSA_ERROR_INVALID_ARGUMENT );
4393 status = psa_get_key_from_slot( private_key, &slot,
4394 PSA_KEY_USAGE_DERIVE, alg );
4395 if( status != PSA_SUCCESS )
4396 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004397 status = psa_key_agreement_internal( generator,
4398 slot,
4399 peer_key, peer_key_length,
4400 alg );
4401 if( status != PSA_SUCCESS )
4402 psa_generator_abort( generator );
4403 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004404}
4405
4406
4407
4408/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004409/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004410/****************************************************************/
4411
4412psa_status_t psa_generate_random( uint8_t *output,
4413 size_t output_size )
4414{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004415 int ret;
4416 GUARD_MODULE_INITIALIZED;
4417
4418 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004419 return( mbedtls_to_psa_error( ret ) );
4420}
4421
Gilles Peskine6bf4bae2019-02-24 17:47:27 +01004422#if defined(MBEDTLS_ENTROPY_NV_SEED) && \
4423 defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004424psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4425 size_t seed_size )
4426{
4427 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +02004428 struct psa_storage_info_t p_info;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004429 if( global_data.initialized )
4430 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004431
4432 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4433 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4434 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4435 return( PSA_ERROR_INVALID_ARGUMENT );
4436
David Saadaa2523b22019-02-18 13:56:26 +02004437 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004438
David Saadaa2523b22019-02-18 13:56:26 +02004439 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004440 {
David Saadaa2523b22019-02-18 13:56:26 +02004441 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004442 }
David Saadaa2523b22019-02-18 13:56:26 +02004443 else if( PSA_SUCCESS == status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004444 {
4445 /* You should not be here. Seed needs to be injected only once */
4446 status = PSA_ERROR_NOT_PERMITTED;
4447 }
4448 return( status );
4449}
4450#endif
4451
Gilles Peskinec5487a82018-12-03 18:08:14 +01004452psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004453 psa_key_type_t type,
4454 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004455 const void *extra,
4456 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004457{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004458 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004459 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004460
Gilles Peskine53d991e2018-07-12 01:14:59 +02004461 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004462 return( PSA_ERROR_INVALID_ARGUMENT );
4463
Gilles Peskinec5487a82018-12-03 18:08:14 +01004464 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004465 if( status != PSA_SUCCESS )
4466 return( status );
4467
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004468 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004469 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004470 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004471 if( status != PSA_SUCCESS )
4472 return( status );
4473 status = psa_generate_random( slot->data.raw.data,
4474 slot->data.raw.bytes );
4475 if( status != PSA_SUCCESS )
4476 {
4477 mbedtls_free( slot->data.raw.data );
4478 return( status );
4479 }
4480#if defined(MBEDTLS_DES_C)
4481 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004482 psa_des_set_key_parity( slot->data.raw.data,
4483 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004484#endif /* MBEDTLS_DES_C */
4485 }
4486 else
4487
4488#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4489 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4490 {
4491 mbedtls_rsa_context *rsa;
4492 int ret;
4493 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004494 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4495 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004496 /* Accept only byte-aligned keys, for the same reasons as
4497 * in psa_import_rsa_key(). */
4498 if( bits % 8 != 0 )
4499 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004500 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004501 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004502 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004503 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004504 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004505#if INT_MAX < 0xffffffff
4506 /* Check that the uint32_t value passed by the caller fits
4507 * in the range supported by this implementation. */
4508 if( p->e > INT_MAX )
4509 return( PSA_ERROR_NOT_SUPPORTED );
4510#endif
4511 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004512 }
4513 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4514 if( rsa == NULL )
4515 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4516 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4517 ret = mbedtls_rsa_gen_key( rsa,
4518 mbedtls_ctr_drbg_random,
4519 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004520 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004521 exponent );
4522 if( ret != 0 )
4523 {
4524 mbedtls_rsa_free( rsa );
4525 mbedtls_free( rsa );
4526 return( mbedtls_to_psa_error( ret ) );
4527 }
4528 slot->data.rsa = rsa;
4529 }
4530 else
4531#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4532
4533#if defined(MBEDTLS_ECP_C)
4534 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4535 {
4536 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4537 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4538 const mbedtls_ecp_curve_info *curve_info =
4539 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4540 mbedtls_ecp_keypair *ecp;
4541 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004542 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004543 return( PSA_ERROR_NOT_SUPPORTED );
4544 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4545 return( PSA_ERROR_NOT_SUPPORTED );
4546 if( curve_info->bit_size != bits )
4547 return( PSA_ERROR_INVALID_ARGUMENT );
4548 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4549 if( ecp == NULL )
4550 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4551 mbedtls_ecp_keypair_init( ecp );
4552 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4553 mbedtls_ctr_drbg_random,
4554 &global_data.ctr_drbg );
4555 if( ret != 0 )
4556 {
4557 mbedtls_ecp_keypair_free( ecp );
4558 mbedtls_free( ecp );
4559 return( mbedtls_to_psa_error( ret ) );
4560 }
4561 slot->data.ecp = ecp;
4562 }
4563 else
4564#endif /* MBEDTLS_ECP_C */
4565
4566 return( PSA_ERROR_NOT_SUPPORTED );
4567
4568 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004569
4570#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4571 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4572 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004573 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004574 }
4575#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4576
4577 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004578}
4579
4580
4581/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004582/* Module setup */
4583/****************************************************************/
4584
Gilles Peskine5e769522018-11-20 21:59:56 +01004585psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4586 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4587 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4588{
4589 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4590 return( PSA_ERROR_BAD_STATE );
4591 global_data.entropy_init = entropy_init;
4592 global_data.entropy_free = entropy_free;
4593 return( PSA_SUCCESS );
4594}
4595
Gilles Peskinee59236f2018-01-27 23:32:46 +01004596void mbedtls_psa_crypto_free( void )
4597{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004598 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004599 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4600 {
4601 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004602 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004603 }
4604 /* Wipe all remaining data, including configuration.
4605 * In particular, this sets all state indicator to the value
4606 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004607 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004608}
4609
4610psa_status_t psa_crypto_init( void )
4611{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004612 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004613 const unsigned char drbg_seed[] = "PSA";
4614
Gilles Peskinec6b69072018-11-20 21:42:52 +01004615 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004616 if( global_data.initialized != 0 )
4617 return( PSA_SUCCESS );
4618
Gilles Peskine5e769522018-11-20 21:59:56 +01004619 /* Set default configuration if
4620 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4621 if( global_data.entropy_init == NULL )
4622 global_data.entropy_init = mbedtls_entropy_init;
4623 if( global_data.entropy_free == NULL )
4624 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004625
Gilles Peskinec6b69072018-11-20 21:42:52 +01004626 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004627 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004628 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004629 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004630 status = mbedtls_to_psa_error(
4631 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4632 mbedtls_entropy_func,
4633 &global_data.entropy,
4634 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4635 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004636 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004637 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004638
Gilles Peskine66fb1262018-12-10 16:29:04 +01004639 status = psa_initialize_key_slots( );
4640 if( status != PSA_SUCCESS )
4641 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004642
4643 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004644 global_data.initialized = 1;
4645
Gilles Peskinee59236f2018-01-27 23:32:46 +01004646exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004647 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004648 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004649 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004650}
4651
4652#endif /* MBEDTLS_PSA_CRYPTO_C */