blob: 22b4c0cf8f99c5c2955b495c624bdf99cf6bba4e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020063#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/sha1.h"
77#include "mbedtls/sha256.h"
78#include "mbedtls/sha512.h"
79#include "mbedtls/xtea.h"
80
Netanel Gonen2bcd3122018-11-19 11:53:02 +020081#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020082#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020083#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010084
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskine9ef733f2018-02-07 21:05:37 +010087/* constant-time buffer comparison */
88static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
89{
90 size_t i;
91 unsigned char diff = 0;
92
93 for( i = 0; i < n; i++ )
94 diff |= a[i] ^ b[i];
95
96 return( diff );
97}
98
99
100
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101/****************************************************************/
102/* Global data, support functions and library management */
103/****************************************************************/
104
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105static int key_type_is_raw_bytes( psa_key_type_t type )
106{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200107 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108}
109
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100110/* Values for psa_global_data_t::rng_state */
111#define RNG_NOT_INITIALIZED 0
112#define RNG_INITIALIZED 1
113#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine5e769522018-11-20 21:59:56 +0100117 void (* entropy_init )( mbedtls_entropy_context *ctx );
118 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119 mbedtls_entropy_context entropy;
120 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131static psa_status_t mbedtls_to_psa_error( int ret )
132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
189 return( PSA_ERROR_NOT_SUPPORTED );
190 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
193 return( PSA_ERROR_INSUFFICIENT_MEMORY );
194 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
195 return( PSA_ERROR_INVALID_PADDING );
196 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
197 return( PSA_ERROR_BAD_STATE );
198 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
199 return( PSA_ERROR_INVALID_SIGNATURE );
200 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
201 return( PSA_ERROR_TAMPERING_DETECTED );
202 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
203 return( PSA_ERROR_HARDWARE_FAILURE );
204
205 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
206 return( PSA_ERROR_HARDWARE_FAILURE );
207
208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215
216 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
222 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
223 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225
226 case MBEDTLS_ERR_GCM_AUTH_FAILED:
227 return( PSA_ERROR_INVALID_SIGNATURE );
228 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200229 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
234 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
235 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
241 return( PSA_ERROR_INVALID_ARGUMENT );
242 case MBEDTLS_ERR_MD_ALLOC_FAILED:
243 return( PSA_ERROR_INSUFFICIENT_MEMORY );
244 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
Gilles Peskinef76aa772018-10-29 19:24:33 +0100249 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
252 return( PSA_ERROR_INVALID_ARGUMENT );
253 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
254 return( PSA_ERROR_INVALID_ARGUMENT );
255 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
256 return( PSA_ERROR_BUFFER_TOO_SMALL );
257 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
264 return( PSA_ERROR_INSUFFICIENT_MEMORY );
265
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100266 case MBEDTLS_ERR_PK_ALLOC_FAILED:
267 return( PSA_ERROR_INSUFFICIENT_MEMORY );
268 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
269 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100272 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
274 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
277 return( PSA_ERROR_NOT_SUPPORTED );
278 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
279 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
280 return( PSA_ERROR_NOT_PERMITTED );
281 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_INVALID_ALG:
284 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
285 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
288 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
290 return( PSA_ERROR_HARDWARE_FAILURE );
291
292 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
293 return( PSA_ERROR_HARDWARE_FAILURE );
294
295 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_RSA_INVALID_PADDING:
298 return( PSA_ERROR_INVALID_PADDING );
299 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
304 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
305 return( PSA_ERROR_TAMPERING_DETECTED );
306 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
307 return( PSA_ERROR_INVALID_SIGNATURE );
308 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
309 return( PSA_ERROR_BUFFER_TOO_SMALL );
310 case MBEDTLS_ERR_RSA_RNG_FAILED:
311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
312 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
318 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
319 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
322 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
323 return( PSA_ERROR_INVALID_ARGUMENT );
324 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
itayzafrir5c753392018-05-08 11:18:38 +0300327 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300328 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300329 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
331 return( PSA_ERROR_BUFFER_TOO_SMALL );
332 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
333 return( PSA_ERROR_NOT_SUPPORTED );
334 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
335 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
336 return( PSA_ERROR_INVALID_SIGNATURE );
337 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
338 return( PSA_ERROR_INSUFFICIENT_MEMORY );
339 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300341
Gilles Peskinee59236f2018-01-27 23:32:46 +0100342 default:
David Saadab4ecc272019-02-14 13:48:10 +0200343 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100344 }
345}
346
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200348
349
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350/****************************************************************/
351/* Key management */
352/****************************************************************/
353
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100354#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200355static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
356{
357 switch( grpid )
358 {
359 case MBEDTLS_ECP_DP_SECP192R1:
360 return( PSA_ECC_CURVE_SECP192R1 );
361 case MBEDTLS_ECP_DP_SECP224R1:
362 return( PSA_ECC_CURVE_SECP224R1 );
363 case MBEDTLS_ECP_DP_SECP256R1:
364 return( PSA_ECC_CURVE_SECP256R1 );
365 case MBEDTLS_ECP_DP_SECP384R1:
366 return( PSA_ECC_CURVE_SECP384R1 );
367 case MBEDTLS_ECP_DP_SECP521R1:
368 return( PSA_ECC_CURVE_SECP521R1 );
369 case MBEDTLS_ECP_DP_BP256R1:
370 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
371 case MBEDTLS_ECP_DP_BP384R1:
372 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
373 case MBEDTLS_ECP_DP_BP512R1:
374 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
375 case MBEDTLS_ECP_DP_CURVE25519:
376 return( PSA_ECC_CURVE_CURVE25519 );
377 case MBEDTLS_ECP_DP_SECP192K1:
378 return( PSA_ECC_CURVE_SECP192K1 );
379 case MBEDTLS_ECP_DP_SECP224K1:
380 return( PSA_ECC_CURVE_SECP224K1 );
381 case MBEDTLS_ECP_DP_SECP256K1:
382 return( PSA_ECC_CURVE_SECP256K1 );
383 case MBEDTLS_ECP_DP_CURVE448:
384 return( PSA_ECC_CURVE_CURVE448 );
385 default:
386 return( 0 );
387 }
388}
389
Gilles Peskine12313cd2018-06-20 00:20:32 +0200390static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
391{
392 switch( curve )
393 {
394 case PSA_ECC_CURVE_SECP192R1:
395 return( MBEDTLS_ECP_DP_SECP192R1 );
396 case PSA_ECC_CURVE_SECP224R1:
397 return( MBEDTLS_ECP_DP_SECP224R1 );
398 case PSA_ECC_CURVE_SECP256R1:
399 return( MBEDTLS_ECP_DP_SECP256R1 );
400 case PSA_ECC_CURVE_SECP384R1:
401 return( MBEDTLS_ECP_DP_SECP384R1 );
402 case PSA_ECC_CURVE_SECP521R1:
403 return( MBEDTLS_ECP_DP_SECP521R1 );
404 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 case PSA_ECC_CURVE_CURVE25519:
411 return( MBEDTLS_ECP_DP_CURVE25519 );
412 case PSA_ECC_CURVE_SECP192K1:
413 return( MBEDTLS_ECP_DP_SECP192K1 );
414 case PSA_ECC_CURVE_SECP224K1:
415 return( MBEDTLS_ECP_DP_SECP224K1 );
416 case PSA_ECC_CURVE_SECP256K1:
417 return( MBEDTLS_ECP_DP_SECP256K1 );
418 case PSA_ECC_CURVE_CURVE448:
419 return( MBEDTLS_ECP_DP_CURVE448 );
420 default:
421 return( MBEDTLS_ECP_DP_NONE );
422 }
423}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100424#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200425
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200426static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
427 size_t bits,
428 struct raw_data *raw )
429{
430 /* Check that the bit size is acceptable for the key type */
431 switch( type )
432 {
433 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200434 if( bits == 0 )
435 {
436 raw->bytes = 0;
437 raw->data = NULL;
438 return( PSA_SUCCESS );
439 }
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_MD_C)
442 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200443#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200444 case PSA_KEY_TYPE_DERIVE:
445 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446#if defined(MBEDTLS_AES_C)
447 case PSA_KEY_TYPE_AES:
448 if( bits != 128 && bits != 192 && bits != 256 )
449 return( PSA_ERROR_INVALID_ARGUMENT );
450 break;
451#endif
452#if defined(MBEDTLS_CAMELLIA_C)
453 case PSA_KEY_TYPE_CAMELLIA:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_DES_C)
459 case PSA_KEY_TYPE_DES:
460 if( bits != 64 && bits != 128 && bits != 192 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_ARC4_C)
465 case PSA_KEY_TYPE_ARC4:
466 if( bits < 8 || bits > 2048 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470 default:
471 return( PSA_ERROR_NOT_SUPPORTED );
472 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200473 if( bits % 8 != 0 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200475
476 /* Allocate memory for the key */
477 raw->bytes = PSA_BITS_TO_BYTES( bits );
478 raw->data = mbedtls_calloc( 1, raw->bytes );
479 if( raw->data == NULL )
480 {
481 raw->bytes = 0;
482 return( PSA_ERROR_INSUFFICIENT_MEMORY );
483 }
484 return( PSA_SUCCESS );
485}
486
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200487#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100488/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
489 * that are not a multiple of 8) well. For example, there is only
490 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
491 * way to return the exact bit size of a key.
492 * To keep things simple, reject non-byte-aligned key sizes. */
493static psa_status_t psa_check_rsa_key_byte_aligned(
494 const mbedtls_rsa_context *rsa )
495{
496 mbedtls_mpi n;
497 psa_status_t status;
498 mbedtls_mpi_init( &n );
499 status = mbedtls_to_psa_error(
500 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
501 if( status == PSA_SUCCESS )
502 {
503 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
504 status = PSA_ERROR_NOT_SUPPORTED;
505 }
506 mbedtls_mpi_free( &n );
507 return( status );
508}
509
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000510static psa_status_t psa_import_rsa_key( psa_key_type_t type,
511 const uint8_t *data,
512 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200513 mbedtls_rsa_context **p_rsa )
514{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000515 psa_status_t status;
516 mbedtls_pk_context pk;
517 mbedtls_rsa_context *rsa;
518 size_t bits;
519
520 mbedtls_pk_init( &pk );
521
522 /* Parse the data. */
523 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
524 status = mbedtls_to_psa_error(
525 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200526 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000527 status = mbedtls_to_psa_error(
528 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
529 if( status != PSA_SUCCESS )
530 goto exit;
531
532 /* We have something that the pkparse module recognizes. If it is a
533 * valid RSA key, store it. */
534 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 status = PSA_ERROR_INVALID_ARGUMENT;
537 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000539
540 rsa = mbedtls_pk_rsa( pk );
541 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
542 * supports non-byte-aligned key sizes, but not well. For example,
543 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
544 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
545 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
546 {
547 status = PSA_ERROR_NOT_SUPPORTED;
548 goto exit;
549 }
550 status = psa_check_rsa_key_byte_aligned( rsa );
551
552exit:
553 /* Free the content of the pk object only on error. */
554 if( status != PSA_SUCCESS )
555 {
556 mbedtls_pk_free( &pk );
557 return( status );
558 }
559
560 /* On success, store the content of the object in the RSA context. */
561 *p_rsa = rsa;
562
563 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200564}
565#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
566
Jaeden Ameroccdce902019-01-10 11:42:27 +0000567#if defined(MBEDTLS_ECP_C)
568
569/* Import a public key given as the uncompressed representation defined by SEC1
570 * 2.3.3 as the content of an ECPoint. */
571static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
572 const uint8_t *data,
573 size_t data_length,
574 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200575{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000576 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
577 mbedtls_ecp_keypair *ecp = NULL;
578 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
579
580 *p_ecp = NULL;
581 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
582 if( ecp == NULL )
583 return( PSA_ERROR_INSUFFICIENT_MEMORY );
584 mbedtls_ecp_keypair_init( ecp );
585
586 /* Load the group. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
589 if( status != PSA_SUCCESS )
590 goto exit;
591 /* Load the public value. */
592 status = mbedtls_to_psa_error(
593 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
594 data, data_length ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597
598 /* Check that the point is on the curve. */
599 status = mbedtls_to_psa_error(
600 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603
604 *p_ecp = ecp;
605 return( PSA_SUCCESS );
606
607exit:
608 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000610 mbedtls_ecp_keypair_free( ecp );
611 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200612 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000613 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200614}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000615#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200616
Gilles Peskinef76aa772018-10-29 19:24:33 +0100617#if defined(MBEDTLS_ECP_C)
618/* Import a private key given as a byte string which is the private value
619 * in big-endian order. */
620static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
621 const uint8_t *data,
622 size_t data_length,
623 mbedtls_ecp_keypair **p_ecp )
624{
625 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
626 mbedtls_ecp_keypair *ecp = NULL;
627 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
628
629 *p_ecp = NULL;
630 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
631 if( ecp == NULL )
632 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000633 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100634
635 /* Load the group. */
636 status = mbedtls_to_psa_error(
637 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Load the secret value. */
641 status = mbedtls_to_psa_error(
642 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Validate the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
648 if( status != PSA_SUCCESS )
649 goto exit;
650 /* Calculate the public key from the private key. */
651 status = mbedtls_to_psa_error(
652 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
653 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
654 if( status != PSA_SUCCESS )
655 goto exit;
656
657 *p_ecp = ecp;
658 return( PSA_SUCCESS );
659
660exit:
661 if( ecp != NULL )
662 {
663 mbedtls_ecp_keypair_free( ecp );
664 mbedtls_free( ecp );
665 }
666 return( status );
667}
668#endif /* defined(MBEDTLS_ECP_C) */
669
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100670/** Import key data into a slot. `slot->type` must have been set
671 * previously. This function assumes that the slot does not contain
672 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100673psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
674 const uint8_t *data,
675 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200677 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
Darryl Green940d72c2018-07-13 13:18:51 +0100679 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100681 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 if( data_length > SIZE_MAX / 8 )
683 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100684 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200685 PSA_BYTES_TO_BITS( data_length ),
686 &slot->data.raw );
687 if( status != PSA_SUCCESS )
688 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200689 if( data_length != 0 )
690 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 }
692 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100693#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100694 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100695 {
Darryl Green940d72c2018-07-13 13:18:51 +0100696 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100697 data, data_length,
698 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100699 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000700 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000702 status = psa_import_ec_public_key(
703 PSA_KEY_TYPE_GET_CURVE( slot->type ),
704 data, data_length,
705 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 }
707 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100708#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000709#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
710 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000712 status = psa_import_rsa_key( slot->type,
713 data, data_length,
714 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 }
716 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000717#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 {
719 return( PSA_ERROR_NOT_SUPPORTED );
720 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000721 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100722}
723
Darryl Green06fd18d2018-07-16 11:21:11 +0100724/* Retrieve an empty key slot (slot with no key data, but possibly
725 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100726static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100727 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100728{
729 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100730 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100731
732 *p_slot = NULL;
733
Gilles Peskinec5487a82018-12-03 18:08:14 +0100734 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735 if( status != PSA_SUCCESS )
736 return( status );
737
738 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200739 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100740
741 *p_slot = slot;
742 return( status );
743}
744
Gilles Peskinef603c712019-01-19 13:40:11 +0100745/** Calculate the intersection of two algorithm usage policies.
746 *
747 * Return 0 (which allows no operation) on incompatibility.
748 */
749static psa_algorithm_t psa_key_policy_algorithm_intersection(
750 psa_algorithm_t alg1,
751 psa_algorithm_t alg2 )
752{
753 /* Common case: the policy only allows alg. */
754 if( alg1 == alg2 )
755 return( alg1 );
756 /* If the policies are from the same hash-and-sign family, check
757 * if one is a wildcard. If so the other has the specific algorithm. */
758 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
759 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
760 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
761 {
762 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
763 return( alg2 );
764 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
765 return( alg1 );
766 }
767 /* If the policies are incompatible, allow nothing. */
768 return( 0 );
769}
770
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100771/** Test whether a policy permits an algorithm.
772 *
773 * The caller must test usage flags separately.
774 */
775static int psa_key_policy_permits( const psa_key_policy_t *policy,
776 psa_algorithm_t alg )
777{
778 /* Common case: the policy only allows alg. */
779 if( alg == policy->alg )
780 return( 1 );
781 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
782 * and alg is the same hash-and-sign family with any hash,
783 * then alg is compliant with policy->alg. */
784 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
785 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
786 {
787 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
788 ( alg & ~PSA_ALG_HASH_MASK ) );
789 }
790 /* If it isn't permitted, it's forbidden. */
791 return( 0 );
792}
793
Gilles Peskinef603c712019-01-19 13:40:11 +0100794/** Restrict a key policy based on a constraint.
795 *
796 * \param[in,out] policy The policy to restrict.
797 * \param[in] constraint The policy constraint to apply.
798 *
799 * \retval #PSA_SUCCESS
800 * \c *policy contains the intersection of the original value of
801 * \c *policy and \c *constraint.
802 * \retval #PSA_ERROR_INVALID_ARGUMENT
803 * \c *policy and \c *constraint are incompatible.
804 * \c *policy is unchanged.
805 */
806static psa_status_t psa_restrict_key_policy(
807 psa_key_policy_t *policy,
808 const psa_key_policy_t *constraint )
809{
810 psa_algorithm_t intersection_alg =
811 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
812 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
813 return( PSA_ERROR_INVALID_ARGUMENT );
814 policy->usage &= constraint->usage;
815 policy->alg = intersection_alg;
816 return( PSA_SUCCESS );
817}
818
Darryl Green06fd18d2018-07-16 11:21:11 +0100819/** Retrieve a slot which must contain a key. The key must have allow all the
820 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
821 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100822static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100823 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100824 psa_key_usage_t usage,
825 psa_algorithm_t alg )
826{
827 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100828 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100829
830 *p_slot = NULL;
831
Gilles Peskinec5487a82018-12-03 18:08:14 +0100832 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100833 if( status != PSA_SUCCESS )
834 return( status );
835 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200836 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100837
838 /* Enforce that usage policy for the key slot contains all the flags
839 * required by the usage parameter. There is one exception: public
840 * keys can always be exported, so we treat public key objects as
841 * if they had the export flag. */
842 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
843 usage &= ~PSA_KEY_USAGE_EXPORT;
844 if( ( slot->policy.usage & usage ) != usage )
845 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100846
847 /* Enforce that the usage policy permits the requested algortihm. */
848 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100849 return( PSA_ERROR_NOT_PERMITTED );
850
851 *p_slot = slot;
852 return( PSA_SUCCESS );
853}
Darryl Green940d72c2018-07-13 13:18:51 +0100854
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100855/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100856static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000857{
858 if( slot->type == PSA_KEY_TYPE_NONE )
859 {
860 /* No key material to clean. */
861 }
862 else if( key_type_is_raw_bytes( slot->type ) )
863 {
864 mbedtls_free( slot->data.raw.data );
865 }
866 else
867#if defined(MBEDTLS_RSA_C)
868 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
869 {
870 mbedtls_rsa_free( slot->data.rsa );
871 mbedtls_free( slot->data.rsa );
872 }
873 else
874#endif /* defined(MBEDTLS_RSA_C) */
875#if defined(MBEDTLS_ECP_C)
876 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
877 {
878 mbedtls_ecp_keypair_free( slot->data.ecp );
879 mbedtls_free( slot->data.ecp );
880 }
881 else
882#endif /* defined(MBEDTLS_ECP_C) */
883 {
884 /* Shouldn't happen: the key type is not any type that we
885 * put in. */
886 return( PSA_ERROR_TAMPERING_DETECTED );
887 }
888
889 return( PSA_SUCCESS );
890}
891
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100892/** Completely wipe a slot in memory, including its policy.
893 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100894psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100895{
896 psa_status_t status = psa_remove_key_data_from_memory( slot );
897 /* At this point, key material and other type-specific content has
898 * been wiped. Clear remaining metadata. We can call memset and not
899 * zeroize because the metadata is not particularly sensitive. */
900 memset( slot, 0, sizeof( *slot ) );
901 return( status );
902}
903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100905 psa_key_type_t type,
906 const uint8_t *data,
907 size_t data_length )
908{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100909 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100910 psa_status_t status;
911
Gilles Peskinec5487a82018-12-03 18:08:14 +0100912 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100913 if( status != PSA_SUCCESS )
914 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100915
916 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100917
918 status = psa_import_key_into_slot( slot, data, data_length );
919 if( status != PSA_SUCCESS )
920 {
921 slot->type = PSA_KEY_TYPE_NONE;
922 return( status );
923 }
924
Darryl Greend49a4992018-06-18 17:27:26 +0100925#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
926 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
927 {
928 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100929 status = psa_save_persistent_key( slot->persistent_storage_id,
930 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100931 data_length );
932 if( status != PSA_SUCCESS )
933 {
934 (void) psa_remove_key_data_from_memory( slot );
935 slot->type = PSA_KEY_TYPE_NONE;
936 }
937 }
938#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
939
940 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941}
942
Gilles Peskinec5487a82018-12-03 18:08:14 +0100943psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100945 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100946 psa_status_t status = PSA_SUCCESS;
947 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948
Gilles Peskinec5487a82018-12-03 18:08:14 +0100949 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( status != PSA_SUCCESS )
951 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100952#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
953 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
954 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100955 storage_status =
956 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100957 }
958#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100959 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100960 if( status != PSA_SUCCESS )
961 return( status );
962 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100963}
964
Gilles Peskineb870b182018-07-06 16:02:09 +0200965/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100966static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200967{
968 if( key_type_is_raw_bytes( slot->type ) )
969 return( slot->data.raw.bytes * 8 );
970#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200971 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100972 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200973#endif /* defined(MBEDTLS_RSA_C) */
974#if defined(MBEDTLS_ECP_C)
975 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
976 return( slot->data.ecp->grp.pbits );
977#endif /* defined(MBEDTLS_ECP_C) */
978 /* Shouldn't happen except on an empty slot. */
979 return( 0 );
980}
981
Gilles Peskinec5487a82018-12-03 18:08:14 +0100982psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200983 psa_key_type_t *type,
984 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100986 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200987 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200990 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( bits != NULL )
992 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100993 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200994 if( status != PSA_SUCCESS )
995 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200996
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200998 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200999 if( type != NULL )
1000 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001001 if( bits != NULL )
1002 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 return( PSA_SUCCESS );
1004}
1005
Jaeden Ameroccdce902019-01-10 11:42:27 +00001006#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001007static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1008 unsigned char *buf, size_t size )
1009{
1010 int ret;
1011 unsigned char *c;
1012 size_t len = 0;
1013
1014 c = buf + size;
1015
1016 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1017
1018 return( (int) len );
1019}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001020#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001021
Gilles Peskinef603c712019-01-19 13:40:11 +01001022static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1023 uint8_t *data,
1024 size_t data_size,
1025 size_t *data_length,
1026 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001028 *data_length = 0;
1029
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001030 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001031 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001032
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001033 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 {
1035 if( slot->data.raw.bytes > data_size )
1036 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001037 if( data_size != 0 )
1038 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001039 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001040 memset( data + slot->data.raw.bytes, 0,
1041 data_size - slot->data.raw.bytes );
1042 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043 *data_length = slot->data.raw.bytes;
1044 return( PSA_SUCCESS );
1045 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001046#if defined(MBEDTLS_ECP_C)
1047 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1048 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001049 psa_status_t status;
1050
Gilles Peskine188c71e2018-10-29 19:26:02 +01001051 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1052 if( bytes > data_size )
1053 return( PSA_ERROR_BUFFER_TOO_SMALL );
1054 status = mbedtls_to_psa_error(
1055 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1056 if( status != PSA_SUCCESS )
1057 return( status );
1058 memset( data + bytes, 0, data_size - bytes );
1059 *data_length = bytes;
1060 return( PSA_SUCCESS );
1061 }
1062#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063 else
Moran Peker17e36e12018-05-02 12:55:20 +03001064 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001065#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001066 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001067 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001068 {
Moran Pekera998bc62018-04-16 18:16:20 +03001069 mbedtls_pk_context pk;
1070 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001071 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001072 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001073#if defined(MBEDTLS_RSA_C)
1074 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001075 pk.pk_info = &mbedtls_rsa_info;
1076 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001077#else
1078 return( PSA_ERROR_NOT_SUPPORTED );
1079#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001080 }
1081 else
1082 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001083#if defined(MBEDTLS_ECP_C)
1084 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001085 pk.pk_info = &mbedtls_eckey_info;
1086 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001087#else
1088 return( PSA_ERROR_NOT_SUPPORTED );
1089#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001090 }
Moran Pekerd7326592018-05-29 16:56:39 +03001091 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001092 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001093 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001094 }
Moran Peker17e36e12018-05-02 12:55:20 +03001095 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001096 {
Moran Peker17e36e12018-05-02 12:55:20 +03001097 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001098 }
Moran Peker60364322018-04-29 11:34:58 +03001099 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001100 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001101 /* If data_size is 0 then data may be NULL and then the
1102 * call to memset would have undefined behavior. */
1103 if( data_size != 0 )
1104 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001105 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001106 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001107 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1108 * Move the data to the beginning and erase remaining data
1109 * at the original location. */
1110 if( 2 * (size_t) ret <= data_size )
1111 {
1112 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001113 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001114 }
1115 else if( (size_t) ret < data_size )
1116 {
1117 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001118 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001119 }
Moran Pekera998bc62018-04-16 18:16:20 +03001120 *data_length = ret;
1121 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001122 }
1123 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001124#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001125 {
1126 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001127 it is valid for a special-purpose implementation to omit
1128 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001129 return( PSA_ERROR_NOT_SUPPORTED );
1130 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001131 }
1132}
1133
Gilles Peskinec5487a82018-12-03 18:08:14 +01001134psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001135 uint8_t *data,
1136 size_t data_size,
1137 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001138{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001139 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001140 psa_status_t status;
1141
1142 /* Set the key to empty now, so that even when there are errors, we always
1143 * set data_length to a value between 0 and data_size. On error, setting
1144 * the key to empty is a good choice because an empty key representation is
1145 * unlikely to be accepted anywhere. */
1146 *data_length = 0;
1147
1148 /* Export requires the EXPORT flag. There is an exception for public keys,
1149 * which don't require any flag, but psa_get_key_from_slot takes
1150 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001151 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001152 if( status != PSA_SUCCESS )
1153 return( status );
1154 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001155 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001157
Gilles Peskinec5487a82018-12-03 18:08:14 +01001158psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001159 uint8_t *data,
1160 size_t data_size,
1161 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001162{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001163 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001164 psa_status_t status;
1165
1166 /* Set the key to empty now, so that even when there are errors, we always
1167 * set data_length to a value between 0 and data_size. On error, setting
1168 * the key to empty is a good choice because an empty key representation is
1169 * unlikely to be accepted anywhere. */
1170 *data_length = 0;
1171
1172 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001173 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001174 if( status != PSA_SUCCESS )
1175 return( status );
1176 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001177 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001178}
1179
Darryl Green0c6575a2018-11-07 16:05:30 +00001180#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001181static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001182 size_t bits )
1183{
1184 psa_status_t status;
1185 uint8_t *data;
1186 size_t key_length;
1187 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1188 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001189 if( data == NULL )
1190 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001191 /* Get key data in export format */
1192 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1193 if( status != PSA_SUCCESS )
1194 {
1195 slot->type = PSA_KEY_TYPE_NONE;
1196 goto exit;
1197 }
1198 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001199 status = psa_save_persistent_key( slot->persistent_storage_id,
1200 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001201 data, key_length );
1202 if( status != PSA_SUCCESS )
1203 {
1204 slot->type = PSA_KEY_TYPE_NONE;
1205 }
1206exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001207 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001208 mbedtls_free( data );
1209 return( status );
1210}
1211#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1212
Gilles Peskinef603c712019-01-19 13:40:11 +01001213static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1214 psa_key_handle_t target )
1215{
1216 psa_status_t status;
1217 uint8_t *buffer = NULL;
1218 size_t buffer_size = 0;
1219 size_t length;
1220
1221 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1222 psa_get_key_bits( source ) );
1223 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001224 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001225 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001226 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1227 if( status != PSA_SUCCESS )
1228 goto exit;
1229 status = psa_import_key( target, source->type, buffer, length );
1230
1231exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001232 if( buffer_size != 0 )
1233 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001234 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001235 return( status );
1236}
1237
1238psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1239 psa_key_handle_t target_handle,
1240 const psa_key_policy_t *constraint)
1241{
1242 psa_key_slot_t *source_slot = NULL;
1243 psa_key_slot_t *target_slot = NULL;
1244 psa_key_policy_t new_policy;
1245 psa_status_t status;
1246 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1247 if( status != PSA_SUCCESS )
1248 return( status );
1249 status = psa_get_empty_key_slot( target_handle, &target_slot );
1250 if( status != PSA_SUCCESS )
1251 return( status );
1252
1253 new_policy = target_slot->policy;
1254 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1255 if( status != PSA_SUCCESS )
1256 return( status );
1257 if( constraint != NULL )
1258 {
1259 status = psa_restrict_key_policy( &new_policy, constraint );
1260 if( status != PSA_SUCCESS )
1261 return( status );
1262 }
1263
1264 status = psa_copy_key_material( source_slot, target_handle );
1265 if( status != PSA_SUCCESS )
1266 return( status );
1267
1268 target_slot->policy = new_policy;
1269 return( PSA_SUCCESS );
1270}
1271
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001272
1273
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001275/* Message digests */
1276/****************************************************************/
1277
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001278static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001279{
1280 switch( alg )
1281 {
1282#if defined(MBEDTLS_MD2_C)
1283 case PSA_ALG_MD2:
1284 return( &mbedtls_md2_info );
1285#endif
1286#if defined(MBEDTLS_MD4_C)
1287 case PSA_ALG_MD4:
1288 return( &mbedtls_md4_info );
1289#endif
1290#if defined(MBEDTLS_MD5_C)
1291 case PSA_ALG_MD5:
1292 return( &mbedtls_md5_info );
1293#endif
1294#if defined(MBEDTLS_RIPEMD160_C)
1295 case PSA_ALG_RIPEMD160:
1296 return( &mbedtls_ripemd160_info );
1297#endif
1298#if defined(MBEDTLS_SHA1_C)
1299 case PSA_ALG_SHA_1:
1300 return( &mbedtls_sha1_info );
1301#endif
1302#if defined(MBEDTLS_SHA256_C)
1303 case PSA_ALG_SHA_224:
1304 return( &mbedtls_sha224_info );
1305 case PSA_ALG_SHA_256:
1306 return( &mbedtls_sha256_info );
1307#endif
1308#if defined(MBEDTLS_SHA512_C)
1309 case PSA_ALG_SHA_384:
1310 return( &mbedtls_sha384_info );
1311 case PSA_ALG_SHA_512:
1312 return( &mbedtls_sha512_info );
1313#endif
1314 default:
1315 return( NULL );
1316 }
1317}
1318
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001319psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1320{
1321 switch( operation->alg )
1322 {
Gilles Peskine81736312018-06-26 15:04:31 +02001323 case 0:
1324 /* The object has (apparently) been initialized but it is not
1325 * in use. It's ok to call abort on such an object, and there's
1326 * nothing to do. */
1327 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001328#if defined(MBEDTLS_MD2_C)
1329 case PSA_ALG_MD2:
1330 mbedtls_md2_free( &operation->ctx.md2 );
1331 break;
1332#endif
1333#if defined(MBEDTLS_MD4_C)
1334 case PSA_ALG_MD4:
1335 mbedtls_md4_free( &operation->ctx.md4 );
1336 break;
1337#endif
1338#if defined(MBEDTLS_MD5_C)
1339 case PSA_ALG_MD5:
1340 mbedtls_md5_free( &operation->ctx.md5 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_RIPEMD160_C)
1344 case PSA_ALG_RIPEMD160:
1345 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1346 break;
1347#endif
1348#if defined(MBEDTLS_SHA1_C)
1349 case PSA_ALG_SHA_1:
1350 mbedtls_sha1_free( &operation->ctx.sha1 );
1351 break;
1352#endif
1353#if defined(MBEDTLS_SHA256_C)
1354 case PSA_ALG_SHA_224:
1355 case PSA_ALG_SHA_256:
1356 mbedtls_sha256_free( &operation->ctx.sha256 );
1357 break;
1358#endif
1359#if defined(MBEDTLS_SHA512_C)
1360 case PSA_ALG_SHA_384:
1361 case PSA_ALG_SHA_512:
1362 mbedtls_sha512_free( &operation->ctx.sha512 );
1363 break;
1364#endif
1365 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001366 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001367 }
1368 operation->alg = 0;
1369 return( PSA_SUCCESS );
1370}
1371
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001372psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001373 psa_algorithm_t alg )
1374{
1375 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001376
1377 /* A context must be freshly initialized before it can be set up. */
1378 if( operation->alg != 0 )
1379 {
1380 return( PSA_ERROR_BAD_STATE );
1381 }
1382
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001383 switch( alg )
1384 {
1385#if defined(MBEDTLS_MD2_C)
1386 case PSA_ALG_MD2:
1387 mbedtls_md2_init( &operation->ctx.md2 );
1388 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1389 break;
1390#endif
1391#if defined(MBEDTLS_MD4_C)
1392 case PSA_ALG_MD4:
1393 mbedtls_md4_init( &operation->ctx.md4 );
1394 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1395 break;
1396#endif
1397#if defined(MBEDTLS_MD5_C)
1398 case PSA_ALG_MD5:
1399 mbedtls_md5_init( &operation->ctx.md5 );
1400 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1401 break;
1402#endif
1403#if defined(MBEDTLS_RIPEMD160_C)
1404 case PSA_ALG_RIPEMD160:
1405 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1406 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1407 break;
1408#endif
1409#if defined(MBEDTLS_SHA1_C)
1410 case PSA_ALG_SHA_1:
1411 mbedtls_sha1_init( &operation->ctx.sha1 );
1412 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1413 break;
1414#endif
1415#if defined(MBEDTLS_SHA256_C)
1416 case PSA_ALG_SHA_224:
1417 mbedtls_sha256_init( &operation->ctx.sha256 );
1418 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1419 break;
1420 case PSA_ALG_SHA_256:
1421 mbedtls_sha256_init( &operation->ctx.sha256 );
1422 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1423 break;
1424#endif
1425#if defined(MBEDTLS_SHA512_C)
1426 case PSA_ALG_SHA_384:
1427 mbedtls_sha512_init( &operation->ctx.sha512 );
1428 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1429 break;
1430 case PSA_ALG_SHA_512:
1431 mbedtls_sha512_init( &operation->ctx.sha512 );
1432 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1433 break;
1434#endif
1435 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001436 return( PSA_ALG_IS_HASH( alg ) ?
1437 PSA_ERROR_NOT_SUPPORTED :
1438 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001439 }
1440 if( ret == 0 )
1441 operation->alg = alg;
1442 else
1443 psa_hash_abort( operation );
1444 return( mbedtls_to_psa_error( ret ) );
1445}
1446
1447psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1448 const uint8_t *input,
1449 size_t input_length )
1450{
1451 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001452
1453 /* Don't require hash implementations to behave correctly on a
1454 * zero-length input, which may have an invalid pointer. */
1455 if( input_length == 0 )
1456 return( PSA_SUCCESS );
1457
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001458 switch( operation->alg )
1459 {
1460#if defined(MBEDTLS_MD2_C)
1461 case PSA_ALG_MD2:
1462 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1463 input, input_length );
1464 break;
1465#endif
1466#if defined(MBEDTLS_MD4_C)
1467 case PSA_ALG_MD4:
1468 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1469 input, input_length );
1470 break;
1471#endif
1472#if defined(MBEDTLS_MD5_C)
1473 case PSA_ALG_MD5:
1474 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1475 input, input_length );
1476 break;
1477#endif
1478#if defined(MBEDTLS_RIPEMD160_C)
1479 case PSA_ALG_RIPEMD160:
1480 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1481 input, input_length );
1482 break;
1483#endif
1484#if defined(MBEDTLS_SHA1_C)
1485 case PSA_ALG_SHA_1:
1486 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1487 input, input_length );
1488 break;
1489#endif
1490#if defined(MBEDTLS_SHA256_C)
1491 case PSA_ALG_SHA_224:
1492 case PSA_ALG_SHA_256:
1493 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1494 input, input_length );
1495 break;
1496#endif
1497#if defined(MBEDTLS_SHA512_C)
1498 case PSA_ALG_SHA_384:
1499 case PSA_ALG_SHA_512:
1500 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1501 input, input_length );
1502 break;
1503#endif
1504 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001505 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001506 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001507
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001508 if( ret != 0 )
1509 psa_hash_abort( operation );
1510 return( mbedtls_to_psa_error( ret ) );
1511}
1512
1513psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1514 uint8_t *hash,
1515 size_t hash_size,
1516 size_t *hash_length )
1517{
itayzafrir40835d42018-08-02 13:14:17 +03001518 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001519 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001520 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001521
1522 /* Fill the output buffer with something that isn't a valid hash
1523 * (barring an attack on the hash and deliberately-crafted input),
1524 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001525 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001526 /* If hash_size is 0 then hash may be NULL and then the
1527 * call to memset would have undefined behavior. */
1528 if( hash_size != 0 )
1529 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001530
1531 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001532 {
1533 status = PSA_ERROR_BUFFER_TOO_SMALL;
1534 goto exit;
1535 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001536
1537 switch( operation->alg )
1538 {
1539#if defined(MBEDTLS_MD2_C)
1540 case PSA_ALG_MD2:
1541 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1542 break;
1543#endif
1544#if defined(MBEDTLS_MD4_C)
1545 case PSA_ALG_MD4:
1546 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1547 break;
1548#endif
1549#if defined(MBEDTLS_MD5_C)
1550 case PSA_ALG_MD5:
1551 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1552 break;
1553#endif
1554#if defined(MBEDTLS_RIPEMD160_C)
1555 case PSA_ALG_RIPEMD160:
1556 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1557 break;
1558#endif
1559#if defined(MBEDTLS_SHA1_C)
1560 case PSA_ALG_SHA_1:
1561 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1562 break;
1563#endif
1564#if defined(MBEDTLS_SHA256_C)
1565 case PSA_ALG_SHA_224:
1566 case PSA_ALG_SHA_256:
1567 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1568 break;
1569#endif
1570#if defined(MBEDTLS_SHA512_C)
1571 case PSA_ALG_SHA_384:
1572 case PSA_ALG_SHA_512:
1573 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1574 break;
1575#endif
1576 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001577 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 }
itayzafrir40835d42018-08-02 13:14:17 +03001579 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580
itayzafrir40835d42018-08-02 13:14:17 +03001581exit:
1582 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001583 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001584 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 return( psa_hash_abort( operation ) );
1586 }
1587 else
1588 {
1589 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001590 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001591 }
1592}
1593
Gilles Peskine2d277862018-06-18 15:41:12 +02001594psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1595 const uint8_t *hash,
1596 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001597{
1598 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1599 size_t actual_hash_length;
1600 psa_status_t status = psa_hash_finish( operation,
1601 actual_hash, sizeof( actual_hash ),
1602 &actual_hash_length );
1603 if( status != PSA_SUCCESS )
1604 return( status );
1605 if( actual_hash_length != hash_length )
1606 return( PSA_ERROR_INVALID_SIGNATURE );
1607 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1608 return( PSA_ERROR_INVALID_SIGNATURE );
1609 return( PSA_SUCCESS );
1610}
1611
Gilles Peskineeb35d782019-01-22 17:56:16 +01001612psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1613 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001614{
1615 if( target_operation->alg != 0 )
1616 return( PSA_ERROR_BAD_STATE );
1617
1618 switch( source_operation->alg )
1619 {
1620 case 0:
1621 return( PSA_ERROR_BAD_STATE );
1622#if defined(MBEDTLS_MD2_C)
1623 case PSA_ALG_MD2:
1624 mbedtls_md2_clone( &target_operation->ctx.md2,
1625 &source_operation->ctx.md2 );
1626 break;
1627#endif
1628#if defined(MBEDTLS_MD4_C)
1629 case PSA_ALG_MD4:
1630 mbedtls_md4_clone( &target_operation->ctx.md4,
1631 &source_operation->ctx.md4 );
1632 break;
1633#endif
1634#if defined(MBEDTLS_MD5_C)
1635 case PSA_ALG_MD5:
1636 mbedtls_md5_clone( &target_operation->ctx.md5,
1637 &source_operation->ctx.md5 );
1638 break;
1639#endif
1640#if defined(MBEDTLS_RIPEMD160_C)
1641 case PSA_ALG_RIPEMD160:
1642 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1643 &source_operation->ctx.ripemd160 );
1644 break;
1645#endif
1646#if defined(MBEDTLS_SHA1_C)
1647 case PSA_ALG_SHA_1:
1648 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1649 &source_operation->ctx.sha1 );
1650 break;
1651#endif
1652#if defined(MBEDTLS_SHA256_C)
1653 case PSA_ALG_SHA_224:
1654 case PSA_ALG_SHA_256:
1655 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1656 &source_operation->ctx.sha256 );
1657 break;
1658#endif
1659#if defined(MBEDTLS_SHA512_C)
1660 case PSA_ALG_SHA_384:
1661 case PSA_ALG_SHA_512:
1662 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1663 &source_operation->ctx.sha512 );
1664 break;
1665#endif
1666 default:
1667 return( PSA_ERROR_NOT_SUPPORTED );
1668 }
1669
1670 target_operation->alg = source_operation->alg;
1671 return( PSA_SUCCESS );
1672}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001673
1674
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001675/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676/* MAC */
1677/****************************************************************/
1678
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001679static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680 psa_algorithm_t alg,
1681 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001682 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001683 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001686 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001688 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001689 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001690
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1692 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001693 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001695 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 mode = MBEDTLS_MODE_STREAM;
1697 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 case PSA_ALG_CTR:
1699 mode = MBEDTLS_MODE_CTR;
1700 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001701 case PSA_ALG_CFB:
1702 mode = MBEDTLS_MODE_CFB;
1703 break;
1704 case PSA_ALG_OFB:
1705 mode = MBEDTLS_MODE_OFB;
1706 break;
1707 case PSA_ALG_CBC_NO_PADDING:
1708 mode = MBEDTLS_MODE_CBC;
1709 break;
1710 case PSA_ALG_CBC_PKCS7:
1711 mode = MBEDTLS_MODE_CBC;
1712 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001713 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001714 mode = MBEDTLS_MODE_CCM;
1715 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001716 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001717 mode = MBEDTLS_MODE_GCM;
1718 break;
1719 default:
1720 return( NULL );
1721 }
1722 }
1723 else if( alg == PSA_ALG_CMAC )
1724 mode = MBEDTLS_MODE_ECB;
1725 else if( alg == PSA_ALG_GMAC )
1726 mode = MBEDTLS_MODE_GCM;
1727 else
1728 return( NULL );
1729
1730 switch( key_type )
1731 {
1732 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001733 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001734 break;
1735 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001736 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1737 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001738 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001739 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001740 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001741 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001742 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1743 * but two-key Triple-DES is functionally three-key Triple-DES
1744 * with K1=K3, so that's how we present it to mbedtls. */
1745 if( key_bits == 128 )
1746 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747 break;
1748 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001749 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 break;
1751 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001752 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001753 break;
1754 default:
1755 return( NULL );
1756 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001757 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001758 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001759
Jaeden Amero23bbb752018-06-26 14:16:54 +01001760 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1761 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001762}
1763
Gilles Peskinea05219c2018-11-16 16:02:56 +01001764#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001765static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001766{
Gilles Peskine2d277862018-06-18 15:41:12 +02001767 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001768 {
1769 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001770 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001771 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001774 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001775 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001776 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001779 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001783 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001784 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001786 return( 128 );
1787 default:
1788 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001789 }
1790}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001791#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001792
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001793/* Initialize the MAC operation structure. Once this function has been
1794 * called, psa_mac_abort can run and will do the right thing. */
1795static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1796 psa_algorithm_t alg )
1797{
1798 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1799
1800 operation->alg = alg;
1801 operation->key_set = 0;
1802 operation->iv_set = 0;
1803 operation->iv_required = 0;
1804 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001805 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001806
1807#if defined(MBEDTLS_CMAC_C)
1808 if( alg == PSA_ALG_CMAC )
1809 {
1810 operation->iv_required = 0;
1811 mbedtls_cipher_init( &operation->ctx.cmac );
1812 status = PSA_SUCCESS;
1813 }
1814 else
1815#endif /* MBEDTLS_CMAC_C */
1816#if defined(MBEDTLS_MD_C)
1817 if( PSA_ALG_IS_HMAC( operation->alg ) )
1818 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001819 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1820 operation->ctx.hmac.hash_ctx.alg = 0;
1821 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001822 }
1823 else
1824#endif /* MBEDTLS_MD_C */
1825 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001826 if( ! PSA_ALG_IS_MAC( alg ) )
1827 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828 }
1829
1830 if( status != PSA_SUCCESS )
1831 memset( operation, 0, sizeof( *operation ) );
1832 return( status );
1833}
1834
Gilles Peskine01126fa2018-07-12 17:04:55 +02001835#if defined(MBEDTLS_MD_C)
1836static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1837{
Gilles Peskine3f108122018-12-07 18:14:53 +01001838 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001839 return( psa_hash_abort( &hmac->hash_ctx ) );
1840}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001841
1842static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1843{
1844 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1845 memset( hmac, 0, sizeof( *hmac ) );
1846}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001847#endif /* MBEDTLS_MD_C */
1848
Gilles Peskine8c9def32018-02-08 10:02:12 +01001849psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1850{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001851 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001852 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001853 /* The object has (apparently) been initialized but it is not
1854 * in use. It's ok to call abort on such an object, and there's
1855 * nothing to do. */
1856 return( PSA_SUCCESS );
1857 }
1858 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001859#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001860 if( operation->alg == PSA_ALG_CMAC )
1861 {
1862 mbedtls_cipher_free( &operation->ctx.cmac );
1863 }
1864 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001866#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001867 if( PSA_ALG_IS_HMAC( operation->alg ) )
1868 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001869 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001870 }
1871 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001873 {
1874 /* Sanity check (shouldn't happen: operation->alg should
1875 * always have been initialized to a valid value). */
1876 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001877 }
Moran Peker41deec42018-04-04 15:43:05 +03001878
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879 operation->alg = 0;
1880 operation->key_set = 0;
1881 operation->iv_set = 0;
1882 operation->iv_required = 0;
1883 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001884 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001885
Gilles Peskine8c9def32018-02-08 10:02:12 +01001886 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001887
1888bad_state:
1889 /* If abort is called on an uninitialized object, we can't trust
1890 * anything. Wipe the object in case it contains confidential data.
1891 * This may result in a memory leak if a pointer gets overwritten,
1892 * but it's too late to do anything about this. */
1893 memset( operation, 0, sizeof( *operation ) );
1894 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001895}
1896
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001897#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001898static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001899 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001900 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901 const mbedtls_cipher_info_t *cipher_info )
1902{
1903 int ret;
1904
1905 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001906
1907 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1908 if( ret != 0 )
1909 return( ret );
1910
1911 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1912 slot->data.raw.data,
1913 key_bits );
1914 return( ret );
1915}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001916#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001917
Gilles Peskine248051a2018-06-20 16:09:38 +02001918#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001919static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1920 const uint8_t *key,
1921 size_t key_length,
1922 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001923{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001924 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001925 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001926 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001927 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001928 psa_status_t status;
1929
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001930 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1931 * overflow below. This should never trigger if the hash algorithm
1932 * is implemented correctly. */
1933 /* The size checks against the ipad and opad buffers cannot be written
1934 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1935 * because that triggers -Wlogical-op on GCC 7.3. */
1936 if( block_size > sizeof( ipad ) )
1937 return( PSA_ERROR_NOT_SUPPORTED );
1938 if( block_size > sizeof( hmac->opad ) )
1939 return( PSA_ERROR_NOT_SUPPORTED );
1940 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001941 return( PSA_ERROR_NOT_SUPPORTED );
1942
Gilles Peskined223b522018-06-11 18:12:58 +02001943 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001944 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001945 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001946 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001947 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001948 status = psa_hash_update( &hmac->hash_ctx, key, 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 Peskine01126fa2018-07-12 17:04:55 +02001951 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001952 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001953 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001954 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001955 }
Gilles Peskine96889972018-07-12 17:07:03 +02001956 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1957 * but it is permitted. It is common when HMAC is used in HKDF, for
1958 * example. Don't call `memcpy` in the 0-length because `key` could be
1959 * an invalid pointer which would make the behavior undefined. */
1960 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001961 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001962
Gilles Peskined223b522018-06-11 18:12:58 +02001963 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1964 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001965 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001966 ipad[i] ^= 0x36;
1967 memset( ipad + key_length, 0x36, block_size - key_length );
1968
1969 /* Copy the key material from ipad to opad, flipping the requisite bits,
1970 * and filling the rest of opad with the requisite constant. */
1971 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001972 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1973 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001974
Gilles Peskine01126fa2018-07-12 17:04:55 +02001975 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001976 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001977 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001978
Gilles Peskine01126fa2018-07-12 17:04:55 +02001979 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001980
1981cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001982 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001983
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001984 return( status );
1985}
Gilles Peskine248051a2018-06-20 16:09:38 +02001986#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001987
Gilles Peskine89167cb2018-07-08 20:12:23 +02001988static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001989 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001990 psa_algorithm_t alg,
1991 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001992{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001993 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001994 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001996 psa_key_usage_t usage =
1997 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001998 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001999 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002001 /* A context must be freshly initialized before it can be set up. */
2002 if( operation->alg != 0 )
2003 {
2004 return( PSA_ERROR_BAD_STATE );
2005 }
2006
Gilles Peskined911eb72018-08-14 15:18:45 +02002007 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002008 if( status != PSA_SUCCESS )
2009 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002010 if( is_sign )
2011 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002012
Gilles Peskinec5487a82018-12-03 18:08:14 +01002013 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002014 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002015 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002016 key_bits = psa_get_key_bits( slot );
2017
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002019 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020 {
2021 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002022 mbedtls_cipher_info_from_psa( full_length_alg,
2023 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002024 int ret;
2025 if( cipher_info == NULL )
2026 {
2027 status = PSA_ERROR_NOT_SUPPORTED;
2028 goto exit;
2029 }
2030 operation->mac_size = cipher_info->block_size;
2031 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2032 status = mbedtls_to_psa_error( ret );
2033 }
2034 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002036#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002037 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002038 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002039 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002040 if( hash_alg == 0 )
2041 {
2042 status = PSA_ERROR_NOT_SUPPORTED;
2043 goto exit;
2044 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002045
2046 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2047 /* Sanity check. This shouldn't fail on a valid configuration. */
2048 if( operation->mac_size == 0 ||
2049 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2050 {
2051 status = PSA_ERROR_NOT_SUPPORTED;
2052 goto exit;
2053 }
2054
Gilles Peskine01126fa2018-07-12 17:04:55 +02002055 if( slot->type != PSA_KEY_TYPE_HMAC )
2056 {
2057 status = PSA_ERROR_INVALID_ARGUMENT;
2058 goto exit;
2059 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002060
Gilles Peskine01126fa2018-07-12 17:04:55 +02002061 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2062 slot->data.raw.data,
2063 slot->data.raw.bytes,
2064 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002065 }
2066 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002068 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002069 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002070 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002071 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002072
Gilles Peskined911eb72018-08-14 15:18:45 +02002073 if( truncated == 0 )
2074 {
2075 /* The "normal" case: untruncated algorithm. Nothing to do. */
2076 }
2077 else if( truncated < 4 )
2078 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002079 /* A very short MAC is too short for security since it can be
2080 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2081 * so we make this our minimum, even though 32 bits is still
2082 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002083 status = PSA_ERROR_NOT_SUPPORTED;
2084 }
2085 else if( truncated > operation->mac_size )
2086 {
2087 /* It's impossible to "truncate" to a larger length. */
2088 status = PSA_ERROR_INVALID_ARGUMENT;
2089 }
2090 else
2091 operation->mac_size = truncated;
2092
Gilles Peskinefbfac682018-07-08 20:51:54 +02002093exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002094 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002095 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002096 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002097 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002098 else
2099 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002100 operation->key_set = 1;
2101 }
2102 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103}
2104
Gilles Peskine89167cb2018-07-08 20:12:23 +02002105psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002106 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002107 psa_algorithm_t alg )
2108{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002109 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002110}
2111
2112psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002113 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002114 psa_algorithm_t alg )
2115{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002116 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002117}
2118
Gilles Peskine8c9def32018-02-08 10:02:12 +01002119psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2120 const uint8_t *input,
2121 size_t input_length )
2122{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002123 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002124 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002125 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002127 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128 operation->has_input = 1;
2129
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002131 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002132 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002133 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2134 input, input_length );
2135 status = mbedtls_to_psa_error( ret );
2136 }
2137 else
2138#endif /* MBEDTLS_CMAC_C */
2139#if defined(MBEDTLS_MD_C)
2140 if( PSA_ALG_IS_HMAC( operation->alg ) )
2141 {
2142 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2143 input_length );
2144 }
2145 else
2146#endif /* MBEDTLS_MD_C */
2147 {
2148 /* This shouldn't happen if `operation` was initialized by
2149 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002150 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002151 }
2152
Gilles Peskinefbfac682018-07-08 20:51:54 +02002153 if( status != PSA_SUCCESS )
2154 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002155 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156}
2157
Gilles Peskine01126fa2018-07-12 17:04:55 +02002158#if defined(MBEDTLS_MD_C)
2159static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2160 uint8_t *mac,
2161 size_t mac_size )
2162{
2163 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2164 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2165 size_t hash_size = 0;
2166 size_t block_size = psa_get_hash_block_size( hash_alg );
2167 psa_status_t status;
2168
Gilles Peskine01126fa2018-07-12 17:04:55 +02002169 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2170 if( status != PSA_SUCCESS )
2171 return( status );
2172 /* From here on, tmp needs to be wiped. */
2173
2174 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2175 if( status != PSA_SUCCESS )
2176 goto exit;
2177
2178 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2179 if( status != PSA_SUCCESS )
2180 goto exit;
2181
2182 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2183 if( status != PSA_SUCCESS )
2184 goto exit;
2185
Gilles Peskined911eb72018-08-14 15:18:45 +02002186 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2187 if( status != PSA_SUCCESS )
2188 goto exit;
2189
2190 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002191
2192exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002193 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002194 return( status );
2195}
2196#endif /* MBEDTLS_MD_C */
2197
mohammad16036df908f2018-04-02 08:34:15 -07002198static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002199 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002200 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002201{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002202 if( ! operation->key_set )
2203 return( PSA_ERROR_BAD_STATE );
2204 if( operation->iv_required && ! operation->iv_set )
2205 return( PSA_ERROR_BAD_STATE );
2206
Gilles Peskine8c9def32018-02-08 10:02:12 +01002207 if( mac_size < operation->mac_size )
2208 return( PSA_ERROR_BUFFER_TOO_SMALL );
2209
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002211 if( operation->alg == PSA_ALG_CMAC )
2212 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002213 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2214 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2215 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002216 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002217 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002218 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002219 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002220 else
2221#endif /* MBEDTLS_CMAC_C */
2222#if defined(MBEDTLS_MD_C)
2223 if( PSA_ALG_IS_HMAC( operation->alg ) )
2224 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002225 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002226 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002227 }
2228 else
2229#endif /* MBEDTLS_MD_C */
2230 {
2231 /* This shouldn't happen if `operation` was initialized by
2232 * a setup function. */
2233 return( PSA_ERROR_BAD_STATE );
2234 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235}
2236
Gilles Peskineacd4be32018-07-08 19:56:25 +02002237psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2238 uint8_t *mac,
2239 size_t mac_size,
2240 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002241{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002242 psa_status_t status;
2243
Jaeden Amero252ef282019-02-15 14:05:35 +00002244 if( operation->alg == 0 )
2245 {
2246 return( PSA_ERROR_BAD_STATE );
2247 }
2248
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002249 /* Fill the output buffer with something that isn't a valid mac
2250 * (barring an attack on the mac and deliberately-crafted input),
2251 * in case the caller doesn't check the return status properly. */
2252 *mac_length = mac_size;
2253 /* If mac_size is 0 then mac may be NULL and then the
2254 * call to memset would have undefined behavior. */
2255 if( mac_size != 0 )
2256 memset( mac, '!', mac_size );
2257
Gilles Peskine89167cb2018-07-08 20:12:23 +02002258 if( ! operation->is_sign )
2259 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002260 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002261 }
mohammad16036df908f2018-04-02 08:34:15 -07002262
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002263 status = psa_mac_finish_internal( operation, mac, mac_size );
2264
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002265 if( status == PSA_SUCCESS )
2266 {
2267 status = psa_mac_abort( operation );
2268 if( status == PSA_SUCCESS )
2269 *mac_length = operation->mac_size;
2270 else
2271 memset( mac, '!', mac_size );
2272 }
2273 else
2274 psa_mac_abort( operation );
2275 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002276}
2277
Gilles Peskineacd4be32018-07-08 19:56:25 +02002278psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2279 const uint8_t *mac,
2280 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002281{
Gilles Peskine828ed142018-06-18 23:25:51 +02002282 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002283 psa_status_t status;
2284
Jaeden Amero252ef282019-02-15 14:05:35 +00002285 if( operation->alg == 0 )
2286 {
2287 return( PSA_ERROR_BAD_STATE );
2288 }
2289
Gilles Peskine89167cb2018-07-08 20:12:23 +02002290 if( operation->is_sign )
2291 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002292 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002293 }
2294 if( operation->mac_size != mac_length )
2295 {
2296 status = PSA_ERROR_INVALID_SIGNATURE;
2297 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002298 }
mohammad16036df908f2018-04-02 08:34:15 -07002299
2300 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002301 actual_mac, sizeof( actual_mac ) );
2302
2303 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2304 status = PSA_ERROR_INVALID_SIGNATURE;
2305
2306cleanup:
2307 if( status == PSA_SUCCESS )
2308 status = psa_mac_abort( operation );
2309 else
2310 psa_mac_abort( operation );
2311
Gilles Peskine3f108122018-12-07 18:14:53 +01002312 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002313
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002314 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002315}
2316
2317
Gilles Peskine20035e32018-02-03 22:44:14 +01002318
Gilles Peskine20035e32018-02-03 22:44:14 +01002319/****************************************************************/
2320/* Asymmetric cryptography */
2321/****************************************************************/
2322
Gilles Peskine2b450e32018-06-27 15:42:46 +02002323#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002324/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002325 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002326static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2327 size_t hash_length,
2328 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002329{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002330 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002331 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002332 *md_alg = mbedtls_md_get_type( md_info );
2333
2334 /* The Mbed TLS RSA module uses an unsigned int for hash length
2335 * parameters. Validate that it fits so that we don't risk an
2336 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002337#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002338 if( hash_length > UINT_MAX )
2339 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002340#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002341
2342#if defined(MBEDTLS_PKCS1_V15)
2343 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2344 * must be correct. */
2345 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2346 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002347 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348 if( md_info == NULL )
2349 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002350 if( mbedtls_md_get_size( md_info ) != hash_length )
2351 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002352 }
2353#endif /* MBEDTLS_PKCS1_V15 */
2354
2355#if defined(MBEDTLS_PKCS1_V21)
2356 /* PSS requires a hash internally. */
2357 if( PSA_ALG_IS_RSA_PSS( alg ) )
2358 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002359 if( md_info == NULL )
2360 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002361 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002362#endif /* MBEDTLS_PKCS1_V21 */
2363
Gilles Peskine61b91d42018-06-08 16:09:36 +02002364 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002365}
2366
Gilles Peskine2b450e32018-06-27 15:42:46 +02002367static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2368 psa_algorithm_t alg,
2369 const uint8_t *hash,
2370 size_t hash_length,
2371 uint8_t *signature,
2372 size_t signature_size,
2373 size_t *signature_length )
2374{
2375 psa_status_t status;
2376 int ret;
2377 mbedtls_md_type_t md_alg;
2378
2379 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2380 if( status != PSA_SUCCESS )
2381 return( status );
2382
Gilles Peskine630a18a2018-06-29 17:49:35 +02002383 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002384 return( PSA_ERROR_BUFFER_TOO_SMALL );
2385
2386#if defined(MBEDTLS_PKCS1_V15)
2387 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2388 {
2389 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2390 MBEDTLS_MD_NONE );
2391 ret = mbedtls_rsa_pkcs1_sign( rsa,
2392 mbedtls_ctr_drbg_random,
2393 &global_data.ctr_drbg,
2394 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002395 md_alg,
2396 (unsigned int) hash_length,
2397 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002398 signature );
2399 }
2400 else
2401#endif /* MBEDTLS_PKCS1_V15 */
2402#if defined(MBEDTLS_PKCS1_V21)
2403 if( PSA_ALG_IS_RSA_PSS( alg ) )
2404 {
2405 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2406 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2407 mbedtls_ctr_drbg_random,
2408 &global_data.ctr_drbg,
2409 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002410 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002411 (unsigned int) hash_length,
2412 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002413 signature );
2414 }
2415 else
2416#endif /* MBEDTLS_PKCS1_V21 */
2417 {
2418 return( PSA_ERROR_INVALID_ARGUMENT );
2419 }
2420
2421 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002422 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002423 return( mbedtls_to_psa_error( ret ) );
2424}
2425
2426static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2427 psa_algorithm_t alg,
2428 const uint8_t *hash,
2429 size_t hash_length,
2430 const uint8_t *signature,
2431 size_t signature_length )
2432{
2433 psa_status_t status;
2434 int ret;
2435 mbedtls_md_type_t md_alg;
2436
2437 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2438 if( status != PSA_SUCCESS )
2439 return( status );
2440
Gilles Peskine630a18a2018-06-29 17:49:35 +02002441 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002442 return( PSA_ERROR_BUFFER_TOO_SMALL );
2443
2444#if defined(MBEDTLS_PKCS1_V15)
2445 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2446 {
2447 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2448 MBEDTLS_MD_NONE );
2449 ret = mbedtls_rsa_pkcs1_verify( rsa,
2450 mbedtls_ctr_drbg_random,
2451 &global_data.ctr_drbg,
2452 MBEDTLS_RSA_PUBLIC,
2453 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002454 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002455 hash,
2456 signature );
2457 }
2458 else
2459#endif /* MBEDTLS_PKCS1_V15 */
2460#if defined(MBEDTLS_PKCS1_V21)
2461 if( PSA_ALG_IS_RSA_PSS( alg ) )
2462 {
2463 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2464 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2465 mbedtls_ctr_drbg_random,
2466 &global_data.ctr_drbg,
2467 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002468 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002469 (unsigned int) hash_length,
2470 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002471 signature );
2472 }
2473 else
2474#endif /* MBEDTLS_PKCS1_V21 */
2475 {
2476 return( PSA_ERROR_INVALID_ARGUMENT );
2477 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002478
2479 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2480 * the rest of the signature is invalid". This has little use in
2481 * practice and PSA doesn't report this distinction. */
2482 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2483 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002484 return( mbedtls_to_psa_error( ret ) );
2485}
2486#endif /* MBEDTLS_RSA_C */
2487
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002488#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002489/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2490 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2491 * (even though these functions don't modify it). */
2492static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2493 psa_algorithm_t alg,
2494 const uint8_t *hash,
2495 size_t hash_length,
2496 uint8_t *signature,
2497 size_t signature_size,
2498 size_t *signature_length )
2499{
2500 int ret;
2501 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002502 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002503 mbedtls_mpi_init( &r );
2504 mbedtls_mpi_init( &s );
2505
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002506 if( signature_size < 2 * curve_bytes )
2507 {
2508 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2509 goto cleanup;
2510 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002511
Gilles Peskinea05219c2018-11-16 16:02:56 +01002512#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002513 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2514 {
2515 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2516 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2517 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2518 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2519 hash, hash_length,
2520 md_alg ) );
2521 }
2522 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002523#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002524 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002525 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002526 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2527 hash, hash_length,
2528 mbedtls_ctr_drbg_random,
2529 &global_data.ctr_drbg ) );
2530 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002531
2532 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2533 signature,
2534 curve_bytes ) );
2535 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2536 signature + curve_bytes,
2537 curve_bytes ) );
2538
2539cleanup:
2540 mbedtls_mpi_free( &r );
2541 mbedtls_mpi_free( &s );
2542 if( ret == 0 )
2543 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002544 return( mbedtls_to_psa_error( ret ) );
2545}
2546
2547static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2548 const uint8_t *hash,
2549 size_t hash_length,
2550 const uint8_t *signature,
2551 size_t signature_length )
2552{
2553 int ret;
2554 mbedtls_mpi r, s;
2555 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2556 mbedtls_mpi_init( &r );
2557 mbedtls_mpi_init( &s );
2558
2559 if( signature_length != 2 * curve_bytes )
2560 return( PSA_ERROR_INVALID_SIGNATURE );
2561
2562 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2563 signature,
2564 curve_bytes ) );
2565 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2566 signature + curve_bytes,
2567 curve_bytes ) );
2568
2569 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2570 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002571
2572cleanup:
2573 mbedtls_mpi_free( &r );
2574 mbedtls_mpi_free( &s );
2575 return( mbedtls_to_psa_error( ret ) );
2576}
2577#endif /* MBEDTLS_ECDSA_C */
2578
Gilles Peskinec5487a82018-12-03 18:08:14 +01002579psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002580 psa_algorithm_t alg,
2581 const uint8_t *hash,
2582 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002583 uint8_t *signature,
2584 size_t signature_size,
2585 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002586{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002587 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002588 psa_status_t status;
2589
2590 *signature_length = signature_size;
2591
Gilles Peskinec5487a82018-12-03 18:08:14 +01002592 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002593 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002594 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002595 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002596 {
2597 status = PSA_ERROR_INVALID_ARGUMENT;
2598 goto exit;
2599 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002600
Gilles Peskine20035e32018-02-03 22:44:14 +01002601#if defined(MBEDTLS_RSA_C)
2602 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2603 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002604 status = psa_rsa_sign( slot->data.rsa,
2605 alg,
2606 hash, hash_length,
2607 signature, signature_size,
2608 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002609 }
2610 else
2611#endif /* defined(MBEDTLS_RSA_C) */
2612#if defined(MBEDTLS_ECP_C)
2613 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2614 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002615#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002616 if(
2617#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2618 PSA_ALG_IS_ECDSA( alg )
2619#else
2620 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2621#endif
2622 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002623 status = psa_ecdsa_sign( slot->data.ecp,
2624 alg,
2625 hash, hash_length,
2626 signature, signature_size,
2627 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002628 else
2629#endif /* defined(MBEDTLS_ECDSA_C) */
2630 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002631 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002632 }
itayzafrir5c753392018-05-08 11:18:38 +03002633 }
2634 else
2635#endif /* defined(MBEDTLS_ECP_C) */
2636 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002637 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002638 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002639
2640exit:
2641 /* Fill the unused part of the output buffer (the whole buffer on error,
2642 * the trailing part on success) with something that isn't a valid mac
2643 * (barring an attack on the mac and deliberately-crafted input),
2644 * in case the caller doesn't check the return status properly. */
2645 if( status == PSA_SUCCESS )
2646 memset( signature + *signature_length, '!',
2647 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002648 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002649 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002650 /* If signature_size is 0 then we have nothing to do. We must not call
2651 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002652 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002653}
2654
Gilles Peskinec5487a82018-12-03 18:08:14 +01002655psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002656 psa_algorithm_t alg,
2657 const uint8_t *hash,
2658 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002659 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002660 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002661{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002662 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002663 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002664
Gilles Peskinec5487a82018-12-03 18:08:14 +01002665 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002666 if( status != PSA_SUCCESS )
2667 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002668
Gilles Peskine61b91d42018-06-08 16:09:36 +02002669#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002670 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002672 return( psa_rsa_verify( slot->data.rsa,
2673 alg,
2674 hash, hash_length,
2675 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002676 }
2677 else
2678#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002679#if defined(MBEDTLS_ECP_C)
2680 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2681 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002682#if defined(MBEDTLS_ECDSA_C)
2683 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002684 return( psa_ecdsa_verify( slot->data.ecp,
2685 hash, hash_length,
2686 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002687 else
2688#endif /* defined(MBEDTLS_ECDSA_C) */
2689 {
2690 return( PSA_ERROR_INVALID_ARGUMENT );
2691 }
itayzafrir5c753392018-05-08 11:18:38 +03002692 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002693 else
2694#endif /* defined(MBEDTLS_ECP_C) */
2695 {
2696 return( PSA_ERROR_NOT_SUPPORTED );
2697 }
2698}
2699
Gilles Peskine072ac562018-06-30 00:21:29 +02002700#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2701static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2702 mbedtls_rsa_context *rsa )
2703{
2704 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2705 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2706 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2707 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2708}
2709#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2710
Gilles Peskinec5487a82018-12-03 18:08:14 +01002711psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002712 psa_algorithm_t alg,
2713 const uint8_t *input,
2714 size_t input_length,
2715 const uint8_t *salt,
2716 size_t salt_length,
2717 uint8_t *output,
2718 size_t output_size,
2719 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002721 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002722 psa_status_t status;
2723
Darryl Green5cc689a2018-07-24 15:34:10 +01002724 (void) input;
2725 (void) input_length;
2726 (void) salt;
2727 (void) output;
2728 (void) output_size;
2729
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002730 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002731
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002732 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2733 return( PSA_ERROR_INVALID_ARGUMENT );
2734
Gilles Peskinec5487a82018-12-03 18:08:14 +01002735 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002736 if( status != PSA_SUCCESS )
2737 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002738 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2739 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741
2742#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002743 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744 {
2745 mbedtls_rsa_context *rsa = slot->data.rsa;
2746 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002747 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002748 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002749#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002750 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002751 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002752 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2753 mbedtls_ctr_drbg_random,
2754 &global_data.ctr_drbg,
2755 MBEDTLS_RSA_PUBLIC,
2756 input_length,
2757 input,
2758 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002759 }
2760 else
2761#endif /* MBEDTLS_PKCS1_V15 */
2762#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002763 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002764 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002765 psa_rsa_oaep_set_padding_mode( alg, rsa );
2766 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2767 mbedtls_ctr_drbg_random,
2768 &global_data.ctr_drbg,
2769 MBEDTLS_RSA_PUBLIC,
2770 salt, salt_length,
2771 input_length,
2772 input,
2773 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774 }
2775 else
2776#endif /* MBEDTLS_PKCS1_V21 */
2777 {
2778 return( PSA_ERROR_INVALID_ARGUMENT );
2779 }
2780 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002781 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 return( mbedtls_to_psa_error( ret ) );
2783 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002785#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002786 {
2787 return( PSA_ERROR_NOT_SUPPORTED );
2788 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002789}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002790
Gilles Peskinec5487a82018-12-03 18:08:14 +01002791psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002792 psa_algorithm_t alg,
2793 const uint8_t *input,
2794 size_t input_length,
2795 const uint8_t *salt,
2796 size_t salt_length,
2797 uint8_t *output,
2798 size_t output_size,
2799 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002800{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002801 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002802 psa_status_t status;
2803
Darryl Green5cc689a2018-07-24 15:34:10 +01002804 (void) input;
2805 (void) input_length;
2806 (void) salt;
2807 (void) output;
2808 (void) output_size;
2809
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002810 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002811
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002812 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2813 return( PSA_ERROR_INVALID_ARGUMENT );
2814
Gilles Peskinec5487a82018-12-03 18:08:14 +01002815 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002816 if( status != PSA_SUCCESS )
2817 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002818 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2819 return( PSA_ERROR_INVALID_ARGUMENT );
2820
2821#if defined(MBEDTLS_RSA_C)
2822 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2823 {
2824 mbedtls_rsa_context *rsa = slot->data.rsa;
2825 int ret;
2826
Gilles Peskine630a18a2018-06-29 17:49:35 +02002827 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828 return( PSA_ERROR_INVALID_ARGUMENT );
2829
2830#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002831 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002832 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002833 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2834 mbedtls_ctr_drbg_random,
2835 &global_data.ctr_drbg,
2836 MBEDTLS_RSA_PRIVATE,
2837 output_length,
2838 input,
2839 output,
2840 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002841 }
2842 else
2843#endif /* MBEDTLS_PKCS1_V15 */
2844#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002845 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002847 psa_rsa_oaep_set_padding_mode( alg, rsa );
2848 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2849 mbedtls_ctr_drbg_random,
2850 &global_data.ctr_drbg,
2851 MBEDTLS_RSA_PRIVATE,
2852 salt, salt_length,
2853 output_length,
2854 input,
2855 output,
2856 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002857 }
2858 else
2859#endif /* MBEDTLS_PKCS1_V21 */
2860 {
2861 return( PSA_ERROR_INVALID_ARGUMENT );
2862 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002863
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002864 return( mbedtls_to_psa_error( ret ) );
2865 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002866 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002867#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002868 {
2869 return( PSA_ERROR_NOT_SUPPORTED );
2870 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002871}
Gilles Peskine20035e32018-02-03 22:44:14 +01002872
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002873
2874
mohammad1603503973b2018-03-12 15:59:30 +02002875/****************************************************************/
2876/* Symmetric cryptography */
2877/****************************************************************/
2878
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879/* Initialize the cipher operation structure. Once this function has been
2880 * called, psa_cipher_abort can run and will do the right thing. */
2881static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2882 psa_algorithm_t alg )
2883{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002884 if( ! PSA_ALG_IS_CIPHER( alg ) )
2885 {
2886 memset( operation, 0, sizeof( *operation ) );
2887 return( PSA_ERROR_INVALID_ARGUMENT );
2888 }
2889
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002890 operation->alg = alg;
2891 operation->key_set = 0;
2892 operation->iv_set = 0;
2893 operation->iv_required = 1;
2894 operation->iv_size = 0;
2895 operation->block_size = 0;
2896 mbedtls_cipher_init( &operation->ctx.cipher );
2897 return( PSA_SUCCESS );
2898}
2899
Gilles Peskinee553c652018-06-04 16:22:46 +02002900static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002901 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002902 psa_algorithm_t alg,
2903 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002904{
2905 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2906 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002907 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002908 size_t key_bits;
2909 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002910 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2911 PSA_KEY_USAGE_ENCRYPT :
2912 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002913
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002914 /* A context must be freshly initialized before it can be set up. */
2915 if( operation->alg != 0 )
2916 {
2917 return( PSA_ERROR_BAD_STATE );
2918 }
2919
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002920 status = psa_cipher_init( operation, alg );
2921 if( status != PSA_SUCCESS )
2922 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002923
Gilles Peskinec5487a82018-12-03 18:08:14 +01002924 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002925 if( status != PSA_SUCCESS )
2926 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002927 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002928
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002929 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002930 if( cipher_info == NULL )
2931 return( PSA_ERROR_NOT_SUPPORTED );
2932
mohammad1603503973b2018-03-12 15:59:30 +02002933 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002934 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002935 {
2936 psa_cipher_abort( operation );
2937 return( mbedtls_to_psa_error( ret ) );
2938 }
2939
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002940#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002941 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002942 {
2943 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2944 unsigned char keys[24];
2945 memcpy( keys, slot->data.raw.data, 16 );
2946 memcpy( keys + 16, slot->data.raw.data, 8 );
2947 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2948 keys,
2949 192, cipher_operation );
2950 }
2951 else
2952#endif
2953 {
2954 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2955 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002956 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002957 }
Moran Peker41deec42018-04-04 15:43:05 +03002958 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002959 {
2960 psa_cipher_abort( operation );
2961 return( mbedtls_to_psa_error( ret ) );
2962 }
2963
mohammad16038481e742018-03-18 13:57:31 +02002964#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002965 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002966 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002967 case PSA_ALG_CBC_NO_PADDING:
2968 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2969 MBEDTLS_PADDING_NONE );
2970 break;
2971 case PSA_ALG_CBC_PKCS7:
2972 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2973 MBEDTLS_PADDING_PKCS7 );
2974 break;
2975 default:
2976 /* The algorithm doesn't involve padding. */
2977 ret = 0;
2978 break;
2979 }
2980 if( ret != 0 )
2981 {
2982 psa_cipher_abort( operation );
2983 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002984 }
2985#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2986
mohammad1603503973b2018-03-12 15:59:30 +02002987 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002988 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2989 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2990 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002991 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002992 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002993 }
mohammad1603503973b2018-03-12 15:59:30 +02002994
Moran Peker395db872018-05-31 14:07:14 +03002995 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002996}
2997
Gilles Peskinefe119512018-07-08 21:39:34 +02002998psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002999 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003000 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003001{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003002 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003003}
3004
Gilles Peskinefe119512018-07-08 21:39:34 +02003005psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003006 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003007 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003008{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003009 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003010}
3011
Gilles Peskinefe119512018-07-08 21:39:34 +02003012psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3013 unsigned char *iv,
3014 size_t iv_size,
3015 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003016{
itayzafrir534bd7c2018-08-02 13:56:32 +03003017 psa_status_t status;
3018 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003019 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003020 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003021 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003022 }
Moran Peker41deec42018-04-04 15:43:05 +03003023 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003024 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003025 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003026 goto exit;
3027 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003028 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3029 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003030 if( ret != 0 )
3031 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003032 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003033 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003034 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003035
mohammad16038481e742018-03-18 13:57:31 +02003036 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003037 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003038
Moran Peker395db872018-05-31 14:07:14 +03003039exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003040 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003041 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003042 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003043}
3044
Gilles Peskinefe119512018-07-08 21:39:34 +02003045psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3046 const unsigned char *iv,
3047 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003048{
itayzafrir534bd7c2018-08-02 13:56:32 +03003049 psa_status_t status;
3050 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003051 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003052 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003053 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003054 }
Moran Pekera28258c2018-05-29 16:25:04 +03003055 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003056 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003057 status = PSA_ERROR_INVALID_ARGUMENT;
3058 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003059 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003060 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3061 status = mbedtls_to_psa_error( ret );
3062exit:
3063 if( status == PSA_SUCCESS )
3064 operation->iv_set = 1;
3065 else
mohammad1603503973b2018-03-12 15:59:30 +02003066 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003067 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003068}
3069
Gilles Peskinee553c652018-06-04 16:22:46 +02003070psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3071 const uint8_t *input,
3072 size_t input_length,
3073 unsigned char *output,
3074 size_t output_size,
3075 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003076{
itayzafrir534bd7c2018-08-02 13:56:32 +03003077 psa_status_t status;
3078 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003079 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003080
3081 if( operation->alg == 0 )
3082 {
3083 return( PSA_ERROR_BAD_STATE );
3084 }
3085
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003086 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003087 {
3088 /* Take the unprocessed partial block left over from previous
3089 * update calls, if any, plus the input to this call. Remove
3090 * the last partial block, if any. You get the data that will be
3091 * output in this call. */
3092 expected_output_size =
3093 ( operation->ctx.cipher.unprocessed_len + input_length )
3094 / operation->block_size * operation->block_size;
3095 }
3096 else
3097 {
3098 expected_output_size = input_length;
3099 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003100
Gilles Peskine89d789c2018-06-04 17:17:16 +02003101 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003102 {
3103 status = PSA_ERROR_BUFFER_TOO_SMALL;
3104 goto exit;
3105 }
mohammad160382759612018-03-12 18:16:40 +02003106
mohammad1603503973b2018-03-12 15:59:30 +02003107 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003108 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003109 status = mbedtls_to_psa_error( ret );
3110exit:
3111 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003112 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003113 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003114}
3115
Gilles Peskinee553c652018-06-04 16:22:46 +02003116psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3117 uint8_t *output,
3118 size_t output_size,
3119 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003120{
David Saadab4ecc272019-02-14 13:48:10 +02003121 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003122 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003123 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003124
mohammad1603503973b2018-03-12 15:59:30 +02003125 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003126 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003127 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003128 }
3129 if( operation->iv_required && ! operation->iv_set )
3130 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003131 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003132 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003133
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003134 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003135 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3136 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003137 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003138 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003139 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003140 }
3141
Janos Follath315b51c2018-07-09 16:04:51 +01003142 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3143 temp_output_buffer,
3144 output_length );
3145 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003146 {
Janos Follath315b51c2018-07-09 16:04:51 +01003147 status = mbedtls_to_psa_error( cipher_ret );
3148 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003149 }
Janos Follath315b51c2018-07-09 16:04:51 +01003150
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003151 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003152 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003153 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003154 memcpy( output, temp_output_buffer, *output_length );
3155 else
3156 {
Janos Follath315b51c2018-07-09 16:04:51 +01003157 status = PSA_ERROR_BUFFER_TOO_SMALL;
3158 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003159 }
mohammad1603503973b2018-03-12 15:59:30 +02003160
Gilles Peskine3f108122018-12-07 18:14:53 +01003161 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003162 status = psa_cipher_abort( operation );
3163
3164 return( status );
3165
3166error:
3167
3168 *output_length = 0;
3169
Gilles Peskine3f108122018-12-07 18:14:53 +01003170 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003171 (void) psa_cipher_abort( operation );
3172
3173 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003174}
3175
Gilles Peskinee553c652018-06-04 16:22:46 +02003176psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3177{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003179 {
3180 /* The object has (apparently) been initialized but it is not
3181 * in use. It's ok to call abort on such an object, and there's
3182 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003183 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003184 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003185
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003186 /* Sanity check (shouldn't happen: operation->alg should
3187 * always have been initialized to a valid value). */
3188 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3189 return( PSA_ERROR_BAD_STATE );
3190
mohammad1603503973b2018-03-12 15:59:30 +02003191 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003192
Moran Peker41deec42018-04-04 15:43:05 +03003193 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003194 operation->key_set = 0;
3195 operation->iv_set = 0;
3196 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003197 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003198 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003199
Moran Peker395db872018-05-31 14:07:14 +03003200 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003201}
3202
Gilles Peskinea0655c32018-04-30 17:06:50 +02003203
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003204
mohammad16038cc1cee2018-03-28 01:21:33 +03003205/****************************************************************/
3206/* Key Policy */
3207/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003208
mohammad160327010052018-07-03 13:16:15 +03003209#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003210void psa_key_policy_set_usage( psa_key_policy_t *policy,
3211 psa_key_usage_t usage,
3212 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003213{
mohammad16034eed7572018-03-28 05:14:59 -07003214 policy->usage = usage;
3215 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003216}
3217
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003218psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003219{
mohammad16036df908f2018-04-02 08:34:15 -07003220 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003221}
3222
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003223psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003224{
mohammad16036df908f2018-04-02 08:34:15 -07003225 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003226}
mohammad160327010052018-07-03 13:16:15 +03003227#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003228
Gilles Peskinec5487a82018-12-03 18:08:14 +01003229psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003230 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003231{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003232 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003233 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003234
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003235 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003237
Gilles Peskinec5487a82018-12-03 18:08:14 +01003238 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003239 if( status != PSA_SUCCESS )
3240 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003241
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003242 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3243 PSA_KEY_USAGE_ENCRYPT |
3244 PSA_KEY_USAGE_DECRYPT |
3245 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003246 PSA_KEY_USAGE_VERIFY |
3247 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003248 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003249
mohammad16036df908f2018-04-02 08:34:15 -07003250 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003251
3252 return( PSA_SUCCESS );
3253}
3254
Gilles Peskinec5487a82018-12-03 18:08:14 +01003255psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003256 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003257{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003258 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003259 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003260
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003261 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003262 return( PSA_ERROR_INVALID_ARGUMENT );
3263
Gilles Peskinec5487a82018-12-03 18:08:14 +01003264 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003265 if( status != PSA_SUCCESS )
3266 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003267
mohammad16036df908f2018-04-02 08:34:15 -07003268 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003269
3270 return( PSA_SUCCESS );
3271}
Gilles Peskine20035e32018-02-03 22:44:14 +01003272
Gilles Peskinea0655c32018-04-30 17:06:50 +02003273
3274
mohammad1603804cd712018-03-20 22:44:08 +02003275/****************************************************************/
3276/* Key Lifetime */
3277/****************************************************************/
3278
Gilles Peskinec5487a82018-12-03 18:08:14 +01003279psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003280 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003281{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003282 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003283 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003284
Gilles Peskinec5487a82018-12-03 18:08:14 +01003285 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003286 if( status != PSA_SUCCESS )
3287 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003288
mohammad1603804cd712018-03-20 22:44:08 +02003289 *lifetime = slot->lifetime;
3290
3291 return( PSA_SUCCESS );
3292}
3293
Gilles Peskine20035e32018-02-03 22:44:14 +01003294
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003295
mohammad16035955c982018-04-26 00:53:03 +03003296/****************************************************************/
3297/* AEAD */
3298/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003299
Gilles Peskineedf9a652018-08-17 18:11:56 +02003300typedef struct
3301{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003302 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003303 const mbedtls_cipher_info_t *cipher_info;
3304 union
3305 {
3306#if defined(MBEDTLS_CCM_C)
3307 mbedtls_ccm_context ccm;
3308#endif /* MBEDTLS_CCM_C */
3309#if defined(MBEDTLS_GCM_C)
3310 mbedtls_gcm_context gcm;
3311#endif /* MBEDTLS_GCM_C */
3312 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003313 psa_algorithm_t core_alg;
3314 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003315 uint8_t tag_length;
3316} aead_operation_t;
3317
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003318static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003319{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003320 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321 {
3322#if defined(MBEDTLS_CCM_C)
3323 case PSA_ALG_CCM:
3324 mbedtls_ccm_free( &operation->ctx.ccm );
3325 break;
3326#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003327#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003328 case PSA_ALG_GCM:
3329 mbedtls_gcm_free( &operation->ctx.gcm );
3330 break;
3331#endif /* MBEDTLS_GCM_C */
3332 }
3333}
3334
3335static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003336 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003337 psa_key_usage_t usage,
3338 psa_algorithm_t alg )
3339{
3340 psa_status_t status;
3341 size_t key_bits;
3342 mbedtls_cipher_id_t cipher_id;
3343
Gilles Peskinec5487a82018-12-03 18:08:14 +01003344 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003345 if( status != PSA_SUCCESS )
3346 return( status );
3347
3348 key_bits = psa_get_key_bits( operation->slot );
3349
3350 operation->cipher_info =
3351 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3352 &cipher_id );
3353 if( operation->cipher_info == NULL )
3354 return( PSA_ERROR_NOT_SUPPORTED );
3355
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003356 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003357 {
3358#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003359 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3360 operation->core_alg = PSA_ALG_CCM;
3361 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003362 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3363 return( PSA_ERROR_INVALID_ARGUMENT );
3364 mbedtls_ccm_init( &operation->ctx.ccm );
3365 status = mbedtls_to_psa_error(
3366 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3367 operation->slot->data.raw.data,
3368 (unsigned int) key_bits ) );
3369 if( status != 0 )
3370 goto cleanup;
3371 break;
3372#endif /* MBEDTLS_CCM_C */
3373
3374#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003375 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3376 operation->core_alg = PSA_ALG_GCM;
3377 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003378 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3379 return( PSA_ERROR_INVALID_ARGUMENT );
3380 mbedtls_gcm_init( &operation->ctx.gcm );
3381 status = mbedtls_to_psa_error(
3382 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3383 operation->slot->data.raw.data,
3384 (unsigned int) key_bits ) );
3385 break;
3386#endif /* MBEDTLS_GCM_C */
3387
3388 default:
3389 return( PSA_ERROR_NOT_SUPPORTED );
3390 }
3391
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003392 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3393 {
3394 status = PSA_ERROR_INVALID_ARGUMENT;
3395 goto cleanup;
3396 }
3397 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3398 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3399 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3400 * In both cases, mbedtls_xxx will validate the tag length below. */
3401
Gilles Peskineedf9a652018-08-17 18:11:56 +02003402 return( PSA_SUCCESS );
3403
3404cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003405 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 return( status );
3407}
3408
Gilles Peskinec5487a82018-12-03 18:08:14 +01003409psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003410 psa_algorithm_t alg,
3411 const uint8_t *nonce,
3412 size_t nonce_length,
3413 const uint8_t *additional_data,
3414 size_t additional_data_length,
3415 const uint8_t *plaintext,
3416 size_t plaintext_length,
3417 uint8_t *ciphertext,
3418 size_t ciphertext_size,
3419 size_t *ciphertext_length )
3420{
mohammad16035955c982018-04-26 00:53:03 +03003421 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003422 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003423 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003424
mohammad1603f08a5502018-06-03 15:05:47 +03003425 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003426
Gilles Peskinec5487a82018-12-03 18:08:14 +01003427 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428 if( status != PSA_SUCCESS )
3429 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003430
Gilles Peskineedf9a652018-08-17 18:11:56 +02003431 /* For all currently supported modes, the tag is at the end of the
3432 * ciphertext. */
3433 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3434 {
3435 status = PSA_ERROR_BUFFER_TOO_SMALL;
3436 goto exit;
3437 }
3438 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003439
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003440#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003441 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003442 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003443 status = mbedtls_to_psa_error(
3444 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3445 MBEDTLS_GCM_ENCRYPT,
3446 plaintext_length,
3447 nonce, nonce_length,
3448 additional_data, additional_data_length,
3449 plaintext, ciphertext,
3450 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003451 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003452 else
3453#endif /* MBEDTLS_GCM_C */
3454#if defined(MBEDTLS_CCM_C)
3455 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003456 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003457 status = mbedtls_to_psa_error(
3458 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3459 plaintext_length,
3460 nonce, nonce_length,
3461 additional_data,
3462 additional_data_length,
3463 plaintext, ciphertext,
3464 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003465 }
mohammad16035c8845f2018-05-09 05:40:09 -07003466 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003467#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003468 {
mohammad1603554faad2018-06-03 15:07:38 +03003469 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003470 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003471
Gilles Peskineedf9a652018-08-17 18:11:56 +02003472 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3473 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003474
Gilles Peskineedf9a652018-08-17 18:11:56 +02003475exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003476 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003477 if( status == PSA_SUCCESS )
3478 *ciphertext_length = plaintext_length + operation.tag_length;
3479 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003480}
3481
Gilles Peskineee652a32018-06-01 19:23:52 +02003482/* Locate the tag in a ciphertext buffer containing the encrypted data
3483 * followed by the tag. Return the length of the part preceding the tag in
3484 * *plaintext_length. This is the size of the plaintext in modes where
3485 * the encrypted data has the same size as the plaintext, such as
3486 * CCM and GCM. */
3487static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3488 const uint8_t *ciphertext,
3489 size_t ciphertext_length,
3490 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003491 const uint8_t **p_tag )
3492{
3493 size_t payload_length;
3494 if( tag_length > ciphertext_length )
3495 return( PSA_ERROR_INVALID_ARGUMENT );
3496 payload_length = ciphertext_length - tag_length;
3497 if( payload_length > plaintext_size )
3498 return( PSA_ERROR_BUFFER_TOO_SMALL );
3499 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003500 return( PSA_SUCCESS );
3501}
3502
Gilles Peskinec5487a82018-12-03 18:08:14 +01003503psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003504 psa_algorithm_t alg,
3505 const uint8_t *nonce,
3506 size_t nonce_length,
3507 const uint8_t *additional_data,
3508 size_t additional_data_length,
3509 const uint8_t *ciphertext,
3510 size_t ciphertext_length,
3511 uint8_t *plaintext,
3512 size_t plaintext_size,
3513 size_t *plaintext_length )
3514{
mohammad16035955c982018-04-26 00:53:03 +03003515 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003516 aead_operation_t operation;
3517 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003518
Gilles Peskineee652a32018-06-01 19:23:52 +02003519 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003520
Gilles Peskinec5487a82018-12-03 18:08:14 +01003521 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003522 if( status != PSA_SUCCESS )
3523 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003524
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003525#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003526 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003527 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003528 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003529 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003530 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003531 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003532 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003533
Gilles Peskineedf9a652018-08-17 18:11:56 +02003534 status = mbedtls_to_psa_error(
3535 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3536 ciphertext_length - operation.tag_length,
3537 nonce, nonce_length,
3538 additional_data,
3539 additional_data_length,
3540 tag, operation.tag_length,
3541 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003542 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003543 else
3544#endif /* MBEDTLS_GCM_C */
3545#if defined(MBEDTLS_CCM_C)
3546 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003547 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003548 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003549 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003550 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003551 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003552 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003553
Gilles Peskineedf9a652018-08-17 18:11:56 +02003554 status = mbedtls_to_psa_error(
3555 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3556 ciphertext_length - operation.tag_length,
3557 nonce, nonce_length,
3558 additional_data,
3559 additional_data_length,
3560 ciphertext, plaintext,
3561 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003562 }
mohammad160339574652018-06-01 04:39:53 -07003563 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003564#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003565 {
mohammad1603554faad2018-06-03 15:07:38 +03003566 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003567 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003568
Gilles Peskineedf9a652018-08-17 18:11:56 +02003569 if( status != PSA_SUCCESS && plaintext_size != 0 )
3570 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003571
Gilles Peskineedf9a652018-08-17 18:11:56 +02003572exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003573 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003574 if( status == PSA_SUCCESS )
3575 *plaintext_length = ciphertext_length - operation.tag_length;
3576 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003577}
3578
Gilles Peskinea0655c32018-04-30 17:06:50 +02003579
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003580
Gilles Peskine20035e32018-02-03 22:44:14 +01003581/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003582/* Generators */
3583/****************************************************************/
3584
3585psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3586{
3587 psa_status_t status = PSA_SUCCESS;
3588 if( generator->alg == 0 )
3589 {
3590 /* The object has (apparently) been initialized but it is not
3591 * in use. It's ok to call abort on such an object, and there's
3592 * nothing to do. */
3593 }
3594 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003595 if( generator->alg == PSA_ALG_SELECT_RAW )
3596 {
3597 if( generator->ctx.buffer.data != NULL )
3598 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003599 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003600 generator->ctx.buffer.size );
3601 mbedtls_free( generator->ctx.buffer.data );
3602 }
3603 }
3604 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003605#if defined(MBEDTLS_MD_C)
3606 if( PSA_ALG_IS_HKDF( generator->alg ) )
3607 {
3608 mbedtls_free( generator->ctx.hkdf.info );
3609 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3610 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003611 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3612 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3613 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003614 {
3615 if( generator->ctx.tls12_prf.key != NULL )
3616 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003617 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003618 generator->ctx.tls12_prf.key_len );
3619 mbedtls_free( generator->ctx.tls12_prf.key );
3620 }
Hanno Becker580fba12018-11-13 20:50:45 +00003621
3622 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3623 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003624 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003625 generator->ctx.tls12_prf.Ai_with_seed_len );
3626 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3627 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003628 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003629 else
3630#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003631 {
3632 status = PSA_ERROR_BAD_STATE;
3633 }
3634 memset( generator, 0, sizeof( *generator ) );
3635 return( status );
3636}
3637
3638
3639psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3640 size_t *capacity)
3641{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003642 if( generator->alg == 0 )
3643 {
3644 /* This is a blank generator. */
3645 return PSA_ERROR_BAD_STATE;
3646 }
3647
Gilles Peskineeab56e42018-07-12 17:12:33 +02003648 *capacity = generator->capacity;
3649 return( PSA_SUCCESS );
3650}
3651
Gilles Peskinebef7f142018-07-12 17:22:21 +02003652#if defined(MBEDTLS_MD_C)
3653/* Read some bytes from an HKDF-based generator. This performs a chunk
3654 * of the expand phase of the HKDF algorithm. */
3655static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3656 psa_algorithm_t hash_alg,
3657 uint8_t *output,
3658 size_t output_length )
3659{
3660 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3661 psa_status_t status;
3662
3663 while( output_length != 0 )
3664 {
3665 /* Copy what remains of the current block */
3666 uint8_t n = hash_length - hkdf->offset_in_block;
3667 if( n > output_length )
3668 n = (uint8_t) output_length;
3669 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3670 output += n;
3671 output_length -= n;
3672 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003673 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003674 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003675 /* We can't be wanting more output after block 0xff, otherwise
3676 * the capacity check in psa_generator_read() would have
3677 * prevented this call. It could happen only if the generator
3678 * object was corrupted or if this function is called directly
3679 * inside the library. */
3680 if( hkdf->block_number == 0xff )
3681 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003682
3683 /* We need a new block */
3684 ++hkdf->block_number;
3685 hkdf->offset_in_block = 0;
3686 status = psa_hmac_setup_internal( &hkdf->hmac,
3687 hkdf->prk, hash_length,
3688 hash_alg );
3689 if( status != PSA_SUCCESS )
3690 return( status );
3691 if( hkdf->block_number != 1 )
3692 {
3693 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3694 hkdf->output_block,
3695 hash_length );
3696 if( status != PSA_SUCCESS )
3697 return( status );
3698 }
3699 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3700 hkdf->info,
3701 hkdf->info_length );
3702 if( status != PSA_SUCCESS )
3703 return( status );
3704 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3705 &hkdf->block_number, 1 );
3706 if( status != PSA_SUCCESS )
3707 return( status );
3708 status = psa_hmac_finish_internal( &hkdf->hmac,
3709 hkdf->output_block,
3710 sizeof( hkdf->output_block ) );
3711 if( status != PSA_SUCCESS )
3712 return( status );
3713 }
3714
3715 return( PSA_SUCCESS );
3716}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003717
3718static psa_status_t psa_generator_tls12_prf_generate_next_block(
3719 psa_tls12_prf_generator_t *tls12_prf,
3720 psa_algorithm_t alg )
3721{
3722 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3723 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3724 psa_hmac_internal_data hmac;
3725 psa_status_t status, cleanup_status;
3726
Hanno Becker3b339e22018-11-13 20:56:14 +00003727 unsigned char *Ai;
3728 size_t Ai_len;
3729
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003730 /* We can't be wanting more output after block 0xff, otherwise
3731 * the capacity check in psa_generator_read() would have
3732 * prevented this call. It could happen only if the generator
3733 * object was corrupted or if this function is called directly
3734 * inside the library. */
3735 if( tls12_prf->block_number == 0xff )
3736 return( PSA_ERROR_BAD_STATE );
3737
3738 /* We need a new block */
3739 ++tls12_prf->block_number;
3740 tls12_prf->offset_in_block = 0;
3741
3742 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3743 *
3744 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3745 *
3746 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3747 * HMAC_hash(secret, A(2) + seed) +
3748 * HMAC_hash(secret, A(3) + seed) + ...
3749 *
3750 * A(0) = seed
3751 * A(i) = HMAC_hash( secret, A(i-1) )
3752 *
3753 * The `psa_tls12_prf_generator` structures saves the block
3754 * `HMAC_hash(secret, A(i) + seed)` from which the output
3755 * is currently extracted as `output_block`, while
3756 * `A(i) + seed` is stored in `Ai_with_seed`.
3757 *
3758 * Generating a new block means recalculating `Ai_with_seed`
3759 * from the A(i)-part of it, and afterwards recalculating
3760 * `output_block`.
3761 *
3762 * A(0) is computed at setup time.
3763 *
3764 */
3765
3766 psa_hmac_init_internal( &hmac );
3767
3768 /* We must distinguish the calculation of A(1) from those
3769 * of A(2) and higher, because A(0)=seed has a different
3770 * length than the other A(i). */
3771 if( tls12_prf->block_number == 1 )
3772 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003773 Ai = tls12_prf->Ai_with_seed + hash_length;
3774 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003775 }
3776 else
3777 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003778 Ai = tls12_prf->Ai_with_seed;
3779 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003780 }
3781
Hanno Becker3b339e22018-11-13 20:56:14 +00003782 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3783 status = psa_hmac_setup_internal( &hmac,
3784 tls12_prf->key,
3785 tls12_prf->key_len,
3786 hash_alg );
3787 if( status != PSA_SUCCESS )
3788 goto cleanup;
3789
3790 status = psa_hash_update( &hmac.hash_ctx,
3791 Ai, Ai_len );
3792 if( status != PSA_SUCCESS )
3793 goto cleanup;
3794
3795 status = psa_hmac_finish_internal( &hmac,
3796 tls12_prf->Ai_with_seed,
3797 hash_length );
3798 if( status != PSA_SUCCESS )
3799 goto cleanup;
3800
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003801 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3802 status = psa_hmac_setup_internal( &hmac,
3803 tls12_prf->key,
3804 tls12_prf->key_len,
3805 hash_alg );
3806 if( status != PSA_SUCCESS )
3807 goto cleanup;
3808
3809 status = psa_hash_update( &hmac.hash_ctx,
3810 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003811 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003812 if( status != PSA_SUCCESS )
3813 goto cleanup;
3814
3815 status = psa_hmac_finish_internal( &hmac,
3816 tls12_prf->output_block,
3817 hash_length );
3818 if( status != PSA_SUCCESS )
3819 goto cleanup;
3820
3821cleanup:
3822
3823 cleanup_status = psa_hmac_abort_internal( &hmac );
3824 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3825 status = cleanup_status;
3826
3827 return( status );
3828}
3829
3830/* Read some bytes from an TLS-1.2-PRF-based generator.
3831 * See Section 5 of RFC 5246. */
3832static psa_status_t psa_generator_tls12_prf_read(
3833 psa_tls12_prf_generator_t *tls12_prf,
3834 psa_algorithm_t alg,
3835 uint8_t *output,
3836 size_t output_length )
3837{
3838 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3839 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3840 psa_status_t status;
3841
3842 while( output_length != 0 )
3843 {
3844 /* Copy what remains of the current block */
3845 uint8_t n = hash_length - tls12_prf->offset_in_block;
3846
3847 /* Check if we have fully processed the current block. */
3848 if( n == 0 )
3849 {
3850 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3851 alg );
3852 if( status != PSA_SUCCESS )
3853 return( status );
3854
3855 continue;
3856 }
3857
3858 if( n > output_length )
3859 n = (uint8_t) output_length;
3860 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3861 n );
3862 output += n;
3863 output_length -= n;
3864 tls12_prf->offset_in_block += n;
3865 }
3866
3867 return( PSA_SUCCESS );
3868}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003869#endif /* MBEDTLS_MD_C */
3870
Gilles Peskineeab56e42018-07-12 17:12:33 +02003871psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3872 uint8_t *output,
3873 size_t output_length )
3874{
3875 psa_status_t status;
3876
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003877 if( generator->alg == 0 )
3878 {
3879 /* This is a blank generator. */
3880 return PSA_ERROR_BAD_STATE;
3881 }
3882
Gilles Peskineeab56e42018-07-12 17:12:33 +02003883 if( output_length > generator->capacity )
3884 {
3885 generator->capacity = 0;
3886 /* Go through the error path to wipe all confidential data now
3887 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003888 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889 goto exit;
3890 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003891 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003892 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003893 /* Edge case: this is a finished generator, and 0 bytes
3894 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003895 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3896 * INSUFFICIENT_CAPACITY, which is right for a finished
3897 * generator, for consistency with the case when
3898 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003899 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900 }
3901 generator->capacity -= output_length;
3902
Gilles Peskine751d9652018-09-18 12:05:44 +02003903 if( generator->alg == PSA_ALG_SELECT_RAW )
3904 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003905 /* Initially, the capacity of a selection generator is always
3906 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3907 * abbreviated in this comment as `size`. When the remaining
3908 * capacity is `c`, the next bytes to serve start `c` bytes
3909 * from the end of the buffer, i.e. `size - c` from the
3910 * beginning of the buffer. Since `generator->capacity` was just
3911 * decremented above, we need to serve the bytes from
3912 * `size - generator->capacity - output_length` to
3913 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003914 size_t offset =
3915 generator->ctx.buffer.size - generator->capacity - output_length;
3916 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3917 status = PSA_SUCCESS;
3918 }
3919 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003920#if defined(MBEDTLS_MD_C)
3921 if( PSA_ALG_IS_HKDF( generator->alg ) )
3922 {
3923 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3924 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3925 output, output_length );
3926 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003927 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3928 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003929 {
3930 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3931 generator->alg, output,
3932 output_length );
3933 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003934 else
3935#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003936 {
3937 return( PSA_ERROR_BAD_STATE );
3938 }
3939
3940exit:
3941 if( status != PSA_SUCCESS )
3942 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003943 /* Preserve the algorithm upon errors, but clear all sensitive state.
3944 * This allows us to differentiate between exhausted generators and
3945 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3946 * generators. */
3947 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003948 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003949 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003950 memset( output, '!', output_length );
3951 }
3952 return( status );
3953}
3954
Gilles Peskine08542d82018-07-19 17:05:42 +02003955#if defined(MBEDTLS_DES_C)
3956static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3957{
3958 if( data_size >= 8 )
3959 mbedtls_des_key_set_parity( data );
3960 if( data_size >= 16 )
3961 mbedtls_des_key_set_parity( data + 8 );
3962 if( data_size >= 24 )
3963 mbedtls_des_key_set_parity( data + 16 );
3964}
3965#endif /* MBEDTLS_DES_C */
3966
Gilles Peskinec5487a82018-12-03 18:08:14 +01003967psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003968 psa_key_type_t type,
3969 size_t bits,
3970 psa_crypto_generator_t *generator )
3971{
3972 uint8_t *data = NULL;
3973 size_t bytes = PSA_BITS_TO_BYTES( bits );
3974 psa_status_t status;
3975
3976 if( ! key_type_is_raw_bytes( type ) )
3977 return( PSA_ERROR_INVALID_ARGUMENT );
3978 if( bits % 8 != 0 )
3979 return( PSA_ERROR_INVALID_ARGUMENT );
3980 data = mbedtls_calloc( 1, bytes );
3981 if( data == NULL )
3982 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3983
3984 status = psa_generator_read( generator, data, bytes );
3985 if( status != PSA_SUCCESS )
3986 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003987#if defined(MBEDTLS_DES_C)
3988 if( type == PSA_KEY_TYPE_DES )
3989 psa_des_set_key_parity( data, bytes );
3990#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003991 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003992
3993exit:
3994 mbedtls_free( data );
3995 return( status );
3996}
3997
3998
3999
4000/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004001/* Key derivation */
4002/****************************************************************/
4003
Gilles Peskinea05219c2018-11-16 16:02:56 +01004004#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004005/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004006 * of the HKDF algorithm.
4007 *
4008 * Note that if this function fails, you must call psa_generator_abort()
4009 * to potentially free embedded data structures and wipe confidential data.
4010 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004011static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004012 const uint8_t *secret,
4013 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004014 psa_algorithm_t hash_alg,
4015 const uint8_t *salt,
4016 size_t salt_length,
4017 const uint8_t *label,
4018 size_t label_length )
4019{
4020 psa_status_t status;
4021 status = psa_hmac_setup_internal( &hkdf->hmac,
4022 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004023 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004024 if( status != PSA_SUCCESS )
4025 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004026 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004027 if( status != PSA_SUCCESS )
4028 return( status );
4029 status = psa_hmac_finish_internal( &hkdf->hmac,
4030 hkdf->prk,
4031 sizeof( hkdf->prk ) );
4032 if( status != PSA_SUCCESS )
4033 return( status );
4034 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4035 hkdf->block_number = 0;
4036 hkdf->info_length = label_length;
4037 if( label_length != 0 )
4038 {
4039 hkdf->info = mbedtls_calloc( 1, label_length );
4040 if( hkdf->info == NULL )
4041 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4042 memcpy( hkdf->info, label, label_length );
4043 }
4044 return( PSA_SUCCESS );
4045}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004046#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004047
Gilles Peskinea05219c2018-11-16 16:02:56 +01004048#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004049/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4050 *
4051 * Note that if this function fails, you must call psa_generator_abort()
4052 * to potentially free embedded data structures and wipe confidential data.
4053 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004054static psa_status_t psa_generator_tls12_prf_setup(
4055 psa_tls12_prf_generator_t *tls12_prf,
4056 const unsigned char *key,
4057 size_t key_len,
4058 psa_algorithm_t hash_alg,
4059 const uint8_t *salt,
4060 size_t salt_length,
4061 const uint8_t *label,
4062 size_t label_length )
4063{
4064 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004065 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4066 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004067
4068 tls12_prf->key = mbedtls_calloc( 1, key_len );
4069 if( tls12_prf->key == NULL )
4070 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4071 tls12_prf->key_len = key_len;
4072 memcpy( tls12_prf->key, key, key_len );
4073
Hanno Becker580fba12018-11-13 20:50:45 +00004074 overflow = ( salt_length + label_length < salt_length ) ||
4075 ( salt_length + label_length + hash_length < hash_length );
4076 if( overflow )
4077 return( PSA_ERROR_INVALID_ARGUMENT );
4078
4079 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4080 if( tls12_prf->Ai_with_seed == NULL )
4081 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4082 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4083
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004084 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4085 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004086 if( label_length != 0 )
4087 {
4088 memcpy( tls12_prf->Ai_with_seed + hash_length,
4089 label, label_length );
4090 }
4091
4092 if( salt_length != 0 )
4093 {
4094 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4095 salt, salt_length );
4096 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004097
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004098 /* The first block gets generated when
4099 * psa_generator_read() is called. */
4100 tls12_prf->block_number = 0;
4101 tls12_prf->offset_in_block = hash_length;
4102
4103 return( PSA_SUCCESS );
4104}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004105
4106/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4107static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4108 psa_tls12_prf_generator_t *tls12_prf,
4109 const unsigned char *psk,
4110 size_t psk_len,
4111 psa_algorithm_t hash_alg,
4112 const uint8_t *salt,
4113 size_t salt_length,
4114 const uint8_t *label,
4115 size_t label_length )
4116{
4117 psa_status_t status;
4118 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4119
4120 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4121 return( PSA_ERROR_INVALID_ARGUMENT );
4122
4123 /* Quoting RFC 4279, Section 2:
4124 *
4125 * The premaster secret is formed as follows: if the PSK is N octets
4126 * long, concatenate a uint16 with the value N, N zero octets, a second
4127 * uint16 with the value N, and the PSK itself.
4128 */
4129
4130 pms[0] = ( psk_len >> 8 ) & 0xff;
4131 pms[1] = ( psk_len >> 0 ) & 0xff;
4132 memset( pms + 2, 0, psk_len );
4133 pms[2 + psk_len + 0] = pms[0];
4134 pms[2 + psk_len + 1] = pms[1];
4135 memcpy( pms + 4 + psk_len, psk, psk_len );
4136
4137 status = psa_generator_tls12_prf_setup( tls12_prf,
4138 pms, 4 + 2 * psk_len,
4139 hash_alg,
4140 salt, salt_length,
4141 label, label_length );
4142
Gilles Peskine3f108122018-12-07 18:14:53 +01004143 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004144 return( status );
4145}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004146#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004147
Gilles Peskine346797d2018-11-16 16:05:06 +01004148/* Note that if this function fails, you must call psa_generator_abort()
4149 * to potentially free embedded data structures and wipe confidential data.
4150 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004151static psa_status_t psa_key_derivation_internal(
4152 psa_crypto_generator_t *generator,
4153 const uint8_t *secret, size_t secret_length,
4154 psa_algorithm_t alg,
4155 const uint8_t *salt, size_t salt_length,
4156 const uint8_t *label, size_t label_length,
4157 size_t capacity )
4158{
4159 psa_status_t status;
4160 size_t max_capacity;
4161
4162 /* Set generator->alg even on failure so that abort knows what to do. */
4163 generator->alg = alg;
4164
Gilles Peskine751d9652018-09-18 12:05:44 +02004165 if( alg == PSA_ALG_SELECT_RAW )
4166 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004167 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004168 if( salt_length != 0 )
4169 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004170 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004171 if( label_length != 0 )
4172 return( PSA_ERROR_INVALID_ARGUMENT );
4173 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4174 if( generator->ctx.buffer.data == NULL )
4175 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4176 memcpy( generator->ctx.buffer.data, secret, secret_length );
4177 generator->ctx.buffer.size = secret_length;
4178 max_capacity = secret_length;
4179 status = PSA_SUCCESS;
4180 }
4181 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004182#if defined(MBEDTLS_MD_C)
4183 if( PSA_ALG_IS_HKDF( alg ) )
4184 {
4185 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4186 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4187 if( hash_size == 0 )
4188 return( PSA_ERROR_NOT_SUPPORTED );
4189 max_capacity = 255 * hash_size;
4190 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4191 secret, secret_length,
4192 hash_alg,
4193 salt, salt_length,
4194 label, label_length );
4195 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004196 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4197 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4198 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004199 {
4200 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4201 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4202
4203 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4204 if( hash_alg != PSA_ALG_SHA_256 &&
4205 hash_alg != PSA_ALG_SHA_384 )
4206 {
4207 return( PSA_ERROR_NOT_SUPPORTED );
4208 }
4209
4210 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004211
4212 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4213 {
4214 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4215 secret, secret_length,
4216 hash_alg, salt, salt_length,
4217 label, label_length );
4218 }
4219 else
4220 {
4221 status = psa_generator_tls12_psk_to_ms_setup(
4222 &generator->ctx.tls12_prf,
4223 secret, secret_length,
4224 hash_alg, salt, salt_length,
4225 label, label_length );
4226 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004227 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004228 else
4229#endif
4230 {
4231 return( PSA_ERROR_NOT_SUPPORTED );
4232 }
4233
4234 if( status != PSA_SUCCESS )
4235 return( status );
4236
4237 if( capacity <= max_capacity )
4238 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004239 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4240 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004241 else
4242 return( PSA_ERROR_INVALID_ARGUMENT );
4243
4244 return( PSA_SUCCESS );
4245}
4246
Gilles Peskineea0fb492018-07-12 17:17:20 +02004247psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004248 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004249 psa_algorithm_t alg,
4250 const uint8_t *salt,
4251 size_t salt_length,
4252 const uint8_t *label,
4253 size_t label_length,
4254 size_t capacity )
4255{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004256 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004257 psa_status_t status;
4258
4259 if( generator->alg != 0 )
4260 return( PSA_ERROR_BAD_STATE );
4261
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004262 /* Make sure that alg is a key derivation algorithm. This prevents
4263 * key selection algorithms, which psa_key_derivation_internal
4264 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004265 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4266 return( PSA_ERROR_INVALID_ARGUMENT );
4267
Gilles Peskinec5487a82018-12-03 18:08:14 +01004268 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004269 if( status != PSA_SUCCESS )
4270 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004271
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004272 if( slot->type != PSA_KEY_TYPE_DERIVE )
4273 return( PSA_ERROR_INVALID_ARGUMENT );
4274
4275 status = psa_key_derivation_internal( generator,
4276 slot->data.raw.data,
4277 slot->data.raw.bytes,
4278 alg,
4279 salt, salt_length,
4280 label, label_length,
4281 capacity );
4282 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004283 psa_generator_abort( generator );
4284 return( status );
4285}
4286
4287
4288
4289/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004290/* Key agreement */
4291/****************************************************************/
4292
Gilles Peskinea05219c2018-11-16 16:02:56 +01004293#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004294static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4295 size_t peer_key_length,
4296 const mbedtls_ecp_keypair *our_key,
4297 uint8_t *shared_secret,
4298 size_t shared_secret_size,
4299 size_t *shared_secret_length )
4300{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004301 mbedtls_ecp_keypair *their_key = NULL;
4302 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004303 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004304 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004305
Jaeden Ameroccdce902019-01-10 11:42:27 +00004306 status = psa_import_ec_public_key(
4307 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4308 peer_key, peer_key_length,
4309 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004310 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004311 goto exit;
4312
Jaeden Amero97271b32019-01-10 19:38:51 +00004313 status = mbedtls_to_psa_error(
4314 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4315 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004316 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004317 status = mbedtls_to_psa_error(
4318 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4319 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004320 goto exit;
4321
Jaeden Amero97271b32019-01-10 19:38:51 +00004322 status = mbedtls_to_psa_error(
4323 mbedtls_ecdh_calc_secret( &ecdh,
4324 shared_secret_length,
4325 shared_secret, shared_secret_size,
4326 mbedtls_ctr_drbg_random,
4327 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004328
4329exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004330 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004331 mbedtls_ecp_keypair_free( their_key );
4332 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004333 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004334}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004335#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004336
Gilles Peskine01d718c2018-09-18 12:01:02 +02004337#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4338
Gilles Peskine346797d2018-11-16 16:05:06 +01004339/* Note that if this function fails, you must call psa_generator_abort()
4340 * to potentially free embedded data structures and wipe confidential data.
4341 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004342static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004343 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004344 const uint8_t *peer_key,
4345 size_t peer_key_length,
4346 psa_algorithm_t alg )
4347{
4348 psa_status_t status;
4349 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4350 size_t shared_secret_length = 0;
4351
4352 /* Step 1: run the secret agreement algorithm to generate the shared
4353 * secret. */
4354 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4355 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004356#if defined(MBEDTLS_ECDH_C)
4357 case PSA_ALG_ECDH_BASE:
4358 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4359 return( PSA_ERROR_INVALID_ARGUMENT );
4360 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4361 private_key->data.ecp,
4362 shared_secret,
4363 sizeof( shared_secret ),
4364 &shared_secret_length );
4365 break;
4366#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004367 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004368 (void) private_key;
4369 (void) peer_key;
4370 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004371 return( PSA_ERROR_NOT_SUPPORTED );
4372 }
4373 if( status != PSA_SUCCESS )
4374 goto exit;
4375
4376 /* Step 2: set up the key derivation to generate key material from
4377 * the shared secret. */
4378 status = psa_key_derivation_internal( generator,
4379 shared_secret, shared_secret_length,
4380 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4381 NULL, 0, NULL, 0,
4382 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4383exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004384 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004385 return( status );
4386}
4387
4388psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004389 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004390 const uint8_t *peer_key,
4391 size_t peer_key_length,
4392 psa_algorithm_t alg )
4393{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004394 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004395 psa_status_t status;
4396 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4397 return( PSA_ERROR_INVALID_ARGUMENT );
4398 status = psa_get_key_from_slot( private_key, &slot,
4399 PSA_KEY_USAGE_DERIVE, alg );
4400 if( status != PSA_SUCCESS )
4401 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004402 status = psa_key_agreement_internal( generator,
4403 slot,
4404 peer_key, peer_key_length,
4405 alg );
4406 if( status != PSA_SUCCESS )
4407 psa_generator_abort( generator );
4408 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004409}
4410
4411
4412
4413/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004414/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004415/****************************************************************/
4416
4417psa_status_t psa_generate_random( uint8_t *output,
4418 size_t output_size )
4419{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004420 int ret;
4421 GUARD_MODULE_INITIALIZED;
4422
4423 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004424 return( mbedtls_to_psa_error( ret ) );
4425}
4426
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004427#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004428
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004429psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4430 size_t seed_size )
4431{
4432 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +02004433 struct psa_storage_info_t p_info;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004434 if( global_data.initialized )
4435 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004436
4437 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4438 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4439 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4440 return( PSA_ERROR_INVALID_ARGUMENT );
4441
David Saadaa2523b22019-02-18 13:56:26 +02004442 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004443
David Saadaa2523b22019-02-18 13:56:26 +02004444 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004445 {
David Saadaa2523b22019-02-18 13:56:26 +02004446 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004447 }
David Saadaa2523b22019-02-18 13:56:26 +02004448 else if( PSA_SUCCESS == status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004449 {
4450 /* You should not be here. Seed needs to be injected only once */
4451 status = PSA_ERROR_NOT_PERMITTED;
4452 }
4453 return( status );
4454}
4455#endif
4456
Gilles Peskinec5487a82018-12-03 18:08:14 +01004457psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004458 psa_key_type_t type,
4459 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004460 const void *extra,
4461 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004463 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004464 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004465
Gilles Peskine53d991e2018-07-12 01:14:59 +02004466 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004467 return( PSA_ERROR_INVALID_ARGUMENT );
4468
Gilles Peskinec5487a82018-12-03 18:08:14 +01004469 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004470 if( status != PSA_SUCCESS )
4471 return( status );
4472
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004473 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004474 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004475 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004476 if( status != PSA_SUCCESS )
4477 return( status );
4478 status = psa_generate_random( slot->data.raw.data,
4479 slot->data.raw.bytes );
4480 if( status != PSA_SUCCESS )
4481 {
4482 mbedtls_free( slot->data.raw.data );
4483 return( status );
4484 }
4485#if defined(MBEDTLS_DES_C)
4486 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004487 psa_des_set_key_parity( slot->data.raw.data,
4488 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004489#endif /* MBEDTLS_DES_C */
4490 }
4491 else
4492
4493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4494 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4495 {
4496 mbedtls_rsa_context *rsa;
4497 int ret;
4498 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004499 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4500 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004501 /* Accept only byte-aligned keys, for the same reasons as
4502 * in psa_import_rsa_key(). */
4503 if( bits % 8 != 0 )
4504 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004505 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004506 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004507 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004508 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004509 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004510#if INT_MAX < 0xffffffff
4511 /* Check that the uint32_t value passed by the caller fits
4512 * in the range supported by this implementation. */
4513 if( p->e > INT_MAX )
4514 return( PSA_ERROR_NOT_SUPPORTED );
4515#endif
4516 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004517 }
4518 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4519 if( rsa == NULL )
4520 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4521 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4522 ret = mbedtls_rsa_gen_key( rsa,
4523 mbedtls_ctr_drbg_random,
4524 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004525 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004526 exponent );
4527 if( ret != 0 )
4528 {
4529 mbedtls_rsa_free( rsa );
4530 mbedtls_free( rsa );
4531 return( mbedtls_to_psa_error( ret ) );
4532 }
4533 slot->data.rsa = rsa;
4534 }
4535 else
4536#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4537
4538#if defined(MBEDTLS_ECP_C)
4539 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4540 {
4541 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4542 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4543 const mbedtls_ecp_curve_info *curve_info =
4544 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4545 mbedtls_ecp_keypair *ecp;
4546 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004547 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004548 return( PSA_ERROR_NOT_SUPPORTED );
4549 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4550 return( PSA_ERROR_NOT_SUPPORTED );
4551 if( curve_info->bit_size != bits )
4552 return( PSA_ERROR_INVALID_ARGUMENT );
4553 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4554 if( ecp == NULL )
4555 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4556 mbedtls_ecp_keypair_init( ecp );
4557 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4558 mbedtls_ctr_drbg_random,
4559 &global_data.ctr_drbg );
4560 if( ret != 0 )
4561 {
4562 mbedtls_ecp_keypair_free( ecp );
4563 mbedtls_free( ecp );
4564 return( mbedtls_to_psa_error( ret ) );
4565 }
4566 slot->data.ecp = ecp;
4567 }
4568 else
4569#endif /* MBEDTLS_ECP_C */
4570
4571 return( PSA_ERROR_NOT_SUPPORTED );
4572
4573 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004574
4575#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4576 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4577 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004578 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004579 }
4580#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4581
4582 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004583}
4584
4585
4586/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004587/* Module setup */
4588/****************************************************************/
4589
Gilles Peskine5e769522018-11-20 21:59:56 +01004590psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4591 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4592 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4593{
4594 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4595 return( PSA_ERROR_BAD_STATE );
4596 global_data.entropy_init = entropy_init;
4597 global_data.entropy_free = entropy_free;
4598 return( PSA_SUCCESS );
4599}
4600
Gilles Peskinee59236f2018-01-27 23:32:46 +01004601void mbedtls_psa_crypto_free( void )
4602{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004603 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004604 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4605 {
4606 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004607 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004608 }
4609 /* Wipe all remaining data, including configuration.
4610 * In particular, this sets all state indicator to the value
4611 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004612 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004613}
4614
4615psa_status_t psa_crypto_init( void )
4616{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004617 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004618 const unsigned char drbg_seed[] = "PSA";
4619
Gilles Peskinec6b69072018-11-20 21:42:52 +01004620 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004621 if( global_data.initialized != 0 )
4622 return( PSA_SUCCESS );
4623
Gilles Peskine5e769522018-11-20 21:59:56 +01004624 /* Set default configuration if
4625 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4626 if( global_data.entropy_init == NULL )
4627 global_data.entropy_init = mbedtls_entropy_init;
4628 if( global_data.entropy_free == NULL )
4629 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004630
Gilles Peskinec6b69072018-11-20 21:42:52 +01004631 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004632 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004633 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004634 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004635 status = mbedtls_to_psa_error(
4636 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4637 mbedtls_entropy_func,
4638 &global_data.entropy,
4639 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4640 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004641 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004642 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004643
Gilles Peskine66fb1262018-12-10 16:29:04 +01004644 status = psa_initialize_key_slots( );
4645 if( status != PSA_SUCCESS )
4646 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004647
4648 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004649 global_data.initialized = 1;
4650
Gilles Peskinee59236f2018-01-27 23:32:46 +01004651exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004652 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004653 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004654 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004655}
4656
4657#endif /* MBEDTLS_PSA_CRYPTO_C */