blob: cd1499a3801353d8a46509f8013b1fb4b6eabb74 [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{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002905 int ret = 0;
2906 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
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 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002926 goto exit;
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 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002931 {
2932 status = PSA_ERROR_NOT_SUPPORTED;
2933 goto exit;
2934 }
mohammad1603503973b2018-03-12 15:59:30 +02002935
mohammad1603503973b2018-03-12 15:59:30 +02002936 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002937 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002938 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002939
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 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002959 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002960
mohammad16038481e742018-03-18 13:57:31 +02002961#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002962 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002963 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002964 case PSA_ALG_CBC_NO_PADDING:
2965 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2966 MBEDTLS_PADDING_NONE );
2967 break;
2968 case PSA_ALG_CBC_PKCS7:
2969 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2970 MBEDTLS_PADDING_PKCS7 );
2971 break;
2972 default:
2973 /* The algorithm doesn't involve padding. */
2974 ret = 0;
2975 break;
2976 }
2977 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002978 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02002979#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2980
mohammad1603503973b2018-03-12 15:59:30 +02002981 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002982 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2983 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2984 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002985 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002986 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002987 }
mohammad1603503973b2018-03-12 15:59:30 +02002988
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002989exit:
2990 if( status == 0 )
2991 status = mbedtls_to_psa_error( ret );
2992 if( status != 0 )
2993 psa_cipher_abort( operation );
2994 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002995}
2996
Gilles Peskinefe119512018-07-08 21:39:34 +02002997psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002998 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002999 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003000{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003001 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003002}
3003
Gilles Peskinefe119512018-07-08 21:39:34 +02003004psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003005 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003006 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003007{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003008 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003009}
3010
Gilles Peskinefe119512018-07-08 21:39:34 +02003011psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3012 unsigned char *iv,
3013 size_t iv_size,
3014 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003015{
itayzafrir534bd7c2018-08-02 13:56:32 +03003016 psa_status_t status;
3017 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003018 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003019 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003020 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003021 }
Moran Peker41deec42018-04-04 15:43:05 +03003022 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003023 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003024 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003025 goto exit;
3026 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003027 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3028 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003029 if( ret != 0 )
3030 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003031 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003032 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003033 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003034
mohammad16038481e742018-03-18 13:57:31 +02003035 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003036 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003037
Moran Peker395db872018-05-31 14:07:14 +03003038exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003039 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003040 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003041 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003042}
3043
Gilles Peskinefe119512018-07-08 21:39:34 +02003044psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3045 const unsigned char *iv,
3046 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003047{
itayzafrir534bd7c2018-08-02 13:56:32 +03003048 psa_status_t status;
3049 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003050 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003051 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003052 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003053 }
Moran Pekera28258c2018-05-29 16:25:04 +03003054 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003055 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003056 status = PSA_ERROR_INVALID_ARGUMENT;
3057 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003058 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003059 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3060 status = mbedtls_to_psa_error( ret );
3061exit:
3062 if( status == PSA_SUCCESS )
3063 operation->iv_set = 1;
3064 else
mohammad1603503973b2018-03-12 15:59:30 +02003065 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003066 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003067}
3068
Gilles Peskinee553c652018-06-04 16:22:46 +02003069psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3070 const uint8_t *input,
3071 size_t input_length,
3072 unsigned char *output,
3073 size_t output_size,
3074 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003075{
itayzafrir534bd7c2018-08-02 13:56:32 +03003076 psa_status_t status;
3077 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003078 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003079
3080 if( operation->alg == 0 )
3081 {
3082 return( PSA_ERROR_BAD_STATE );
3083 }
3084
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003085 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003086 {
3087 /* Take the unprocessed partial block left over from previous
3088 * update calls, if any, plus the input to this call. Remove
3089 * the last partial block, if any. You get the data that will be
3090 * output in this call. */
3091 expected_output_size =
3092 ( operation->ctx.cipher.unprocessed_len + input_length )
3093 / operation->block_size * operation->block_size;
3094 }
3095 else
3096 {
3097 expected_output_size = input_length;
3098 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003099
Gilles Peskine89d789c2018-06-04 17:17:16 +02003100 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003101 {
3102 status = PSA_ERROR_BUFFER_TOO_SMALL;
3103 goto exit;
3104 }
mohammad160382759612018-03-12 18:16:40 +02003105
mohammad1603503973b2018-03-12 15:59:30 +02003106 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003107 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003108 status = mbedtls_to_psa_error( ret );
3109exit:
3110 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003111 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003112 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003113}
3114
Gilles Peskinee553c652018-06-04 16:22:46 +02003115psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3116 uint8_t *output,
3117 size_t output_size,
3118 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003119{
David Saadab4ecc272019-02-14 13:48:10 +02003120 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003121 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003122 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003123
mohammad1603503973b2018-03-12 15:59:30 +02003124 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003125 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003126 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003127 }
3128 if( operation->iv_required && ! operation->iv_set )
3129 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003130 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003131 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003132
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003133 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003134 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3135 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003136 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003137 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003138 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003139 }
3140
Janos Follath315b51c2018-07-09 16:04:51 +01003141 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3142 temp_output_buffer,
3143 output_length );
3144 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003145 {
Janos Follath315b51c2018-07-09 16:04:51 +01003146 status = mbedtls_to_psa_error( cipher_ret );
3147 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003148 }
Janos Follath315b51c2018-07-09 16:04:51 +01003149
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003150 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003151 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003152 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003153 memcpy( output, temp_output_buffer, *output_length );
3154 else
3155 {
Janos Follath315b51c2018-07-09 16:04:51 +01003156 status = PSA_ERROR_BUFFER_TOO_SMALL;
3157 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003158 }
mohammad1603503973b2018-03-12 15:59:30 +02003159
Gilles Peskine3f108122018-12-07 18:14:53 +01003160 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003161 status = psa_cipher_abort( operation );
3162
3163 return( status );
3164
3165error:
3166
3167 *output_length = 0;
3168
Gilles Peskine3f108122018-12-07 18:14:53 +01003169 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003170 (void) psa_cipher_abort( operation );
3171
3172 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003173}
3174
Gilles Peskinee553c652018-06-04 16:22:46 +02003175psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3176{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003178 {
3179 /* The object has (apparently) been initialized but it is not
3180 * in use. It's ok to call abort on such an object, and there's
3181 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003182 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003183 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003184
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003185 /* Sanity check (shouldn't happen: operation->alg should
3186 * always have been initialized to a valid value). */
3187 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3188 return( PSA_ERROR_BAD_STATE );
3189
mohammad1603503973b2018-03-12 15:59:30 +02003190 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003191
Moran Peker41deec42018-04-04 15:43:05 +03003192 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003193 operation->key_set = 0;
3194 operation->iv_set = 0;
3195 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003196 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003197 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003198
Moran Peker395db872018-05-31 14:07:14 +03003199 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003200}
3201
Gilles Peskinea0655c32018-04-30 17:06:50 +02003202
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003203
mohammad16038cc1cee2018-03-28 01:21:33 +03003204/****************************************************************/
3205/* Key Policy */
3206/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003207
mohammad160327010052018-07-03 13:16:15 +03003208#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003209void psa_key_policy_set_usage( psa_key_policy_t *policy,
3210 psa_key_usage_t usage,
3211 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003212{
mohammad16034eed7572018-03-28 05:14:59 -07003213 policy->usage = usage;
3214 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003215}
3216
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003217psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003218{
mohammad16036df908f2018-04-02 08:34:15 -07003219 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003220}
3221
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003222psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003223{
mohammad16036df908f2018-04-02 08:34:15 -07003224 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003225}
mohammad160327010052018-07-03 13:16:15 +03003226#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003227
Gilles Peskinec5487a82018-12-03 18:08:14 +01003228psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003229 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003230{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003231 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003232 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003233
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003234 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003236
Gilles Peskinec5487a82018-12-03 18:08:14 +01003237 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003238 if( status != PSA_SUCCESS )
3239 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003240
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003241 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3242 PSA_KEY_USAGE_ENCRYPT |
3243 PSA_KEY_USAGE_DECRYPT |
3244 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003245 PSA_KEY_USAGE_VERIFY |
3246 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003247 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003248
mohammad16036df908f2018-04-02 08:34:15 -07003249 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003250
3251 return( PSA_SUCCESS );
3252}
3253
Gilles Peskinec5487a82018-12-03 18:08:14 +01003254psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003255 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003256{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003257 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003258 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003259
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003260 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003261 return( PSA_ERROR_INVALID_ARGUMENT );
3262
Gilles Peskinec5487a82018-12-03 18:08:14 +01003263 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003264 if( status != PSA_SUCCESS )
3265 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003266
mohammad16036df908f2018-04-02 08:34:15 -07003267 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003268
3269 return( PSA_SUCCESS );
3270}
Gilles Peskine20035e32018-02-03 22:44:14 +01003271
Gilles Peskinea0655c32018-04-30 17:06:50 +02003272
3273
mohammad1603804cd712018-03-20 22:44:08 +02003274/****************************************************************/
3275/* Key Lifetime */
3276/****************************************************************/
3277
Gilles Peskinec5487a82018-12-03 18:08:14 +01003278psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003279 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003280{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003281 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003282 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003283
Gilles Peskinec5487a82018-12-03 18:08:14 +01003284 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003285 if( status != PSA_SUCCESS )
3286 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003287
mohammad1603804cd712018-03-20 22:44:08 +02003288 *lifetime = slot->lifetime;
3289
3290 return( PSA_SUCCESS );
3291}
3292
Gilles Peskine20035e32018-02-03 22:44:14 +01003293
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003294
mohammad16035955c982018-04-26 00:53:03 +03003295/****************************************************************/
3296/* AEAD */
3297/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003298
Gilles Peskineedf9a652018-08-17 18:11:56 +02003299typedef struct
3300{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003301 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003302 const mbedtls_cipher_info_t *cipher_info;
3303 union
3304 {
3305#if defined(MBEDTLS_CCM_C)
3306 mbedtls_ccm_context ccm;
3307#endif /* MBEDTLS_CCM_C */
3308#if defined(MBEDTLS_GCM_C)
3309 mbedtls_gcm_context gcm;
3310#endif /* MBEDTLS_GCM_C */
3311 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003312 psa_algorithm_t core_alg;
3313 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003314 uint8_t tag_length;
3315} aead_operation_t;
3316
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003317static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003318{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003319 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003320 {
3321#if defined(MBEDTLS_CCM_C)
3322 case PSA_ALG_CCM:
3323 mbedtls_ccm_free( &operation->ctx.ccm );
3324 break;
3325#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003326#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003327 case PSA_ALG_GCM:
3328 mbedtls_gcm_free( &operation->ctx.gcm );
3329 break;
3330#endif /* MBEDTLS_GCM_C */
3331 }
3332}
3333
3334static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003335 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003336 psa_key_usage_t usage,
3337 psa_algorithm_t alg )
3338{
3339 psa_status_t status;
3340 size_t key_bits;
3341 mbedtls_cipher_id_t cipher_id;
3342
Gilles Peskinec5487a82018-12-03 18:08:14 +01003343 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003344 if( status != PSA_SUCCESS )
3345 return( status );
3346
3347 key_bits = psa_get_key_bits( operation->slot );
3348
3349 operation->cipher_info =
3350 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3351 &cipher_id );
3352 if( operation->cipher_info == NULL )
3353 return( PSA_ERROR_NOT_SUPPORTED );
3354
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003355 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003356 {
3357#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003358 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3359 operation->core_alg = PSA_ALG_CCM;
3360 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003361 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3362 return( PSA_ERROR_INVALID_ARGUMENT );
3363 mbedtls_ccm_init( &operation->ctx.ccm );
3364 status = mbedtls_to_psa_error(
3365 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3366 operation->slot->data.raw.data,
3367 (unsigned int) key_bits ) );
3368 if( status != 0 )
3369 goto cleanup;
3370 break;
3371#endif /* MBEDTLS_CCM_C */
3372
3373#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003374 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3375 operation->core_alg = PSA_ALG_GCM;
3376 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003377 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3378 return( PSA_ERROR_INVALID_ARGUMENT );
3379 mbedtls_gcm_init( &operation->ctx.gcm );
3380 status = mbedtls_to_psa_error(
3381 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3382 operation->slot->data.raw.data,
3383 (unsigned int) key_bits ) );
3384 break;
3385#endif /* MBEDTLS_GCM_C */
3386
3387 default:
3388 return( PSA_ERROR_NOT_SUPPORTED );
3389 }
3390
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003391 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3392 {
3393 status = PSA_ERROR_INVALID_ARGUMENT;
3394 goto cleanup;
3395 }
3396 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3397 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3398 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3399 * In both cases, mbedtls_xxx will validate the tag length below. */
3400
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 return( PSA_SUCCESS );
3402
3403cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003404 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003405 return( status );
3406}
3407
Gilles Peskinec5487a82018-12-03 18:08:14 +01003408psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003409 psa_algorithm_t alg,
3410 const uint8_t *nonce,
3411 size_t nonce_length,
3412 const uint8_t *additional_data,
3413 size_t additional_data_length,
3414 const uint8_t *plaintext,
3415 size_t plaintext_length,
3416 uint8_t *ciphertext,
3417 size_t ciphertext_size,
3418 size_t *ciphertext_length )
3419{
mohammad16035955c982018-04-26 00:53:03 +03003420 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003421 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003422 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003423
mohammad1603f08a5502018-06-03 15:05:47 +03003424 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003425
Gilles Peskinec5487a82018-12-03 18:08:14 +01003426 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003427 if( status != PSA_SUCCESS )
3428 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003429
Gilles Peskineedf9a652018-08-17 18:11:56 +02003430 /* For all currently supported modes, the tag is at the end of the
3431 * ciphertext. */
3432 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3433 {
3434 status = PSA_ERROR_BUFFER_TOO_SMALL;
3435 goto exit;
3436 }
3437 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003438
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003439#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003440 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003441 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003442 status = mbedtls_to_psa_error(
3443 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3444 MBEDTLS_GCM_ENCRYPT,
3445 plaintext_length,
3446 nonce, nonce_length,
3447 additional_data, additional_data_length,
3448 plaintext, ciphertext,
3449 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003450 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003451 else
3452#endif /* MBEDTLS_GCM_C */
3453#if defined(MBEDTLS_CCM_C)
3454 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003455 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003456 status = mbedtls_to_psa_error(
3457 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3458 plaintext_length,
3459 nonce, nonce_length,
3460 additional_data,
3461 additional_data_length,
3462 plaintext, ciphertext,
3463 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003464 }
mohammad16035c8845f2018-05-09 05:40:09 -07003465 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003466#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003467 {
mohammad1603554faad2018-06-03 15:07:38 +03003468 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003469 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003470
Gilles Peskineedf9a652018-08-17 18:11:56 +02003471 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3472 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003473
Gilles Peskineedf9a652018-08-17 18:11:56 +02003474exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003475 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003476 if( status == PSA_SUCCESS )
3477 *ciphertext_length = plaintext_length + operation.tag_length;
3478 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003479}
3480
Gilles Peskineee652a32018-06-01 19:23:52 +02003481/* Locate the tag in a ciphertext buffer containing the encrypted data
3482 * followed by the tag. Return the length of the part preceding the tag in
3483 * *plaintext_length. This is the size of the plaintext in modes where
3484 * the encrypted data has the same size as the plaintext, such as
3485 * CCM and GCM. */
3486static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3487 const uint8_t *ciphertext,
3488 size_t ciphertext_length,
3489 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003490 const uint8_t **p_tag )
3491{
3492 size_t payload_length;
3493 if( tag_length > ciphertext_length )
3494 return( PSA_ERROR_INVALID_ARGUMENT );
3495 payload_length = ciphertext_length - tag_length;
3496 if( payload_length > plaintext_size )
3497 return( PSA_ERROR_BUFFER_TOO_SMALL );
3498 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003499 return( PSA_SUCCESS );
3500}
3501
Gilles Peskinec5487a82018-12-03 18:08:14 +01003502psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003503 psa_algorithm_t alg,
3504 const uint8_t *nonce,
3505 size_t nonce_length,
3506 const uint8_t *additional_data,
3507 size_t additional_data_length,
3508 const uint8_t *ciphertext,
3509 size_t ciphertext_length,
3510 uint8_t *plaintext,
3511 size_t plaintext_size,
3512 size_t *plaintext_length )
3513{
mohammad16035955c982018-04-26 00:53:03 +03003514 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003515 aead_operation_t operation;
3516 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003517
Gilles Peskineee652a32018-06-01 19:23:52 +02003518 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003519
Gilles Peskinec5487a82018-12-03 18:08:14 +01003520 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003521 if( status != PSA_SUCCESS )
3522 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003523
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003524#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003525 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003526 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003527 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003528 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003529 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003530 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003531 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003532
Gilles Peskineedf9a652018-08-17 18:11:56 +02003533 status = mbedtls_to_psa_error(
3534 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3535 ciphertext_length - operation.tag_length,
3536 nonce, nonce_length,
3537 additional_data,
3538 additional_data_length,
3539 tag, operation.tag_length,
3540 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003541 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003542 else
3543#endif /* MBEDTLS_GCM_C */
3544#if defined(MBEDTLS_CCM_C)
3545 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003546 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003547 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003548 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003549 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003550 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003551 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003552
Gilles Peskineedf9a652018-08-17 18:11:56 +02003553 status = mbedtls_to_psa_error(
3554 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3555 ciphertext_length - operation.tag_length,
3556 nonce, nonce_length,
3557 additional_data,
3558 additional_data_length,
3559 ciphertext, plaintext,
3560 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003561 }
mohammad160339574652018-06-01 04:39:53 -07003562 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003563#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003564 {
mohammad1603554faad2018-06-03 15:07:38 +03003565 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003566 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003567
Gilles Peskineedf9a652018-08-17 18:11:56 +02003568 if( status != PSA_SUCCESS && plaintext_size != 0 )
3569 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003570
Gilles Peskineedf9a652018-08-17 18:11:56 +02003571exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003572 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003573 if( status == PSA_SUCCESS )
3574 *plaintext_length = ciphertext_length - operation.tag_length;
3575 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003576}
3577
Gilles Peskinea0655c32018-04-30 17:06:50 +02003578
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003579
Gilles Peskine20035e32018-02-03 22:44:14 +01003580/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003581/* Generators */
3582/****************************************************************/
3583
3584psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3585{
3586 psa_status_t status = PSA_SUCCESS;
3587 if( generator->alg == 0 )
3588 {
3589 /* The object has (apparently) been initialized but it is not
3590 * in use. It's ok to call abort on such an object, and there's
3591 * nothing to do. */
3592 }
3593 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003594 if( generator->alg == PSA_ALG_SELECT_RAW )
3595 {
3596 if( generator->ctx.buffer.data != NULL )
3597 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003598 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003599 generator->ctx.buffer.size );
3600 mbedtls_free( generator->ctx.buffer.data );
3601 }
3602 }
3603 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003604#if defined(MBEDTLS_MD_C)
3605 if( PSA_ALG_IS_HKDF( generator->alg ) )
3606 {
3607 mbedtls_free( generator->ctx.hkdf.info );
3608 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3609 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003610 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3611 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3612 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003613 {
3614 if( generator->ctx.tls12_prf.key != NULL )
3615 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003616 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003617 generator->ctx.tls12_prf.key_len );
3618 mbedtls_free( generator->ctx.tls12_prf.key );
3619 }
Hanno Becker580fba12018-11-13 20:50:45 +00003620
3621 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3622 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003623 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003624 generator->ctx.tls12_prf.Ai_with_seed_len );
3625 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3626 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003627 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003628 else
3629#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003630 {
3631 status = PSA_ERROR_BAD_STATE;
3632 }
3633 memset( generator, 0, sizeof( *generator ) );
3634 return( status );
3635}
3636
3637
3638psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3639 size_t *capacity)
3640{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003641 if( generator->alg == 0 )
3642 {
3643 /* This is a blank generator. */
3644 return PSA_ERROR_BAD_STATE;
3645 }
3646
Gilles Peskineeab56e42018-07-12 17:12:33 +02003647 *capacity = generator->capacity;
3648 return( PSA_SUCCESS );
3649}
3650
Gilles Peskinebef7f142018-07-12 17:22:21 +02003651#if defined(MBEDTLS_MD_C)
3652/* Read some bytes from an HKDF-based generator. This performs a chunk
3653 * of the expand phase of the HKDF algorithm. */
3654static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3655 psa_algorithm_t hash_alg,
3656 uint8_t *output,
3657 size_t output_length )
3658{
3659 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3660 psa_status_t status;
3661
3662 while( output_length != 0 )
3663 {
3664 /* Copy what remains of the current block */
3665 uint8_t n = hash_length - hkdf->offset_in_block;
3666 if( n > output_length )
3667 n = (uint8_t) output_length;
3668 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3669 output += n;
3670 output_length -= n;
3671 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003672 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003673 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003674 /* We can't be wanting more output after block 0xff, otherwise
3675 * the capacity check in psa_generator_read() would have
3676 * prevented this call. It could happen only if the generator
3677 * object was corrupted or if this function is called directly
3678 * inside the library. */
3679 if( hkdf->block_number == 0xff )
3680 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003681
3682 /* We need a new block */
3683 ++hkdf->block_number;
3684 hkdf->offset_in_block = 0;
3685 status = psa_hmac_setup_internal( &hkdf->hmac,
3686 hkdf->prk, hash_length,
3687 hash_alg );
3688 if( status != PSA_SUCCESS )
3689 return( status );
3690 if( hkdf->block_number != 1 )
3691 {
3692 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3693 hkdf->output_block,
3694 hash_length );
3695 if( status != PSA_SUCCESS )
3696 return( status );
3697 }
3698 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3699 hkdf->info,
3700 hkdf->info_length );
3701 if( status != PSA_SUCCESS )
3702 return( status );
3703 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3704 &hkdf->block_number, 1 );
3705 if( status != PSA_SUCCESS )
3706 return( status );
3707 status = psa_hmac_finish_internal( &hkdf->hmac,
3708 hkdf->output_block,
3709 sizeof( hkdf->output_block ) );
3710 if( status != PSA_SUCCESS )
3711 return( status );
3712 }
3713
3714 return( PSA_SUCCESS );
3715}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003716
3717static psa_status_t psa_generator_tls12_prf_generate_next_block(
3718 psa_tls12_prf_generator_t *tls12_prf,
3719 psa_algorithm_t alg )
3720{
3721 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3722 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3723 psa_hmac_internal_data hmac;
3724 psa_status_t status, cleanup_status;
3725
Hanno Becker3b339e22018-11-13 20:56:14 +00003726 unsigned char *Ai;
3727 size_t Ai_len;
3728
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003729 /* We can't be wanting more output after block 0xff, otherwise
3730 * the capacity check in psa_generator_read() would have
3731 * prevented this call. It could happen only if the generator
3732 * object was corrupted or if this function is called directly
3733 * inside the library. */
3734 if( tls12_prf->block_number == 0xff )
3735 return( PSA_ERROR_BAD_STATE );
3736
3737 /* We need a new block */
3738 ++tls12_prf->block_number;
3739 tls12_prf->offset_in_block = 0;
3740
3741 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3742 *
3743 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3744 *
3745 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3746 * HMAC_hash(secret, A(2) + seed) +
3747 * HMAC_hash(secret, A(3) + seed) + ...
3748 *
3749 * A(0) = seed
3750 * A(i) = HMAC_hash( secret, A(i-1) )
3751 *
3752 * The `psa_tls12_prf_generator` structures saves the block
3753 * `HMAC_hash(secret, A(i) + seed)` from which the output
3754 * is currently extracted as `output_block`, while
3755 * `A(i) + seed` is stored in `Ai_with_seed`.
3756 *
3757 * Generating a new block means recalculating `Ai_with_seed`
3758 * from the A(i)-part of it, and afterwards recalculating
3759 * `output_block`.
3760 *
3761 * A(0) is computed at setup time.
3762 *
3763 */
3764
3765 psa_hmac_init_internal( &hmac );
3766
3767 /* We must distinguish the calculation of A(1) from those
3768 * of A(2) and higher, because A(0)=seed has a different
3769 * length than the other A(i). */
3770 if( tls12_prf->block_number == 1 )
3771 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003772 Ai = tls12_prf->Ai_with_seed + hash_length;
3773 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003774 }
3775 else
3776 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003777 Ai = tls12_prf->Ai_with_seed;
3778 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003779 }
3780
Hanno Becker3b339e22018-11-13 20:56:14 +00003781 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3782 status = psa_hmac_setup_internal( &hmac,
3783 tls12_prf->key,
3784 tls12_prf->key_len,
3785 hash_alg );
3786 if( status != PSA_SUCCESS )
3787 goto cleanup;
3788
3789 status = psa_hash_update( &hmac.hash_ctx,
3790 Ai, Ai_len );
3791 if( status != PSA_SUCCESS )
3792 goto cleanup;
3793
3794 status = psa_hmac_finish_internal( &hmac,
3795 tls12_prf->Ai_with_seed,
3796 hash_length );
3797 if( status != PSA_SUCCESS )
3798 goto cleanup;
3799
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003800 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3801 status = psa_hmac_setup_internal( &hmac,
3802 tls12_prf->key,
3803 tls12_prf->key_len,
3804 hash_alg );
3805 if( status != PSA_SUCCESS )
3806 goto cleanup;
3807
3808 status = psa_hash_update( &hmac.hash_ctx,
3809 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003810 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003811 if( status != PSA_SUCCESS )
3812 goto cleanup;
3813
3814 status = psa_hmac_finish_internal( &hmac,
3815 tls12_prf->output_block,
3816 hash_length );
3817 if( status != PSA_SUCCESS )
3818 goto cleanup;
3819
3820cleanup:
3821
3822 cleanup_status = psa_hmac_abort_internal( &hmac );
3823 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3824 status = cleanup_status;
3825
3826 return( status );
3827}
3828
3829/* Read some bytes from an TLS-1.2-PRF-based generator.
3830 * See Section 5 of RFC 5246. */
3831static psa_status_t psa_generator_tls12_prf_read(
3832 psa_tls12_prf_generator_t *tls12_prf,
3833 psa_algorithm_t alg,
3834 uint8_t *output,
3835 size_t output_length )
3836{
3837 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3838 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3839 psa_status_t status;
3840
3841 while( output_length != 0 )
3842 {
3843 /* Copy what remains of the current block */
3844 uint8_t n = hash_length - tls12_prf->offset_in_block;
3845
3846 /* Check if we have fully processed the current block. */
3847 if( n == 0 )
3848 {
3849 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3850 alg );
3851 if( status != PSA_SUCCESS )
3852 return( status );
3853
3854 continue;
3855 }
3856
3857 if( n > output_length )
3858 n = (uint8_t) output_length;
3859 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3860 n );
3861 output += n;
3862 output_length -= n;
3863 tls12_prf->offset_in_block += n;
3864 }
3865
3866 return( PSA_SUCCESS );
3867}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003868#endif /* MBEDTLS_MD_C */
3869
Gilles Peskineeab56e42018-07-12 17:12:33 +02003870psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3871 uint8_t *output,
3872 size_t output_length )
3873{
3874 psa_status_t status;
3875
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003876 if( generator->alg == 0 )
3877 {
3878 /* This is a blank generator. */
3879 return PSA_ERROR_BAD_STATE;
3880 }
3881
Gilles Peskineeab56e42018-07-12 17:12:33 +02003882 if( output_length > generator->capacity )
3883 {
3884 generator->capacity = 0;
3885 /* Go through the error path to wipe all confidential data now
3886 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003887 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003888 goto exit;
3889 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003890 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003891 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003892 /* Edge case: this is a finished generator, and 0 bytes
3893 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003894 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3895 * INSUFFICIENT_CAPACITY, which is right for a finished
3896 * generator, for consistency with the case when
3897 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003898 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003899 }
3900 generator->capacity -= output_length;
3901
Gilles Peskine751d9652018-09-18 12:05:44 +02003902 if( generator->alg == PSA_ALG_SELECT_RAW )
3903 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003904 /* Initially, the capacity of a selection generator is always
3905 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3906 * abbreviated in this comment as `size`. When the remaining
3907 * capacity is `c`, the next bytes to serve start `c` bytes
3908 * from the end of the buffer, i.e. `size - c` from the
3909 * beginning of the buffer. Since `generator->capacity` was just
3910 * decremented above, we need to serve the bytes from
3911 * `size - generator->capacity - output_length` to
3912 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003913 size_t offset =
3914 generator->ctx.buffer.size - generator->capacity - output_length;
3915 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3916 status = PSA_SUCCESS;
3917 }
3918 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003919#if defined(MBEDTLS_MD_C)
3920 if( PSA_ALG_IS_HKDF( generator->alg ) )
3921 {
3922 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3923 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3924 output, output_length );
3925 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003926 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3927 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003928 {
3929 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3930 generator->alg, output,
3931 output_length );
3932 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003933 else
3934#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003935 {
3936 return( PSA_ERROR_BAD_STATE );
3937 }
3938
3939exit:
3940 if( status != PSA_SUCCESS )
3941 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003942 /* Preserve the algorithm upon errors, but clear all sensitive state.
3943 * This allows us to differentiate between exhausted generators and
3944 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3945 * generators. */
3946 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003947 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003948 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003949 memset( output, '!', output_length );
3950 }
3951 return( status );
3952}
3953
Gilles Peskine08542d82018-07-19 17:05:42 +02003954#if defined(MBEDTLS_DES_C)
3955static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3956{
3957 if( data_size >= 8 )
3958 mbedtls_des_key_set_parity( data );
3959 if( data_size >= 16 )
3960 mbedtls_des_key_set_parity( data + 8 );
3961 if( data_size >= 24 )
3962 mbedtls_des_key_set_parity( data + 16 );
3963}
3964#endif /* MBEDTLS_DES_C */
3965
Gilles Peskinec5487a82018-12-03 18:08:14 +01003966psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003967 psa_key_type_t type,
3968 size_t bits,
3969 psa_crypto_generator_t *generator )
3970{
3971 uint8_t *data = NULL;
3972 size_t bytes = PSA_BITS_TO_BYTES( bits );
3973 psa_status_t status;
3974
3975 if( ! key_type_is_raw_bytes( type ) )
3976 return( PSA_ERROR_INVALID_ARGUMENT );
3977 if( bits % 8 != 0 )
3978 return( PSA_ERROR_INVALID_ARGUMENT );
3979 data = mbedtls_calloc( 1, bytes );
3980 if( data == NULL )
3981 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3982
3983 status = psa_generator_read( generator, data, bytes );
3984 if( status != PSA_SUCCESS )
3985 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003986#if defined(MBEDTLS_DES_C)
3987 if( type == PSA_KEY_TYPE_DES )
3988 psa_des_set_key_parity( data, bytes );
3989#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003990 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003991
3992exit:
3993 mbedtls_free( data );
3994 return( status );
3995}
3996
3997
3998
3999/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004000/* Key derivation */
4001/****************************************************************/
4002
Gilles Peskinea05219c2018-11-16 16:02:56 +01004003#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004004/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004005 * of the HKDF algorithm.
4006 *
4007 * Note that if this function fails, you must call psa_generator_abort()
4008 * to potentially free embedded data structures and wipe confidential data.
4009 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004010static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004011 const uint8_t *secret,
4012 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004013 psa_algorithm_t hash_alg,
4014 const uint8_t *salt,
4015 size_t salt_length,
4016 const uint8_t *label,
4017 size_t label_length )
4018{
4019 psa_status_t status;
4020 status = psa_hmac_setup_internal( &hkdf->hmac,
4021 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004022 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004023 if( status != PSA_SUCCESS )
4024 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004025 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004026 if( status != PSA_SUCCESS )
4027 return( status );
4028 status = psa_hmac_finish_internal( &hkdf->hmac,
4029 hkdf->prk,
4030 sizeof( hkdf->prk ) );
4031 if( status != PSA_SUCCESS )
4032 return( status );
4033 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4034 hkdf->block_number = 0;
4035 hkdf->info_length = label_length;
4036 if( label_length != 0 )
4037 {
4038 hkdf->info = mbedtls_calloc( 1, label_length );
4039 if( hkdf->info == NULL )
4040 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4041 memcpy( hkdf->info, label, label_length );
4042 }
4043 return( PSA_SUCCESS );
4044}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004045#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004046
Gilles Peskinea05219c2018-11-16 16:02:56 +01004047#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004048/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4049 *
4050 * Note that if this function fails, you must call psa_generator_abort()
4051 * to potentially free embedded data structures and wipe confidential data.
4052 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004053static psa_status_t psa_generator_tls12_prf_setup(
4054 psa_tls12_prf_generator_t *tls12_prf,
4055 const unsigned char *key,
4056 size_t key_len,
4057 psa_algorithm_t hash_alg,
4058 const uint8_t *salt,
4059 size_t salt_length,
4060 const uint8_t *label,
4061 size_t label_length )
4062{
4063 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004064 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4065 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004066
4067 tls12_prf->key = mbedtls_calloc( 1, key_len );
4068 if( tls12_prf->key == NULL )
4069 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4070 tls12_prf->key_len = key_len;
4071 memcpy( tls12_prf->key, key, key_len );
4072
Hanno Becker580fba12018-11-13 20:50:45 +00004073 overflow = ( salt_length + label_length < salt_length ) ||
4074 ( salt_length + label_length + hash_length < hash_length );
4075 if( overflow )
4076 return( PSA_ERROR_INVALID_ARGUMENT );
4077
4078 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4079 if( tls12_prf->Ai_with_seed == NULL )
4080 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4081 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4082
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004083 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4084 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004085 if( label_length != 0 )
4086 {
4087 memcpy( tls12_prf->Ai_with_seed + hash_length,
4088 label, label_length );
4089 }
4090
4091 if( salt_length != 0 )
4092 {
4093 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4094 salt, salt_length );
4095 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004096
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004097 /* The first block gets generated when
4098 * psa_generator_read() is called. */
4099 tls12_prf->block_number = 0;
4100 tls12_prf->offset_in_block = hash_length;
4101
4102 return( PSA_SUCCESS );
4103}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004104
4105/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4106static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4107 psa_tls12_prf_generator_t *tls12_prf,
4108 const unsigned char *psk,
4109 size_t psk_len,
4110 psa_algorithm_t hash_alg,
4111 const uint8_t *salt,
4112 size_t salt_length,
4113 const uint8_t *label,
4114 size_t label_length )
4115{
4116 psa_status_t status;
4117 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4118
4119 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4120 return( PSA_ERROR_INVALID_ARGUMENT );
4121
4122 /* Quoting RFC 4279, Section 2:
4123 *
4124 * The premaster secret is formed as follows: if the PSK is N octets
4125 * long, concatenate a uint16 with the value N, N zero octets, a second
4126 * uint16 with the value N, and the PSK itself.
4127 */
4128
4129 pms[0] = ( psk_len >> 8 ) & 0xff;
4130 pms[1] = ( psk_len >> 0 ) & 0xff;
4131 memset( pms + 2, 0, psk_len );
4132 pms[2 + psk_len + 0] = pms[0];
4133 pms[2 + psk_len + 1] = pms[1];
4134 memcpy( pms + 4 + psk_len, psk, psk_len );
4135
4136 status = psa_generator_tls12_prf_setup( tls12_prf,
4137 pms, 4 + 2 * psk_len,
4138 hash_alg,
4139 salt, salt_length,
4140 label, label_length );
4141
Gilles Peskine3f108122018-12-07 18:14:53 +01004142 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004143 return( status );
4144}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004145#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004146
Gilles Peskine346797d2018-11-16 16:05:06 +01004147/* Note that if this function fails, you must call psa_generator_abort()
4148 * to potentially free embedded data structures and wipe confidential data.
4149 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004150static psa_status_t psa_key_derivation_internal(
4151 psa_crypto_generator_t *generator,
4152 const uint8_t *secret, size_t secret_length,
4153 psa_algorithm_t alg,
4154 const uint8_t *salt, size_t salt_length,
4155 const uint8_t *label, size_t label_length,
4156 size_t capacity )
4157{
4158 psa_status_t status;
4159 size_t max_capacity;
4160
4161 /* Set generator->alg even on failure so that abort knows what to do. */
4162 generator->alg = alg;
4163
Gilles Peskine751d9652018-09-18 12:05:44 +02004164 if( alg == PSA_ALG_SELECT_RAW )
4165 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004166 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004167 if( salt_length != 0 )
4168 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004169 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004170 if( label_length != 0 )
4171 return( PSA_ERROR_INVALID_ARGUMENT );
4172 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4173 if( generator->ctx.buffer.data == NULL )
4174 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4175 memcpy( generator->ctx.buffer.data, secret, secret_length );
4176 generator->ctx.buffer.size = secret_length;
4177 max_capacity = secret_length;
4178 status = PSA_SUCCESS;
4179 }
4180 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004181#if defined(MBEDTLS_MD_C)
4182 if( PSA_ALG_IS_HKDF( alg ) )
4183 {
4184 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4185 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4186 if( hash_size == 0 )
4187 return( PSA_ERROR_NOT_SUPPORTED );
4188 max_capacity = 255 * hash_size;
4189 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4190 secret, secret_length,
4191 hash_alg,
4192 salt, salt_length,
4193 label, label_length );
4194 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004195 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4196 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4197 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004198 {
4199 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4200 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4201
4202 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4203 if( hash_alg != PSA_ALG_SHA_256 &&
4204 hash_alg != PSA_ALG_SHA_384 )
4205 {
4206 return( PSA_ERROR_NOT_SUPPORTED );
4207 }
4208
4209 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004210
4211 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4212 {
4213 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4214 secret, secret_length,
4215 hash_alg, salt, salt_length,
4216 label, label_length );
4217 }
4218 else
4219 {
4220 status = psa_generator_tls12_psk_to_ms_setup(
4221 &generator->ctx.tls12_prf,
4222 secret, secret_length,
4223 hash_alg, salt, salt_length,
4224 label, label_length );
4225 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004226 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004227 else
4228#endif
4229 {
4230 return( PSA_ERROR_NOT_SUPPORTED );
4231 }
4232
4233 if( status != PSA_SUCCESS )
4234 return( status );
4235
4236 if( capacity <= max_capacity )
4237 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004238 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4239 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004240 else
4241 return( PSA_ERROR_INVALID_ARGUMENT );
4242
4243 return( PSA_SUCCESS );
4244}
4245
Gilles Peskineea0fb492018-07-12 17:17:20 +02004246psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004247 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004248 psa_algorithm_t alg,
4249 const uint8_t *salt,
4250 size_t salt_length,
4251 const uint8_t *label,
4252 size_t label_length,
4253 size_t capacity )
4254{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004255 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004256 psa_status_t status;
4257
4258 if( generator->alg != 0 )
4259 return( PSA_ERROR_BAD_STATE );
4260
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004261 /* Make sure that alg is a key derivation algorithm. This prevents
4262 * key selection algorithms, which psa_key_derivation_internal
4263 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004264 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4265 return( PSA_ERROR_INVALID_ARGUMENT );
4266
Gilles Peskinec5487a82018-12-03 18:08:14 +01004267 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004268 if( status != PSA_SUCCESS )
4269 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004270
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004271 if( slot->type != PSA_KEY_TYPE_DERIVE )
4272 return( PSA_ERROR_INVALID_ARGUMENT );
4273
4274 status = psa_key_derivation_internal( generator,
4275 slot->data.raw.data,
4276 slot->data.raw.bytes,
4277 alg,
4278 salt, salt_length,
4279 label, label_length,
4280 capacity );
4281 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004282 psa_generator_abort( generator );
4283 return( status );
4284}
4285
4286
4287
4288/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004289/* Key agreement */
4290/****************************************************************/
4291
Gilles Peskinea05219c2018-11-16 16:02:56 +01004292#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004293static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4294 size_t peer_key_length,
4295 const mbedtls_ecp_keypair *our_key,
4296 uint8_t *shared_secret,
4297 size_t shared_secret_size,
4298 size_t *shared_secret_length )
4299{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004300 mbedtls_ecp_keypair *their_key = NULL;
4301 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004302 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004303 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004304
Jaeden Ameroccdce902019-01-10 11:42:27 +00004305 status = psa_import_ec_public_key(
4306 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4307 peer_key, peer_key_length,
4308 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004309 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004310 goto exit;
4311
Jaeden Amero97271b32019-01-10 19:38:51 +00004312 status = mbedtls_to_psa_error(
4313 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4314 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004315 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004316 status = mbedtls_to_psa_error(
4317 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4318 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004319 goto exit;
4320
Jaeden Amero97271b32019-01-10 19:38:51 +00004321 status = mbedtls_to_psa_error(
4322 mbedtls_ecdh_calc_secret( &ecdh,
4323 shared_secret_length,
4324 shared_secret, shared_secret_size,
4325 mbedtls_ctr_drbg_random,
4326 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004327
4328exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004329 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004330 mbedtls_ecp_keypair_free( their_key );
4331 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004332 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004333}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004334#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004335
Gilles Peskine01d718c2018-09-18 12:01:02 +02004336#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4337
Gilles Peskine346797d2018-11-16 16:05:06 +01004338/* Note that if this function fails, you must call psa_generator_abort()
4339 * to potentially free embedded data structures and wipe confidential data.
4340 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004341static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004342 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004343 const uint8_t *peer_key,
4344 size_t peer_key_length,
4345 psa_algorithm_t alg )
4346{
4347 psa_status_t status;
4348 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4349 size_t shared_secret_length = 0;
4350
4351 /* Step 1: run the secret agreement algorithm to generate the shared
4352 * secret. */
4353 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4354 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004355#if defined(MBEDTLS_ECDH_C)
4356 case PSA_ALG_ECDH_BASE:
4357 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4358 return( PSA_ERROR_INVALID_ARGUMENT );
4359 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4360 private_key->data.ecp,
4361 shared_secret,
4362 sizeof( shared_secret ),
4363 &shared_secret_length );
4364 break;
4365#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004366 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004367 (void) private_key;
4368 (void) peer_key;
4369 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004370 return( PSA_ERROR_NOT_SUPPORTED );
4371 }
4372 if( status != PSA_SUCCESS )
4373 goto exit;
4374
4375 /* Step 2: set up the key derivation to generate key material from
4376 * the shared secret. */
4377 status = psa_key_derivation_internal( generator,
4378 shared_secret, shared_secret_length,
4379 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4380 NULL, 0, NULL, 0,
4381 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4382exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004383 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004384 return( status );
4385}
4386
4387psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004388 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004389 const uint8_t *peer_key,
4390 size_t peer_key_length,
4391 psa_algorithm_t alg )
4392{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004393 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004394 psa_status_t status;
4395 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4396 return( PSA_ERROR_INVALID_ARGUMENT );
4397 status = psa_get_key_from_slot( private_key, &slot,
4398 PSA_KEY_USAGE_DERIVE, alg );
4399 if( status != PSA_SUCCESS )
4400 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004401 status = psa_key_agreement_internal( generator,
4402 slot,
4403 peer_key, peer_key_length,
4404 alg );
4405 if( status != PSA_SUCCESS )
4406 psa_generator_abort( generator );
4407 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004408}
4409
4410
4411
4412/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004414/****************************************************************/
4415
4416psa_status_t psa_generate_random( uint8_t *output,
4417 size_t output_size )
4418{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004419 int ret;
4420 GUARD_MODULE_INITIALIZED;
4421
4422 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004423 return( mbedtls_to_psa_error( ret ) );
4424}
4425
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004426#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004427
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004428psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4429 size_t seed_size )
4430{
4431 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +02004432 struct psa_storage_info_t p_info;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004433 if( global_data.initialized )
4434 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004435
4436 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4437 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4438 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4439 return( PSA_ERROR_INVALID_ARGUMENT );
4440
David Saadaa2523b22019-02-18 13:56:26 +02004441 status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004442
David Saadaa2523b22019-02-18 13:56:26 +02004443 if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004444 {
David Saadaa2523b22019-02-18 13:56:26 +02004445 status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004446 }
David Saadaa2523b22019-02-18 13:56:26 +02004447 else if( PSA_SUCCESS == status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004448 {
4449 /* You should not be here. Seed needs to be injected only once */
4450 status = PSA_ERROR_NOT_PERMITTED;
4451 }
4452 return( status );
4453}
4454#endif
4455
Gilles Peskinec5487a82018-12-03 18:08:14 +01004456psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004457 psa_key_type_t type,
4458 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004459 const void *extra,
4460 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004461{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004462 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004463 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004464
Gilles Peskine53d991e2018-07-12 01:14:59 +02004465 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004466 return( PSA_ERROR_INVALID_ARGUMENT );
4467
Gilles Peskinec5487a82018-12-03 18:08:14 +01004468 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004469 if( status != PSA_SUCCESS )
4470 return( status );
4471
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004472 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004473 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004474 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004475 if( status != PSA_SUCCESS )
4476 return( status );
4477 status = psa_generate_random( slot->data.raw.data,
4478 slot->data.raw.bytes );
4479 if( status != PSA_SUCCESS )
4480 {
4481 mbedtls_free( slot->data.raw.data );
4482 return( status );
4483 }
4484#if defined(MBEDTLS_DES_C)
4485 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004486 psa_des_set_key_parity( slot->data.raw.data,
4487 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004488#endif /* MBEDTLS_DES_C */
4489 }
4490 else
4491
4492#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4493 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4494 {
4495 mbedtls_rsa_context *rsa;
4496 int ret;
4497 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004498 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4499 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004500 /* Accept only byte-aligned keys, for the same reasons as
4501 * in psa_import_rsa_key(). */
4502 if( bits % 8 != 0 )
4503 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004504 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004505 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004506 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004507 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004508 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004509#if INT_MAX < 0xffffffff
4510 /* Check that the uint32_t value passed by the caller fits
4511 * in the range supported by this implementation. */
4512 if( p->e > INT_MAX )
4513 return( PSA_ERROR_NOT_SUPPORTED );
4514#endif
4515 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004516 }
4517 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4518 if( rsa == NULL )
4519 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4520 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4521 ret = mbedtls_rsa_gen_key( rsa,
4522 mbedtls_ctr_drbg_random,
4523 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004524 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004525 exponent );
4526 if( ret != 0 )
4527 {
4528 mbedtls_rsa_free( rsa );
4529 mbedtls_free( rsa );
4530 return( mbedtls_to_psa_error( ret ) );
4531 }
4532 slot->data.rsa = rsa;
4533 }
4534 else
4535#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4536
4537#if defined(MBEDTLS_ECP_C)
4538 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4539 {
4540 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4541 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4542 const mbedtls_ecp_curve_info *curve_info =
4543 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4544 mbedtls_ecp_keypair *ecp;
4545 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004546 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004547 return( PSA_ERROR_NOT_SUPPORTED );
4548 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4549 return( PSA_ERROR_NOT_SUPPORTED );
4550 if( curve_info->bit_size != bits )
4551 return( PSA_ERROR_INVALID_ARGUMENT );
4552 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4553 if( ecp == NULL )
4554 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4555 mbedtls_ecp_keypair_init( ecp );
4556 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4557 mbedtls_ctr_drbg_random,
4558 &global_data.ctr_drbg );
4559 if( ret != 0 )
4560 {
4561 mbedtls_ecp_keypair_free( ecp );
4562 mbedtls_free( ecp );
4563 return( mbedtls_to_psa_error( ret ) );
4564 }
4565 slot->data.ecp = ecp;
4566 }
4567 else
4568#endif /* MBEDTLS_ECP_C */
4569
4570 return( PSA_ERROR_NOT_SUPPORTED );
4571
4572 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004573
4574#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4575 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4576 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004577 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004578 }
4579#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4580
4581 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004582}
4583
4584
4585/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004586/* Module setup */
4587/****************************************************************/
4588
Gilles Peskine5e769522018-11-20 21:59:56 +01004589psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4590 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4591 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4592{
4593 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4594 return( PSA_ERROR_BAD_STATE );
4595 global_data.entropy_init = entropy_init;
4596 global_data.entropy_free = entropy_free;
4597 return( PSA_SUCCESS );
4598}
4599
Gilles Peskinee59236f2018-01-27 23:32:46 +01004600void mbedtls_psa_crypto_free( void )
4601{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004602 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004603 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4604 {
4605 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004606 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004607 }
4608 /* Wipe all remaining data, including configuration.
4609 * In particular, this sets all state indicator to the value
4610 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004611 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004612}
4613
4614psa_status_t psa_crypto_init( void )
4615{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004616 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004617 const unsigned char drbg_seed[] = "PSA";
4618
Gilles Peskinec6b69072018-11-20 21:42:52 +01004619 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004620 if( global_data.initialized != 0 )
4621 return( PSA_SUCCESS );
4622
Gilles Peskine5e769522018-11-20 21:59:56 +01004623 /* Set default configuration if
4624 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4625 if( global_data.entropy_init == NULL )
4626 global_data.entropy_init = mbedtls_entropy_init;
4627 if( global_data.entropy_free == NULL )
4628 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004629
Gilles Peskinec6b69072018-11-20 21:42:52 +01004630 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004631 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004632 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004633 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004634 status = mbedtls_to_psa_error(
4635 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4636 mbedtls_entropy_func,
4637 &global_data.entropy,
4638 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4639 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004640 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004641 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004642
Gilles Peskine66fb1262018-12-10 16:29:04 +01004643 status = psa_initialize_key_slots( );
4644 if( status != PSA_SUCCESS )
4645 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004646
4647 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004648 global_data.initialized = 1;
4649
Gilles Peskinee59236f2018-01-27 23:32:46 +01004650exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004651 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004652 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004653 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004654}
4655
4656#endif /* MBEDTLS_PSA_CRYPTO_C */